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) and log(f) 11 #include "src/__support/FPUtil/BasicOperations.h" 12 #include "src/__support/FPUtil/FEnvImpl.h" 13 #include "src/__support/FPUtil/FMA.h" 14 #include "src/__support/FPUtil/FPBits.h" 15 #include "src/__support/FPUtil/PolyEval.h" 16 #include "src/__support/common.h" 17 18 // This is an algorithm for log(x) in single precision which is correctly 19 // rounded for all rounding modes, based on the implementation of log(x) from 20 // the RLIBM project at: 21 // https://people.cs.rutgers.edu/~sn349/rlibm 22 23 // Step 1 - Range reduction: 24 // For x = 2^m * 1.mant, log(x) = m * log(2) + log(1.m) 25 // If x is denormal, we normalize it by multiplying x by 2^23 and subtracting 26 // m by 23. 27 28 // Step 2 - Another range reduction: 29 // To compute log(1.mant), let f be the highest 8 bits including the hidden 30 // bit, and d be the difference (1.mant - f), i.e. the remaining 16 bits of the 31 // mantissa. Then we have the following approximation formula: 32 // log(1.mant) = log(f) + log(1.mant / f) 33 // = log(f) + log(1 + d/f) 34 // ~ log(f) + P(d/f) 35 // since d/f is sufficiently small. 36 // log(f) and 1/f are then stored in two 2^7 = 128 entries look-up tables. 37 38 // Step 3 - Polynomial approximation: 39 // To compute P(d/f), we use a single degree-5 polynomial in double precision 40 // which provides correct rounding for all but few exception values. 41 // For more detail about how this polynomial is obtained, please refer to the 42 // paper: 43 // Lim, J. and Nagarakatte, S., "One Polynomial Approximation to Produce 44 // Correctly Rounded Results of an Elementary Function for Multiple 45 // Representations and Rounding Modes", Proceedings of the 49th ACM SIGPLAN 46 // Symposium on Principles of Programming Languages (POPL-2022), Philadelphia, 47 // USA, January 16-22, 2022. 48 // https://people.cs.rutgers.edu/~sn349/papers/rlibmall-popl-2022.pdf 49 50 namespace __llvm_libc { 51 52 INLINE_FMA 53 LLVM_LIBC_FUNCTION(float, logf, (float x)) { 54 constexpr double LOG_2 = 0x1.62e42fefa39efp-1; 55 using FPBits = typename fputil::FPBits<float>; 56 FPBits xbits(x); 57 58 switch (FPBits(x).uintval()) { 59 case 0x41178febU: // x = 0x1.2f1fd6p+3f 60 if (fputil::get_round() == FE_TONEAREST) 61 return 0x1.1fcbcep+1f; 62 break; 63 case 0x4c5d65a5U: // x = 0x1.bacb4ap+25f 64 if (fputil::get_round() == FE_TONEAREST) 65 return 0x1.1e0696p+4f; 66 break; 67 case 0x65d890d3U: // x = 0x1.b121a6p+76f 68 if (fputil::get_round() == FE_TONEAREST) 69 return 0x1.a9a3f2p+5f; 70 break; 71 case 0x6f31a8ecU: // x = 0x1.6351d8p+95f 72 if (fputil::get_round() == FE_TONEAREST) 73 return 0x1.08b512p+6f; 74 break; 75 case 0x3f800001U: // x = 0x1.000002p+0f 76 if (fputil::get_round() == FE_UPWARD) 77 return 0x1p-23f; 78 return 0x1.fffffep-24f; 79 case 0x500ffb03U: // x = 0x1.1ff606p+33f 80 if (fputil::get_round() != FE_UPWARD) 81 return 0x1.6fdd34p+4f; 82 break; 83 case 0x7a17f30aU: // x = 0x1.2fe614p+117f 84 if (fputil::get_round() != FE_UPWARD) 85 return 0x1.451436p+6f; 86 break; 87 case 0x5cd69e88U: // x = 0x1.ad3d1p+58f 88 if (fputil::get_round() != FE_UPWARD) 89 return 0x1.45c146p+5f; 90 break; 91 } 92 93 int m = 0; 94 95 if (xbits.uintval() < FPBits::MIN_NORMAL || 96 xbits.uintval() > FPBits::MAX_NORMAL) { 97 if (xbits.is_zero()) { 98 return static_cast<float>(FPBits::neg_inf()); 99 } 100 if (xbits.get_sign() && !xbits.is_nan()) { 101 return FPBits::build_nan(1 << (fputil::MantissaWidth<float>::VALUE - 1)); 102 } 103 if (xbits.is_inf_or_nan()) { 104 return x; 105 } 106 // Normalize denormal inputs. 107 xbits.set_val(xbits.get_val() * 0x1.0p23f); 108 m = -23; 109 } 110 111 m += xbits.get_exponent(); 112 // Set bits to 1.m 113 xbits.set_unbiased_exponent(0x7F); 114 int f_index = xbits.get_mantissa() >> 16; 115 116 FPBits f = xbits; 117 f.bits &= ~0x0000'FFFF; 118 119 double d = static_cast<float>(xbits) - static_cast<float>(f); 120 d *= ONE_OVER_F[f_index]; 121 122 double extra_factor = 123 fputil::multiply_add(static_cast<double>(m), LOG_2, LOG_F[f_index]); 124 125 double r = __llvm_libc::fputil::polyeval( 126 d, extra_factor, 0x1.fffffffffffacp-1, -0x1.fffffffef9cb2p-2, 127 0x1.5555513bc679ap-2, -0x1.fff4805ea441p-3, 0x1.930180dbde91ap-3); 128 129 return static_cast<float>(r); 130 } 131 132 } // namespace __llvm_libc 133