xref: /aosp_15_r20/external/llvm-libc/src/math/generic/expxf16.h (revision 71db0c75aadcf003ffe3238005f61d7618a3fead)
1*71db0c75SAndroid Build Coastguard Worker //===-- Common utilities for half-precision exponential functions ---------===//
2*71db0c75SAndroid Build Coastguard Worker //
3*71db0c75SAndroid Build Coastguard Worker // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4*71db0c75SAndroid Build Coastguard Worker // See https://llvm.org/LICENSE.txt for license information.
5*71db0c75SAndroid Build Coastguard Worker // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6*71db0c75SAndroid Build Coastguard Worker //
7*71db0c75SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
8*71db0c75SAndroid Build Coastguard Worker 
9*71db0c75SAndroid Build Coastguard Worker #ifndef LLVM_LIBC_SRC_MATH_GENERIC_EXPXF16_H
10*71db0c75SAndroid Build Coastguard Worker #define LLVM_LIBC_SRC_MATH_GENERIC_EXPXF16_H
11*71db0c75SAndroid Build Coastguard Worker 
12*71db0c75SAndroid Build Coastguard Worker #include "src/__support/CPP/array.h"
13*71db0c75SAndroid Build Coastguard Worker #include "src/__support/FPUtil/FPBits.h"
14*71db0c75SAndroid Build Coastguard Worker #include "src/__support/FPUtil/PolyEval.h"
15*71db0c75SAndroid Build Coastguard Worker #include "src/__support/FPUtil/cast.h"
16*71db0c75SAndroid Build Coastguard Worker #include "src/__support/FPUtil/multiply_add.h"
17*71db0c75SAndroid Build Coastguard Worker #include "src/__support/FPUtil/nearest_integer.h"
18*71db0c75SAndroid Build Coastguard Worker #include "src/__support/macros/attributes.h"
19*71db0c75SAndroid Build Coastguard Worker #include "src/__support/macros/config.h"
20*71db0c75SAndroid Build Coastguard Worker #include <stdint.h>
21*71db0c75SAndroid Build Coastguard Worker 
22*71db0c75SAndroid Build Coastguard Worker namespace LIBC_NAMESPACE_DECL {
23*71db0c75SAndroid Build Coastguard Worker 
24*71db0c75SAndroid Build Coastguard Worker // Generated by Sollya with the following commands:
25*71db0c75SAndroid Build Coastguard Worker //   > display = hexadecimal;
26*71db0c75SAndroid Build Coastguard Worker //   > for i from -18 to 12 do print(round(exp(i), SG, RN));
27*71db0c75SAndroid Build Coastguard Worker static constexpr cpp::array<float, 31> EXP_HI = {
28*71db0c75SAndroid Build Coastguard Worker     0x1.05a628p-26f, 0x1.639e32p-25f, 0x1.e355bcp-24f, 0x1.4875cap-22f,
29*71db0c75SAndroid Build Coastguard Worker     0x1.be6c7p-21f,  0x1.2f6054p-19f, 0x1.9c54c4p-18f, 0x1.183542p-16f,
30*71db0c75SAndroid Build Coastguard Worker     0x1.7cd79cp-15f, 0x1.02cf22p-13f, 0x1.5fc21p-12f,  0x1.de16bap-11f,
31*71db0c75SAndroid Build Coastguard Worker     0x1.44e52p-9f,   0x1.b993fep-8f,  0x1.2c155cp-6f,  0x1.97db0cp-5f,
32*71db0c75SAndroid Build Coastguard Worker     0x1.152aaap-3f,  0x1.78b564p-2f,  0x1p+0f,         0x1.5bf0a8p+1f,
33*71db0c75SAndroid Build Coastguard Worker     0x1.d8e64cp+2f,  0x1.415e5cp+4f,  0x1.b4c902p+5f,  0x1.28d38ap+7f,
34*71db0c75SAndroid Build Coastguard Worker     0x1.936dc6p+8f,  0x1.122886p+10f, 0x1.749ea8p+11f, 0x1.fa7158p+12f,
35*71db0c75SAndroid Build Coastguard Worker     0x1.5829dcp+14f, 0x1.d3c448p+15f, 0x1.3de166p+17f,
36*71db0c75SAndroid Build Coastguard Worker };
37*71db0c75SAndroid Build Coastguard Worker 
38*71db0c75SAndroid Build Coastguard Worker // Generated by Sollya with the following commands:
39*71db0c75SAndroid Build Coastguard Worker //   > display = hexadecimal;
40*71db0c75SAndroid Build Coastguard Worker //   > for i from 0 to 7 do print(round(exp(i * 2^-3), SG, RN));
41*71db0c75SAndroid Build Coastguard Worker static constexpr cpp::array<float, 8> EXP_MID = {
42*71db0c75SAndroid Build Coastguard Worker     0x1p+0f,        0x1.221604p+0f, 0x1.48b5e4p+0f, 0x1.747a52p+0f,
43*71db0c75SAndroid Build Coastguard Worker     0x1.a61298p+0f, 0x1.de455ep+0f, 0x1.0ef9dcp+1f, 0x1.330e58p+1f,
44*71db0c75SAndroid Build Coastguard Worker };
45*71db0c75SAndroid Build Coastguard Worker 
46*71db0c75SAndroid Build Coastguard Worker struct ExpRangeReduction {
47*71db0c75SAndroid Build Coastguard Worker   float exp_hi_mid;
48*71db0c75SAndroid Build Coastguard Worker   float exp_lo;
49*71db0c75SAndroid Build Coastguard Worker };
50*71db0c75SAndroid Build Coastguard Worker 
exp_range_reduction(float16 x)51*71db0c75SAndroid Build Coastguard Worker LIBC_INLINE ExpRangeReduction exp_range_reduction(float16 x) {
52*71db0c75SAndroid Build Coastguard Worker   // For -18 < x < 12, to compute exp(x), we perform the following range
53*71db0c75SAndroid Build Coastguard Worker   // reduction: find hi, mid, lo, such that:
54*71db0c75SAndroid Build Coastguard Worker   //   x = hi + mid + lo, in which
55*71db0c75SAndroid Build Coastguard Worker   //     hi is an integer,
56*71db0c75SAndroid Build Coastguard Worker   //     mid * 2^3 is an integer,
57*71db0c75SAndroid Build Coastguard Worker   //     -2^(-4) <= lo < 2^(-4).
58*71db0c75SAndroid Build Coastguard Worker   // In particular,
59*71db0c75SAndroid Build Coastguard Worker   //   hi + mid = round(x * 2^3) * 2^(-3).
60*71db0c75SAndroid Build Coastguard Worker   // Then,
61*71db0c75SAndroid Build Coastguard Worker   //   exp(x) = exp(hi + mid + lo) = exp(hi) * exp(mid) * exp(lo).
62*71db0c75SAndroid Build Coastguard Worker   // We store exp(hi) and exp(mid) in the lookup tables EXP_HI and EXP_MID
63*71db0c75SAndroid Build Coastguard Worker   // respectively.  exp(lo) is computed using a degree-3 minimax polynomial
64*71db0c75SAndroid Build Coastguard Worker   // generated by Sollya.
65*71db0c75SAndroid Build Coastguard Worker 
66*71db0c75SAndroid Build Coastguard Worker   float xf = x;
67*71db0c75SAndroid Build Coastguard Worker   float kf = fputil::nearest_integer(xf * 0x1.0p+3f);
68*71db0c75SAndroid Build Coastguard Worker   int x_hi_mid = static_cast<int>(kf);
69*71db0c75SAndroid Build Coastguard Worker   int x_hi = x_hi_mid >> 3;
70*71db0c75SAndroid Build Coastguard Worker   int x_mid = x_hi_mid & 0x7;
71*71db0c75SAndroid Build Coastguard Worker   // lo = x - (hi + mid) = round(x * 2^3) * (-2^(-3)) + x
72*71db0c75SAndroid Build Coastguard Worker   float lo = fputil::multiply_add(kf, -0x1.0p-3f, xf);
73*71db0c75SAndroid Build Coastguard Worker 
74*71db0c75SAndroid Build Coastguard Worker   float exp_hi = EXP_HI[x_hi + 18];
75*71db0c75SAndroid Build Coastguard Worker   float exp_mid = EXP_MID[x_mid];
76*71db0c75SAndroid Build Coastguard Worker   // Degree-3 minimax polynomial generated by Sollya with the following
77*71db0c75SAndroid Build Coastguard Worker   // commands:
78*71db0c75SAndroid Build Coastguard Worker   //   > display = hexadecimal;
79*71db0c75SAndroid Build Coastguard Worker   //   > P = fpminimax(expm1(x)/x, 2, [|SG...|], [-2^-4, 2^-4]);
80*71db0c75SAndroid Build Coastguard Worker   //   > 1 + x * P;
81*71db0c75SAndroid Build Coastguard Worker   float exp_lo =
82*71db0c75SAndroid Build Coastguard Worker       fputil::polyeval(lo, 0x1p+0f, 0x1p+0f, 0x1.001p-1f, 0x1.555ddep-3f);
83*71db0c75SAndroid Build Coastguard Worker   return {exp_hi * exp_mid, exp_lo};
84*71db0c75SAndroid Build Coastguard Worker }
85*71db0c75SAndroid Build Coastguard Worker 
86*71db0c75SAndroid Build Coastguard Worker // Generated by Sollya with the following commands:
87*71db0c75SAndroid Build Coastguard Worker //   > display = hexadecimal;
88*71db0c75SAndroid Build Coastguard Worker //   > for i from 0 to 7 do printsingle(round(2^(i * 2^-3), SG, RN));
89*71db0c75SAndroid Build Coastguard Worker constexpr cpp::array<uint32_t, 8> EXP2_MID_BITS = {
90*71db0c75SAndroid Build Coastguard Worker     0x3f80'0000U, 0x3f8b'95c2U, 0x3f98'37f0U, 0x3fa5'fed7U,
91*71db0c75SAndroid Build Coastguard Worker     0x3fb5'04f3U, 0x3fc5'672aU, 0x3fd7'44fdU, 0x3fea'c0c7U,
92*71db0c75SAndroid Build Coastguard Worker };
93*71db0c75SAndroid Build Coastguard Worker 
exp2_range_reduction(float16 x)94*71db0c75SAndroid Build Coastguard Worker LIBC_INLINE ExpRangeReduction exp2_range_reduction(float16 x) {
95*71db0c75SAndroid Build Coastguard Worker   // For -25 < x < 16, to compute 2^x, we perform the following range reduction:
96*71db0c75SAndroid Build Coastguard Worker   // find hi, mid, lo, such that:
97*71db0c75SAndroid Build Coastguard Worker   //   x = hi + mid + lo, in which
98*71db0c75SAndroid Build Coastguard Worker   //     hi is an integer,
99*71db0c75SAndroid Build Coastguard Worker   //     mid * 2^3 is an integer,
100*71db0c75SAndroid Build Coastguard Worker   //     -2^(-4) <= lo < 2^(-4).
101*71db0c75SAndroid Build Coastguard Worker   // In particular,
102*71db0c75SAndroid Build Coastguard Worker   //   hi + mid = round(x * 2^3) * 2^(-3).
103*71db0c75SAndroid Build Coastguard Worker   // Then,
104*71db0c75SAndroid Build Coastguard Worker   //   2^x = 2^(hi + mid + lo) = 2^hi * 2^mid * 2^lo.
105*71db0c75SAndroid Build Coastguard Worker   // We store 2^mid in the lookup table EXP2_MID_BITS, and compute 2^hi * 2^mid
106*71db0c75SAndroid Build Coastguard Worker   // by adding hi to the exponent field of 2^mid.  2^lo is computed using a
107*71db0c75SAndroid Build Coastguard Worker   // degree-3 minimax polynomial generated by Sollya.
108*71db0c75SAndroid Build Coastguard Worker 
109*71db0c75SAndroid Build Coastguard Worker   float xf = x;
110*71db0c75SAndroid Build Coastguard Worker   float kf = fputil::nearest_integer(xf * 0x1.0p+3f);
111*71db0c75SAndroid Build Coastguard Worker   int x_hi_mid = static_cast<int>(kf);
112*71db0c75SAndroid Build Coastguard Worker   unsigned x_hi = static_cast<unsigned>(x_hi_mid) >> 3;
113*71db0c75SAndroid Build Coastguard Worker   unsigned x_mid = static_cast<unsigned>(x_hi_mid) & 0x7;
114*71db0c75SAndroid Build Coastguard Worker   // lo = x - (hi + mid) = round(x * 2^3) * (-2^(-3)) + x
115*71db0c75SAndroid Build Coastguard Worker   float lo = fputil::multiply_add(kf, -0x1.0p-3f, xf);
116*71db0c75SAndroid Build Coastguard Worker 
117*71db0c75SAndroid Build Coastguard Worker   uint32_t exp2_hi_mid_bits =
118*71db0c75SAndroid Build Coastguard Worker       EXP2_MID_BITS[x_mid] +
119*71db0c75SAndroid Build Coastguard Worker       static_cast<uint32_t>(x_hi << fputil::FPBits<float>::FRACTION_LEN);
120*71db0c75SAndroid Build Coastguard Worker   float exp2_hi_mid = fputil::FPBits<float>(exp2_hi_mid_bits).get_val();
121*71db0c75SAndroid Build Coastguard Worker   // Degree-3 minimax polynomial generated by Sollya with the following
122*71db0c75SAndroid Build Coastguard Worker   // commands:
123*71db0c75SAndroid Build Coastguard Worker   //   > display = hexadecimal;
124*71db0c75SAndroid Build Coastguard Worker   //   > P = fpminimax((2^x - 1)/x, 2, [|SG...|], [-2^-4, 2^-4]);
125*71db0c75SAndroid Build Coastguard Worker   //   > 1 + x * P;
126*71db0c75SAndroid Build Coastguard Worker   float exp2_lo = fputil::polyeval(lo, 0x1p+0f, 0x1.62e43p-1f, 0x1.ec0aa6p-3f,
127*71db0c75SAndroid Build Coastguard Worker                                    0x1.c6b4a6p-5f);
128*71db0c75SAndroid Build Coastguard Worker   return {exp2_hi_mid, exp2_lo};
129*71db0c75SAndroid Build Coastguard Worker }
130*71db0c75SAndroid Build Coastguard Worker 
131*71db0c75SAndroid Build Coastguard Worker // Generated by Sollya with the following commands:
132*71db0c75SAndroid Build Coastguard Worker //   > display = hexadecimal;
133*71db0c75SAndroid Build Coastguard Worker //   > round(log2(10), SG, RN);
134*71db0c75SAndroid Build Coastguard Worker static constexpr float LOG2F_10 = 0x1.a934fp+1f;
135*71db0c75SAndroid Build Coastguard Worker 
136*71db0c75SAndroid Build Coastguard Worker // Generated by Sollya with the following commands:
137*71db0c75SAndroid Build Coastguard Worker //   > display = hexadecimal;
138*71db0c75SAndroid Build Coastguard Worker //   > round(log10(2), SG, RN);
139*71db0c75SAndroid Build Coastguard Worker static constexpr float LOG10F_2 = 0x1.344136p-2f;
140*71db0c75SAndroid Build Coastguard Worker 
exp10_range_reduction(float16 x)141*71db0c75SAndroid Build Coastguard Worker LIBC_INLINE ExpRangeReduction exp10_range_reduction(float16 x) {
142*71db0c75SAndroid Build Coastguard Worker   // For -8 < x < 5, to compute 10^x, we perform the following range reduction:
143*71db0c75SAndroid Build Coastguard Worker   // find hi, mid, lo, such that:
144*71db0c75SAndroid Build Coastguard Worker   //   x = (hi + mid) * log2(10) + lo, in which
145*71db0c75SAndroid Build Coastguard Worker   //     hi is an integer,
146*71db0c75SAndroid Build Coastguard Worker   //     mid * 2^3 is an integer,
147*71db0c75SAndroid Build Coastguard Worker   //     -2^(-4) <= lo < 2^(-4).
148*71db0c75SAndroid Build Coastguard Worker   // In particular,
149*71db0c75SAndroid Build Coastguard Worker   //   hi + mid = round(x * 2^3) * 2^(-3).
150*71db0c75SAndroid Build Coastguard Worker   // Then,
151*71db0c75SAndroid Build Coastguard Worker   //   10^x = 10^(hi + mid + lo) = 2^((hi + mid) * log2(10)) + 10^lo
152*71db0c75SAndroid Build Coastguard Worker   // We store 2^mid in the lookup table EXP2_MID_BITS, and compute 2^hi * 2^mid
153*71db0c75SAndroid Build Coastguard Worker   // by adding hi to the exponent field of 2^mid.  10^lo is computed using a
154*71db0c75SAndroid Build Coastguard Worker   // degree-4 minimax polynomial generated by Sollya.
155*71db0c75SAndroid Build Coastguard Worker 
156*71db0c75SAndroid Build Coastguard Worker   float xf = x;
157*71db0c75SAndroid Build Coastguard Worker   float kf = fputil::nearest_integer(xf * (LOG2F_10 * 0x1.0p+3f));
158*71db0c75SAndroid Build Coastguard Worker   int x_hi_mid = static_cast<int>(kf);
159*71db0c75SAndroid Build Coastguard Worker   unsigned x_hi = static_cast<unsigned>(x_hi_mid) >> 3;
160*71db0c75SAndroid Build Coastguard Worker   unsigned x_mid = static_cast<unsigned>(x_hi_mid) & 0x7;
161*71db0c75SAndroid Build Coastguard Worker   // lo = x - (hi + mid) = round(x * 2^3 * log2(10)) * log10(2) * (-2^(-3)) + x
162*71db0c75SAndroid Build Coastguard Worker   float lo = fputil::multiply_add(kf, LOG10F_2 * -0x1.0p-3f, xf);
163*71db0c75SAndroid Build Coastguard Worker 
164*71db0c75SAndroid Build Coastguard Worker   uint32_t exp2_hi_mid_bits =
165*71db0c75SAndroid Build Coastguard Worker       EXP2_MID_BITS[x_mid] +
166*71db0c75SAndroid Build Coastguard Worker       static_cast<uint32_t>(x_hi << fputil::FPBits<float>::FRACTION_LEN);
167*71db0c75SAndroid Build Coastguard Worker   float exp2_hi_mid = fputil::FPBits<float>(exp2_hi_mid_bits).get_val();
168*71db0c75SAndroid Build Coastguard Worker   // Degree-4 minimax polynomial generated by Sollya with the following
169*71db0c75SAndroid Build Coastguard Worker   // commands:
170*71db0c75SAndroid Build Coastguard Worker   //   > display = hexadecimal;
171*71db0c75SAndroid Build Coastguard Worker   //   > P = fpminimax((10^x - 1)/x, 3, [|SG...|], [-2^-4, 2^-4]);
172*71db0c75SAndroid Build Coastguard Worker   //   > 1 + x * P;
173*71db0c75SAndroid Build Coastguard Worker   float exp10_lo = fputil::polyeval(lo, 0x1p+0f, 0x1.26bb14p+1f, 0x1.53526p+1f,
174*71db0c75SAndroid Build Coastguard Worker                                     0x1.04b434p+1f, 0x1.2bcf9ep+0f);
175*71db0c75SAndroid Build Coastguard Worker   return {exp2_hi_mid, exp10_lo};
176*71db0c75SAndroid Build Coastguard Worker }
177*71db0c75SAndroid Build Coastguard Worker 
178*71db0c75SAndroid Build Coastguard Worker // Generated by Sollya with the following commands:
179*71db0c75SAndroid Build Coastguard Worker //   > display = hexadecimal;
180*71db0c75SAndroid Build Coastguard Worker //   > round(log2(exp(1)), SG, RN);
181*71db0c75SAndroid Build Coastguard Worker static constexpr float LOG2F_E = 0x1.715476p+0f;
182*71db0c75SAndroid Build Coastguard Worker 
183*71db0c75SAndroid Build Coastguard Worker // Generated by Sollya with the following commands:
184*71db0c75SAndroid Build Coastguard Worker //   > display = hexadecimal;
185*71db0c75SAndroid Build Coastguard Worker //   > round(log(2), SG, RN);
186*71db0c75SAndroid Build Coastguard Worker static constexpr float LOGF_2 = 0x1.62e43p-1f;
187*71db0c75SAndroid Build Coastguard Worker 
188*71db0c75SAndroid Build Coastguard Worker // Generated by Sollya with the following commands:
189*71db0c75SAndroid Build Coastguard Worker //   > display = hexadecimal;
190*71db0c75SAndroid Build Coastguard Worker //   > for i from 0 to 31 do printsingle(round(2^(i * 2^-5), SG, RN));
191*71db0c75SAndroid Build Coastguard Worker static constexpr cpp::array<uint32_t, 32> EXP2_MID_5_BITS = {
192*71db0c75SAndroid Build Coastguard Worker     0x3f80'0000U, 0x3f82'cd87U, 0x3f85'aac3U, 0x3f88'980fU, 0x3f8b'95c2U,
193*71db0c75SAndroid Build Coastguard Worker     0x3f8e'a43aU, 0x3f91'c3d3U, 0x3f94'f4f0U, 0x3f98'37f0U, 0x3f9b'8d3aU,
194*71db0c75SAndroid Build Coastguard Worker     0x3f9e'f532U, 0x3fa2'7043U, 0x3fa5'fed7U, 0x3fa9'a15bU, 0x3fad'583fU,
195*71db0c75SAndroid Build Coastguard Worker     0x3fb1'23f6U, 0x3fb5'04f3U, 0x3fb8'fbafU, 0x3fbd'08a4U, 0x3fc1'2c4dU,
196*71db0c75SAndroid Build Coastguard Worker     0x3fc5'672aU, 0x3fc9'b9beU, 0x3fce'248cU, 0x3fd2'a81eU, 0x3fd7'44fdU,
197*71db0c75SAndroid Build Coastguard Worker     0x3fdb'fbb8U, 0x3fe0'ccdfU, 0x3fe5'b907U, 0x3fea'c0c7U, 0x3fef'e4baU,
198*71db0c75SAndroid Build Coastguard Worker     0x3ff5'257dU, 0x3ffa'83b3U,
199*71db0c75SAndroid Build Coastguard Worker };
200*71db0c75SAndroid Build Coastguard Worker 
201*71db0c75SAndroid Build Coastguard Worker // This function correctly calculates sinh(x) and cosh(x) by calculating exp(x)
202*71db0c75SAndroid Build Coastguard Worker // and exp(-x) simultaneously.
203*71db0c75SAndroid Build Coastguard Worker // To compute e^x, we perform the following range reduction:
204*71db0c75SAndroid Build Coastguard Worker // find hi, mid, lo such that:
205*71db0c75SAndroid Build Coastguard Worker //   x = (hi + mid) * log(2) + lo, in which
206*71db0c75SAndroid Build Coastguard Worker //     hi is an integer,
207*71db0c75SAndroid Build Coastguard Worker //     0 <= mid * 2^5 < 32 is an integer
208*71db0c75SAndroid Build Coastguard Worker //     -2^(-5) <= lo * log2(e) <= 2^-5.
209*71db0c75SAndroid Build Coastguard Worker // In particular,
210*71db0c75SAndroid Build Coastguard Worker //   hi + mid = round(x * log2(e) * 2^5) * 2^(-5).
211*71db0c75SAndroid Build Coastguard Worker // Then,
212*71db0c75SAndroid Build Coastguard Worker //   e^x = 2^(hi + mid) * e^lo = 2^hi * 2^mid * e^lo.
213*71db0c75SAndroid Build Coastguard Worker // We store 2^mid in the lookup table EXP2_MID_5_BITS, and compute 2^hi * 2^mid
214*71db0c75SAndroid Build Coastguard Worker // by adding hi to the exponent field of 2^mid.
215*71db0c75SAndroid Build Coastguard Worker // e^lo is computed using a degree-3 minimax polynomial generated by Sollya:
216*71db0c75SAndroid Build Coastguard Worker //   e^lo ~ P(lo)
217*71db0c75SAndroid Build Coastguard Worker //        = 1 + lo + c2 * lo^2 + ... + c5 * lo^5
218*71db0c75SAndroid Build Coastguard Worker //        = (1 + c2*lo^2 + c4*lo^4) + lo * (1 + c3*lo^2 + c5*lo^4)
219*71db0c75SAndroid Build Coastguard Worker //        = P_even + lo * P_odd
220*71db0c75SAndroid Build Coastguard Worker // To compute e^(-x), notice that:
221*71db0c75SAndroid Build Coastguard Worker //   e^(-x) = 2^(-(hi + mid)) * e^(-lo)
222*71db0c75SAndroid Build Coastguard Worker //          ~ 2^(-(hi + mid)) * P(-lo)
223*71db0c75SAndroid Build Coastguard Worker //          = 2^(-(hi + mid)) * (P_even - lo * P_odd)
224*71db0c75SAndroid Build Coastguard Worker // So:
225*71db0c75SAndroid Build Coastguard Worker //   sinh(x) = (e^x - e^(-x)) / 2
226*71db0c75SAndroid Build Coastguard Worker //           ~ 0.5 * (2^(hi + mid) * (P_even + lo * P_odd) -
227*71db0c75SAndroid Build Coastguard Worker //                    2^(-(hi + mid)) * (P_even - lo * P_odd))
228*71db0c75SAndroid Build Coastguard Worker //           = 0.5 * (P_even * (2^(hi + mid) - 2^(-(hi + mid))) +
229*71db0c75SAndroid Build Coastguard Worker //                    lo * P_odd * (2^(hi + mid) + 2^(-(hi + mid))))
230*71db0c75SAndroid Build Coastguard Worker // And similarly:
231*71db0c75SAndroid Build Coastguard Worker //   cosh(x) = (e^x + e^(-x)) / 2
232*71db0c75SAndroid Build Coastguard Worker //           ~ 0.5 * (P_even * (2^(hi + mid) + 2^(-(hi + mid))) +
233*71db0c75SAndroid Build Coastguard Worker //                    lo * P_odd * (2^(hi + mid) - 2^(-(hi + mid))))
234*71db0c75SAndroid Build Coastguard Worker // The main point of these formulas is that the expensive part of calculating
235*71db0c75SAndroid Build Coastguard Worker // the polynomials approximating lower parts of e^x and e^(-x) is shared and
236*71db0c75SAndroid Build Coastguard Worker // only done once.
eval_sinh_or_cosh(float16 x)237*71db0c75SAndroid Build Coastguard Worker template <bool IsSinh> LIBC_INLINE float16 eval_sinh_or_cosh(float16 x) {
238*71db0c75SAndroid Build Coastguard Worker   float xf = x;
239*71db0c75SAndroid Build Coastguard Worker   float kf = fputil::nearest_integer(xf * (LOG2F_E * 0x1.0p+5f));
240*71db0c75SAndroid Build Coastguard Worker   int x_hi_mid_p = static_cast<int>(kf);
241*71db0c75SAndroid Build Coastguard Worker   int x_hi_mid_m = -x_hi_mid_p;
242*71db0c75SAndroid Build Coastguard Worker 
243*71db0c75SAndroid Build Coastguard Worker   unsigned x_hi_p = static_cast<unsigned>(x_hi_mid_p) >> 5;
244*71db0c75SAndroid Build Coastguard Worker   unsigned x_hi_m = static_cast<unsigned>(x_hi_mid_m) >> 5;
245*71db0c75SAndroid Build Coastguard Worker   unsigned x_mid_p = static_cast<unsigned>(x_hi_mid_p) & 0x1f;
246*71db0c75SAndroid Build Coastguard Worker   unsigned x_mid_m = static_cast<unsigned>(x_hi_mid_m) & 0x1f;
247*71db0c75SAndroid Build Coastguard Worker 
248*71db0c75SAndroid Build Coastguard Worker   uint32_t exp2_hi_mid_bits_p =
249*71db0c75SAndroid Build Coastguard Worker       EXP2_MID_5_BITS[x_mid_p] +
250*71db0c75SAndroid Build Coastguard Worker       static_cast<uint32_t>(x_hi_p << fputil::FPBits<float>::FRACTION_LEN);
251*71db0c75SAndroid Build Coastguard Worker   uint32_t exp2_hi_mid_bits_m =
252*71db0c75SAndroid Build Coastguard Worker       EXP2_MID_5_BITS[x_mid_m] +
253*71db0c75SAndroid Build Coastguard Worker       static_cast<uint32_t>(x_hi_m << fputil::FPBits<float>::FRACTION_LEN);
254*71db0c75SAndroid Build Coastguard Worker   // exp2_hi_mid_p = 2^(hi + mid)
255*71db0c75SAndroid Build Coastguard Worker   float exp2_hi_mid_p = fputil::FPBits<float>(exp2_hi_mid_bits_p).get_val();
256*71db0c75SAndroid Build Coastguard Worker   // exp2_hi_mid_m = 2^(-(hi + mid))
257*71db0c75SAndroid Build Coastguard Worker   float exp2_hi_mid_m = fputil::FPBits<float>(exp2_hi_mid_bits_m).get_val();
258*71db0c75SAndroid Build Coastguard Worker 
259*71db0c75SAndroid Build Coastguard Worker   // exp2_hi_mid_sum = 2^(hi + mid) + 2^(-(hi + mid))
260*71db0c75SAndroid Build Coastguard Worker   float exp2_hi_mid_sum = exp2_hi_mid_p + exp2_hi_mid_m;
261*71db0c75SAndroid Build Coastguard Worker   // exp2_hi_mid_diff = 2^(hi + mid) - 2^(-(hi + mid))
262*71db0c75SAndroid Build Coastguard Worker   float exp2_hi_mid_diff = exp2_hi_mid_p - exp2_hi_mid_m;
263*71db0c75SAndroid Build Coastguard Worker 
264*71db0c75SAndroid Build Coastguard Worker   // lo = x - (hi + mid) = round(x * log2(e) * 2^5) * log(2) * (-2^(-5)) + x
265*71db0c75SAndroid Build Coastguard Worker   float lo = fputil::multiply_add(kf, LOGF_2 * -0x1.0p-5f, xf);
266*71db0c75SAndroid Build Coastguard Worker   float lo_sq = lo * lo;
267*71db0c75SAndroid Build Coastguard Worker 
268*71db0c75SAndroid Build Coastguard Worker   // Degree-3 minimax polynomial generated by Sollya with the following
269*71db0c75SAndroid Build Coastguard Worker   // commands:
270*71db0c75SAndroid Build Coastguard Worker   //   > display = hexadecimal;
271*71db0c75SAndroid Build Coastguard Worker   //   > P = fpminimax(expm1(x)/x, 2, [|SG...|], [-2^-5, 2^-5]);
272*71db0c75SAndroid Build Coastguard Worker   //   > 1 + x * P;
273*71db0c75SAndroid Build Coastguard Worker   constexpr cpp::array<float, 4> COEFFS = {0x1p+0f, 0x1p+0f, 0x1.0004p-1f,
274*71db0c75SAndroid Build Coastguard Worker                                            0x1.555778p-3f};
275*71db0c75SAndroid Build Coastguard Worker   float half_p_odd =
276*71db0c75SAndroid Build Coastguard Worker       fputil::polyeval(lo_sq, COEFFS[1] * 0.5f, COEFFS[3] * 0.5f);
277*71db0c75SAndroid Build Coastguard Worker   float half_p_even =
278*71db0c75SAndroid Build Coastguard Worker       fputil::polyeval(lo_sq, COEFFS[0] * 0.5f, COEFFS[2] * 0.5f);
279*71db0c75SAndroid Build Coastguard Worker 
280*71db0c75SAndroid Build Coastguard Worker   // sinh(x) = lo * (0.5 * P_odd * (2^(hi + mid) + 2^(-(hi + mid)))) +
281*71db0c75SAndroid Build Coastguard Worker   //                (0.5 * P_even * (2^(hi + mid) - 2^(-(hi + mid))))
282*71db0c75SAndroid Build Coastguard Worker   if constexpr (IsSinh)
283*71db0c75SAndroid Build Coastguard Worker     return fputil::cast<float16>(fputil::multiply_add(
284*71db0c75SAndroid Build Coastguard Worker         lo, half_p_odd * exp2_hi_mid_sum, half_p_even * exp2_hi_mid_diff));
285*71db0c75SAndroid Build Coastguard Worker   // cosh(x) = lo * (0.5 * P_odd * (2^(hi + mid) - 2^(-(hi + mid)))) +
286*71db0c75SAndroid Build Coastguard Worker   //                (0.5 * P_even * (2^(hi + mid) + 2^(-(hi + mid))))
287*71db0c75SAndroid Build Coastguard Worker   return fputil::cast<float16>(fputil::multiply_add(
288*71db0c75SAndroid Build Coastguard Worker       lo, half_p_odd * exp2_hi_mid_diff, half_p_even * exp2_hi_mid_sum));
289*71db0c75SAndroid Build Coastguard Worker }
290*71db0c75SAndroid Build Coastguard Worker 
291*71db0c75SAndroid Build Coastguard Worker // Generated by Sollya with the following commands:
292*71db0c75SAndroid Build Coastguard Worker //   > display = hexadecimal;
293*71db0c75SAndroid Build Coastguard Worker //   > for i from 0 to 31 do print(round(log(1 + i * 2^-5), SG, RN));
294*71db0c75SAndroid Build Coastguard Worker constexpr cpp::array<float, 32> LOGF_F = {
295*71db0c75SAndroid Build Coastguard Worker     0x0p+0f,        0x1.f829bp-6f,  0x1.f0a30cp-5f, 0x1.6f0d28p-4f,
296*71db0c75SAndroid Build Coastguard Worker     0x1.e27076p-4f, 0x1.29553p-3f,  0x1.5ff308p-3f, 0x1.9525aap-3f,
297*71db0c75SAndroid Build Coastguard Worker     0x1.c8ff7cp-3f, 0x1.fb9186p-3f, 0x1.1675cap-2f, 0x1.2e8e2cp-2f,
298*71db0c75SAndroid Build Coastguard Worker     0x1.4618bcp-2f, 0x1.5d1bdcp-2f, 0x1.739d8p-2f,  0x1.89a338p-2f,
299*71db0c75SAndroid Build Coastguard Worker     0x1.9f323ep-2f, 0x1.b44f78p-2f, 0x1.c8ff7cp-2f, 0x1.dd46ap-2f,
300*71db0c75SAndroid Build Coastguard Worker     0x1.f128f6p-2f, 0x1.02552ap-1f, 0x1.0be72ep-1f, 0x1.154c3ep-1f,
301*71db0c75SAndroid Build Coastguard Worker     0x1.1e85f6p-1f, 0x1.2795e2p-1f, 0x1.307d74p-1f, 0x1.393e0ep-1f,
302*71db0c75SAndroid Build Coastguard Worker     0x1.41d8fep-1f, 0x1.4a4f86p-1f, 0x1.52a2d2p-1f, 0x1.5ad404p-1f,
303*71db0c75SAndroid Build Coastguard Worker };
304*71db0c75SAndroid Build Coastguard Worker 
305*71db0c75SAndroid Build Coastguard Worker // Generated by Sollya with the following commands:
306*71db0c75SAndroid Build Coastguard Worker //   > display = hexadecimal;
307*71db0c75SAndroid Build Coastguard Worker //   > for i from 0 to 31 do print(round(log2(1 + i * 2^-5), SG, RN));
308*71db0c75SAndroid Build Coastguard Worker constexpr cpp::array<float, 32> LOG2F_F = {
309*71db0c75SAndroid Build Coastguard Worker     0x0p+0f,        0x1.6bad38p-5f, 0x1.663f7p-4f,  0x1.08c588p-3f,
310*71db0c75SAndroid Build Coastguard Worker     0x1.5c01a4p-3f, 0x1.acf5e2p-3f, 0x1.fbc16cp-3f, 0x1.24407ap-2f,
311*71db0c75SAndroid Build Coastguard Worker     0x1.49a784p-2f, 0x1.6e221cp-2f, 0x1.91bba8p-2f, 0x1.b47ecp-2f,
312*71db0c75SAndroid Build Coastguard Worker     0x1.d6753ep-2f, 0x1.f7a856p-2f, 0x1.0c105p-1f,  0x1.1bf312p-1f,
313*71db0c75SAndroid Build Coastguard Worker     0x1.2b8034p-1f, 0x1.3abb4p-1f,  0x1.49a784p-1f, 0x1.584822p-1f,
314*71db0c75SAndroid Build Coastguard Worker     0x1.66a008p-1f, 0x1.74b1fep-1f, 0x1.82809ep-1f, 0x1.900e62p-1f,
315*71db0c75SAndroid Build Coastguard Worker     0x1.9d5dap-1f,  0x1.aa709p-1f,  0x1.b74948p-1f, 0x1.c3e9cap-1f,
316*71db0c75SAndroid Build Coastguard Worker     0x1.d053f6p-1f, 0x1.dc899ap-1f, 0x1.e88c6cp-1f, 0x1.f45e08p-1f,
317*71db0c75SAndroid Build Coastguard Worker };
318*71db0c75SAndroid Build Coastguard Worker 
319*71db0c75SAndroid Build Coastguard Worker // Generated by Sollya with the following commands:
320*71db0c75SAndroid Build Coastguard Worker //   > display = hexadecimal;
321*71db0c75SAndroid Build Coastguard Worker //   > for i from 0 to 31 do print(round(log10(1 + i * 2^-5), SG, RN));
322*71db0c75SAndroid Build Coastguard Worker constexpr cpp::array<float, 32> LOG10F_F = {
323*71db0c75SAndroid Build Coastguard Worker     0x0p+0f,        0x1.b5e908p-7f, 0x1.af5f92p-6f, 0x1.3ed11ap-5f,
324*71db0c75SAndroid Build Coastguard Worker     0x1.a30a9ep-5f, 0x1.02428cp-4f, 0x1.31b306p-4f, 0x1.5fe804p-4f,
325*71db0c75SAndroid Build Coastguard Worker     0x1.8cf184p-4f, 0x1.b8de4ep-4f, 0x1.e3bc1ap-4f, 0x1.06cbd6p-3f,
326*71db0c75SAndroid Build Coastguard Worker     0x1.1b3e72p-3f, 0x1.2f3b6ap-3f, 0x1.42c7e8p-3f, 0x1.55e8c6p-3f,
327*71db0c75SAndroid Build Coastguard Worker     0x1.68a288p-3f, 0x1.7af974p-3f, 0x1.8cf184p-3f, 0x1.9e8e7cp-3f,
328*71db0c75SAndroid Build Coastguard Worker     0x1.afd3e4p-3f, 0x1.c0c514p-3f, 0x1.d1653p-3f,  0x1.e1b734p-3f,
329*71db0c75SAndroid Build Coastguard Worker     0x1.f1bdeep-3f, 0x1.00be06p-2f, 0x1.087a08p-2f, 0x1.101432p-2f,
330*71db0c75SAndroid Build Coastguard Worker     0x1.178da6p-2f, 0x1.1ee778p-2f, 0x1.2622bp-2f,  0x1.2d404cp-2f,
331*71db0c75SAndroid Build Coastguard Worker };
332*71db0c75SAndroid Build Coastguard Worker 
333*71db0c75SAndroid Build Coastguard Worker // Generated by Sollya with the following commands:
334*71db0c75SAndroid Build Coastguard Worker //   > display = hexadecimal;
335*71db0c75SAndroid Build Coastguard Worker //   > for i from 0 to 31 do print(round(1 / (1 + i * 2^-5), SG, RN));
336*71db0c75SAndroid Build Coastguard Worker constexpr cpp::array<float, 32> ONE_OVER_F_F = {
337*71db0c75SAndroid Build Coastguard Worker     0x1p+0f,        0x1.f07c2p-1f,  0x1.e1e1e2p-1f, 0x1.d41d42p-1f,
338*71db0c75SAndroid Build Coastguard Worker     0x1.c71c72p-1f, 0x1.bacf92p-1f, 0x1.af286cp-1f, 0x1.a41a42p-1f,
339*71db0c75SAndroid Build Coastguard Worker     0x1.99999ap-1f, 0x1.8f9c18p-1f, 0x1.861862p-1f, 0x1.7d05f4p-1f,
340*71db0c75SAndroid Build Coastguard Worker     0x1.745d18p-1f, 0x1.6c16c2p-1f, 0x1.642c86p-1f, 0x1.5c9882p-1f,
341*71db0c75SAndroid Build Coastguard Worker     0x1.555556p-1f, 0x1.4e5e0ap-1f, 0x1.47ae14p-1f, 0x1.414142p-1f,
342*71db0c75SAndroid Build Coastguard Worker     0x1.3b13b2p-1f, 0x1.3521dp-1f,  0x1.2f684cp-1f, 0x1.29e412p-1f,
343*71db0c75SAndroid Build Coastguard Worker     0x1.24924ap-1f, 0x1.1f7048p-1f, 0x1.1a7b96p-1f, 0x1.15b1e6p-1f,
344*71db0c75SAndroid Build Coastguard Worker     0x1.111112p-1f, 0x1.0c9714p-1f, 0x1.08421p-1f,  0x1.041042p-1f,
345*71db0c75SAndroid Build Coastguard Worker };
346*71db0c75SAndroid Build Coastguard Worker 
347*71db0c75SAndroid Build Coastguard Worker } // namespace LIBC_NAMESPACE_DECL
348*71db0c75SAndroid Build Coastguard Worker 
349*71db0c75SAndroid Build Coastguard Worker #endif // LLVM_LIBC_SRC_MATH_GENERIC_EXPXF16_H
350