mirror of
https://github.com/capstone-engine/llvm-capstone.git
synced 2024-11-24 06:10:12 +00:00
[libc] Make logf function correctly rounded for all rounding modes.
Make logf function correctly rounded for all rounding modes. Reviewed By: sivachandra, zimmermann6, santoshn, jpl169 Differential Revision: https://reviews.llvm.org/D118149
This commit is contained in:
parent
491c154677
commit
82df72cc67
@ -9,13 +9,15 @@
|
||||
#include "src/math/logf.h"
|
||||
#include "common_constants.h" // Lookup table for (1/f)
|
||||
#include "src/__support/FPUtil/BasicOperations.h"
|
||||
#include "src/__support/FPUtil/FEnvImpl.h"
|
||||
#include "src/__support/FPUtil/FMA.h"
|
||||
#include "src/__support/FPUtil/FPBits.h"
|
||||
#include "src/__support/FPUtil/PolyEval.h"
|
||||
#include "src/__support/common.h"
|
||||
|
||||
// This is a correctly-rounded algorithm for log(x) in single precision with
|
||||
// round-to-nearest, tie-to-even mode from the RLIBM project at:
|
||||
// This is an algorithm for log(x) in single precision which is correctly
|
||||
// rounded for all rounding modes, based on the implementation of log(x) from
|
||||
// the RLIBM project at:
|
||||
// https://people.cs.rutgers.edu/~sn349/rlibm
|
||||
|
||||
// Step 1 - Range reduction:
|
||||
@ -101,6 +103,42 @@ LLVM_LIBC_FUNCTION(float, logf, (float x)) {
|
||||
constexpr double LOG_2 = 0x1.62e42fefa39efp-1;
|
||||
using FPBits = typename fputil::FPBits<float>;
|
||||
FPBits xbits(x);
|
||||
|
||||
switch (FPBits(x).uintval()) {
|
||||
case 0x41178febU: // x = 0x1.2f1fd6p+3f
|
||||
if (fputil::get_round() == FE_TONEAREST)
|
||||
return 0x1.1fcbcep+1f;
|
||||
break;
|
||||
case 0x4c5d65a5U: // x = 0x1.bacb4ap+25f
|
||||
if (fputil::get_round() == FE_TONEAREST)
|
||||
return 0x1.1e0696p+4f;
|
||||
break;
|
||||
case 0x65d890d3U: // x = 0x1.b121a6p+76f
|
||||
if (fputil::get_round() == FE_TONEAREST)
|
||||
return 0x1.a9a3f2p+5f;
|
||||
break;
|
||||
case 0x6f31a8ecU: // x = 0x1.6351d8p+95f
|
||||
if (fputil::get_round() == FE_TONEAREST)
|
||||
return 0x1.08b512p+6f;
|
||||
break;
|
||||
case 0x3f800001U: // x = 0x1.000002p+0f
|
||||
if (fputil::get_round() == FE_UPWARD)
|
||||
return 0x1p-23f;
|
||||
return 0x1.fffffep-24f;
|
||||
case 0x500ffb03U: // x = 0x1.1ff606p+33f
|
||||
if (fputil::get_round() != FE_UPWARD)
|
||||
return 0x1.6fdd34p+4f;
|
||||
break;
|
||||
case 0x7a17f30aU: // x = 0x1.2fe614p+117f
|
||||
if (fputil::get_round() != FE_UPWARD)
|
||||
return 0x1.451436p+6f;
|
||||
break;
|
||||
case 0x5cd69e88U: // x = 0x1.ad3d1p+58f
|
||||
if (fputil::get_round() != FE_UPWARD)
|
||||
return 0x1.45c146p+5f;
|
||||
break;
|
||||
}
|
||||
|
||||
int m = 0;
|
||||
|
||||
if (xbits.uintval() < FPBits::MIN_NORMAL ||
|
||||
@ -130,26 +168,14 @@ LLVM_LIBC_FUNCTION(float, logf, (float x)) {
|
||||
double d = static_cast<float>(xbits) - static_cast<float>(f);
|
||||
d *= ONE_OVER_F[f_index];
|
||||
|
||||
double r = __llvm_libc::fputil::polyeval(
|
||||
d, 0x1.0000000008169p+0, -0x1.0000004f78405p-1, 0x1.555654d2bc769p-2,
|
||||
-0x1.00a570d090322p-2, 0x1.e158d823f89cap-3);
|
||||
|
||||
double extra_factor =
|
||||
__llvm_libc::fputil::fma(static_cast<double>(m), LOG_2, LOG_F[f_index]);
|
||||
switch (FPBits(x).uintval()) {
|
||||
case 0x3f80d19f:
|
||||
return 0x1.a1e82cp-8f;
|
||||
case 0x41178feb:
|
||||
return 0x1.1fcbcep+1f;
|
||||
case 0x4c5d65a5:
|
||||
return 0x1.1e0696p+4f;
|
||||
case 0x65d890d3:
|
||||
return 0x1.a9a3f2p+5f;
|
||||
case 0x6f31a8ec:
|
||||
return 0x1.08b512p+6f;
|
||||
default:
|
||||
return static_cast<float>(__llvm_libc::fputil::fma(d, r, extra_factor));
|
||||
}
|
||||
fputil::fma(static_cast<double>(m), LOG_2, LOG_F[f_index]);
|
||||
|
||||
double r = __llvm_libc::fputil::polyeval(
|
||||
d, extra_factor, 0x1.fffffffffffacp-1, -0x1.fffffffef9cb2p-2,
|
||||
0x1.5555513bc679ap-2, -0x1.fff4805ea441p-3, 0x1.930180dbde91ap-3);
|
||||
|
||||
return static_cast<float>(r);
|
||||
}
|
||||
|
||||
#pragma clang diagnostic pop
|
||||
|
@ -31,13 +31,37 @@ TEST(LlvmLibcLogfTest, SpecialNumbers) {
|
||||
}
|
||||
|
||||
TEST(LlvmLibcLogfTest, TrickyInputs) {
|
||||
constexpr int N = 24;
|
||||
constexpr int N = 28;
|
||||
constexpr uint32_t INPUTS[N] = {
|
||||
0x3509dcf6U, 0x3bf86ef0U, 0x3ca1c99fU, 0x3d13e105U, 0x3f7ff1f2U,
|
||||
0x3f7fffffU, 0x3f800006U, 0x3f800014U, 0x3f80001cU, 0x3f80c777U,
|
||||
0x3f80ce72U, 0x3f80d19fU, 0x3f80f7bfU, 0x3f80fcfeU, 0x3f81feb4U,
|
||||
0x3f83d731U, 0x3f90cb1dU, 0x3fc55379U, 0x3fd364d7U, 0x41178febU,
|
||||
0x4c5d65a5U, 0x4e85f412U, 0x65d890d3U, 0x6f31a8ecU};
|
||||
0x3509dcf6U, /*0x1.13b9ecp-21f*/
|
||||
0x3bf86ef0U, /*0x1.f0ddep-8f*/
|
||||
0x3ca1c99fU, /*0x1.43933ep-6f*/
|
||||
0x3d13e105U, /*0x1.27c20ap-5f*/
|
||||
0x3f7ff1f2U, /*0x1.ffe3e4p-1f*/
|
||||
0x3f7fffffU, /*0x1.fffffep-1f*/
|
||||
0x3f800001U, /*0x1.000002p+0f*/
|
||||
0x3f800006U, /*0x1.00000cp+0f*/
|
||||
0x3f800014U, /*0x1.000028p+0f*/
|
||||
0x3f80001cU, /*0x1.000038p+0f*/
|
||||
0x3f80c777U, /*0x1.018eeep+0f*/
|
||||
0x3f80ce72U, /*0x1.019ce4p+0f*/
|
||||
0x3f80d19fU, /*0x1.01a33ep+0f*/
|
||||
0x3f80f7bfU, /*0x1.01ef7ep+0f*/
|
||||
0x3f80fcfeU, /*0x1.01f9fcp+0f*/
|
||||
0x3f81feb4U, /*0x1.03fd68p+0f*/
|
||||
0x3f83d731U, /*0x1.07ae62p+0f*/
|
||||
0x3f90cb1dU, /*0x1.21963ap+0f*/
|
||||
0x3fc55379U, /*0x1.8aa6f2p+0f*/
|
||||
0x3fd364d7U, /*0x1.a6c9aep+0f*/
|
||||
0x41178febU, /*0x1.2f1fd6p+3f*/
|
||||
0x4c5d65a5U, /*0x1.bacb4ap+25f*/
|
||||
0x4e85f412U, /*0x1.0be824p+30f*/
|
||||
0x500ffb03U, /*0x1.1ff606p+33f*/
|
||||
0x5cd69e88U, /*0x1.ad3d1p+58f*/
|
||||
0x65d890d3U, /*0x1.b121a6p+76f*/
|
||||
0x6f31a8ecU, /*0x1.6351d8p+95f*/
|
||||
0x7a17f30aU, /*0x1.2fe614p+117f*/
|
||||
};
|
||||
for (int i = 0; i < N; ++i) {
|
||||
float x = float(FPBits(INPUTS[i]));
|
||||
EXPECT_MPFR_MATCH(mpfr::Operation::Log, x, __llvm_libc::logf(x), 0.5);
|
||||
|
Loading…
Reference in New Issue
Block a user