xref: /llvm-project/libc/src/stdfix/expk.cpp (revision 5ff3ff33ff930e4ec49da7910612d8a41eb068cb)
114171b87Slntue //===-- Implementation of expk function ----------------------------------===//
214171b87Slntue //
314171b87Slntue // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
414171b87Slntue // See https://llvm.org/LICENSE.txt for license information.
514171b87Slntue // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
614171b87Slntue //
714171b87Slntue //===----------------------------------------------------------------------===//
814171b87Slntue 
914171b87Slntue #include "expk.h"
1014171b87Slntue #include "src/__support/CPP/bit.h"
1114171b87Slntue #include "src/__support/common.h"
1214171b87Slntue #include "src/__support/fixed_point/fx_bits.h"
13*5ff3ff33SPetr Hosek #include "src/__support/macros/config.h"
1414171b87Slntue 
15*5ff3ff33SPetr Hosek namespace LIBC_NAMESPACE_DECL {
1614171b87Slntue 
1714171b87Slntue namespace {
1814171b87Slntue 
1914171b87Slntue // Look up tables for exp(hi) and exp(mid).
2014171b87Slntue // Generated with Sollya:
2114171b87Slntue // > for i from 0 to 23 do {
2214171b87Slntue //     hi = i - 11;
2314171b87Slntue //     e_hi = nearestint(exp(hi) * 2^15) * 2^-15;
2414171b87Slntue //     print(e_hi, "k,");
2514171b87Slntue //   };
2614171b87Slntue static constexpr accum EXP_HI[24] = {
2714171b87Slntue     0x1p-15k,        0x1p-15k,         0x1p-13k,        0x1.6p-12k,
2814171b87Slntue     0x1.ep-11k,      0x1.44p-9k,       0x1.bap-8k,      0x1.2cp-6k,
2914171b87Slntue     0x1.97cp-5k,     0x1.153p-3k,      0x1.78b8p-2k,    0x1p0k,
3014171b87Slntue     0x1.5bf1p1k,     0x1.d8e68p2k,     0x1.415e6p4k,    0x1.b4c9p5k,
3114171b87Slntue     0x1.28d388p7k,   0x1.936dc6p8k,    0x1.1228858p10k, 0x1.749ea7cp11k,
3214171b87Slntue     0x1.fa7157cp12k, 0x1.5829dcf8p14k, 0x1.d3c4489p15k, ACCUM_MAX,
3314171b87Slntue };
3414171b87Slntue 
3514171b87Slntue // Generated with Sollya:
3614171b87Slntue // > for i from 0 to 15 do {
3714171b87Slntue //     m = i/16 - 0.0625;
3814171b87Slntue //     e_m = nearestint(exp(m) * 2^15) * 2^-15;
3914171b87Slntue //     print(e_m, "k,");
4014171b87Slntue //   };
4114171b87Slntue static constexpr accum EXP_MID[16] = {
4214171b87Slntue     0x1.e0fcp-1k, 0x1p0k,      0x1.1082p0k, 0x1.2216p0k,
4314171b87Slntue     0x1.34ccp0k,  0x1.48b6p0k, 0x1.5deap0k, 0x1.747ap0k,
4414171b87Slntue     0x1.8c8p0k,   0x1.a612p0k, 0x1.c14cp0k, 0x1.de46p0k,
4514171b87Slntue     0x1.fd1ep0k,  0x1.0efap1k, 0x1.2074p1k, 0x1.330ep1k,
4614171b87Slntue };
4714171b87Slntue 
4814171b87Slntue } // anonymous namespace
4914171b87Slntue 
5014171b87Slntue LLVM_LIBC_FUNCTION(accum, expk, (accum x)) {
5114171b87Slntue   using FXRep = fixed_point::FXRep<accum>;
5214171b87Slntue   using StorageType = typename FXRep::StorageType;
5314171b87Slntue   // Output overflow
5414171b87Slntue   // > floor(log(2^16) * 2^15) * 2^-15
5514171b87Slntue   if (LIBC_UNLIKELY(x >= 0x1.62e4p3k))
5614171b87Slntue     return FXRep::MAX();
5714171b87Slntue   // Lower bound where exp(x) -> 0:
5814171b87Slntue   //   floor(log(2^-16) * 2^15) * 2^-15
5914171b87Slntue   if (LIBC_UNLIKELY(x <= -0x1.62e44p3k))
6014171b87Slntue     return FXRep::ZERO();
6114171b87Slntue 
6214171b87Slntue   // Current range of x:
6314171b87Slntue   //   -0x1.62e4p3 <= x <= 0x1.62e3cp3
6414171b87Slntue   // Range reduction:
6514171b87Slntue   //   x = hi + mid + lo,
6614171b87Slntue   // where:
6714171b87Slntue   //   hi is an integer
6814171b87Slntue   //   mid * 2^4 is an integer
6914171b87Slntue   //   |lo| <= 2^-5.
7014171b87Slntue   // Then exp(x) = exp(hi + mid + lo) = exp(hi) * exp(mid) * exp(lo)
7114171b87Slntue   //             ~ exp(hi) * exp(mid) * (1 + lo + lo^2 / 2)
7214171b87Slntue   // with relative errors < |lo|^3/2 <= 2^-16.
7314171b87Slntue   //   exp(hi) and exp(mid) are extracted from small lookup tables.
7414171b87Slntue 
7514171b87Slntue   // Round-to-nearest 1/16, tie-to-(+Int):
7614171b87Slntue   constexpr accum ONE_THIRTY_SECOND = 0x1.0p-5k;
7714171b87Slntue   // x_rounded = floor(x + 1/16).
7814171b87Slntue   accum x_rounded = ((x + ONE_THIRTY_SECOND) >> (FXRep::FRACTION_LEN - 4))
7914171b87Slntue                     << (FXRep::FRACTION_LEN - 4);
8014171b87Slntue   accum lo = x - x_rounded;
8114171b87Slntue 
8214171b87Slntue   // Range of x_rounded:
8314171b87Slntue   //   x_rounded >= floor((-0x1.62e4p3 + 0x1.0p-5) * 2^4) * 2^-4
8414171b87Slntue   //              = -0x1.62p3 = -11.0625
8514171b87Slntue   // To get the indices, we shift the values so that it start with 0.
8614171b87Slntue   // Range of indices: 0 <= indices <= 355.
8714171b87Slntue   StorageType indices = cpp::bit_cast<StorageType>((x_rounded + 0x1.62p3k) >>
8814171b87Slntue                                                    (FXRep::FRACTION_LEN - 4));
8914171b87Slntue   // So we have the following relation:
9014171b87Slntue   //   indices = (hi + mid + 177/16) * 16
9114171b87Slntue   // That implies:
9214171b87Slntue   //   hi + mid = indices/16 - 11.0625
9314171b87Slntue   // So for lookup tables, we can use the upper 4 bits to get:
9414171b87Slntue   //   exp( floor(indices / 16) - 11 )
9514171b87Slntue   // and lower 4 bits for:
9614171b87Slntue   //   exp( (indices - floor(indices)) - 0.0625 )
9714171b87Slntue   accum exp_hi = EXP_HI[indices >> 4];
9814171b87Slntue   accum exp_mid = EXP_MID[indices & 0xf];
9914171b87Slntue   // exp(x) ~ exp(hi) * exp(mid) * (1 + lo);
10014171b87Slntue   accum l1 = 0x1.0p0k + (lo >> 1); // = 1 + lo / 2
10114171b87Slntue   accum l2 = 0x1.0p0k + lo * l1;   // = 1 + lo * (1 + lo / 2) = 1 + lo + lo^2/2
10214171b87Slntue   return (exp_hi * (exp_mid * l2));
10314171b87Slntue }
10414171b87Slntue 
105*5ff3ff33SPetr Hosek } // namespace LIBC_NAMESPACE_DECL
106