xref: /aoo4110/main/sal/inc/sal/mathconf.h (revision b1cdbd2c)
1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 #if !defined INCLUDED_SAL_MATHCONF_H
25 #define INCLUDED_SAL_MATHCONF_H
26 
27 #include "osl/endian.h"
28 
29 #include <float.h>
30 #include <math.h>
31 
32 #if defined SOLARIS
33 #include <ieeefp.h>
34 #endif /* SOLARIS */
35 
36 #if defined __cplusplus
37 extern "C" {
38 #endif /* __cplusplus */
39 
40 
41 /* Generally, the C standard guarantees that at program startup, "trapping or
42    stopping (if supported) is disabled on all [floating-point] exceptions"
43    (F.7.3/1 of the August 3, 1998 draft of C99), and that during program
44    execution, "a programmer can safely assume default modes (or be unaware of
45    them)" (7.6/2, footnote 161 of the August 3, 1998 draft of C99).  Reportedly,
46    on Windows there are printer drivers that switch on exceptions.  To avoid
47    problems, the SAL_MATH_FPEXCEPTIONS_OFF macro can be used to explicitly
48    switch off exceptions (on Windows).
49  */
50 #if defined WNT
51 #define SAL_MATH_FPEXCEPTIONS_OFF() _control87( _MCW_EM, _MCW_EM )
52 #else /* WNT */
53 #define SAL_MATH_FPEXCEPTIONS_OFF()
54 #endif /* WNT */
55 
56 
57 /* SAL_MATH_FINITE(d): test double d on INFINITY, NaN et al. */
58 #if defined(__GNUC__) && !defined(__clang__) // workaround gcc bug 14608
59 	#if (__GNUC_MINOR__ >= 3) // gcc>=4.3 has a builtin
60 		#define SAL_MATH_FINITE(d) __builtin_isfinite(d)
61 	#else
62 		#define SAL_MATH_FINITE(d) finite(d) // fall back to pre-C99 name
63 	#endif
64 #elif defined(__STDC__)
65 	// isfinite() should be available in math.h according to C99,C++99,SUSv3,etc.
66 	// unless GCC bug 14608 hits us where cmath undefines isfinite() as macro
67 	#define SAL_MATH_FINITE(d) isfinite(d)
68 #elif defined( WNT)
69 #define SAL_MATH_FINITE(d) _finite(d)
70 #elif defined OS2
71 #define SAL_MATH_FINITE(d) finite(d)
72 #elif defined LINUX || defined UNX
73 #define SAL_MATH_FINITE(d) finite(d)
74 #else /* WNT, LINUX, UNX */
75 #error "SAL_MATH_FINITE not defined"
76 #endif /* WNT, LINUX, UNX */
77 
78 
79 /* This needs to be fixed for non--IEEE-754 platforms: */
80 #if 1 /* IEEE 754 supported */
81 #if defined OSL_BIGENDIAN
82 
83 /* IEEE 754 double structures for BigEndian */
84 union sal_math_Double
85 {
86     struct
87     {
88         unsigned sign         : 1;
89         unsigned exponent     :11;
90         unsigned fraction_hi  :20;
91         unsigned fraction_lo  :32;
92     } inf_parts;
93     struct
94     {
95         unsigned sign         : 1;
96         unsigned exponent     :11;
97         unsigned qnan_bit     : 1;
98         unsigned bits         :19;
99         unsigned fraction_lo  :32;
100     } nan_parts;
101     struct
102     {
103         unsigned msw          :32;
104         unsigned lsw          :32;
105     } w32_parts;
106     double value;
107 };
108 
109 #elif defined OSL_LITENDIAN
110 
111 /* IEEE 754 double structures for LittleEndian */
112 union sal_math_Double
113 {
114     struct {
115         unsigned fraction_lo  :32;
116         unsigned fraction_hi  :20;
117         unsigned exponent     :11;
118         unsigned sign         : 1;
119     } inf_parts;
120     struct {
121         unsigned fraction_lo  :32;
122         unsigned bits         :19;
123         unsigned qnan_bit     : 1;
124         unsigned exponent     :11;
125         unsigned sign         : 1;
126     } nan_parts;
127     struct
128     {
129         unsigned lsw          :32;
130         unsigned msw          :32;
131     } w32_parts;
132     double value;
133 };
134 
135 #else /* OSL_BIGENDIAN, OSL_LITENDIAN */
136 
137 #error "neither OSL_BIGENDIAN nor OSL_LITENDIAN"
138 
139 #endif /* OSL_BIGENDIAN, OSL_LITENDIAN */
140 #else /* IEEE 754 supported */
141 
142 #error "don't know how to handle IEEE 754"
143 
144 #endif /* IEEE 754 supported */
145 
146 
147 #if defined __cplusplus
148 }
149 #endif /* __cplusplus */
150 
151 #endif /* INCLUDED_SAL_MATHCONF_H */
152