[libc-commits] [libc] 6c1fc7e - [libc][math] Added atanhf function.

Kirill Okhotnikov via libc-commits libc-commits at lists.llvm.org
Tue Aug 30 13:40:46 PDT 2022


Author: Kirill Okhotnikov
Date: 2022-08-30T22:39:54+02:00
New Revision: 6c1fc7e43033cbab5b16eeb2dee651078d06009d

URL: https://github.com/llvm/llvm-project/commit/6c1fc7e43033cbab5b16eeb2dee651078d06009d
DIFF: https://github.com/llvm/llvm-project/commit/6c1fc7e43033cbab5b16eeb2dee651078d06009d.diff

LOG: [libc][math] Added atanhf function.

Performance by core-math (core-math/glibc 2.31/current llvm-14):
10.845/43.174/13.467

The review is done on top of D132809.

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

Added: 
    libc/src/math/atanhf.h
    libc/src/math/generic/atanhf.cpp
    libc/test/src/math/atanhf_test.cpp
    libc/test/src/math/exhaustive/atanhf_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/spec/stdc.td
    libc/src/math/CMakeLists.txt
    libc/src/math/generic/CMakeLists.txt
    libc/test/src/math/CMakeLists.txt
    libc/test/src/math/exhaustive/CMakeLists.txt
    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 934d8a3bccad0..08c3a2e098ec8 100644
