From 30cd5374be45c49fc146666ea882113737128ecb Mon Sep 17 00:00:00 2001 From: OverMighty Date: Mon, 3 Jun 2024 15:22:42 +0200 Subject: [PATCH] [libc][math][c23] Add lroundf16 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/lroundf16.cpp | 19 +++++++ libc/src/math/lroundf16.h | 20 +++++++ libc/test/src/math/smoke/CMakeLists.txt | 58 ++++++++++++--------- libc/test/src/math/smoke/lroundf16_test.cpp | 13 +++++ 10 files changed, 104 insertions(+), 25 deletions(-) create mode 100644 libc/src/math/generic/lroundf16.cpp create mode 100644 libc/src/math/lroundf16.h create mode 100644 libc/test/src/math/smoke/lroundf16_test.cpp diff --git a/libc/config/linux/aarch64/entrypoints.txt b/libc/config/linux/aarch64/entrypoints.txt index c1207babfe06ae..dfdebf7c277be6 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.llrintf16 libc.src.math.lrintf16 + libc.src.math.lroundf16 libc.src.math.nearbyintf16 libc.src.math.rintf16 libc.src.math.roundf16 diff --git a/libc/config/linux/x86_64/entrypoints.txt b/libc/config/linux/x86_64/entrypoints.txt index 9871d7b00ca1b4..8558fd847ab7bd 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.llrintf16 libc.src.math.lrintf16 + libc.src.math.lroundf16 libc.src.math.nearbyintf16 libc.src.math.rintf16 libc.src.math.roundf16 diff --git a/libc/docs/math/index.rst b/libc/docs/math/index.rst index ffbfec8c400395..abd96dc124d54c 100644 --- a/libc/docs/math/index.rst +++ b/libc/docs/math/index.rst @@ -182,7 +182,7 @@ Basic Operations +------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+ | lrint | |check| | |check| | |check| | |check| | |check| | 7.12.9.5 | F.10.6.5 | +------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+ -| lround | |check| | |check| | |check| | | |check| | 7.12.9.7 | F.10.6.7 | +| lround | |check| | |check| | |check| | |check| | |check| | 7.12.9.7 | F.10.6.7 | +------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+ | modf | |check| | |check| | |check| | | |check| | 7.12.6.18 | F.10.3.18 | +------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+ diff --git a/libc/spec/stdc.td b/libc/spec/stdc.td index 75fc9b83c1ba59..8e0a4b21540786 100644 --- a/libc/spec/stdc.td +++ b/libc/spec/stdc.td @@ -579,6 +579,7 @@ def StdC : StandardSpec<"stdc"> { FunctionSpec<"lround", RetValSpec, [ArgSpec]>, FunctionSpec<"lroundf", RetValSpec, [ArgSpec]>, FunctionSpec<"lroundl", RetValSpec, [ArgSpec]>, + GuardedFunctionSpec<"lroundf16", RetValSpec, [ArgSpec], "LIBC_TYPES_HAS_FLOAT16">, GuardedFunctionSpec<"lroundf128", RetValSpec, [ArgSpec], "LIBC_TYPES_HAS_FLOAT128">, FunctionSpec<"llround", RetValSpec, [ArgSpec]>, diff --git a/libc/src/math/CMakeLists.txt b/libc/src/math/CMakeLists.txt index 09c6b6bbd9c468..99cb3982b58c47 100644 --- a/libc/src/math/CMakeLists.txt +++ b/libc/src/math/CMakeLists.txt @@ -242,6 +242,7 @@ add_math_entrypoint_object(lrintf128) add_math_entrypoint_object(lround) add_math_entrypoint_object(lroundf) add_math_entrypoint_object(lroundl) +add_math_entrypoint_object(lroundf16) add_math_entrypoint_object(lroundf128) add_math_entrypoint_object(modf) diff --git a/libc/src/math/generic/CMakeLists.txt b/libc/src/math/generic/CMakeLists.txt index 978899a2134526..ddbf594144c2d5 100644 --- a/libc/src/math/generic/CMakeLists.txt +++ b/libc/src/math/generic/CMakeLists.txt @@ -564,6 +564,19 @@ add_entrypoint_object( libc.src.__support.FPUtil.nearest_integer_operations ) +add_entrypoint_object( + lroundf16 + SRCS + lroundf16.cpp + HDRS + ../lroundf16.h + COMPILE_OPTIONS + -O3 + DEPENDS + libc.src.__support.macros.properties.types + libc.src.__support.FPUtil.nearest_integer_operations +) + add_entrypoint_object( lroundf128 SRCS diff --git a/libc/src/math/generic/lroundf16.cpp b/libc/src/math/generic/lroundf16.cpp new file mode 100644 index 00000000000000..c854168d807868 --- /dev/null +++ b/libc/src/math/generic/lroundf16.cpp @@ -0,0 +1,19 @@ +//===-- Implementation of lroundf16 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/lroundf16.h" +#include "src/__support/FPUtil/NearestIntegerOperations.h" +#include "src/__support/common.h" + +namespace LIBC_NAMESPACE { + +LLVM_LIBC_FUNCTION(long, lroundf16, (float16 x)) { + return fputil::round_to_signed_integer(x); +} + +} // namespace LIBC_NAMESPACE diff --git a/libc/src/math/lroundf16.h b/libc/src/math/lroundf16.h new file mode 100644 index 00000000000000..57201e7063a9e3 --- /dev/null +++ b/libc/src/math/lroundf16.h @@ -0,0 +1,20 @@ +//===-- Implementation header for lroundf16 ---------------------*- 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_LROUNDF16_H +#define LLVM_LIBC_SRC_MATH_LROUNDF16_H + +#include "src/__support/macros/properties/types.h" + +namespace LIBC_NAMESPACE { + +long lroundf16(float16 x); + +} // namespace LIBC_NAMESPACE + +#endif // LLVM_LIBC_SRC_MATH_LROUNDF16_H diff --git a/libc/test/src/math/smoke/CMakeLists.txt b/libc/test/src/math/smoke/CMakeLists.txt index b281a3ffdce510..48bc2e545a4c3a 100644 --- a/libc/test/src/math/smoke/CMakeLists.txt +++ b/libc/test/src/math/smoke/CMakeLists.txt @@ -507,11 +507,9 @@ add_fp_unittest( RoundToIntegerTest.h DEPENDS libc.src.errno.errno - libc.src.fenv.feclearexcept - libc.src.fenv.feraiseexcept - libc.src.fenv.fetestexcept libc.src.math.lround libc.src.__support.CPP.algorithm + libc.src.__support.FPUtil.fenv_impl libc.src.__support.FPUtil.fp_bits ) @@ -525,11 +523,9 @@ add_fp_unittest( RoundToIntegerTest.h DEPENDS libc.src.errno.errno - libc.src.fenv.feclearexcept - libc.src.fenv.feraiseexcept - libc.src.fenv.fetestexcept libc.src.math.lroundf libc.src.__support.CPP.algorithm + libc.src.__support.FPUtil.fenv_impl libc.src.__support.FPUtil.fp_bits ) @@ -543,11 +539,25 @@ add_fp_unittest( RoundToIntegerTest.h DEPENDS libc.src.errno.errno - libc.src.fenv.feclearexcept - libc.src.fenv.feraiseexcept - libc.src.fenv.fetestexcept libc.src.math.lroundl libc.src.__support.CPP.algorithm + libc.src.__support.FPUtil.fenv_impl + libc.src.__support.FPUtil.fp_bits +) + +add_fp_unittest( + lroundf16_test + SUITE + libc-math-smoke-tests + SRCS + lroundf16_test.cpp + HDRS + RoundToIntegerTest.h + DEPENDS + libc.src.errno.errno + libc.src.math.lroundf16 + libc.src.__support.CPP.algorithm + libc.src.__support.FPUtil.fenv_impl libc.src.__support.FPUtil.fp_bits ) @@ -561,11 +571,9 @@ add_fp_unittest( RoundToIntegerTest.h DEPENDS libc.src.errno.errno - libc.src.fenv.feclearexcept - libc.src.fenv.feraiseexcept - libc.src.fenv.fetestexcept libc.src.math.lroundf128 libc.src.__support.CPP.algorithm + libc.src.__support.FPUtil.fenv_impl libc.src.__support.FPUtil.fp_bits ) @@ -579,11 +587,9 @@ add_fp_unittest( RoundToIntegerTest.h DEPENDS libc.src.errno.errno - libc.src.fenv.feclearexcept - libc.src.fenv.feraiseexcept - libc.src.fenv.fetestexcept libc.src.math.llround libc.src.__support.CPP.algorithm + libc.src.__support.FPUtil.fenv_impl libc.src.__support.FPUtil.fp_bits ) @@ -597,11 +603,9 @@ add_fp_unittest( RoundToIntegerTest.h DEPENDS libc.src.errno.errno - libc.src.fenv.feclearexcept - libc.src.fenv.feraiseexcept - libc.src.fenv.fetestexcept libc.src.math.llroundf libc.src.__support.CPP.algorithm + libc.src.__support.FPUtil.fenv_impl libc.src.__support.FPUtil.fp_bits ) @@ -615,11 +619,9 @@ add_fp_unittest( RoundToIntegerTest.h DEPENDS libc.src.errno.errno - libc.src.fenv.feclearexcept - libc.src.fenv.feraiseexcept - libc.src.fenv.fetestexcept libc.src.math.llroundl libc.src.__support.CPP.algorithm + libc.src.__support.FPUtil.fenv_impl libc.src.__support.FPUtil.fp_bits ) @@ -633,11 +635,9 @@ add_fp_unittest( RoundToIntegerTest.h DEPENDS libc.src.errno.errno - libc.src.fenv.feclearexcept - libc.src.fenv.feraiseexcept - libc.src.fenv.fetestexcept libc.src.math.llroundf128 libc.src.__support.CPP.algorithm + libc.src.__support.FPUtil.fenv_impl libc.src.__support.FPUtil.fp_bits ) @@ -720,6 +720,7 @@ add_fp_unittest( HDRS RoundToIntegerTest.h DEPENDS + libc.src.errno.errno libc.src.math.lrint libc.src.__support.CPP.algorithm libc.src.__support.FPUtil.fenv_impl @@ -735,6 +736,7 @@ add_fp_unittest( HDRS RoundToIntegerTest.h DEPENDS + libc.src.errno.errno libc.src.math.lrintf libc.src.__support.CPP.algorithm libc.src.__support.FPUtil.fenv_impl @@ -750,6 +752,7 @@ add_fp_unittest( HDRS RoundToIntegerTest.h DEPENDS + libc.src.errno.errno libc.src.math.lrintl libc.src.__support.CPP.algorithm libc.src.__support.FPUtil.fenv_impl @@ -765,6 +768,7 @@ add_fp_unittest( HDRS RoundToIntegerTest.h DEPENDS + libc.src.errno.errno libc.src.math.lrintf16 libc.src.__support.CPP.algorithm libc.src.__support.FPUtil.fenv_impl @@ -780,6 +784,7 @@ add_fp_unittest( HDRS RoundToIntegerTest.h DEPENDS + libc.src.errno.errno libc.src.math.lrintf128 libc.src.__support.CPP.algorithm libc.src.__support.FPUtil.fenv_impl @@ -795,6 +800,7 @@ add_fp_unittest( HDRS RoundToIntegerTest.h DEPENDS + libc.src.errno.errno libc.src.math.llrint libc.src.__support.CPP.algorithm libc.src.__support.FPUtil.fenv_impl @@ -810,6 +816,7 @@ add_fp_unittest( HDRS RoundToIntegerTest.h DEPENDS + libc.src.errno.errno libc.src.math.llrintf libc.src.__support.CPP.algorithm libc.src.__support.FPUtil.fenv_impl @@ -825,6 +832,7 @@ add_fp_unittest( HDRS RoundToIntegerTest.h DEPENDS + libc.src.errno.errno libc.src.math.llrintl libc.src.__support.CPP.algorithm libc.src.__support.FPUtil.fenv_impl @@ -840,6 +848,7 @@ add_fp_unittest( HDRS RoundToIntegerTest.h DEPENDS + libc.src.errno.errno libc.src.math.llrintf16 libc.src.__support.CPP.algorithm libc.src.__support.FPUtil.fenv_impl @@ -855,6 +864,7 @@ add_fp_unittest( HDRS RoundToIntegerTest.h DEPENDS + libc.src.errno.errno libc.src.math.llrintf128 libc.src.__support.CPP.algorithm libc.src.__support.FPUtil.fenv_impl diff --git a/libc/test/src/math/smoke/lroundf16_test.cpp b/libc/test/src/math/smoke/lroundf16_test.cpp new file mode 100644 index 00000000000000..3077134d58f916 --- /dev/null +++ b/libc/test/src/math/smoke/lroundf16_test.cpp @@ -0,0 +1,13 @@ +//===-- Unittests for lroundf16 -------------------------------------------===// +// +// 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 "RoundToIntegerTest.h" + +#include "src/math/lroundf16.h" + +LIST_ROUND_TO_INTEGER_TESTS(float16, long, LIBC_NAMESPACE::lroundf16)