[libc-commits] [libc] 46b15fd - [libc][math] Implement asinhf function correctly rounded for all rounding modes.

Tue Ly via libc-commits libc-commits at lists.llvm.org
Fri Jan 27 08:12:43 PST 2023


Author: Tue Ly
Date: 2023-01-27T11:12:27-05:00
New Revision: 46b15fd19e84f6914e067ef9812c282a43b5924d

URL: https://github.com/llvm/llvm-project/commit/46b15fd19e84f6914e067ef9812c282a43b5924d
DIFF: https://github.com/llvm/llvm-project/commit/46b15fd19e84f6914e067ef9812c282a43b5924d.diff

LOG: [libc][math] Implement asinhf function correctly rounded for all rounding modes.

Implement asinhf function correctly rounded for all rounding modes.

Reviewed By: zimmermann6

Differential Revision: https://reviews.llvm.org/D142681

Added: 
    libc/src/math/asinhf.h
    libc/src/math/generic/asinhf.cpp
    libc/test/src/math/asinhf_test.cpp
    libc/test/src/math/exhaustive/asinhf_test.cpp

Modified: 
    libc/config/darwin/arm/entrypoints.txt
    libc/config/linux/aarch64/entrypoints.txt
    libc/config/linux/x86_64/entrypoints.txt
    libc/config/windows/entrypoints.txt
    libc/docs/math.rst
    libc/spec/stdc.td
    libc/src/math/CMakeLists.txt
    libc/src/math/generic/CMakeLists.txt
    libc/src/math/generic/explogxf.h
    libc/test/src/math/CMakeLists.txt
    libc/test/src/math/exhaustive/CMakeLists.txt
    libc/test/src/math/exhaustive/exhaustive_test.cpp
    libc/utils/MPFRWrapper/MPFRUtils.cpp
    libc/utils/MPFRWrapper/MPFRUtils.h

Removed: 
    


