xref: /llvm-project/libc/src/math/generic/pow.cpp (revision 0f4b3c409fbd756d826c89d5539d9ea22bcc56aa)
19f6b440aSlntue //===-- Double-precision x^y function -------------------------------------===//
29f6b440aSlntue //
39f6b440aSlntue // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
49f6b440aSlntue // See https://llvm.org/LICENSE.txt for license information.
59f6b440aSlntue // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
69f6b440aSlntue //
79f6b440aSlntue //===----------------------------------------------------------------------===//
89f6b440aSlntue 
99f6b440aSlntue #include "src/math/pow.h"
109f6b440aSlntue #include "common_constants.h" // Lookup tables EXP_M1 and EXP_M2.
119f6b440aSlntue #include "hdr/errno_macros.h"
129f6b440aSlntue #include "hdr/fenv_macros.h"
139f6b440aSlntue #include "src/__support/CPP/bit.h"
149f6b440aSlntue #include "src/__support/FPUtil/FEnvImpl.h"
159f6b440aSlntue #include "src/__support/FPUtil/FPBits.h"
169f6b440aSlntue #include "src/__support/FPUtil/PolyEval.h"
179f6b440aSlntue #include "src/__support/FPUtil/double_double.h"
189f6b440aSlntue #include "src/__support/FPUtil/multiply_add.h"
199f6b440aSlntue #include "src/__support/FPUtil/nearest_integer.h"
209f6b440aSlntue #include "src/__support/FPUtil/sqrt.h" // Speedup for pow(x, 1/2) = sqrt(x)
219f6b440aSlntue #include "src/__support/common.h"
229f6b440aSlntue #include "src/__support/macros/config.h"
239f6b440aSlntue #include "src/__support/macros/optimization.h" // LIBC_UNLIKELY
249f6b440aSlntue 
259f6b440aSlntue namespace LIBC_NAMESPACE_DECL {
269f6b440aSlntue 
279f6b440aSlntue using fputil::DoubleDouble;
289f6b440aSlntue 
299f6b440aSlntue namespace {
309f6b440aSlntue 
319f6b440aSlntue // Constants for log2(x) range reduction, generated by Sollya with:
329f6b440aSlntue // > for i from 0 to 127 do {
339f6b440aSlntue //     r = 2^-8 * ceil( 2^8 * (1 - 2^(-8)) / (1 + i*2^-7) );
349f6b440aSlntue //     b = nearestint(log2(r) * 2^41) * 2^-41;
359f6b440aSlntue //     c = round(log2(r) - b, D, RN);
369f6b440aSlntue //     print("{", -c, ",", -b, "},");
379f6b440aSlntue //   };
389f6b440aSlntue // This is the same as -log2(RD[i]), with the least significant bits of the
399f6b440aSlntue // high part set to be 2^-41, so that the sum of high parts + e_x is exact in
409f6b440aSlntue // double precision.
419f6b440aSlntue // We also replace the first and the last ones to be 0.
429f6b440aSlntue constexpr DoubleDouble LOG2_R_DD[128] = {
439f6b440aSlntue     {0.0, 0.0},
449f6b440aSlntue     {-0x1.19b14945cf6bap-44, 0x1.72c7ba21p-7},
459f6b440aSlntue     {-0x1.95539356f93dcp-43, 0x1.743ee862p-6},
469f6b440aSlntue     {0x1.abe0a48f83604p-43, 0x1.184b8e4c5p-5},
479f6b440aSlntue     {0x1.635577970e04p-43, 0x1.77394c9d9p-5},
489f6b440aSlntue     {-0x1.401fbaaa67e3cp-45, 0x1.d6ebd1f2p-5},
499f6b440aSlntue     {-0x1.5b1799ceaeb51p-43, 0x1.1bb32a6008p-4},
509f6b440aSlntue     {0x1.7c407050799bfp-43, 0x1.4c560fe688p-4},
519f6b440aSlntue     {0x1.da6339da288fcp-43, 0x1.7d60496cf8p-4},
529f6b440aSlntue     {0x1.be4f6f22dbbadp-43, 0x1.960caf9ab8p-4},
539f6b440aSlntue     {-0x1.c760bc9b188c4p-45, 0x1.c7b528b71p-4},
549f6b440aSlntue     {0x1.164e932b2d51cp-44, 0x1.f9c95dc1dp-4},
559f6b440aSlntue     {0x1.924ae921f7ecap-45, 0x1.097e38ce6p-3},
569f6b440aSlntue     {-0x1.6d25a5b8a19b2p-44, 0x1.22dadc2ab4p-3},
579f6b440aSlntue     {0x1.e50a1644ac794p-43, 0x1.3c6fb650ccp-3},
589f6b440aSlntue     {0x1.f34baa74a7942p-43, 0x1.494f863b8cp-3},
599f6b440aSlntue     {-0x1.8f7aac147fdc1p-46, 0x1.633a8bf438p-3},
609f6b440aSlntue     {0x1.f84be19cb9578p-43, 0x1.7046031c78p-3},
619f6b440aSlntue     {-0x1.66cccab240e9p-46, 0x1.8a8980abfcp-3},
629f6b440aSlntue     {-0x1.3f7a55cd2af4cp-47, 0x1.97c1cb13c8p-3},
639f6b440aSlntue     {0x1.3458cde69308cp-43, 0x1.b2602497d4p-3},
649f6b440aSlntue     {-0x1.667f21fa8423fp-44, 0x1.bfc67a8p-3},
659f6b440aSlntue     {0x1.d2fe4574e09b9p-47, 0x1.dac22d3e44p-3},
669f6b440aSlntue     {0x1.367bde40c5e6dp-43, 0x1.e857d3d36p-3},
679f6b440aSlntue     {0x1.d45da26510033p-46, 0x1.01d9bbcfa6p-2},
689f6b440aSlntue     {-0x1.7204f55bbf90dp-44, 0x1.08bce0d96p-2},
699f6b440aSlntue     {-0x1.d4f1b95e0ff45p-43, 0x1.169c05364p-2},
709f6b440aSlntue     {0x1.c20d74c0211bfp-44, 0x1.1d982c9d52p-2},
719f6b440aSlntue     {0x1.ad89a083e072ap-43, 0x1.249cd2b13cp-2},
729f6b440aSlntue     {0x1.cd0cb4492f1bcp-43, 0x1.32bfee370ep-2},
739f6b440aSlntue     {-0x1.2101a9685c779p-47, 0x1.39de8e155ap-2},
749f6b440aSlntue     {0x1.9451cd394fe8dp-43, 0x1.4106017c3ep-2},
759f6b440aSlntue     {0x1.661e393a16b95p-44, 0x1.4f6fbb2cecp-2},
769f6b440aSlntue     {-0x1.c6d8d86531d56p-44, 0x1.56b22e6b58p-2},
779f6b440aSlntue     {0x1.c1c885adb21d3p-43, 0x1.5dfdcf1eeap-2},
789f6b440aSlntue     {0x1.3bb5921006679p-45, 0x1.6552b49986p-2},
799f6b440aSlntue     {0x1.1d406db502403p-43, 0x1.6cb0f6865cp-2},
809f6b440aSlntue     {0x1.55a63e278bad5p-43, 0x1.7b89f02cf2p-2},
819f6b440aSlntue     {-0x1.66ae2a7ada553p-49, 0x1.8304d90c12p-2},
829f6b440aSlntue     {-0x1.66cccab240e9p-45, 0x1.8a8980abfcp-2},
839f6b440aSlntue     {-0x1.62404772a151dp-45, 0x1.921800924ep-2},
849f6b440aSlntue     {0x1.ac9bca36fd02ep-44, 0x1.99b072a96cp-2},
859f6b440aSlntue     {0x1.4bc302ffa76fbp-43, 0x1.a8ff97181p-2},
869f6b440aSlntue     {0x1.01fea1ec47c71p-43, 0x1.b0b67f4f46p-2},
879f6b440aSlntue     {-0x1.f20203b3186a6p-43, 0x1.b877c57b1cp-2},
889f6b440aSlntue     {-0x1.2642415d47384p-45, 0x1.c043859e3p-2},
899f6b440aSlntue     {-0x1.bc76a2753b99bp-50, 0x1.c819dc2d46p-2},
909f6b440aSlntue     {-0x1.da93ae3a5f451p-43, 0x1.cffae611aep-2},
919f6b440aSlntue     {-0x1.50e785694a8c6p-43, 0x1.d7e6c0abc4p-2},
929f6b440aSlntue     {0x1.c56138c894641p-43, 0x1.dfdd89d586p-2},
939f6b440aSlntue     {0x1.5669df6a2b592p-43, 0x1.e7df5fe538p-2},
949f6b440aSlntue     {-0x1.ea92d9e0e8ac2p-48, 0x1.efec61b012p-2},
959f6b440aSlntue     {0x1.a0331af2e6feap-43, 0x1.f804ae8d0cp-2},
969f6b440aSlntue     {0x1.9518ce032f41dp-48, 0x1.0014332bep-1},
979f6b440aSlntue     {-0x1.b3b3864c60011p-44, 0x1.042bd4b9a8p-1},
989f6b440aSlntue     {-0x1.103e8f00d41c8p-45, 0x1.08494c66b9p-1},
999f6b440aSlntue     {0x1.65be75cc3da17p-43, 0x1.0c6caaf0c5p-1},
1009f6b440aSlntue     {0x1.3676289cd3dd4p-43, 0x1.1096015deep-1},
1019f6b440aSlntue     {-0x1.41dfc7d7c3321p-43, 0x1.14c560fe69p-1},
1029f6b440aSlntue     {0x1.e0cda8bd74461p-44, 0x1.18fadb6e2dp-1},
1039f6b440aSlntue     {0x1.2a606046ad444p-44, 0x1.1d368296b5p-1},
1049f6b440aSlntue     {0x1.f9ea977a639cp-43, 0x1.217868b0c3p-1},
1059f6b440aSlntue     {-0x1.50520a377c7ecp-45, 0x1.25c0a0463cp-1},
1069f6b440aSlntue     {0x1.6e3cb71b554e7p-47, 0x1.2a0f3c3407p-1},
1079f6b440aSlntue     {-0x1.4275f1035e5e8p-48, 0x1.2e644fac05p-1},
1089f6b440aSlntue     {-0x1.4275f1035e5e8p-48, 0x1.2e644fac05p-1},
1099f6b440aSlntue     {-0x1.979a5db68721dp-45, 0x1.32bfee370fp-1},
1109f6b440aSlntue     {0x1.1ee969a95f529p-43, 0x1.37222bb707p-1},
1119f6b440aSlntue     {0x1.bb4b69336b66ep-43, 0x1.3b8b1c68fap-1},
1129f6b440aSlntue     {0x1.d5e6a8a4fb059p-45, 0x1.3ffad4e74fp-1},
1139f6b440aSlntue     {0x1.3106e404cabb7p-44, 0x1.44716a2c08p-1},
1149f6b440aSlntue     {0x1.3106e404cabb7p-44, 0x1.44716a2c08p-1},
1159f6b440aSlntue     {-0x1.9bcaf1aa4168ap-43, 0x1.48eef19318p-1},
1169f6b440aSlntue     {0x1.1646b761c48dep-44, 0x1.4d7380dcc4p-1},
1179f6b440aSlntue     {0x1.2f0c0bfe9dbecp-43, 0x1.51ff2e3021p-1},
1189f6b440aSlntue     {0x1.29904613e33cp-43, 0x1.5692101d9bp-1},
1199f6b440aSlntue     {0x1.1d406db502403p-44, 0x1.5b2c3da197p-1},
1209f6b440aSlntue     {0x1.1d406db502403p-44, 0x1.5b2c3da197p-1},
1219f6b440aSlntue     {-0x1.125d6cbcd1095p-44, 0x1.5fcdce2728p-1},
1229f6b440aSlntue     {-0x1.bd9b32266d92cp-43, 0x1.6476d98adap-1},
1239f6b440aSlntue     {0x1.54243b21709cep-44, 0x1.6927781d93p-1},
1249f6b440aSlntue     {0x1.54243b21709cep-44, 0x1.6927781d93p-1},
1259f6b440aSlntue     {-0x1.ce60916e52e91p-44, 0x1.6ddfc2a79p-1},
1269f6b440aSlntue     {0x1.f1f5ae718f241p-43, 0x1.729fd26b7p-1},
1279f6b440aSlntue     {-0x1.6eb9612e0b4f3p-43, 0x1.7767c12968p-1},
1289f6b440aSlntue     {-0x1.6eb9612e0b4f3p-43, 0x1.7767c12968p-1},
1299f6b440aSlntue     {0x1.fed21f9cb2cc5p-43, 0x1.7c37a9227ep-1},
1309f6b440aSlntue     {0x1.7f5dc57266758p-43, 0x1.810fa51bf6p-1},
1319f6b440aSlntue     {0x1.7f5dc57266758p-43, 0x1.810fa51bf6p-1},
1329f6b440aSlntue     {0x1.5b338360c2ae2p-43, 0x1.85efd062c6p-1},
1339f6b440aSlntue     {-0x1.96fc8f4b56502p-43, 0x1.8ad846cf37p-1},
1349f6b440aSlntue     {-0x1.96fc8f4b56502p-43, 0x1.8ad846cf37p-1},
1359f6b440aSlntue     {-0x1.bdc81c4db3134p-44, 0x1.8fc924c89bp-1},
1369f6b440aSlntue     {0x1.36c101ee1344p-43, 0x1.94c287492cp-1},
1379f6b440aSlntue     {0x1.36c101ee1344p-43, 0x1.94c287492cp-1},
1389f6b440aSlntue     {0x1.e41fa0a62e6aep-44, 0x1.99c48be206p-1},
1399f6b440aSlntue     {-0x1.d97ee9124773bp-46, 0x1.9ecf50bf44p-1},
1409f6b440aSlntue     {-0x1.d97ee9124773bp-46, 0x1.9ecf50bf44p-1},
1419f6b440aSlntue     {-0x1.3f94e00e7d6bcp-46, 0x1.a3e2f4ac44p-1},
1429f6b440aSlntue     {-0x1.6879fa00b120ap-43, 0x1.a8ff971811p-1},
1439f6b440aSlntue     {-0x1.6879fa00b120ap-43, 0x1.a8ff971811p-1},
1449f6b440aSlntue     {0x1.1659d8e2d7d38p-44, 0x1.ae255819fp-1},
1459f6b440aSlntue     {0x1.1e5e0ae0d3f8ap-43, 0x1.b35458761dp-1},
1469f6b440aSlntue     {0x1.1e5e0ae0d3f8ap-43, 0x1.b35458761dp-1},
1479f6b440aSlntue     {0x1.484a15babcf88p-43, 0x1.b88cb9a2abp-1},
1489f6b440aSlntue     {0x1.484a15babcf88p-43, 0x1.b88cb9a2abp-1},
1499f6b440aSlntue     {0x1.871a7610e40bdp-45, 0x1.bdce9dcc96p-1},
1509f6b440aSlntue     {-0x1.2d90e5edaeceep-43, 0x1.c31a27dd01p-1},
1519f6b440aSlntue     {-0x1.2d90e5edaeceep-43, 0x1.c31a27dd01p-1},
1529f6b440aSlntue     {-0x1.5dd31d962d373p-43, 0x1.c86f7b7ea5p-1},
1539f6b440aSlntue     {-0x1.5dd31d962d373p-43, 0x1.c86f7b7ea5p-1},
1549f6b440aSlntue     {-0x1.9ad57391924a7p-43, 0x1.cdcebd2374p-1},
1559f6b440aSlntue     {-0x1.3167ccc538261p-44, 0x1.d338120a6ep-1},
1569f6b440aSlntue     {-0x1.3167ccc538261p-44, 0x1.d338120a6ep-1},
1579f6b440aSlntue     {0x1.c7a4ff65ddbc9p-45, 0x1.d8aba045bp-1},
1589f6b440aSlntue     {0x1.c7a4ff65ddbc9p-45, 0x1.d8aba045bp-1},
1599f6b440aSlntue     {-0x1.f9ab3cf74babap-44, 0x1.de298ec0bbp-1},
1609f6b440aSlntue     {-0x1.f9ab3cf74babap-44, 0x1.de298ec0bbp-1},
1619f6b440aSlntue     {0x1.52842c1c1e586p-43, 0x1.e3b20546f5p-1},
1629f6b440aSlntue     {0x1.52842c1c1e586p-43, 0x1.e3b20546f5p-1},
1639f6b440aSlntue     {0x1.3c6764fc87b4ap-48, 0x1.e9452c8a71p-1},
1649f6b440aSlntue     {0x1.3c6764fc87b4ap-48, 0x1.e9452c8a71p-1},
1659f6b440aSlntue     {-0x1.a0976c0a2827dp-44, 0x1.eee32e2aedp-1},
1669f6b440aSlntue     {-0x1.a0976c0a2827dp-44, 0x1.eee32e2aedp-1},
1679f6b440aSlntue     {-0x1.a45314dc4fc42p-43, 0x1.f48c34bd1fp-1},
1689f6b440aSlntue     {-0x1.a45314dc4fc42p-43, 0x1.f48c34bd1fp-1},
1699f6b440aSlntue     {0x1.ef5d00e390ap-44, 0x1.fa406bd244p-1},
1709f6b440aSlntue     {0.0, 1.0},
1719f6b440aSlntue };
1729f6b440aSlntue 
1739f6b440aSlntue bool is_odd_integer(double x) {
1749f6b440aSlntue   using FPBits = fputil::FPBits<double>;
1759f6b440aSlntue   FPBits xbits(x);
1769f6b440aSlntue   uint64_t x_u = xbits.uintval();
1779f6b440aSlntue   unsigned x_e = static_cast<unsigned>(xbits.get_biased_exponent());
1789f6b440aSlntue   unsigned lsb =
1799f6b440aSlntue       static_cast<unsigned>(cpp::countr_zero(x_u | FPBits::EXP_MASK));
1809f6b440aSlntue   constexpr unsigned UNIT_EXPONENT =
1819f6b440aSlntue       static_cast<unsigned>(FPBits::EXP_BIAS + FPBits::FRACTION_LEN);
1829f6b440aSlntue   return (x_e + lsb == UNIT_EXPONENT);
1839f6b440aSlntue }
1849f6b440aSlntue 
1859f6b440aSlntue bool is_integer(double x) {
1869f6b440aSlntue   using FPBits = fputil::FPBits<double>;
1879f6b440aSlntue   FPBits xbits(x);
1889f6b440aSlntue   uint64_t x_u = xbits.uintval();
1899f6b440aSlntue   unsigned x_e = static_cast<unsigned>(xbits.get_biased_exponent());
1909f6b440aSlntue   unsigned lsb =
1919f6b440aSlntue       static_cast<unsigned>(cpp::countr_zero(x_u | FPBits::EXP_MASK));
1929f6b440aSlntue   constexpr unsigned UNIT_EXPONENT =
1939f6b440aSlntue       static_cast<unsigned>(FPBits::EXP_BIAS + FPBits::FRACTION_LEN);
1949f6b440aSlntue   return (x_e + lsb >= UNIT_EXPONENT);
1959f6b440aSlntue }
1969f6b440aSlntue 
1979f6b440aSlntue } // namespace
1989f6b440aSlntue 
1999f6b440aSlntue LLVM_LIBC_FUNCTION(double, pow, (double x, double y)) {
2009f6b440aSlntue   using FPBits = fputil::FPBits<double>;
2019f6b440aSlntue 
2029f6b440aSlntue   FPBits xbits(x), ybits(y);
2039f6b440aSlntue 
2049f6b440aSlntue   bool x_sign = xbits.sign() == Sign::NEG;
2059f6b440aSlntue   bool y_sign = ybits.sign() == Sign::NEG;
2069f6b440aSlntue 
2079f6b440aSlntue   FPBits x_abs = xbits.abs();
2089f6b440aSlntue   FPBits y_abs = ybits.abs();
2099f6b440aSlntue 
2109f6b440aSlntue   uint64_t x_mant = xbits.get_mantissa();
2119f6b440aSlntue   uint64_t y_mant = ybits.get_mantissa();
2129f6b440aSlntue   uint64_t x_u = xbits.uintval();
2139f6b440aSlntue   uint64_t x_a = x_abs.uintval();
2149f6b440aSlntue   uint64_t y_a = y_abs.uintval();
2159f6b440aSlntue 
2169f6b440aSlntue   double e_x = static_cast<double>(xbits.get_exponent());
2179f6b440aSlntue   uint64_t sign = 0;
2189f6b440aSlntue 
2199f6b440aSlntue   ///////// BEGIN - Check exceptional cases ////////////////////////////////////
2209f6b440aSlntue 
2219f6b440aSlntue   // The double precision number that is closest to 1 is (1 - 2^-53), which has
2229f6b440aSlntue   //   log2(1 - 2^-53) ~ -1.715...p-53.
2239f6b440aSlntue   // So if |y| > |1075 / log2(1 - 2^-53)|, and x is finite:
2249f6b440aSlntue   //   |y * log2(x)| = 0 or > 1075.
2259f6b440aSlntue   // Hence x^y will either overflow or underflow if x is not zero.
2269f6b440aSlntue   if (LIBC_UNLIKELY(y_mant == 0 || y_a > 0x43d7'4910'd52d'3052 ||
2279f6b440aSlntue                     x_u == FPBits::one().uintval() ||
2289f6b440aSlntue                     x_u >= FPBits::inf().uintval() ||
2299f6b440aSlntue                     x_u < FPBits::min_normal().uintval())) {
2309f6b440aSlntue     // Exceptional exponents.
231*0f4b3c40Slntue     if (y == 0.0)
2329f6b440aSlntue       return 1.0;
233*0f4b3c40Slntue 
234*0f4b3c40Slntue     switch (y_a) {
23579ecb814Slntue     case 0x3fe0'0000'0000'0000: { // y = +-0.5
2369f6b440aSlntue       // TODO: speed up x^(-1/2) with rsqrt(x) when available.
237*0f4b3c40Slntue       if (LIBC_UNLIKELY(
238*0f4b3c40Slntue               (x == 0.0 || x_u == FPBits::inf(Sign::NEG).uintval()))) {
23979ecb814Slntue         // pow(-0, 1/2) = +0
24079ecb814Slntue         // pow(-inf, 1/2) = +inf
241*0f4b3c40Slntue         // Make sure it works correctly for FTZ/DAZ.
242*0f4b3c40Slntue         return y_sign ? 1.0 / (x * x) : (x * x);
24379ecb814Slntue       }
2449f6b440aSlntue       return y_sign ? (1.0 / fputil::sqrt<double>(x)) : fputil::sqrt<double>(x);
24579ecb814Slntue     }
2469f6b440aSlntue     case 0x3ff0'0000'0000'0000: // y = +-1.0
2479f6b440aSlntue       return y_sign ? (1.0 / x) : x;
2489f6b440aSlntue     case 0x4000'0000'0000'0000: // y = +-2.0;
2499f6b440aSlntue       return y_sign ? (1.0 / (x * x)) : (x * x);
2509f6b440aSlntue     }
2519f6b440aSlntue 
2529f6b440aSlntue     // |y| > |1075 / log2(1 - 2^-53)|.
2539f6b440aSlntue     if (y_a > 0x43d7'4910'd52d'3052) {
2549f6b440aSlntue       if (y_a >= 0x7ff0'0000'0000'0000) {
2559f6b440aSlntue         // y is inf or nan
2569f6b440aSlntue         if (y_mant != 0) {
2579f6b440aSlntue           // y is NaN
2589f6b440aSlntue           // pow(1, NaN) = 1
2599f6b440aSlntue           // pow(x, NaN) = NaN
2609f6b440aSlntue           return (x_u == FPBits::one().uintval()) ? 1.0 : y;
2619f6b440aSlntue         }
2629f6b440aSlntue 
2639f6b440aSlntue         // Now y is +-Inf
2649f6b440aSlntue         if (x_abs.is_nan()) {
2659f6b440aSlntue           // pow(NaN, +-Inf) = NaN
2669f6b440aSlntue           return x;
2679f6b440aSlntue         }
2689f6b440aSlntue 
2699f6b440aSlntue         if (x_a == 0x3ff0'0000'0000'0000) {
2709f6b440aSlntue           // pow(+-1, +-Inf) = 1.0
2719f6b440aSlntue           return 1.0;
2729f6b440aSlntue         }
2739f6b440aSlntue 
274*0f4b3c40Slntue         if (x == 0.0 && y_sign) {
2759f6b440aSlntue           // pow(+-0, -Inf) = +inf and raise FE_DIVBYZERO
2769f6b440aSlntue           fputil::set_errno_if_required(EDOM);
2779f6b440aSlntue           fputil::raise_except_if_required(FE_DIVBYZERO);
2789f6b440aSlntue           return FPBits::inf().get_val();
2799f6b440aSlntue         }
2809f6b440aSlntue         // pow (|x| < 1, -inf) = +inf
2819f6b440aSlntue         // pow (|x| < 1, +inf) = 0.0
2829f6b440aSlntue         // pow (|x| > 1, -inf) = 0.0
2839f6b440aSlntue         // pow (|x| > 1, +inf) = +inf
2849f6b440aSlntue         return ((x_a < FPBits::one().uintval()) == y_sign)
2859f6b440aSlntue                    ? FPBits::inf().get_val()
2869f6b440aSlntue                    : 0.0;
2879f6b440aSlntue       }
2889f6b440aSlntue       // x^y will overflow / underflow in double precision.  Set y to a
2899f6b440aSlntue       // large enough exponent but not too large, so that the computations
2909f6b440aSlntue       // won't overflow in double precision.
2919f6b440aSlntue       y = y_sign ? -0x1.0p100 : 0x1.0p100;
2929f6b440aSlntue     }
2939f6b440aSlntue 
2949f6b440aSlntue     // y is finite and non-zero.
2959f6b440aSlntue 
2969f6b440aSlntue     if (x_u == FPBits::one().uintval()) {
2979f6b440aSlntue       // pow(1, y) = 1
2989f6b440aSlntue       return 1.0;
2999f6b440aSlntue     }
3009f6b440aSlntue 
3019f6b440aSlntue     // TODO: Speed things up with pow(2, y) = exp2(y) and pow(10, y) = exp10(y).
3029f6b440aSlntue 
303*0f4b3c40Slntue     if (x == 0.0) {
3049f6b440aSlntue       bool out_is_neg = x_sign && is_odd_integer(y);
3059f6b440aSlntue       if (y_sign) {
3069f6b440aSlntue         // pow(0, negative number) = inf
3079f6b440aSlntue         fputil::set_errno_if_required(EDOM);
3089f6b440aSlntue         fputil::raise_except_if_required(FE_DIVBYZERO);
3099f6b440aSlntue         return FPBits::inf(out_is_neg ? Sign::NEG : Sign::POS).get_val();
3109f6b440aSlntue       }
3119f6b440aSlntue       // pow(0, positive number) = 0
3129f6b440aSlntue       return out_is_neg ? -0.0 : 0.0;
3139f6b440aSlntue     }
3149f6b440aSlntue 
3159f6b440aSlntue     if (x_a == FPBits::inf().uintval()) {
3169f6b440aSlntue       bool out_is_neg = x_sign && is_odd_integer(y);
3179f6b440aSlntue       if (y_sign)
3189f6b440aSlntue         return out_is_neg ? -0.0 : 0.0;
3199f6b440aSlntue       return FPBits::inf(out_is_neg ? Sign::NEG : Sign::POS).get_val();
3209f6b440aSlntue     }
3219f6b440aSlntue 
3229f6b440aSlntue     if (x_a > FPBits::inf().uintval()) {
3239f6b440aSlntue       // x is NaN.
3249f6b440aSlntue       // pow (aNaN, 0) is already taken care above.
3259f6b440aSlntue       return x;
3269f6b440aSlntue     }
3279f6b440aSlntue 
3289f6b440aSlntue     // Normalize denormal inputs.
3299f6b440aSlntue     if (x_a < FPBits::min_normal().uintval()) {
3309f6b440aSlntue       e_x -= 64.0;
3319f6b440aSlntue       x_mant = FPBits(x * 0x1.0p64).get_mantissa();
3329f6b440aSlntue     }
3339f6b440aSlntue 
3349f6b440aSlntue     // x is finite and negative, and y is a finite integer.
3359f6b440aSlntue     if (x_sign) {
3369f6b440aSlntue       if (is_integer(y)) {
3379f6b440aSlntue         x = -x;
3389f6b440aSlntue         if (is_odd_integer(y))
3399f6b440aSlntue           // sign = -1.0;
3409f6b440aSlntue           sign = 0x8000'0000'0000'0000;
3419f6b440aSlntue       } else {
3429f6b440aSlntue         // pow( negative, non-integer ) = NaN
3439f6b440aSlntue         fputil::set_errno_if_required(EDOM);
3449f6b440aSlntue         fputil::raise_except_if_required(FE_INVALID);
3459f6b440aSlntue         return FPBits::quiet_nan().get_val();
3469f6b440aSlntue       }
3479f6b440aSlntue     }
3489f6b440aSlntue   }
3499f6b440aSlntue 
3509f6b440aSlntue   ///////// END - Check exceptional cases //////////////////////////////////////
3519f6b440aSlntue 
3529f6b440aSlntue   // x^y = 2^( y * log2(x) )
3539f6b440aSlntue   //     = 2^( y * ( e_x + log2(m_x) ) )
3549f6b440aSlntue   // First we compute log2(x) = e_x + log2(m_x)
3559f6b440aSlntue 
3569f6b440aSlntue   // Extract exponent field of x.
3579f6b440aSlntue 
3589f6b440aSlntue   // Use the highest 7 fractional bits of m_x as the index for look up tables.
3599f6b440aSlntue   unsigned idx_x = static_cast<unsigned>(x_mant >> (FPBits::FRACTION_LEN - 7));
3609f6b440aSlntue   // Add the hidden bit to the mantissa.
3619f6b440aSlntue   // 1 <= m_x < 2
3629f6b440aSlntue   FPBits m_x = FPBits(x_mant | 0x3ff0'0000'0000'0000);
3639f6b440aSlntue 
3649f6b440aSlntue   // Reduced argument for log2(m_x):
3659f6b440aSlntue   //   dx = r * m_x - 1.
3669f6b440aSlntue   // The computation is exact, and -2^-8 <= dx < 2^-7.
3679f6b440aSlntue   // Then m_x = (1 + dx) / r, and
3689f6b440aSlntue   //   log2(m_x) = log2( (1 + dx) / r )
3699f6b440aSlntue   //             = log2(1 + dx) - log2(r).
370f133dd92Slntue 
371f133dd92Slntue   // In order for the overall computations x^y = 2^(y * log2(x)) to have the
372f133dd92Slntue   // relative errors < 2^-52 (1ULP), we will need to evaluate the exponent part
373f133dd92Slntue   // y * log2(x) with absolute errors < 2^-52 (or better, 2^-53).  Since the
374f133dd92Slntue   // whole exponent range for double precision is bounded by
375f133dd92Slntue   // |y * log2(x)| < 1076 ~ 2^10, we need to evaluate log2(x) with absolute
376f133dd92Slntue   // errors < 2^-53 * 2^-10 = 2^-63.
377f133dd92Slntue 
378f133dd92Slntue   // With that requirement, we use the following degree-6 polynomial
379f133dd92Slntue   // approximation:
380f133dd92Slntue   //   P(dx) ~ log2(1 + dx) / dx
381f133dd92Slntue   // Generated by Sollya with:
382f133dd92Slntue   // > P = fpminimax(log2(1 + x)/x, 6, [|D...|], [-2^-8, 2^-7]); P;
383f133dd92Slntue   // > dirtyinfnorm(log2(1 + x) - x*P, [-2^-8, 2^-7]);
384f133dd92Slntue   //   0x1.d03cc...p-66
385f133dd92Slntue   constexpr double COEFFS[] = {0x1.71547652b82fep0,  -0x1.71547652b82e7p-1,
386f133dd92Slntue                                0x1.ec709dc3b1fd5p-2, -0x1.7154766124215p-2,
387f133dd92Slntue                                0x1.2776bd90259d8p-2, -0x1.ec586c6f3d311p-3,
388f133dd92Slntue                                0x1.9c4775eccf524p-3};
389f133dd92Slntue   // Error: ulp(dx^2) <= (2^-7)^2 * 2^-52 = 2^-66
390f133dd92Slntue   // Extra errors from various computations and rounding directions, the overall
391f133dd92Slntue   // errors we can be bounded by 2^-65.
392f133dd92Slntue 
3939f6b440aSlntue   double dx;
394f133dd92Slntue   DoubleDouble dx_c0;
395f133dd92Slntue 
396f133dd92Slntue   // Perform exact range reduction and exact product dx * c0.
3979f6b440aSlntue #ifdef LIBC_TARGET_CPU_HAS_FMA
3989f6b440aSlntue   dx = fputil::multiply_add(RD[idx_x], m_x.get_val(), -1.0); // Exact
399f133dd92Slntue   dx_c0 = fputil::exact_mult(COEFFS[0], dx);
4009f6b440aSlntue #else
4019f6b440aSlntue   double c = FPBits(m_x.uintval() & 0x3fff'e000'0000'0000).get_val();
4029f6b440aSlntue   dx = fputil::multiply_add(RD[idx_x], m_x.get_val() - c, CD[idx_x]); // Exact
40351e9430aSlntue   dx_c0 = fputil::exact_mult<28>(dx, COEFFS[0]);                      // Exact
4049f6b440aSlntue #endif // LIBC_TARGET_CPU_HAS_FMA
4059f6b440aSlntue 
406f133dd92Slntue   double dx2 = dx * dx;
407f133dd92Slntue   double c0 = fputil::multiply_add(dx, COEFFS[2], COEFFS[1]);
408f133dd92Slntue   double c1 = fputil::multiply_add(dx, COEFFS[4], COEFFS[3]);
409f133dd92Slntue   double c2 = fputil::multiply_add(dx, COEFFS[6], COEFFS[5]);
4109f6b440aSlntue 
4119f6b440aSlntue   double p = fputil::polyeval(dx2, c0, c1, c2);
4129f6b440aSlntue 
4139f6b440aSlntue   // s = e_x - log2(r) + dx * P(dx)
4149f6b440aSlntue   // Absolute error bound:
415f133dd92Slntue   //   |log2(x) - log2_x.hi - log2_x.lo| < 2^-65.
4169f6b440aSlntue 
417f133dd92Slntue   // Notice that e_x - log2(r).hi is exact, so we perform an exact sum of
418f133dd92Slntue   // e_x - log2(r).hi and the high part of the product dx * c0:
419f133dd92Slntue   //   log2_x_hi.hi + log2_x_hi.lo = e_x - log2(r).hi + (dx * c0).hi
420f133dd92Slntue   DoubleDouble log2_x_hi =
421f133dd92Slntue       fputil::exact_add(e_x + LOG2_R_DD[idx_x].hi, dx_c0.hi);
422f133dd92Slntue   // The low part is dx^2 * p + low part of (dx * c0) + low part of -log2(r).
423f133dd92Slntue   double log2_x_lo =
424f133dd92Slntue       fputil::multiply_add(dx2, p, dx_c0.lo + LOG2_R_DD[idx_x].lo);
425f133dd92Slntue   // Perform accurate sums.
426f133dd92Slntue   DoubleDouble log2_x = fputil::exact_add(log2_x_hi.hi, log2_x_lo);
427f133dd92Slntue   log2_x.lo += log2_x_hi.lo;
4289f6b440aSlntue 
4299f6b440aSlntue   // To compute 2^(y * log2(x)), we break the exponent into 3 parts:
4309f6b440aSlntue   //   y * log(2) = hi + mid + lo, where
4319f6b440aSlntue   //   hi is an integer
4329f6b440aSlntue   //   mid * 2^6 is an integer
4339f6b440aSlntue   //   |lo| <= 2^-7
4349f6b440aSlntue   // Then:
4359f6b440aSlntue   //   x^y = 2^(y * log2(x)) = 2^hi * 2^mid * 2^lo,
4369f6b440aSlntue   // In which 2^mid is obtained from a look-up table of size 2^6 = 64 elements,
4379f6b440aSlntue   // and 2^lo ~ 1 + lo * P(lo).
4389f6b440aSlntue   // Thus, we have:
4399f6b440aSlntue   //   hi + mid = 2^-6 * round( 2^6 * y * log2(x) )
4409f6b440aSlntue   // If we restrict the output such that |hi| < 150, (hi + mid) uses (8 + 6)
4419f6b440aSlntue   // bits, hence, if we use double precision to perform
4429f6b440aSlntue   //   round( 2^6 * y * log2(x))
4439f6b440aSlntue   // the lo part is bounded by 2^-7 + 2^(-(52 - 14)) = 2^-7 + 2^-38
4449f6b440aSlntue 
4459f6b440aSlntue   // In the following computations:
4469f6b440aSlntue   //   y6  = 2^6 * y
4479f6b440aSlntue   //   hm  = 2^6 * (hi + mid) = round(2^6 * y * log2(x)) ~ round(y6 * s)
4489f6b440aSlntue   //   lo6 = 2^6 * lo = 2^6 * (y - (hi + mid)) = y6 * log2(x) - hm.
4499f6b440aSlntue   double y6 = y * 0x1.0p6; // Exact.
4509f6b440aSlntue 
4519f6b440aSlntue   DoubleDouble y6_log2_x = fputil::exact_mult(y6, log2_x.hi);
4529f6b440aSlntue   y6_log2_x.lo = fputil::multiply_add(y6, log2_x.lo, y6_log2_x.lo);
4539f6b440aSlntue 
4549f6b440aSlntue   // Check overflow/underflow.
4559f6b440aSlntue   double scale = 1.0;
4569f6b440aSlntue 
4579f6b440aSlntue   // |2^(hi + mid) - exp2_hi_mid| <= ulp(exp2_hi_mid) / 2
4589f6b440aSlntue   // Clamp the exponent part into smaller range that fits double precision.
4599f6b440aSlntue   // For those exponents that are out of range, the final conversion will round
4609f6b440aSlntue   // them correctly to inf/max float or 0/min float accordingly.
4619f6b440aSlntue   constexpr double UPPER_EXP_BOUND = 512.0 * 0x1.0p6;
4629f6b440aSlntue   if (LIBC_UNLIKELY(FPBits(y6_log2_x.hi).abs().get_val() >= UPPER_EXP_BOUND)) {
4639f6b440aSlntue     if (FPBits(y6_log2_x.hi).sign() == Sign::POS) {
4649f6b440aSlntue       scale = 0x1.0p512;
4659f6b440aSlntue       y6_log2_x.hi -= 512.0 * 64.0;
4669f6b440aSlntue       if (y6_log2_x.hi > 513.0 * 64.0)
4679f6b440aSlntue         y6_log2_x.hi = 513.0 * 64.0;
4689f6b440aSlntue     } else {
4699f6b440aSlntue       scale = 0x1.0p-512;
4709f6b440aSlntue       y6_log2_x.hi += 512.0 * 64.0;
4719f6b440aSlntue       if (y6_log2_x.hi < (-1076.0 + 512.0) * 64.0)
4729f6b440aSlntue         y6_log2_x.hi = -564.0 * 64.0;
4739f6b440aSlntue     }
4749f6b440aSlntue   }
4759f6b440aSlntue 
4769f6b440aSlntue   double hm = fputil::nearest_integer(y6_log2_x.hi);
4779f6b440aSlntue 
4789f6b440aSlntue   // lo6 = 2^6 * lo.
4799f6b440aSlntue   double lo6_hi = y6_log2_x.hi - hm;
4809f6b440aSlntue   double lo6 = lo6_hi + y6_log2_x.lo;
4819f6b440aSlntue 
4829f6b440aSlntue   int hm_i = static_cast<int>(hm);
4839f6b440aSlntue   unsigned idx_y = static_cast<unsigned>(hm_i) & 0x3f;
4849f6b440aSlntue 
4859f6b440aSlntue   // 2^hi
4869f6b440aSlntue   int64_t exp2_hi_i = static_cast<int64_t>(
4879f6b440aSlntue       static_cast<uint64_t>(static_cast<int64_t>(hm_i >> 6))
4889f6b440aSlntue       << FPBits::FRACTION_LEN);
4899f6b440aSlntue   // 2^mid
4909f6b440aSlntue   int64_t exp2_mid_hi_i =
4919f6b440aSlntue       static_cast<int64_t>(FPBits(EXP2_MID1[idx_y].hi).uintval());
4929f6b440aSlntue   int64_t exp2_mid_lo_i =
4939f6b440aSlntue       static_cast<int64_t>(FPBits(EXP2_MID1[idx_y].mid).uintval());
4949f6b440aSlntue   // (-1)^sign * 2^hi * 2^mid
4959f6b440aSlntue   // Error <= 2^hi * 2^-53
4969f6b440aSlntue   uint64_t exp2_hm_hi_i =
4979f6b440aSlntue       static_cast<uint64_t>(exp2_hi_i + exp2_mid_hi_i) + sign;
4989f6b440aSlntue   // The low part could be 0.
4999f6b440aSlntue   uint64_t exp2_hm_lo_i =
5009f6b440aSlntue       idx_y != 0 ? static_cast<uint64_t>(exp2_hi_i + exp2_mid_lo_i) + sign
5019f6b440aSlntue                  : sign;
5029f6b440aSlntue   double exp2_hm_hi = FPBits(exp2_hm_hi_i).get_val();
5039f6b440aSlntue   double exp2_hm_lo = FPBits(exp2_hm_lo_i).get_val();
5049f6b440aSlntue 
5059f6b440aSlntue   // Degree-5 polynomial approximation P(lo6) ~ 2^(lo6 / 2^6) = 2^(lo).
5069f6b440aSlntue   // Generated by Sollya with:
5079f6b440aSlntue   // > P = fpminimax(2^(x/64), 5, [|1, D...|], [-2^-1, 2^-1]);
5089f6b440aSlntue   // > dirtyinfnorm(2^(x/64) - P, [-0.5, 0.5]);
5099f6b440aSlntue   // 0x1.a2b77e618f5c4c176fd11b7659016cde5de83cb72p-60
5109f6b440aSlntue   constexpr double EXP2_COEFFS[] = {0x1p0,
5119f6b440aSlntue                                     0x1.62e42fefa39efp-7,
5129f6b440aSlntue                                     0x1.ebfbdff82a23ap-15,
5139f6b440aSlntue                                     0x1.c6b08d7076268p-23,
5149f6b440aSlntue                                     0x1.3b2ad33f8b48bp-31,
5159f6b440aSlntue                                     0x1.5d870c4d84445p-40};
5169f6b440aSlntue 
5179f6b440aSlntue   double lo6_sqr = lo6 * lo6;
5189f6b440aSlntue 
5199f6b440aSlntue   double d0 = fputil::multiply_add(lo6, EXP2_COEFFS[2], EXP2_COEFFS[1]);
5209f6b440aSlntue   double d1 = fputil::multiply_add(lo6, EXP2_COEFFS[4], EXP2_COEFFS[3]);
5219f6b440aSlntue   double pp = fputil::polyeval(lo6_sqr, d0, d1, EXP2_COEFFS[5]);
5229f6b440aSlntue 
5239f6b440aSlntue   double r = fputil::multiply_add(exp2_hm_hi * lo6, pp, exp2_hm_lo);
5249f6b440aSlntue   r += exp2_hm_hi;
5259f6b440aSlntue 
5269f6b440aSlntue   return r * scale;
5279f6b440aSlntue }
5289f6b440aSlntue 
5299f6b440aSlntue } // namespace LIBC_NAMESPACE_DECL
530