Cygwin: mmap: allow remapping part of an existing anonymous mapping
[newlib-cygwin.git] / newlib / libm / common / math_config.h
blobbf881e81b8190441154a94008b20e307437145ad
1 /* Configuration for math routines.
2 Copyright (c) 2017-2018 Arm Ltd. All rights reserved.
4 SPDX-License-Identifier: BSD-3-Clause
6 Redistribution and use in source and binary forms, with or without
7 modification, are permitted provided that the following conditions
8 are met:
9 1. Redistributions of source code must retain the above copyright
10 notice, this list of conditions and the following disclaimer.
11 2. Redistributions in binary form must reproduce the above copyright
12 notice, this list of conditions and the following disclaimer in the
13 documentation and/or other materials provided with the distribution.
14 3. The name of the company may not be used to endorse or promote
15 products derived from this software without specific prior written
16 permission.
18 THIS SOFTWARE IS PROVIDED BY ARM LTD ``AS IS'' AND ANY EXPRESS OR IMPLIED
19 WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
20 MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 IN NO EVENT SHALL ARM LTD BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
23 TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
24 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
25 LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
26 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */
29 #ifndef _MATH_CONFIG_H
30 #define _MATH_CONFIG_H
32 #include <math.h>
33 #include <stdint.h>
35 #ifndef WANT_ROUNDING
36 /* Correct special case results in non-nearest rounding modes. */
37 # define WANT_ROUNDING 1
38 #endif
39 #ifdef _IEEE_LIBM
40 # define WANT_ERRNO 0
41 # define _LIB_VERSION _IEEE_
42 #else
43 /* Set errno according to ISO C with (math_errhandling & MATH_ERRNO) != 0. */
44 # define WANT_ERRNO 1
45 # define _LIB_VERSION _POSIX_
46 #endif
47 #ifndef WANT_ERRNO_UFLOW
48 /* Set errno to ERANGE if result underflows to 0 (in all rounding modes). */
49 # define WANT_ERRNO_UFLOW (WANT_ROUNDING && WANT_ERRNO)
50 #endif
52 #define _IEEE_ -1
53 #define _POSIX_ 0
55 /* Compiler can inline round as a single instruction. */
56 #ifndef HAVE_FAST_ROUND
57 # if __aarch64__
58 # define HAVE_FAST_ROUND 1
59 # else
60 # define HAVE_FAST_ROUND 0
61 # endif
62 #endif
64 /* Compiler can inline lround, but not (long)round(x). */
65 #ifndef HAVE_FAST_LROUND
66 # if __aarch64__ && (100*__GNUC__ + __GNUC_MINOR__) >= 408 && __NO_MATH_ERRNO__
67 # define HAVE_FAST_LROUND 1
68 # else
69 # define HAVE_FAST_LROUND 0
70 # endif
71 #endif
73 /* Compiler can inline fma as a single instruction. */
74 #ifndef HAVE_FAST_FMA
75 # if __aarch64__ || (__ARM_FEATURE_FMA && (__ARM_FP & 8)) \
76 || __riscv_flen >= 64 || defined (__riscv_zdinx)
77 # define HAVE_FAST_FMA 1
78 # else
79 # define HAVE_FAST_FMA 0
80 # endif
81 #endif
83 #ifndef HAVE_FAST_FMAF
84 # if HAVE_FAST_FMA || (__ARM_FEATURE_FMA && (__ARM_FP & 4)) \
85 || __riscv_flen >= 32 || defined (__riscv_zfinx)
86 # define HAVE_FAST_FMAF 1
87 # else
88 # define HAVE_FAST_FMAF 0
89 # endif
90 #endif
92 #if HAVE_FAST_ROUND
93 /* When set, the roundtoint and converttoint functions are provided with
94 the semantics documented below. */
95 # define TOINT_INTRINSICS 1
97 /* Round x to nearest int in all rounding modes, ties have to be rounded
98 consistently with converttoint so the results match. If the result
99 would be outside of [-2^31, 2^31-1] then the semantics is unspecified. */
100 static inline double_t
101 roundtoint (double_t x)
103 return round (x);
106 /* Convert x to nearest int in all rounding modes, ties have to be rounded
107 consistently with roundtoint. If the result is not representible in an
108 int32_t then the semantics is unspecified. */
109 static inline int32_t
110 converttoint (double_t x)
112 # if HAVE_FAST_LROUND
113 return lround (x);
114 # else
115 return (long) round (x);
116 # endif
118 #endif
120 #ifndef TOINT_INTRINSICS
121 # define TOINT_INTRINSICS 0
122 #endif
124 static inline uint32_t
125 asuint (float f)
127 union
129 float f;
130 uint32_t i;
131 } u = {f};
132 return u.i;
135 static inline float
136 asfloat (uint32_t i)
138 union
140 uint32_t i;
141 float f;
142 } u = {i};
143 return u.f;
146 static inline uint64_t
147 asuint64 (double f)
149 union
151 double f;
152 uint64_t i;
153 } u = {f};
154 return u.i;
157 static inline double
158 asdouble (uint64_t i)
160 union
162 uint64_t i;
163 double f;
164 } u = {i};
165 return u.f;
168 #ifndef IEEE_754_2008_SNAN
169 # define IEEE_754_2008_SNAN 1
170 #endif
171 static inline int
172 issignalingf_inline (float x)
174 uint32_t ix = asuint (x);
175 if (!IEEE_754_2008_SNAN)
176 return (ix & 0x7fc00000) == 0x7fc00000;
177 return 2 * (ix ^ 0x00400000) > 0xFF800000u;
180 static inline int
181 issignaling_inline (double x)
183 uint64_t ix = asuint64 (x);
184 if (!IEEE_754_2008_SNAN)
185 return (ix & 0x7ff8000000000000) == 0x7ff8000000000000;
186 return 2 * (ix ^ 0x0008000000000000) > 2 * 0x7ff8000000000000ULL;
189 #if __aarch64__ && __GNUC__
190 /* Prevent the optimization of a floating-point expression. */
191 static inline float
192 opt_barrier_float (float x)
194 __asm__ __volatile__ ("" : "+w" (x));
195 return x;
197 static inline double
198 opt_barrier_double (double x)
200 __asm__ __volatile__ ("" : "+w" (x));
201 return x;
203 /* Force the evaluation of a floating-point expression for its side-effect. */
204 static inline void
205 force_eval_float (float x)
207 __asm__ __volatile__ ("" : "+w" (x));
209 static inline void
210 force_eval_double (double x)
212 __asm__ __volatile__ ("" : "+w" (x));
214 #else
215 static inline float
216 opt_barrier_float (float x)
218 volatile float y = x;
219 return y;
221 static inline double
222 opt_barrier_double (double x)
224 volatile double y = x;
225 return y;
227 #pragma GCC diagnostic ignored "-Wunused-variable"
228 static inline void
229 force_eval_float (float x)
231 volatile float y = x;
233 static inline void
234 force_eval_double (double x)
236 volatile double y = x;
238 #pragma GCC diagnostic pop
239 #endif
241 /* Evaluate an expression as the specified type, normally a type
242 cast should be enough, but compilers implement non-standard
243 excess-precision handling, so when FLT_EVAL_METHOD != 0 then
244 these functions may need to be customized. */
245 static inline float
246 eval_as_float (float x)
248 return x;
250 static inline double
251 eval_as_double (double x)
253 return x;
256 #ifdef __GNUC__
257 # define NOINLINE __attribute__ ((noinline))
258 # define likely(x) __builtin_expect (!!(x), 1)
259 # define unlikely(x) __builtin_expect (x, 0)
260 #else
261 # define NOINLINE
262 # define likely(x) (x)
263 # define unlikely(x) (x)
264 #endif
266 /* gcc emitting PE/COFF doesn't support visibility */
267 #if defined (__GNUC__) && !defined (__CYGWIN__)
268 # define HIDDEN __attribute__ ((__visibility__ ("hidden")))
269 #else
270 # define HIDDEN
271 #endif
273 /* Error handling tail calls for special cases, with a sign argument.
274 The sign of the return value is set if the argument is non-zero. */
276 /* The result overflows. */
277 HIDDEN float __math_oflowf (uint32_t);
278 /* The result underflows to 0 in nearest rounding mode. */
279 HIDDEN float __math_uflowf (uint32_t);
280 /* The result underflows to 0 in some directed rounding mode only. */
281 HIDDEN float __math_may_uflowf (uint32_t);
282 /* Division by zero. */
283 HIDDEN float __math_divzerof (uint32_t);
284 /* The result overflows. */
285 HIDDEN double __math_oflow (uint32_t);
286 /* The result underflows to 0 in nearest rounding mode. */
287 HIDDEN double __math_uflow (uint32_t);
288 /* The result underflows to 0 in some directed rounding mode only. */
289 HIDDEN double __math_may_uflow (uint32_t);
290 /* Division by zero. */
291 HIDDEN double __math_divzero (uint32_t);
293 /* Error handling using input checking. */
295 /* Invalid input unless it is a quiet NaN. */
296 HIDDEN float __math_invalidf (float);
297 /* Invalid input unless it is a quiet NaN. */
298 HIDDEN double __math_invalid (double);
300 /* Error handling using output checking, only for errno setting. */
302 /* Check if the result overflowed to infinity. */
303 HIDDEN double __math_check_oflow (double);
304 /* Check if the result underflowed to 0. */
305 HIDDEN double __math_check_uflow (double);
307 /* Check if the result overflowed to infinity. */
308 static inline double
309 check_oflow (double x)
311 return WANT_ERRNO ? __math_check_oflow (x) : x;
314 /* Check if the result underflowed to 0. */
315 static inline double
316 check_uflow (double x)
318 return WANT_ERRNO ? __math_check_uflow (x) : x;
321 /* Shared between expf, exp2f and powf. */
322 #define EXP2F_TABLE_BITS 5
323 #define EXP2F_POLY_ORDER 3
324 extern const struct exp2f_data
326 uint64_t tab[1 << EXP2F_TABLE_BITS];
327 double shift_scaled;
328 double poly[EXP2F_POLY_ORDER];
329 double shift;
330 double invln2_scaled;
331 double poly_scaled[EXP2F_POLY_ORDER];
332 } __exp2f_data HIDDEN;
334 #define LOGF_TABLE_BITS 4
335 #define LOGF_POLY_ORDER 4
336 extern const struct logf_data
338 struct
340 double invc, logc;
341 } tab[1 << LOGF_TABLE_BITS];
342 double ln2;
343 double poly[LOGF_POLY_ORDER - 1]; /* First order coefficient is 1. */
344 } __logf_data HIDDEN;
346 #define LOG2F_TABLE_BITS 4
347 #define LOG2F_POLY_ORDER 4
348 extern const struct log2f_data
350 struct
352 double invc, logc;
353 } tab[1 << LOG2F_TABLE_BITS];
354 double poly[LOG2F_POLY_ORDER];
355 } __log2f_data HIDDEN;
357 #define POWF_LOG2_TABLE_BITS 4
358 #define POWF_LOG2_POLY_ORDER 5
359 #if TOINT_INTRINSICS
360 # define POWF_SCALE_BITS EXP2F_TABLE_BITS
361 #else
362 # define POWF_SCALE_BITS 0
363 #endif
364 #define POWF_SCALE ((double) (1 << POWF_SCALE_BITS))
365 extern const struct powf_log2_data
367 struct
369 double invc, logc;
370 } tab[1 << POWF_LOG2_TABLE_BITS];
371 double poly[POWF_LOG2_POLY_ORDER];
372 } __powf_log2_data HIDDEN;
374 #define EXP_TABLE_BITS 7
375 #define EXP_POLY_ORDER 5
376 /* Use polynomial that is optimized for a wider input range. This may be
377 needed for good precision in non-nearest rounding and !TOINT_INTRINSICS. */
378 #define EXP_POLY_WIDE 0
379 /* Use close to nearest rounding toint when !TOINT_INTRINSICS. This may be
380 needed for good precision in non-nearest rouning and !EXP_POLY_WIDE. */
381 #define EXP_USE_TOINT_NARROW 0
382 #define EXP2_POLY_ORDER 5
383 #define EXP2_POLY_WIDE 0
384 extern const struct exp_data
386 double invln2N;
387 double shift;
388 double negln2hiN;
389 double negln2loN;
390 double poly[4]; /* Last four coefficients. */
391 double exp2_shift;
392 double exp2_poly[EXP2_POLY_ORDER];
393 uint64_t tab[2*(1 << EXP_TABLE_BITS)];
394 } __exp_data HIDDEN;
396 #define LOG_TABLE_BITS 7
397 #define LOG_POLY_ORDER 6
398 #define LOG_POLY1_ORDER 12
399 extern const struct log_data
401 double ln2hi;
402 double ln2lo;
403 double poly[LOG_POLY_ORDER - 1]; /* First coefficient is 1. */
404 double poly1[LOG_POLY1_ORDER - 1];
405 struct {double invc, logc;} tab[1 << LOG_TABLE_BITS];
406 #if !HAVE_FAST_FMA
407 struct {double chi, clo;} tab2[1 << LOG_TABLE_BITS];
408 #endif
409 } __log_data HIDDEN;
411 #define LOG2_TABLE_BITS 6
412 #define LOG2_POLY_ORDER 7
413 #define LOG2_POLY1_ORDER 11
414 extern const struct log2_data
416 double invln2hi;
417 double invln2lo;
418 double poly[LOG2_POLY_ORDER - 1];
419 double poly1[LOG2_POLY1_ORDER - 1];
420 struct {double invc, logc;} tab[1 << LOG2_TABLE_BITS];
421 #if !HAVE_FAST_FMA
422 struct {double chi, clo;} tab2[1 << LOG2_TABLE_BITS];
423 #endif
424 } __log2_data HIDDEN;
426 #define POW_LOG_TABLE_BITS 7
427 #define POW_LOG_POLY_ORDER 8
428 extern const struct pow_log_data
430 double ln2hi;
431 double ln2lo;
432 double poly[POW_LOG_POLY_ORDER - 1]; /* First coefficient is 1. */
433 /* Note: the pad field is unused, but allows slightly faster indexing. */
434 struct {double invc, pad, logc, logctail;} tab[1 << POW_LOG_TABLE_BITS];
435 } __pow_log_data HIDDEN;
437 #endif