Skip to content

Commit

Permalink
[libc][math][c23] Add lrintf16 C23 math function
Browse files Browse the repository at this point in the history
  • Loading branch information
overmighty committed Jun 3, 2024
1 parent 3599323 commit a886cb8
Show file tree
Hide file tree
Showing 11 changed files with 107 additions and 3 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 @@ -502,6 +502,7 @@ if(LIBC_TYPES_HAS_FLOAT16)
libc.src.math.ceilf16
libc.src.math.fabsf16
libc.src.math.floorf16
libc.src.math.lrintf16
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 @@ -535,6 +535,7 @@ if(LIBC_TYPES_HAS_FLOAT16)
libc.src.math.ceilf16
libc.src.math.fabsf16
libc.src.math.floorf16
libc.src.math.lrintf16
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 @@ -180,7 +180,7 @@ Basic Operations
+------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
| logb | |check| | |check| | |check| | | |check| | 7.12.6.17 | F.10.3.17 |
+------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
| lrint | |check| | |check| | |check| | | |check| | 7.12.9.5 | F.10.6.5 |
| 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 |
+------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
Expand Down
1 change: 1 addition & 0 deletions libc/spec/stdc.td
Original file line number Diff line number Diff line change
Expand Up @@ -595,6 +595,7 @@ def StdC : StandardSpec<"stdc"> {
FunctionSpec<"lrint", RetValSpec<LongType>, [ArgSpec<DoubleType>]>,
FunctionSpec<"lrintf", RetValSpec<LongType>, [ArgSpec<FloatType>]>,
FunctionSpec<"lrintl", RetValSpec<LongType>, [ArgSpec<LongDoubleType>]>,
GuardedFunctionSpec<"lrintf16", RetValSpec<LongType>, [ArgSpec<Float16Type>], "LIBC_TYPES_HAS_FLOAT16">,
GuardedFunctionSpec<"lrintf128", RetValSpec<LongType>, [ArgSpec<Float128Type>], "LIBC_TYPES_HAS_FLOAT128">,

FunctionSpec<"llrint", 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 @@ -235,6 +235,7 @@ add_math_entrypoint_object(llroundf128)
add_math_entrypoint_object(lrint)
add_math_entrypoint_object(lrintf)
add_math_entrypoint_object(lrintl)
add_math_entrypoint_object(lrintf16)
add_math_entrypoint_object(lrintf128)

add_math_entrypoint_object(lround)
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 @@ -724,6 +724,19 @@ add_entrypoint_object(
libc.src.__support.FPUtil.nearest_integer_operations
)

add_entrypoint_object(
lrintf16
SRCS
lrintf16.cpp
HDRS
../lrintf16.h
COMPILE_OPTIONS
-O3
DEPENDS
libc.src.__support.macros.properties.types
libc.src.__support.FPUtil.nearest_integer_operations
)

add_entrypoint_object(
lrintf128
SRCS
Expand Down
20 changes: 20 additions & 0 deletions libc/src/math/generic/lrintf16.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
//===-- Implementation of lrintf16 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/lrintf16.h"
#include "src/__support/FPUtil/NearestIntegerOperations.h"
#include "src/__support/common.h"

namespace LIBC_NAMESPACE {

LLVM_LIBC_FUNCTION(long, lrintf16, (float16 x)) {
return fputil::round_to_signed_integer_using_current_rounding_mode<float16,
long>(x);
}

} // namespace LIBC_NAMESPACE
20 changes: 20 additions & 0 deletions libc/src/math/lrintf16.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
//===-- Implementation header for lrintf16 ----------------------*- 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_LRINTF16_H
#define LLVM_LIBC_SRC_MATH_LRINTF16_H

#include "src/__support/macros/properties/types.h"

namespace LIBC_NAMESPACE {

long lrintf16(float16 x);

} // namespace LIBC_NAMESPACE

#endif // LLVM_LIBC_SRC_MATH_LRINTF16_H
31 changes: 31 additions & 0 deletions libc/test/src/math/smoke/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -511,6 +511,7 @@ add_fp_unittest(
libc.src.fenv.feraiseexcept
libc.src.fenv.fetestexcept
libc.src.math.lround
libc.src.__support.CPP.algorithm
libc.src.__support.FPUtil.fp_bits
)

Expand All @@ -528,6 +529,7 @@ add_fp_unittest(
libc.src.fenv.feraiseexcept
libc.src.fenv.fetestexcept
libc.src.math.lroundf
libc.src.__support.CPP.algorithm
libc.src.__support.FPUtil.fp_bits
)

Expand All @@ -545,6 +547,7 @@ add_fp_unittest(
libc.src.fenv.feraiseexcept
libc.src.fenv.fetestexcept
libc.src.math.lroundl
libc.src.__support.CPP.algorithm
libc.src.__support.FPUtil.fp_bits
)

Expand All @@ -562,6 +565,7 @@ add_fp_unittest(
libc.src.fenv.feraiseexcept
libc.src.fenv.fetestexcept
libc.src.math.lroundf128
libc.src.__support.CPP.algorithm
libc.src.__support.FPUtil.fp_bits
)

Expand All @@ -579,6 +583,7 @@ add_fp_unittest(
libc.src.fenv.feraiseexcept
libc.src.fenv.fetestexcept
libc.src.math.llround
libc.src.__support.CPP.algorithm
libc.src.__support.FPUtil.fp_bits
)

Expand All @@ -596,6 +601,7 @@ add_fp_unittest(
libc.src.fenv.feraiseexcept
libc.src.fenv.fetestexcept
libc.src.math.llroundf
libc.src.__support.CPP.algorithm
libc.src.__support.FPUtil.fp_bits
)

Expand All @@ -613,6 +619,7 @@ add_fp_unittest(
libc.src.fenv.feraiseexcept
libc.src.fenv.fetestexcept
libc.src.math.llroundl
libc.src.__support.CPP.algorithm
libc.src.__support.FPUtil.fp_bits
)

Expand All @@ -630,6 +637,7 @@ add_fp_unittest(
libc.src.fenv.feraiseexcept
libc.src.fenv.fetestexcept
libc.src.math.llroundf128
libc.src.__support.CPP.algorithm
libc.src.__support.FPUtil.fp_bits
)

Expand Down Expand Up @@ -713,6 +721,7 @@ add_fp_unittest(
RoundToIntegerTest.h
DEPENDS
libc.src.math.lrint
libc.src.__support.CPP.algorithm
libc.src.__support.FPUtil.fenv_impl
libc.src.__support.FPUtil.fp_bits
)
Expand All @@ -727,6 +736,7 @@ add_fp_unittest(
RoundToIntegerTest.h
DEPENDS
libc.src.math.lrintf
libc.src.__support.CPP.algorithm
libc.src.__support.FPUtil.fenv_impl
libc.src.__support.FPUtil.fp_bits
)
Expand All @@ -741,6 +751,22 @@ add_fp_unittest(
RoundToIntegerTest.h
DEPENDS
libc.src.math.lrintl
libc.src.__support.CPP.algorithm
libc.src.__support.FPUtil.fenv_impl
libc.src.__support.FPUtil.fp_bits
)

add_fp_unittest(
lrintf16_test
SUITE
libc-math-smoke-tests
SRCS
lrintf16_test.cpp
HDRS
RoundToIntegerTest.h
DEPENDS
libc.src.math.lrintf16
libc.src.__support.CPP.algorithm
libc.src.__support.FPUtil.fenv_impl
libc.src.__support.FPUtil.fp_bits
)
Expand All @@ -755,6 +781,7 @@ add_fp_unittest(
RoundToIntegerTest.h
DEPENDS
libc.src.math.lrintf128
libc.src.__support.CPP.algorithm
libc.src.__support.FPUtil.fenv_impl
libc.src.__support.FPUtil.fp_bits
)
Expand All @@ -769,6 +796,7 @@ add_fp_unittest(
RoundToIntegerTest.h
DEPENDS
libc.src.math.llrint
libc.src.__support.CPP.algorithm
libc.src.__support.FPUtil.fenv_impl
libc.src.__support.FPUtil.fp_bits
)
Expand All @@ -783,6 +811,7 @@ add_fp_unittest(
RoundToIntegerTest.h
DEPENDS
libc.src.math.llrintf
libc.src.__support.CPP.algorithm
libc.src.__support.FPUtil.fenv_impl
libc.src.__support.FPUtil.fp_bits
)
Expand All @@ -797,6 +826,7 @@ add_fp_unittest(
RoundToIntegerTest.h
DEPENDS
libc.src.math.llrintl
libc.src.__support.CPP.algorithm
libc.src.__support.FPUtil.fenv_impl
libc.src.__support.FPUtil.fp_bits
)
Expand All @@ -811,6 +841,7 @@ add_fp_unittest(
RoundToIntegerTest.h
DEPENDS
libc.src.math.llrintf128
libc.src.__support.CPP.algorithm
libc.src.__support.FPUtil.fenv_impl
libc.src.__support.FPUtil.fp_bits
)
Expand Down
7 changes: 5 additions & 2 deletions libc/test/src/math/smoke/RoundToIntegerTest.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#ifndef LLVM_LIBC_TEST_SRC_MATH_SMOKE_ROUNDTOINTEGERTEST_H
#define LLVM_LIBC_TEST_SRC_MATH_SMOKE_ROUNDTOINTEGERTEST_H

#include "src/__support/CPP/algorithm.h"
#include "src/__support/FPUtil/FEnvImpl.h"
#include "src/__support/FPUtil/FPBits.h"
#include "test/UnitTest/FEnvSafeTest.h"
Expand Down Expand Up @@ -113,8 +114,10 @@ class RoundToIntegerTestTemplate
}

void testSubnormalRange(RoundToIntegerFunc func) {
constexpr StorageType COUNT = 1'000'001;
constexpr StorageType STEP = (MAX_SUBNORMAL - MIN_SUBNORMAL) / COUNT;
constexpr int COUNT = 1'000'001;
constexpr StorageType STEP = LIBC_NAMESPACE::cpp::max(
static_cast<StorageType>((MAX_SUBNORMAL - MIN_SUBNORMAL) / COUNT),
StorageType(1));
for (StorageType i = MIN_SUBNORMAL; i <= MAX_SUBNORMAL; i += STEP) {
F x = FPBits(i).get_val();
if (x == F(0.0))
Expand Down
13 changes: 13 additions & 0 deletions libc/test/src/math/smoke/lrintf16_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
//===-- Unittests for lrintf16 --------------------------------------------===//
//
// 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/lrintf16.h"

LIST_ROUND_TO_INTEGER_TESTS_WITH_MODES(float16, long, LIBC_NAMESPACE::lrintf16)

0 comments on commit a886cb8

Please sign in to comment.