[libc-commits] [libc] [libc][math][c23] Add exp10m1f C23 math function (PR #87992)
via libc-commits
libc-commits at lists.llvm.org
Tue Nov 5 03:15:56 PST 2024
https://github.com/overmighty updated https://github.com/llvm/llvm-project/pull/87992
>From 93559110df56574107990cf6c49200c7c4a6aa7f Mon Sep 17 00:00:00 2001
From: OverMighty <its.overmighty at gmail.com>
Date: Mon, 8 Apr 2024 14:31:11 +0100
Subject: [PATCH 1/6] [libc][math][c23] Add exp10m1f C23 math function
Fixes #86503.
---
libc/config/linux/x86_64/entrypoints.txt | 1 +
libc/docs/math/index.rst | 2 +-
libc/spec/stdc.td | 3 +-
libc/src/math/CMakeLists.txt | 1 +
libc/src/math/exp10m1f.h | 18 ++
libc/src/math/generic/CMakeLists.txt | 24 +-
libc/src/math/generic/exp10m1f.cpp | 215 ++++++++++++++++++
libc/test/src/math/CMakeLists.txt | 16 ++
libc/test/src/math/exhaustive/CMakeLists.txt | 15 ++
.../src/math/exhaustive/exp10m1f_test.cpp | 33 +++
libc/test/src/math/exp10m1f_test.cpp | 85 +++++++
libc/test/src/math/exp2m1f_test.cpp | 21 +-
libc/test/src/math/smoke/CMakeLists.txt | 11 +
libc/test/src/math/smoke/exp10m1f_test.cpp | 56 +++++
libc/test/src/math/smoke/exp2m1f_test.cpp | 10 +-
15 files changed, 491 insertions(+), 20 deletions(-)
create mode 100644 libc/src/math/exp10m1f.h
create mode 100644 libc/src/math/generic/exp10m1f.cpp
create mode 100644 libc/test/src/math/exhaustive/exp10m1f_test.cpp
create mode 100644 libc/test/src/math/exp10m1f_test.cpp
create mode 100644 libc/test/src/math/smoke/exp10m1f_test.cpp
diff --git a/libc/config/linux/x86_64/entrypoints.txt b/libc/config/linux/x86_64/entrypoints.txt
index 3ed77c76709da8..4be003c05bf724 100644
--- a/libc/config/linux/x86_64/entrypoints.txt
+++ b/libc/config/linux/x86_64/entrypoints.txt
@@ -415,6 +415,7 @@ set(TARGET_LIBM_ENTRYPOINTS
libc.src.math.exp
libc.src.math.exp10
libc.src.math.exp10f
+ libc.src.math.exp10m1f
libc.src.math.exp2
libc.src.math.exp2f
libc.src.math.exp2m1f
diff --git a/libc/docs/math/index.rst b/libc/docs/math/index.rst
index a50e054622e1a4..92580cb1592757 100644
--- a/libc/docs/math/index.rst
+++ b/libc/docs/math/index.rst
@@ -292,7 +292,7 @@ Higher Math Functions
+-----------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
| exp10 | |check| | |check| | | |check| | | 7.12.6.2 | F.10.3.2 |
+-----------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
-| exp10m1 | | | | |check| | | 7.12.6.3 | F.10.3.3 |
+| exp10m1 | |check| | | | |check| | | 7.12.6.3 | F.10.3.3 |
+-----------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
| exp2 | |check| | |check| | | |check| | | 7.12.6.4 | F.10.3.4 |
+-----------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
diff --git a/libc/spec/stdc.td b/libc/spec/stdc.td
index d1ebc6ffb5821e..75220ccf464651 100644
--- a/libc/spec/stdc.td
+++ b/libc/spec/stdc.td
@@ -695,6 +695,7 @@ def StdC : StandardSpec<"stdc"> {
FunctionSpec<"exp10f", RetValSpec<FloatType>, [ArgSpec<FloatType>]>,
GuardedFunctionSpec<"exp10f16", RetValSpec<Float16Type>, [ArgSpec<Float16Type>], "LIBC_TYPES_HAS_FLOAT16">,
+ FunctionSpec<"exp10m1f", RetValSpec<FloatType>, [ArgSpec<FloatType>]>,
GuardedFunctionSpec<"exp10m1f16", RetValSpec<Float16Type>, [ArgSpec<Float16Type>], "LIBC_TYPES_HAS_FLOAT16">,
FunctionSpec<"remainder", RetValSpec<DoubleType>, [ArgSpec<DoubleType>, ArgSpec<DoubleType>]>,
@@ -1737,7 +1738,7 @@ def StdC : StandardSpec<"stdc"> {
]
>;
-
+
NamedType StructLconv = NamedType<"struct lconv">;
PtrType StructLconvPtr = PtrType<StructLconv>;
diff --git a/libc/src/math/CMakeLists.txt b/libc/src/math/CMakeLists.txt
index 80c1867d2116f6..88cef320cee76d 100644
--- a/libc/src/math/CMakeLists.txt
+++ b/libc/src/math/CMakeLists.txt
@@ -131,6 +131,7 @@ add_math_entrypoint_object(exp10)
add_math_entrypoint_object(exp10f)
add_math_entrypoint_object(exp10f16)
+add_math_entrypoint_object(exp10m1f)
add_math_entrypoint_object(exp10m1f16)
add_math_entrypoint_object(expm1)
diff --git a/libc/src/math/exp10m1f.h b/libc/src/math/exp10m1f.h
new file mode 100644
index 00000000000000..e8a242eaba2cbd
--- /dev/null
+++ b/libc/src/math/exp10m1f.h
@@ -0,0 +1,18 @@
+//===-- Implementation header for exp10m1f ----------------------*- 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_EXP10M1F_H
+#define LLVM_LIBC_SRC_MATH_EXP10M1F_H
+
+namespace LIBC_NAMESPACE {
+
+float exp10m1f(float x);
+
+} // namespace LIBC_NAMESPACE
+
+#endif // LLVM_LIBC_SRC_MATH_EXP10M1F_H
diff --git a/libc/src/math/generic/CMakeLists.txt b/libc/src/math/generic/CMakeLists.txt
index ca27759d3212f2..93780a79a8e2f4 100644
--- a/libc/src/math/generic/CMakeLists.txt
+++ b/libc/src/math/generic/CMakeLists.txt
@@ -359,7 +359,7 @@ add_header_library(
libc.src.__support.FPUtil.fp_bits
libc.src.__support.FPUtil.polyeval
libc.src.__support.FPUtil.nearest_integer
- libc.src.__support.common
+ libc.src.__support.common
)
add_header_library(
@@ -1569,6 +1569,7 @@ add_entrypoint_object(
.explogxf
libc.src.errno.errno
libc.src.__support.common
+ libc.src.__support.FPUtil.except_value_utils
libc.src.__support.FPUtil.fenv_impl
libc.src.__support.FPUtil.fp_bits
libc.src.__support.FPUtil.multiply_add
@@ -1686,6 +1687,27 @@ add_entrypoint_object(
-O3
)
+add_entrypoint_object(
+ exp10m1f
+ SRCS
+ exp10m1f.cpp
+ HDRS
+ ../exp10m1f.h
+ DEPENDS
+ .explogxf
+ libc.src.errno.errno
+ libc.src.__support.common
+ libc.src.__support.FPUtil.except_value_utils
+ libc.src.__support.FPUtil.fenv_impl
+ libc.src.__support.FPUtil.fp_bits
+ libc.src.__support.FPUtil.multiply_add
+ libc.src.__support.FPUtil.polyeval
+ libc.src.__support.FPUtil.rounding_mode
+ libc.src.__support.macros.optimization
+ COMPILE_OPTIONS
+ -O3
+)
+
add_entrypoint_object(
exp10m1f16
SRCS
diff --git a/libc/src/math/generic/exp10m1f.cpp b/libc/src/math/generic/exp10m1f.cpp
new file mode 100644
index 00000000000000..50d4c50fb869ca
--- /dev/null
+++ b/libc/src/math/generic/exp10m1f.cpp
@@ -0,0 +1,215 @@
+//===-- Implementation of exp10m1f 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/exp10m1f.h"
+#include "src/__support/FPUtil/FEnvImpl.h"
+#include "src/__support/FPUtil/FPBits.h"
+#include "src/__support/FPUtil/PolyEval.h"
+#include "src/__support/FPUtil/except_value_utils.h"
+#include "src/__support/FPUtil/multiply_add.h"
+#include "src/__support/FPUtil/rounding_mode.h"
+#include "src/__support/common.h"
+#include "src/__support/macros/optimization.h"
+#include "src/errno/libc_errno.h"
+
+#include "explogxf.h"
+
+namespace LIBC_NAMESPACE {
+
+static constexpr size_t N_EXCEPTS_LO = 11;
+
+static constexpr fputil::ExceptValues<float, N_EXCEPTS_LO> EXP10M1F_EXCEPTS_LO =
+ {{
+ // x = 0x1.0fe54ep-11, exp10m1f(x) = 0x1.3937eep-10 (RZ)
+ {0x3a07'f2a7U, 0x3a9c'9bf7U, 1U, 0U, 1U},
+ // x = 0x1.80e6eap-11, exp10m1f(x) = 0x1.bb8272p-10 (RZ)
+ {0x3a40'7375U, 0x3add'c139U, 1U, 0U, 1U},
+ // x = -0x1.2a33bcp-51, exp10m1f(x) = -0x1.57515ep-50 (RZ)
+ {0xa615'19deU, 0xa6ab'a8afU, 0U, 1U, 0U},
+ // x = -0x0p+0, exp10m1f(x) = -0x0p+0 (RZ)
+ {0x8000'0000U, 0x8000'0000U, 0U, 0U, 0U},
+ // x = -0x1.b59e08p-31, exp10m1f(x) = -0x1.f7d356p-30 (RZ)
+ {0xb05a'cf04U, 0xb0fb'e9abU, 0U, 1U, 1U},
+ // x = -0x1.bf342p-12, exp10m1f(x) = -0x1.014e02p-10 (RZ)
+ {0xb9df'9a10U, 0xba80'a701U, 0U, 1U, 0U},
+ // x = -0x1.6207fp-11, exp10m1f(x) = -0x1.9746cap-10 (RZ)
+ {0xba31'03f8U, 0xbacb'a365U, 0U, 1U, 1U},
+ // x = -0x1.bd0c66p-11, exp10m1f(x) = -0x1.ffe168p-10 (RZ)
+ {0xba5e'8633U, 0xbaff'f0b4U, 0U, 1U, 1U},
+ // x = -0x1.ffd84cp-10, exp10m1f(x) = -0x1.25faf2p-8 (RZ)
+ {0xbaff'ec26U, 0xbb92'fd79U, 0U, 1U, 0U},
+ // x = -0x1.a74172p-9, exp10m1f(x) = -0x1.e57be2p-8 (RZ)
+ {0xbb53'a0b9U, 0xbbf2'bdf1U, 0U, 1U, 1U},
+ // x = -0x1.cb694cp-9, exp10m1f(x) = -0x1.0764e4p-7 (RZ)
+ {0xbb65'b4a6U, 0xbc03'b272U, 0U, 1U, 0U},
+ }};
+
+static constexpr size_t N_EXCEPTS_HI = 19;
+
+static constexpr fputil::ExceptValues<float, N_EXCEPTS_HI> EXP10M1F_EXCEPTS_HI =
+ {{
+ // (input, RZ output, RU offset, RD offset, RN offset)
+ // x = 0x1.8d31eep-8, exp10m1f(x) = 0x1.cc7e4cp-7 (RZ)
+ {0x3bc6'98f7U, 0x3c66'3f26U, 1U, 0U, 1U},
+ // x = 0x1.915fcep-8, exp10m1f(x) = 0x1.d15f72p-7 (RZ)
+ {0x3bc8'afe7U, 0x3c68'afb9U, 1U, 0U, 0U},
+ // x = 0x1.bcf982p-8, exp10m1f(x) = 0x1.022928p-6 (RZ)
+ {0x3bde'7cc1U, 0x3c81'1494U, 1U, 0U, 1U},
+ // x = 0x1.99ff0ap-7, exp10m1f(x) = 0x1.dee416p-6 (RZ)
+ {0x3c4c'ff85U, 0x3cef'720bU, 1U, 0U, 0U},
+ // x = 0x1.75ea14p-6, exp10m1f(x) = 0x1.b9ff16p-5 (RZ)
+ {0x3cba'f50aU, 0x3d5c'ff8bU, 1U, 0U, 0U},
+ // x = 0x1.f81b64p-6, exp10m1f(x) = 0x1.2cb6bcp-4 (RZ)
+ {0x3cfc'0db2U, 0x3d96'5b5eU, 1U, 0U, 0U},
+ // x = 0x1.fafecp+3, exp10m1f(x) = 0x1.8c880ap+52 (RZ)
+ {0x417d'7f60U, 0x59c6'4405U, 1U, 0U, 0U},
+ // x = -0x1.3bf094p-8, exp10m1f(x) = -0x1.69ba4ap-7 (RZ)
+ {0xbb9d'f84aU, 0xbc34'dd25U, 0U, 1U, 0U},
+ // x = -0x1.4558bcp-8, exp10m1f(x) = -0x1.746fb8p-7 (RZ)
+ {0xbba2'ac5eU, 0xbc3a'37dcU, 0U, 1U, 1U},
+ // x = -0x1.4bb43p-8, exp10m1f(x) = -0x1.7babe4p-7 (RZ)
+ {0xbba5'da18U, 0xbc3d'd5f2U, 0U, 1U, 1U},
+ // x = -0x1.776cc8p-8, exp10m1f(x) = -0x1.ad62c4p-7 (RZ)
+ {0xbbbb'b664U, 0xbc56'b162U, 0U, 1U, 0U},
+ // x = -0x1.f024cp-8, exp10m1f(x) = -0x1.1b20d6p-6 (RZ)
+ {0xbbf8'1260U, 0xbc8d'906bU, 0U, 1U, 1U},
+ // x = -0x1.f510eep-8, exp10m1f(x) = -0x1.1de9aap-6 (RZ)
+ {0xbbfa'8877U, 0xbc8e'f4d5U, 0U, 1U, 0U},
+ // x = -0x1.0b43c4p-7, exp10m1f(x) = -0x1.30d418p-6 (RZ)
+ {0xbc05'a1e2U, 0xbc98'6a0cU, 0U, 1U, 0U},
+ // x = -0x1.245ee4p-7, exp10m1f(x) = -0x1.4d2b86p-6 (RZ)
+ {0xbc12'2f72U, 0xbca6'95c3U, 0U, 1U, 0U},
+ // x = -0x1.f9f2dap-7, exp10m1f(x) = -0x1.1e2186p-5 (RZ)
+ {0xbc7c'f96dU, 0xbd0f'10c3U, 0U, 1U, 0U},
+ // x = -0x1.08e42p-6, exp10m1f(x) = -0x1.2b5c4p-5 (RZ)
+ {0xbc84'7210U, 0xbd15'ae20U, 0U, 1U, 1U},
+ // x = -0x1.0cdc44p-5, exp10m1f(x) = -0x1.2a2152p-4 (RZ)
+ {0xbd06'6e22U, 0xbd95'10a9U, 0U, 1U, 1U},
+ // x = -0x1.ca4322p-5, exp10m1f(x) = -0x1.ef073p-4 (RZ)
+ {0xbd65'2191U, 0xbdf7'8398U, 0U, 1U, 1U},
+ }};
+
+LLVM_LIBC_FUNCTION(float, exp10m1f, (float x)) {
+ using FPBits = fputil::FPBits<float>;
+ FPBits xbits(x);
+
+ uint32_t x_u = xbits.uintval();
+ uint32_t x_abs = x_u & 0x7fff'ffffU;
+
+ // When x >= log10(2^128), or x is nan
+ if (LIBC_UNLIKELY(xbits.is_pos() && x_u >= 0x421a'209bU)) {
+ if (xbits.is_finite()) {
+ int rounding = fputil::quick_get_round();
+ if (rounding == FE_DOWNWARD || rounding == FE_TOWARDZERO)
+ return FPBits::max_normal().get_val();
+
+ fputil::set_errno_if_required(ERANGE);
+ fputil::raise_except_if_required(FE_OVERFLOW);
+ }
+
+ // x >= log10(2^128) and 10^x - 1 rounds to +inf, or x is +inf or nan
+ return x + FPBits::inf().get_val();
+ }
+
+ // When |x| <= log10(2) * 2^(-6)
+ if (LIBC_UNLIKELY(x_abs <= 0x3b9a'209bU)) {
+ if (auto r = EXP10M1F_EXCEPTS_LO.lookup(x_u); LIBC_UNLIKELY(r.has_value()))
+ return r.value();
+
+ double dx = x;
+ double dx_sq = dx * dx;
+ double c0 = dx * Exp10Base::COEFFS[0];
+ double c1 =
+ fputil::multiply_add(dx, Exp10Base::COEFFS[2], Exp10Base::COEFFS[1]);
+ double c2 =
+ fputil::multiply_add(dx, Exp10Base::COEFFS[4], Exp10Base::COEFFS[3]);
+ // 10^dx - 1 ~ (1 + COEFFS[0] * dx + ... + COEFFS[4] * dx^5) - 1
+ // = COEFFS[0] * dx + ... + COEFFS[4] * dx^5
+ return static_cast<float>(fputil::polyeval(dx_sq, c0, c1, c2));
+ }
+
+ // When x <= log10(2^-25), or x is nan
+ if (LIBC_UNLIKELY(x <= -0x1.e1a5e2p+2f)) {
+ // exp10m1(-inf) = -1
+ if (xbits.is_inf())
+ return -1.0f;
+ // exp10m1(nan) = nan
+ if (xbits.is_nan())
+ return x;
+
+ int rounding = fputil::quick_get_round();
+ if (rounding == FE_UPWARD || rounding == FE_TOWARDZERO ||
+ (rounding == FE_TONEAREST && x == -0x1.e1a5e2p+2f))
+ return -0x1.ffff'fep-1f; // -1.0f + 0x1.0p-24f
+
+ fputil::set_errno_if_required(ERANGE);
+ fputil::raise_except_if_required(FE_UNDERFLOW);
+ return -1.0f;
+ }
+
+ // Exact outputs when x = 1, 2, ..., 10.
+ // Quick check mask: 0x800f'ffffU = ~(bits of 1.0f | ... | bits of 10.0f)
+ if (LIBC_UNLIKELY((x_u & 0x800f'ffffU) == 0)) {
+ switch (x_u) {
+ case 0x3f800000U: // x = 1.0f
+ return 9.0f;
+ case 0x40000000U: // x = 2.0f
+ return 99.0f;
+ case 0x40400000U: // x = 3.0f
+ return 999.0f;
+ case 0x40800000U: // x = 4.0f
+ return 9'999.0f;
+ case 0x40a00000U: // x = 5.0f
+ return 99'999.0f;
+ case 0x40c00000U: // x = 6.0f
+ return 999'999.0f;
+ case 0x40e00000U: // x = 7.0f
+ return 9'999'999.0f;
+ case 0x41000000U: { // x = 8.0f
+ int rounding = fputil::quick_get_round();
+ if (rounding == FE_UPWARD || rounding == FE_TONEAREST)
+ return 100'000'000.0f;
+ return 99'999'992.0f;
+ }
+ case 0x41100000U: { // x = 9.0f
+ int rounding = fputil::quick_get_round();
+ if (rounding == FE_UPWARD || rounding == FE_TONEAREST)
+ return 1'000'000'000.0f;
+ return 999'999'936.0f;
+ }
+ case 0x41200000U: { // x = 10.0f
+ int rounding = fputil::quick_get_round();
+ if (rounding == FE_UPWARD || rounding == FE_TONEAREST)
+ return 10'000'000'000.0f;
+ return 9'999'998'976.0f;
+ }
+ }
+ }
+
+ if (auto r = EXP10M1F_EXCEPTS_HI.lookup(x_u); LIBC_UNLIKELY(r.has_value()))
+ return r.value();
+
+ // Range reduction: 10^x = 2^(mid + hi) * 10^lo
+ // rr = (2^(mid + hi), lo)
+ auto rr = exp_b_range_reduc<Exp10Base>(x);
+
+ // The low part is approximated by a degree-5 minimax polynomial.
+ // 10^lo ~ 1 + COEFFS[0] * lo + ... + COEFFS[4] * lo^5
+ double lo_sq = rr.lo * rr.lo;
+ double c0 = fputil::multiply_add(rr.lo, Exp10Base::COEFFS[0], 1.0);
+ double c1 =
+ fputil::multiply_add(rr.lo, Exp10Base::COEFFS[2], Exp10Base::COEFFS[1]);
+ double c2 =
+ fputil::multiply_add(rr.lo, Exp10Base::COEFFS[4], Exp10Base::COEFFS[3]);
+ double exp10_lo = fputil::polyeval(lo_sq, c0, c1, c2);
+ // 10^x - 1 = 2^(mid + hi) * 10^lo - 1
+ // ~ mh * exp10_lo - 1
+ return static_cast<float>(fputil::multiply_add(exp10_lo, rr.mh, -1.0));
+}
+
+} // namespace LIBC_NAMESPACE
diff --git a/libc/test/src/math/CMakeLists.txt b/libc/test/src/math/CMakeLists.txt
index b46ef4028915ba..a13e0f42009a4e 100644
--- a/libc/test/src/math/CMakeLists.txt
+++ b/libc/test/src/math/CMakeLists.txt
@@ -1019,6 +1019,7 @@ add_fp_unittest(
SRCS
exp2m1f_test.cpp
DEPENDS
+ libc.hdr.math_macros
libc.src.errno.errno
libc.src.math.exp2m1f
libc.src.__support.CPP.array
@@ -1084,6 +1085,21 @@ add_fp_unittest(
libc.src.math.exp10m1f16
)
+add_fp_unittest(
+ exp10m1f_test
+ NEED_MPFR
+ SUITE
+ libc-math-unittests
+ SRCS
+ exp10m1f_test.cpp
+ DEPENDS
+ libc.hdr.math_macros
+ libc.src.errno.errno
+ libc.src.math.exp10m1f
+ libc.src.__support.CPP.array
+ libc.src.__support.FPUtil.fp_bits
+)
+
add_fp_unittest(
copysign_test
SUITE
diff --git a/libc/test/src/math/exhaustive/CMakeLists.txt b/libc/test/src/math/exhaustive/CMakeLists.txt
index 6c10ea422109e7..423c3b7a8bfd11 100644
--- a/libc/test/src/math/exhaustive/CMakeLists.txt
+++ b/libc/test/src/math/exhaustive/CMakeLists.txt
@@ -201,6 +201,21 @@ add_fp_unittest(
-lpthread
)
+add_fp_unittest(
+ exp10m1f_test
+ NO_RUN_POSTBUILD
+ NEED_MPFR
+ SUITE
+ libc_math_exhaustive_tests
+ SRCS
+ exp10m1f_test.cpp
+ DEPENDS
+ .exhaustive_test
+ libc.src.math.exp10m1f
+ LINK_LIBRARIES
+ -lpthread
+)
+
add_fp_unittest(
expm1f_test
NO_RUN_POSTBUILD
diff --git a/libc/test/src/math/exhaustive/exp10m1f_test.cpp b/libc/test/src/math/exhaustive/exp10m1f_test.cpp
new file mode 100644
index 00000000000000..b9b2290f8b570d
--- /dev/null
+++ b/libc/test/src/math/exhaustive/exp10m1f_test.cpp
@@ -0,0 +1,33 @@
+//===-- Exhaustive test for exp10m1f --------------------------------------===//
+//
+// 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/math/exp10m1f.h"
+#include "utils/MPFRWrapper/MPFRUtils.h"
+
+namespace mpfr = LIBC_NAMESPACE::testing::mpfr;
+
+using LlvmLibcExp10m1fExhaustiveTest =
+ LlvmLibcUnaryOpExhaustiveMathTest<float, mpfr::Operation::Exp10m1,
+ LIBC_NAMESPACE::exp10m1f>;
+
+// Range: [0, Inf];
+static constexpr uint32_t POS_START = 0x0000'0000U;
+static constexpr uint32_t POS_STOP = 0x7f80'0000U;
+
+TEST_F(LlvmLibcExp10m1fExhaustiveTest, PostiveRange) {
+ test_full_range_all_roundings(POS_START, POS_STOP);
+}
+
+// Range: [-Inf, 0];
+static constexpr uint32_t NEG_START = 0x8000'0000U;
+static constexpr uint32_t NEG_STOP = 0xff80'0000U;
+
+TEST_F(LlvmLibcExp10m1fExhaustiveTest, NegativeRange) {
+ test_full_range_all_roundings(NEG_START, NEG_STOP);
+}
diff --git a/libc/test/src/math/exp10m1f_test.cpp b/libc/test/src/math/exp10m1f_test.cpp
new file mode 100644
index 00000000000000..946b7b4db9280c
--- /dev/null
+++ b/libc/test/src/math/exp10m1f_test.cpp
@@ -0,0 +1,85 @@
+//===-- Unittests for exp10m1f --------------------------------------------===//
+//
+// 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 "hdr/math_macros.h"
+#include "src/__support/CPP/array.h"
+#include "src/errno/libc_errno.h"
+#include "src/math/exp10m1f.h"
+#include "test/UnitTest/FPMatcher.h"
+#include "test/UnitTest/Test.h"
+#include "utils/MPFRWrapper/MPFRUtils.h"
+
+#include <stdint.h>
+
+using LlvmLibcExp10m1fTest = LIBC_NAMESPACE::testing::FPTest<float>;
+
+namespace mpfr = LIBC_NAMESPACE::testing::mpfr;
+
+TEST_F(LlvmLibcExp10m1fTest, TrickyInputs) {
+ constexpr LIBC_NAMESPACE::cpp::array<float, 30> INPUTS = {
+ // EXP10M1F_EXCEPTS_LO
+ 0x1.0fe54ep-11f,
+ 0x1.80e6eap-11f,
+ -0x1.2a33bcp-51f,
+ -0x0p+0f,
+ -0x1.b59e08p-31f,
+ -0x1.bf342p-12f,
+ -0x1.6207fp-11f,
+ -0x1.bd0c66p-11f,
+ -0x1.ffd84cp-10f,
+ -0x1.a74172p-9f,
+ -0x1.cb694cp-9f,
+ // EXP10M1F_EXCEPTS_HI
+ 0x1.8d31eep-8f,
+ 0x1.915fcep-8f,
+ 0x1.bcf982p-8f,
+ 0x1.99ff0ap-7f,
+ 0x1.75ea14p-6f,
+ 0x1.f81b64p-6f,
+ 0x1.fafecp+3f,
+ -0x1.3bf094p-8f,
+ -0x1.4558bcp-8f,
+ -0x1.4bb43p-8f,
+ -0x1.776cc8p-8f,
+ -0x1.f024cp-8f,
+ -0x1.f510eep-8f,
+ -0x1.0b43c4p-7f,
+ -0x1.245ee4p-7f,
+ -0x1.f9f2dap-7f,
+ -0x1.08e42p-6f,
+ -0x1.0cdc44p-5f,
+ -0x1.ca4322p-5f,
+ };
+
+ for (float x : INPUTS) {
+ LIBC_NAMESPACE::libc_errno = 0;
+ EXPECT_MPFR_MATCH_ALL_ROUNDING(mpfr::Operation::Exp10m1, x,
+ LIBC_NAMESPACE::exp10m1f(x), 0.5);
+ }
+}
+
+TEST_F(LlvmLibcExp10m1fTest, InFloatRange) {
+ constexpr uint32_t COUNT = 100'000;
+ constexpr uint32_t STEP = UINT32_MAX / COUNT;
+ for (uint32_t i = 0, v = 0; i <= COUNT; ++i, v += STEP) {
+ float x = FPBits(v).get_val();
+ if (isnan(x) || isinf(x))
+ continue;
+ LIBC_NAMESPACE::libc_errno = 0;
+ float result = LIBC_NAMESPACE::exp10m1f(x);
+
+ // If the computation resulted in an error or did not produce valid result
+ // in the single-precision floating point range, then ignore comparing with
+ // MPFR result as MPFR can still produce valid results because of its
+ // wider precision.
+ if (isnan(result) || isinf(result) || LIBC_NAMESPACE::libc_errno != 0)
+ continue;
+ ASSERT_MPFR_MATCH_ALL_ROUNDING(mpfr::Operation::Exp10m1, x,
+ LIBC_NAMESPACE::exp10m1f(x), 0.5);
+ }
+}
diff --git a/libc/test/src/math/exp2m1f_test.cpp b/libc/test/src/math/exp2m1f_test.cpp
index 793cf0cc2cbb47..b8d4b084a3498e 100644
--- a/libc/test/src/math/exp2m1f_test.cpp
+++ b/libc/test/src/math/exp2m1f_test.cpp
@@ -8,7 +8,6 @@
#include "hdr/math_macros.h"
#include "src/__support/CPP/array.h"
-#include "src/__support/FPUtil/FPBits.h"
#include "src/errno/libc_errno.h"
#include "src/math/exp2m1f.h"
#include "test/UnitTest/FPMatcher.h"
@@ -24,17 +23,17 @@ namespace mpfr = LIBC_NAMESPACE::testing::mpfr;
TEST_F(LlvmLibcExp2m1fTest, TrickyInputs) {
constexpr LIBC_NAMESPACE::cpp::array<float, 10> INPUTS = {
// EXP2M1F_EXCEPTS_LO
- 0x1.36dc8ep-36,
- 0x1.224936p-19,
- 0x1.d16d2p-20,
- 0x1.17949ep-14,
- -0x1.9c3e1ep-38,
- -0x1.4d89b4p-32,
- -0x1.a6eac4p-10,
- -0x1.e7526ep-6,
+ 0x1.36dc8ep-36f,
+ 0x1.224936p-19f,
+ 0x1.d16d2p-20f,
+ 0x1.17949ep-14f,
+ -0x1.9c3e1ep-38f,
+ -0x1.4d89b4p-32f,
+ -0x1.a6eac4p-10f,
+ -0x1.e7526ep-6f,
// EXP2M1F_EXCEPTS_HI
- 0x1.16a972p-1,
- -0x1.9f12acp-5,
+ 0x1.16a972p-1f,
+ -0x1.9f12acp-5f,
};
for (float x : INPUTS) {
diff --git a/libc/test/src/math/smoke/CMakeLists.txt b/libc/test/src/math/smoke/CMakeLists.txt
index 269e92c5900628..1da6f377a1debb 100644
--- a/libc/test/src/math/smoke/CMakeLists.txt
+++ b/libc/test/src/math/smoke/CMakeLists.txt
@@ -1259,6 +1259,17 @@ add_fp_unittest(
libc.src.__support.FPUtil.cast
)
+add_fp_unittest(
+ exp10m1f_test
+ SUITE
+ libc-math-smoke-tests
+ SRCS
+ exp10m1f_test.cpp
+ DEPENDS
+ libc.src.errno.errno
+ libc.src.math.exp10m1f
+)
+
add_fp_unittest(
copysign_test
SUITE
diff --git a/libc/test/src/math/smoke/exp10m1f_test.cpp b/libc/test/src/math/smoke/exp10m1f_test.cpp
new file mode 100644
index 00000000000000..36957af14bb2fa
--- /dev/null
+++ b/libc/test/src/math/smoke/exp10m1f_test.cpp
@@ -0,0 +1,56 @@
+//===-- Unittests for exp10m1f --------------------------------------------===//
+//
+// 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/errno/libc_errno.h"
+#include "src/math/exp10m1f.h"
+#include "test/UnitTest/FPMatcher.h"
+#include "test/UnitTest/Test.h"
+
+using LlvmLibcExp10m1fTest = LIBC_NAMESPACE::testing::FPTest<float>;
+
+TEST_F(LlvmLibcExp10m1fTest, SpecialNumbers) {
+ LIBC_NAMESPACE::libc_errno = 0;
+
+ EXPECT_FP_EQ_ALL_ROUNDING(aNaN, LIBC_NAMESPACE::exp10m1f(aNaN));
+ EXPECT_FP_EQ_ALL_ROUNDING(inf, LIBC_NAMESPACE::exp10m1f(inf));
+ EXPECT_FP_EQ_ALL_ROUNDING(-1.0f, LIBC_NAMESPACE::exp10m1f(neg_inf));
+ EXPECT_FP_EQ_ALL_ROUNDING(0.0f, LIBC_NAMESPACE::exp10m1f(0.0f));
+ EXPECT_FP_EQ_ALL_ROUNDING(-0.0f, LIBC_NAMESPACE::exp10m1f(-0.0f));
+
+ EXPECT_FP_EQ_ALL_ROUNDING(9.0f, LIBC_NAMESPACE::exp10m1f(1.0f));
+ EXPECT_FP_EQ_ALL_ROUNDING(99.0f, LIBC_NAMESPACE::exp10m1f(2.0f));
+ EXPECT_FP_EQ_ALL_ROUNDING(999.0f, LIBC_NAMESPACE::exp10m1f(3.0f));
+}
+
+TEST_F(LlvmLibcExp10m1fTest, Overflow) {
+ LIBC_NAMESPACE::libc_errno = 0;
+
+ EXPECT_FP_EQ_WITH_EXCEPTION(inf, LIBC_NAMESPACE::exp10m1f(0x1.fffffep+127f),
+ FE_OVERFLOW);
+ EXPECT_MATH_ERRNO(ERANGE);
+
+ EXPECT_FP_EQ_WITH_EXCEPTION(inf, LIBC_NAMESPACE::exp10m1f(128.0f),
+ FE_OVERFLOW);
+ EXPECT_MATH_ERRNO(ERANGE);
+
+ EXPECT_FP_EQ_WITH_EXCEPTION(inf, LIBC_NAMESPACE::exp10m1f(0x1.000002p+7f),
+ FE_OVERFLOW);
+ EXPECT_MATH_ERRNO(ERANGE);
+}
+
+TEST_F(LlvmLibcExp10m1fTest, Underflow) {
+ LIBC_NAMESPACE::libc_errno = 0;
+
+ EXPECT_FP_EQ_WITH_EXCEPTION(
+ -1.0f, LIBC_NAMESPACE::exp10m1f(-0x1.fffffep+127f), FE_UNDERFLOW);
+ EXPECT_MATH_ERRNO(ERANGE);
+
+ EXPECT_FP_EQ_WITH_EXCEPTION(-1.0f, LIBC_NAMESPACE::exp10m1f(-0x1.e1a5e4p+2f),
+ FE_UNDERFLOW);
+ EXPECT_MATH_ERRNO(ERANGE);
+}
diff --git a/libc/test/src/math/smoke/exp2m1f_test.cpp b/libc/test/src/math/smoke/exp2m1f_test.cpp
index 4657d088f07a89..a90ae9662771bb 100644
--- a/libc/test/src/math/smoke/exp2m1f_test.cpp
+++ b/libc/test/src/math/smoke/exp2m1f_test.cpp
@@ -12,8 +12,6 @@
#include "test/UnitTest/Test.h"
using LlvmLibcExp2m1fTest = LIBC_NAMESPACE::testing::FPTest<float>;
-using LIBC_NAMESPACE::fputil::testing::ForceRoundingMode;
-using LIBC_NAMESPACE::fputil::testing::RoundingMode;
TEST_F(LlvmLibcExp2m1fTest, SpecialNumbers) {
LIBC_NAMESPACE::libc_errno = 0;
@@ -33,7 +31,7 @@ TEST_F(LlvmLibcExp2m1fTest, SpecialNumbers) {
TEST_F(LlvmLibcExp2m1fTest, Overflow) {
LIBC_NAMESPACE::libc_errno = 0;
- EXPECT_FP_EQ_WITH_EXCEPTION(inf, LIBC_NAMESPACE::exp2m1f(0x1.fffffep+127),
+ EXPECT_FP_EQ_WITH_EXCEPTION(inf, LIBC_NAMESPACE::exp2m1f(0x1.fffffep+127f),
FE_OVERFLOW);
EXPECT_MATH_ERRNO(ERANGE);
@@ -41,7 +39,7 @@ TEST_F(LlvmLibcExp2m1fTest, Overflow) {
FE_OVERFLOW);
EXPECT_MATH_ERRNO(ERANGE);
- EXPECT_FP_EQ_WITH_EXCEPTION(inf, LIBC_NAMESPACE::exp2m1f(0x1.000002p+7),
+ EXPECT_FP_EQ_WITH_EXCEPTION(inf, LIBC_NAMESPACE::exp2m1f(0x1.000002p+7f),
FE_OVERFLOW);
EXPECT_MATH_ERRNO(ERANGE);
}
@@ -49,7 +47,7 @@ TEST_F(LlvmLibcExp2m1fTest, Overflow) {
TEST_F(LlvmLibcExp2m1fTest, Underflow) {
LIBC_NAMESPACE::libc_errno = 0;
- EXPECT_FP_EQ_WITH_EXCEPTION(-1.0f, LIBC_NAMESPACE::exp2m1f(-0x1.fffffep+127),
+ EXPECT_FP_EQ_WITH_EXCEPTION(-1.0f, LIBC_NAMESPACE::exp2m1f(-0x1.fffffep+127f),
FE_UNDERFLOW);
EXPECT_MATH_ERRNO(ERANGE);
@@ -57,7 +55,7 @@ TEST_F(LlvmLibcExp2m1fTest, Underflow) {
FE_UNDERFLOW);
EXPECT_MATH_ERRNO(ERANGE);
- EXPECT_FP_EQ_WITH_EXCEPTION(-1.0f, LIBC_NAMESPACE::exp2m1f(-0x1.900002p4),
+ EXPECT_FP_EQ_WITH_EXCEPTION(-1.0f, LIBC_NAMESPACE::exp2m1f(-0x1.900002p+4f),
FE_UNDERFLOW);
EXPECT_MATH_ERRNO(ERANGE);
}
>From 569752cadbdaab505fce68ea0a76b72a279b9433 Mon Sep 17 00:00:00 2001
From: OverMighty <its.overmighty at gmail.com>
Date: Tue, 9 Apr 2024 20:13:53 +0100
Subject: [PATCH 2/6] fixup! [libc][math][c23] Add exp10m1f C23 math function
Undo changes to exp2m1f build and tests.
---
libc/test/src/math/CMakeLists.txt | 1 -
libc/test/src/math/exp2m1f_test.cpp | 21 +++++++++++----------
libc/test/src/math/smoke/exp2m1f_test.cpp | 10 ++++++----
3 files changed, 17 insertions(+), 15 deletions(-)
diff --git a/libc/test/src/math/CMakeLists.txt b/libc/test/src/math/CMakeLists.txt
index a13e0f42009a4e..d120f8e2fab219 100644
--- a/libc/test/src/math/CMakeLists.txt
+++ b/libc/test/src/math/CMakeLists.txt
@@ -1019,7 +1019,6 @@ add_fp_unittest(
SRCS
exp2m1f_test.cpp
DEPENDS
- libc.hdr.math_macros
libc.src.errno.errno
libc.src.math.exp2m1f
libc.src.__support.CPP.array
diff --git a/libc/test/src/math/exp2m1f_test.cpp b/libc/test/src/math/exp2m1f_test.cpp
index b8d4b084a3498e..793cf0cc2cbb47 100644
--- a/libc/test/src/math/exp2m1f_test.cpp
+++ b/libc/test/src/math/exp2m1f_test.cpp
@@ -8,6 +8,7 @@
#include "hdr/math_macros.h"
#include "src/__support/CPP/array.h"
+#include "src/__support/FPUtil/FPBits.h"
#include "src/errno/libc_errno.h"
#include "src/math/exp2m1f.h"
#include "test/UnitTest/FPMatcher.h"
@@ -23,17 +24,17 @@ namespace mpfr = LIBC_NAMESPACE::testing::mpfr;
TEST_F(LlvmLibcExp2m1fTest, TrickyInputs) {
constexpr LIBC_NAMESPACE::cpp::array<float, 10> INPUTS = {
// EXP2M1F_EXCEPTS_LO
- 0x1.36dc8ep-36f,
- 0x1.224936p-19f,
- 0x1.d16d2p-20f,
- 0x1.17949ep-14f,
- -0x1.9c3e1ep-38f,
- -0x1.4d89b4p-32f,
- -0x1.a6eac4p-10f,
- -0x1.e7526ep-6f,
+ 0x1.36dc8ep-36,
+ 0x1.224936p-19,
+ 0x1.d16d2p-20,
+ 0x1.17949ep-14,
+ -0x1.9c3e1ep-38,
+ -0x1.4d89b4p-32,
+ -0x1.a6eac4p-10,
+ -0x1.e7526ep-6,
// EXP2M1F_EXCEPTS_HI
- 0x1.16a972p-1f,
- -0x1.9f12acp-5f,
+ 0x1.16a972p-1,
+ -0x1.9f12acp-5,
};
for (float x : INPUTS) {
diff --git a/libc/test/src/math/smoke/exp2m1f_test.cpp b/libc/test/src/math/smoke/exp2m1f_test.cpp
index a90ae9662771bb..4657d088f07a89 100644
--- a/libc/test/src/math/smoke/exp2m1f_test.cpp
+++ b/libc/test/src/math/smoke/exp2m1f_test.cpp
@@ -12,6 +12,8 @@
#include "test/UnitTest/Test.h"
using LlvmLibcExp2m1fTest = LIBC_NAMESPACE::testing::FPTest<float>;
+using LIBC_NAMESPACE::fputil::testing::ForceRoundingMode;
+using LIBC_NAMESPACE::fputil::testing::RoundingMode;
TEST_F(LlvmLibcExp2m1fTest, SpecialNumbers) {
LIBC_NAMESPACE::libc_errno = 0;
@@ -31,7 +33,7 @@ TEST_F(LlvmLibcExp2m1fTest, SpecialNumbers) {
TEST_F(LlvmLibcExp2m1fTest, Overflow) {
LIBC_NAMESPACE::libc_errno = 0;
- EXPECT_FP_EQ_WITH_EXCEPTION(inf, LIBC_NAMESPACE::exp2m1f(0x1.fffffep+127f),
+ EXPECT_FP_EQ_WITH_EXCEPTION(inf, LIBC_NAMESPACE::exp2m1f(0x1.fffffep+127),
FE_OVERFLOW);
EXPECT_MATH_ERRNO(ERANGE);
@@ -39,7 +41,7 @@ TEST_F(LlvmLibcExp2m1fTest, Overflow) {
FE_OVERFLOW);
EXPECT_MATH_ERRNO(ERANGE);
- EXPECT_FP_EQ_WITH_EXCEPTION(inf, LIBC_NAMESPACE::exp2m1f(0x1.000002p+7f),
+ EXPECT_FP_EQ_WITH_EXCEPTION(inf, LIBC_NAMESPACE::exp2m1f(0x1.000002p+7),
FE_OVERFLOW);
EXPECT_MATH_ERRNO(ERANGE);
}
@@ -47,7 +49,7 @@ TEST_F(LlvmLibcExp2m1fTest, Overflow) {
TEST_F(LlvmLibcExp2m1fTest, Underflow) {
LIBC_NAMESPACE::libc_errno = 0;
- EXPECT_FP_EQ_WITH_EXCEPTION(-1.0f, LIBC_NAMESPACE::exp2m1f(-0x1.fffffep+127f),
+ EXPECT_FP_EQ_WITH_EXCEPTION(-1.0f, LIBC_NAMESPACE::exp2m1f(-0x1.fffffep+127),
FE_UNDERFLOW);
EXPECT_MATH_ERRNO(ERANGE);
@@ -55,7 +57,7 @@ TEST_F(LlvmLibcExp2m1fTest, Underflow) {
FE_UNDERFLOW);
EXPECT_MATH_ERRNO(ERANGE);
- EXPECT_FP_EQ_WITH_EXCEPTION(-1.0f, LIBC_NAMESPACE::exp2m1f(-0x1.900002p+4f),
+ EXPECT_FP_EQ_WITH_EXCEPTION(-1.0f, LIBC_NAMESPACE::exp2m1f(-0x1.900002p4),
FE_UNDERFLOW);
EXPECT_MATH_ERRNO(ERANGE);
}
>From cda46fe0a7dc82c10d32a3a82e4633f999d8cdfa Mon Sep 17 00:00:00 2001
From: OverMighty <its.overmighty at gmail.com>
Date: Wed, 10 Apr 2024 00:08:17 +0100
Subject: [PATCH 3/6] fixup! [libc][math][c23] Add exp10m1f C23 math function
---
libc/src/math/generic/exp10m1f.cpp | 4 ++--
libc/test/UnitTest/FPMatcher.h | 3 ++-
libc/test/src/math/exp10m1f_test.cpp | 14 +++++++++++++-
libc/test/src/math/smoke/exp10m1f_test.cpp | 13 ++++++++-----
4 files changed, 25 insertions(+), 9 deletions(-)
diff --git a/libc/src/math/generic/exp10m1f.cpp b/libc/src/math/generic/exp10m1f.cpp
index 50d4c50fb869ca..48621bb5158004 100644
--- a/libc/src/math/generic/exp10m1f.cpp
+++ b/libc/src/math/generic/exp10m1f.cpp
@@ -134,7 +134,7 @@ LLVM_LIBC_FUNCTION(float, exp10m1f, (float x)) {
}
// When x <= log10(2^-25), or x is nan
- if (LIBC_UNLIKELY(x <= -0x1.e1a5e2p+2f)) {
+ if (LIBC_UNLIKELY(x_u >= 0xc0f0d2f1)) {
// exp10m1(-inf) = -1
if (xbits.is_inf())
return -1.0f;
@@ -144,7 +144,7 @@ LLVM_LIBC_FUNCTION(float, exp10m1f, (float x)) {
int rounding = fputil::quick_get_round();
if (rounding == FE_UPWARD || rounding == FE_TOWARDZERO ||
- (rounding == FE_TONEAREST && x == -0x1.e1a5e2p+2f))
+ (rounding == FE_TONEAREST && x_u == 0xc0f0d2f1))
return -0x1.ffff'fep-1f; // -1.0f + 0x1.0p-24f
fputil::set_errno_if_required(ERANGE);
diff --git a/libc/test/UnitTest/FPMatcher.h b/libc/test/UnitTest/FPMatcher.h
index 55fe73cd2f1ac9..9f2bae3279208b 100644
--- a/libc/test/UnitTest/FPMatcher.h
+++ b/libc/test/UnitTest/FPMatcher.h
@@ -174,7 +174,8 @@ template <typename T> struct FPTest : public Test {
LIBC_NAMESPACE::cpp::numeric_limits<StorageType>::max();
static constexpr T zero = FPBits::zero(Sign::POS).get_val();
static constexpr T neg_zero = FPBits::zero(Sign::NEG).get_val();
- static constexpr T aNaN = FPBits::quiet_nan().get_val();
+ static constexpr T aNaN = FPBits::quiet_nan(Sign::POS).get_val();
+ static constexpr T neg_aNaN = FPBits::quiet_nan(Sign::NEG).get_val();
static constexpr T sNaN = FPBits::signaling_nan().get_val();
static constexpr T inf = FPBits::inf(Sign::POS).get_val();
static constexpr T neg_inf = FPBits::inf(Sign::NEG).get_val();
diff --git a/libc/test/src/math/exp10m1f_test.cpp b/libc/test/src/math/exp10m1f_test.cpp
index 946b7b4db9280c..cc960321175cbf 100644
--- a/libc/test/src/math/exp10m1f_test.cpp
+++ b/libc/test/src/math/exp10m1f_test.cpp
@@ -21,7 +21,7 @@ using LlvmLibcExp10m1fTest = LIBC_NAMESPACE::testing::FPTest<float>;
namespace mpfr = LIBC_NAMESPACE::testing::mpfr;
TEST_F(LlvmLibcExp10m1fTest, TrickyInputs) {
- constexpr LIBC_NAMESPACE::cpp::array<float, 30> INPUTS = {
+ constexpr LIBC_NAMESPACE::cpp::array<float, 39> INPUTS = {
// EXP10M1F_EXCEPTS_LO
0x1.0fe54ep-11f,
0x1.80e6eap-11f,
@@ -54,6 +54,18 @@ TEST_F(LlvmLibcExp10m1fTest, TrickyInputs) {
-0x1.08e42p-6f,
-0x1.0cdc44p-5f,
-0x1.ca4322p-5f,
+ // Exceptional integers.
+ 8.0f,
+ 9.0f,
+ 10.0f,
+ // Overflow boundaries.
+ 0x1.344134p+5f,
+ 0x1.344136p+5f,
+ 0x1.344138p+5f,
+ // Underflow boundaries.
+ -0x1.e1a5e0p+2f,
+ -0x1.e1a5e2p+2f,
+ -0x1.e1a5e4p+2f,
};
for (float x : INPUTS) {
diff --git a/libc/test/src/math/smoke/exp10m1f_test.cpp b/libc/test/src/math/smoke/exp10m1f_test.cpp
index 36957af14bb2fa..2c56aef2ebdc0a 100644
--- a/libc/test/src/math/smoke/exp10m1f_test.cpp
+++ b/libc/test/src/math/smoke/exp10m1f_test.cpp
@@ -16,7 +16,10 @@ using LlvmLibcExp10m1fTest = LIBC_NAMESPACE::testing::FPTest<float>;
TEST_F(LlvmLibcExp10m1fTest, SpecialNumbers) {
LIBC_NAMESPACE::libc_errno = 0;
- EXPECT_FP_EQ_ALL_ROUNDING(aNaN, LIBC_NAMESPACE::exp10m1f(aNaN));
+ EXPECT_EQ(FPBits(aNaN).uintval(),
+ FPBits(LIBC_NAMESPACE::exp10m1f(aNaN)).uintval());
+ EXPECT_EQ(FPBits(neg_aNaN).uintval(),
+ FPBits(LIBC_NAMESPACE::exp10m1f(neg_aNaN)).uintval());
EXPECT_FP_EQ_ALL_ROUNDING(inf, LIBC_NAMESPACE::exp10m1f(inf));
EXPECT_FP_EQ_ALL_ROUNDING(-1.0f, LIBC_NAMESPACE::exp10m1f(neg_inf));
EXPECT_FP_EQ_ALL_ROUNDING(0.0f, LIBC_NAMESPACE::exp10m1f(0.0f));
@@ -34,11 +37,11 @@ TEST_F(LlvmLibcExp10m1fTest, Overflow) {
FE_OVERFLOW);
EXPECT_MATH_ERRNO(ERANGE);
- EXPECT_FP_EQ_WITH_EXCEPTION(inf, LIBC_NAMESPACE::exp10m1f(128.0f),
+ EXPECT_FP_EQ_WITH_EXCEPTION(inf, LIBC_NAMESPACE::exp10m1f(0x1.344136p+5),
FE_OVERFLOW);
EXPECT_MATH_ERRNO(ERANGE);
- EXPECT_FP_EQ_WITH_EXCEPTION(inf, LIBC_NAMESPACE::exp10m1f(0x1.000002p+7f),
+ EXPECT_FP_EQ_WITH_EXCEPTION(inf, LIBC_NAMESPACE::exp10m1f(0x1.344138p+5),
FE_OVERFLOW);
EXPECT_MATH_ERRNO(ERANGE);
}
@@ -46,8 +49,8 @@ TEST_F(LlvmLibcExp10m1fTest, Overflow) {
TEST_F(LlvmLibcExp10m1fTest, Underflow) {
LIBC_NAMESPACE::libc_errno = 0;
- EXPECT_FP_EQ_WITH_EXCEPTION(
- -1.0f, LIBC_NAMESPACE::exp10m1f(-0x1.fffffep+127f), FE_UNDERFLOW);
+ EXPECT_FP_EQ_WITH_EXCEPTION(-1.0f, LIBC_NAMESPACE::exp10m1f(-max_normal),
+ FE_UNDERFLOW);
EXPECT_MATH_ERRNO(ERANGE);
EXPECT_FP_EQ_WITH_EXCEPTION(-1.0f, LIBC_NAMESPACE::exp10m1f(-0x1.e1a5e4p+2f),
>From ad16e96a23b093af9a908ac488a5a57e1fef9d9e Mon Sep 17 00:00:00 2001
From: OverMighty <its.overmighty at gmail.com>
Date: Mon, 29 Apr 2024 22:04:59 +0100
Subject: [PATCH 4/6] fixup! [libc][math][c23] Add exp10m1f C23 math function
Fix left shift of negative values.
---
libc/src/math/generic/explogxf.h | 8 ++++----
1 file changed, 4 insertions(+), 4 deletions(-)
diff --git a/libc/src/math/generic/explogxf.h b/libc/src/math/generic/explogxf.h
index f3f50c21aacce7..651524a165f032 100644
--- a/libc/src/math/generic/explogxf.h
+++ b/libc/src/math/generic/explogxf.h
@@ -159,12 +159,12 @@ template <class Base> LIBC_INLINE exp_b_reduc_t exp_b_range_reduc(float x) {
int k = static_cast<int>(kd);
// hi = floor(kd * 2^(-MID_BITS))
// exp_hi = shift hi to the exponent field of double precision.
- int64_t exp_hi = static_cast<int64_t>((k >> Base::MID_BITS))
- << fputil::FPBits<double>::FRACTION_LEN;
+ uint64_t exp_hi = static_cast<uint64_t>(k >> Base::MID_BITS)
+ << fputil::FPBits<double>::FRACTION_LEN;
// mh = 2^hi * 2^mid
// mh_bits = bit field of mh
- int64_t mh_bits = Base::EXP_2_MID[k & Base::MID_MASK] + exp_hi;
- double mh = fputil::FPBits<double>(uint64_t(mh_bits)).get_val();
+ uint64_t mh_bits = Base::EXP_2_MID[k & Base::MID_MASK] + exp_hi;
+ double mh = fputil::FPBits<double>(mh_bits).get_val();
// dx = lo = x - (hi + mid) * log(2)
double dx = fputil::multiply_add(
kd, Base::M_LOGB_2_LO, fputil::multiply_add(kd, Base::M_LOGB_2_HI, xd));
>From d0848dd1915779f838b83b1a2c70b049984c7902 Mon Sep 17 00:00:00 2001
From: OverMighty <its.overmighty at gmail.com>
Date: Tue, 5 Nov 2024 12:13:30 +0100
Subject: [PATCH 5/6] Add exp10m1f to newhdrgen math.yaml
---
libc/newhdrgen/yaml/math.yaml | 6 ++++++
1 file changed, 6 insertions(+)
diff --git a/libc/newhdrgen/yaml/math.yaml b/libc/newhdrgen/yaml/math.yaml
index 3cc4b599c777bf..d6669f1e8ffcc4 100644
--- a/libc/newhdrgen/yaml/math.yaml
+++ b/libc/newhdrgen/yaml/math.yaml
@@ -280,6 +280,12 @@ functions:
return_type: float
arguments:
- type: float
+ - name: exp10m1f
+ standards:
+ - stdc
+ return_type: float
+ arguments:
+ - type: float
- name: exp10m1f16
standards:
- stdc
>From cd963f6434ead26347d5c971dabf7d44ad49037d Mon Sep 17 00:00:00 2001
From: OverMighty <its.overmighty at gmail.com>
Date: Tue, 5 Nov 2024 12:15:17 +0100
Subject: [PATCH 6/6] Change smoke test to use zero and neg_zero instead of
literals
---
libc/test/src/math/smoke/exp10m1f_test.cpp | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/libc/test/src/math/smoke/exp10m1f_test.cpp b/libc/test/src/math/smoke/exp10m1f_test.cpp
index 2c56aef2ebdc0a..9c65a38425d778 100644
--- a/libc/test/src/math/smoke/exp10m1f_test.cpp
+++ b/libc/test/src/math/smoke/exp10m1f_test.cpp
@@ -22,8 +22,8 @@ TEST_F(LlvmLibcExp10m1fTest, SpecialNumbers) {
FPBits(LIBC_NAMESPACE::exp10m1f(neg_aNaN)).uintval());
EXPECT_FP_EQ_ALL_ROUNDING(inf, LIBC_NAMESPACE::exp10m1f(inf));
EXPECT_FP_EQ_ALL_ROUNDING(-1.0f, LIBC_NAMESPACE::exp10m1f(neg_inf));
- EXPECT_FP_EQ_ALL_ROUNDING(0.0f, LIBC_NAMESPACE::exp10m1f(0.0f));
- EXPECT_FP_EQ_ALL_ROUNDING(-0.0f, LIBC_NAMESPACE::exp10m1f(-0.0f));
+ EXPECT_FP_EQ_ALL_ROUNDING(zero, LIBC_NAMESPACE::exp10m1f(zero));
+ EXPECT_FP_EQ_ALL_ROUNDING(neg_zero, LIBC_NAMESPACE::exp10m1f(neg_zero));
EXPECT_FP_EQ_ALL_ROUNDING(9.0f, LIBC_NAMESPACE::exp10m1f(1.0f));
EXPECT_FP_EQ_ALL_ROUNDING(99.0f, LIBC_NAMESPACE::exp10m1f(2.0f));
More information about the libc-commits
mailing list