blob: eb8c9290902784d3ff609bd8fda2d6bbc92575f5 [file] [edit]
//===-- Implementation of exphk function ----------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
#include "exphk.h"
#include "src/__support/CPP/bit.h"
#include "src/__support/common.h"
#include "src/__support/fixed_point/fx_bits.h"
#include "src/__support/macros/config.h"
namespace LIBC_NAMESPACE_DECL {
namespace {
// Look up tables for exp(hi) and exp(mid).
// Generated with Sollya:
// > for i from 0 to 11 do {
// hi = i - 6;
// e_hi = nearestint(exp(hi) * 2^7) * 2^-7;
// print(hi, e_hi);
// };
// > for i from 0 to 7 do {
// m = i/8;
// e_mid = nearestint(exp(m) * 2^7) * 2^-7;
// print(m, e_mid);
// };
static constexpr short accum EXP_HI[12] = {
0x0.0p0hk, 0x1.0p-7hk, 0x1.0p-6hk, 0x1.8p-5hk, 0x1.1p-3hk, 0x1.78p-2hk,
0x1.0p0hk, 0x1.5cp1hk, 0x1.d9p2hk, 0x1.416p4hk, 0x1.b4dp5hk, 0x1.28d4p7hk,
};
static constexpr short accum EXP_MID[8] = {
0x1.0p0hk, 0x1.22p0hk, 0x1.48p0hk, 0x1.74p0hk,
0x1.a6p0hk, 0x1.dep0hk, 0x1.0fp1hk, 0x1.33p1hk,
};
} // anonymous namespace
LLVM_LIBC_FUNCTION(short accum, exphk, (short accum x)) {
using FXRep = fixed_point::FXRep<short accum>;
using StorageType = typename FXRep::StorageType;
// Output overflow
// ceil(log(2^8) * 2^7) * 2^-7
if (LIBC_UNLIKELY(x >= 0x1.63p2hk))
return FXRep::MAX();
// Lower bound where exp(x) -> 0:
// floor(log(2^-8) * 2^7) * 2^-7
if (LIBC_UNLIKELY(x <= -0x1.63p2hk))
return FXRep::ZERO();
// Current range of x:
// -0x1.628p2 <= x <= 0x1.628p2
// Range reduction:
// x = hi + mid + lo,
// where:
// hi is an integer
// mid * 2^3 is an integer
// |lo| <= 2^-4.
// Then exp(x) = exp(hi + mid + lo) = exp(hi) * exp(mid) * exp(lo)
// ~ exp(hi) * exp(mid) * (1 + lo)
// with relative errors < |lo|^2 <= 2^-8.
// exp(hi) and exp(mid) are extracted from small lookup tables.
// Round-to-nearest 1/8, tie-to-(+Inf):
constexpr short accum ONE_SIXTEENTH = 0x1.0p-4hk;
// x_rounded = floor((x + 1/16) * 2^3) * 2^-3.
short accum x_rounded = ((x + ONE_SIXTEENTH) >> (FXRep::FRACTION_LEN - 3))
<< (FXRep::FRACTION_LEN - 3);
short accum lo = x - x_rounded;
// Range of x_rounded:
// x_rounded >= floor((-0x1.628p2 + 0x1.0p-4) * 2^3) * 2^-3
// = -0x1.6p2 = -5.5
// x_rounded <= floor((0x1.628p2 + 0x1.0p-4) * 2^3) * 2^-3
// = 0x1.6p2 = 5.5
// As hi = floor(x_rounded) >= -6, we add 6 (0x1.8p2hk) so that hi + 6 >= 0.
// Range of indices: 4 <= indices <= 92
StorageType indices = cpp::bit_cast<StorageType>((x_rounded + 0x1.8p2hk) >>
(FXRep::FRACTION_LEN - 3));
// So we have the following relation:
// indices = (hi + mid + 6) * 8
// That implies:
// hi + mid = indices/8 - 6
// So for lookup tables, we can use the upper 4 bits to get:
// exp( floor(indices / 8) - 6 )
// and lower 3 bits for:
// exp( indices/8 - floor(indices / 8) )
short accum exp_hi = EXP_HI[indices >> 3];
short accum exp_mid = EXP_MID[indices & 0x7];
// exp(x) ~ exp(hi) * exp(mid) * (1 + lo);
return exp_hi * (exp_mid * (0x1.0p0hk + lo));
}
} // namespace LIBC_NAMESPACE_DECL