brintos

brintos / llvm-project-archived public Read only

0
0
Text · 20.6 KiB · c9f685b Raw
538 lines · cpp
1//===-- Double-precision x^y function -------------------------------------===//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#include "src/math/pow.h"10#include "hdr/errno_macros.h"11#include "hdr/fenv_macros.h"12#include "src/__support/CPP/bit.h"13#include "src/__support/FPUtil/FEnvImpl.h"14#include "src/__support/FPUtil/FPBits.h"15#include "src/__support/FPUtil/PolyEval.h"16#include "src/__support/FPUtil/double_double.h"17#include "src/__support/FPUtil/multiply_add.h"18#include "src/__support/FPUtil/nearest_integer.h"19#include "src/__support/FPUtil/sqrt.h" // Speedup for pow(x, 1/2) = sqrt(x)20#include "src/__support/common.h"21#include "src/__support/macros/config.h"22#include "src/__support/macros/optimization.h" // LIBC_UNLIKELY23#include "src/__support/math/common_constants.h" // Lookup tables EXP_M1 and EXP_M2.24#include "src/__support/math/exp_constants.h" // Lookup tables EXP_M1 and EXP_M2.25 26namespace LIBC_NAMESPACE_DECL {27 28using fputil::DoubleDouble;29 30namespace {31 32using namespace common_constants_internal;33 34// Constants for log2(x) range reduction, generated by Sollya with:35// > for i from 0 to 127 do {36//     r = 2^-8 * ceil( 2^8 * (1 - 2^(-8)) / (1 + i*2^-7) );37//     b = nearestint(log2(r) * 2^41) * 2^-41;38//     c = round(log2(r) - b, D, RN);39//     print("{", -c, ",", -b, "},");40//   };41// This is the same as -log2(RD[i]), with the least significant bits of the42// high part set to be 2^-41, so that the sum of high parts + e_x is exact in43// double precision.44// We also replace the first and the last ones to be 0.45constexpr DoubleDouble LOG2_R_DD[128] = {46    {0.0, 0.0},47    {-0x1.19b14945cf6bap-44, 0x1.72c7ba21p-7},48    {-0x1.95539356f93dcp-43, 0x1.743ee862p-6},49    {0x1.abe0a48f83604p-43, 0x1.184b8e4c5p-5},50    {0x1.635577970e04p-43, 0x1.77394c9d9p-5},51    {-0x1.401fbaaa67e3cp-45, 0x1.d6ebd1f2p-5},52    {-0x1.5b1799ceaeb51p-43, 0x1.1bb32a6008p-4},53    {0x1.7c407050799bfp-43, 0x1.4c560fe688p-4},54    {0x1.da6339da288fcp-43, 0x1.7d60496cf8p-4},55    {0x1.be4f6f22dbbadp-43, 0x1.960caf9ab8p-4},56    {-0x1.c760bc9b188c4p-45, 0x1.c7b528b71p-4},57    {0x1.164e932b2d51cp-44, 0x1.f9c95dc1dp-4},58    {0x1.924ae921f7ecap-45, 0x1.097e38ce6p-3},59    {-0x1.6d25a5b8a19b2p-44, 0x1.22dadc2ab4p-3},60    {0x1.e50a1644ac794p-43, 0x1.3c6fb650ccp-3},61    {0x1.f34baa74a7942p-43, 0x1.494f863b8cp-3},62    {-0x1.8f7aac147fdc1p-46, 0x1.633a8bf438p-3},63    {0x1.f84be19cb9578p-43, 0x1.7046031c78p-3},64    {-0x1.66cccab240e9p-46, 0x1.8a8980abfcp-3},65    {-0x1.3f7a55cd2af4cp-47, 0x1.97c1cb13c8p-3},66    {0x1.3458cde69308cp-43, 0x1.b2602497d4p-3},67    {-0x1.667f21fa8423fp-44, 0x1.bfc67a8p-3},68    {0x1.d2fe4574e09b9p-47, 0x1.dac22d3e44p-3},69    {0x1.367bde40c5e6dp-43, 0x1.e857d3d36p-3},70    {0x1.d45da26510033p-46, 0x1.01d9bbcfa6p-2},71    {-0x1.7204f55bbf90dp-44, 0x1.08bce0d96p-2},72    {-0x1.d4f1b95e0ff45p-43, 0x1.169c05364p-2},73    {0x1.c20d74c0211bfp-44, 0x1.1d982c9d52p-2},74    {0x1.ad89a083e072ap-43, 0x1.249cd2b13cp-2},75    {0x1.cd0cb4492f1bcp-43, 0x1.32bfee370ep-2},76    {-0x1.2101a9685c779p-47, 0x1.39de8e155ap-2},77    {0x1.9451cd394fe8dp-43, 0x1.4106017c3ep-2},78    {0x1.661e393a16b95p-44, 0x1.4f6fbb2cecp-2},79    {-0x1.c6d8d86531d56p-44, 0x1.56b22e6b58p-2},80    {0x1.c1c885adb21d3p-43, 0x1.5dfdcf1eeap-2},81    {0x1.3bb5921006679p-45, 0x1.6552b49986p-2},82    {0x1.1d406db502403p-43, 0x1.6cb0f6865cp-2},83    {0x1.55a63e278bad5p-43, 0x1.7b89f02cf2p-2},84    {-0x1.66ae2a7ada553p-49, 0x1.8304d90c12p-2},85    {-0x1.66cccab240e9p-45, 0x1.8a8980abfcp-2},86    {-0x1.62404772a151dp-45, 0x1.921800924ep-2},87    {0x1.ac9bca36fd02ep-44, 0x1.99b072a96cp-2},88    {0x1.4bc302ffa76fbp-43, 0x1.a8ff97181p-2},89    {0x1.01fea1ec47c71p-43, 0x1.b0b67f4f46p-2},90    {-0x1.f20203b3186a6p-43, 0x1.b877c57b1cp-2},91    {-0x1.2642415d47384p-45, 0x1.c043859e3p-2},92    {-0x1.bc76a2753b99bp-50, 0x1.c819dc2d46p-2},93    {-0x1.da93ae3a5f451p-43, 0x1.cffae611aep-2},94    {-0x1.50e785694a8c6p-43, 0x1.d7e6c0abc4p-2},95    {0x1.c56138c894641p-43, 0x1.dfdd89d586p-2},96    {0x1.5669df6a2b592p-43, 0x1.e7df5fe538p-2},97    {-0x1.ea92d9e0e8ac2p-48, 0x1.efec61b012p-2},98    {0x1.a0331af2e6feap-43, 0x1.f804ae8d0cp-2},99    {0x1.9518ce032f41dp-48, 0x1.0014332bep-1},100    {-0x1.b3b3864c60011p-44, 0x1.042bd4b9a8p-1},101    {-0x1.103e8f00d41c8p-45, 0x1.08494c66b9p-1},102    {0x1.65be75cc3da17p-43, 0x1.0c6caaf0c5p-1},103    {0x1.3676289cd3dd4p-43, 0x1.1096015deep-1},104    {-0x1.41dfc7d7c3321p-43, 0x1.14c560fe69p-1},105    {0x1.e0cda8bd74461p-44, 0x1.18fadb6e2dp-1},106    {0x1.2a606046ad444p-44, 0x1.1d368296b5p-1},107    {0x1.f9ea977a639cp-43, 0x1.217868b0c3p-1},108    {-0x1.50520a377c7ecp-45, 0x1.25c0a0463cp-1},109    {0x1.6e3cb71b554e7p-47, 0x1.2a0f3c3407p-1},110    {-0x1.4275f1035e5e8p-48, 0x1.2e644fac05p-1},111    {-0x1.4275f1035e5e8p-48, 0x1.2e644fac05p-1},112    {-0x1.979a5db68721dp-45, 0x1.32bfee370fp-1},113    {0x1.1ee969a95f529p-43, 0x1.37222bb707p-1},114    {0x1.bb4b69336b66ep-43, 0x1.3b8b1c68fap-1},115    {0x1.d5e6a8a4fb059p-45, 0x1.3ffad4e74fp-1},116    {0x1.3106e404cabb7p-44, 0x1.44716a2c08p-1},117    {0x1.3106e404cabb7p-44, 0x1.44716a2c08p-1},118    {-0x1.9bcaf1aa4168ap-43, 0x1.48eef19318p-1},119    {0x1.1646b761c48dep-44, 0x1.4d7380dcc4p-1},120    {0x1.2f0c0bfe9dbecp-43, 0x1.51ff2e3021p-1},121    {0x1.29904613e33cp-43, 0x1.5692101d9bp-1},122    {0x1.1d406db502403p-44, 0x1.5b2c3da197p-1},123    {0x1.1d406db502403p-44, 0x1.5b2c3da197p-1},124    {-0x1.125d6cbcd1095p-44, 0x1.5fcdce2728p-1},125    {-0x1.bd9b32266d92cp-43, 0x1.6476d98adap-1},126    {0x1.54243b21709cep-44, 0x1.6927781d93p-1},127    {0x1.54243b21709cep-44, 0x1.6927781d93p-1},128    {-0x1.ce60916e52e91p-44, 0x1.6ddfc2a79p-1},129    {0x1.f1f5ae718f241p-43, 0x1.729fd26b7p-1},130    {-0x1.6eb9612e0b4f3p-43, 0x1.7767c12968p-1},131    {-0x1.6eb9612e0b4f3p-43, 0x1.7767c12968p-1},132    {0x1.fed21f9cb2cc5p-43, 0x1.7c37a9227ep-1},133    {0x1.7f5dc57266758p-43, 0x1.810fa51bf6p-1},134    {0x1.7f5dc57266758p-43, 0x1.810fa51bf6p-1},135    {0x1.5b338360c2ae2p-43, 0x1.85efd062c6p-1},136    {-0x1.96fc8f4b56502p-43, 0x1.8ad846cf37p-1},137    {-0x1.96fc8f4b56502p-43, 0x1.8ad846cf37p-1},138    {-0x1.bdc81c4db3134p-44, 0x1.8fc924c89bp-1},139    {0x1.36c101ee1344p-43, 0x1.94c287492cp-1},140    {0x1.36c101ee1344p-43, 0x1.94c287492cp-1},141    {0x1.e41fa0a62e6aep-44, 0x1.99c48be206p-1},142    {-0x1.d97ee9124773bp-46, 0x1.9ecf50bf44p-1},143    {-0x1.d97ee9124773bp-46, 0x1.9ecf50bf44p-1},144    {-0x1.3f94e00e7d6bcp-46, 0x1.a3e2f4ac44p-1},145    {-0x1.6879fa00b120ap-43, 0x1.a8ff971811p-1},146    {-0x1.6879fa00b120ap-43, 0x1.a8ff971811p-1},147    {0x1.1659d8e2d7d38p-44, 0x1.ae255819fp-1},148    {0x1.1e5e0ae0d3f8ap-43, 0x1.b35458761dp-1},149    {0x1.1e5e0ae0d3f8ap-43, 0x1.b35458761dp-1},150    {0x1.484a15babcf88p-43, 0x1.b88cb9a2abp-1},151    {0x1.484a15babcf88p-43, 0x1.b88cb9a2abp-1},152    {0x1.871a7610e40bdp-45, 0x1.bdce9dcc96p-1},153    {-0x1.2d90e5edaeceep-43, 0x1.c31a27dd01p-1},154    {-0x1.2d90e5edaeceep-43, 0x1.c31a27dd01p-1},155    {-0x1.5dd31d962d373p-43, 0x1.c86f7b7ea5p-1},156    {-0x1.5dd31d962d373p-43, 0x1.c86f7b7ea5p-1},157    {-0x1.9ad57391924a7p-43, 0x1.cdcebd2374p-1},158    {-0x1.3167ccc538261p-44, 0x1.d338120a6ep-1},159    {-0x1.3167ccc538261p-44, 0x1.d338120a6ep-1},160    {0x1.c7a4ff65ddbc9p-45, 0x1.d8aba045bp-1},161    {0x1.c7a4ff65ddbc9p-45, 0x1.d8aba045bp-1},162    {-0x1.f9ab3cf74babap-44, 0x1.de298ec0bbp-1},163    {-0x1.f9ab3cf74babap-44, 0x1.de298ec0bbp-1},164    {0x1.52842c1c1e586p-43, 0x1.e3b20546f5p-1},165    {0x1.52842c1c1e586p-43, 0x1.e3b20546f5p-1},166    {0x1.3c6764fc87b4ap-48, 0x1.e9452c8a71p-1},167    {0x1.3c6764fc87b4ap-48, 0x1.e9452c8a71p-1},168    {-0x1.a0976c0a2827dp-44, 0x1.eee32e2aedp-1},169    {-0x1.a0976c0a2827dp-44, 0x1.eee32e2aedp-1},170    {-0x1.a45314dc4fc42p-43, 0x1.f48c34bd1fp-1},171    {-0x1.a45314dc4fc42p-43, 0x1.f48c34bd1fp-1},172    {0x1.ef5d00e390ap-44, 0x1.fa406bd244p-1},173    {0.0, 1.0},174};175 176bool is_odd_integer(double x) {177  using FPBits = fputil::FPBits<double>;178  FPBits xbits(x);179  uint64_t x_u = xbits.uintval();180  unsigned x_e = static_cast<unsigned>(xbits.get_biased_exponent());181  unsigned lsb =182      static_cast<unsigned>(cpp::countr_zero(x_u | FPBits::EXP_MASK));183  constexpr unsigned UNIT_EXPONENT =184      static_cast<unsigned>(FPBits::EXP_BIAS + FPBits::FRACTION_LEN);185  return (x_e + lsb == UNIT_EXPONENT);186}187 188bool is_integer(double x) {189  using FPBits = fputil::FPBits<double>;190  FPBits xbits(x);191  uint64_t x_u = xbits.uintval();192  unsigned x_e = static_cast<unsigned>(xbits.get_biased_exponent());193  unsigned lsb =194      static_cast<unsigned>(cpp::countr_zero(x_u | FPBits::EXP_MASK));195  constexpr unsigned UNIT_EXPONENT =196      static_cast<unsigned>(FPBits::EXP_BIAS + FPBits::FRACTION_LEN);197  return (x_e + lsb >= UNIT_EXPONENT);198}199 200} // namespace201 202LLVM_LIBC_FUNCTION(double, pow, (double x, double y)) {203  using FPBits = fputil::FPBits<double>;204 205  FPBits xbits(x), ybits(y);206 207  bool x_sign = xbits.sign() == Sign::NEG;208  bool y_sign = ybits.sign() == Sign::NEG;209 210  FPBits x_abs = xbits.abs();211  FPBits y_abs = ybits.abs();212 213  uint64_t x_mant = xbits.get_mantissa();214  uint64_t y_mant = ybits.get_mantissa();215  uint64_t x_u = xbits.uintval();216  uint64_t x_a = x_abs.uintval();217  uint64_t y_a = y_abs.uintval();218 219  double e_x = static_cast<double>(xbits.get_exponent());220  uint64_t sign = 0;221 222  ///////// BEGIN - Check exceptional cases ////////////////////////////////////223  // If x or y is signaling NaN224  if (x_abs.is_signaling_nan() || y_abs.is_signaling_nan()) {225    fputil::raise_except_if_required(FE_INVALID);226    return FPBits::quiet_nan().get_val();227  }228 229  // The double precision number that is closest to 1 is (1 - 2^-53), which has230  //   log2(1 - 2^-53) ~ -1.715...p-53.231  // So if |y| > |1075 / log2(1 - 2^-53)|, and x is finite:232  //   |y * log2(x)| = 0 or > 1075.233  // Hence x^y will either overflow or underflow if x is not zero.234  if (LIBC_UNLIKELY(y_mant == 0 || y_a > 0x43d7'4910'd52d'3052 ||235                    x_u == FPBits::one().uintval() ||236                    x_u >= FPBits::inf().uintval() ||237                    x_u < FPBits::min_normal().uintval())) {238    // Exceptional exponents.239    if (y == 0.0)240      return 1.0;241 242    switch (y_a) {243    case 0x3fe0'0000'0000'0000: { // y = +-0.5244      // TODO: speed up x^(-1/2) with rsqrt(x) when available.245      if (LIBC_UNLIKELY(246              (x == 0.0 || x_u == FPBits::inf(Sign::NEG).uintval()))) {247        // pow(-0, 1/2) = +0248        // pow(-inf, 1/2) = +inf249        // Make sure it works correctly for FTZ/DAZ.250        return y_sign ? 1.0 / (x * x) : (x * x);251      }252      return y_sign ? (1.0 / fputil::sqrt<double>(x)) : fputil::sqrt<double>(x);253    }254    case 0x3ff0'0000'0000'0000: // y = +-1.0255      return y_sign ? (1.0 / x) : x;256    case 0x4000'0000'0000'0000: // y = +-2.0;257      return y_sign ? (1.0 / (x * x)) : (x * x);258    }259 260    // |y| > |1075 / log2(1 - 2^-53)|.261    if (y_a > 0x43d7'4910'd52d'3052) {262      if (y_a >= 0x7ff0'0000'0000'0000) {263        // y is inf or nan264        if (y_mant != 0) {265          // y is NaN266          // pow(1, NaN) = 1267          // pow(x, NaN) = NaN268          return (x_u == FPBits::one().uintval()) ? 1.0 : y;269        }270 271        // Now y is +-Inf272        if (x_abs.is_nan()) {273          // pow(NaN, +-Inf) = NaN274          return x;275        }276 277        if (x_a == 0x3ff0'0000'0000'0000) {278          // pow(+-1, +-Inf) = 1.0279          return 1.0;280        }281 282        if (x == 0.0 && y_sign) {283          // pow(+-0, -Inf) = +inf and raise FE_DIVBYZERO284          fputil::set_errno_if_required(EDOM);285          fputil::raise_except_if_required(FE_DIVBYZERO);286          return FPBits::inf().get_val();287        }288        // pow (|x| < 1, -inf) = +inf289        // pow (|x| < 1, +inf) = 0.0290        // pow (|x| > 1, -inf) = 0.0291        // pow (|x| > 1, +inf) = +inf292        return ((x_a < FPBits::one().uintval()) == y_sign)293                   ? FPBits::inf().get_val()294                   : 0.0;295      }296      // x^y will overflow / underflow in double precision.  Set y to a297      // large enough exponent but not too large, so that the computations298      // won't overflow in double precision.299      y = y_sign ? -0x1.0p100 : 0x1.0p100;300    }301 302    // y is finite and non-zero.303 304    if (x_u == FPBits::one().uintval()) {305      // pow(1, y) = 1306      return 1.0;307    }308 309    // TODO: Speed things up with pow(2, y) = exp2(y) and pow(10, y) = exp10(y).310 311    if (x == 0.0) {312      bool out_is_neg = x_sign && is_odd_integer(y);313      if (y_sign) {314        // pow(0, negative number) = inf315        fputil::set_errno_if_required(EDOM);316        fputil::raise_except_if_required(FE_DIVBYZERO);317        return FPBits::inf(out_is_neg ? Sign::NEG : Sign::POS).get_val();318      }319      // pow(0, positive number) = 0320      return out_is_neg ? -0.0 : 0.0;321    }322 323    if (x_a == FPBits::inf().uintval()) {324      bool out_is_neg = x_sign && is_odd_integer(y);325      if (y_sign)326        return out_is_neg ? -0.0 : 0.0;327      return FPBits::inf(out_is_neg ? Sign::NEG : Sign::POS).get_val();328    }329 330    if (x_a > FPBits::inf().uintval()) {331      // x is NaN.332      // pow (aNaN, 0) is already taken care above.333      return x;334    }335 336    // Normalize denormal inputs.337    if (x_a < FPBits::min_normal().uintval()) {338      e_x -= 64.0;339      x_mant = FPBits(x * 0x1.0p64).get_mantissa();340    }341 342    // x is finite and negative, and y is a finite integer.343    if (x_sign) {344      if (is_integer(y)) {345        x = -x;346        if (is_odd_integer(y))347          // sign = -1.0;348          sign = 0x8000'0000'0000'0000;349      } else {350        // pow( negative, non-integer ) = NaN351        fputil::set_errno_if_required(EDOM);352        fputil::raise_except_if_required(FE_INVALID);353        return FPBits::quiet_nan().get_val();354      }355    }356  }357 358  ///////// END - Check exceptional cases //////////////////////////////////////359 360  // x^y = 2^( y * log2(x) )361  //     = 2^( y * ( e_x + log2(m_x) ) )362  // First we compute log2(x) = e_x + log2(m_x)363 364  // Extract exponent field of x.365 366  // Use the highest 7 fractional bits of m_x as the index for look up tables.367  unsigned idx_x = static_cast<unsigned>(x_mant >> (FPBits::FRACTION_LEN - 7));368  // Add the hidden bit to the mantissa.369  // 1 <= m_x < 2370  FPBits m_x = FPBits(x_mant | 0x3ff0'0000'0000'0000);371 372  // Reduced argument for log2(m_x):373  //   dx = r * m_x - 1.374  // The computation is exact, and -2^-8 <= dx < 2^-7.375  // Then m_x = (1 + dx) / r, and376  //   log2(m_x) = log2( (1 + dx) / r )377  //             = log2(1 + dx) - log2(r).378 379  // In order for the overall computations x^y = 2^(y * log2(x)) to have the380  // relative errors < 2^-52 (1ULP), we will need to evaluate the exponent part381  // y * log2(x) with absolute errors < 2^-52 (or better, 2^-53).  Since the382  // whole exponent range for double precision is bounded by383  // |y * log2(x)| < 1076 ~ 2^10, we need to evaluate log2(x) with absolute384  // errors < 2^-53 * 2^-10 = 2^-63.385 386  // With that requirement, we use the following degree-6 polynomial387  // approximation:388  //   P(dx) ~ log2(1 + dx) / dx389  // Generated by Sollya with:390  // > P = fpminimax(log2(1 + x)/x, 6, [|D...|], [-2^-8, 2^-7]); P;391  // > dirtyinfnorm(log2(1 + x) - x*P, [-2^-8, 2^-7]);392  //   0x1.d03cc...p-66393  constexpr double COEFFS[] = {0x1.71547652b82fep0,  -0x1.71547652b82e7p-1,394                               0x1.ec709dc3b1fd5p-2, -0x1.7154766124215p-2,395                               0x1.2776bd90259d8p-2, -0x1.ec586c6f3d311p-3,396                               0x1.9c4775eccf524p-3};397  // Error: ulp(dx^2) <= (2^-7)^2 * 2^-52 = 2^-66398  // Extra errors from various computations and rounding directions, the overall399  // errors we can be bounded by 2^-65.400 401  double dx;402  DoubleDouble dx_c0;403 404  // Perform exact range reduction and exact product dx * c0.405#ifdef LIBC_TARGET_CPU_HAS_FMA_DOUBLE406  dx = fputil::multiply_add(RD[idx_x], m_x.get_val(), -1.0); // Exact407  dx_c0 = fputil::exact_mult(COEFFS[0], dx);408#else409  double c = FPBits(m_x.uintval() & 0x3fff'e000'0000'0000).get_val();410  dx = fputil::multiply_add(RD[idx_x], m_x.get_val() - c, CD[idx_x]); // Exact411  dx_c0 = fputil::exact_mult<double, 28>(dx, COEFFS[0]);              // Exact412#endif // LIBC_TARGET_CPU_HAS_FMA_DOUBLE413 414  double dx2 = dx * dx;415  double c0 = fputil::multiply_add(dx, COEFFS[2], COEFFS[1]);416  double c1 = fputil::multiply_add(dx, COEFFS[4], COEFFS[3]);417  double c2 = fputil::multiply_add(dx, COEFFS[6], COEFFS[5]);418 419  double p = fputil::polyeval(dx2, c0, c1, c2);420 421  // s = e_x - log2(r) + dx * P(dx)422  // Absolute error bound:423  //   |log2(x) - log2_x.hi - log2_x.lo| < 2^-65.424 425  // Notice that e_x - log2(r).hi is exact, so we perform an exact sum of426  // e_x - log2(r).hi and the high part of the product dx * c0:427  //   log2_x_hi.hi + log2_x_hi.lo = e_x - log2(r).hi + (dx * c0).hi428  DoubleDouble log2_x_hi =429      fputil::exact_add(e_x + LOG2_R_DD[idx_x].hi, dx_c0.hi);430  // The low part is dx^2 * p + low part of (dx * c0) + low part of -log2(r).431  double log2_x_lo =432      fputil::multiply_add(dx2, p, dx_c0.lo + LOG2_R_DD[idx_x].lo);433  // Perform accurate sums.434  DoubleDouble log2_x = fputil::exact_add(log2_x_hi.hi, log2_x_lo);435  log2_x.lo += log2_x_hi.lo;436 437  // To compute 2^(y * log2(x)), we break the exponent into 3 parts:438  //   y * log(2) = hi + mid + lo, where439  //   hi is an integer440  //   mid * 2^6 is an integer441  //   |lo| <= 2^-7442  // Then:443  //   x^y = 2^(y * log2(x)) = 2^hi * 2^mid * 2^lo,444  // In which 2^mid is obtained from a look-up table of size 2^6 = 64 elements,445  // and 2^lo ~ 1 + lo * P(lo).446  // Thus, we have:447  //   hi + mid = 2^-6 * round( 2^6 * y * log2(x) )448  // If we restrict the output such that |hi| < 150, (hi + mid) uses (8 + 6)449  // bits, hence, if we use double precision to perform450  //   round( 2^6 * y * log2(x))451  // the lo part is bounded by 2^-7 + 2^(-(52 - 14)) = 2^-7 + 2^-38452 453  // In the following computations:454  //   y6  = 2^6 * y455  //   hm  = 2^6 * (hi + mid) = round(2^6 * y * log2(x)) ~ round(y6 * s)456  //   lo6 = 2^6 * lo = 2^6 * (y - (hi + mid)) = y6 * log2(x) - hm.457  double y6 = y * 0x1.0p6; // Exact.458 459  DoubleDouble y6_log2_x = fputil::exact_mult(y6, log2_x.hi);460  y6_log2_x.lo = fputil::multiply_add(y6, log2_x.lo, y6_log2_x.lo);461 462  // Check overflow/underflow.463  double scale = 1.0;464 465  // |2^(hi + mid) - exp2_hi_mid| <= ulp(exp2_hi_mid) / 2466  // Clamp the exponent part into smaller range that fits double precision.467  // For those exponents that are out of range, the final conversion will round468  // them correctly to inf/max float or 0/min float accordingly.469  constexpr double UPPER_EXP_BOUND = 512.0 * 0x1.0p6;470  if (LIBC_UNLIKELY(FPBits(y6_log2_x.hi).abs().get_val() >= UPPER_EXP_BOUND)) {471    if (FPBits(y6_log2_x.hi).sign() == Sign::POS) {472      scale = 0x1.0p512;473      y6_log2_x.hi -= 512.0 * 64.0;474      if (y6_log2_x.hi > 513.0 * 64.0)475        y6_log2_x.hi = 513.0 * 64.0;476    } else {477      scale = 0x1.0p-512;478      y6_log2_x.hi += 512.0 * 64.0;479      if (y6_log2_x.hi < (-1076.0 + 512.0) * 64.0)480        y6_log2_x.hi = -564.0 * 64.0;481    }482  }483 484  double hm = fputil::nearest_integer(y6_log2_x.hi);485 486  // lo6 = 2^6 * lo.487  double lo6_hi = y6_log2_x.hi - hm;488  double lo6 = lo6_hi + y6_log2_x.lo;489 490  int hm_i = static_cast<int>(hm);491  unsigned idx_y = static_cast<unsigned>(hm_i) & 0x3f;492 493  // 2^hi494  int64_t exp2_hi_i = static_cast<int64_t>(495      static_cast<uint64_t>(static_cast<int64_t>(hm_i >> 6))496      << FPBits::FRACTION_LEN);497  // 2^mid498  int64_t exp2_mid_hi_i =499      static_cast<int64_t>(FPBits(EXP2_MID1[idx_y].hi).uintval());500  int64_t exp2_mid_lo_i =501      static_cast<int64_t>(FPBits(EXP2_MID1[idx_y].mid).uintval());502  // (-1)^sign * 2^hi * 2^mid503  // Error <= 2^hi * 2^-53504  uint64_t exp2_hm_hi_i =505      static_cast<uint64_t>(exp2_hi_i + exp2_mid_hi_i) + sign;506  // The low part could be 0.507  uint64_t exp2_hm_lo_i =508      idx_y != 0 ? static_cast<uint64_t>(exp2_hi_i + exp2_mid_lo_i) + sign509                 : sign;510  double exp2_hm_hi = FPBits(exp2_hm_hi_i).get_val();511  double exp2_hm_lo = FPBits(exp2_hm_lo_i).get_val();512 513  // Degree-5 polynomial approximation P(lo6) ~ 2^(lo6 / 2^6) = 2^(lo).514  // Generated by Sollya with:515  // > P = fpminimax(2^(x/64), 5, [|1, D...|], [-2^-1, 2^-1]);516  // > dirtyinfnorm(2^(x/64) - P, [-0.5, 0.5]);517  // 0x1.a2b77e618f5c4c176fd11b7659016cde5de83cb72p-60518  constexpr double EXP2_COEFFS[] = {0x1p0,519                                    0x1.62e42fefa39efp-7,520                                    0x1.ebfbdff82a23ap-15,521                                    0x1.c6b08d7076268p-23,522                                    0x1.3b2ad33f8b48bp-31,523                                    0x1.5d870c4d84445p-40};524 525  double lo6_sqr = lo6 * lo6;526 527  double d0 = fputil::multiply_add(lo6, EXP2_COEFFS[2], EXP2_COEFFS[1]);528  double d1 = fputil::multiply_add(lo6, EXP2_COEFFS[4], EXP2_COEFFS[3]);529  double pp = fputil::polyeval(lo6_sqr, d0, d1, EXP2_COEFFS[5]);530 531  double r = fputil::multiply_add(exp2_hm_hi * lo6, pp, exp2_hm_lo);532  r += exp2_hm_hi;533 534  return r * scale;535}536 537} // namespace LIBC_NAMESPACE_DECL538