xref: /llvm-project/libc/src/stdfix/exphk.cpp (revision 5ff3ff33ff930e4ec49da7910612d8a41eb068cb)
114171b87Slntue //===-- Implementation of exphk 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 "exphk.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 89 do {
2214171b87Slntue //     hi = floor(i/8) - 5;
2314171b87Slntue //     m = i/8 - floor(i/8) - 0.5;
2414171b87Slntue //     e_hi = nearestint(exp(hi) * 2^7) * 2^-7;
2514171b87Slntue //     e_mid = nearestint(exp(m) * 2^7) * 2^-7;
2614171b87Slntue //     print(hi, e_hi, m, e_mid);
2714171b87Slntue //   };
2814171b87Slntue // Notice that when i = 88 and 89, e_hi will overflow short accum range.
2914171b87Slntue static constexpr short accum EXP_HI[12] = {
3014171b87Slntue     0x1.0p-7hk, 0x1.0p-6hk, 0x1.8p-5hk,  0x1.1p-3hk,  0x1.78p-2hk,  0x1.0p0hk,
3114171b87Slntue     0x1.5cp1hk, 0x1.d9p2hk, 0x1.416p4hk, 0x1.b4dp5hk, 0x1.28d4p7hk, SACCUM_MAX,
3214171b87Slntue };
3314171b87Slntue 
3414171b87Slntue static constexpr short accum EXP_MID[8] = {
3514171b87Slntue     0x1.38p-1hk, 0x1.6p-1hk, 0x1.9p-1hk, 0x1.c4p-1hk,
3614171b87Slntue     0x1.0p0hk,   0x1.22p0hk, 0x1.48p0hk, 0x1.74p0hk,
3714171b87Slntue };
3814171b87Slntue 
3914171b87Slntue } // anonymous namespace
4014171b87Slntue 
4114171b87Slntue LLVM_LIBC_FUNCTION(short accum, exphk, (short accum x)) {
4214171b87Slntue   using FXRep = fixed_point::FXRep<short accum>;
4314171b87Slntue   using StorageType = typename FXRep::StorageType;
4414171b87Slntue   // Output overflow
4514171b87Slntue   if (LIBC_UNLIKELY(x >= 0x1.64p2hk))
4614171b87Slntue     return FXRep::MAX();
4714171b87Slntue   // Lower bound where exp(x) -> 0:
4814171b87Slntue   //   floor(log(2^-8) * 2^7) * 2^-7
4914171b87Slntue   if (LIBC_UNLIKELY(x <= -0x1.63p2hk))
5014171b87Slntue     return FXRep::ZERO();
5114171b87Slntue 
5214171b87Slntue   // Current range of x:
5314171b87Slntue   //   -0x1.628p2 <= x <= 0x1.638p2
5414171b87Slntue   // Range reduction:
5514171b87Slntue   //   x = hi + mid + lo,
5614171b87Slntue   // where:
5714171b87Slntue   //   hi is an integer
5814171b87Slntue   //   mid * 2^3 is an integer
5914171b87Slntue   //   |lo| <= 2^-4.
6014171b87Slntue   // Then exp(x) = exp(hi + mid + lo) = exp(hi) * exp(mid) * exp(lo)
6114171b87Slntue   //             ~ exp(hi) * exp(mid) * (1 + lo)
6214171b87Slntue   // with relative errors < |lo|^2 <= 2^-8.
6314171b87Slntue   //   exp(hi) and exp(mid) are extracted from small lookup tables.
6414171b87Slntue 
6514171b87Slntue   // Round-to-nearest 1/8, tie-to-(+Int):
6614171b87Slntue   constexpr short accum ONE_SIXTEENTH = 0x1.0p-4hk;
6714171b87Slntue   // x_rounded = floor(x + 1/16).
6814171b87Slntue   short accum x_rounded = ((x + ONE_SIXTEENTH) >> (FXRep::FRACTION_LEN - 3))
6914171b87Slntue                           << (FXRep::FRACTION_LEN - 3);
7014171b87Slntue   short accum lo = x - x_rounded;
7114171b87Slntue 
7214171b87Slntue   // Range of x_rounded:
7314171b87Slntue   //   x_rounded >= floor((-0x1.628p2 + 0x1.0p-4) * 2^3) * 2^-3
7414171b87Slntue   //              = -0x1.6p2 = -5.5
7514171b87Slntue   // To get the indices, we shift the values so that it start with 0.
7614171b87Slntue   // Range of indices:  0 <= indices <= 89
7714171b87Slntue   StorageType indices = cpp::bit_cast<StorageType>((x_rounded + 0x1.6p2hk) >>
7814171b87Slntue                                                    (FXRep::FRACTION_LEN - 3));
7914171b87Slntue   // So we have the following relation:
8014171b87Slntue   //   indices = (hi + mid + 44/8) * 8
8114171b87Slntue   // That implies:
8214171b87Slntue   //   hi + mid = indices/8 - 5.5
8314171b87Slntue   // So for lookup tables, we can use the upper 4 bits to get:
8414171b87Slntue   //   exp( floor(indices / 8) - 5 )
8514171b87Slntue   // and lower 3 bits for:
8614171b87Slntue   //   exp( (indices - floor(indices)) - 0.5 )
8714171b87Slntue   short accum exp_hi = EXP_HI[indices >> 3];
8814171b87Slntue   short accum exp_mid = EXP_MID[indices & 0x7];
8914171b87Slntue   // exp(x) ~ exp(hi) * exp(mid) * (1 + lo);
9014171b87Slntue   return (exp_hi * (exp_mid * (0x1.0p0hk + lo)));
9114171b87Slntue }
9214171b87Slntue 
93*5ff3ff33SPetr Hosek } // namespace LIBC_NAMESPACE_DECL
94