1/* Configuration for double precision math routines.
2 Copyright (C) 2018-2024 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
9
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <https://www.gnu.org/licenses/>. */
18
19#ifndef _MATH_CONFIG_H
20#define _MATH_CONFIG_H
21
22#include <math.h>
23#include <math_private.h>
24#include <nan-high-order-bit.h>
25#include <stdint.h>
26
27#ifndef WANT_ROUNDING
28/* Correct special case results in non-nearest rounding modes. */
29# define WANT_ROUNDING 1
30#endif
31#ifndef WANT_ERRNO
32/* Set errno according to ISO C with (math_errhandling & MATH_ERRNO) != 0. */
33# define WANT_ERRNO 1
34#endif
35#ifndef WANT_ERRNO_UFLOW
36/* Set errno to ERANGE if result underflows to 0 (in all rounding modes). */
37# define WANT_ERRNO_UFLOW (WANT_ROUNDING && WANT_ERRNO)
38#endif
39
40#ifndef TOINT_INTRINSICS
41/* When set, the roundtoint and converttoint functions are provided with
42 the semantics documented below. */
43# define TOINT_INTRINSICS 0
44#endif
45
46static inline int
47clz_uint64 (uint64_t x)
48{
49 if (sizeof (uint64_t) == sizeof (unsigned long))
50 return __builtin_clzl (x);
51 else
52 return __builtin_clzll (x);
53}
54
55static inline int
56ctz_uint64 (uint64_t x)
57{
58 if (sizeof (uint64_t) == sizeof (unsigned long))
59 return __builtin_ctzl (x);
60 else
61 return __builtin_ctzll (x);
62}
63
64#if TOINT_INTRINSICS
65/* Round x to nearest int in all rounding modes, ties have to be rounded
66 consistently with converttoint so the results match. If the result
67 would be outside of [-2^31, 2^31-1] then the semantics is unspecified. */
68static inline double_t
69roundtoint (double_t x);
70
71/* Convert x to nearest int in all rounding modes, ties have to be rounded
72 consistently with roundtoint. If the result is not representible in an
73 int32_t then the semantics is unspecified. */
74static inline int32_t
75converttoint (double_t x);
76#endif
77
78static inline uint64_t
79asuint64 (double f)
80{
81 union
82 {
83 double f;
84 uint64_t i;
85 } u = {f};
86 return u.i;
87}
88
89static inline double
90asdouble (uint64_t i)
91{
92 union
93 {
94 uint64_t i;
95 double f;
96 } u = {i};
97 return u.f;
98}
99
100static inline int
101issignaling_inline (double x)
102{
103 uint64_t ix = asuint64 (f: x);
104 if (HIGH_ORDER_BIT_IS_SET_FOR_SNAN)
105 return (ix & 0x7ff8000000000000) == 0x7ff8000000000000;
106 return 2 * (ix ^ 0x0008000000000000) > 2 * 0x7ff8000000000000ULL;
107}
108
109#define BIT_WIDTH 64
110#define MANTISSA_WIDTH 52
111#define EXPONENT_WIDTH 11
112#define MANTISSA_MASK UINT64_C(0x000fffffffffffff)
113#define EXPONENT_MASK UINT64_C(0x7ff0000000000000)
114#define EXP_MANT_MASK UINT64_C(0x7fffffffffffffff)
115#define QUIET_NAN_MASK UINT64_C(0x0008000000000000)
116#define SIGN_MASK UINT64_C(0x8000000000000000)
117
118static inline bool
119is_nan (uint64_t x)
120{
121 return (x & EXP_MANT_MASK) > EXPONENT_MASK;
122}
123
124static inline uint64_t
125get_mantissa (uint64_t x)
126{
127 return x & MANTISSA_MASK;
128}
129
130/* Convert integer number X, unbiased exponent EP, and sign S to double:
131
132 result = X * 2^(EP+1 - exponent_bias)
133
134 NB: zero is not supported. */
135static inline double
136make_double (uint64_t x, int64_t ep, uint64_t s)
137{
138 int lz = clz_uint64 (x) - EXPONENT_WIDTH;
139 x <<= lz;
140 ep -= lz;
141
142 if (__glibc_unlikely (ep < 0 || x == 0))
143 {
144 x >>= -ep;
145 ep = 0;
146 }
147
148 return asdouble (i: s + x + (ep << MANTISSA_WIDTH));
149}
150
151/* Error handling tail calls for special cases, with a sign argument.
152 The sign of the return value is set if the argument is non-zero. */
153
154/* The result overflows. */
155attribute_hidden double __math_oflow (uint32_t);
156/* The result underflows to 0 in nearest rounding mode. */
157attribute_hidden double __math_uflow (uint32_t);
158/* The result underflows to 0 in some directed rounding mode only. */
159attribute_hidden double __math_may_uflow (uint32_t);
160/* Division by zero. */
161attribute_hidden double __math_divzero (uint32_t);
162
163/* Error handling using input checking. */
164
165/* Invalid input unless it is a quiet NaN. */
166attribute_hidden double __math_invalid (double);
167
168/* Error handling using output checking, only for errno setting. */
169
170/* Check if the result generated a demain error. */
171attribute_hidden double __math_edom (double x);
172
173/* Check if the result overflowed to infinity. */
174attribute_hidden double __math_check_oflow (double);
175/* Check if the result underflowed to 0. */
176attribute_hidden double __math_check_uflow (double);
177
178/* Check if the result overflowed to infinity. */
179static inline double
180check_oflow (double x)
181{
182 return WANT_ERRNO ? __math_check_oflow (x) : x;
183}
184
185/* Check if the result underflowed to 0. */
186static inline double
187check_uflow (double x)
188{
189 return WANT_ERRNO ? __math_check_uflow (x) : x;
190}
191
192#define EXP_TABLE_BITS 7
193#define EXP_POLY_ORDER 5
194#define EXP2_POLY_ORDER 5
195extern const struct exp_data
196{
197 double invln2N;
198 double shift;
199 double negln2hiN;
200 double negln2loN;
201 double poly[4]; /* Last four coefficients. */
202 double exp2_shift;
203 double exp2_poly[EXP2_POLY_ORDER];
204 double invlog10_2N;
205 double neglog10_2hiN;
206 double neglog10_2loN;
207 double exp10_poly[5];
208 uint64_t tab[2*(1 << EXP_TABLE_BITS)];
209} __exp_data attribute_hidden;
210
211#define LOG_TABLE_BITS 7
212#define LOG_POLY_ORDER 6
213#define LOG_POLY1_ORDER 12
214extern const struct log_data
215{
216 double ln2hi;
217 double ln2lo;
218 double poly[LOG_POLY_ORDER - 1]; /* First coefficient is 1. */
219 double poly1[LOG_POLY1_ORDER - 1];
220 /* See e_log_data.c for details. */
221 struct {double invc, logc;} tab[1 << LOG_TABLE_BITS];
222#ifndef __FP_FAST_FMA
223 struct {double chi, clo;} tab2[1 << LOG_TABLE_BITS];
224#endif
225} __log_data attribute_hidden;
226
227#define LOG2_TABLE_BITS 6
228#define LOG2_POLY_ORDER 7
229#define LOG2_POLY1_ORDER 11
230extern const struct log2_data
231{
232 double invln2hi;
233 double invln2lo;
234 double poly[LOG2_POLY_ORDER - 1];
235 double poly1[LOG2_POLY1_ORDER - 1];
236 /* See e_log2_data.c for details. */
237 struct {double invc, logc;} tab[1 << LOG2_TABLE_BITS];
238#ifndef __FP_FAST_FMA
239 struct {double chi, clo;} tab2[1 << LOG2_TABLE_BITS];
240#endif
241} __log2_data attribute_hidden;
242
243#define POW_LOG_TABLE_BITS 7
244#define POW_LOG_POLY_ORDER 8
245extern const struct pow_log_data
246{
247 double ln2hi;
248 double ln2lo;
249 double poly[POW_LOG_POLY_ORDER - 1]; /* First coefficient is 1. */
250 /* Note: the pad field is unused, but allows slightly faster indexing. */
251 /* See e_pow_log_data.c for details. */
252 struct {double invc, pad, logc, logctail;} tab[1 << POW_LOG_TABLE_BITS];
253} __pow_log_data attribute_hidden;
254
255#endif
256

source code of glibc/sysdeps/ieee754/dbl-64/math_config.h