--- a/libc/config/darwin/arm/entrypoints.txt
+++ b/libc/config/darwin/arm/entrypoints.txt
@@ -105,6 +105,7 @@ set(TARGET_LIBM_ENTRYPOINTS
     libc.src.fenv.feupdateenv
 
     # math.h entrypoints
+    libc.src.math.atanhf
     libc.src.math.copysign
     libc.src.math.copysignf
     libc.src.math.copysignl

diff  --git a/libc/config/linux/aarch64/entrypoints.txt b/libc/config/linux/aarch64/entrypoints.txt
index fe56176cdd922..3dd0a16cb4188 100644
--- a/libc/config/linux/aarch64/entrypoints.txt
+++ b/libc/config/linux/aarch64/entrypoints.txt
@@ -149,6 +149,7 @@ set(TARGET_LIBM_ENTRYPOINTS
     libc.src.fenv.feupdateenv
 
     # math.h entrypoints
+    libc.src.math.atanhf    
     libc.src.math.copysign
     libc.src.math.copysignf
     libc.src.math.copysignl

diff  --git a/libc/config/linux/x86_64/entrypoints.txt b/libc/config/linux/x86_64/entrypoints.txt
index 431730677b033..0a64077f5e4bb 100644
--- a/libc/config/linux/x86_64/entrypoints.txt
+++ b/libc/config/linux/x86_64/entrypoints.txt
@@ -149,6 +149,7 @@ set(TARGET_LIBM_ENTRYPOINTS
     libc.src.fenv.feupdateenv
 
     # math.h entrypoints
+    libc.src.math.atanhf    
     libc.src.math.copysign
     libc.src.math.copysignf
     libc.src.math.copysignl

diff  --git a/libc/config/windows/entrypoints.txt b/libc/config/windows/entrypoints.txt
index 88cc1649a7866..28298f30155ba 100644
--- a/libc/config/windows/entrypoints.txt
+++ b/libc/config/windows/entrypoints.txt
@@ -106,6 +106,7 @@ set(TARGET_LIBM_ENTRYPOINTS
     libc.src.fenv.feupdateenv
 
     # math.h entrypoints
+    libc.src.math.atanhf
     libc.src.math.copysign
     libc.src.math.copysignf
     libc.src.math.copysignl

diff  --git a/libc/spec/stdc.td b/libc/spec/stdc.td
index b9946be388c56..7224ddee0eed2 100644
--- a/libc/spec/stdc.td
+++ b/libc/spec/stdc.td
@@ -479,6 +479,8 @@ def StdC : StandardSpec<"stdc"> {
           FunctionSpec<"coshf", RetValSpec<FloatType>, [ArgSpec<FloatType>]>,
           FunctionSpec<"sinhf", RetValSpec<FloatType>, [ArgSpec<FloatType>]>,
           FunctionSpec<"tanhf", 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 b801a08516257..46565ed6ee69d 100644
--- a/libc/src/math/CMakeLists.txt
+++ b/libc/src/math/CMakeLists.txt
@@ -66,6 +66,8 @@ add_entrypoint_object(
     -O3
 )
 
+add_math_entrypoint_object(atanhf)
+
 add_math_entrypoint_object(ceil)
 add_math_entrypoint_object(ceilf)
 add_math_entrypoint_object(ceill)

diff  --git a/libc/src/math/atanhf.h b/libc/src/math/atanhf.h
new file mode 100644
index 0000000000000..8e8b4ebf0bfdb
--- /dev/null
+++ b/libc/src/math/atanhf.h
@@ -0,0 +1,18 @@
+//===-- Implementation header for atanhf ------------------------*- 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_ATANHF_H
+#define LLVM_LIBC_SRC_MATH_ATANHF_H
+
+namespace __llvm_libc {
+
+float atanhf(float x);
+
+} // namespace __llvm_libc
+
+#endif // LLVM_LIBC_SRC_MATH_ATANHF_H

diff  --git a/libc/src/math/generic/CMakeLists.txt b/libc/src/math/generic/CMakeLists.txt
index ac7a0e6f0a3d3..fa55a75187489 100644
--- a/libc/src/math/generic/CMakeLists.txt
+++ b/libc/src/math/generic/CMakeLists.txt
@@ -1225,3 +1225,20 @@ add_entrypoint_object(
     -O3
 )
 
+add_entrypoint_object(
+  atanhf
+  SRCS
+    atanhf.cpp
+  HDRS
+    ../atanhf.h
+  DEPENDS
+    .explogxf
+    libc.src.__support.FPUtil.fputil
+    libc.src.__support.FPUtil.multiply_add
+    libc.src.__support.FPUtil.nearest_integer
+    libc.src.__support.FPUtil.polyeval
+    libc.include.math
+  COMPILE_OPTIONS
+    -O3
+)
+

diff  --git a/libc/src/math/generic/atanhf.cpp b/libc/src/math/generic/atanhf.cpp
new file mode 100644
index 0000000000000..0d015101090a0
--- /dev/null
+++ b/libc/src/math/generic/atanhf.cpp
@@ -0,0 +1,57 @@
+//===-- Single-precision atanh 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/atanhf.h"
+#include "src/__support/FPUtil/FPBits.h"
+#include "src/math/generic/explogxf.h"
+
+namespace __llvm_libc {
+
+LLVM_LIBC_FUNCTION(float, atanhf, (float x)) {
+  using FPBits = typename fputil::FPBits<float>;
+  FPBits xbits(x);
+  bool sign = xbits.get_sign();
+  uint32_t x_abs = xbits.uintval() & FPBits::FloatProp::EXP_MANT_MASK;
+
+  // |x| >= 1.0
+  if (unlikely(x_abs >= 0x3F80'0000U)) {
+    if (xbits.is_nan()) {
+      return x + 1.0f;
+    }
+    // |x| == 0
+    if (x_abs == 0x3F80'0000U) {
+      fputil::set_except(FE_DIVBYZERO);
+      return with_errno(FPBits::inf(sign).get_val(), ERANGE);
+    } else {
+      fputil::set_except(FE_INVALID);
+      return with_errno(
+          FPBits::build_nan(1 << (fputil::MantissaWidth<float>::VALUE - 1)),
+          EDOM);
+    }
+  }
+
+  // |x| < ~0.10
+  if (unlikely(x_abs <= 0x3dcc'0000U)) {
+    // |x| <= 2^-26
+    if (unlikely(x_abs <= 0x3280'0000U)) {
+      return unlikely(x_abs == 0) ? x : (x + 0x1.5555555555555p-2 * x * x * x);
+    }
+
+    double xdbl = x;
+    double x2 = xdbl * xdbl;
+    // Pure Taylor series.
+    double pe = fputil::polyeval(x2, 0.0, 0x1.5555555555555p-2,
+                                 0x1.999999999999ap-3, 0x1.2492492492492p-3,
+                                 0x1.c71c71c71c71cp-4, 0x1.745d1745d1746p-4);
+    return fputil::multiply_add(xdbl, pe, xdbl);
+  }
+  double xdbl = x;
+  return 0.5 * log_eval((xdbl + 1.0) / (xdbl - 1.0));
+}
+
+} // namespace __llvm_libc

diff  --git a/libc/test/src/math/CMakeLists.txt b/libc/test/src/math/CMakeLists.txt
index 8d43f16513eae..28a5a3c27f09b 100644
--- a/libc/test/src/math/CMakeLists.txt
+++ b/libc/test/src/math/CMakeLists.txt
@@ -1376,6 +1376,18 @@ add_fp_unittest(
     libc.src.__support.FPUtil.fputil
 )
 
+add_fp_unittest(
+  atanhf_test
+  NEED_MPFR
+  SUITE
+    libc_math_unittests
+  SRCS
+    atanhf_test.cpp
+  DEPENDS
+    libc.src.math.atanhf
+    libc.src.__support.FPUtil.fputil
+)
+
 add_subdirectory(generic)
 add_subdirectory(exhaustive)
 add_subdirectory(
diff erential_testing)

diff  --git a/libc/test/src/math/atanhf_test.cpp b/libc/test/src/math/atanhf_test.cpp
new file mode 100644
index 0000000000000..306fc1962df51
--- /dev/null
+++ b/libc/test/src/math/atanhf_test.cpp
@@ -0,0 +1,108 @@
+//===-- Unittests for atanhf ----------------------------------------------===//
+//
+// 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/atanhf.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 = __llvm_libc::fputil::FPBits<float>;
+
+namespace mpfr = __llvm_libc::testing::mpfr;
+
+DECLARE_SPECIAL_CONSTANTS(float)
+
+TEST(LlvmLibcAtanhfTest, SpecialNumbers) {
+  errno = 0;
+  __llvm_libc::fputil::clear_except(FE_ALL_EXCEPT);
+  EXPECT_FP_EQ(aNaN, __llvm_libc::atanhf(aNaN));
+  EXPECT_FP_EXCEPTION(FE_INVALID);
+  EXPECT_MATH_ERRNO(0);
+
+  __llvm_libc::fputil::clear_except(FE_ALL_EXCEPT);
+  EXPECT_FP_EQ(0.0f, __llvm_libc::atanhf(0.0f));
+  EXPECT_FP_EXCEPTION(0);
+  EXPECT_MATH_ERRNO(0);
+
+  __llvm_libc::fputil::clear_except(FE_ALL_EXCEPT);
+  EXPECT_FP_EQ(-0.0f, __llvm_libc::atanhf(-0.0f));
+  EXPECT_FP_EXCEPTION(0);
+  EXPECT_MATH_ERRNO(0);
+
+  __llvm_libc::fputil::clear_except(FE_ALL_EXCEPT);
+  EXPECT_FP_EQ(inf, __llvm_libc::atanhf(1.0f));
+  EXPECT_FP_EXCEPTION(FE_DIVBYZERO);
+  EXPECT_MATH_ERRNO(ERANGE);
+
+  __llvm_libc::fputil::clear_except(FE_ALL_EXCEPT);
+  EXPECT_FP_EQ(neg_inf, __llvm_libc::atanhf(-1.0f));
+  EXPECT_FP_EXCEPTION(FE_DIVBYZERO);
+  EXPECT_MATH_ERRNO(ERANGE);
+
+  auto bt = FPBits(1.0f);
+  bt.bits += 1;
+
+  __llvm_libc::fputil::clear_except(FE_ALL_EXCEPT);
+  EXPECT_FP_EQ(aNaN, __llvm_libc::atanhf(bt.get_val()));
+  EXPECT_FP_EXCEPTION(FE_INVALID);
+  EXPECT_MATH_ERRNO(EDOM);
+
+  __llvm_libc::fputil::clear_except(FE_ALL_EXCEPT);
+  bt.set_sign(true);
+  EXPECT_FP_EQ(aNaN, __llvm_libc::atanhf(bt.get_val()));
+  EXPECT_FP_EXCEPTION(FE_INVALID);
+  EXPECT_MATH_ERRNO(EDOM);
+
+  __llvm_libc::fputil::clear_except(FE_ALL_EXCEPT);
+  EXPECT_FP_EQ(aNaN, __llvm_libc::atanhf(2.0f));
+  EXPECT_FP_EXCEPTION(FE_INVALID);
+  EXPECT_MATH_ERRNO(EDOM);
+
+  __llvm_libc::fputil::clear_except(FE_ALL_EXCEPT);
+  EXPECT_FP_EQ(aNaN, __llvm_libc::atanhf(-2.0f));
+  EXPECT_FP_EXCEPTION(FE_INVALID);
+  EXPECT_MATH_ERRNO(EDOM);
+
+  __llvm_libc::fputil::clear_except(FE_ALL_EXCEPT);
+  EXPECT_FP_EQ(aNaN, __llvm_libc::atanhf(inf));
+  EXPECT_FP_EXCEPTION(FE_INVALID);
+  EXPECT_MATH_ERRNO(EDOM);
+
+  bt.set_sign(true);
+  EXPECT_FP_EQ(aNaN, __llvm_libc::atanhf(neg_inf));
+  EXPECT_FP_EXCEPTION(FE_INVALID);
+  EXPECT_MATH_ERRNO(EDOM);
+}
+
+TEST(LlvmLibcAtanhfTest, InFloatRange) {
+  constexpr uint32_t COUNT = 1000000;
+  const uint32_t STEP = FPBits(1.0f).uintval() / COUNT;
+  for (uint32_t i = 0, v = 0; i <= COUNT; ++i, v += STEP) {
+    float x = float(FPBits(v));
+    ASSERT_MPFR_MATCH(mpfr::Operation::Atanh, x, __llvm_libc::atanhf(x), 0.5);
+    ASSERT_MPFR_MATCH(mpfr::Operation::Atanh, -x, __llvm_libc::atanhf(-x), 0.5);
+  }
+}
+
+// For small values, atanh(x) is x.
+TEST(LlvmLibcAtanhfTest, SmallValues) {
+  float x = float(FPBits(uint32_t(0x17800000)));
+  float result = __llvm_libc::atanhf(x);
+  EXPECT_MPFR_MATCH(mpfr::Operation::Atanh, x, result, 0.5);
+  EXPECT_FP_EQ(x, result);
+
+  x = float(FPBits(uint32_t(0x00400000)));
+  result = __llvm_libc::atanhf(x);
+  EXPECT_MPFR_MATCH(mpfr::Operation::Atanh, x, result, 0.5);
+  EXPECT_FP_EQ(x, result);
+}

diff  --git a/libc/test/src/math/exhaustive/CMakeLists.txt b/libc/test/src/math/exhaustive/CMakeLists.txt
index ab80e157e3f34..b3d3efa41cb4a 100644
--- a/libc/test/src/math/exhaustive/CMakeLists.txt
+++ b/libc/test/src/math/exhaustive/CMakeLists.txt
@@ -291,3 +291,19 @@ add_fp_unittest(
     -lpthread
 )
 
+add_fp_unittest(
+  atanhf_test
+  NO_RUN_POSTBUILD
+  NEED_MPFR
+  SUITE
+    libc_math_exhaustive_tests
+  SRCS
+    atanhf_test.cpp
+  DEPENDS
+    .exhaustive_test
+    libc.include.math
+    libc.src.math.atanhf
+    libc.src.__support.FPUtil.fputil
+  LINK_LIBRARIES
+    -lpthread
+)

diff  --git a/libc/test/src/math/exhaustive/atanhf_test.cpp b/libc/test/src/math/exhaustive/atanhf_test.cpp
new file mode 100644
index 0000000000000..781db3e68925d
--- /dev/null
+++ b/libc/test/src/math/exhaustive/atanhf_test.cpp
@@ -0,0 +1,76 @@
+//===-- Exhaustive test for atanhf ----------------------------------------===//
+//
+// 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/atanhf.h"
+#include "utils/MPFRWrapper/MPFRUtils.h"
+
+#include <thread>
+
+using FPBits = __llvm_libc::fputil::FPBits<float>;
+
+namespace mpfr = __llvm_libc::testing::mpfr;
+
+struct LlvmLibcAtanhfExhaustiveTest : 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::Atanh, x,
+                                  __llvm_libc::atanhf(x), 0.5, rounding);
+    } while (bits++ < stop);
+    return result;
+  }
+};
+
+static const int NUM_THREADS = std::thread::hardware_concurrency();
+
+// Range: [0, 1.0];
+static const uint32_t POS_START = 0x0000'0000U;
+static const uint32_t POS_STOP = FPBits(1.0f).uintval();
+
+TEST_F(LlvmLibcAtanhfExhaustiveTest, PostiveRangeRoundNearestTieToEven) {
+  test_full_range(POS_START, POS_STOP, mpfr::RoundingMode::Nearest);
+}
+
+TEST_F(LlvmLibcAtanhfExhaustiveTest, PostiveRangeRoundUp) {
+  test_full_range(POS_START, POS_STOP, mpfr::RoundingMode::Upward);
+}
+
+TEST_F(LlvmLibcAtanhfExhaustiveTest, PostiveRangeRoundDown) {
+  test_full_range(POS_START, POS_STOP, mpfr::RoundingMode::Downward);
+}
+
+TEST_F(LlvmLibcAtanhfExhaustiveTest, 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 = FPBits(-1.0f).uintval();
+
+TEST_F(LlvmLibcAtanhfExhaustiveTest, NegativeRangeRoundNearestTieToEven) {
+  test_full_range(NEG_START, NEG_STOP, mpfr::RoundingMode::Nearest);
+}
+
+TEST_F(LlvmLibcAtanhfExhaustiveTest, NegativeRangeRoundUp) {
+  test_full_range(NEG_START, NEG_STOP, mpfr::RoundingMode::Upward);
+}
+
+TEST_F(LlvmLibcAtanhfExhaustiveTest, NegativeRangeRoundDown) {
+  test_full_range(NEG_START, NEG_STOP, mpfr::RoundingMode::Downward);
+}
+
+TEST_F(LlvmLibcAtanhfExhaustiveTest, NegativeRangeRoundTowardZero) {
+  test_full_range(NEG_START, NEG_STOP, mpfr::RoundingMode::TowardZero);
+}

diff  --git a/libc/utils/MPFRWrapper/MPFRUtils.cpp b/libc/utils/MPFRWrapper/MPFRUtils.cpp
index 54c0939929576..720c966fb6a39 100644
--- a/libc/utils/MPFRWrapper/MPFRUtils.cpp
+++ b/libc/utils/MPFRWrapper/MPFRUtils.cpp
@@ -182,6 +182,12 @@ class MPFRNumber {
     return result;
   }
 
+  MPFRNumber atanh() const {
+    MPFRNumber result(*this);
+    mpfr_atanh(result.value, value, mpfr_rounding);
+    return result;
+  }
+
   MPFRNumber ceil() const {
     MPFRNumber result(*this);
     mpfr_ceil(result.value, value);
@@ -500,6 +506,8 @@ unary_operation(Operation op, InputType input, unsigned int precision,
   switch (op) {
   case Operation::Abs:
     return mpfrInput.abs();
+  case Operation::Atanh:
+    return mpfrInput.atanh();
   case Operation::Ceil:
     return mpfrInput.ceil();
   case Operation::Cos:

diff  --git a/libc/utils/MPFRWrapper/MPFRUtils.h b/libc/utils/MPFRWrapper/MPFRUtils.h
index 9d9962e5d8573..4aa3eea6562b4 100644
--- a/libc/utils/MPFRWrapper/MPFRUtils.h
+++ b/libc/utils/MPFRWrapper/MPFRUtils.h
@@ -25,6 +25,7 @@ enum class Operation : int {
   // and output floating point numbers are of the same kind.
   BeginUnaryOperationsSingleOutput,
   Abs,
+  Atanh,
   Ceil,
   Cos,
   Cosh,


        


More information about the libc-commits mailing list