################################################################################
diff  --git a/libc/config/darwin/arm/entrypoints.txt b/libc/config/darwin/arm/entrypoints.txt
index a1ad122e13c7f..f88981c7e57c3 100644
--- a/libc/config/darwin/arm/entrypoints.txt
+++ b/libc/config/darwin/arm/entrypoints.txt
@@ -111,6 +111,7 @@ set(TARGET_LIBM_ENTRYPOINTS
     # math.h entrypoints
     libc.src.math.acosf
     libc.src.math.asinf
+    libc.src.math.asinhf
     libc.src.math.atanf
     libc.src.math.atanhf
     libc.src.math.copysign

diff  --git a/libc/config/linux/aarch64/entrypoints.txt b/libc/config/linux/aarch64/entrypoints.txt
index b9460acaa77f5..4af9b37ec6d99 100644
--- a/libc/config/linux/aarch64/entrypoints.txt
+++ b/libc/config/linux/aarch64/entrypoints.txt
@@ -212,6 +212,7 @@ set(TARGET_LIBM_ENTRYPOINTS
     libc.src.math.acosf
     libc.src.math.asin
     libc.src.math.asinf
+    libc.src.math.asinhf
     libc.src.math.atanf
     libc.src.math.atanhf    
     libc.src.math.copysign

diff  --git a/libc/config/linux/x86_64/entrypoints.txt b/libc/config/linux/x86_64/entrypoints.txt
index 7b12ec710100b..64d49984dfd9b 100644
--- a/libc/config/linux/x86_64/entrypoints.txt
+++ b/libc/config/linux/x86_64/entrypoints.txt
@@ -213,6 +213,7 @@ set(TARGET_LIBM_ENTRYPOINTS
     libc.src.math.acosf
     libc.src.math.asin
     libc.src.math.asinf
+    libc.src.math.asinhf
     libc.src.math.atanf
     libc.src.math.atanhf
     libc.src.math.copysign

diff  --git a/libc/config/windows/entrypoints.txt b/libc/config/windows/entrypoints.txt
index 61cf5a15c6ab6..9dedd14c0df77 100644
--- a/libc/config/windows/entrypoints.txt
+++ b/libc/config/windows/entrypoints.txt
@@ -113,6 +113,7 @@ set(TARGET_LIBM_ENTRYPOINTS
     libc.src.math.acosf
     libc.src.math.asin
     libc.src.math.asinf
+    libc.src.math.asinhf
     libc.src.math.atanf    
     libc.src.math.atanhf
     libc.src.math.copysign

diff  --git a/libc/docs/math.rst b/libc/docs/math.rst
index 9cf0440038026..09e057402daa3 100644
--- a/libc/docs/math.rst
+++ b/libc/docs/math.rst
@@ -124,7 +124,7 @@ Higher Math Functions
 acos           :green:`XA`
 acosh
 asin           :green:`XA`
-asinh
+asinh          :green:`XA`
 atan           :green:`XA`
 atan2
 atanh          :green:`XA`
@@ -162,6 +162,7 @@ Accuracy of Higher Math Functions
 ============== ================ =============== ======================
 acos           :green:`XA`
 asin           :green:`XA`
+asinh          :green:`XA`
 atan           :green:`XA`
 atanh          :green:`XA`
 cos            :green:`XA`      large
@@ -217,9 +218,11 @@ Performance
 +--------------+-----------+-------------------+-----------+-------------------+-------------------------------------+------------+-------------------------+--------------+---------------+
 | asinf        |        23 |                27 |        62 |                62 | :math:`[-1, 1]`                     | Ryzen 1700 | Ubuntu 22.04 LTS x86_64 | Clang 14.0.0 | FMA           |
 +--------------+-----------+-------------------+-----------+-------------------+-------------------------------------+------------+-------------------------+--------------+---------------+
+| asinhf       |        21 |                39 |        77 |                91 | :math:`[-10, 10]`                   | Ryzen 1700 | Ubuntu 22.04 LTS x86_64 | Clang 14.0.0 | FMA           |
++--------------+-----------+-------------------+-----------+-------------------+-------------------------------------+------------+-------------------------+--------------+---------------+
 | atanf        |        27 |                29 |        79 |                68 | :math:`[-10, 10]`                   | Ryzen 1700 | Ubuntu 22.04 LTS x86_64 | Clang 14.0.0 | FMA           |
 +--------------+-----------+-------------------+-----------+-------------------+-------------------------------------+------------+-------------------------+--------------+---------------+
-| atanhf       |        20 |                66 |        71 |               133 | :math:`[-1, 1]`                     | Ryzen 1700 | Ubuntu 22.04 LTS x86_64 | Clang 14.0.0 | FMA           |
+| atanhf       |        18 |                66 |        68 |               133 | :math:`[-1, 1]`                     | Ryzen 1700 | Ubuntu 22.04 LTS x86_64 | Clang 14.0.0 | FMA           |
 +--------------+-----------+-------------------+-----------+-------------------+-------------------------------------+------------+-------------------------+--------------+---------------+
 | cosf         |        13 |                32 |        53 |                59 | :math:`[0, 2\pi]`                   | Ryzen 1700 | Ubuntu 20.04 LTS x86_64 | Clang 12.0.0 | FMA           |
 +--------------+-----------+-------------------+-----------+-------------------+-------------------------------------+------------+-------------------------+--------------+---------------+

diff  --git a/libc/spec/stdc.td b/libc/spec/stdc.td
index 17cd88ad97c21..7712c1a42623d 100644
--- a/libc/spec/stdc.td
+++ b/libc/spec/stdc.td
@@ -492,6 +492,7 @@ def StdC : StandardSpec<"stdc"> {
           FunctionSpec<"asin", RetValSpec<DoubleType>, [ArgSpec<DoubleType>]>,
           FunctionSpec<"atanf", RetValSpec<FloatType>, [ArgSpec<FloatType>]>,
 
+          FunctionSpec<"asinhf", RetValSpec<FloatType>, [ArgSpec<FloatType>]>,
           FunctionSpec<"atanhf", RetValSpec<FloatType>, [ArgSpec<FloatType>]>,
       ]
   >;

diff  --git a/libc/src/math/CMakeLists.txt b/libc/src/math/CMakeLists.txt
index b38eed31f6489..83ccf30f5f157 100644
--- a/libc/src/math/CMakeLists.txt
+++ b/libc/src/math/CMakeLists.txt
@@ -68,6 +68,7 @@ add_math_entrypoint_object(acosf)
 
 add_math_entrypoint_object(asin)
 add_math_entrypoint_object(asinf)
+add_math_entrypoint_object(asinhf)
 
 add_math_entrypoint_object(atanf)
 

diff  --git a/libc/src/math/asinhf.h b/libc/src/math/asinhf.h
new file mode 100644
index 0000000000000..ff4b32552c1e2
--- /dev/null
+++ b/libc/src/math/asinhf.h
@@ -0,0 +1,18 @@
+//===-- Implementation header for asinhf ------------------------*- C++ -*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SRC_MATH_ASINHF_H
+#define LLVM_LIBC_SRC_MATH_ASINHF_H
+
+namespace __llvm_libc {
+
+float asinhf(float x);
+
+} // namespace __llvm_libc
+
+#endif // LLVM_LIBC_SRC_MATH_ASINHF_H

diff  --git a/libc/src/math/generic/CMakeLists.txt b/libc/src/math/generic/CMakeLists.txt
index dd4b07ed97b4f..8ef3804afec27 100644
--- a/libc/src/math/generic/CMakeLists.txt
+++ b/libc/src/math/generic/CMakeLists.txt
@@ -1296,6 +1296,22 @@ add_entrypoint_object(
     -O3
 )
 
+add_entrypoint_object(
+  asinhf
+  SRCS
+    asinhf.cpp
+  HDRS
+    ../asinhf.h
+  DEPENDS
+    .explogxf
+    libc.src.__support.FPUtil.fp_bits
+    libc.src.__support.FPUtil.multiply_add
+    libc.src.__support.FPUtil.polyeval
+    libc.src.__support.FPUtil.sqrt
+  COMPILE_OPTIONS
+    -O3
+)
+
 add_entrypoint_object(
   atanhf
   SRCS

diff  --git a/libc/src/math/generic/asinhf.cpp b/libc/src/math/generic/asinhf.cpp
new file mode 100644
index 0000000000000..023843d1cc0c8
--- /dev/null
+++ b/libc/src/math/generic/asinhf.cpp
@@ -0,0 +1,103 @@
+//===-- Single-precision asinh function -----------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#include "src/math/asinhf.h"
+#include "src/__support/FPUtil/FPBits.h"
+#include "src/__support/FPUtil/PolyEval.h"
+#include "src/__support/FPUtil/multiply_add.h"
+#include "src/__support/FPUtil/sqrt.h"
+#include "src/math/generic/common_constants.h"
+#include "src/math/generic/explogxf.h"
+
+namespace __llvm_libc {
+
+LLVM_LIBC_FUNCTION(float, asinhf, (float x)) {
+  using FPBits_t = typename fputil::FPBits<float>;
+  FPBits_t xbits(x);
+  uint32_t x_u = xbits.uintval();
+  uint32_t x_abs = x_u & FPBits_t::FloatProp::EXP_MANT_MASK;
+
+  // |x| <= 2^-3
+  if (unlikely(x_abs <= 0x3e80'0000U)) {
+    // |x| <= 2^-26
+    if (unlikely(x_abs <= 0x3280'0000U)) {
+      return unlikely(x_abs == 0) ? x : (x - 0x1.5555555555555p-3 * x * x * x);
+    }
+
+    double x_d = x;
+    double x_sq = x_d * x_d;
+    // Generated by Sollya with:
+    // > P = fpminimax(asinh(x)/x, [|0, 2, 4, 6, 8, 10, 12, 14, 16], [|D...|],
+    //                 [0, 2^-2]);
+    double p = fputil::polyeval(
+        x_sq, 0.0, -0x1.555555555551ep-3, 0x1.3333333325495p-4,
+        -0x1.6db6db5a7622bp-5, 0x1.f1c70f82928c6p-6, -0x1.6e893934266b7p-6,
+        0x1.1c0b41d3fbe78p-6, -0x1.c0f47810b3c4fp-7, 0x1.2c8602690143dp-7);
+    return fputil::multiply_add(x_d, p, x_d);
+  }
+
+  const double SIGN[2] = {1.0, -1.0};
+  double x_sign = SIGN[x_u >> 31];
+  double x_d = x;
+
+  // Helper functions to set results for exceptional cases.
+  auto round_result_slightly_down = [x_sign](float r) -> float {
+    return fputil::multiply_add(static_cast<float>(x_sign), r,
+                                static_cast<float>(x_sign) * (-0x1.0p-24f));
+  };
+  auto round_result_slightly_up = [x_sign](float r) -> float {
+    return fputil::multiply_add(static_cast<float>(x_sign), r,
+                                static_cast<float>(x_sign) * 0x1.0p-24f);
+  };
+
+  if (unlikely(x_abs >= 0x4bdd'65a5U)) {
+    if (unlikely(x_abs >= 0x7f80'0000U)) {
+      // x is +-inf or nan
+      return x;
+    }
+
+    // Exceptional cases when x > 2^24.
+    switch (x_abs) {
+    case 0x4bdd65a5: // |x| = 0x1.bacb4ap24f
+      return round_result_slightly_down(0x1.1e0696p4f);
+    case 0x4c803f2c: // |x| = 0x1.007e58p26f
+      return round_result_slightly_down(0x1.2b786cp4f);
+    case 0x4f8ffb03: // |x| = 0x1.1ff606p32f
+      return round_result_slightly_up(0x1.6fdd34p4f);
+    case 0x5c569e88: // |x| = 0x1.ad3d1p57f
+      return round_result_slightly_up(0x1.45c146p5f);
+    case 0x5e68984e: // |x| = 0x1.d1309cp61f
+      return round_result_slightly_up(0x1.5c9442p5f);
+    case 0x655890d3: // |x| = 0x1.b121a6p75f
+      return round_result_slightly_down(0x1.a9a3f2p5f);
+    case 0x65de7ca6: // |x| = 0x1.bcf94cp76f
+      return round_result_slightly_up(0x1.af66cp5f);
+    case 0x6eb1a8ec: // |x| = 0x1.6351d8p94f
+      return round_result_slightly_down(0x1.08b512p6f);
+    case 0x7997f30a: // |x| = 0x1.2fe614p116f
+      return round_result_slightly_up(0x1.451436p6f);
+    }
+  } else {
+    // Exceptional cases when x < 2^24.
+    if (unlikely(x_abs == 0x45abaf26)) {
+      // |x| = 0x1.575e4cp12f
+      return round_result_slightly_down(0x1.29becap3f);
+    }
+    if (unlikely(x_abs == 0x49d29048)) {
+      // |x| = 0x1.a5209p20f
+      return round_result_slightly_down(0x1.e1b92p3f);
+    }
+  }
+
+  // asinh(x) = log(x + sqrt(x^2 + 1))
+  return x_sign *
+         log_eval(fputil::multiply_add(
+             x_d, x_sign, fputil::sqrt(fputil::multiply_add(x_d, x_d, 1.0))));
+}
+
+} // namespace __llvm_libc

diff  --git a/libc/src/math/generic/explogxf.h b/libc/src/math/generic/explogxf.h
index cce96172cf0ea..38a63ff3216d9 100644
--- a/libc/src/math/generic/explogxf.h
+++ b/libc/src/math/generic/explogxf.h
@@ -9,6 +9,7 @@
 #ifndef LLVM_LIBC_SRC_MATH_GENERIC_EXPLOGXF_H
 #define LLVM_LIBC_SRC_MATH_GENERIC_EXPLOGXF_H
 
+#include "common_constants.h"
 #include "math_utils.h"
 #include "src/__support/FPUtil/FEnvImpl.h"
 #include "src/__support/FPUtil/FPBits.h"
@@ -288,8 +289,37 @@ LIBC_INLINE static double log2_eval(double x) {
 
 // x should be positive, normal finite value
 LIBC_INLINE static double log_eval(double x) {
-  // ln(x) = log[2,x] * ln(2)
-  return log2_eval(x) * 0x1.62e42fefa39efp-1;
+  // For x = 2^ex * (1 + mx)
+  //   log(x) = ex * log(2) + log(1 + mx)
+  using FPB = fputil::FPBits<double>;
+  FPB bs(x);
+
+  double ex = static_cast<double>(bs.get_exponent());
+
+  // p1 is the leading 7 bits of mx, i.e.
+  // p1 * 2^(-7) <= m_x < (p1 + 1) * 2^(-7).
+  int p1 = (bs.get_mantissa() >> (FPB::FloatProp::MANTISSA_WIDTH - 7));
+
+  // Set bs to (1 + (mx - p1*2^(-7))
+  bs.bits &= FPB::FloatProp::MANTISSA_MASK >> 7;
+  bs.set_unbiased_exponent(FPB::FloatProp::EXPONENT_BIAS);
+  // dx = (mx - p1*2^(-7)) / (1 + p1*2^(-7)).
+  double dx = (bs.get_val() - 1.0) * ONE_OVER_F[p1];
+
+  // Minimax polynomial of log(1 + dx) generated by Sollya with:
+  // > P = fpminimax(log(1 + x)/x, 6, [|D...|], [0, 2^-7]);
+  const double COEFFS[6] = {-0x1.ffffffffffffcp-2, 0x1.5555555552ddep-2,
+                            -0x1.ffffffefe562dp-3, 0x1.9999817d3a50fp-3,
+                            -0x1.554317b3f67a5p-3, 0x1.1dc5c45e09c18p-3};
+  double dx2 = dx * dx;
+  double c1 = fputil::multiply_add(dx, COEFFS[1], COEFFS[0]);
+  double c2 = fputil::multiply_add(dx, COEFFS[3], COEFFS[2]);
+  double c3 = fputil::multiply_add(dx, COEFFS[5], COEFFS[4]);
+
+  double p = fputil::polyeval(dx2, dx, c1, c2, c3);
+  double result =
+      fputil::multiply_add(ex, /*log(2)*/ 0x1.62e42fefa39efp-1, LOG_F[p1] + p);
+  return result;
 }
 
 } // namespace __llvm_libc

diff  --git a/libc/test/src/math/CMakeLists.txt b/libc/test/src/math/CMakeLists.txt
index 4e78328384b14..6113d54eb7188 100644
--- a/libc/test/src/math/CMakeLists.txt
+++ b/libc/test/src/math/CMakeLists.txt
@@ -1471,6 +1471,20 @@ add_fp_unittest(
     libc.src.__support.FPUtil.fp_bits
 )
 
+add_fp_unittest(
+  asinhf_test
+  NEED_MPFR
+  SUITE
+    libc_math_unittests
+  SRCS
+    asinhf_test.cpp
+  DEPENDS
+    libc.include.errno
+    libc.src.errno.errno
+    libc.src.math.asinhf
+    libc.src.__support.FPUtil.fp_bits
+)
+
 add_fp_unittest(
   asinf_test
   NEED_MPFR

diff  --git a/libc/test/src/math/asinhf_test.cpp b/libc/test/src/math/asinhf_test.cpp
new file mode 100644
index 0000000000000..30f82c5f0c859
--- /dev/null
+++ b/libc/test/src/math/asinhf_test.cpp
@@ -0,0 +1,81 @@
+//===-- Unittests for asinhf ----------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#include "src/__support/FPUtil/FPBits.h"
+#include "src/math/asinhf.h"
+#include "utils/MPFRWrapper/MPFRUtils.h"
+#include "utils/UnitTest/FPMatcher.h"
+#include "utils/UnitTest/Test.h"
+#include <math.h>
+
+#include <errno.h>
+#include <stdint.h>
+
+using FPBits_t = __llvm_libc::fputil::FPBits<float>;
+
+namespace mpfr = __llvm_libc::testing::mpfr;
+
+DECLARE_SPECIAL_CONSTANTS(float)
+
+TEST(LlvmLibcAsinhfTest, SpecialNumbers) {
+  errno = 0;
+
+  EXPECT_FP_EQ(aNaN, __llvm_libc::asinhf(aNaN));
+  EXPECT_MATH_ERRNO(0);
+
+  EXPECT_FP_EQ(0.0f, __llvm_libc::asinhf(0.0f));
+  EXPECT_MATH_ERRNO(0);
+
+  EXPECT_FP_EQ(-0.0f, __llvm_libc::asinhf(-0.0f));
+  EXPECT_MATH_ERRNO(0);
+
+  EXPECT_FP_EQ(inf, __llvm_libc::asinhf(inf));
+  EXPECT_MATH_ERRNO(0);
+
+  EXPECT_FP_EQ(neg_inf, __llvm_libc::asinhf(neg_inf));
+  EXPECT_MATH_ERRNO(0);
+}
+
+TEST(LlvmLibcAsinhfTest, InFloatRange) {
+  constexpr uint32_t COUNT = 1000000;
+  constexpr uint32_t STEP = UINT32_MAX / COUNT;
+  for (uint32_t i = 0, v = 0; i <= COUNT; ++i, v += STEP) {
+    float x = float(FPBits_t(v));
+    if (isnan(x) || isinf(x))
+      continue;
+    ASSERT_MPFR_MATCH_ALL_ROUNDING(mpfr::Operation::Asinh, x,
+                                   __llvm_libc::asinhf(x), 0.5);
+    ASSERT_MPFR_MATCH_ALL_ROUNDING(mpfr::Operation::Asinh, -x,
+                                   __llvm_libc::asinhf(-x), 0.5);
+  }
+}
+
+TEST(LlvmLibcAsinhfTest, SpecificBitPatterns) {
+  constexpr int N = 11;
+  constexpr uint32_t INPUTS[N] = {
+      0x45abaf26, // |x| = 0x1.575e4cp12f
+      0x49d29048, // |x| = 0x1.a5209p20f
+      0x4bdd65a5, // |x| = 0x1.bacb4ap24f
+      0x4c803f2c, // |x| = 0x1.007e58p26f
+      0x4f8ffb03, // |x| = 0x1.1ff606p32f
+      0x5c569e88, // |x| = 0x1.ad3d1p57f
+      0x5e68984e, // |x| = 0x1.d1309cp61f
+      0x655890d3, // |x| = 0x1.b121a6p75f
+      0x65de7ca6, // |x| = 0x1.bcf94cp76f
+      0x6eb1a8ec, // |x| = 0x1.6351d8p94f
+      0x7997f30a, // |x| = 0x1.2fe614p116f
+  };
+
+  for (int i = 0; i < N; ++i) {
+    float x = float(FPBits_t(INPUTS[i]));
+    EXPECT_MPFR_MATCH_ALL_ROUNDING(mpfr::Operation::Asinh, x,
+                                   __llvm_libc::asinhf(x), 0.5);
+    EXPECT_MPFR_MATCH_ALL_ROUNDING(mpfr::Operation::Asinh, -x,
+                                   __llvm_libc::asinhf(-x), 0.5);
+  }
+}

diff  --git a/libc/test/src/math/exhaustive/CMakeLists.txt b/libc/test/src/math/exhaustive/CMakeLists.txt
index 4d812bfa4e774..a660e3b2c5c55 100644
--- a/libc/test/src/math/exhaustive/CMakeLists.txt
+++ b/libc/test/src/math/exhaustive/CMakeLists.txt
@@ -308,6 +308,23 @@ add_fp_unittest(
     -lpthread
 )
 
+add_fp_unittest(
+  asinhf_test
+  NO_RUN_POSTBUILD
+  NEED_MPFR
+  SUITE
+    libc_math_exhaustive_tests
+  SRCS
+    asinhf_test.cpp
+  DEPENDS
+    .exhaustive_test
+    libc.include.math
+    libc.src.math.asinhf
+    libc.src.__support.FPUtil.fp_bits
+  LINK_LIBRARIES
+    -lpthread
+)
+
 add_fp_unittest(
   atanhf_test
   NO_RUN_POSTBUILD

diff  --git a/libc/test/src/math/exhaustive/asinhf_test.cpp b/libc/test/src/math/exhaustive/asinhf_test.cpp
new file mode 100644
index 0000000000000..d43dd3d6023ec
--- /dev/null
+++ b/libc/test/src/math/exhaustive/asinhf_test.cpp
@@ -0,0 +1,76 @@
+//===-- Exhaustive test for asinhf ----------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#include "exhaustive_test.h"
+#include "src/__support/FPUtil/FPBits.h"
+#include "src/math/asinhf.h"
+#include "utils/MPFRWrapper/MPFRUtils.h"
+
+#include <thread>
+
+using FPBits = __llvm_libc::fputil::FPBits<float>;
+
+namespace mpfr = __llvm_libc::testing::mpfr;
+
+struct LlvmLibcAsinhfExhaustiveTest : public LlvmLibcExhaustiveTest<uint32_t> {
+  bool check(uint32_t start, uint32_t stop,
+             mpfr::RoundingMode rounding) override {
+    mpfr::ForceRoundingMode r(rounding);
+    uint32_t bits = start;
+    bool result = true;
+    do {
+      FPBits xbits(bits);
+      float x = float(xbits);
+      result &= EXPECT_MPFR_MATCH(mpfr::Operation::Asinh, x,
+                                  __llvm_libc::asinhf(x), 0.5, rounding);
+    } while (bits++ < stop);
+    return result;
+  }
+};
+
+static const int NUM_THREADS = std::thread::hardware_concurrency();
+
+// Range: [0, Inf];
+static const uint32_t POS_START = 0x0000'0000U;
+static const uint32_t POS_STOP = 0x7f80'0000U;
+
+TEST_F(LlvmLibcAsinhfExhaustiveTest, PostiveRangeRoundNearestTieToEven) {
+  test_full_range(POS_START, POS_STOP, mpfr::RoundingMode::Nearest);
+}
+
+TEST_F(LlvmLibcAsinhfExhaustiveTest, PostiveRangeRoundUp) {
+  test_full_range(POS_START, POS_STOP, mpfr::RoundingMode::Upward);
+}
+
+TEST_F(LlvmLibcAsinhfExhaustiveTest, PostiveRangeRoundDown) {
+  test_full_range(POS_START, POS_STOP, mpfr::RoundingMode::Downward);
+}
+
+TEST_F(LlvmLibcAsinhfExhaustiveTest, PostiveRangeRoundTowardZero) {
+  test_full_range(POS_START, POS_STOP, mpfr::RoundingMode::TowardZero);
+}
+
+// Range: [-1.0, 0];
+static const uint32_t NEG_START = 0x8000'0000U;
+static const uint32_t NEG_STOP = 0xff80'0000U;
+
+TEST_F(LlvmLibcAsinhfExhaustiveTest, NegativeRangeRoundNearestTieToEven) {
+  test_full_range(NEG_START, NEG_STOP, mpfr::RoundingMode::Nearest);
+}
+
+TEST_F(LlvmLibcAsinhfExhaustiveTest, NegativeRangeRoundUp) {
+  test_full_range(NEG_START, NEG_STOP, mpfr::RoundingMode::Upward);
+}
+
+TEST_F(LlvmLibcAsinhfExhaustiveTest, NegativeRangeRoundDown) {
+  test_full_range(NEG_START, NEG_STOP, mpfr::RoundingMode::Downward);
+}
+
+TEST_F(LlvmLibcAsinhfExhaustiveTest, NegativeRangeRoundTowardZero) {
+  test_full_range(NEG_START, NEG_STOP, mpfr::RoundingMode::TowardZero);
+}

diff  --git a/libc/test/src/math/exhaustive/exhaustive_test.cpp b/libc/test/src/math/exhaustive/exhaustive_test.cpp
index 5ee21fe6bd661..a6d8929560c9e 100644
--- a/libc/test/src/math/exhaustive/exhaustive_test.cpp
+++ b/libc/test/src/math/exhaustive/exhaustive_test.cpp
@@ -41,10 +41,11 @@ void LlvmLibcExhaustiveTest<T, FloatType>::test_full_range(
           range_begin = current_value;
           if (stop >= increment && stop - increment >= current_value) {
             range_end = current_value + increment;
-          } else
+          } else {
             range_end = stop;
+          }
           current_value = range_end;
-          int pc = 100.0 * double(range_end - start) / double(stop - start);
+          int pc = 100.0 * (range_end - start) / (stop - start);
           if (current_percent != pc) {
             new_percent = pc;
             current_percent = pc;
@@ -54,7 +55,6 @@ void LlvmLibcExhaustiveTest<T, FloatType>::test_full_range(
           std::stringstream msg;
           msg << new_percent << "% is in process     \r";
           std::cout << msg.str() << std::flush;
-          ;
         }
 
         bool check_passed = check(range_begin, range_end, rounding);
@@ -84,6 +84,7 @@ void LlvmLibcExhaustiveTest<T, FloatType>::test_full_range(
   }
   std::cout << std::endl;
   std::cout << "Test " << ((failed > 0) ? "FAILED" : "PASSED") << std::endl;
+  ASSERT_EQ(failed.load(), uint64_t(0));
 }
 
 template void

diff  --git a/libc/utils/MPFRWrapper/MPFRUtils.cpp b/libc/utils/MPFRWrapper/MPFRUtils.cpp
index 0b63fdd335f0c..f1f0fd9a66e0e 100644
--- a/libc/utils/MPFRWrapper/MPFRUtils.cpp
+++ b/libc/utils/MPFRWrapper/MPFRUtils.cpp
@@ -196,6 +196,11 @@ class MPFRNumber {
     return result;
   }
 
+  MPFRNumber asinh() const {
+    MPFRNumber result(*this);
+    mpfr_asinh(result.value, value, mpfr_rounding);
+    return result;
+  }
   MPFRNumber atan() const {
     MPFRNumber result(*this);
     mpfr_atan(result.value, value, mpfr_rounding);
@@ -542,6 +547,8 @@ unary_operation(Operation op, InputType input, unsigned int precision,
     return mpfrInput.acos();
   case Operation::Asin:
     return mpfrInput.asin();
+  case Operation::Asinh:
+    return mpfrInput.asinh();
   case Operation::Atan:
     return mpfrInput.atan();
   case Operation::Atanh:

diff  --git a/libc/utils/MPFRWrapper/MPFRUtils.h b/libc/utils/MPFRWrapper/MPFRUtils.h
index 3b9e24ae92660..35e1606b1cce6 100644
--- a/libc/utils/MPFRWrapper/MPFRUtils.h
+++ b/libc/utils/MPFRWrapper/MPFRUtils.h
@@ -27,6 +27,7 @@ enum class Operation : int {
   Abs,
   Acos,
   Asin,
+  Asinh,
   Atan,
   Atanh,
   Ceil,


        


More information about the libc-commits mailing list