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
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
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
36 /* Correct special case results in non-nearest rounding modes. */
37 # define WANT_ROUNDING 1
41 # define _LIB_VERSION _IEEE_
43 /* Set errno according to ISO C with (math_errhandling & MATH_ERRNO) != 0. */
45 # define _LIB_VERSION _POSIX_
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)
55 /* Compiler can inline round as a single instruction. */
56 #ifndef HAVE_FAST_ROUND
58 # define HAVE_FAST_ROUND 1
60 # define HAVE_FAST_ROUND 0
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
69 # define HAVE_FAST_LROUND 0
73 /* Compiler can inline fma as a single instruction. */
75 # if __aarch64__ || (__ARM_FEATURE_FMA && (__ARM_FP & 8)) \
76 || __riscv_flen >= 64 || defined (__riscv_zdinx)
77 # define HAVE_FAST_FMA 1
79 # define HAVE_FAST_FMA 0
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
88 # define HAVE_FAST_FMAF 0
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
)
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
115 return (long) round (x
);
120 #ifndef TOINT_INTRINSICS
121 # define TOINT_INTRINSICS 0
124 static inline uint32_t
146 static inline uint64_t
158 asdouble (uint64_t i
)
168 #ifndef IEEE_754_2008_SNAN
169 # define IEEE_754_2008_SNAN 1
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
;
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. */
192 opt_barrier_float (float x
)
194 __asm__
__volatile__ ("" : "+w" (x
));
198 opt_barrier_double (double x
)
200 __asm__
__volatile__ ("" : "+w" (x
));
203 /* Force the evaluation of a floating-point expression for its side-effect. */
205 force_eval_float (float x
)
207 __asm__
__volatile__ ("" : "+w" (x
));
210 force_eval_double (double x
)
212 __asm__
__volatile__ ("" : "+w" (x
));
216 opt_barrier_float (float x
)
218 volatile float y
= x
;
222 opt_barrier_double (double x
)
224 volatile double y
= x
;
227 #pragma GCC diagnostic ignored "-Wunused-variable"
229 force_eval_float (float x
)
231 volatile float y
= x
;
234 force_eval_double (double x
)
236 volatile double y
= x
;
238 #pragma GCC diagnostic pop
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. */
246 eval_as_float (float x
)
251 eval_as_double (double x
)
257 # define NOINLINE __attribute__ ((noinline))
258 # define likely(x) __builtin_expect (!!(x), 1)
259 # define unlikely(x) __builtin_expect (x, 0)
262 # define likely(x) (x)
263 # define unlikely(x) (x)
266 /* gcc emitting PE/COFF doesn't support visibility */
267 #if defined (__GNUC__) && !defined (__CYGWIN__)
268 # define HIDDEN __attribute__ ((__visibility__ ("hidden")))
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. */
309 check_oflow (double x
)
311 return WANT_ERRNO
? __math_check_oflow (x
) : x
;
314 /* Check if the result underflowed to 0. */
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
];
328 double poly
[EXP2F_POLY_ORDER
];
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
341 } tab
[1 << LOGF_TABLE_BITS
];
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
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
360 # define POWF_SCALE_BITS EXP2F_TABLE_BITS
362 # define POWF_SCALE_BITS 0
364 #define POWF_SCALE ((double) (1 << POWF_SCALE_BITS))
365 extern const struct powf_log2_data
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
390 double poly
[4]; /* Last four coefficients. */
392 double exp2_poly
[EXP2_POLY_ORDER
];
393 uint64_t tab
[2*(1 << EXP_TABLE_BITS
)];
396 #define LOG_TABLE_BITS 7
397 #define LOG_POLY_ORDER 6
398 #define LOG_POLY1_ORDER 12
399 extern const struct log_data
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
];
407 struct {double chi
, clo
;} tab2
[1 << LOG_TABLE_BITS
];
411 #define LOG2_TABLE_BITS 6
412 #define LOG2_POLY_ORDER 7
413 #define LOG2_POLY1_ORDER 11
414 extern const struct log2_data
418 double poly
[LOG2_POLY_ORDER
- 1];
419 double poly1
[LOG2_POLY1_ORDER
- 1];
420 struct {double invc
, logc
;} tab
[1 << LOG2_TABLE_BITS
];
422 struct {double chi
, clo
;} tab2
[1 << LOG2_TABLE_BITS
];
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
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
;