1 //===-- Single-precision log(x) 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-exception 6 // 7 //===----------------------------------------------------------------------===// 8 9 #include "src/math/logf.h" 10 #include "common_constants.h" // Lookup table for (1/f) 11 #include "src/__support/FPUtil/BasicOperations.h" 12 #include "src/__support/FPUtil/FMA.h" 13 #include "src/__support/FPUtil/FPBits.h" 14 #include "src/__support/FPUtil/PolyEval.h" 15 #include "src/__support/common.h" 16 17 // This is a correctly-rounded algorithm for log(x) in single precision with 18 // round-to-nearest, tie-to-even mode from the RLIBM project at: 19 // https://people.cs.rutgers.edu/~sn349/rlibm 20 21 // Step 1 - Range reduction: 22 // For x = 2^m * 1.mant, log(x) = m * log(2) + log(1.m) 23 // If x is denormal, we normalize it by multiplying x by 2^23 and subtracting 24 // m by 23. 25 26 // Step 2 - Another range reduction: 27 // To compute log(1.mant), let f be the highest 8 bits including the hidden 28 // bit, and d be the difference (1.mant - f), i.e. the remaining 16 bits of the 29 // mantissa. Then we have the following approximation formula: 30 // log(1.mant) = log(f) + log(1.mant / f) 31 // = log(f) + log(1 + d/f) 32 // ~ log(f) + P(d/f) 33 // since d/f is sufficiently small. 34 // log(f) and 1/f are then stored in two 2^7 = 128 entries look-up tables. 35 36 // Step 3 - Polynomial approximation: 37 // To compute P(d/f), we use a single degree-5 polynomial in double precision 38 // which provides correct rounding for all but few exception values. 39 // For more detail about how this polynomial is obtained, please refer to the 40 // paper: 41 // Lim, J. and Nagarakatte, S., "One Polynomial Approximation to Produce 42 // Correctly Rounded Results of an Elementary Function for Multiple 43 // Representations and Rounding Modes", Proceedings of the 49th ACM SIGPLAN 44 // Symposium on Principles of Programming Languages (POPL-2022), Philadelphia, 45 // USA, January 16-22, 2022. 46 // https://people.cs.rutgers.edu/~sn349/papers/rlibmall-popl-2022.pdf 47 48 namespace __llvm_libc { 49 50 #pragma clang diagnostic push 51 #pragma clang diagnostic ignored "-Wc++17-extensions" 52 53 // Lookup table for log(f) = log(1 + n*2^(-7)) where n = 0..127. 54 static constexpr double LOG_F[128] = { 55 0x0.0000000000000p+0, 0x1.fe02a6b106788p-8, 0x1.fc0a8b0fc03e3p-7, 56 0x1.7b91b07d5b11ap-6, 0x1.f829b0e783300p-6, 0x1.39e87b9febd5fp-5, 57 0x1.77458f632dcfcp-5, 0x1.b42dd711971bep-5, 0x1.f0a30c01162a6p-5, 58 0x1.16536eea37ae0p-4, 0x1.341d7961bd1d0p-4, 0x1.51b073f06183fp-4, 59 0x1.6f0d28ae56b4bp-4, 0x1.8c345d6319b20p-4, 0x1.a926d3a4ad563p-4, 60 0x1.c5e548f5bc743p-4, 0x1.e27076e2af2e5p-4, 0x1.fec9131dbeabap-4, 61 0x1.0d77e7cd08e59p-3, 0x1.1b72ad52f67a0p-3, 0x1.29552f81ff523p-3, 62 0x1.371fc201e8f74p-3, 0x1.44d2b6ccb7d1ep-3, 0x1.526e5e3a1b437p-3, 63 0x1.5ff3070a793d3p-3, 0x1.6d60fe719d21cp-3, 0x1.7ab890210d909p-3, 64 0x1.87fa06520c910p-3, 0x1.9525a9cf456b4p-3, 0x1.a23bc1fe2b563p-3, 65 0x1.af3c94e80bff2p-3, 0x1.bc286742d8cd6p-3, 0x1.c8ff7c79a9a21p-3, 66 0x1.d5c216b4fbb91p-3, 0x1.e27076e2af2e5p-3, 0x1.ef0adcbdc5936p-3, 67 0x1.fb9186d5e3e2ap-3, 0x1.0402594b4d040p-2, 0x1.0a324e27390e3p-2, 68 0x1.1058bf9ae4ad5p-2, 0x1.1675cababa60ep-2, 0x1.1c898c16999fap-2, 69 0x1.22941fbcf7965p-2, 0x1.2895a13de86a3p-2, 0x1.2e8e2bae11d30p-2, 70 0x1.347dd9a987d54p-2, 0x1.3a64c556945e9p-2, 0x1.404308686a7e3p-2, 71 0x1.4618bc21c5ec2p-2, 0x1.4be5f957778a0p-2, 0x1.51aad872df82dp-2, 72 0x1.5767717455a6cp-2, 0x1.5d1bdbf5809cap-2, 0x1.62c82f2b9c795p-2, 73 0x1.686c81e9b14aep-2, 0x1.6e08eaa2ba1e3p-2, 0x1.739d7f6bbd006p-2, 74 0x1.792a55fdd47a2p-2, 0x1.7eaf83b82afc3p-2, 0x1.842d1da1e8b17p-2, 75 0x1.89a3386c1425ap-2, 0x1.8f11e873662c7p-2, 0x1.947941c2116fap-2, 76 0x1.99d958117e08ap-2, 0x1.9f323ecbf984bp-2, 0x1.a484090e5bb0ap-2, 77 0x1.a9cec9a9a0849p-2, 0x1.af1293247786bp-2, 0x1.b44f77bcc8f62p-2, 78 0x1.b9858969310fbp-2, 0x1.beb4d9da71b7bp-2, 0x1.c3dd7a7cdad4dp-2, 79 0x1.c8ff7c79a9a21p-2, 0x1.ce1af0b85f3ebp-2, 0x1.d32fe7e00ebd5p-2, 80 0x1.d83e7258a2f3ep-2, 0x1.dd46a04c1c4a0p-2, 0x1.e24881a7c6c26p-2, 81 0x1.e744261d68787p-2, 0x1.ec399d2468cc0p-2, 0x1.f128f5faf06ecp-2, 82 0x1.f6123fa7028acp-2, 0x1.faf588f78f31ep-2, 0x1.ffd2e0857f498p-2, 83 0x1.02552a5a5d0fep-1, 0x1.04bdf9da926d2p-1, 0x1.0723e5c1cdf40p-1, 84 0x1.0986f4f573520p-1, 0x1.0be72e4252a82p-1, 0x1.0e44985d1cc8bp-1, 85 0x1.109f39e2d4c96p-1, 0x1.12f719593efbcp-1, 0x1.154c3d2f4d5e9p-1, 86 0x1.179eabbd899a0p-1, 0x1.19ee6b467c96ep-1, 0x1.1c3b81f713c24p-1, 87 0x1.1e85f5e7040d0p-1, 0x1.20cdcd192ab6dp-1, 0x1.23130d7bebf42p-1, 88 0x1.2555bce98f7cbp-1, 0x1.2795e1289b11ap-1, 0x1.29d37fec2b08ap-1, 89 0x1.2c0e9ed448e8bp-1, 0x1.2e47436e40268p-1, 0x1.307d7334f10bep-1, 90 0x1.32b1339121d71p-1, 0x1.34e289d9ce1d3p-1, 0x1.37117b54747b5p-1, 91 0x1.393e0d3562a19p-1, 0x1.3b68449fffc22p-1, 0x1.3d9026a7156fap-1, 92 0x1.3fb5b84d16f42p-1, 0x1.41d8fe84672aep-1, 0x1.43f9fe2f9ce67p-1, 93 0x1.4618bc21c5ec2p-1, 0x1.48353d1ea88dfp-1, 0x1.4a4f85db03ebbp-1, 94 0x1.4c679afccee39p-1, 0x1.4e7d811b75bb0p-1, 0x1.50913cc01686bp-1, 95 0x1.52a2d265bc5aap-1, 0x1.54b2467999497p-1, 0x1.56bf9d5b3f399p-1, 96 0x1.58cadb5cd7989p-1, 0x1.5ad404c359f2cp-1, 0x1.5cdb1dc6c1764p-1, 97 0x1.5ee02a9241675p-1, 0x1.60e32f44788d8p-1}; 98 99 INLINE_FMA 100 LLVM_LIBC_FUNCTION(float, logf, (float x)) { 101 constexpr double LOG_2 = 0x1.62e42fefa39efp-1; 102 using FPBits = typename fputil::FPBits<float>; 103 FPBits xbits(x); 104 int m = 0; 105 106 if (xbits.uintval() < FPBits::MIN_NORMAL || 107 xbits.uintval() > FPBits::MAX_NORMAL) { 108 if (xbits.is_zero()) { 109 return static_cast<float>(FPBits::neg_inf()); 110 } 111 if (xbits.get_sign() && !xbits.is_nan()) { 112 return FPBits::build_nan(1 << (fputil::MantissaWidth<float>::VALUE - 1)); 113 } 114 if (xbits.is_inf_or_nan()) { 115 return x; 116 } 117 // Normalize denormal inputs. 118 xbits.val *= 0x1.0p23f; 119 m = -23; 120 } 121 122 m += xbits.get_exponent(); 123 // Set bits to 1.m 124 xbits.set_unbiased_exponent(0x7F); 125 int f_index = xbits.get_mantissa() >> 16; 126 127 FPBits f(xbits.val); 128 f.bits &= ~0x0000'FFFF; 129 130 double d = static_cast<float>(xbits) - static_cast<float>(f); 131 d *= ONE_OVER_F[f_index]; 132 133 double r = __llvm_libc::fputil::polyeval( 134 d, 0x1.0000000008169p+0, -0x1.0000004f78405p-1, 0x1.555654d2bc769p-2, 135 -0x1.00a570d090322p-2, 0x1.e158d823f89cap-3); 136 137 double extra_factor = 138 __llvm_libc::fputil::fma(static_cast<double>(m), LOG_2, LOG_F[f_index]); 139 switch (FPBits(x).uintval()) { 140 case 0x3f80d19f: 141 return 0x1.a1e82cp-8f; 142 case 0x41178feb: 143 return 0x1.1fcbcep+1f; 144 case 0x4c5d65a5: 145 return 0x1.1e0696p+4f; 146 case 0x65d890d3: 147 return 0x1.a9a3f2p+5f; 148 case 0x6f31a8ec: 149 return 0x1.08b512p+6f; 150 default: 151 return static_cast<float>(__llvm_libc::fputil::fma(d, r, extra_factor)); 152 } 153 } 154 155 #pragma clang diagnostic pop 156 157 } // namespace __llvm_libc 158