[libc-commits] [libc] [libc][math][c23] Add remainderf16 C23 math function (PR #94773)
via libc-commits
libc-commits at lists.llvm.org
Fri Jun 7 10:23:40 PDT 2024
https://github.com/overmighty created https://github.com/llvm/llvm-project/pull/94773
None
>From 932ade6538f8b6335069ff42afc2da5a6cd2d21c Mon Sep 17 00:00:00 2001
From: OverMighty <its.overmighty at gmail.com>
Date: Fri, 7 Jun 2024 18:50:48 +0200
Subject: [PATCH] [libc][math][c23] Add remainderf16 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 | 3 ++-
libc/src/__support/FPUtil/NormalFloat.h | 13 ++++++++-----
libc/src/math/CMakeLists.txt | 1 +
libc/src/math/generic/CMakeLists.txt | 13 +++++++++++++
libc/src/math/generic/remainderf16.cpp | 20 ++++++++++++++++++++
libc/src/math/remainderf16.h | 20 ++++++++++++++++++++
9 files changed, 67 insertions(+), 7 deletions(-)
create mode 100644 libc/src/math/generic/remainderf16.cpp
create mode 100644 libc/src/math/remainderf16.h
diff --git a/libc/config/linux/aarch64/entrypoints.txt b/libc/config/linux/aarch64/entrypoints.txt
index 33ecff813a1fb..596c2c0e3b9d4 100644
--- a/libc/config/linux/aarch64/entrypoints.txt
+++ b/libc/config/linux/aarch64/entrypoints.txt
@@ -529,6 +529,7 @@ if(LIBC_TYPES_HAS_FLOAT16)
# clang-12 and after: https://godbolt.org/z/8ceT9454c
# libc.src.math.nexttowardf16
libc.src.math.nextupf16
+ libc.src.math.remainderf16
libc.src.math.rintf16
libc.src.math.roundf16
libc.src.math.roundevenf16
diff --git a/libc/config/linux/x86_64/entrypoints.txt b/libc/config/linux/x86_64/entrypoints.txt
index e3ca544ae0185..6871ac8a0d279 100644
--- a/libc/config/linux/x86_64/entrypoints.txt
+++ b/libc/config/linux/x86_64/entrypoints.txt
@@ -558,6 +558,7 @@ if(LIBC_TYPES_HAS_FLOAT16)
libc.src.math.nextdownf16
libc.src.math.nexttowardf16
libc.src.math.nextupf16
+ libc.src.math.remainderf16
libc.src.math.rintf16
libc.src.math.roundf16
libc.src.math.roundevenf16
diff --git a/libc/docs/math/index.rst b/libc/docs/math/index.rst
index b9507f0887cd7..915d8fc4b7f91 100644
--- a/libc/docs/math/index.rst
+++ b/libc/docs/math/index.rst
@@ -198,7 +198,7 @@ Basic Operations
+------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
| nextup | |check| | |check| | |check| | |check| | |check| | 7.12.11.5 | F.10.8.5 |
+------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
-| remainder | |check| | |check| | |check| | | | 7.12.10.2 | F.10.7.2 |
+| remainder | |check| | |check| | |check| | |check| | | 7.12.10.2 | F.10.7.2 |
+------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
| remquo | |check| | |check| | |check| | | | 7.12.10.3 | F.10.7.3 |
+------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
diff --git a/libc/spec/stdc.td b/libc/spec/stdc.td
index 9a436c8ae38d2..7dae555048591 100644
--- a/libc/spec/stdc.td
+++ b/libc/spec/stdc.td
@@ -572,9 +572,10 @@ def StdC : StandardSpec<"stdc"> {
FunctionSpec<"exp10", RetValSpec<DoubleType>, [ArgSpec<DoubleType>]>,
FunctionSpec<"exp10f", RetValSpec<FloatType>, [ArgSpec<FloatType>]>,
- FunctionSpec<"remainderf", RetValSpec<FloatType>, [ArgSpec<FloatType>, ArgSpec<FloatType>]>,
FunctionSpec<"remainder", RetValSpec<DoubleType>, [ArgSpec<DoubleType>, ArgSpec<DoubleType>]>,
+ FunctionSpec<"remainderf", RetValSpec<FloatType>, [ArgSpec<FloatType>, ArgSpec<FloatType>]>,
FunctionSpec<"remainderl", RetValSpec<LongDoubleType>, [ArgSpec<LongDoubleType>, ArgSpec<LongDoubleType>]>,
+ GuardedFunctionSpec<"remainderf16", RetValSpec<Float16Type>, [ArgSpec<Float16Type>, ArgSpec<Float16Type>], "LIBC_TYPES_HAS_FLOAT16">,
FunctionSpec<"remquof", RetValSpec<FloatType>, [ArgSpec<FloatType>, ArgSpec<FloatType>, ArgSpec<IntPtr>]>,
FunctionSpec<"remquo", RetValSpec<DoubleType>, [ArgSpec<DoubleType>, ArgSpec<DoubleType>, ArgSpec<IntPtr>]>,
diff --git a/libc/src/__support/FPUtil/NormalFloat.h b/libc/src/__support/FPUtil/NormalFloat.h
index 8bc1fecd653bf..413d20430090b 100644
--- a/libc/src/__support/FPUtil/NormalFloat.h
+++ b/libc/src/__support/FPUtil/NormalFloat.h
@@ -52,7 +52,7 @@ template <typename T> struct NormalFloat {
return;
unsigned normalization_shift = evaluate_normalization_shift(mantissa);
- mantissa = mantissa << normalization_shift;
+ mantissa <<= normalization_shift;
exponent -= normalization_shift;
}
@@ -110,9 +110,11 @@ template <typename T> struct NormalFloat {
if (shift <= FPBits<T>::FRACTION_LEN + 1) {
// Generate a subnormal number. Might lead to loss of precision.
// We round to nearest and round halfway cases to even.
- const StorageType shift_out_mask = (StorageType(1) << shift) - 1;
+ const StorageType shift_out_mask =
+ static_cast<StorageType>(StorageType(1) << shift) - 1;
const StorageType shift_out_value = mantissa & shift_out_mask;
- const StorageType halfway_value = StorageType(1) << (shift - 1);
+ const StorageType halfway_value =
+ static_cast<StorageType>(StorageType(1) << (shift - 1));
result.set_biased_exponent(0);
result.set_mantissa(mantissa >> shift);
StorageType new_mantissa = result.get_mantissa();
@@ -135,7 +137,8 @@ template <typename T> struct NormalFloat {
}
}
- result.set_biased_exponent(exponent + FPBits<T>::EXP_BIAS);
+ result.set_biased_exponent(
+ static_cast<StorageType>(exponent + FPBits<T>::EXP_BIAS));
result.set_mantissa(mantissa);
return result.get_val();
}
@@ -155,7 +158,7 @@ template <typename T> struct NormalFloat {
// Normalize subnormal numbers.
if (bits.is_subnormal()) {
unsigned shift = evaluate_normalization_shift(bits.get_mantissa());
- mantissa = StorageType(bits.get_mantissa()) << shift;
+ mantissa = static_cast<StorageType>(bits.get_mantissa() << shift);
exponent = 1 - FPBits<T>::EXP_BIAS - shift;
} else {
exponent = bits.get_biased_exponent() - FPBits<T>::EXP_BIAS;
diff --git a/libc/src/math/CMakeLists.txt b/libc/src/math/CMakeLists.txt
index 7a349ddc53724..2ab8fd7c5c710 100644
--- a/libc/src/math/CMakeLists.txt
+++ b/libc/src/math/CMakeLists.txt
@@ -306,6 +306,7 @@ add_math_entrypoint_object(powf)
add_math_entrypoint_object(remainder)
add_math_entrypoint_object(remainderf)
add_math_entrypoint_object(remainderl)
+add_math_entrypoint_object(remainderf16)
add_math_entrypoint_object(remquo)
add_math_entrypoint_object(remquof)
diff --git a/libc/src/math/generic/CMakeLists.txt b/libc/src/math/generic/CMakeLists.txt
index b1d786fc6b29f..d46684b6626fa 100644
--- a/libc/src/math/generic/CMakeLists.txt
+++ b/libc/src/math/generic/CMakeLists.txt
@@ -2476,6 +2476,19 @@ add_entrypoint_object(
-O2
)
+add_entrypoint_object(
+ remainderf16
+ SRCS
+ remainderf16.cpp
+ HDRS
+ ../remainderf16.h
+ DEPENDS
+ libc.src.__support.macros.properties.types
+ libc.src.__support.FPUtil.division_and_remainder_operations
+ COMPILE_OPTIONS
+ -O2
+)
+
add_entrypoint_object(
hypotf
SRCS
diff --git a/libc/src/math/generic/remainderf16.cpp b/libc/src/math/generic/remainderf16.cpp
new file mode 100644
index 0000000000000..35177228acdbf
--- /dev/null
+++ b/libc/src/math/generic/remainderf16.cpp
@@ -0,0 +1,20 @@
+//===-- Implementation of remainderf16 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/remainderf16.h"
+#include "src/__support/FPUtil/DivisionAndRemainderOperations.h"
+#include "src/__support/common.h"
+
+namespace LIBC_NAMESPACE {
+
+LLVM_LIBC_FUNCTION(float16, remainderf16, (float16 x, float16 y)) {
+ int quotient;
+ return fputil::remquo(x, y, quotient);
+}
+
+} // namespace LIBC_NAMESPACE
diff --git a/libc/src/math/remainderf16.h b/libc/src/math/remainderf16.h
new file mode 100644
index 0000000000000..e23eead4bae2c
--- /dev/null
+++ b/libc/src/math/remainderf16.h
@@ -0,0 +1,20 @@
+//===-- Implementation header for remainderf16 ------------------*- 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_REMAINDERF16_H
+#define LLVM_LIBC_SRC_MATH_REMAINDERF16_H
+
+#include "src/__support/macros/properties/types.h"
+
+namespace LIBC_NAMESPACE {
+
+float16 remainderf16(float16 x, float16 y);
+
+} // namespace LIBC_NAMESPACE
+
+#endif // LLVM_LIBC_SRC_MATH_REMAINDERF16_H
More information about the libc-commits
mailing list