1 //   Copyright Naoki Shibata and contributors 2010 - 2020.
2 // Distributed under the Boost Software License, Version 1.0.
3 //    (See accompanying file LICENSE.txt or copy at
4 //          http://www.boost.org/LICENSE_1_0.txt)
5 
6 //
7 
8 #ifndef __MISC_H__
9 #define __MISC_H__
10 
11 #if !defined(SLEEF_GENHEADER)
12 #include <stdint.h>
13 #include <string.h>
14 #endif
15 
16 #ifndef M_PI
17 #define M_PI 3.141592653589793238462643383279502884
18 #endif
19 
20 #ifndef M_PIl
21 #define M_PIl 3.141592653589793238462643383279502884L
22 #endif
23 
24 #ifndef M_1_PI
25 #define M_1_PI 0.318309886183790671537767526745028724
26 #endif
27 
28 #ifndef M_1_PIl
29 #define M_1_PIl 0.318309886183790671537767526745028724L
30 #endif
31 
32 #ifndef M_2_PI
33 #define M_2_PI 0.636619772367581343075535053490057448
34 #endif
35 
36 #ifndef M_2_PIl
37 #define M_2_PIl 0.636619772367581343075535053490057448L
38 #endif
39 
40 #ifndef SLEEF_FP_ILOGB0
41 #define SLEEF_FP_ILOGB0 ((int)-2147483648)
42 #endif
43 
44 #ifndef SLEEF_FP_ILOGBNAN
45 #define SLEEF_FP_ILOGBNAN ((int)2147483647)
46 #endif
47 
48 #define SLEEF_SNAN (((union { long long int i; double d; }) { .i = 0x7ff0000000000001LL }).d)
49 #define SLEEF_SNANf (((union { long int i; float f; }) { .i = 0xff800001 }).f)
50 
51 
52 //
53 
54 /*
55   PI_A to PI_D are constants that satisfy the following two conditions.
56 
57   * For PI_A, PI_B and PI_C, the last 28 bits are zero.
58   * PI_A + PI_B + PI_C + PI_D is close to PI as much as possible.
59 
60   The argument of a trig function is multiplied by 1/PI, and the
61   integral part is divided into two parts, each has at most 28
62   bits. So, the maximum argument that could be correctly reduced
63   should be 2^(28*2-1) PI = 1.1e+17. However, due to internal
64   double precision calculation, the actual maximum argument that can
65   be correctly reduced is around 2^47.
66  */
67 
68 #define PI_A 3.1415926218032836914
69 #define PI_B 3.1786509424591713469e-08
70 #define PI_C 1.2246467864107188502e-16
71 #define PI_D 1.2736634327021899816e-24
72 #define TRIGRANGEMAX 1e+14
73 
74 /*
75   PI_A2 and PI_B2 are constants that satisfy the following two conditions.
76 
77   * The last 3 bits of PI_A2 are zero.
78   * PI_A2 + PI_B2 is close to PI as much as possible.
79 
80   The argument of a trig function is multiplied by 1/PI, and the
81   integral part is multiplied by PI_A2. So, the maximum argument that
82   could be correctly reduced should be 2^(3-1) PI = 12.6. By testing,
83   we confirmed that it correctly reduces the argument up to around 15.
84  */
85 
86 #define PI_A2 3.141592653589793116
87 #define PI_B2 1.2246467991473532072e-16
88 #define TRIGRANGEMAX2 15
89 
90 #define M_2_PI_H 0.63661977236758138243
91 #define M_2_PI_L -3.9357353350364971764e-17
92 
93 #define SQRT_DBL_MAX 1.3407807929942596355e+154
94 
95 #define TRIGRANGEMAX3 1e+9
96 
97 #define M_4_PI 1.273239544735162542821171882678754627704620361328125
98 
99 #define L2U .69314718055966295651160180568695068359375
100 #define L2L .28235290563031577122588448175013436025525412068e-12
101 #define R_LN2 1.442695040888963407359924681001892137426645954152985934135449406931
102 
103 #define L10U 0.30102999566383914498 // log 2 / log 10
104 #define L10L 1.4205023227266099418e-13
105 #define LOG10_2 3.3219280948873623478703194294893901758648313930
106 
107 #define L10Uf 0.3010253906f
108 #define L10Lf 4.605038981e-06f
109 
110 //
111 
112 #define PI_Af 3.140625f
113 #define PI_Bf 0.0009670257568359375f
114 #define PI_Cf 6.2771141529083251953e-07f
115 #define PI_Df 1.2154201256553420762e-10f
116 #define TRIGRANGEMAXf 39000
117 
118 #define PI_A2f 3.1414794921875f
119 #define PI_B2f 0.00011315941810607910156f
120 #define PI_C2f 1.9841872589410058936e-09f
121 #define TRIGRANGEMAX2f 125.0f
122 
123 #define TRIGRANGEMAX4f 8e+6f
124 
125 #define SQRT_FLT_MAX 18446743523953729536.0
126 
127 #define L2Uf 0.693145751953125f
128 #define L2Lf 1.428606765330187045e-06f
129 
130 #define R_LN2f 1.442695040888963407359924681001892137426645954152985934135449406931f
131 #define M_PIf ((float)M_PI)
132 
133 //
134 
135 #ifndef MIN
136 #define MIN(x, y) ((x) < (y) ? (x) : (y))
137 #endif
138 
139 #ifndef MAX
140 #define MAX(x, y) ((x) > (y) ? (x) : (y))
141 #endif
142 
143 #ifndef ABS
144 #define ABS(x) ((x) < 0 ? -(x) : (x))
145 #endif
146 
147 #define stringify(s) stringify_(s)
148 #define stringify_(s) #s
149 
150 #if !defined(SLEEF_GENHEADER)
151 typedef long double longdouble;
152 #endif
153 
154 #if !defined(Sleef_double2_DEFINED) && !defined(SLEEF_GENHEADER)
155 #define Sleef_double2_DEFINED
156 typedef struct {
157   double x, y;
158 } Sleef_double2;
159 #endif
160 
161 #if !defined(Sleef_float2_DEFINED) && !defined(SLEEF_GENHEADER)
162 #define Sleef_float2_DEFINED
163 typedef struct {
164   float x, y;
165 } Sleef_float2;
166 #endif
167 
168 #if !defined(Sleef_longdouble2_DEFINED) && !defined(SLEEF_GENHEADER)
169 #define Sleef_longdouble2_DEFINED
170 typedef struct {
171   long double x, y;
172 } Sleef_longdouble2;
173 #endif
174 
175 #if !defined(Sleef_quad_DEFINED) && !defined(SLEEF_GENHEADER)
176 #define Sleef_quad_DEFINED
177 #if defined(ENABLEFLOAT128)
178 typedef __float128 Sleef_quad;
179 #else
180 typedef struct { double x, y; } Sleef_quad;
181 #endif
182 #endif
183 
184 #if !defined(Sleef_quad1_DEFINED) && !defined(SLEEF_GENHEADER)
185 #define Sleef_quad1_DEFINED
186 typedef union {
187   struct {
188     Sleef_quad x;
189   };
190   Sleef_quad s[1];
191 } Sleef_quad1;
192 #endif
193 
194 #if !defined(Sleef_quad2_DEFINED) && !defined(SLEEF_GENHEADER)
195 #define Sleef_quad2_DEFINED
196 typedef union {
197   struct {
198     Sleef_quad x, y;
199   };
200   Sleef_quad s[2];
201 } Sleef_quad2;
202 #endif
203 
204 #if !defined(Sleef_quad4_DEFINED) && !defined(SLEEF_GENHEADER)
205 #define Sleef_quad4_DEFINED
206 typedef union {
207   struct {
208     Sleef_quad x, y, z, w;
209   };
210   Sleef_quad s[4];
211 } Sleef_quad4;
212 #endif
213 
214 #if !defined(Sleef_quad8_DEFINED) && !defined(SLEEF_GENHEADER)
215 #define Sleef_quad8_DEFINED
216 typedef union {
217   Sleef_quad s[8];
218 } Sleef_quad8;
219 #endif
220 
221 #if defined(__ARM_FEATURE_SVE) && !defined(Sleef_quadx_DEFINED) && !defined(SLEEF_GENHEADER)
222 #define Sleef_quadx_DEFINED
223 typedef union {
224   Sleef_quad s[32];
225 } Sleef_quadx;
226 #endif
227 
228 //
229 
230 #if (defined (__GNUC__) || defined (__clang__) || defined(__INTEL_COMPILER)) && !defined(_MSC_VER)
231 
232 #define LIKELY(condition) __builtin_expect(!!(condition), 1)
233 #define UNLIKELY(condition) __builtin_expect(!!(condition), 0)
234 #define RESTRICT __restrict__
235 
236 #ifndef __arm__
237 #define ALIGNED(x) __attribute__((aligned(x)))
238 #else
239 #define ALIGNED(x)
240 #endif
241 
242 #if defined(SLEEF_GENHEADER)
243 
244 #define INLINE SLEEF_ALWAYS_INLINE
245 #define EXPORT SLEEF_INLINE
246 #define CONST SLEEF_CONST
247 #define NOEXPORT
248 
249 #else // #if defined(SLEEF_GENHEADER)
250 
251 #ifndef __INTEL_COMPILER
252 #define CONST const
253 #else
254 #define CONST
255 #endif
256 #define INLINE __attribute__((always_inline))
257 
258 #if defined(__MINGW32__) || defined(__MINGW64__) || defined(__CYGWIN__)
259 #ifndef SLEEF_STATIC_LIBS
260 #define EXPORT __stdcall __declspec(dllexport)
261 #define NOEXPORT
262 #else // #ifndef SLEEF_STATIC_LIBS
263 #define EXPORT
264 #define NOEXPORT
265 #endif // #ifndef SLEEF_STATIC_LIBS
266 #else // #if defined(__MINGW32__) || defined(__MINGW64__) || defined(__CYGWIN__)
267 #define EXPORT __attribute__((visibility("default")))
268 #define NOEXPORT __attribute__ ((visibility ("hidden")))
269 #endif // #if defined(__MINGW32__) || defined(__MINGW64__) || defined(__CYGWIN__)
270 
271 #endif // #if defined(SLEEF_GENHEADER)
272 
273 #define SLEEF_NAN __builtin_nan("")
274 #define SLEEF_NANf __builtin_nanf("")
275 #define SLEEF_NANl __builtin_nanl("")
276 #define SLEEF_INFINITY __builtin_inf()
277 #define SLEEF_INFINITYf __builtin_inff()
278 #define SLEEF_INFINITYl __builtin_infl()
279 
280 #if defined(__INTEL_COMPILER) || defined (__clang__)
281 #define SLEEF_INFINITYq __builtin_inf()
282 #define SLEEF_NANq __builtin_nan("")
283 #else
284 #define SLEEF_INFINITYq __builtin_infq()
285 #define SLEEF_NANq (SLEEF_INFINITYq - SLEEF_INFINITYq)
286 #endif
287 
288 #elif defined(_MSC_VER) // #if (defined (__GNUC__) || defined (__clang__) || defined(__INTEL_COMPILER)) && !defined(_MSC_VER)
289 
290 #define INLINE __forceinline
291 #define CONST
292 #define RESTRICT
293 #define ALIGNED(x)
294 #define LIKELY(condition) (condition)
295 #define UNLIKELY(condition) (condition)
296 
297 #ifndef SLEEF_STATIC_LIBS
298 #define EXPORT __declspec(dllexport)
299 #define NOEXPORT
300 #else
301 #define EXPORT
302 #define NOEXPORT
303 #endif
304 
305 #if (defined(__GNUC__) || defined(__CLANG__)) && (defined(__i386__) || defined(__x86_64__)) && !defined(SLEEF_GENHEADER)
306 #include <x86intrin.h>
307 #endif
308 
309 #define SLEEF_INFINITY (1e+300 * 1e+300)
310 #define SLEEF_NAN (SLEEF_INFINITY - SLEEF_INFINITY)
311 #define SLEEF_INFINITYf ((float)SLEEF_INFINITY)
312 #define SLEEF_NANf ((float)SLEEF_NAN)
313 #define SLEEF_INFINITYl ((long double)SLEEF_INFINITY)
314 #define SLEEF_NANl ((long double)SLEEF_NAN)
315 
316 #if (defined(_M_AMD64) || defined(_M_X64))
317 #ifndef __SSE2__
318 #define __SSE2__
319 #define __SSE3__
320 #define __SSE4_1__
321 #endif
322 #elif _M_IX86_FP == 2
323 #ifndef __SSE2__
324 #define __SSE2__
325 #define __SSE3__
326 #define __SSE4_1__
327 #endif
328 #elif _M_IX86_FP == 1
329 #ifndef __SSE__
330 #define __SSE__
331 #endif
332 #endif
333 
334 #endif // #elif defined(_MSC_VER) // #if (defined (__GNUC__) || defined (__clang__) || defined(__INTEL_COMPILER)) && !defined(_MSC_VER)
335 
336 #if !defined(__linux__)
337 #define isinff(x) ((x) == SLEEF_INFINITYf || (x) == -SLEEF_INFINITYf)
338 #define isinfl(x) ((x) == SLEEF_INFINITYl || (x) == -SLEEF_INFINITYl)
339 #define isnanf(x) ((x) != (x))
340 #define isnanl(x) ((x) != (x))
341 #endif
342 
343 #endif // #ifndef __MISC_H__
344 
345 #ifdef ENABLE_AAVPCS
346 #define VECTOR_CC __attribute__((aarch64_vector_pcs))
347 #else
348 #define VECTOR_CC
349 #endif
350