xref: /llvm-project/libc/src/math/generic/common_constants.h (revision 5ff3ff33ff930e4ec49da7910612d8a41eb068cb)
163d2df00STue Ly //===-- Common constants for math functions ---------------------*- C++ -*-===//
263d2df00STue Ly //
363d2df00STue Ly // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
463d2df00STue Ly // See https://llvm.org/LICENSE.txt for license information.
563d2df00STue Ly // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
663d2df00STue Ly //
763d2df00STue Ly //===----------------------------------------------------------------------===//
863d2df00STue Ly 
963d2df00STue Ly #ifndef LLVM_LIBC_SRC_MATH_GENERIC_COMMON_CONSTANTS_H
1063d2df00STue Ly #define LLVM_LIBC_SRC_MATH_GENERIC_COMMON_CONSTANTS_H
1163d2df00STue Ly 
128ca614aaSTue Ly #include "src/__support/FPUtil/triple_double.h"
13*5ff3ff33SPetr Hosek #include "src/__support/macros/config.h"
14a0c92a38STue Ly #include "src/__support/number_pair.h"
15a0c92a38STue Ly 
16*5ff3ff33SPetr Hosek namespace LIBC_NAMESPACE_DECL {
1763d2df00STue Ly 
1863d2df00STue Ly // Lookup table for (1/f) where f = 1 + n*2^(-7), n = 0..127.
1963d2df00STue Ly extern const double ONE_OVER_F[128];
2063d2df00STue Ly 
219e7688c7STue Ly // Lookup table for log(f) = log(1 + n*2^(-7)) where n = 0..127.
229e7688c7STue Ly extern const double LOG_F[128];
239e7688c7STue Ly 
249af8dca7STue Ly // Lookup table for range reduction constants r for logarithms.
259af8dca7STue Ly extern const float R[128];
269af8dca7STue Ly 
27bc8e87efSTue Ly // Lookup table for range reduction constants r for logarithms.
28bc8e87efSTue Ly extern const double RD[128];
29bc8e87efSTue Ly 
30a0c92a38STue Ly // Lookup table for compensated constants for exact range reduction when FMA
31a0c92a38STue Ly // instructions are not available.
32a0c92a38STue Ly extern const double CD[128];
33a0c92a38STue Ly 
34bc8e87efSTue Ly // Lookup table for -log(r)
35bc8e87efSTue Ly extern const double LOG_R[128];
36a0c92a38STue Ly extern const NumberPair<double> LOG_R_DD[128];
37a0c92a38STue Ly 
38bc7a3bd8Slntue // Lookup table for -log2(r)
39bc7a3bd8Slntue extern const double LOG2_R[128];
40bc7a3bd8Slntue 
41a0c92a38STue Ly // Minimax polynomial for (log(1 + x) - x)/x^2, generated by sollya with:
42a0c92a38STue Ly // > P = fpminimax((log(1 + x) - x)/x^2, 5, [|D...|], [-2^-8, 2^-7]);
43a0c92a38STue Ly constexpr double LOG_COEFFS[6] = {-0x1.fffffffffffffp-2, 0x1.5555555554a9bp-2,
44a0c92a38STue Ly                                   -0x1.0000000094567p-2, 0x1.99999dcc9823cp-3,
45a0c92a38STue Ly                                   -0x1.55550ac2e537ap-3, 0x1.21a02c4e624d7p-3};
46a0c92a38STue Ly 
47a68bbf42STue Ly // Logarithm Range Reduction - Step 2, 3, and 4.
48a68bbf42STue Ly extern const int S2[193];
49a68bbf42STue Ly extern const int S3[161];
50a68bbf42STue Ly extern const int S4[130];
51a68bbf42STue Ly 
52bc7a3bd8Slntue extern const double R2[193];
53bc7a3bd8Slntue 
54a0c92a38STue Ly // log(2) generated by Sollya with:
55a0c92a38STue Ly // > a = 2^-43 * nearestint(2^43*log(2));
56a0c92a38STue Ly // LSB = 2^-43 is chosen so that e_x * LOG_2_HI is exact for -1075 < e_x < 1024.
57a0c92a38STue Ly constexpr double LOG_2_HI = 0x1.62e42fefa38p-1; // LSB = 2^-43
58a0c92a38STue Ly // > b = round(log10(2) - a, D, RN);
59a0c92a38STue Ly constexpr double LOG_2_LO = 0x1.ef35793c7673p-45; // LSB = 2^-97
60bc8e87efSTue Ly 
6164af346bSTue Ly // Lookup table for exp(m) with m = -104, ..., 89.
6264af346bSTue Ly //   -104 = floor(log(single precision's min denormal))
6364af346bSTue Ly //     89 = ceil(log(single precision's max normal))
6464af346bSTue Ly // Table is generated with Sollya as follow:
6564af346bSTue Ly // > display = hexadecimal;
6664af346bSTue Ly // > for i from -104 to 89 do { D(exp(i)); };
6764af346bSTue Ly extern const double EXP_M1[195];
6864af346bSTue Ly 
6964af346bSTue Ly // Lookup table for exp(m * 2^(-7)) with m = 0, ..., 127.
7064af346bSTue Ly // Table is generated with Sollya as follow:
7164af346bSTue Ly // > display = hexadecimal;
7264af346bSTue Ly // > for i from 0 to 127 do { D(exp(i / 128)); };
7364af346bSTue Ly extern const double EXP_M2[128];
7464af346bSTue Ly 
758ca614aaSTue Ly // Lookup table for 2^(k * 2^-6) with k = 0..63.
768ca614aaSTue Ly extern const fputil::TripleDouble EXP2_MID1[64];
778ca614aaSTue Ly 
788ca614aaSTue Ly // Lookup table for 2^(k * 2^-12) with k = 0..63.
798ca614aaSTue Ly extern const fputil::TripleDouble EXP2_MID2[64];
808ca614aaSTue Ly 
81*5ff3ff33SPetr Hosek } // namespace LIBC_NAMESPACE_DECL
8263d2df00STue Ly 
8363d2df00STue Ly #endif // LLVM_LIBC_SRC_MATH_GENERIC_COMMON_CONSTANTS_H
84