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 LLVM_LIBC_FUNCTION(float, logf, (float x)) { 53 constexpr double LOG_2 = 0x1.62e42fefa39efp-1; 54 using FPBits = typename fputil::FPBits<float>; 55 FPBits xbits(x); 56 57 switch (FPBits(x).uintval()) { 58 case 0x41178febU: // x = 0x1.2f1fd6p+3f 59 if (fputil::get_round() == FE_TONEAREST) 60 return 0x1.1fcbcep+1f; 61 break; 62 case 0x4c5d65a5U: // x = 0x1.bacb4ap+25f 63 if (fputil::get_round() == FE_TONEAREST) 64 return 0x1.1e0696p+4f; 65 break; 66 case 0x65d890d3U: // x = 0x1.b121a6p+76f 67 if (fputil::get_round() == FE_TONEAREST) 68 return 0x1.a9a3f2p+5f; 69 break; 70 case 0x6f31a8ecU: // x = 0x1.6351d8p+95f 71 if (fputil::get_round() == FE_TONEAREST) 72 return 0x1.08b512p+6f; 73 break; 74 case 0x3f800001U: // x = 0x1.000002p+0f 75 if (fputil::get_round() == FE_UPWARD) 76 return 0x1p-23f; 77 return 0x1.fffffep-24f; 78 case 0x500ffb03U: // x = 0x1.1ff606p+33f 79 if (fputil::get_round() != FE_UPWARD) 80 return 0x1.6fdd34p+4f; 81 break; 82 case 0x7a17f30aU: // x = 0x1.2fe614p+117f 83 if (fputil::get_round() != FE_UPWARD) 84 return 0x1.451436p+6f; 85 break; 86 case 0x5cd69e88U: // x = 0x1.ad3d1p+58f 87 if (fputil::get_round() != FE_UPWARD) 88 return 0x1.45c146p+5f; 89 break; 90 } 91 92 int m = 0; 93 94 if (xbits.uintval() < FPBits::MIN_NORMAL || 95 xbits.uintval() > FPBits::MAX_NORMAL) { 96 if (xbits.is_zero()) { 97 return static_cast<float>(FPBits::neg_inf()); 98 } 99 if (xbits.get_sign() && !xbits.is_nan()) { 100 return FPBits::build_nan(1 << (fputil::MantissaWidth<float>::VALUE - 1)); 101 } 102 if (xbits.is_inf_or_nan()) { 103 return x; 104 } 105 // Normalize denormal inputs. 106 xbits.set_val(xbits.get_val() * 0x1.0p23f); 107 m = -23; 108 } 109 110 m += xbits.get_exponent(); 111 // Set bits to 1.m 112 xbits.set_unbiased_exponent(0x7F); 113 int f_index = xbits.get_mantissa() >> 16; 114 115 FPBits f = xbits; 116 f.bits &= ~0x0000'FFFF; 117 118 double d = static_cast<float>(xbits) - static_cast<float>(f); 119 d *= ONE_OVER_F[f_index]; 120 121 double extra_factor = 122 fputil::multiply_add(static_cast<double>(m), LOG_2, LOG_F[f_index]); 123 124 double r = __llvm_libc::fputil::polyeval( 125 d, extra_factor, 0x1.fffffffffffacp-1, -0x1.fffffffef9cb2p-2, 126 0x1.5555513bc679ap-2, -0x1.fff4805ea441p-3, 0x1.930180dbde91ap-3); 127 128 return static_cast<float>(r); 129 } 130 131 } // namespace __llvm_libc 132