[libc][math][c++23] Add {frexp,ilogb,ldexp,llogb,logb}bf16 math functions (#154427)
This PR adds the following basic math functions for BFloat16 type along
with the tests:
- frexpbf16
- ilobbf16
- ldexpbf16
- llogbbf16
- logbbf16
---------
Signed-off-by: Krishna Pandey <kpandey81930@gmail.com>diff --git a/libc/config/baremetal/aarch64/entrypoints.txt b/libc/config/baremetal/aarch64/entrypoints.txt
index 1784e10..5649c2b 100644
--- a/libc/config/baremetal/aarch64/entrypoints.txt
+++ b/libc/config/baremetal/aarch64/entrypoints.txt
@@ -785,6 +785,7 @@
libc.src.math.fminimum_magbf16
libc.src.math.fminimum_mag_numbf16
libc.src.math.fminimum_numbf16
+ libc.src.math.frexpbf16
libc.src.math.llrintbf16
libc.src.math.llroundbf16
libc.src.math.lrintbf16
@@ -792,6 +793,10 @@
libc.src.math.fromfpbf16
libc.src.math.fromfpxbf16
libc.src.math.getpayloadbf16
+ libc.src.math.ilogbbf16
+ libc.src.math.ldexpbf16
+ libc.src.math.llogbbf16
+ libc.src.math.logbbf16
libc.src.math.nanbf16
libc.src.math.nearbyintbf16
libc.src.math.nextafterbf16
diff --git a/libc/config/baremetal/arm/entrypoints.txt b/libc/config/baremetal/arm/entrypoints.txt
index 9a58ebd..ad3ceab 100644
--- a/libc/config/baremetal/arm/entrypoints.txt
+++ b/libc/config/baremetal/arm/entrypoints.txt
@@ -788,6 +788,7 @@
libc.src.math.fminimum_magbf16
libc.src.math.fminimum_mag_numbf16
libc.src.math.fminimum_numbf16
+ libc.src.math.frexpbf16
libc.src.math.llrintbf16
libc.src.math.llroundbf16
libc.src.math.lrintbf16
@@ -795,6 +796,10 @@
libc.src.math.fromfpbf16
libc.src.math.fromfpxbf16
libc.src.math.getpayloadbf16
+ libc.src.math.ilogbbf16
+ libc.src.math.ldexpbf16
+ libc.src.math.llogbbf16
+ libc.src.math.logbbf16
libc.src.math.nanbf16
libc.src.math.nearbyintbf16
libc.src.math.nextafterbf16
diff --git a/libc/config/baremetal/riscv/entrypoints.txt b/libc/config/baremetal/riscv/entrypoints.txt
index 2422551..8c50fd1 100644
--- a/libc/config/baremetal/riscv/entrypoints.txt
+++ b/libc/config/baremetal/riscv/entrypoints.txt
@@ -788,6 +788,7 @@
libc.src.math.fminimum_magbf16
libc.src.math.fminimum_mag_numbf16
libc.src.math.fminimum_numbf16
+ libc.src.math.frexpbf16
libc.src.math.llrintbf16
libc.src.math.llroundbf16
libc.src.math.lrintbf16
@@ -795,6 +796,10 @@
libc.src.math.fromfpbf16
libc.src.math.fromfpxbf16
libc.src.math.getpayloadbf16
+ libc.src.math.ilogbbf16
+ libc.src.math.ldexpbf16
+ libc.src.math.llogbbf16
+ libc.src.math.logbbf16
libc.src.math.nanbf16
libc.src.math.nearbyintbf16
libc.src.math.nextafterbf16
diff --git a/libc/config/darwin/aarch64/entrypoints.txt b/libc/config/darwin/aarch64/entrypoints.txt
index a3e7aad..08bc10b 100644
--- a/libc/config/darwin/aarch64/entrypoints.txt
+++ b/libc/config/darwin/aarch64/entrypoints.txt
@@ -618,6 +618,7 @@
libc.src.math.fminimum_magbf16
libc.src.math.fminimum_mag_numbf16
libc.src.math.fminimum_numbf16
+ libc.src.math.frexpbf16
libc.src.math.llrintbf16
libc.src.math.llroundbf16
libc.src.math.lrintbf16
@@ -625,6 +626,10 @@
libc.src.math.fromfpbf16
libc.src.math.fromfpxbf16
libc.src.math.getpayloadbf16
+ libc.src.math.ilogbbf16
+ libc.src.math.ldexpbf16
+ libc.src.math.llogbbf16
+ libc.src.math.logbbf16
libc.src.math.nanbf16
libc.src.math.nearbyintbf16
libc.src.math.nextafterbf16
diff --git a/libc/config/darwin/x86_64/entrypoints.txt b/libc/config/darwin/x86_64/entrypoints.txt
index 2343a3d..49f4404 100644
--- a/libc/config/darwin/x86_64/entrypoints.txt
+++ b/libc/config/darwin/x86_64/entrypoints.txt
@@ -261,6 +261,7 @@
libc.src.math.fminimum_magbf16
libc.src.math.fminimum_mag_numbf16
libc.src.math.fminimum_numbf16
+ libc.src.math.frexpbf16
libc.src.math.llrintbf16
libc.src.math.llroundbf16
libc.src.math.lrintbf16
@@ -268,6 +269,10 @@
libc.src.math.fromfpbf16
libc.src.math.fromfpxbf16
libc.src.math.getpayloadbf16
+ libc.src.math.ilogbbf16
+ libc.src.math.ldexpbf16
+ libc.src.math.llogbbf16
+ libc.src.math.logbbf16
libc.src.math.nanbf16
libc.src.math.nearbyintbf16
libc.src.math.nextafterbf16
diff --git a/libc/config/gpu/amdgpu/entrypoints.txt b/libc/config/gpu/amdgpu/entrypoints.txt
index 7b9ee44..b557046 100644
--- a/libc/config/gpu/amdgpu/entrypoints.txt
+++ b/libc/config/gpu/amdgpu/entrypoints.txt
@@ -644,6 +644,7 @@
libc.src.math.fminimum_magbf16
libc.src.math.fminimum_mag_numbf16
libc.src.math.fminimum_numbf16
+ libc.src.math.frexpbf16
libc.src.math.llrintbf16
libc.src.math.llroundbf16
libc.src.math.lrintbf16
@@ -651,6 +652,10 @@
libc.src.math.fromfpbf16
libc.src.math.fromfpxbf16
libc.src.math.getpayloadbf16
+ libc.src.math.ilogbbf16
+ libc.src.math.ldexpbf16
+ libc.src.math.llogbbf16
+ libc.src.math.logbbf16
libc.src.math.nanbf16
libc.src.math.nearbyintbf16
libc.src.math.nextafterbf16
diff --git a/libc/config/gpu/nvptx/entrypoints.txt b/libc/config/gpu/nvptx/entrypoints.txt
index 5b724a3..f1eac1f 100644
--- a/libc/config/gpu/nvptx/entrypoints.txt
+++ b/libc/config/gpu/nvptx/entrypoints.txt
@@ -646,6 +646,7 @@
libc.src.math.fminimum_magbf16
libc.src.math.fminimum_mag_numbf16
libc.src.math.fminimum_numbf16
+ libc.src.math.frexpbf16
libc.src.math.llrintbf16
libc.src.math.llroundbf16
libc.src.math.lrintbf16
@@ -653,6 +654,10 @@
libc.src.math.fromfpbf16
libc.src.math.fromfpxbf16
libc.src.math.getpayloadbf16
+ libc.src.math.ilogbbf16
+ libc.src.math.ldexpbf16
+ libc.src.math.llogbbf16
+ libc.src.math.logbbf16
libc.src.math.nanbf16
libc.src.math.nearbyintbf16
libc.src.math.nextafterbf16
diff --git a/libc/config/linux/aarch64/entrypoints.txt b/libc/config/linux/aarch64/entrypoints.txt
index 1f2c58c..63471f9 100644
--- a/libc/config/linux/aarch64/entrypoints.txt
+++ b/libc/config/linux/aarch64/entrypoints.txt
@@ -873,6 +873,7 @@
libc.src.math.fminimum_magbf16
libc.src.math.fminimum_mag_numbf16
libc.src.math.fminimum_numbf16
+ libc.src.math.frexpbf16
libc.src.math.llrintbf16
libc.src.math.llroundbf16
libc.src.math.lrintbf16
@@ -880,6 +881,10 @@
libc.src.math.fromfpbf16
libc.src.math.fromfpxbf16
libc.src.math.getpayloadbf16
+ libc.src.math.ilogbbf16
+ libc.src.math.ldexpbf16
+ libc.src.math.llogbbf16
+ libc.src.math.logbbf16
libc.src.math.nanbf16
libc.src.math.nearbyintbf16
libc.src.math.nextafterbf16
diff --git a/libc/config/linux/arm/entrypoints.txt b/libc/config/linux/arm/entrypoints.txt
index daeeee5c..5f95c80 100644
--- a/libc/config/linux/arm/entrypoints.txt
+++ b/libc/config/linux/arm/entrypoints.txt
@@ -488,6 +488,7 @@
libc.src.math.fminimum_magbf16
libc.src.math.fminimum_mag_numbf16
libc.src.math.fminimum_numbf16
+ libc.src.math.frexpbf16
libc.src.math.llrintbf16
libc.src.math.llroundbf16
libc.src.math.lrintbf16
@@ -495,6 +496,10 @@
libc.src.math.fromfpbf16
libc.src.math.fromfpxbf16
libc.src.math.getpayloadbf16
+ libc.src.math.ilogbbf16
+ libc.src.math.ldexpbf16
+ libc.src.math.llogbbf16
+ libc.src.math.logbbf16
libc.src.math.nanbf16
libc.src.math.nearbyintbf16
libc.src.math.nextafterbf16
diff --git a/libc/config/linux/riscv/entrypoints.txt b/libc/config/linux/riscv/entrypoints.txt
index 3a42342..7c4a287 100644
--- a/libc/config/linux/riscv/entrypoints.txt
+++ b/libc/config/linux/riscv/entrypoints.txt
@@ -891,6 +891,7 @@
libc.src.math.fminimum_magbf16
libc.src.math.fminimum_mag_numbf16
libc.src.math.fminimum_numbf16
+ libc.src.math.frexpbf16
libc.src.math.llrintbf16
libc.src.math.llroundbf16
libc.src.math.lrintbf16
@@ -898,6 +899,10 @@
libc.src.math.fromfpbf16
libc.src.math.fromfpxbf16
libc.src.math.getpayloadbf16
+ libc.src.math.ilogbbf16
+ libc.src.math.ldexpbf16
+ libc.src.math.llogbbf16
+ libc.src.math.logbbf16
libc.src.math.nanbf16
libc.src.math.nearbyintbf16
libc.src.math.nextafterbf16
diff --git a/libc/config/linux/x86_64/entrypoints.txt b/libc/config/linux/x86_64/entrypoints.txt
index fc52bf6..5e8bfc6 100644
--- a/libc/config/linux/x86_64/entrypoints.txt
+++ b/libc/config/linux/x86_64/entrypoints.txt
@@ -924,6 +924,7 @@
libc.src.math.fminimum_magbf16
libc.src.math.fminimum_mag_numbf16
libc.src.math.fminimum_numbf16
+ libc.src.math.frexpbf16
libc.src.math.llrintbf16
libc.src.math.llroundbf16
libc.src.math.lrintbf16
@@ -931,6 +932,10 @@
libc.src.math.fromfpbf16
libc.src.math.fromfpxbf16
libc.src.math.getpayloadbf16
+ libc.src.math.ilogbbf16
+ libc.src.math.ldexpbf16
+ libc.src.math.llogbbf16
+ libc.src.math.logbbf16
libc.src.math.nanbf16
libc.src.math.nearbyintbf16
libc.src.math.nextafterbf16
diff --git a/libc/config/windows/entrypoints.txt b/libc/config/windows/entrypoints.txt
index 413a7a3..8a61845 100644
--- a/libc/config/windows/entrypoints.txt
+++ b/libc/config/windows/entrypoints.txt
@@ -334,6 +334,7 @@
libc.src.math.fminimum_magbf16
libc.src.math.fminimum_mag_numbf16
libc.src.math.fminimum_numbf16
+ libc.src.math.frexpbf16
libc.src.math.llrintbf16
libc.src.math.llroundbf16
libc.src.math.lrintbf16
@@ -341,6 +342,10 @@
libc.src.math.fromfpbf16
libc.src.math.fromfpxbf16
libc.src.math.getpayloadbf16
+ libc.src.math.ilogbbf16
+ libc.src.math.ldexpbf16
+ libc.src.math.llogbbf16
+ libc.src.math.logbbf16
libc.src.math.nanbf16
libc.src.math.nearbyintbf16
libc.src.math.nextafterbf16
diff --git a/libc/docs/headers/math/index.rst b/libc/docs/headers/math/index.rst
index b0fd807..6a2699e 100644
--- a/libc/docs/headers/math/index.rst
+++ b/libc/docs/headers/math/index.rst
@@ -179,7 +179,7 @@
+------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+------------------------+----------------------------+
| fmul | N/A | |check| | |check| | N/A | |check|\* | N/A | 7.12.14.3 | F.10.11 |
+------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+------------------------+----------------------------+
-| frexp | |check| | |check| | |check| | |check| | |check| | | 7.12.6.7 | F.10.3.7 |
+| frexp | |check| | |check| | |check| | |check| | |check| | |check| | 7.12.6.7 | F.10.3.7 |
+------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+------------------------+----------------------------+
| fromfp | |check| | |check| | |check| | |check| | |check| | |check| | 7.12.9.10 | F.10.6.10 |
+------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+------------------------+----------------------------+
@@ -189,21 +189,21 @@
+------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+------------------------+----------------------------+
| getpayload | |check| | |check| | |check| | |check| | |check| | |check| | F.10.13.1 | N/A |
+------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+------------------------+----------------------------+
-| ilogb | |check| | |check| | |check| | |check| | |check| | | 7.12.6.8 | F.10.3.8 |
+| ilogb | |check| | |check| | |check| | |check| | |check| | |check| | 7.12.6.8 | F.10.3.8 |
+------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+------------------------+----------------------------+
| iscanonical | |check| | |check| | |check| | |check| | |check| | | 7.12.3.2 | N/A |
+------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+------------------------+----------------------------+
| issignaling | |check| | |check| | |check| | |check| | |check| | | 7.12.3.8 | N/A |
+------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+------------------------+----------------------------+
-| ldexp | |check| | |check| | |check| | |check| | |check| | | 7.12.6.9 | F.10.3.9 |
+| ldexp | |check| | |check| | |check| | |check| | |check| | |check| | 7.12.6.9 | F.10.3.9 |
+------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+------------------------+----------------------------+
-| llogb | |check| | |check| | |check| | |check| | |check| | | 7.12.6.10 | F.10.3.10 |
+| llogb | |check| | |check| | |check| | |check| | |check| | |check| | 7.12.6.10 | F.10.3.10 |
+------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+------------------------+----------------------------+
| llrint | |check| | |check| | |check| | |check| | |check| | |check| | 7.12.9.5 | F.10.6.5 |
+------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+------------------------+----------------------------+
| llround | |check| | |check| | |check| | |check| | |check| | |check| | 7.12.9.7 | F.10.6.7 |
+------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+------------------------+----------------------------+
-| logb | |check| | |check| | |check| | |check| | |check| | | 7.12.6.17 | F.10.3.17 |
+| logb | |check| | |check| | |check| | |check| | |check| | |check| | 7.12.6.17 | F.10.3.17 |
+------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+------------------------+----------------------------+
| lrint | |check| | |check| | |check| | |check| | |check| | |check| | 7.12.9.5 | F.10.6.5 |
+------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+------------------------+----------------------------+
diff --git a/libc/src/__support/FPUtil/BasicOperations.h b/libc/src/__support/FPUtil/BasicOperations.h
index 994237b..2357b05 100644
--- a/libc/src/__support/FPUtil/BasicOperations.h
+++ b/libc/src/__support/FPUtil/BasicOperations.h
@@ -354,10 +354,7 @@
return static_cast<T>(payload_dfloat);
} else {
- if constexpr (cpp::is_same_v<T, bfloat16>)
- return T(static_cast<int>(payload));
- else
- return static_cast<T>(payload);
+ return static_cast<T>(payload);
}
}
diff --git a/libc/src/__support/FPUtil/bfloat16.h b/libc/src/__support/FPUtil/bfloat16.h
index ba97b42..b607fd6 100644
--- a/libc/src/__support/FPUtil/bfloat16.h
+++ b/libc/src/__support/FPUtil/bfloat16.h
@@ -46,6 +46,8 @@
xd(sign, 0, value);
bits = xd.template as<bfloat16, /*ShouldSignalExceptions=*/true>().bits;
+ } else if constexpr (cpp::is_convertible_v<T, BFloat16>) {
+ bits = value.operator BFloat16().bits;
} else {
bits = fputil::cast<bfloat16>(static_cast<float>(value)).bits;
}
diff --git a/libc/src/math/CMakeLists.txt b/libc/src/math/CMakeLists.txt
index 2fe3d19..cbafa63 100644
--- a/libc/src/math/CMakeLists.txt
+++ b/libc/src/math/CMakeLists.txt
@@ -308,6 +308,7 @@
add_math_entrypoint_object(frexpl)
add_math_entrypoint_object(frexpf16)
add_math_entrypoint_object(frexpf128)
+add_math_entrypoint_object(frexpbf16)
add_math_entrypoint_object(fromfp)
add_math_entrypoint_object(fromfpf)
@@ -343,6 +344,7 @@
add_math_entrypoint_object(ilogbl)
add_math_entrypoint_object(ilogbf16)
add_math_entrypoint_object(ilogbf128)
+add_math_entrypoint_object(ilogbbf16)
add_math_entrypoint_object(isnan)
add_math_entrypoint_object(isnanf)
@@ -359,12 +361,14 @@
add_math_entrypoint_object(llogbl)
add_math_entrypoint_object(llogbf16)
add_math_entrypoint_object(llogbf128)
+add_math_entrypoint_object(llogbbf16)
add_math_entrypoint_object(ldexp)
add_math_entrypoint_object(ldexpf)
add_math_entrypoint_object(ldexpl)
add_math_entrypoint_object(ldexpf16)
add_math_entrypoint_object(ldexpf128)
+add_math_entrypoint_object(ldexpbf16)
add_math_entrypoint_object(log10)
add_math_entrypoint_object(log10f)
@@ -386,6 +390,7 @@
add_math_entrypoint_object(logbl)
add_math_entrypoint_object(logbf16)
add_math_entrypoint_object(logbf128)
+add_math_entrypoint_object(logbbf16)
add_math_entrypoint_object(llrint)
add_math_entrypoint_object(llrintf)
diff --git a/libc/src/math/frexpbf16.h b/libc/src/math/frexpbf16.h
new file mode 100644
index 0000000..1e9bba1
--- /dev/null
+++ b/libc/src/math/frexpbf16.h
@@ -0,0 +1,21 @@
+//===-- Implementation header for frexpbf16 ---------------------*- 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_FREXPFB16_H
+#define LLVM_LIBC_SRC_MATH_FREXPFB16_H
+
+#include "src/__support/macros/config.h"
+#include "src/__support/macros/properties/types.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+bfloat16 frexpbf16(bfloat16 x, int *exp);
+
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC_MATH_FREXPFB16_H
diff --git a/libc/src/math/generic/CMakeLists.txt b/libc/src/math/generic/CMakeLists.txt
index 1a61ccb..7f4f250 100644
--- a/libc/src/math/generic/CMakeLists.txt
+++ b/libc/src/math/generic/CMakeLists.txt
@@ -1799,6 +1799,20 @@
)
add_entrypoint_object(
+ frexpbf16
+ SRCS
+ frexpbf16.cpp
+ HDRS
+ ../frexpbf16.h
+ DEPENDS
+ libc.src.__support.common
+ libc.src.__support.FPUtil.bfloat16
+ libc.src.__support.FPUtil.manipulation_functions
+ libc.src.__support.macros.config
+ libc.src.__support.macros.properties.types
+)
+
+add_entrypoint_object(
ilogb
SRCS
ilogb.cpp
@@ -1851,6 +1865,20 @@
)
add_entrypoint_object(
+ ilogbbf16
+ SRCS
+ ilogbbf16.cpp
+ HDRS
+ ../ilogbbf16.h
+ DEPENDS
+ libc.src.__support.common
+ libc.src.__support.FPUtil.bfloat16
+ libc.src.__support.FPUtil.manipulation_functions
+ libc.src.__support.macros.config
+ libc.src.__support.macros.properties.types
+)
+
+add_entrypoint_object(
llogb
SRCS
llogb.cpp
@@ -1903,6 +1931,20 @@
)
add_entrypoint_object(
+ llogbbf16
+ SRCS
+ llogbbf16.cpp
+ HDRS
+ ../llogbbf16.h
+ DEPENDS
+ libc.src.__support.common
+ libc.src.__support.FPUtil.bfloat16
+ libc.src.__support.FPUtil.manipulation_functions
+ libc.src.__support.macros.config
+ libc.src.__support.macros.properties.types
+)
+
+add_entrypoint_object(
ldexp
SRCS
ldexp.cpp
@@ -1952,6 +1994,20 @@
libc.src.__support.math.ldexpf128
)
+add_entrypoint_object(
+ ldexpbf16
+ SRCS
+ ldexpbf16.cpp
+ HDRS
+ ../ldexpbf16.h
+ DEPENDS
+ libc.src.__support.common
+ libc.src.__support.FPUtil.bfloat16
+ libc.src.__support.FPUtil.manipulation_functions
+ libc.src.__support.macros.config
+ libc.src.__support.macros.properties.types
+)
+
add_object_library(
common_constants
HDRS
@@ -2225,6 +2281,20 @@
)
add_entrypoint_object(
+ logbbf16
+ SRCS
+ logbbf16.cpp
+ HDRS
+ ../logbbf16.h
+ DEPENDS
+ libc.src.__support.common
+ libc.src.__support.FPUtil.bfloat16
+ libc.src.__support.FPUtil.manipulation_functions
+ libc.src.__support.macros.config
+ libc.src.__support.macros.properties.types
+)
+
+add_entrypoint_object(
modf
SRCS
modf.cpp
diff --git a/libc/src/math/generic/frexpbf16.cpp b/libc/src/math/generic/frexpbf16.cpp
new file mode 100644
index 0000000..004f64f
--- /dev/null
+++ b/libc/src/math/generic/frexpbf16.cpp
@@ -0,0 +1,21 @@
+//===-- Implementation of frexpbf16 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/frexpbf16.h"
+#include "src/__support/FPUtil/ManipulationFunctions.h"
+#include "src/__support/FPUtil/bfloat16.h"
+#include "src/__support/common.h"
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+LLVM_LIBC_FUNCTION(bfloat16, frexpbf16, (bfloat16 x, int *exp)) {
+ return fputil::frexp(x, *exp);
+}
+
+} // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/math/generic/ilogbbf16.cpp b/libc/src/math/generic/ilogbbf16.cpp
new file mode 100644
index 0000000..6811139
--- /dev/null
+++ b/libc/src/math/generic/ilogbbf16.cpp
@@ -0,0 +1,21 @@
+//===-- Implementation of ilogbbf16 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/ilogbbf16.h"
+#include "src/__support/FPUtil/ManipulationFunctions.h"
+#include "src/__support/FPUtil/bfloat16.h"
+#include "src/__support/common.h"
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+LLVM_LIBC_FUNCTION(int, ilogbbf16, (bfloat16 x)) {
+ return fputil::intlogb<int>(x);
+}
+
+} // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/math/generic/ldexpbf16.cpp b/libc/src/math/generic/ldexpbf16.cpp
new file mode 100644
index 0000000..42a5039
--- /dev/null
+++ b/libc/src/math/generic/ldexpbf16.cpp
@@ -0,0 +1,21 @@
+//===-- Implementation of ldexpbf16 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/ldexpbf16.h"
+#include "src/__support/FPUtil/ManipulationFunctions.h"
+#include "src/__support/FPUtil/bfloat16.h"
+#include "src/__support/common.h"
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+LLVM_LIBC_FUNCTION(bfloat16, ldexpbf16, (bfloat16 x, int exp)) {
+ return fputil::ldexp(x, exp);
+}
+
+} // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/math/generic/llogbbf16.cpp b/libc/src/math/generic/llogbbf16.cpp
new file mode 100644
index 0000000..74c2762
--- /dev/null
+++ b/libc/src/math/generic/llogbbf16.cpp
@@ -0,0 +1,21 @@
+//===-- Implementation of llogbbf16 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/llogbbf16.h"
+#include "src/__support/FPUtil/ManipulationFunctions.h"
+#include "src/__support/FPUtil/bfloat16.h"
+#include "src/__support/common.h"
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+LLVM_LIBC_FUNCTION(long, llogbbf16, (bfloat16 x)) {
+ return fputil::intlogb<long>(x);
+}
+
+} // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/math/generic/logbbf16.cpp b/libc/src/math/generic/logbbf16.cpp
new file mode 100644
index 0000000..5a43ddf
--- /dev/null
+++ b/libc/src/math/generic/logbbf16.cpp
@@ -0,0 +1,19 @@
+//===-- Implementation of logbbf16 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/logbbf16.h"
+#include "src/__support/FPUtil/ManipulationFunctions.h"
+#include "src/__support/FPUtil/bfloat16.h"
+#include "src/__support/common.h"
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+LLVM_LIBC_FUNCTION(bfloat16, logbbf16, (bfloat16 x)) { return fputil::logb(x); }
+
+} // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/math/ilogbbf16.h b/libc/src/math/ilogbbf16.h
new file mode 100644
index 0000000..da2384b
--- /dev/null
+++ b/libc/src/math/ilogbbf16.h
@@ -0,0 +1,21 @@
+//===-- Implementation header for ilogbbf16 ---------------------*- 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_ILOGBBF16_H
+#define LLVM_LIBC_SRC_MATH_ILOGBBF16_H
+
+#include "src/__support/macros/config.h"
+#include "src/__support/macros/properties/types.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+int ilogbbf16(bfloat16 x);
+
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC_MATH_ILOGBBF16_H
diff --git a/libc/src/math/ldexpbf16.h b/libc/src/math/ldexpbf16.h
new file mode 100644
index 0000000..7436d8d
--- /dev/null
+++ b/libc/src/math/ldexpbf16.h
@@ -0,0 +1,21 @@
+//===-- Implementation header for ldexpbf16 ---------------------*- 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_LDEXPBF16_H
+#define LLVM_LIBC_SRC_MATH_LDEXPBF16_H
+
+#include "src/__support/macros/config.h"
+#include "src/__support/macros/properties/types.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+bfloat16 ldexpbf16(bfloat16 x, int exp);
+
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC_MATH_LDEXPBF16_H
diff --git a/libc/src/math/llogbbf16.h b/libc/src/math/llogbbf16.h
new file mode 100644
index 0000000..13f0570
--- /dev/null
+++ b/libc/src/math/llogbbf16.h
@@ -0,0 +1,21 @@
+//===-- Implementation header for llogbbf16 ---------------------*- 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_LLOGBBF16_H
+#define LLVM_LIBC_SRC_MATH_LLOGBBF16_H
+
+#include "src/__support/macros/config.h"
+#include "src/__support/macros/properties/types.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+long llogbbf16(bfloat16 x);
+
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC_MATH_LLOGBBF16_H
diff --git a/libc/src/math/logbbf16.h b/libc/src/math/logbbf16.h
new file mode 100644
index 0000000..2c0d77e
--- /dev/null
+++ b/libc/src/math/logbbf16.h
@@ -0,0 +1,21 @@
+//===-- Implementation header for logbbf16 ----------------------*- 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_LOGBBF16_H
+#define LLVM_LIBC_SRC_MATH_LOGBBF16_H
+
+#include "src/__support/macros/config.h"
+#include "src/__support/macros/properties/types.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+bfloat16 logbbf16(bfloat16 x);
+
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC_MATH_LOGBBF16_H
diff --git a/libc/test/src/math/smoke/CMakeLists.txt b/libc/test/src/math/smoke/CMakeLists.txt
index 929d7b0..f9da396 100644
--- a/libc/test/src/math/smoke/CMakeLists.txt
+++ b/libc/test/src/math/smoke/CMakeLists.txt
@@ -1620,6 +1620,19 @@
)
add_fp_unittest(
+ frexpbf16_test
+ SUITE
+ libc-math-smoke-tests
+ SRCS
+ frexpbf16_test.cpp
+ HDRS
+ FrexpTest.h
+ DEPENDS
+ libc.src.math.frexpbf16
+ libc.src.__support.FPUtil.bfloat16
+)
+
+add_fp_unittest(
fromfp_test
SUITE
libc-math-smoke-tests
@@ -1988,6 +2001,22 @@
)
add_fp_unittest(
+ ilogbbf16_test
+ SUITE
+ libc-math-smoke-tests
+ SRCS
+ ilogbbf16_test.cpp
+ HDRS
+ ILogbTest.h
+ DEPENDS
+ libc.src.math.ilogbbf16
+ libc.src.__support.CPP.algorithm
+ libc.src.__support.FPUtil.bfloat16
+ libc.src.__support.FPUtil.fp_bits
+ libc.src.__support.FPUtil.manipulation_functions
+)
+
+add_fp_unittest(
issignaling_test
SUITE
libc-math-smoke-tests
@@ -2123,6 +2152,22 @@
)
add_fp_unittest(
+ llogbbf16_test
+ SUITE
+ libc-math-smoke-tests
+ SRCS
+ llogbbf16_test.cpp
+ HDRS
+ ILogbTest.h
+ DEPENDS
+ libc.src.math.llogbbf16
+ libc.src.__support.CPP.algorithm
+ libc.src.__support.FPUtil.bfloat16
+ libc.src.__support.FPUtil.fp_bits
+ libc.src.__support.FPUtil.manipulation_functions
+)
+
+add_fp_unittest(
ldexp_test
SUITE
libc-math-smoke-tests
@@ -2132,6 +2177,7 @@
LdExpTest.h
DEPENDS
libc.src.math.ldexp
+ libc.src.__support.CPP.algorithm
libc.src.__support.CPP.limits
libc.src.__support.FPUtil.fp_bits
libc.src.__support.FPUtil.normal_float
@@ -2147,6 +2193,7 @@
LdExpTest.h
DEPENDS
libc.src.math.ldexpf
+ libc.src.__support.CPP.algorithm
libc.src.__support.CPP.limits
libc.src.__support.FPUtil.fp_bits
libc.src.__support.FPUtil.normal_float
@@ -2162,6 +2209,7 @@
LdExpTest.h
DEPENDS
libc.src.math.ldexpl
+ libc.src.__support.CPP.algorithm
libc.src.__support.CPP.limits
libc.src.__support.FPUtil.fp_bits
libc.src.__support.FPUtil.normal_float
@@ -2177,6 +2225,7 @@
LdExpTest.h
DEPENDS
libc.src.math.ldexpf16
+ libc.src.__support.CPP.algorithm
libc.src.__support.CPP.limits
libc.src.__support.FPUtil.fp_bits
libc.src.__support.FPUtil.normal_float
@@ -2192,12 +2241,30 @@
LdExpTest.h
DEPENDS
libc.src.math.ldexpf128
+ libc.src.__support.CPP.algorithm
libc.src.__support.CPP.limits
libc.src.__support.FPUtil.fp_bits
libc.src.__support.FPUtil.normal_float
)
add_fp_unittest(
+ ldexpbf16_test
+ SUITE
+ libc-math-smoke-tests
+ SRCS
+ ldexpbf16_test.cpp
+ HDRS
+ LdExpTest.h
+ DEPENDS
+ libc.src.math.ldexpbf16
+ libc.src.__support.CPP.algorithm
+ libc.src.__support.CPP.limits
+ libc.src.__support.FPUtil.bfloat16
+ libc.src.__support.FPUtil.fp_bits
+ libc.src.__support.FPUtil.normal_float
+)
+
+add_fp_unittest(
logb_test
SUITE
libc-math-smoke-tests
@@ -2268,6 +2335,21 @@
)
add_fp_unittest(
+ logbbf16_test
+ SUITE
+ libc-math-smoke-tests
+ SRCS
+ logbbf16_test.cpp
+ HDRS
+ LogbTest.h
+ DEPENDS
+ libc.src.math.logbbf16
+ libc.src.__support.CPP.algorithm
+ libc.src.__support.FPUtil.bfloat16
+ libc.src.__support.FPUtil.manipulation_functions
+)
+
+add_fp_unittest(
modf_test
SUITE
libc-math-smoke-tests
diff --git a/libc/test/src/math/smoke/LdExpTest.h b/libc/test/src/math/smoke/LdExpTest.h
index 8de70ad..d005f05 100644
--- a/libc/test/src/math/smoke/LdExpTest.h
+++ b/libc/test/src/math/smoke/LdExpTest.h
@@ -10,7 +10,8 @@
#define LLVM_LIBC_TEST_SRC_MATH_LDEXPTEST_H
#include "hdr/stdint_proxy.h"
-#include "src/__support/CPP/limits.h" // INT_MAX
+#include "src/__support/CPP/algorithm.h" // cpp::min
+#include "src/__support/CPP/limits.h" // INT_MAX
#include "src/__support/FPUtil/FPBits.h"
#include "src/__support/FPUtil/NormalFloat.h"
#include "test/UnitTest/FEnvSafeTest.h"
@@ -135,8 +136,8 @@
// Normal which trigger mantissa overflow.
T x = NormalFloat(Sign::POS, -FPBits::EXP_BIAS + 1,
StorageType(2) * NormalFloat::ONE - StorageType(1));
- ASSERT_FP_EQ(func(x, -1), x / 2);
- ASSERT_FP_EQ(func(-x, -1), -x / 2);
+ ASSERT_FP_EQ(func(x, -1), T(x / 2));
+ ASSERT_FP_EQ(func(-x, -1), -T(x / 2));
// Start with a normal number high exponent but pass a very low number for
// exp. The result should be a subnormal number.
@@ -154,7 +155,9 @@
// Start with a subnormal number but pass a very high number for exponent.
// The result should not be infinity.
- x = NormalFloat(Sign::POS, -FPBits::EXP_BIAS + 1, NormalFloat::ONE >> 10);
+ x = NormalFloat(Sign::POS, -FPBits::EXP_BIAS + 1,
+ NormalFloat::ONE >>
+ LIBC_NAMESPACE::cpp::min(FPBits::FRACTION_LEN, 10));
exp = FPBits::MAX_BIASED_EXPONENT + 5;
ASSERT_FALSE(FPBits(func(x, exp)).is_inf());
// But if the exp is large enough to oversome than the normalization shift,
diff --git a/libc/test/src/math/smoke/frexpbf16_test.cpp b/libc/test/src/math/smoke/frexpbf16_test.cpp
new file mode 100644
index 0000000..3c80edb
--- /dev/null
+++ b/libc/test/src/math/smoke/frexpbf16_test.cpp
@@ -0,0 +1,14 @@
+//===-- Unittests for frexpbf16 -------------------------------------------===//
+//
+// 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 "FrexpTest.h"
+
+#include "src/__support/FPUtil/bfloat16.h"
+#include "src/math/frexpbf16.h"
+
+LIST_FREXP_TESTS(bfloat16, LIBC_NAMESPACE::frexpbf16);
diff --git a/libc/test/src/math/smoke/ilogbbf16_test.cpp b/libc/test/src/math/smoke/ilogbbf16_test.cpp
new file mode 100644
index 0000000..d9fa03a
--- /dev/null
+++ b/libc/test/src/math/smoke/ilogbbf16_test.cpp
@@ -0,0 +1,14 @@
+//===-- Unittests for ilogbbf16 -------------------------------------------===//
+//
+// 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 "ILogbTest.h"
+
+#include "src/__support/FPUtil/bfloat16.h"
+#include "src/math/ilogbbf16.h"
+
+LIST_INTLOGB_TESTS(int, bfloat16, LIBC_NAMESPACE::ilogbbf16);
diff --git a/libc/test/src/math/smoke/ldexpbf16_test.cpp b/libc/test/src/math/smoke/ldexpbf16_test.cpp
new file mode 100644
index 0000000..2e57eb8
--- /dev/null
+++ b/libc/test/src/math/smoke/ldexpbf16_test.cpp
@@ -0,0 +1,14 @@
+//===-- Unittests for ldexpbf16 -------------------------------------------===//
+//
+// 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 "LdExpTest.h"
+
+#include "src/__support/FPUtil/bfloat16.h"
+#include "src/math/ldexpbf16.h"
+
+LIST_LDEXP_TESTS(bfloat16, LIBC_NAMESPACE::ldexpbf16);
diff --git a/libc/test/src/math/smoke/llogbbf16_test.cpp b/libc/test/src/math/smoke/llogbbf16_test.cpp
new file mode 100644
index 0000000..4454fbf
--- /dev/null
+++ b/libc/test/src/math/smoke/llogbbf16_test.cpp
@@ -0,0 +1,14 @@
+//===-- Unittests for llogbbf16 -------------------------------------------===//
+//
+// 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 "ILogbTest.h"
+
+#include "src/__support/FPUtil/bfloat16.h"
+#include "src/math/llogbbf16.h"
+
+LIST_INTLOGB_TESTS(long, bfloat16, LIBC_NAMESPACE::llogbbf16);
diff --git a/libc/test/src/math/smoke/logbbf16_test.cpp b/libc/test/src/math/smoke/logbbf16_test.cpp
new file mode 100644
index 0000000..4ceec3d
--- /dev/null
+++ b/libc/test/src/math/smoke/logbbf16_test.cpp
@@ -0,0 +1,14 @@
+//===-- Unittests for logbbf16 --------------------------------------------===//
+//
+// 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 "LogbTest.h"
+
+#include "src/__support/FPUtil/bfloat16.h"
+#include "src/math/logbbf16.h"
+
+LIST_LOGB_TESTS(bfloat16, LIBC_NAMESPACE::logbbf16)