[libc-commits] [libc] [libc][math][c23] Add {ceil, floor, round, roundeven, trunc}f16 C23 math functions (PR #94001)
via libc-commits
libc-commits at lists.llvm.org
Mon Jun 3 04:30:16 PDT 2024
https://github.com/overmighty updated https://github.com/llvm/llvm-project/pull/94001
>From e9644ef93098a5c2e3fccf61c714b32d1a91e658 Mon Sep 17 00:00:00 2001
From: OverMighty <its.overmighty at gmail.com>
Date: Fri, 31 May 2024 20:01:34 +0200
Subject: [PATCH 1/7] [libc][math][c23] Add floorf16 C23 math function
---
libc/config/linux/aarch64/entrypoints.txt | 1 +
libc/config/linux/x86_64/entrypoints.txt | 1 +
libc/spec/stdc.td | 1 +
.../FPUtil/NearestIntegerOperations.h | 10 ++++++++--
libc/src/math/CMakeLists.txt | 1 +
libc/src/math/floorf16.h | 20 +++++++++++++++++++
libc/src/math/generic/CMakeLists.txt | 13 ++++++++++++
libc/src/math/generic/floorf16.cpp | 17 ++++++++++++++++
libc/test/src/math/smoke/CMakeLists.txt | 12 +++++++++++
libc/test/src/math/smoke/FloorTest.h | 8 ++++----
libc/test/src/math/smoke/floorf16_test.cpp | 13 ++++++++++++
11 files changed, 91 insertions(+), 6 deletions(-)
create mode 100644 libc/src/math/floorf16.h
create mode 100644 libc/src/math/generic/floorf16.cpp
create mode 100644 libc/test/src/math/smoke/floorf16_test.cpp
diff --git a/libc/config/linux/aarch64/entrypoints.txt b/libc/config/linux/aarch64/entrypoints.txt
index ca0418c3618ae..cbe1a85b6cda0 100644
--- a/libc/config/linux/aarch64/entrypoints.txt
+++ b/libc/config/linux/aarch64/entrypoints.txt
@@ -500,6 +500,7 @@ if(LIBC_TYPES_HAS_FLOAT16)
list(APPEND TARGET_LIBM_ENTRYPOINTS
# math.h C23 _Float16 entrypoints
libc.src.math.fabsf16
+ libc.src.math.floorf16
)
endif()
diff --git a/libc/config/linux/x86_64/entrypoints.txt b/libc/config/linux/x86_64/entrypoints.txt
index 367db7d384d23..6e4a2aacefc63 100644
--- a/libc/config/linux/x86_64/entrypoints.txt
+++ b/libc/config/linux/x86_64/entrypoints.txt
@@ -533,6 +533,7 @@ if(LIBC_TYPES_HAS_FLOAT16)
list(APPEND TARGET_LIBM_ENTRYPOINTS
# math.h C23 _Float16 entrypoints
libc.src.math.fabsf16
+ libc.src.math.floorf16
)
endif()
diff --git a/libc/spec/stdc.td b/libc/spec/stdc.td
index 109721b8b12a0..7b5389630e0c7 100644
--- a/libc/spec/stdc.td
+++ b/libc/spec/stdc.td
@@ -406,6 +406,7 @@ def StdC : StandardSpec<"stdc"> {
FunctionSpec<"floor", RetValSpec<DoubleType>, [ArgSpec<DoubleType>]>,
FunctionSpec<"floorf", RetValSpec<FloatType>, [ArgSpec<FloatType>]>,
FunctionSpec<"floorl", RetValSpec<LongDoubleType>, [ArgSpec<LongDoubleType>]>,
+ GuardedFunctionSpec<"floorf16", RetValSpec<Float16Type>, [ArgSpec<Float16Type>], "LIBC_TYPES_HAS_FLOAT16">,
GuardedFunctionSpec<"floorf128", RetValSpec<Float128Type>, [ArgSpec<Float128Type>], "LIBC_TYPES_HAS_FLOAT128">,
FunctionSpec<"fmin", RetValSpec<DoubleType>, [ArgSpec<DoubleType>, ArgSpec<DoubleType>]>,
diff --git a/libc/src/__support/FPUtil/NearestIntegerOperations.h b/libc/src/__support/FPUtil/NearestIntegerOperations.h
index 4645ab0b5350b..2b9587c641328 100644
--- a/libc/src/__support/FPUtil/NearestIntegerOperations.h
+++ b/libc/src/__support/FPUtil/NearestIntegerOperations.h
@@ -22,6 +22,7 @@ namespace fputil {
template <typename T, cpp::enable_if_t<cpp::is_floating_point_v<T>, int> = 0>
LIBC_INLINE T trunc(T x) {
+ using StorageType = typename FPBits<T>::StorageType;
FPBits<T> bits(x);
// If x is infinity or NaN, return it.
@@ -43,12 +44,15 @@ LIBC_INLINE T trunc(T x) {
return FPBits<T>::zero(bits.sign()).get_val();
int trim_size = FPBits<T>::FRACTION_LEN - exponent;
- bits.set_mantissa((bits.get_mantissa() >> trim_size) << trim_size);
+ StorageType trunc_mantissa =
+ static_cast<StorageType>((bits.get_mantissa() >> trim_size) << trim_size);
+ bits.set_mantissa(trunc_mantissa);
return bits.get_val();
}
template <typename T, cpp::enable_if_t<cpp::is_floating_point_v<T>, int> = 0>
LIBC_INLINE T ceil(T x) {
+ using StorageType = typename FPBits<T>::StorageType;
FPBits<T> bits(x);
// If x is infinity NaN or zero, return it.
@@ -71,7 +75,9 @@ LIBC_INLINE T ceil(T x) {
}
uint32_t trim_size = FPBits<T>::FRACTION_LEN - exponent;
- bits.set_mantissa((bits.get_mantissa() >> trim_size) << trim_size);
+ StorageType trunc_mantissa =
+ static_cast<StorageType>((bits.get_mantissa() >> trim_size) << trim_size);
+ bits.set_mantissa(trunc_mantissa);
T trunc_value = bits.get_val();
// If x is already an integer, return it.
diff --git a/libc/src/math/CMakeLists.txt b/libc/src/math/CMakeLists.txt
index 31df5d0ab8809..e880365073a43 100644
--- a/libc/src/math/CMakeLists.txt
+++ b/libc/src/math/CMakeLists.txt
@@ -110,6 +110,7 @@ add_math_entrypoint_object(fdimf128)
add_math_entrypoint_object(floor)
add_math_entrypoint_object(floorf)
add_math_entrypoint_object(floorl)
+add_math_entrypoint_object(floorf16)
add_math_entrypoint_object(floorf128)
add_math_entrypoint_object(fma)
diff --git a/libc/src/math/floorf16.h b/libc/src/math/floorf16.h
new file mode 100644
index 0000000000000..98dd212c5b689
--- /dev/null
+++ b/libc/src/math/floorf16.h
@@ -0,0 +1,20 @@
+//===-- floorf16 ------------------------------------------------*- 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_FLOORF16_H
+#define LLVM_LIBC_SRC_MATH_FLOORF16_H
+
+#include "src/__support/macros/properties/types.h"
+
+namespace LIBC_NAMESPACE {
+
+float16 floorf16(float16 x);
+
+} // namespace LIBC_NAMESPACE
+
+#endif // LLVM_LIBC_SRC_MATH_FLOORF16_H
diff --git a/libc/src/math/generic/CMakeLists.txt b/libc/src/math/generic/CMakeLists.txt
index 04656e3186181..4d4a7fa977ad2 100644
--- a/libc/src/math/generic/CMakeLists.txt
+++ b/libc/src/math/generic/CMakeLists.txt
@@ -352,6 +352,19 @@ add_entrypoint_object(
libc.src.__support.FPUtil.nearest_integer_operations
)
+add_entrypoint_object(
+ floorf16
+ SRCS
+ floorf16.cpp
+ HDRS
+ ../floorf16.h
+ COMPILE_OPTIONS
+ -O3
+ DEPENDS
+ libc.src.__support.macros.properties.types
+ libc.src.__support.FPUtil.nearest_integer_operations
+)
+
add_entrypoint_object(
floorf128
SRCS
diff --git a/libc/src/math/generic/floorf16.cpp b/libc/src/math/generic/floorf16.cpp
new file mode 100644
index 0000000000000..ec0657ebc1081
--- /dev/null
+++ b/libc/src/math/generic/floorf16.cpp
@@ -0,0 +1,17 @@
+//===-- floorf16 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/floorf16.h"
+#include "src/__support/FPUtil/NearestIntegerOperations.h"
+#include "src/__support/common.h"
+
+namespace LIBC_NAMESPACE {
+
+LLVM_LIBC_FUNCTION(float16, floorf16, (float16 x)) { return fputil::floor(x); }
+
+} // namespace LIBC_NAMESPACE
diff --git a/libc/test/src/math/smoke/CMakeLists.txt b/libc/test/src/math/smoke/CMakeLists.txt
index b46fe5e902c67..a54091955ef58 100644
--- a/libc/test/src/math/smoke/CMakeLists.txt
+++ b/libc/test/src/math/smoke/CMakeLists.txt
@@ -320,6 +320,18 @@ add_fp_unittest(
libc.src.__support.FPUtil.fp_bits
)
+add_fp_unittest(
+ floorf16_test
+ SUITE
+ libc-math-smoke-tests
+ SRCS
+ floorf16_test.cpp
+ HDRS
+ FloorTest.h
+ DEPENDS
+ libc.src.math.floorf16
+)
+
add_fp_unittest(
floorf128_test
SUITE
diff --git a/libc/test/src/math/smoke/FloorTest.h b/libc/test/src/math/smoke/FloorTest.h
index b2102459bc3de..bc19e4f285915 100644
--- a/libc/test/src/math/smoke/FloorTest.h
+++ b/libc/test/src/math/smoke/FloorTest.h
@@ -59,10 +59,10 @@ class FloorTest : public LIBC_NAMESPACE::testing::FEnvSafeTest {
EXPECT_FP_EQ(T(-11.0), func(T(-10.32)));
EXPECT_FP_EQ(T(10.0), func(T(10.65)));
EXPECT_FP_EQ(T(-11.0), func(T(-10.65)));
- EXPECT_FP_EQ(T(1234.0), func(T(1234.38)));
- EXPECT_FP_EQ(T(-1235.0), func(T(-1234.38)));
- EXPECT_FP_EQ(T(1234.0), func(T(1234.96)));
- EXPECT_FP_EQ(T(-1235.0), func(T(-1234.96)));
+ EXPECT_FP_EQ(T(123.0), func(T(123.38)));
+ EXPECT_FP_EQ(T(-124.0), func(T(-123.38)));
+ EXPECT_FP_EQ(T(123.0), func(T(123.96)));
+ EXPECT_FP_EQ(T(-124.0), func(T(-123.96)));
}
};
diff --git a/libc/test/src/math/smoke/floorf16_test.cpp b/libc/test/src/math/smoke/floorf16_test.cpp
new file mode 100644
index 0000000000000..ca5160e927035
--- /dev/null
+++ b/libc/test/src/math/smoke/floorf16_test.cpp
@@ -0,0 +1,13 @@
+//===-- Unittests for floorf16 --------------------------------------------===//
+//
+// 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 "FloorTest.h"
+
+#include "src/math/floorf16.h"
+
+LIST_FLOOR_TESTS(float16, LIBC_NAMESPACE::floorf16)
>From 4bb3c31cc71c583fbaad321a95c0219811612a27 Mon Sep 17 00:00:00 2001
From: OverMighty <its.overmighty at gmail.com>
Date: Fri, 31 May 2024 18:48:51 +0200
Subject: [PATCH 2/7] [libc][math][c23] Add ceilf16 C23 math function
---
libc/config/linux/aarch64/entrypoints.txt | 1 +
libc/config/linux/x86_64/entrypoints.txt | 1 +
libc/spec/stdc.td | 1 +
libc/src/math/CMakeLists.txt | 1 +
libc/src/math/ceilf16.h | 20 ++++++++++++++++++++
libc/src/math/generic/CMakeLists.txt | 13 +++++++++++++
libc/src/math/generic/ceilf16.cpp | 17 +++++++++++++++++
libc/test/src/math/smoke/CMakeLists.txt | 12 ++++++++++++
libc/test/src/math/smoke/CeilTest.h | 8 ++++----
libc/test/src/math/smoke/ceilf16_test.cpp | 13 +++++++++++++
10 files changed, 83 insertions(+), 4 deletions(-)
create mode 100644 libc/src/math/ceilf16.h
create mode 100644 libc/src/math/generic/ceilf16.cpp
create mode 100644 libc/test/src/math/smoke/ceilf16_test.cpp
diff --git a/libc/config/linux/aarch64/entrypoints.txt b/libc/config/linux/aarch64/entrypoints.txt
index cbe1a85b6cda0..7a9155d8fd466 100644
--- a/libc/config/linux/aarch64/entrypoints.txt
+++ b/libc/config/linux/aarch64/entrypoints.txt
@@ -499,6 +499,7 @@ set(TARGET_LIBM_ENTRYPOINTS
if(LIBC_TYPES_HAS_FLOAT16)
list(APPEND TARGET_LIBM_ENTRYPOINTS
# math.h C23 _Float16 entrypoints
+ libc.src.math.ceilf16
libc.src.math.fabsf16
libc.src.math.floorf16
)
diff --git a/libc/config/linux/x86_64/entrypoints.txt b/libc/config/linux/x86_64/entrypoints.txt
index 6e4a2aacefc63..b1cca1fc6d8c7 100644
--- a/libc/config/linux/x86_64/entrypoints.txt
+++ b/libc/config/linux/x86_64/entrypoints.txt
@@ -532,6 +532,7 @@ set(TARGET_LIBM_ENTRYPOINTS
if(LIBC_TYPES_HAS_FLOAT16)
list(APPEND TARGET_LIBM_ENTRYPOINTS
# math.h C23 _Float16 entrypoints
+ libc.src.math.ceilf16
libc.src.math.fabsf16
libc.src.math.floorf16
)
diff --git a/libc/spec/stdc.td b/libc/spec/stdc.td
index 7b5389630e0c7..0cdc77167de25 100644
--- a/libc/spec/stdc.td
+++ b/libc/spec/stdc.td
@@ -390,6 +390,7 @@ def StdC : StandardSpec<"stdc"> {
FunctionSpec<"ceil", RetValSpec<DoubleType>, [ArgSpec<DoubleType>]>,
FunctionSpec<"ceilf", RetValSpec<FloatType>, [ArgSpec<FloatType>]>,
FunctionSpec<"ceill", RetValSpec<LongDoubleType>, [ArgSpec<LongDoubleType>]>,
+ GuardedFunctionSpec<"ceilf16", RetValSpec<Float16Type>, [ArgSpec<Float16Type>], "LIBC_TYPES_HAS_FLOAT16">,
GuardedFunctionSpec<"ceilf128", RetValSpec<Float128Type>, [ArgSpec<Float128Type>], "LIBC_TYPES_HAS_FLOAT128">,
FunctionSpec<"fabs", RetValSpec<DoubleType>, [ArgSpec<DoubleType>], [ConstAttr]>,
diff --git a/libc/src/math/CMakeLists.txt b/libc/src/math/CMakeLists.txt
index e880365073a43..cdb6212a88dda 100644
--- a/libc/src/math/CMakeLists.txt
+++ b/libc/src/math/CMakeLists.txt
@@ -67,6 +67,7 @@ add_math_entrypoint_object(canonicalizel)
add_math_entrypoint_object(ceil)
add_math_entrypoint_object(ceilf)
add_math_entrypoint_object(ceill)
+add_math_entrypoint_object(ceilf16)
add_math_entrypoint_object(ceilf128)
add_math_entrypoint_object(copysign)
diff --git a/libc/src/math/ceilf16.h b/libc/src/math/ceilf16.h
new file mode 100644
index 0000000000000..8263710cd183d
--- /dev/null
+++ b/libc/src/math/ceilf16.h
@@ -0,0 +1,20 @@
+//===-- ceilf16 -------------------------------------------------*- 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_CEILF16_H
+#define LLVM_LIBC_SRC_MATH_CEILF16_H
+
+#include "src/__support/macros/properties/types.h"
+
+namespace LIBC_NAMESPACE {
+
+float16 ceilf16(float16 x);
+
+} // namespace LIBC_NAMESPACE
+
+#endif // LLVM_LIBC_SRC_MATH_CEILF16_H
diff --git a/libc/src/math/generic/CMakeLists.txt b/libc/src/math/generic/CMakeLists.txt
index 4d4a7fa977ad2..736c18502be73 100644
--- a/libc/src/math/generic/CMakeLists.txt
+++ b/libc/src/math/generic/CMakeLists.txt
@@ -82,6 +82,19 @@ add_entrypoint_object(
libc.src.__support.FPUtil.nearest_integer_operations
)
+add_entrypoint_object(
+ ceilf16
+ SRCS
+ ceilf16.cpp
+ HDRS
+ ../ceilf16.h
+ COMPILE_OPTIONS
+ -O3
+ DEPENDS
+ libc.src.__support.macros.properties.types
+ libc.src.__support.FPUtil.nearest_integer_operations
+)
+
add_entrypoint_object(
ceilf128
SRCS
diff --git a/libc/src/math/generic/ceilf16.cpp b/libc/src/math/generic/ceilf16.cpp
new file mode 100644
index 0000000000000..634fe3015f6ce
--- /dev/null
+++ b/libc/src/math/generic/ceilf16.cpp
@@ -0,0 +1,17 @@
+//===-- ceilf16 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/ceilf16.h"
+#include "src/__support/FPUtil/NearestIntegerOperations.h"
+#include "src/__support/common.h"
+
+namespace LIBC_NAMESPACE {
+
+LLVM_LIBC_FUNCTION(float16, ceilf16, (float16 x)) { return fputil::ceil(x); }
+
+} // namespace LIBC_NAMESPACE
diff --git a/libc/test/src/math/smoke/CMakeLists.txt b/libc/test/src/math/smoke/CMakeLists.txt
index a54091955ef58..5fc6bd5a022a9 100644
--- a/libc/test/src/math/smoke/CMakeLists.txt
+++ b/libc/test/src/math/smoke/CMakeLists.txt
@@ -268,6 +268,18 @@ add_fp_unittest(
libc.src.__support.FPUtil.fp_bits
)
+add_fp_unittest(
+ ceilf16_test
+ SUITE
+ libc-math-smoke-tests
+ SRCS
+ ceilf16_test.cpp
+ HDRS
+ CeilTest.h
+ DEPENDS
+ libc.src.math.ceilf16
+)
+
add_fp_unittest(
ceilf128_test
SUITE
diff --git a/libc/test/src/math/smoke/CeilTest.h b/libc/test/src/math/smoke/CeilTest.h
index 5e108c0e0feea..7998eab62ec6b 100644
--- a/libc/test/src/math/smoke/CeilTest.h
+++ b/libc/test/src/math/smoke/CeilTest.h
@@ -59,10 +59,10 @@ class CeilTest : public LIBC_NAMESPACE::testing::FEnvSafeTest {
EXPECT_FP_EQ(T(-10.0), func(T(-10.32)));
EXPECT_FP_EQ(T(11.0), func(T(10.65)));
EXPECT_FP_EQ(T(-10.0), func(T(-10.65)));
- EXPECT_FP_EQ(T(1235.0), func(T(1234.38)));
- EXPECT_FP_EQ(T(-1234.0), func(T(-1234.38)));
- EXPECT_FP_EQ(T(1235.0), func(T(1234.96)));
- EXPECT_FP_EQ(T(-1234.0), func(T(-1234.96)));
+ EXPECT_FP_EQ(T(124.0), func(T(123.38)));
+ EXPECT_FP_EQ(T(-123.0), func(T(-123.38)));
+ EXPECT_FP_EQ(T(124.0), func(T(123.96)));
+ EXPECT_FP_EQ(T(-123.0), func(T(-123.96)));
}
};
diff --git a/libc/test/src/math/smoke/ceilf16_test.cpp b/libc/test/src/math/smoke/ceilf16_test.cpp
new file mode 100644
index 0000000000000..a6ec922836a75
--- /dev/null
+++ b/libc/test/src/math/smoke/ceilf16_test.cpp
@@ -0,0 +1,13 @@
+//===-- Unittests for ceilf16 ---------------------------------------------===//
+//
+// 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 "CeilTest.h"
+
+#include "src/math/ceilf16.h"
+
+LIST_CEIL_TESTS(float16, LIBC_NAMESPACE::ceilf16)
>From 4c393339827ebbbe71e1915c791e8c40bc1cd4b3 Mon Sep 17 00:00:00 2001
From: OverMighty <its.overmighty at gmail.com>
Date: Sat, 1 Jun 2024 16:35:36 +0200
Subject: [PATCH 3/7] [libc][math][c23] Update math functions implementation
status
---
libc/docs/math/index.rst | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/libc/docs/math/index.rst b/libc/docs/math/index.rst
index cd90b6ae85769..38b159031cce9 100644
--- a/libc/docs/math/index.rst
+++ b/libc/docs/math/index.rst
@@ -108,7 +108,7 @@ Basic Operations
+------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
| <Func> | <Func_f> (float) | <Func> (double) | <Func_l> (long double) | <Func_f16> (float16) | <Func_f128> (float128) | C23 Definition Section | C23 Error Handling Section |
+==================+==================+=================+========================+======================+========================+========================+============================+
-| ceil | |check| | |check| | |check| | | |check| | 7.12.9.1 | F.10.6.1 |
+| ceil | |check| | |check| | |check| | |check| | |check| | 7.12.9.1 | F.10.6.1 |
+------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
| canonicalize | |check| | |check| | |check| | | |check| | 7.12.11.7 | F.10.8.7 |
+------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
@@ -134,7 +134,7 @@ Basic Operations
+------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
| ffma | N/A | | | N/A | | 7.12.14.5 | F.10.11 |
+------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
-| floor | |check| | |check| | |check| | | |check| | 7.12.9.2 | F.10.6.2 |
+| floor | |check| | |check| | |check| | |check| | |check| | 7.12.9.2 | F.10.6.2 |
+------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
| fmax | |check| | |check| | |check| | | |check| | 7.12.12.2 | F.10.9.2 |
+------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
>From 18c746282e4a1155217ae9adb44d4090ee729909 Mon Sep 17 00:00:00 2001
From: OverMighty <its.overmighty at gmail.com>
Date: Sat, 1 Jun 2024 16:42:56 +0200
Subject: [PATCH 4/7] [libc][math][c23] Fix file headers for ceilf16 and
floorf16
---
libc/src/math/ceilf16.h | 2 +-
libc/src/math/floorf16.h | 2 +-
libc/src/math/generic/ceilf16.cpp | 2 +-
libc/src/math/generic/floorf16.cpp | 2 +-
4 files changed, 4 insertions(+), 4 deletions(-)
diff --git a/libc/src/math/ceilf16.h b/libc/src/math/ceilf16.h
index 8263710cd183d..36a6bd219d5b5 100644
--- a/libc/src/math/ceilf16.h
+++ b/libc/src/math/ceilf16.h
@@ -1,4 +1,4 @@
-//===-- ceilf16 -------------------------------------------------*- C++ -*-===//
+//===-- Implementation header for ceilf16 -----------------------*- C++ -*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
diff --git a/libc/src/math/floorf16.h b/libc/src/math/floorf16.h
index 98dd212c5b689..4e5e196105cf6 100644
--- a/libc/src/math/floorf16.h
+++ b/libc/src/math/floorf16.h
@@ -1,4 +1,4 @@
-//===-- floorf16 ------------------------------------------------*- C++ -*-===//
+//===-- Implementation header for floorf16 ----------------------*- C++ -*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
diff --git a/libc/src/math/generic/ceilf16.cpp b/libc/src/math/generic/ceilf16.cpp
index 634fe3015f6ce..205d7428f66e6 100644
--- a/libc/src/math/generic/ceilf16.cpp
+++ b/libc/src/math/generic/ceilf16.cpp
@@ -1,4 +1,4 @@
-//===-- ceilf16 function --------------------------------------------------===//
+//===-- Implementation of ceilf16 function --------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
diff --git a/libc/src/math/generic/floorf16.cpp b/libc/src/math/generic/floorf16.cpp
index ec0657ebc1081..db0b326c0e5f6 100644
--- a/libc/src/math/generic/floorf16.cpp
+++ b/libc/src/math/generic/floorf16.cpp
@@ -1,4 +1,4 @@
-//===-- floorf16 function -------------------------------------------------===//
+//===-- Implementation of floorf16 function -------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
>From d916947cdae52ad0b0c8e3753d77989b1d75e72a Mon Sep 17 00:00:00 2001
From: OverMighty <its.overmighty at gmail.com>
Date: Sat, 1 Jun 2024 22:14:42 +0200
Subject: [PATCH 5/7] [libc][math][c23] Add roundf16 C23 math function
---
libc/config/linux/aarch64/entrypoints.txt | 1 +
libc/config/linux/x86_64/entrypoints.txt | 1 +
libc/docs/math/index.rst | 2 +-
libc/spec/stdc.td | 1 +
.../FPUtil/NearestIntegerOperations.h | 4 +++-
libc/src/math/CMakeLists.txt | 1 +
libc/src/math/generic/CMakeLists.txt | 13 ++++++++++++
libc/src/math/generic/roundf16.cpp | 17 ++++++++++++++++
libc/src/math/roundf16.h | 20 +++++++++++++++++++
libc/test/src/math/smoke/CMakeLists.txt | 12 +++++++++++
libc/test/src/math/smoke/RoundTest.h | 8 ++++----
libc/test/src/math/smoke/roundf16_test.cpp | 13 ++++++++++++
12 files changed, 87 insertions(+), 6 deletions(-)
create mode 100644 libc/src/math/generic/roundf16.cpp
create mode 100644 libc/src/math/roundf16.h
create mode 100644 libc/test/src/math/smoke/roundf16_test.cpp
diff --git a/libc/config/linux/aarch64/entrypoints.txt b/libc/config/linux/aarch64/entrypoints.txt
index 7a9155d8fd466..cca28d69f6c12 100644
--- a/libc/config/linux/aarch64/entrypoints.txt
+++ b/libc/config/linux/aarch64/entrypoints.txt
@@ -502,6 +502,7 @@ if(LIBC_TYPES_HAS_FLOAT16)
libc.src.math.ceilf16
libc.src.math.fabsf16
libc.src.math.floorf16
+ libc.src.math.roundf16
)
endif()
diff --git a/libc/config/linux/x86_64/entrypoints.txt b/libc/config/linux/x86_64/entrypoints.txt
index b1cca1fc6d8c7..bc6b5fdfdc737 100644
--- a/libc/config/linux/x86_64/entrypoints.txt
+++ b/libc/config/linux/x86_64/entrypoints.txt
@@ -535,6 +535,7 @@ if(LIBC_TYPES_HAS_FLOAT16)
libc.src.math.ceilf16
libc.src.math.fabsf16
libc.src.math.floorf16
+ libc.src.math.roundf16
)
endif()
diff --git a/libc/docs/math/index.rst b/libc/docs/math/index.rst
index 38b159031cce9..e4eda84e1be5d 100644
--- a/libc/docs/math/index.rst
+++ b/libc/docs/math/index.rst
@@ -204,7 +204,7 @@ Basic Operations
+------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
| rint | |check| | |check| | |check| | | |check| | 7.12.9.4 | F.10.6.4 |
+------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
-| round | |check| | |check| | |check| | | |check| | 7.12.9.6 | F.10.6.6 |
+| round | |check| | |check| | |check| | |check| | |check| | 7.12.9.6 | F.10.6.6 |
+------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
| roundeven | |check| | |check| | |check| | | |check| | 7.12.9.8 | F.10.6.8 |
+------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
diff --git a/libc/spec/stdc.td b/libc/spec/stdc.td
index 0cdc77167de25..d192d327186c9 100644
--- a/libc/spec/stdc.td
+++ b/libc/spec/stdc.td
@@ -567,6 +567,7 @@ def StdC : StandardSpec<"stdc"> {
FunctionSpec<"round", RetValSpec<DoubleType>, [ArgSpec<DoubleType>]>,
FunctionSpec<"roundf", RetValSpec<FloatType>, [ArgSpec<FloatType>]>,
FunctionSpec<"roundl", RetValSpec<LongDoubleType>, [ArgSpec<LongDoubleType>]>,
+ GuardedFunctionSpec<"roundf16", RetValSpec<Float16Type>, [ArgSpec<Float16Type>], "LIBC_TYPES_HAS_FLOAT16">,
GuardedFunctionSpec<"roundf128", RetValSpec<Float128Type>, [ArgSpec<Float128Type>], "LIBC_TYPES_HAS_FLOAT128">,
FunctionSpec<"lround", RetValSpec<LongType>, [ArgSpec<DoubleType>]>,
diff --git a/libc/src/__support/FPUtil/NearestIntegerOperations.h b/libc/src/__support/FPUtil/NearestIntegerOperations.h
index 2b9587c641328..d436710d91a5d 100644
--- a/libc/src/__support/FPUtil/NearestIntegerOperations.h
+++ b/libc/src/__support/FPUtil/NearestIntegerOperations.h
@@ -130,7 +130,9 @@ LIBC_INLINE T round(T x) {
uint32_t trim_size = FPBits<T>::FRACTION_LEN - exponent;
bool half_bit_set =
bool(bits.get_mantissa() & (StorageType(1) << (trim_size - 1)));
- bits.set_mantissa((bits.get_mantissa() >> trim_size) << trim_size);
+ StorageType trunc_mantissa =
+ static_cast<StorageType>((bits.get_mantissa() >> trim_size) << trim_size);
+ bits.set_mantissa(trunc_mantissa);
T trunc_value = bits.get_val();
// If x is already an integer, return it.
diff --git a/libc/src/math/CMakeLists.txt b/libc/src/math/CMakeLists.txt
index cdb6212a88dda..515a78a35e639 100644
--- a/libc/src/math/CMakeLists.txt
+++ b/libc/src/math/CMakeLists.txt
@@ -295,6 +295,7 @@ add_math_entrypoint_object(rintf128)
add_math_entrypoint_object(round)
add_math_entrypoint_object(roundf)
add_math_entrypoint_object(roundl)
+add_math_entrypoint_object(roundf16)
add_math_entrypoint_object(roundf128)
add_math_entrypoint_object(roundeven)
diff --git a/libc/src/math/generic/CMakeLists.txt b/libc/src/math/generic/CMakeLists.txt
index 736c18502be73..0ad55574a8e7d 100644
--- a/libc/src/math/generic/CMakeLists.txt
+++ b/libc/src/math/generic/CMakeLists.txt
@@ -427,6 +427,19 @@ add_entrypoint_object(
libc.src.__support.FPUtil.nearest_integer_operations
)
+add_entrypoint_object(
+ roundf16
+ SRCS
+ roundf16.cpp
+ HDRS
+ ../roundf16.h
+ COMPILE_OPTIONS
+ -O3
+ DEPENDS
+ libc.src.__support.macros.properties.types
+ libc.src.__support.FPUtil.nearest_integer_operations
+)
+
add_entrypoint_object(
roundf128
SRCS
diff --git a/libc/src/math/generic/roundf16.cpp b/libc/src/math/generic/roundf16.cpp
new file mode 100644
index 0000000000000..75a255d7798d5
--- /dev/null
+++ b/libc/src/math/generic/roundf16.cpp
@@ -0,0 +1,17 @@
+//===-- Implementation of roundf16 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/roundf16.h"
+#include "src/__support/FPUtil/NearestIntegerOperations.h"
+#include "src/__support/common.h"
+
+namespace LIBC_NAMESPACE {
+
+LLVM_LIBC_FUNCTION(float16, roundf16, (float16 x)) { return fputil::round(x); }
+
+} // namespace LIBC_NAMESPACE
diff --git a/libc/src/math/roundf16.h b/libc/src/math/roundf16.h
new file mode 100644
index 0000000000000..e52f2aeb87e6d
--- /dev/null
+++ b/libc/src/math/roundf16.h
@@ -0,0 +1,20 @@
+//===-- Implementation header for roundf16 ----------------------*- 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_ROUNDF16_H
+#define LLVM_LIBC_SRC_MATH_ROUNDF16_H
+
+#include "src/__support/macros/properties/types.h"
+
+namespace LIBC_NAMESPACE {
+
+float16 roundf16(float16 x);
+
+} // namespace LIBC_NAMESPACE
+
+#endif // LLVM_LIBC_SRC_MATH_ROUNDF16_H
diff --git a/libc/test/src/math/smoke/CMakeLists.txt b/libc/test/src/math/smoke/CMakeLists.txt
index 5fc6bd5a022a9..37eaeb9383475 100644
--- a/libc/test/src/math/smoke/CMakeLists.txt
+++ b/libc/test/src/math/smoke/CMakeLists.txt
@@ -396,6 +396,18 @@ add_fp_unittest(
libc.src.__support.FPUtil.fp_bits
)
+add_fp_unittest(
+ roundf16_test
+ SUITE
+ libc-math-smoke-tests
+ SRCS
+ roundf16_test.cpp
+ HDRS
+ RoundTest.h
+ DEPENDS
+ libc.src.math.roundf16
+)
+
add_fp_unittest(
roundf128_test
SUITE
diff --git a/libc/test/src/math/smoke/RoundTest.h b/libc/test/src/math/smoke/RoundTest.h
index 36994f27eb4c0..beb70008c330b 100644
--- a/libc/test/src/math/smoke/RoundTest.h
+++ b/libc/test/src/math/smoke/RoundTest.h
@@ -59,10 +59,10 @@ class RoundTest : public LIBC_NAMESPACE::testing::FEnvSafeTest {
EXPECT_FP_EQ(T(-10.0), func(T(-10.32)));
EXPECT_FP_EQ(T(11.0), func(T(10.65)));
EXPECT_FP_EQ(T(-11.0), func(T(-10.65)));
- EXPECT_FP_EQ(T(1234.0), func(T(1234.38)));
- EXPECT_FP_EQ(T(-1234.0), func(T(-1234.38)));
- EXPECT_FP_EQ(T(1235.0), func(T(1234.96)));
- EXPECT_FP_EQ(T(-1235.0), func(T(-1234.96)));
+ EXPECT_FP_EQ(T(123.0), func(T(123.38)));
+ EXPECT_FP_EQ(T(-123.0), func(T(-123.38)));
+ EXPECT_FP_EQ(T(124.0), func(T(123.96)));
+ EXPECT_FP_EQ(T(-124.0), func(T(-123.96)));
}
};
diff --git a/libc/test/src/math/smoke/roundf16_test.cpp b/libc/test/src/math/smoke/roundf16_test.cpp
new file mode 100644
index 0000000000000..54ead855934db
--- /dev/null
+++ b/libc/test/src/math/smoke/roundf16_test.cpp
@@ -0,0 +1,13 @@
+//===-- Unittests for roundf16 --------------------------------------------===//
+//
+// 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 "RoundTest.h"
+
+#include "src/math/roundf16.h"
+
+LIST_ROUND_TESTS(float16, LIBC_NAMESPACE::roundf16)
>From 11b6cbdc1266dab49cf8606bf03705caf4b53355 Mon Sep 17 00:00:00 2001
From: OverMighty <its.overmighty at gmail.com>
Date: Sat, 1 Jun 2024 22:39:30 +0200
Subject: [PATCH 6/7] [libc][math][c23] Add roundevenf16 C23 math function
---
libc/config/linux/aarch64/entrypoints.txt | 1 +
libc/config/linux/x86_64/entrypoints.txt | 1 +
libc/docs/math/index.rst | 2 +-
libc/spec/stdc.td | 6 ++++++
.../FPUtil/NearestIntegerOperations.h | 7 +++++--
libc/src/math/CMakeLists.txt | 1 +
libc/src/math/generic/CMakeLists.txt | 13 ++++++++++++
libc/src/math/generic/roundevenf16.cpp | 19 ++++++++++++++++++
libc/src/math/roundevenf16.h | 20 +++++++++++++++++++
libc/test/src/math/smoke/CMakeLists.txt | 12 +++++++++++
libc/test/src/math/smoke/RoundEvenTest.h | 8 ++++----
.../test/src/math/smoke/roundevenf16_test.cpp | 13 ++++++++++++
12 files changed, 96 insertions(+), 7 deletions(-)
create mode 100644 libc/src/math/generic/roundevenf16.cpp
create mode 100644 libc/src/math/roundevenf16.h
create mode 100644 libc/test/src/math/smoke/roundevenf16_test.cpp
diff --git a/libc/config/linux/aarch64/entrypoints.txt b/libc/config/linux/aarch64/entrypoints.txt
index cca28d69f6c12..1ceb1d8a5c307 100644
--- a/libc/config/linux/aarch64/entrypoints.txt
+++ b/libc/config/linux/aarch64/entrypoints.txt
@@ -503,6 +503,7 @@ if(LIBC_TYPES_HAS_FLOAT16)
libc.src.math.fabsf16
libc.src.math.floorf16
libc.src.math.roundf16
+ libc.src.math.roundevenf16
)
endif()
diff --git a/libc/config/linux/x86_64/entrypoints.txt b/libc/config/linux/x86_64/entrypoints.txt
index bc6b5fdfdc737..999ff68efacb5 100644
--- a/libc/config/linux/x86_64/entrypoints.txt
+++ b/libc/config/linux/x86_64/entrypoints.txt
@@ -536,6 +536,7 @@ if(LIBC_TYPES_HAS_FLOAT16)
libc.src.math.fabsf16
libc.src.math.floorf16
libc.src.math.roundf16
+ libc.src.math.roundevenf16
)
endif()
diff --git a/libc/docs/math/index.rst b/libc/docs/math/index.rst
index e4eda84e1be5d..dc4ca1491f79e 100644
--- a/libc/docs/math/index.rst
+++ b/libc/docs/math/index.rst
@@ -206,7 +206,7 @@ Basic Operations
+------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
| round | |check| | |check| | |check| | |check| | |check| | 7.12.9.6 | F.10.6.6 |
+------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
-| roundeven | |check| | |check| | |check| | | |check| | 7.12.9.8 | F.10.6.8 |
+| roundeven | |check| | |check| | |check| | |check| | |check| | 7.12.9.8 | F.10.6.8 |
+------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
| scalbn | |check| | |check| | |check| | | |check| | 7.12.6.19 | F.10.3.19 |
+------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
diff --git a/libc/spec/stdc.td b/libc/spec/stdc.td
index d192d327186c9..2a6f0fc8aa84f 100644
--- a/libc/spec/stdc.td
+++ b/libc/spec/stdc.td
@@ -570,6 +570,12 @@ def StdC : StandardSpec<"stdc"> {
GuardedFunctionSpec<"roundf16", RetValSpec<Float16Type>, [ArgSpec<Float16Type>], "LIBC_TYPES_HAS_FLOAT16">,
GuardedFunctionSpec<"roundf128", RetValSpec<Float128Type>, [ArgSpec<Float128Type>], "LIBC_TYPES_HAS_FLOAT128">,
+ FunctionSpec<"roundeven", RetValSpec<DoubleType>, [ArgSpec<DoubleType>]>,
+ FunctionSpec<"roundevenf", RetValSpec<FloatType>, [ArgSpec<FloatType>]>,
+ FunctionSpec<"roundevenl", RetValSpec<LongDoubleType>, [ArgSpec<LongDoubleType>]>,
+ GuardedFunctionSpec<"roundevenf16", RetValSpec<Float16Type>, [ArgSpec<Float16Type>], "LIBC_TYPES_HAS_FLOAT16">,
+ GuardedFunctionSpec<"roundevenf128", RetValSpec<Float128Type>, [ArgSpec<Float128Type>], "LIBC_TYPES_HAS_FLOAT128">,
+
FunctionSpec<"lround", RetValSpec<LongType>, [ArgSpec<DoubleType>]>,
FunctionSpec<"lroundf", RetValSpec<LongType>, [ArgSpec<FloatType>]>,
FunctionSpec<"lroundl", RetValSpec<LongType>, [ArgSpec<LongDoubleType>]>,
diff --git a/libc/src/__support/FPUtil/NearestIntegerOperations.h b/libc/src/__support/FPUtil/NearestIntegerOperations.h
index d436710d91a5d..65fd9794dd66e 100644
--- a/libc/src/__support/FPUtil/NearestIntegerOperations.h
+++ b/libc/src/__support/FPUtil/NearestIntegerOperations.h
@@ -189,7 +189,9 @@ round_using_specific_rounding_mode(T x, int rnd) {
uint32_t trim_size = FPBits<T>::FRACTION_LEN - exponent;
FPBits<T> new_bits = bits;
- new_bits.set_mantissa((bits.get_mantissa() >> trim_size) << trim_size);
+ StorageType trunc_mantissa =
+ static_cast<StorageType>((bits.get_mantissa() >> trim_size) << trim_size);
+ new_bits.set_mantissa(trunc_mantissa);
T trunc_value = new_bits.get_val();
// If x is already an integer, return it.
@@ -198,7 +200,8 @@ round_using_specific_rounding_mode(T x, int rnd) {
StorageType trim_value =
bits.get_mantissa() & ((StorageType(1) << trim_size) - 1);
- StorageType half_value = (StorageType(1) << (trim_size - 1));
+ StorageType half_value =
+ static_cast<StorageType>((StorageType(1) << (trim_size - 1)));
// If exponent is 0, trimSize will be equal to the mantissa width, and
// truncIsOdd` will not be correct. So, we handle it as a special case
// below.
diff --git a/libc/src/math/CMakeLists.txt b/libc/src/math/CMakeLists.txt
index 515a78a35e639..b0a7d918b8dba 100644
--- a/libc/src/math/CMakeLists.txt
+++ b/libc/src/math/CMakeLists.txt
@@ -301,6 +301,7 @@ add_math_entrypoint_object(roundf128)
add_math_entrypoint_object(roundeven)
add_math_entrypoint_object(roundevenf)
add_math_entrypoint_object(roundevenl)
+add_math_entrypoint_object(roundevenf16)
add_math_entrypoint_object(roundevenf128)
add_math_entrypoint_object(scalbn)
diff --git a/libc/src/math/generic/CMakeLists.txt b/libc/src/math/generic/CMakeLists.txt
index 0ad55574a8e7d..70796145392e7 100644
--- a/libc/src/math/generic/CMakeLists.txt
+++ b/libc/src/math/generic/CMakeLists.txt
@@ -489,6 +489,19 @@ add_entrypoint_object(
libc.src.__support.FPUtil.nearest_integer_operations
)
+add_entrypoint_object(
+ roundevenf16
+ SRCS
+ roundevenf16.cpp
+ HDRS
+ ../roundevenf16.h
+ COMPILE_OPTIONS
+ -O3
+ DEPENDS
+ libc.src.__support.macros.properties.types
+ libc.src.__support.FPUtil.nearest_integer_operations
+)
+
add_entrypoint_object(
roundevenf128
SRCS
diff --git a/libc/src/math/generic/roundevenf16.cpp b/libc/src/math/generic/roundevenf16.cpp
new file mode 100644
index 0000000000000..9ecf79ce6f6c2
--- /dev/null
+++ b/libc/src/math/generic/roundevenf16.cpp
@@ -0,0 +1,19 @@
+//===-- Implementation of roundevenf16 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/roundevenf16.h"
+#include "src/__support/FPUtil/NearestIntegerOperations.h"
+#include "src/__support/common.h"
+
+namespace LIBC_NAMESPACE {
+
+LLVM_LIBC_FUNCTION(float16, roundevenf16, (float16 x)) {
+ return fputil::round_using_specific_rounding_mode(x, FP_INT_TONEAREST);
+}
+
+} // namespace LIBC_NAMESPACE
diff --git a/libc/src/math/roundevenf16.h b/libc/src/math/roundevenf16.h
new file mode 100644
index 0000000000000..382e2e39cff3f
--- /dev/null
+++ b/libc/src/math/roundevenf16.h
@@ -0,0 +1,20 @@
+//===-- Implementation header for roundevenf16 ------------------*- 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_ROUNDEVENF16_H
+#define LLVM_LIBC_SRC_MATH_ROUNDEVENF16_H
+
+#include "src/__support/macros/properties/types.h"
+
+namespace LIBC_NAMESPACE {
+
+float16 roundevenf16(float16 x);
+
+} // namespace LIBC_NAMESPACE
+
+#endif // LLVM_LIBC_SRC_MATH_ROUNDEVENF16_H
diff --git a/libc/test/src/math/smoke/CMakeLists.txt b/libc/test/src/math/smoke/CMakeLists.txt
index 37eaeb9383475..f4ebd78982c72 100644
--- a/libc/test/src/math/smoke/CMakeLists.txt
+++ b/libc/test/src/math/smoke/CMakeLists.txt
@@ -460,6 +460,18 @@ add_fp_unittest(
libc.src.__support.FPUtil.fp_bits
)
+add_fp_unittest(
+ roundevenf16_test
+ SUITE
+ libc-math-smoke-tests
+ SRCS
+ roundevenf16_test.cpp
+ HDRS
+ RoundEvenTest.h
+ DEPENDS
+ libc.src.math.roundevenf16
+)
+
add_fp_unittest(
roundevenf128_test
SUITE
diff --git a/libc/test/src/math/smoke/RoundEvenTest.h b/libc/test/src/math/smoke/RoundEvenTest.h
index 479b70912fedc..3477315e683a4 100644
--- a/libc/test/src/math/smoke/RoundEvenTest.h
+++ b/libc/test/src/math/smoke/RoundEvenTest.h
@@ -57,10 +57,10 @@ class RoundEvenTest : public LIBC_NAMESPACE::testing::FEnvSafeTest {
EXPECT_FP_EQ(T(-10.0), func(T(-10.50)));
EXPECT_FP_EQ(T(11.0), func(T(10.65)));
EXPECT_FP_EQ(T(-11.0), func(T(-10.65)));
- EXPECT_FP_EQ(T(1234.0), func(T(1234.50)));
- EXPECT_FP_EQ(T(-1234.0), func(T(-1234.50)));
- EXPECT_FP_EQ(T(1236.0), func(T(1235.50)));
- EXPECT_FP_EQ(T(-1236.0), func(T(-1235.50)));
+ EXPECT_FP_EQ(T(124.0), func(T(124.50)));
+ EXPECT_FP_EQ(T(-124.0), func(T(-124.50)));
+ EXPECT_FP_EQ(T(126.0), func(T(125.50)));
+ EXPECT_FP_EQ(T(-126.0), func(T(-125.50)));
}
};
diff --git a/libc/test/src/math/smoke/roundevenf16_test.cpp b/libc/test/src/math/smoke/roundevenf16_test.cpp
new file mode 100644
index 0000000000000..911a32c9f73f4
--- /dev/null
+++ b/libc/test/src/math/smoke/roundevenf16_test.cpp
@@ -0,0 +1,13 @@
+//===-- Unittests for roundevenf16 ----------------------------------------===//
+//
+// 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 "RoundEvenTest.h"
+
+#include "src/math/roundevenf16.h"
+
+LIST_ROUNDEVEN_TESTS(float16, LIBC_NAMESPACE::roundevenf16)
>From 898ea454aee0fc21f7bf8d58c8b21ef672c0ee9f Mon Sep 17 00:00:00 2001
From: OverMighty <its.overmighty at gmail.com>
Date: Sat, 1 Jun 2024 22:46:00 +0200
Subject: [PATCH 7/7] [libc][math][c23] Add truncf16 C23 math function
---
libc/config/linux/aarch64/entrypoints.txt | 1 +
libc/config/linux/x86_64/entrypoints.txt | 1 +
libc/docs/math/index.rst | 2 +-
libc/spec/stdc.td | 1 +
libc/src/math/CMakeLists.txt | 1 +
libc/src/math/generic/CMakeLists.txt | 13 +++++++++++++
libc/src/math/generic/truncf16.cpp | 17 +++++++++++++++++
libc/src/math/truncf16.h | 20 ++++++++++++++++++++
libc/test/src/math/smoke/CMakeLists.txt | 12 ++++++++++++
libc/test/src/math/smoke/TruncTest.h | 8 ++++----
libc/test/src/math/smoke/truncf16_test.cpp | 13 +++++++++++++
11 files changed, 84 insertions(+), 5 deletions(-)
create mode 100644 libc/src/math/generic/truncf16.cpp
create mode 100644 libc/src/math/truncf16.h
create mode 100644 libc/test/src/math/smoke/truncf16_test.cpp
diff --git a/libc/config/linux/aarch64/entrypoints.txt b/libc/config/linux/aarch64/entrypoints.txt
index 1ceb1d8a5c307..3bbd1629e3d8d 100644
--- a/libc/config/linux/aarch64/entrypoints.txt
+++ b/libc/config/linux/aarch64/entrypoints.txt
@@ -504,6 +504,7 @@ if(LIBC_TYPES_HAS_FLOAT16)
libc.src.math.floorf16
libc.src.math.roundf16
libc.src.math.roundevenf16
+ libc.src.math.truncf16
)
endif()
diff --git a/libc/config/linux/x86_64/entrypoints.txt b/libc/config/linux/x86_64/entrypoints.txt
index 999ff68efacb5..62434298890f0 100644
--- a/libc/config/linux/x86_64/entrypoints.txt
+++ b/libc/config/linux/x86_64/entrypoints.txt
@@ -537,6 +537,7 @@ if(LIBC_TYPES_HAS_FLOAT16)
libc.src.math.floorf16
libc.src.math.roundf16
libc.src.math.roundevenf16
+ libc.src.math.truncf16
)
endif()
diff --git a/libc/docs/math/index.rst b/libc/docs/math/index.rst
index dc4ca1491f79e..9df7dcfc256db 100644
--- a/libc/docs/math/index.rst
+++ b/libc/docs/math/index.rst
@@ -210,7 +210,7 @@ Basic Operations
+------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
| scalbn | |check| | |check| | |check| | | |check| | 7.12.6.19 | F.10.3.19 |
+------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
-| trunc | |check| | |check| | |check| | | |check| | 7.12.9.9 | F.10.6.9 |
+| trunc | |check| | |check| | |check| | |check| | |check| | 7.12.9.9 | F.10.6.9 |
+------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
| ufromfp | |check| | |check| | |check| | | |check| | 7.12.9.10 | F.10.6.10 |
+------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
diff --git a/libc/spec/stdc.td b/libc/spec/stdc.td
index 2a6f0fc8aa84f..cacc91ce8789a 100644
--- a/libc/spec/stdc.td
+++ b/libc/spec/stdc.td
@@ -609,6 +609,7 @@ def StdC : StandardSpec<"stdc"> {
FunctionSpec<"trunc", RetValSpec<DoubleType>, [ArgSpec<DoubleType>]>,
FunctionSpec<"truncf", RetValSpec<FloatType>, [ArgSpec<FloatType>]>,
FunctionSpec<"truncl", RetValSpec<LongDoubleType>, [ArgSpec<LongDoubleType>]>,
+ GuardedFunctionSpec<"truncf16", RetValSpec<Float16Type>, [ArgSpec<Float16Type>], "LIBC_TYPES_HAS_FLOAT16">,
GuardedFunctionSpec<"truncf128", RetValSpec<Float128Type>, [ArgSpec<Float128Type>], "LIBC_TYPES_HAS_FLOAT128">,
FunctionSpec<"nearbyint", RetValSpec<DoubleType>, [ArgSpec<DoubleType>]>,
diff --git a/libc/src/math/CMakeLists.txt b/libc/src/math/CMakeLists.txt
index b0a7d918b8dba..3b07b0b8679c4 100644
--- a/libc/src/math/CMakeLists.txt
+++ b/libc/src/math/CMakeLists.txt
@@ -335,6 +335,7 @@ add_math_entrypoint_object(tgammaf)
add_math_entrypoint_object(trunc)
add_math_entrypoint_object(truncf)
add_math_entrypoint_object(truncl)
+add_math_entrypoint_object(truncf16)
add_math_entrypoint_object(truncf128)
add_math_entrypoint_object(ufromfp)
diff --git a/libc/src/math/generic/CMakeLists.txt b/libc/src/math/generic/CMakeLists.txt
index 70796145392e7..369616caa2565 100644
--- a/libc/src/math/generic/CMakeLists.txt
+++ b/libc/src/math/generic/CMakeLists.txt
@@ -316,6 +316,19 @@ add_entrypoint_object(
libc.src.__support.FPUtil.nearest_integer_operations
)
+add_entrypoint_object(
+ truncf16
+ SRCS
+ truncf16.cpp
+ HDRS
+ ../truncf16.h
+ COMPILE_OPTIONS
+ -O3
+ DEPENDS
+ libc.src.__support.macros.properties.types
+ libc.src.__support.FPUtil.nearest_integer_operations
+)
+
add_entrypoint_object(
truncf128
SRCS
diff --git a/libc/src/math/generic/truncf16.cpp b/libc/src/math/generic/truncf16.cpp
new file mode 100644
index 0000000000000..65bd57d810323
--- /dev/null
+++ b/libc/src/math/generic/truncf16.cpp
@@ -0,0 +1,17 @@
+//===-- Implementation of truncf16 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/truncf16.h"
+#include "src/__support/FPUtil/NearestIntegerOperations.h"
+#include "src/__support/common.h"
+
+namespace LIBC_NAMESPACE {
+
+LLVM_LIBC_FUNCTION(float16, truncf16, (float16 x)) { return fputil::trunc(x); }
+
+} // namespace LIBC_NAMESPACE
diff --git a/libc/src/math/truncf16.h b/libc/src/math/truncf16.h
new file mode 100644
index 0000000000000..6d65980461c49
--- /dev/null
+++ b/libc/src/math/truncf16.h
@@ -0,0 +1,20 @@
+//===-- Implementation header for truncf16 ----------------------*- 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_TRUNCF16_H
+#define LLVM_LIBC_SRC_MATH_TRUNCF16_H
+
+#include "src/__support/macros/properties/types.h"
+
+namespace LIBC_NAMESPACE {
+
+float16 truncf16(float16 x);
+
+} // namespace LIBC_NAMESPACE
+
+#endif // LLVM_LIBC_SRC_MATH_TRUNCF16_H
diff --git a/libc/test/src/math/smoke/CMakeLists.txt b/libc/test/src/math/smoke/CMakeLists.txt
index f4ebd78982c72..685a53d8e7c6a 100644
--- a/libc/test/src/math/smoke/CMakeLists.txt
+++ b/libc/test/src/math/smoke/CMakeLists.txt
@@ -156,6 +156,18 @@ add_fp_unittest(
libc.src.__support.FPUtil.fp_bits
)
+add_fp_unittest(
+ truncf16_test
+ SUITE
+ libc-math-smoke-tests
+ SRCS
+ truncf16_test.cpp
+ HDRS
+ TruncTest.h
+ DEPENDS
+ libc.src.math.truncf16
+)
+
add_fp_unittest(
truncf128_test
SUITE
diff --git a/libc/test/src/math/smoke/TruncTest.h b/libc/test/src/math/smoke/TruncTest.h
index 1d9c44dfb3748..49688e81707a1 100644
--- a/libc/test/src/math/smoke/TruncTest.h
+++ b/libc/test/src/math/smoke/TruncTest.h
@@ -59,10 +59,10 @@ class TruncTest : public LIBC_NAMESPACE::testing::FEnvSafeTest {
EXPECT_FP_EQ(T(-10.0), func(T(-10.32)));
EXPECT_FP_EQ(T(10.0), func(T(10.65)));
EXPECT_FP_EQ(T(-10.0), func(T(-10.65)));
- EXPECT_FP_EQ(T(1234.0), func(T(1234.38)));
- EXPECT_FP_EQ(T(-1234.0), func(T(-1234.38)));
- EXPECT_FP_EQ(T(1234.0), func(T(1234.96)));
- EXPECT_FP_EQ(T(-1234.0), func(T(-1234.96)));
+ EXPECT_FP_EQ(T(123.0), func(T(123.38)));
+ EXPECT_FP_EQ(T(-123.0), func(T(-123.38)));
+ EXPECT_FP_EQ(T(123.0), func(T(123.96)));
+ EXPECT_FP_EQ(T(-123.0), func(T(-123.96)));
}
};
diff --git a/libc/test/src/math/smoke/truncf16_test.cpp b/libc/test/src/math/smoke/truncf16_test.cpp
new file mode 100644
index 0000000000000..832d88ec84f8e
--- /dev/null
+++ b/libc/test/src/math/smoke/truncf16_test.cpp
@@ -0,0 +1,13 @@
+//===-- Unittests for truncf16 --------------------------------------------===//
+//
+// 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 "TruncTest.h"
+
+#include "src/math/truncf16.h"
+
+LIST_TRUNC_TESTS(float16, LIBC_NAMESPACE::truncf16)
More information about the libc-commits
mailing list