[libc-commits] [libc] 1fda257 - [libc][math][c++23] Add log_bf16 math function (#157811)
via libc-commits
libc-commits at lists.llvm.org
Wed Sep 24 08:17:14 PDT 2025
Author: Krishna Pandey
Date: 2025-09-24T20:47:10+05:30
New Revision: 1fda25772843bdb2d399734e386540203aa834d0
URL: https://github.com/llvm/llvm-project/commit/1fda25772843bdb2d399734e386540203aa834d0
DIFF: https://github.com/llvm/llvm-project/commit/1fda25772843bdb2d399734e386540203aa834d0.diff
LOG: [libc][math][c++23] Add log_bf16 math function (#157811)
This PR adds log_bf16 higher math function for BFloat16 type along with
the tests.
---------
Signed-off-by: Krishna Pandey <kpandey81930 at gmail.com>
Added:
libc/src/math/generic/log_bf16.cpp
libc/src/math/log_bf16.h
libc/test/src/math/log_bf16_test.cpp
libc/test/src/math/smoke/log_bf16_test.cpp
Modified:
libc/config/baremetal/aarch64/entrypoints.txt
libc/config/baremetal/arm/entrypoints.txt
libc/config/baremetal/riscv/entrypoints.txt
libc/config/darwin/aarch64/entrypoints.txt
libc/config/darwin/x86_64/entrypoints.txt
libc/config/gpu/amdgpu/entrypoints.txt
libc/config/gpu/nvptx/entrypoints.txt
libc/config/linux/aarch64/entrypoints.txt
libc/config/linux/arm/entrypoints.txt
libc/config/linux/riscv/entrypoints.txt
libc/config/linux/x86_64/entrypoints.txt
libc/config/windows/entrypoints.txt
libc/docs/headers/math/index.rst
libc/src/math/CMakeLists.txt
libc/src/math/generic/CMakeLists.txt
libc/test/src/math/CMakeLists.txt
libc/test/src/math/smoke/CMakeLists.txt
Removed:
################################################################################
diff --git a/libc/config/baremetal/aarch64/entrypoints.txt b/libc/config/baremetal/aarch64/entrypoints.txt
index 685a80b4002a3..935c95af0d4af 100644
--- a/libc/config/baremetal/aarch64/entrypoints.txt
+++ b/libc/config/baremetal/aarch64/entrypoints.txt
@@ -803,6 +803,7 @@ list(APPEND TARGET_LIBM_ENTRYPOINTS
libc.src.math.llogbbf16
libc.src.math.llrintbf16
libc.src.math.llroundbf16
+ libc.src.math.log_bf16
libc.src.math.logbbf16
libc.src.math.lrintbf16
libc.src.math.lroundbf16
diff --git a/libc/config/baremetal/arm/entrypoints.txt b/libc/config/baremetal/arm/entrypoints.txt
index 8ec972fc58411..82e257c1d2b0d 100644
--- a/libc/config/baremetal/arm/entrypoints.txt
+++ b/libc/config/baremetal/arm/entrypoints.txt
@@ -803,6 +803,7 @@ list(APPEND TARGET_LIBM_ENTRYPOINTS
libc.src.math.llogbbf16
libc.src.math.llrintbf16
libc.src.math.llroundbf16
+ libc.src.math.log_bf16
libc.src.math.logbbf16
libc.src.math.lrintbf16
libc.src.math.lroundbf16
diff --git a/libc/config/baremetal/riscv/entrypoints.txt b/libc/config/baremetal/riscv/entrypoints.txt
index 2f1d930497734..c10cc1162cc5a 100644
--- a/libc/config/baremetal/riscv/entrypoints.txt
+++ b/libc/config/baremetal/riscv/entrypoints.txt
@@ -803,6 +803,7 @@ list(APPEND TARGET_LIBM_ENTRYPOINTS
libc.src.math.llogbbf16
libc.src.math.llrintbf16
libc.src.math.llroundbf16
+ libc.src.math.log_bf16
libc.src.math.logbbf16
libc.src.math.lrintbf16
libc.src.math.lroundbf16
diff --git a/libc/config/darwin/aarch64/entrypoints.txt b/libc/config/darwin/aarch64/entrypoints.txt
index 3af3f7ff66874..e3c6c2b30c415 100644
--- a/libc/config/darwin/aarch64/entrypoints.txt
+++ b/libc/config/darwin/aarch64/entrypoints.txt
@@ -633,6 +633,7 @@ list(APPEND TARGET_LIBM_ENTRYPOINTS
libc.src.math.llogbbf16
libc.src.math.llrintbf16
libc.src.math.llroundbf16
+ libc.src.math.log_bf16
libc.src.math.logbbf16
libc.src.math.lrintbf16
libc.src.math.lroundbf16
diff --git a/libc/config/darwin/x86_64/entrypoints.txt b/libc/config/darwin/x86_64/entrypoints.txt
index a0881e5b02fe8..e899bf97ea3f6 100644
--- a/libc/config/darwin/x86_64/entrypoints.txt
+++ b/libc/config/darwin/x86_64/entrypoints.txt
@@ -276,6 +276,7 @@ list(APPEND TARGET_LIBM_ENTRYPOINTS
libc.src.math.llogbbf16
libc.src.math.llrintbf16
libc.src.math.llroundbf16
+ libc.src.math.log_bf16
libc.src.math.logbbf16
libc.src.math.lrintbf16
libc.src.math.lroundbf16
diff --git a/libc/config/gpu/amdgpu/entrypoints.txt b/libc/config/gpu/amdgpu/entrypoints.txt
index 7e4b1ab6d253f..0dda7d5c683ec 100644
--- a/libc/config/gpu/amdgpu/entrypoints.txt
+++ b/libc/config/gpu/amdgpu/entrypoints.txt
@@ -659,6 +659,7 @@ list(APPEND TARGET_LIBM_ENTRYPOINTS
libc.src.math.llogbbf16
libc.src.math.llrintbf16
libc.src.math.llroundbf16
+ libc.src.math.log_bf16
libc.src.math.logbbf16
libc.src.math.lrintbf16
libc.src.math.lroundbf16
diff --git a/libc/config/gpu/nvptx/entrypoints.txt b/libc/config/gpu/nvptx/entrypoints.txt
index 72a6257283475..6070fb5b17b3c 100644
--- a/libc/config/gpu/nvptx/entrypoints.txt
+++ b/libc/config/gpu/nvptx/entrypoints.txt
@@ -661,6 +661,7 @@ list(APPEND TARGET_LIBM_ENTRYPOINTS
libc.src.math.llogbbf16
libc.src.math.llrintbf16
libc.src.math.llroundbf16
+ libc.src.math.log_bf16
libc.src.math.logbbf16
libc.src.math.lrintbf16
libc.src.math.lroundbf16
diff --git a/libc/config/linux/aarch64/entrypoints.txt b/libc/config/linux/aarch64/entrypoints.txt
index 61b585d2d24ca..ae8deabc97407 100644
--- a/libc/config/linux/aarch64/entrypoints.txt
+++ b/libc/config/linux/aarch64/entrypoints.txt
@@ -886,6 +886,7 @@ list(APPEND TARGET_LIBM_ENTRYPOINTS
libc.src.math.llogbbf16
libc.src.math.llrintbf16
libc.src.math.llroundbf16
+ libc.src.math.log_bf16
libc.src.math.logbbf16
libc.src.math.lrintbf16
libc.src.math.lroundbf16
diff --git a/libc/config/linux/arm/entrypoints.txt b/libc/config/linux/arm/entrypoints.txt
index 97857986d3874..f04ac40145d3a 100644
--- a/libc/config/linux/arm/entrypoints.txt
+++ b/libc/config/linux/arm/entrypoints.txt
@@ -503,6 +503,7 @@ list(APPEND TARGET_LIBM_ENTRYPOINTS
libc.src.math.llogbbf16
libc.src.math.llrintbf16
libc.src.math.llroundbf16
+ libc.src.math.log_bf16
libc.src.math.logbbf16
libc.src.math.lrintbf16
libc.src.math.lroundbf16
diff --git a/libc/config/linux/riscv/entrypoints.txt b/libc/config/linux/riscv/entrypoints.txt
index 2b8003f9f2bbf..5f407e842121e 100644
--- a/libc/config/linux/riscv/entrypoints.txt
+++ b/libc/config/linux/riscv/entrypoints.txt
@@ -908,6 +908,7 @@ list(APPEND TARGET_LIBM_ENTRYPOINTS
libc.src.math.llogbbf16
libc.src.math.llrintbf16
libc.src.math.llroundbf16
+ libc.src.math.log_bf16
libc.src.math.logbbf16
libc.src.math.lrintbf16
libc.src.math.lroundbf16
diff --git a/libc/config/linux/x86_64/entrypoints.txt b/libc/config/linux/x86_64/entrypoints.txt
index 19ed6e02e0ea4..bf2ad4a40ca11 100644
--- a/libc/config/linux/x86_64/entrypoints.txt
+++ b/libc/config/linux/x86_64/entrypoints.txt
@@ -943,6 +943,7 @@ list(APPEND TARGET_LIBM_ENTRYPOINTS
libc.src.math.llogbbf16
libc.src.math.llrintbf16
libc.src.math.llroundbf16
+ libc.src.math.log_bf16
libc.src.math.logbbf16
libc.src.math.lrintbf16
libc.src.math.lroundbf16
diff --git a/libc/config/windows/entrypoints.txt b/libc/config/windows/entrypoints.txt
index b7e6f7be128c4..3a76595b258e2 100644
--- a/libc/config/windows/entrypoints.txt
+++ b/libc/config/windows/entrypoints.txt
@@ -349,6 +349,7 @@ list(APPEND TARGET_LIBM_ENTRYPOINTS
libc.src.math.llogbbf16
libc.src.math.llrintbf16
libc.src.math.llroundbf16
+ libc.src.math.log_bf16
libc.src.math.logbbf16
libc.src.math.lrintbf16
libc.src.math.lroundbf16
diff --git a/libc/docs/headers/math/index.rst b/libc/docs/headers/math/index.rst
index 7d5b341ba674a..51bf238b950b0 100644
--- a/libc/docs/headers/math/index.rst
+++ b/libc/docs/headers/math/index.rst
@@ -319,7 +319,7 @@ Higher Math Functions
+-----------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+------------------------+----------------------------+
| lgamma | | | | | | | 7.12.8.3 | F.10.5.3 |
+-----------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+------------------------+----------------------------+
-| log | |check| | |check| | | |check| | | | 7.12.6.11 | F.10.3.11 |
+| log | |check| | |check| | | |check| | | |check| ? | 7.12.6.11 | F.10.3.11 |
+-----------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+------------------------+----------------------------+
| log10 | |check| | |check| | | |check| | | | 7.12.6.12 | F.10.3.12 |
+-----------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+------------------------+----------------------------+
@@ -373,6 +373,7 @@ Legends:
* x ULPs: largest errors recorded.
* N/A: Not defined in the standard or will not be added.
* \*: LLVM libc extension.
+* ? Because of a conflict between float16 logb function and bfloat16 log function, the latter is implemented as `log_bf16`.
..
TODO(lntue): Add a new page to discuss about the algorithms used in the
diff --git a/libc/src/math/CMakeLists.txt b/libc/src/math/CMakeLists.txt
index a6f400c873b7e..3c7e99f4a9c46 100644
--- a/libc/src/math/CMakeLists.txt
+++ b/libc/src/math/CMakeLists.txt
@@ -392,6 +392,7 @@ add_math_entrypoint_object(log2f16)
add_math_entrypoint_object(log)
add_math_entrypoint_object(logf)
add_math_entrypoint_object(logf16)
+add_math_entrypoint_object(log_bf16)
add_math_entrypoint_object(logb)
add_math_entrypoint_object(logbf)
diff --git a/libc/src/math/generic/CMakeLists.txt b/libc/src/math/generic/CMakeLists.txt
index 0830c7a72a6ec..7cd34fab53480 100644
--- a/libc/src/math/generic/CMakeLists.txt
+++ b/libc/src/math/generic/CMakeLists.txt
@@ -2261,6 +2261,7 @@ add_entrypoint_object(
libc.src.__support.FPUtil.multiply_add
libc.src.__support.FPUtil.polyeval
libc.src.__support.macros.optimization
+ libc.src.__support.macros.properties.cpu_features
)
add_entrypoint_object(
@@ -2283,6 +2284,22 @@ add_entrypoint_object(
libc.src.__support.math.expxf16_utils
)
+add_entrypoint_object(
+ log_bf16
+ SRCS
+ log_bf16.cpp
+ HDRS
+ ../log_bf16.h
+ DEPENDS
+ libc.src.__support.common
+ libc.src.__support.FPUtil.bfloat16
+ libc.src.__support.FPUtil.cast
+ libc.src.__support.FPUtil.fp_bits
+ libc.src.__support.FPUtil.multiply_add
+ libc.src.__support.macros.config
+ libc.src.__support.macros.optimization
+)
+
add_entrypoint_object(
logb
SRCS
diff --git a/libc/src/math/generic/log_bf16.cpp b/libc/src/math/generic/log_bf16.cpp
new file mode 100644
index 0000000000000..213dccca0fb9e
--- /dev/null
+++ b/libc/src/math/generic/log_bf16.cpp
@@ -0,0 +1,137 @@
+//===-- BFloat16 log(x) 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/log_bf16.h"
+#include "src/__support/FPUtil/FPBits.h"
+#include "src/__support/FPUtil/bfloat16.h"
+#include "src/__support/FPUtil/cast.h"
+#include "src/__support/FPUtil/multiply_add.h"
+#include "src/__support/common.h"
+#include "src/__support/macros/config.h"
+#include "src/__support/macros/optimization.h"
+#include "src/__support/macros/properties/cpu_features.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+// Generated by Sollya with the following commands:
+// > display = hexadecimal;
+// > round(log(2), SG, RN);
+static constexpr float LOGF_2 = 0x1.62e43p-1f;
+
+// Generated by Sollya with the following commands:
+// > display = hexadecimal;
+// > for i from 0 to 127 do print(round(log(1 + i * 2^-7), SG, RN));
+static constexpr float LOG_1_PLUS_M[128] = {
+ 0x0.0p0f, 0x1.fe02a6p-8f, 0x1.fc0a8cp-7f, 0x1.7b91bp-6f,
+ 0x1.f829bp-6f, 0x1.39e87cp-5f, 0x1.77459p-5f, 0x1.b42dd8p-5f,
+ 0x1.f0a30cp-5f, 0x1.16536ep-4f, 0x1.341d7ap-4f, 0x1.51b074p-4f,
+ 0x1.6f0d28p-4f, 0x1.8c345ep-4f, 0x1.a926d4p-4f, 0x1.c5e548p-4f,
+ 0x1.e27076p-4f, 0x1.fec914p-4f, 0x1.0d77e8p-3f, 0x1.1b72aep-3f,
+ 0x1.29553p-3f, 0x1.371fc2p-3f, 0x1.44d2b6p-3f, 0x1.526e5ep-3f,
+ 0x1.5ff308p-3f, 0x1.6d60fep-3f, 0x1.7ab89p-3f, 0x1.87fa06p-3f,
+ 0x1.9525aap-3f, 0x1.a23bc2p-3f, 0x1.af3c94p-3f, 0x1.bc2868p-3f,
+ 0x1.c8ff7cp-3f, 0x1.d5c216p-3f, 0x1.e27076p-3f, 0x1.ef0adcp-3f,
+ 0x1.fb9186p-3f, 0x1.04025ap-2f, 0x1.0a324ep-2f, 0x1.1058cp-2f,
+ 0x1.1675cap-2f, 0x1.1c898cp-2f, 0x1.22942p-2f, 0x1.2895a2p-2f,
+ 0x1.2e8e2cp-2f, 0x1.347ddap-2f, 0x1.3a64c6p-2f, 0x1.404308p-2f,
+ 0x1.4618bcp-2f, 0x1.4be5fap-2f, 0x1.51aad8p-2f, 0x1.576772p-2f,
+ 0x1.5d1bdcp-2f, 0x1.62c83p-2f, 0x1.686c82p-2f, 0x1.6e08eap-2f,
+ 0x1.739d8p-2f, 0x1.792a56p-2f, 0x1.7eaf84p-2f, 0x1.842d1ep-2f,
+ 0x1.89a338p-2f, 0x1.8f11e8p-2f, 0x1.947942p-2f, 0x1.99d958p-2f,
+ 0x1.9f323ep-2f, 0x1.a4840ap-2f, 0x1.a9cecap-2f, 0x1.af1294p-2f,
+ 0x1.b44f78p-2f, 0x1.b9858ap-2f, 0x1.beb4dap-2f, 0x1.c3dd7ap-2f,
+ 0x1.c8ff7cp-2f, 0x1.ce1afp-2f, 0x1.d32fe8p-2f, 0x1.d83e72p-2f,
+ 0x1.dd46ap-2f, 0x1.e24882p-2f, 0x1.e74426p-2f, 0x1.ec399ep-2f,
+ 0x1.f128f6p-2f, 0x1.f6124p-2f, 0x1.faf588p-2f, 0x1.ffd2ep-2f,
+ 0x1.02552ap-1f, 0x1.04bdfap-1f, 0x1.0723e6p-1f, 0x1.0986f4p-1f,
+ 0x1.0be72ep-1f, 0x1.0e4498p-1f, 0x1.109f3ap-1f, 0x1.12f71ap-1f,
+ 0x1.154c3ep-1f, 0x1.179eacp-1f, 0x1.19ee6cp-1f, 0x1.1c3b82p-1f,
+ 0x1.1e85f6p-1f, 0x1.20cdcep-1f, 0x1.23130ep-1f, 0x1.2555bcp-1f,
+ 0x1.2795e2p-1f, 0x1.29d38p-1f, 0x1.2c0e9ep-1f, 0x1.2e4744p-1f,
+ 0x1.307d74p-1f, 0x1.32b134p-1f, 0x1.34e28ap-1f, 0x1.37117cp-1f,
+ 0x1.393e0ep-1f, 0x1.3b6844p-1f, 0x1.3d9026p-1f, 0x1.3fb5b8p-1f,
+ 0x1.41d8fep-1f, 0x1.43f9fep-1f, 0x1.4618bcp-1f, 0x1.48353ep-1f,
+ 0x1.4a4f86p-1f, 0x1.4c679ap-1f, 0x1.4e7d82p-1f, 0x1.50913cp-1f,
+ 0x1.52a2d2p-1f, 0x1.54b246p-1f, 0x1.56bf9ep-1f, 0x1.58cadcp-1f,
+ 0x1.5ad404p-1f, 0x1.5cdb1ep-1f, 0x1.5ee02ap-1f, 0x1.60e33p-1f,
+};
+
+LLVM_LIBC_FUNCTION(bfloat16, log_bf16, (bfloat16 x)) {
+ using FPBits = fputil::FPBits<bfloat16>;
+ FPBits x_bits(x);
+
+ uint16_t x_u = x_bits.uintval();
+
+ // If x <= 0, or x is 1, or x is +inf, or x is NaN.
+ if (LIBC_UNLIKELY(x_u == 0U || x_u == 0x3f80U || x_u >= 0x7f80U)) {
+ // log(NaN) = NaN
+ if (x_bits.is_nan()) {
+ if (x_bits.is_signaling_nan()) {
+ fputil::raise_except_if_required(FE_INVALID);
+ return FPBits::quiet_nan().get_val();
+ }
+
+ return x;
+ }
+
+ // log(+/-0) = −inf
+ if ((x_u & 0x7fffU) == 0U) {
+ fputil::raise_except_if_required(FE_DIVBYZERO);
+ return FPBits::inf(Sign::NEG).get_val();
+ }
+
+ // log(1) = 0
+ if (x_u == 0x3f80U)
+ return FPBits::zero().get_val();
+
+ // x < 0
+ if (x_u > 0x8000U) {
+ fputil::set_errno_if_required(EDOM);
+ fputil::raise_except_if_required(FE_INVALID);
+ return FPBits::quiet_nan().get_val();
+ }
+
+ // log(+inf) = +inf
+ return FPBits::inf().get_val();
+ }
+
+#ifndef LIBC_TARGET_CPU_HAS_FMA
+ // log(0.00000000000000171390679426508540927898138761520386)
+ // ~= -34.00000095
+ if (LIBC_UNLIKELY(x_u == 0x26F7U))
+ return bfloat16(-34.0000009);
+#endif // LIBC_TARGET_CPU_HAS_FMA
+
+ int e = -FPBits::EXP_BIAS;
+
+ // When x is subnormal, normalize it.
+ if ((x_u & FPBits::EXP_MASK) == 0U) {
+ // Can't pass an integer to fputil::cast directly.
+ constexpr float NORMALIZE_EXP = 1U << FPBits::FRACTION_LEN;
+ x_bits = FPBits(x_bits.get_val() * fputil::cast<bfloat16>(NORMALIZE_EXP));
+ x_u = x_bits.uintval();
+ e -= FPBits::FRACTION_LEN;
+ }
+
+ // To compute log(x), we perform the following range reduction:
+ // x = 2^e * (1 + m),
+ // log(x) = e * log(2) + log(1 + m).
+ // for BFloat16, mantissa is at most 7 explicit bits, so we lookup
+ // log(1 + m) in LOG_1_PLUS_M table using `m` as key.
+
+ // Get the 7-bit mantissa directly as the table index
+ uint16_t m = x_bits.get_mantissa();
+
+ // Get unbiased exponent
+ e += x_u >> FPBits::FRACTION_LEN;
+
+ return fputil::cast<bfloat16>(
+ fputil::multiply_add(static_cast<float>(e), LOGF_2, LOG_1_PLUS_M[m]));
+}
+
+} // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/math/log_bf16.h b/libc/src/math/log_bf16.h
new file mode 100644
index 0000000000000..6f6b8e4cc55fe
--- /dev/null
+++ b/libc/src/math/log_bf16.h
@@ -0,0 +1,21 @@
+//===-- Implementation header for BFloat16 log(x) function ------*- 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_LOG_BF16_H
+#define LLVM_LIBC_SRC_MATH_LOG_BF16_H
+
+#include "src/__support/macros/config.h"
+#include "src/__support/macros/properties/types.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+bfloat16 log_bf16(bfloat16 x);
+
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC_MATH_LOG_BF16_H
diff --git a/libc/test/src/math/CMakeLists.txt b/libc/test/src/math/CMakeLists.txt
index 9d644703a61ae..2d2d5287bb384 100644
--- a/libc/test/src/math/CMakeLists.txt
+++ b/libc/test/src/math/CMakeLists.txt
@@ -2041,6 +2041,18 @@ add_fp_unittest(
libc.src.math.logf16
)
+add_fp_unittest(
+ log_bf16_test
+ NEED_MPFR
+ SUITE
+ libc-math-unittests
+ SRCS
+ log_bf16_test.cpp
+ DEPENDS
+ libc.src.math.log_bf16
+ libc.src.__support.FPUtil.bfloat16
+)
+
add_fp_unittest(
log2_test
NEED_MPFR
diff --git a/libc/test/src/math/log_bf16_test.cpp b/libc/test/src/math/log_bf16_test.cpp
new file mode 100644
index 0000000000000..ab91ebef4342f
--- /dev/null
+++ b/libc/test/src/math/log_bf16_test.cpp
@@ -0,0 +1,41 @@
+//===-- Full range tests for BFloat16 log(x) 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/__support/FPUtil/bfloat16.h"
+#include "src/math/log_bf16.h"
+#include "test/UnitTest/FPMatcher.h"
+#include "test/UnitTest/Test.h"
+#include "utils/MPFRWrapper/MPFRUtils.h"
+
+using LlvmLibcLogBf16Test = LIBC_NAMESPACE::testing::FPTest<bfloat16>;
+
+namespace mpfr = LIBC_NAMESPACE::testing::mpfr;
+
+// range: [0, inf]
+static constexpr uint16_t POS_START = 0x0000U;
+static constexpr uint16_t POS_STOP = 0x7f80U;
+
+// range: [-0, -inf]
+static constexpr uint16_t NEG_START = 0x8000U;
+static constexpr uint16_t NEG_STOP = 0xff80U;
+
+TEST_F(LlvmLibcLogBf16Test, PositiveRange) {
+ for (uint16_t v = POS_START; v <= POS_STOP; ++v) {
+ bfloat16 x = FPBits(v).get_val();
+ EXPECT_MPFR_MATCH_ALL_ROUNDING(mpfr::Operation::Log, x,
+ LIBC_NAMESPACE::log_bf16(x), 0.5);
+ }
+}
+
+TEST_F(LlvmLibcLogBf16Test, NegativeRange) {
+ for (uint16_t v = NEG_START; v <= NEG_STOP; ++v) {
+ bfloat16 x = FPBits(v).get_val();
+ EXPECT_MPFR_MATCH_ALL_ROUNDING(mpfr::Operation::Log, x,
+ LIBC_NAMESPACE::log_bf16(x), 0.5);
+ }
+}
diff --git a/libc/test/src/math/smoke/CMakeLists.txt b/libc/test/src/math/smoke/CMakeLists.txt
index eadd5f0970722..42a97ba10b601 100644
--- a/libc/test/src/math/smoke/CMakeLists.txt
+++ b/libc/test/src/math/smoke/CMakeLists.txt
@@ -4342,6 +4342,19 @@ add_fp_unittest(
libc.src.__support.FPUtil.cast
)
+add_fp_unittest(
+ log_bf16_test
+ SUITE
+ libc-math-smoke-tests
+ SRCS
+ log_bf16_test.cpp
+ DEPENDS
+ libc.hdr.errno_macros
+ libc.hdr.fenv_macros
+ libc.src.math.log_bf16
+ libc.src.__support.FPUtil.bfloat16
+)
+
add_fp_unittest(
log2_test
SUITE
diff --git a/libc/test/src/math/smoke/log_bf16_test.cpp b/libc/test/src/math/smoke/log_bf16_test.cpp
new file mode 100644
index 0000000000000..ec3b8eb6205fa
--- /dev/null
+++ b/libc/test/src/math/smoke/log_bf16_test.cpp
@@ -0,0 +1,52 @@
+//===-- Unittests for BFloat16 log(x) 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 "hdr/errno_macros.h"
+#include "hdr/fenv_macros.h"
+#include "src/__support/FPUtil/bfloat16.h"
+#include "src/__support/macros/properties/types.h"
+#include "src/math/log_bf16.h"
+#include "test/UnitTest/FEnvSafeTest.h"
+#include "test/UnitTest/FPMatcher.h"
+#include "test/UnitTest/Test.h"
+
+class LlvmLibcLogBf16Test : public LIBC_NAMESPACE::testing::FEnvSafeTest {
+ DECLARE_SPECIAL_CONSTANTS(bfloat16)
+
+public:
+ void test_special_numbers() {
+ EXPECT_FP_EQ_ALL_ROUNDING(aNaN, LIBC_NAMESPACE::log_bf16(aNaN));
+ EXPECT_MATH_ERRNO(0);
+
+ EXPECT_FP_EQ_WITH_EXCEPTION(aNaN, LIBC_NAMESPACE::log_bf16(sNaN),
+ FE_INVALID);
+ EXPECT_MATH_ERRNO(0);
+
+ EXPECT_FP_EQ_ALL_ROUNDING(inf, LIBC_NAMESPACE::log_bf16(inf));
+ EXPECT_MATH_ERRNO(0);
+
+ EXPECT_FP_EQ_ALL_ROUNDING(aNaN, LIBC_NAMESPACE::log_bf16(neg_inf));
+ EXPECT_MATH_ERRNO(EDOM);
+
+ EXPECT_FP_EQ_WITH_EXCEPTION_ALL_ROUNDING(
+ neg_inf, LIBC_NAMESPACE::log_bf16(zero), FE_DIVBYZERO);
+ EXPECT_MATH_ERRNO(0);
+
+ EXPECT_FP_EQ_WITH_EXCEPTION_ALL_ROUNDING(
+ neg_inf, LIBC_NAMESPACE::log_bf16(neg_zero), FE_DIVBYZERO);
+ EXPECT_MATH_ERRNO(0);
+
+ EXPECT_FP_EQ_ALL_ROUNDING(zero, LIBC_NAMESPACE::log_bf16(bfloat16(1.0)));
+ EXPECT_MATH_ERRNO(0);
+
+ EXPECT_FP_EQ_ALL_ROUNDING(aNaN, LIBC_NAMESPACE::log_bf16(bfloat16(-1.0)));
+ EXPECT_MATH_ERRNO(EDOM);
+ }
+};
+
+TEST_F(LlvmLibcLogBf16Test, SpecialNumbers) { test_special_numbers(); }
More information about the libc-commits
mailing list