Skip to content

Commit

Permalink
[libc][math][c23] Add lroundf16 C23 math function
Browse files Browse the repository at this point in the history
  • Loading branch information
overmighty committed Jun 3, 2024
1 parent f4a9850 commit 30cd537
Show file tree
Hide file tree
Showing 10 changed files with 104 additions and 25 deletions.
1 change: 1 addition & 0 deletions libc/config/linux/aarch64/entrypoints.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions libc/config/linux/x86_64/entrypoints.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion libc/docs/math/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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 |
+------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
Expand Down
1 change: 1 addition & 0 deletions libc/spec/stdc.td
Original file line number Diff line number Diff line change
Expand Up @@ -579,6 +579,7 @@ def StdC : StandardSpec<"stdc"> {
FunctionSpec<"lround", RetValSpec<LongType>, [ArgSpec<DoubleType>]>,
FunctionSpec<"lroundf", RetValSpec<LongType>, [ArgSpec<FloatType>]>,
FunctionSpec<"lroundl", RetValSpec<LongType>, [ArgSpec<LongDoubleType>]>,
GuardedFunctionSpec<"lroundf16", RetValSpec<LongType>, [ArgSpec<Float16Type>], "LIBC_TYPES_HAS_FLOAT16">,
GuardedFunctionSpec<"lroundf128", RetValSpec<LongType>, [ArgSpec<Float128Type>], "LIBC_TYPES_HAS_FLOAT128">,

FunctionSpec<"llround", RetValSpec<LongLongType>, [ArgSpec<DoubleType>]>,
Expand Down
1 change: 1 addition & 0 deletions libc/src/math/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
13 changes: 13 additions & 0 deletions libc/src/math/generic/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
19 changes: 19 additions & 0 deletions libc/src/math/generic/lroundf16.cpp
Original file line number Diff line number Diff line change
@@ -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<float128, long>(x);
}

} // namespace LIBC_NAMESPACE
20 changes: 20 additions & 0 deletions libc/src/math/lroundf16.h
Original file line number Diff line number Diff line change
@@ -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
58 changes: 34 additions & 24 deletions libc/test/src/math/smoke/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
)

Expand All @@ -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
)

Expand All @@ -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
)

Expand All @@ -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
)

Expand All @@ -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
)

Expand All @@ -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
)

Expand All @@ -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
)

Expand All @@ -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
)

Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down
13 changes: 13 additions & 0 deletions libc/test/src/math/smoke/lroundf16_test.cpp
Original file line number Diff line number Diff line change
@@ -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)

0 comments on commit 30cd537

Please sign in to comment.