64 lines · c
1//===-- Common utils for exp10f16 -------------------------------*- C++ -*-===//2//3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.4// See https://llvm.org/LICENSE.txt for license information.5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception6//7//===----------------------------------------------------------------------===//8 9#ifndef LLVM_LIBC_SRC___SUPPORT_MATH_EXP10F16_UTILS_H10#define LLVM_LIBC_SRC___SUPPORT_MATH_EXP10F16_UTILS_H11 12#include "include/llvm-libc-macros/float16-macros.h"13 14#ifdef LIBC_TYPES_HAS_FLOAT1615 16#include "exp10_float16_constants.h"17#include "expf16_utils.h"18#include "src/__support/FPUtil/FPBits.h"19 20namespace LIBC_NAMESPACE_DECL {21 22LIBC_INLINE static ExpRangeReduction exp10_range_reduction(float16 x) {23 // For -8 < x < 5, to compute 10^x, we perform the following range reduction:24 // find hi, mid, lo, such that:25 // x = (hi + mid) * log2(10) + lo, in which26 // hi is an integer,27 // mid * 2^3 is an integer,28 // -2^(-4) <= lo < 2^(-4).29 // In particular,30 // hi + mid = round(x * 2^3) * 2^(-3).31 // Then,32 // 10^x = 10^(hi + mid + lo) = 2^((hi + mid) * log2(10)) + 10^lo33 // We store 2^mid in the lookup table EXP2_MID_BITS, and compute 2^hi * 2^mid34 // by adding hi to the exponent field of 2^mid. 10^lo is computed using a35 // degree-4 minimax polynomial generated by Sollya.36 37 float xf = x;38 float kf = fputil::nearest_integer(xf * (LOG2F_10 * 0x1.0p+3f));39 int x_hi_mid = static_cast<int>(kf);40 unsigned x_hi = static_cast<unsigned>(x_hi_mid) >> 3;41 unsigned x_mid = static_cast<unsigned>(x_hi_mid) & 0x7;42 // lo = x - (hi + mid) = round(x * 2^3 * log2(10)) * log10(2) * (-2^(-3)) + x43 float lo = fputil::multiply_add(kf, LOG10F_2 * -0x1.0p-3f, xf);44 45 uint32_t exp2_hi_mid_bits =46 EXP2_MID_BITS[x_mid] +47 static_cast<uint32_t>(x_hi << fputil::FPBits<float>::FRACTION_LEN);48 float exp2_hi_mid = fputil::FPBits<float>(exp2_hi_mid_bits).get_val();49 // Degree-4 minimax polynomial generated by Sollya with the following50 // commands:51 // > display = hexadecimal;52 // > P = fpminimax((10^x - 1)/x, 3, [|SG...|], [-2^-4, 2^-4]);53 // > 1 + x * P;54 float exp10_lo = fputil::polyeval(lo, 0x1p+0f, 0x1.26bb14p+1f, 0x1.53526p+1f,55 0x1.04b434p+1f, 0x1.2bcf9ep+0f);56 return {exp2_hi_mid, exp10_lo};57}58 59} // namespace LIBC_NAMESPACE_DECL60 61#endif // LIBC_TYPES_HAS_FLOAT1662 63#endif // LLVM_LIBC_SRC___SUPPORT_MATH_EXP10F16_UTILS_H64