1570af302Sopenharmony_ci/* origin: FreeBSD /usr/src/lib/msun/ld80/s_exp2l.c and /usr/src/lib/msun/ld128/s_exp2l.c */ 2570af302Sopenharmony_ci/*- 3570af302Sopenharmony_ci * Copyright (c) 2005-2008 David Schultz <das@FreeBSD.ORG> 4570af302Sopenharmony_ci * All rights reserved. 5570af302Sopenharmony_ci * 6570af302Sopenharmony_ci * Redistribution and use in source and binary forms, with or without 7570af302Sopenharmony_ci * modification, are permitted provided that the following conditions 8570af302Sopenharmony_ci * are met: 9570af302Sopenharmony_ci * 1. Redistributions of source code must retain the above copyright 10570af302Sopenharmony_ci * notice, this list of conditions and the following disclaimer. 11570af302Sopenharmony_ci * 2. Redistributions in binary form must reproduce the above copyright 12570af302Sopenharmony_ci * notice, this list of conditions and the following disclaimer in the 13570af302Sopenharmony_ci * documentation and/or other materials provided with the distribution. 14570af302Sopenharmony_ci * 15570af302Sopenharmony_ci * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 16570af302Sopenharmony_ci * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17570af302Sopenharmony_ci * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18570af302Sopenharmony_ci * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 19570af302Sopenharmony_ci * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 20570af302Sopenharmony_ci * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 21570af302Sopenharmony_ci * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 22570af302Sopenharmony_ci * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 23570af302Sopenharmony_ci * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 24570af302Sopenharmony_ci * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 25570af302Sopenharmony_ci * SUCH DAMAGE. 26570af302Sopenharmony_ci */ 27570af302Sopenharmony_ci 28570af302Sopenharmony_ci#include "libm.h" 29570af302Sopenharmony_ci 30570af302Sopenharmony_ci#if LDBL_MANT_DIG == 53 && LDBL_MAX_EXP == 1024 31570af302Sopenharmony_cilong double exp2l(long double x) 32570af302Sopenharmony_ci{ 33570af302Sopenharmony_ci return exp2(x); 34570af302Sopenharmony_ci} 35570af302Sopenharmony_ci#elif LDBL_MANT_DIG == 64 && LDBL_MAX_EXP == 16384 36570af302Sopenharmony_ci#define TBLBITS 7 37570af302Sopenharmony_ci#define TBLSIZE (1 << TBLBITS) 38570af302Sopenharmony_ci 39570af302Sopenharmony_cistatic const double 40570af302Sopenharmony_ciredux = 0x1.8p63 / TBLSIZE, 41570af302Sopenharmony_ciP1 = 0x1.62e42fefa39efp-1, 42570af302Sopenharmony_ciP2 = 0x1.ebfbdff82c58fp-3, 43570af302Sopenharmony_ciP3 = 0x1.c6b08d7049fap-5, 44570af302Sopenharmony_ciP4 = 0x1.3b2ab6fba4da5p-7, 45570af302Sopenharmony_ciP5 = 0x1.5d8804780a736p-10, 46570af302Sopenharmony_ciP6 = 0x1.430918835e33dp-13; 47570af302Sopenharmony_ci 48570af302Sopenharmony_cistatic const double tbl[TBLSIZE * 2] = { 49570af302Sopenharmony_ci 0x1.6a09e667f3bcdp-1, -0x1.bdd3413b2648p-55, 50570af302Sopenharmony_ci 0x1.6c012750bdabfp-1, -0x1.2895667ff0cp-57, 51570af302Sopenharmony_ci 0x1.6dfb23c651a2fp-1, -0x1.bbe3a683c88p-58, 52570af302Sopenharmony_ci 0x1.6ff7df9519484p-1, -0x1.83c0f25860fp-56, 53570af302Sopenharmony_ci 0x1.71f75e8ec5f74p-1, -0x1.16e4786887bp-56, 54570af302Sopenharmony_ci 0x1.73f9a48a58174p-1, -0x1.0a8d96c65d5p-55, 55570af302Sopenharmony_ci 0x1.75feb564267c9p-1, -0x1.0245957316ep-55, 56570af302Sopenharmony_ci 0x1.780694fde5d3fp-1, 0x1.866b80a0216p-55, 57570af302Sopenharmony_ci 0x1.7a11473eb0187p-1, -0x1.41577ee0499p-56, 58570af302Sopenharmony_ci 0x1.7c1ed0130c132p-1, 0x1.f124cd1164ep-55, 59570af302Sopenharmony_ci 0x1.7e2f336cf4e62p-1, 0x1.05d02ba157ap-57, 60570af302Sopenharmony_ci 0x1.80427543e1a12p-1, -0x1.27c86626d97p-55, 61570af302Sopenharmony_ci 0x1.82589994cce13p-1, -0x1.d4c1dd41533p-55, 62570af302Sopenharmony_ci 0x1.8471a4623c7adp-1, -0x1.8d684a341cep-56, 63570af302Sopenharmony_ci 0x1.868d99b4492edp-1, -0x1.fc6f89bd4f68p-55, 64570af302Sopenharmony_ci 0x1.88ac7d98a6699p-1, 0x1.994c2f37cb5p-55, 65570af302Sopenharmony_ci 0x1.8ace5422aa0dbp-1, 0x1.6e9f156864bp-55, 66570af302Sopenharmony_ci 0x1.8cf3216b5448cp-1, -0x1.0d55e32e9e4p-57, 67570af302Sopenharmony_ci 0x1.8f1ae99157736p-1, 0x1.5cc13a2e397p-56, 68570af302Sopenharmony_ci 0x1.9145b0b91ffc6p-1, -0x1.dd6792e5825p-55, 69570af302Sopenharmony_ci 0x1.93737b0cdc5e5p-1, -0x1.75fc781b58p-58, 70570af302Sopenharmony_ci 0x1.95a44cbc8520fp-1, -0x1.64b7c96a5fp-57, 71570af302Sopenharmony_ci 0x1.97d829fde4e5p-1, -0x1.d185b7c1b86p-55, 72570af302Sopenharmony_ci 0x1.9a0f170ca07bap-1, -0x1.173bd91cee6p-55, 73570af302Sopenharmony_ci 0x1.9c49182a3f09p-1, 0x1.c7c46b071f2p-57, 74570af302Sopenharmony_ci 0x1.9e86319e32323p-1, 0x1.824ca78e64cp-57, 75570af302Sopenharmony_ci 0x1.a0c667b5de565p-1, -0x1.359495d1cd5p-55, 76570af302Sopenharmony_ci 0x1.a309bec4a2d33p-1, 0x1.6305c7ddc368p-55, 77570af302Sopenharmony_ci 0x1.a5503b23e255dp-1, -0x1.d2f6edb8d42p-55, 78570af302Sopenharmony_ci 0x1.a799e1330b358p-1, 0x1.bcb7ecac564p-55, 79570af302Sopenharmony_ci 0x1.a9e6b5579fdbfp-1, 0x1.0fac90ef7fdp-55, 80570af302Sopenharmony_ci 0x1.ac36bbfd3f37ap-1, -0x1.f9234cae76dp-56, 81570af302Sopenharmony_ci 0x1.ae89f995ad3adp-1, 0x1.7a1cd345dcc8p-55, 82570af302Sopenharmony_ci 0x1.b0e07298db666p-1, -0x1.bdef54c80e4p-55, 83570af302Sopenharmony_ci 0x1.b33a2b84f15fbp-1, -0x1.2805e3084d8p-58, 84570af302Sopenharmony_ci 0x1.b59728de5593ap-1, -0x1.c71dfbbba6ep-55, 85570af302Sopenharmony_ci 0x1.b7f76f2fb5e47p-1, -0x1.5584f7e54acp-57, 86570af302Sopenharmony_ci 0x1.ba5b030a1064ap-1, -0x1.efcd30e5429p-55, 87570af302Sopenharmony_ci 0x1.bcc1e904bc1d2p-1, 0x1.23dd07a2d9fp-56, 88570af302Sopenharmony_ci 0x1.bf2c25bd71e09p-1, -0x1.efdca3f6b9c8p-55, 89570af302Sopenharmony_ci 0x1.c199bdd85529cp-1, 0x1.11065895049p-56, 90570af302Sopenharmony_ci 0x1.c40ab5fffd07ap-1, 0x1.b4537e083c6p-55, 91570af302Sopenharmony_ci 0x1.c67f12e57d14bp-1, 0x1.2884dff483c8p-55, 92570af302Sopenharmony_ci 0x1.c8f6d9406e7b5p-1, 0x1.1acbc48805cp-57, 93570af302Sopenharmony_ci 0x1.cb720dcef9069p-1, 0x1.503cbd1e94ap-57, 94570af302Sopenharmony_ci 0x1.cdf0b555dc3fap-1, -0x1.dd83b53829dp-56, 95570af302Sopenharmony_ci 0x1.d072d4a07897cp-1, -0x1.cbc3743797a8p-55, 96570af302Sopenharmony_ci 0x1.d2f87080d89f2p-1, -0x1.d487b719d858p-55, 97570af302Sopenharmony_ci 0x1.d5818dcfba487p-1, 0x1.2ed02d75b37p-56, 98570af302Sopenharmony_ci 0x1.d80e316c98398p-1, -0x1.11ec18bedep-55, 99570af302Sopenharmony_ci 0x1.da9e603db3285p-1, 0x1.c2300696db5p-55, 100570af302Sopenharmony_ci 0x1.dd321f301b46p-1, 0x1.2da5778f019p-55, 101570af302Sopenharmony_ci 0x1.dfc97337b9b5fp-1, -0x1.1a5cd4f184b8p-55, 102570af302Sopenharmony_ci 0x1.e264614f5a129p-1, -0x1.7b627817a148p-55, 103570af302Sopenharmony_ci 0x1.e502ee78b3ff6p-1, 0x1.39e8980a9cdp-56, 104570af302Sopenharmony_ci 0x1.e7a51fbc74c83p-1, 0x1.2d522ca0c8ep-55, 105570af302Sopenharmony_ci 0x1.ea4afa2a490dap-1, -0x1.e9c23179c288p-55, 106570af302Sopenharmony_ci 0x1.ecf482d8e67f1p-1, -0x1.c93f3b411ad8p-55, 107570af302Sopenharmony_ci 0x1.efa1bee615a27p-1, 0x1.dc7f486a4b68p-55, 108570af302Sopenharmony_ci 0x1.f252b376bba97p-1, 0x1.3a1a5bf0d8e8p-55, 109570af302Sopenharmony_ci 0x1.f50765b6e454p-1, 0x1.9d3e12dd8a18p-55, 110570af302Sopenharmony_ci 0x1.f7bfdad9cbe14p-1, -0x1.dbb12d00635p-55, 111570af302Sopenharmony_ci 0x1.fa7c1819e90d8p-1, 0x1.74853f3a593p-56, 112570af302Sopenharmony_ci 0x1.fd3c22b8f71f1p-1, 0x1.2eb74966578p-58, 113570af302Sopenharmony_ci 0x1p+0, 0x0p+0, 114570af302Sopenharmony_ci 0x1.0163da9fb3335p+0, 0x1.b61299ab8cd8p-54, 115570af302Sopenharmony_ci 0x1.02c9a3e778061p+0, -0x1.19083535b08p-56, 116570af302Sopenharmony_ci 0x1.04315e86e7f85p+0, -0x1.0a31c1977c98p-54, 117570af302Sopenharmony_ci 0x1.059b0d3158574p+0, 0x1.d73e2a475b4p-55, 118570af302Sopenharmony_ci 0x1.0706b29ddf6dep+0, -0x1.c91dfe2b13cp-55, 119570af302Sopenharmony_ci 0x1.0874518759bc8p+0, 0x1.186be4bb284p-57, 120570af302Sopenharmony_ci 0x1.09e3ecac6f383p+0, 0x1.14878183161p-54, 121570af302Sopenharmony_ci 0x1.0b5586cf9890fp+0, 0x1.8a62e4adc61p-54, 122570af302Sopenharmony_ci 0x1.0cc922b7247f7p+0, 0x1.01edc16e24f8p-54, 123570af302Sopenharmony_ci 0x1.0e3ec32d3d1a2p+0, 0x1.03a1727c58p-59, 124570af302Sopenharmony_ci 0x1.0fb66affed31bp+0, -0x1.b9bedc44ebcp-57, 125570af302Sopenharmony_ci 0x1.11301d0125b51p+0, -0x1.6c51039449bp-54, 126570af302Sopenharmony_ci 0x1.12abdc06c31ccp+0, -0x1.1b514b36ca8p-58, 127570af302Sopenharmony_ci 0x1.1429aaea92dep+0, -0x1.32fbf9af1368p-54, 128570af302Sopenharmony_ci 0x1.15a98c8a58e51p+0, 0x1.2406ab9eeabp-55, 129570af302Sopenharmony_ci 0x1.172b83c7d517bp+0, -0x1.19041b9d78ap-55, 130570af302Sopenharmony_ci 0x1.18af9388c8deap+0, -0x1.11023d1970f8p-54, 131570af302Sopenharmony_ci 0x1.1a35beb6fcb75p+0, 0x1.e5b4c7b4969p-55, 132570af302Sopenharmony_ci 0x1.1bbe084045cd4p+0, -0x1.95386352ef6p-54, 133570af302Sopenharmony_ci 0x1.1d4873168b9aap+0, 0x1.e016e00a264p-54, 134570af302Sopenharmony_ci 0x1.1ed5022fcd91dp+0, -0x1.1df98027bb78p-54, 135570af302Sopenharmony_ci 0x1.2063b88628cd6p+0, 0x1.dc775814a85p-55, 136570af302Sopenharmony_ci 0x1.21f49917ddc96p+0, 0x1.2a97e9494a6p-55, 137570af302Sopenharmony_ci 0x1.2387a6e756238p+0, 0x1.9b07eb6c7058p-54, 138570af302Sopenharmony_ci 0x1.251ce4fb2a63fp+0, 0x1.ac155bef4f5p-55, 139570af302Sopenharmony_ci 0x1.26b4565e27cddp+0, 0x1.2bd339940eap-55, 140570af302Sopenharmony_ci 0x1.284dfe1f56381p+0, -0x1.a4c3a8c3f0d8p-54, 141570af302Sopenharmony_ci 0x1.29e9df51fdee1p+0, 0x1.612e8afad12p-55, 142570af302Sopenharmony_ci 0x1.2b87fd0dad99p+0, -0x1.10adcd6382p-59, 143570af302Sopenharmony_ci 0x1.2d285a6e4030bp+0, 0x1.0024754db42p-54, 144570af302Sopenharmony_ci 0x1.2ecafa93e2f56p+0, 0x1.1ca0f45d524p-56, 145570af302Sopenharmony_ci 0x1.306fe0a31b715p+0, 0x1.6f46ad23183p-55, 146570af302Sopenharmony_ci 0x1.32170fc4cd831p+0, 0x1.a9ce78e1804p-55, 147570af302Sopenharmony_ci 0x1.33c08b26416ffp+0, 0x1.327218436598p-54, 148570af302Sopenharmony_ci 0x1.356c55f929ff1p+0, -0x1.b5cee5c4e46p-55, 149570af302Sopenharmony_ci 0x1.371a7373aa9cbp+0, -0x1.63aeabf42ebp-54, 150570af302Sopenharmony_ci 0x1.38cae6d05d866p+0, -0x1.e958d3c99048p-54, 151570af302Sopenharmony_ci 0x1.3a7db34e59ff7p+0, -0x1.5e436d661f6p-56, 152570af302Sopenharmony_ci 0x1.3c32dc313a8e5p+0, -0x1.efff8375d2ap-54, 153570af302Sopenharmony_ci 0x1.3dea64c123422p+0, 0x1.ada0911f09fp-55, 154570af302Sopenharmony_ci 0x1.3fa4504ac801cp+0, -0x1.7d023f956fap-54, 155570af302Sopenharmony_ci 0x1.4160a21f72e2ap+0, -0x1.ef3691c309p-58, 156570af302Sopenharmony_ci 0x1.431f5d950a897p+0, -0x1.1c7dde35f7ap-55, 157570af302Sopenharmony_ci 0x1.44e086061892dp+0, 0x1.89b7a04ef8p-59, 158570af302Sopenharmony_ci 0x1.46a41ed1d0057p+0, 0x1.c944bd1648a8p-54, 159570af302Sopenharmony_ci 0x1.486a2b5c13cdp+0, 0x1.3c1a3b69062p-56, 160570af302Sopenharmony_ci 0x1.4a32af0d7d3dep+0, 0x1.9cb62f3d1be8p-54, 161570af302Sopenharmony_ci 0x1.4bfdad5362a27p+0, 0x1.d4397afec42p-56, 162570af302Sopenharmony_ci 0x1.4dcb299fddd0dp+0, 0x1.8ecdbbc6a78p-54, 163570af302Sopenharmony_ci 0x1.4f9b2769d2ca7p+0, -0x1.4b309d25958p-54, 164570af302Sopenharmony_ci 0x1.516daa2cf6642p+0, -0x1.f768569bd94p-55, 165570af302Sopenharmony_ci 0x1.5342b569d4f82p+0, -0x1.07abe1db13dp-55, 166570af302Sopenharmony_ci 0x1.551a4ca5d920fp+0, -0x1.d689cefede6p-55, 167570af302Sopenharmony_ci 0x1.56f4736b527dap+0, 0x1.9bb2c011d938p-54, 168570af302Sopenharmony_ci 0x1.58d12d497c7fdp+0, 0x1.295e15b9a1ep-55, 169570af302Sopenharmony_ci 0x1.5ab07dd485429p+0, 0x1.6324c0546478p-54, 170570af302Sopenharmony_ci 0x1.5c9268a5946b7p+0, 0x1.c4b1b81698p-60, 171570af302Sopenharmony_ci 0x1.5e76f15ad2148p+0, 0x1.ba6f93080e68p-54, 172570af302Sopenharmony_ci 0x1.605e1b976dc09p+0, -0x1.3e2429b56de8p-54, 173570af302Sopenharmony_ci 0x1.6247eb03a5585p+0, -0x1.383c17e40b48p-54, 174570af302Sopenharmony_ci 0x1.6434634ccc32p+0, -0x1.c483c759d89p-55, 175570af302Sopenharmony_ci 0x1.6623882552225p+0, -0x1.bb60987591cp-54, 176570af302Sopenharmony_ci 0x1.68155d44ca973p+0, 0x1.038ae44f74p-57, 177570af302Sopenharmony_ci}; 178570af302Sopenharmony_ci 179570af302Sopenharmony_ci/* 180570af302Sopenharmony_ci * exp2l(x): compute the base 2 exponential of x 181570af302Sopenharmony_ci * 182570af302Sopenharmony_ci * Accuracy: Peak error < 0.511 ulp. 183570af302Sopenharmony_ci * 184570af302Sopenharmony_ci * Method: (equally-spaced tables) 185570af302Sopenharmony_ci * 186570af302Sopenharmony_ci * Reduce x: 187570af302Sopenharmony_ci * x = 2**k + y, for integer k and |y| <= 1/2. 188570af302Sopenharmony_ci * Thus we have exp2l(x) = 2**k * exp2(y). 189570af302Sopenharmony_ci * 190570af302Sopenharmony_ci * Reduce y: 191570af302Sopenharmony_ci * y = i/TBLSIZE + z for integer i near y * TBLSIZE. 192570af302Sopenharmony_ci * Thus we have exp2(y) = exp2(i/TBLSIZE) * exp2(z), 193570af302Sopenharmony_ci * with |z| <= 2**-(TBLBITS+1). 194570af302Sopenharmony_ci * 195570af302Sopenharmony_ci * We compute exp2(i/TBLSIZE) via table lookup and exp2(z) via a 196570af302Sopenharmony_ci * degree-6 minimax polynomial with maximum error under 2**-69. 197570af302Sopenharmony_ci * The table entries each have 104 bits of accuracy, encoded as 198570af302Sopenharmony_ci * a pair of double precision values. 199570af302Sopenharmony_ci */ 200570af302Sopenharmony_cilong double exp2l(long double x) 201570af302Sopenharmony_ci{ 202570af302Sopenharmony_ci union ldshape u = {x}; 203570af302Sopenharmony_ci int e = u.i.se & 0x7fff; 204570af302Sopenharmony_ci long double r, z; 205570af302Sopenharmony_ci uint32_t i0; 206570af302Sopenharmony_ci union {uint32_t u; int32_t i;} k; 207570af302Sopenharmony_ci 208570af302Sopenharmony_ci /* Filter out exceptional cases. */ 209570af302Sopenharmony_ci if (e >= 0x3fff + 13) { /* |x| >= 8192 or x is NaN */ 210570af302Sopenharmony_ci if (u.i.se >= 0x3fff + 14 && u.i.se >> 15 == 0) 211570af302Sopenharmony_ci /* overflow */ 212570af302Sopenharmony_ci return x * 0x1p16383L; 213570af302Sopenharmony_ci if (e == 0x7fff) /* -inf or -nan */ 214570af302Sopenharmony_ci return -1/x; 215570af302Sopenharmony_ci if (x < -16382) { 216570af302Sopenharmony_ci if (x <= -16446 || x - 0x1p63 + 0x1p63 != x) 217570af302Sopenharmony_ci /* underflow */ 218570af302Sopenharmony_ci FORCE_EVAL((float)(-0x1p-149/x)); 219570af302Sopenharmony_ci if (x <= -16446) 220570af302Sopenharmony_ci return 0; 221570af302Sopenharmony_ci } 222570af302Sopenharmony_ci } else if (e < 0x3fff - 64) { 223570af302Sopenharmony_ci return 1 + x; 224570af302Sopenharmony_ci } 225570af302Sopenharmony_ci 226570af302Sopenharmony_ci /* 227570af302Sopenharmony_ci * Reduce x, computing z, i0, and k. The low bits of x + redux 228570af302Sopenharmony_ci * contain the 16-bit integer part of the exponent (k) followed by 229570af302Sopenharmony_ci * TBLBITS fractional bits (i0). We use bit tricks to extract these 230570af302Sopenharmony_ci * as integers, then set z to the remainder. 231570af302Sopenharmony_ci * 232570af302Sopenharmony_ci * Example: Suppose x is 0xabc.123456p0 and TBLBITS is 8. 233570af302Sopenharmony_ci * Then the low-order word of x + redux is 0x000abc12, 234570af302Sopenharmony_ci * We split this into k = 0xabc and i0 = 0x12 (adjusted to 235570af302Sopenharmony_ci * index into the table), then we compute z = 0x0.003456p0. 236570af302Sopenharmony_ci */ 237570af302Sopenharmony_ci u.f = x + redux; 238570af302Sopenharmony_ci i0 = u.i.m + TBLSIZE / 2; 239570af302Sopenharmony_ci k.u = i0 / TBLSIZE * TBLSIZE; 240570af302Sopenharmony_ci k.i /= TBLSIZE; 241570af302Sopenharmony_ci i0 %= TBLSIZE; 242570af302Sopenharmony_ci u.f -= redux; 243570af302Sopenharmony_ci z = x - u.f; 244570af302Sopenharmony_ci 245570af302Sopenharmony_ci /* Compute r = exp2l(y) = exp2lt[i0] * p(z). */ 246570af302Sopenharmony_ci long double t_hi = tbl[2*i0]; 247570af302Sopenharmony_ci long double t_lo = tbl[2*i0 + 1]; 248570af302Sopenharmony_ci /* XXX This gives > 1 ulp errors outside of FE_TONEAREST mode */ 249570af302Sopenharmony_ci r = t_lo + (t_hi + t_lo) * z * (P1 + z * (P2 + z * (P3 + z * (P4 250570af302Sopenharmony_ci + z * (P5 + z * P6))))) + t_hi; 251570af302Sopenharmony_ci 252570af302Sopenharmony_ci return scalbnl(r, k.i); 253570af302Sopenharmony_ci} 254570af302Sopenharmony_ci#elif LDBL_MANT_DIG == 113 && LDBL_MAX_EXP == 16384 255570af302Sopenharmony_ci#define TBLBITS 7 256570af302Sopenharmony_ci#define TBLSIZE (1 << TBLBITS) 257570af302Sopenharmony_ci 258570af302Sopenharmony_cistatic const long double 259570af302Sopenharmony_ci P1 = 0x1.62e42fefa39ef35793c7673007e6p-1L, 260570af302Sopenharmony_ci P2 = 0x1.ebfbdff82c58ea86f16b06ec9736p-3L, 261570af302Sopenharmony_ci P3 = 0x1.c6b08d704a0bf8b33a762bad3459p-5L, 262570af302Sopenharmony_ci P4 = 0x1.3b2ab6fba4e7729ccbbe0b4f3fc2p-7L, 263570af302Sopenharmony_ci P5 = 0x1.5d87fe78a67311071dee13fd11d9p-10L, 264570af302Sopenharmony_ci P6 = 0x1.430912f86c7876f4b663b23c5fe5p-13L; 265570af302Sopenharmony_ci 266570af302Sopenharmony_cistatic const double 267570af302Sopenharmony_ci P7 = 0x1.ffcbfc588b041p-17, 268570af302Sopenharmony_ci P8 = 0x1.62c0223a5c7c7p-20, 269570af302Sopenharmony_ci P9 = 0x1.b52541ff59713p-24, 270570af302Sopenharmony_ci P10 = 0x1.e4cf56a391e22p-28, 271570af302Sopenharmony_ci redux = 0x1.8p112 / TBLSIZE; 272570af302Sopenharmony_ci 273570af302Sopenharmony_cistatic const long double tbl[TBLSIZE] = { 274570af302Sopenharmony_ci 0x1.6a09e667f3bcc908b2fb1366dfeap-1L, 275570af302Sopenharmony_ci 0x1.6c012750bdabeed76a99800f4edep-1L, 276570af302Sopenharmony_ci 0x1.6dfb23c651a2ef220e2cbe1bc0d4p-1L, 277570af302Sopenharmony_ci 0x1.6ff7df9519483cf87e1b4f3e1e98p-1L, 278570af302Sopenharmony_ci 0x1.71f75e8ec5f73dd2370f2ef0b148p-1L, 279570af302Sopenharmony_ci 0x1.73f9a48a58173bd5c9a4e68ab074p-1L, 280570af302Sopenharmony_ci 0x1.75feb564267c8bf6e9aa33a489a8p-1L, 281570af302Sopenharmony_ci 0x1.780694fde5d3f619ae02808592a4p-1L, 282570af302Sopenharmony_ci 0x1.7a11473eb0186d7d51023f6ccb1ap-1L, 283570af302Sopenharmony_ci 0x1.7c1ed0130c1327c49334459378dep-1L, 284570af302Sopenharmony_ci 0x1.7e2f336cf4e62105d02ba1579756p-1L, 285570af302Sopenharmony_ci 0x1.80427543e1a11b60de67649a3842p-1L, 286570af302Sopenharmony_ci 0x1.82589994cce128acf88afab34928p-1L, 287570af302Sopenharmony_ci 0x1.8471a4623c7acce52f6b97c6444cp-1L, 288570af302Sopenharmony_ci 0x1.868d99b4492ec80e41d90ac2556ap-1L, 289570af302Sopenharmony_ci 0x1.88ac7d98a669966530bcdf2d4cc0p-1L, 290570af302Sopenharmony_ci 0x1.8ace5422aa0db5ba7c55a192c648p-1L, 291570af302Sopenharmony_ci 0x1.8cf3216b5448bef2aa1cd161c57ap-1L, 292570af302Sopenharmony_ci 0x1.8f1ae991577362b982745c72eddap-1L, 293570af302Sopenharmony_ci 0x1.9145b0b91ffc588a61b469f6b6a0p-1L, 294570af302Sopenharmony_ci 0x1.93737b0cdc5e4f4501c3f2540ae8p-1L, 295570af302Sopenharmony_ci 0x1.95a44cbc8520ee9b483695a0e7fep-1L, 296570af302Sopenharmony_ci 0x1.97d829fde4e4f8b9e920f91e8eb6p-1L, 297570af302Sopenharmony_ci 0x1.9a0f170ca07b9ba3109b8c467844p-1L, 298570af302Sopenharmony_ci 0x1.9c49182a3f0901c7c46b071f28dep-1L, 299570af302Sopenharmony_ci 0x1.9e86319e323231824ca78e64c462p-1L, 300570af302Sopenharmony_ci 0x1.a0c667b5de564b29ada8b8cabbacp-1L, 301570af302Sopenharmony_ci 0x1.a309bec4a2d3358c171f770db1f4p-1L, 302570af302Sopenharmony_ci 0x1.a5503b23e255c8b424491caf88ccp-1L, 303570af302Sopenharmony_ci 0x1.a799e1330b3586f2dfb2b158f31ep-1L, 304570af302Sopenharmony_ci 0x1.a9e6b5579fdbf43eb243bdff53a2p-1L, 305570af302Sopenharmony_ci 0x1.ac36bbfd3f379c0db966a3126988p-1L, 306570af302Sopenharmony_ci 0x1.ae89f995ad3ad5e8734d17731c80p-1L, 307570af302Sopenharmony_ci 0x1.b0e07298db66590842acdfc6fb4ep-1L, 308570af302Sopenharmony_ci 0x1.b33a2b84f15faf6bfd0e7bd941b0p-1L, 309570af302Sopenharmony_ci 0x1.b59728de559398e3881111648738p-1L, 310570af302Sopenharmony_ci 0x1.b7f76f2fb5e46eaa7b081ab53ff6p-1L, 311570af302Sopenharmony_ci 0x1.ba5b030a10649840cb3c6af5b74cp-1L, 312570af302Sopenharmony_ci 0x1.bcc1e904bc1d2247ba0f45b3d06cp-1L, 313570af302Sopenharmony_ci 0x1.bf2c25bd71e088408d7025190cd0p-1L, 314570af302Sopenharmony_ci 0x1.c199bdd85529c2220cb12a0916bap-1L, 315570af302Sopenharmony_ci 0x1.c40ab5fffd07a6d14df820f17deap-1L, 316570af302Sopenharmony_ci 0x1.c67f12e57d14b4a2137fd20f2a26p-1L, 317570af302Sopenharmony_ci 0x1.c8f6d9406e7b511acbc48805c3f6p-1L, 318570af302Sopenharmony_ci 0x1.cb720dcef90691503cbd1e949d0ap-1L, 319570af302Sopenharmony_ci 0x1.cdf0b555dc3f9c44f8958fac4f12p-1L, 320570af302Sopenharmony_ci 0x1.d072d4a07897b8d0f22f21a13792p-1L, 321570af302Sopenharmony_ci 0x1.d2f87080d89f18ade123989ea50ep-1L, 322570af302Sopenharmony_ci 0x1.d5818dcfba48725da05aeb66dff8p-1L, 323570af302Sopenharmony_ci 0x1.d80e316c98397bb84f9d048807a0p-1L, 324570af302Sopenharmony_ci 0x1.da9e603db3285708c01a5b6d480cp-1L, 325570af302Sopenharmony_ci 0x1.dd321f301b4604b695de3c0630c0p-1L, 326570af302Sopenharmony_ci 0x1.dfc97337b9b5eb968cac39ed284cp-1L, 327570af302Sopenharmony_ci 0x1.e264614f5a128a12761fa17adc74p-1L, 328570af302Sopenharmony_ci 0x1.e502ee78b3ff6273d130153992d0p-1L, 329570af302Sopenharmony_ci 0x1.e7a51fbc74c834b548b2832378a4p-1L, 330570af302Sopenharmony_ci 0x1.ea4afa2a490d9858f73a18f5dab4p-1L, 331570af302Sopenharmony_ci 0x1.ecf482d8e67f08db0312fb949d50p-1L, 332570af302Sopenharmony_ci 0x1.efa1bee615a27771fd21a92dabb6p-1L, 333570af302Sopenharmony_ci 0x1.f252b376bba974e8696fc3638f24p-1L, 334570af302Sopenharmony_ci 0x1.f50765b6e4540674f84b762861a6p-1L, 335570af302Sopenharmony_ci 0x1.f7bfdad9cbe138913b4bfe72bd78p-1L, 336570af302Sopenharmony_ci 0x1.fa7c1819e90d82e90a7e74b26360p-1L, 337570af302Sopenharmony_ci 0x1.fd3c22b8f71f10975ba4b32bd006p-1L, 338570af302Sopenharmony_ci 0x1.0000000000000000000000000000p+0L, 339570af302Sopenharmony_ci 0x1.0163da9fb33356d84a66ae336e98p+0L, 340570af302Sopenharmony_ci 0x1.02c9a3e778060ee6f7caca4f7a18p+0L, 341570af302Sopenharmony_ci 0x1.04315e86e7f84bd738f9a20da442p+0L, 342570af302Sopenharmony_ci 0x1.059b0d31585743ae7c548eb68c6ap+0L, 343570af302Sopenharmony_ci 0x1.0706b29ddf6ddc6dc403a9d87b1ep+0L, 344570af302Sopenharmony_ci 0x1.0874518759bc808c35f25d942856p+0L, 345570af302Sopenharmony_ci 0x1.09e3ecac6f3834521e060c584d5cp+0L, 346570af302Sopenharmony_ci 0x1.0b5586cf9890f6298b92b7184200p+0L, 347570af302Sopenharmony_ci 0x1.0cc922b7247f7407b705b893dbdep+0L, 348570af302Sopenharmony_ci 0x1.0e3ec32d3d1a2020742e4f8af794p+0L, 349570af302Sopenharmony_ci 0x1.0fb66affed31af232091dd8a169ep+0L, 350570af302Sopenharmony_ci 0x1.11301d0125b50a4ebbf1aed9321cp+0L, 351570af302Sopenharmony_ci 0x1.12abdc06c31cbfb92bad324d6f84p+0L, 352570af302Sopenharmony_ci 0x1.1429aaea92ddfb34101943b2588ep+0L, 353570af302Sopenharmony_ci 0x1.15a98c8a58e512480d573dd562aep+0L, 354570af302Sopenharmony_ci 0x1.172b83c7d517adcdf7c8c50eb162p+0L, 355570af302Sopenharmony_ci 0x1.18af9388c8de9bbbf70b9a3c269cp+0L, 356570af302Sopenharmony_ci 0x1.1a35beb6fcb753cb698f692d2038p+0L, 357570af302Sopenharmony_ci 0x1.1bbe084045cd39ab1e72b442810ep+0L, 358570af302Sopenharmony_ci 0x1.1d4873168b9aa7805b8028990be8p+0L, 359570af302Sopenharmony_ci 0x1.1ed5022fcd91cb8819ff61121fbep+0L, 360570af302Sopenharmony_ci 0x1.2063b88628cd63b8eeb0295093f6p+0L, 361570af302Sopenharmony_ci 0x1.21f49917ddc962552fd29294bc20p+0L, 362570af302Sopenharmony_ci 0x1.2387a6e75623866c1fadb1c159c0p+0L, 363570af302Sopenharmony_ci 0x1.251ce4fb2a63f3582ab7de9e9562p+0L, 364570af302Sopenharmony_ci 0x1.26b4565e27cdd257a673281d3068p+0L, 365570af302Sopenharmony_ci 0x1.284dfe1f5638096cf15cf03c9fa0p+0L, 366570af302Sopenharmony_ci 0x1.29e9df51fdee12c25d15f5a25022p+0L, 367570af302Sopenharmony_ci 0x1.2b87fd0dad98ffddea46538fca24p+0L, 368570af302Sopenharmony_ci 0x1.2d285a6e4030b40091d536d0733ep+0L, 369570af302Sopenharmony_ci 0x1.2ecafa93e2f5611ca0f45d5239a4p+0L, 370570af302Sopenharmony_ci 0x1.306fe0a31b7152de8d5a463063bep+0L, 371570af302Sopenharmony_ci 0x1.32170fc4cd8313539cf1c3009330p+0L, 372570af302Sopenharmony_ci 0x1.33c08b26416ff4c9c8610d96680ep+0L, 373570af302Sopenharmony_ci 0x1.356c55f929ff0c94623476373be4p+0L, 374570af302Sopenharmony_ci 0x1.371a7373aa9caa7145502f45452ap+0L, 375570af302Sopenharmony_ci 0x1.38cae6d05d86585a9cb0d9bed530p+0L, 376570af302Sopenharmony_ci 0x1.3a7db34e59ff6ea1bc9299e0a1fep+0L, 377570af302Sopenharmony_ci 0x1.3c32dc313a8e484001f228b58cf0p+0L, 378570af302Sopenharmony_ci 0x1.3dea64c12342235b41223e13d7eep+0L, 379570af302Sopenharmony_ci 0x1.3fa4504ac801ba0bf701aa417b9cp+0L, 380570af302Sopenharmony_ci 0x1.4160a21f72e29f84325b8f3dbacap+0L, 381570af302Sopenharmony_ci 0x1.431f5d950a896dc704439410b628p+0L, 382570af302Sopenharmony_ci 0x1.44e086061892d03136f409df0724p+0L, 383570af302Sopenharmony_ci 0x1.46a41ed1d005772512f459229f0ap+0L, 384570af302Sopenharmony_ci 0x1.486a2b5c13cd013c1a3b69062f26p+0L, 385570af302Sopenharmony_ci 0x1.4a32af0d7d3de672d8bcf46f99b4p+0L, 386570af302Sopenharmony_ci 0x1.4bfdad5362a271d4397afec42e36p+0L, 387570af302Sopenharmony_ci 0x1.4dcb299fddd0d63b36ef1a9e19dep+0L, 388570af302Sopenharmony_ci 0x1.4f9b2769d2ca6ad33d8b69aa0b8cp+0L, 389570af302Sopenharmony_ci 0x1.516daa2cf6641c112f52c84d6066p+0L, 390570af302Sopenharmony_ci 0x1.5342b569d4f81df0a83c49d86bf4p+0L, 391570af302Sopenharmony_ci 0x1.551a4ca5d920ec52ec620243540cp+0L, 392570af302Sopenharmony_ci 0x1.56f4736b527da66ecb004764e61ep+0L, 393570af302Sopenharmony_ci 0x1.58d12d497c7fd252bc2b7343d554p+0L, 394570af302Sopenharmony_ci 0x1.5ab07dd48542958c93015191e9a8p+0L, 395570af302Sopenharmony_ci 0x1.5c9268a5946b701c4b1b81697ed4p+0L, 396570af302Sopenharmony_ci 0x1.5e76f15ad21486e9be4c20399d12p+0L, 397570af302Sopenharmony_ci 0x1.605e1b976dc08b076f592a487066p+0L, 398570af302Sopenharmony_ci 0x1.6247eb03a5584b1f0fa06fd2d9eap+0L, 399570af302Sopenharmony_ci 0x1.6434634ccc31fc76f8714c4ee122p+0L, 400570af302Sopenharmony_ci 0x1.66238825522249127d9e29b92ea2p+0L, 401570af302Sopenharmony_ci 0x1.68155d44ca973081c57227b9f69ep+0L, 402570af302Sopenharmony_ci}; 403570af302Sopenharmony_ci 404570af302Sopenharmony_cistatic const float eps[TBLSIZE] = { 405570af302Sopenharmony_ci -0x1.5c50p-101, 406570af302Sopenharmony_ci -0x1.5d00p-106, 407570af302Sopenharmony_ci 0x1.8e90p-102, 408570af302Sopenharmony_ci -0x1.5340p-103, 409570af302Sopenharmony_ci 0x1.1bd0p-102, 410570af302Sopenharmony_ci -0x1.4600p-105, 411570af302Sopenharmony_ci -0x1.7a40p-104, 412570af302Sopenharmony_ci 0x1.d590p-102, 413570af302Sopenharmony_ci -0x1.d590p-101, 414570af302Sopenharmony_ci 0x1.b100p-103, 415570af302Sopenharmony_ci -0x1.0d80p-105, 416570af302Sopenharmony_ci 0x1.6b00p-103, 417570af302Sopenharmony_ci -0x1.9f00p-105, 418570af302Sopenharmony_ci 0x1.c400p-103, 419570af302Sopenharmony_ci 0x1.e120p-103, 420570af302Sopenharmony_ci -0x1.c100p-104, 421570af302Sopenharmony_ci -0x1.9d20p-103, 422570af302Sopenharmony_ci 0x1.a800p-108, 423570af302Sopenharmony_ci 0x1.4c00p-106, 424570af302Sopenharmony_ci -0x1.9500p-106, 425570af302Sopenharmony_ci 0x1.6900p-105, 426570af302Sopenharmony_ci -0x1.29d0p-100, 427570af302Sopenharmony_ci 0x1.4c60p-103, 428570af302Sopenharmony_ci 0x1.13a0p-102, 429570af302Sopenharmony_ci -0x1.5b60p-103, 430570af302Sopenharmony_ci -0x1.1c40p-103, 431570af302Sopenharmony_ci 0x1.db80p-102, 432570af302Sopenharmony_ci 0x1.91a0p-102, 433570af302Sopenharmony_ci 0x1.dc00p-105, 434570af302Sopenharmony_ci 0x1.44c0p-104, 435570af302Sopenharmony_ci 0x1.9710p-102, 436570af302Sopenharmony_ci 0x1.8760p-103, 437570af302Sopenharmony_ci -0x1.a720p-103, 438570af302Sopenharmony_ci 0x1.ed20p-103, 439570af302Sopenharmony_ci -0x1.49c0p-102, 440570af302Sopenharmony_ci -0x1.e000p-111, 441570af302Sopenharmony_ci 0x1.86a0p-103, 442570af302Sopenharmony_ci 0x1.2b40p-103, 443570af302Sopenharmony_ci -0x1.b400p-108, 444570af302Sopenharmony_ci 0x1.1280p-99, 445570af302Sopenharmony_ci -0x1.02d8p-102, 446570af302Sopenharmony_ci -0x1.e3d0p-103, 447570af302Sopenharmony_ci -0x1.b080p-105, 448570af302Sopenharmony_ci -0x1.f100p-107, 449570af302Sopenharmony_ci -0x1.16c0p-105, 450570af302Sopenharmony_ci -0x1.1190p-103, 451570af302Sopenharmony_ci -0x1.a7d2p-100, 452570af302Sopenharmony_ci 0x1.3450p-103, 453570af302Sopenharmony_ci -0x1.67c0p-105, 454570af302Sopenharmony_ci 0x1.4b80p-104, 455570af302Sopenharmony_ci -0x1.c4e0p-103, 456570af302Sopenharmony_ci 0x1.6000p-108, 457570af302Sopenharmony_ci -0x1.3f60p-105, 458570af302Sopenharmony_ci 0x1.93f0p-104, 459570af302Sopenharmony_ci 0x1.5fe0p-105, 460570af302Sopenharmony_ci 0x1.6f80p-107, 461570af302Sopenharmony_ci -0x1.7600p-106, 462570af302Sopenharmony_ci 0x1.21e0p-106, 463570af302Sopenharmony_ci -0x1.3a40p-106, 464570af302Sopenharmony_ci -0x1.40c0p-104, 465570af302Sopenharmony_ci -0x1.9860p-105, 466570af302Sopenharmony_ci -0x1.5d40p-108, 467570af302Sopenharmony_ci -0x1.1d70p-106, 468570af302Sopenharmony_ci 0x1.2760p-105, 469570af302Sopenharmony_ci 0x0.0000p+0, 470570af302Sopenharmony_ci 0x1.21e2p-104, 471570af302Sopenharmony_ci -0x1.9520p-108, 472570af302Sopenharmony_ci -0x1.5720p-106, 473570af302Sopenharmony_ci -0x1.4810p-106, 474570af302Sopenharmony_ci -0x1.be00p-109, 475570af302Sopenharmony_ci 0x1.0080p-105, 476570af302Sopenharmony_ci -0x1.5780p-108, 477570af302Sopenharmony_ci -0x1.d460p-105, 478570af302Sopenharmony_ci -0x1.6140p-105, 479570af302Sopenharmony_ci 0x1.4630p-104, 480570af302Sopenharmony_ci 0x1.ad50p-103, 481570af302Sopenharmony_ci 0x1.82e0p-105, 482570af302Sopenharmony_ci 0x1.1d3cp-101, 483570af302Sopenharmony_ci 0x1.6100p-107, 484570af302Sopenharmony_ci 0x1.ec30p-104, 485570af302Sopenharmony_ci 0x1.f200p-108, 486570af302Sopenharmony_ci 0x1.0b40p-103, 487570af302Sopenharmony_ci 0x1.3660p-102, 488570af302Sopenharmony_ci 0x1.d9d0p-103, 489570af302Sopenharmony_ci -0x1.02d0p-102, 490570af302Sopenharmony_ci 0x1.b070p-103, 491570af302Sopenharmony_ci 0x1.b9c0p-104, 492570af302Sopenharmony_ci -0x1.01c0p-103, 493570af302Sopenharmony_ci -0x1.dfe0p-103, 494570af302Sopenharmony_ci 0x1.1b60p-104, 495570af302Sopenharmony_ci -0x1.ae94p-101, 496570af302Sopenharmony_ci -0x1.3340p-104, 497570af302Sopenharmony_ci 0x1.b3d8p-102, 498570af302Sopenharmony_ci -0x1.6e40p-105, 499570af302Sopenharmony_ci -0x1.3670p-103, 500570af302Sopenharmony_ci 0x1.c140p-104, 501570af302Sopenharmony_ci 0x1.1840p-101, 502570af302Sopenharmony_ci 0x1.1ab0p-102, 503570af302Sopenharmony_ci -0x1.a400p-104, 504570af302Sopenharmony_ci 0x1.1f00p-104, 505570af302Sopenharmony_ci -0x1.7180p-103, 506570af302Sopenharmony_ci 0x1.4ce0p-102, 507570af302Sopenharmony_ci 0x1.9200p-107, 508570af302Sopenharmony_ci -0x1.54c0p-103, 509570af302Sopenharmony_ci 0x1.1b80p-105, 510570af302Sopenharmony_ci -0x1.1828p-101, 511570af302Sopenharmony_ci 0x1.5720p-102, 512570af302Sopenharmony_ci -0x1.a060p-100, 513570af302Sopenharmony_ci 0x1.9160p-102, 514570af302Sopenharmony_ci 0x1.a280p-104, 515570af302Sopenharmony_ci 0x1.3400p-107, 516570af302Sopenharmony_ci 0x1.2b20p-102, 517570af302Sopenharmony_ci 0x1.7800p-108, 518570af302Sopenharmony_ci 0x1.cfd0p-101, 519570af302Sopenharmony_ci 0x1.2ef0p-102, 520570af302Sopenharmony_ci -0x1.2760p-99, 521570af302Sopenharmony_ci 0x1.b380p-104, 522570af302Sopenharmony_ci 0x1.0048p-101, 523570af302Sopenharmony_ci -0x1.60b0p-102, 524570af302Sopenharmony_ci 0x1.a1ccp-100, 525570af302Sopenharmony_ci -0x1.a640p-104, 526570af302Sopenharmony_ci -0x1.08a0p-101, 527570af302Sopenharmony_ci 0x1.7e60p-102, 528570af302Sopenharmony_ci 0x1.22c0p-103, 529570af302Sopenharmony_ci -0x1.7200p-106, 530570af302Sopenharmony_ci 0x1.f0f0p-102, 531570af302Sopenharmony_ci 0x1.eb4ep-99, 532570af302Sopenharmony_ci 0x1.c6e0p-103, 533570af302Sopenharmony_ci}; 534570af302Sopenharmony_ci 535570af302Sopenharmony_ci/* 536570af302Sopenharmony_ci * exp2l(x): compute the base 2 exponential of x 537570af302Sopenharmony_ci * 538570af302Sopenharmony_ci * Accuracy: Peak error < 0.502 ulp. 539570af302Sopenharmony_ci * 540570af302Sopenharmony_ci * Method: (accurate tables) 541570af302Sopenharmony_ci * 542570af302Sopenharmony_ci * Reduce x: 543570af302Sopenharmony_ci * x = 2**k + y, for integer k and |y| <= 1/2. 544570af302Sopenharmony_ci * Thus we have exp2(x) = 2**k * exp2(y). 545570af302Sopenharmony_ci * 546570af302Sopenharmony_ci * Reduce y: 547570af302Sopenharmony_ci * y = i/TBLSIZE + z - eps[i] for integer i near y * TBLSIZE. 548570af302Sopenharmony_ci * Thus we have exp2(y) = exp2(i/TBLSIZE) * exp2(z - eps[i]), 549570af302Sopenharmony_ci * with |z - eps[i]| <= 2**-8 + 2**-98 for the table used. 550570af302Sopenharmony_ci * 551570af302Sopenharmony_ci * We compute exp2(i/TBLSIZE) via table lookup and exp2(z - eps[i]) via 552570af302Sopenharmony_ci * a degree-10 minimax polynomial with maximum error under 2**-120. 553570af302Sopenharmony_ci * The values in exp2t[] and eps[] are chosen such that 554570af302Sopenharmony_ci * exp2t[i] = exp2(i/TBLSIZE + eps[i]), and eps[i] is a small offset such 555570af302Sopenharmony_ci * that exp2t[i] is accurate to 2**-122. 556570af302Sopenharmony_ci * 557570af302Sopenharmony_ci * Note that the range of i is +-TBLSIZE/2, so we actually index the tables 558570af302Sopenharmony_ci * by i0 = i + TBLSIZE/2. 559570af302Sopenharmony_ci * 560570af302Sopenharmony_ci * This method is due to Gal, with many details due to Gal and Bachelis: 561570af302Sopenharmony_ci * 562570af302Sopenharmony_ci * Gal, S. and Bachelis, B. An Accurate Elementary Mathematical Library 563570af302Sopenharmony_ci * for the IEEE Floating Point Standard. TOMS 17(1), 26-46 (1991). 564570af302Sopenharmony_ci */ 565570af302Sopenharmony_cilong double 566570af302Sopenharmony_ciexp2l(long double x) 567570af302Sopenharmony_ci{ 568570af302Sopenharmony_ci union ldshape u = {x}; 569570af302Sopenharmony_ci int e = u.i.se & 0x7fff; 570570af302Sopenharmony_ci long double r, z, t; 571570af302Sopenharmony_ci uint32_t i0; 572570af302Sopenharmony_ci union {uint32_t u; int32_t i;} k; 573570af302Sopenharmony_ci 574570af302Sopenharmony_ci /* Filter out exceptional cases. */ 575570af302Sopenharmony_ci if (e >= 0x3fff + 14) { /* |x| >= 16384 or x is NaN */ 576570af302Sopenharmony_ci if (u.i.se >= 0x3fff + 15 && u.i.se >> 15 == 0) 577570af302Sopenharmony_ci /* overflow */ 578570af302Sopenharmony_ci return x * 0x1p16383L; 579570af302Sopenharmony_ci if (e == 0x7fff) /* -inf or -nan */ 580570af302Sopenharmony_ci return -1/x; 581570af302Sopenharmony_ci if (x < -16382) { 582570af302Sopenharmony_ci if (x <= -16495 || x - 0x1p112 + 0x1p112 != x) 583570af302Sopenharmony_ci /* underflow */ 584570af302Sopenharmony_ci FORCE_EVAL((float)(-0x1p-149/x)); 585570af302Sopenharmony_ci if (x <= -16446) 586570af302Sopenharmony_ci return 0; 587570af302Sopenharmony_ci } 588570af302Sopenharmony_ci } else if (e < 0x3fff - 114) { 589570af302Sopenharmony_ci return 1 + x; 590570af302Sopenharmony_ci } 591570af302Sopenharmony_ci 592570af302Sopenharmony_ci /* 593570af302Sopenharmony_ci * Reduce x, computing z, i0, and k. The low bits of x + redux 594570af302Sopenharmony_ci * contain the 16-bit integer part of the exponent (k) followed by 595570af302Sopenharmony_ci * TBLBITS fractional bits (i0). We use bit tricks to extract these 596570af302Sopenharmony_ci * as integers, then set z to the remainder. 597570af302Sopenharmony_ci * 598570af302Sopenharmony_ci * Example: Suppose x is 0xabc.123456p0 and TBLBITS is 8. 599570af302Sopenharmony_ci * Then the low-order word of x + redux is 0x000abc12, 600570af302Sopenharmony_ci * We split this into k = 0xabc and i0 = 0x12 (adjusted to 601570af302Sopenharmony_ci * index into the table), then we compute z = 0x0.003456p0. 602570af302Sopenharmony_ci */ 603570af302Sopenharmony_ci u.f = x + redux; 604570af302Sopenharmony_ci i0 = u.i2.lo + TBLSIZE / 2; 605570af302Sopenharmony_ci k.u = i0 / TBLSIZE * TBLSIZE; 606570af302Sopenharmony_ci k.i /= TBLSIZE; 607570af302Sopenharmony_ci i0 %= TBLSIZE; 608570af302Sopenharmony_ci u.f -= redux; 609570af302Sopenharmony_ci z = x - u.f; 610570af302Sopenharmony_ci 611570af302Sopenharmony_ci /* Compute r = exp2(y) = exp2t[i0] * p(z - eps[i]). */ 612570af302Sopenharmony_ci t = tbl[i0]; 613570af302Sopenharmony_ci z -= eps[i0]; 614570af302Sopenharmony_ci r = t + t * z * (P1 + z * (P2 + z * (P3 + z * (P4 + z * (P5 + z * (P6 615570af302Sopenharmony_ci + z * (P7 + z * (P8 + z * (P9 + z * P10))))))))); 616570af302Sopenharmony_ci 617570af302Sopenharmony_ci return scalbnl(r, k.i); 618570af302Sopenharmony_ci} 619570af302Sopenharmony_ci#endif 620