Skip to content

[libc][math] Adds entrypoint and tests for nearbyintf128,scalbnf128 #88443

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Apr 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions libc/config/linux/aarch64/entrypoints.txt
Original file line number Diff line number Diff line change
Expand Up @@ -512,11 +512,13 @@ if(LIBC_TYPES_HAS_FLOAT128)
libc.src.math.lroundf128
libc.src.math.modff128
libc.src.math.nanf128
libc.src.math.nearbyintf128
libc.src.math.nextafterf128
libc.src.math.nextdownf128
libc.src.math.nextupf128
libc.src.math.rintf128
libc.src.math.roundf128
libc.src.math.scalbnf128
libc.src.math.sqrtf128
libc.src.math.truncf128
)
Expand Down
2 changes: 2 additions & 0 deletions libc/config/linux/riscv/entrypoints.txt
Original file line number Diff line number Diff line change
Expand Up @@ -520,11 +520,13 @@ if(LIBC_TYPES_HAS_FLOAT128)
libc.src.math.lroundf128
libc.src.math.modff128
libc.src.math.nanf128
libc.src.math.nearbyintf128
libc.src.math.nextafterf128
libc.src.math.nextdownf128
libc.src.math.nextupf128
libc.src.math.rintf128
libc.src.math.roundf128
libc.src.math.scalbnf128
libc.src.math.sqrtf128
libc.src.math.truncf128
)
Expand Down
2 changes: 2 additions & 0 deletions libc/config/linux/x86_64/entrypoints.txt
Original file line number Diff line number Diff line change
Expand Up @@ -555,12 +555,14 @@ if(LIBC_TYPES_HAS_FLOAT128)
libc.src.math.lroundf128
libc.src.math.modff128
libc.src.math.nanf128
libc.src.math.nearbyintf128
libc.src.math.nextafterf128
libc.src.math.nextdownf128
libc.src.math.nextupf128
libc.src.math.rintf128
libc.src.math.roundevenf128
libc.src.math.roundf128
libc.src.math.scalbnf128
libc.src.math.sqrtf128
libc.src.math.truncf128
libc.src.math.ufromfpf128
Expand Down
4 changes: 2 additions & 2 deletions libc/docs/math/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ Basic Operations
+------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
| nan | |check| | |check| | |check| | | |check| | 7.12.11.2 | F.10.8.2 |
+------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
| nearbyint | |check| | |check| | |check| | | | 7.12.9.3 | F.10.6.3 |
| nearbyint | |check| | |check| | |check| | | |check| | 7.12.9.3 | F.10.6.3 |
+------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
| nextafter | |check| | |check| | |check| | | |check| | 7.12.11.3 | F.10.8.3 |
+------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
Expand All @@ -208,7 +208,7 @@ Basic Operations
+------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
| roundeven | |check| | |check| | |check| | | |check| | 7.12.9.8 | F.10.6.8 |
+------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
| scalbn | |check| | |check| | |check| | | | 7.12.6.19 | F.10.3.19 |
| 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 |
+------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
Expand Down
2 changes: 2 additions & 0 deletions libc/spec/stdc.td
Original file line number Diff line number Diff line change
Expand Up @@ -599,6 +599,7 @@ def StdC : StandardSpec<"stdc"> {
FunctionSpec<"nearbyint", RetValSpec<DoubleType>, [ArgSpec<DoubleType>]>,
FunctionSpec<"nearbyintf", RetValSpec<FloatType>, [ArgSpec<FloatType>]>,
FunctionSpec<"nearbyintl", RetValSpec<LongDoubleType>, [ArgSpec<LongDoubleType>]>,
GuardedFunctionSpec<"nearbyintf128", RetValSpec<Float128Type>, [ArgSpec<Float128Type>], "LIBC_TYPES_HAS_FLOAT128">,

FunctionSpec<"nextafterf", RetValSpec<FloatType>, [ArgSpec<FloatType>, ArgSpec<FloatType>]>,
FunctionSpec<"nextafter", RetValSpec<DoubleType>, [ArgSpec<DoubleType>, ArgSpec<DoubleType>]>,
Expand Down Expand Up @@ -642,6 +643,7 @@ def StdC : StandardSpec<"stdc"> {
FunctionSpec<"scalbn", RetValSpec<DoubleType>, [ArgSpec<DoubleType>, ArgSpec<IntType>]>,
FunctionSpec<"scalbnf", RetValSpec<FloatType>, [ArgSpec<FloatType>, ArgSpec<IntType>]>,
FunctionSpec<"scalbnl", RetValSpec<LongDoubleType>, [ArgSpec<LongDoubleType>, ArgSpec<IntType>]>,
GuardedFunctionSpec<"scalbnf128", RetValSpec<Float128Type>, [ArgSpec<Float128Type>, ArgSpec<IntType>], "LIBC_TYPES_HAS_FLOAT128">,

FunctionSpec<"nanf", RetValSpec<FloatType>, [ArgSpec<ConstCharPtr>]>,
FunctionSpec<"nan", RetValSpec<DoubleType>, [ArgSpec<ConstCharPtr>]>,
Expand Down
2 changes: 2 additions & 0 deletions libc/src/math/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,7 @@ add_math_entrypoint_object(nanf128)
add_math_entrypoint_object(nearbyint)
add_math_entrypoint_object(nearbyintf)
add_math_entrypoint_object(nearbyintl)
add_math_entrypoint_object(nearbyintf128)

add_math_entrypoint_object(nextafter)
add_math_entrypoint_object(nextafterf)
Expand Down Expand Up @@ -301,6 +302,7 @@ add_math_entrypoint_object(roundevenf128)
add_math_entrypoint_object(scalbn)
add_math_entrypoint_object(scalbnf)
add_math_entrypoint_object(scalbnl)
add_math_entrypoint_object(scalbnf128)

add_math_entrypoint_object(sincos)
add_math_entrypoint_object(sincosf)
Expand Down
32 changes: 29 additions & 3 deletions libc/src/math/generic/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -704,7 +704,7 @@ add_entrypoint_object(
DEPENDS
libc.src.__support.FPUtil.nearest_integer_operations
COMPILE_OPTIONS
-O2
-O3
)

add_entrypoint_object(
Expand All @@ -716,7 +716,7 @@ add_entrypoint_object(
DEPENDS
libc.src.__support.FPUtil.nearest_integer_operations
COMPILE_OPTIONS
-O2
-O3
)

add_entrypoint_object(
Expand All @@ -728,7 +728,20 @@ add_entrypoint_object(
DEPENDS
libc.src.__support.FPUtil.nearest_integer_operations
COMPILE_OPTIONS
-O2
-O3
)

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

add_object_library(
Expand Down Expand Up @@ -2949,6 +2962,19 @@ add_entrypoint_object(
-O3
)

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

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

namespace LIBC_NAMESPACE {

LLVM_LIBC_FUNCTION(float128, nearbyintf128, (float128 x)) {
return fputil::round_using_current_rounding_mode(x);
}

} // namespace LIBC_NAMESPACE
27 changes: 27 additions & 0 deletions libc/src/math/generic/scalbnf128.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
//===-- Implementation of scalbnf128 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/scalbnf128.h"
#include "src/__support/FPUtil/ManipulationFunctions.h"
#include "src/__support/common.h"

namespace LIBC_NAMESPACE {

LLVM_LIBC_FUNCTION(float128, scalbnf128, (float128 x, int n)) {
// TODO: should be switched to use `FLT_RADIX` in hdr/float_macros.h" instead
// see: https://github.com/llvm/llvm-project/issues/90496
#if !defined(__FLT_RADIX__)
#error __FLT_RADIX__ undefined.
#elif __FLT_RADIX__ != 2
#error __FLT_RADIX__!=2, unimplemented.
#else
return fputil::ldexp(x, n);
#endif
}

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

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

namespace LIBC_NAMESPACE {

float128 nearbyintf128(float128 x);

} // namespace LIBC_NAMESPACE

#endif // LLVM_LIBC_SRC_MATH_NEARBYINTF128_H
20 changes: 20 additions & 0 deletions libc/src/math/scalbnf128.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
//===-- Implementation header for scalbnf128 --------------------*- 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_SCALBNF128_H
#define LLVM_LIBC_SRC_MATH_SCALBNF128_H

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

namespace LIBC_NAMESPACE {

float128 scalbnf128(float128 x, int n);

} // namespace LIBC_NAMESPACE

#endif // LLVM_LIBC_SRC_MATH_SCALBNF128_H
21 changes: 21 additions & 0 deletions libc/test/UnitTest/FPMatcher.h
Original file line number Diff line number Diff line change
Expand Up @@ -219,4 +219,25 @@ template <typename T> struct FPTest : public Test {
} \
} while (0)

#define EXPECT_FP_EQ_ROUNDING_MODE(expected, actual, rounding_mode) \
do { \
using namespace LIBC_NAMESPACE::fputil::testing; \
ForceRoundingMode __r((rounding_mode)); \
if (__r.success) { \
EXPECT_FP_EQ((expected), (actual)); \
} \
} while (0)

#define EXPECT_FP_EQ_ROUNDING_NEAREST(expected, actual) \
EXPECT_FP_EQ_ROUNDING_MODE((expected), (actual), RoundingMode::Nearest)

#define EXPECT_FP_EQ_ROUNDING_UPWARD(expected, actual) \
EXPECT_FP_EQ_ROUNDING_MODE((expected), (actual), RoundingMode::Upward)

#define EXPECT_FP_EQ_ROUNDING_DOWNWARD(expected, actual) \
EXPECT_FP_EQ_ROUNDING_MODE((expected), (actual), RoundingMode::Downward)

#define EXPECT_FP_EQ_ROUNDING_TOWARD_ZERO(expected, actual) \
EXPECT_FP_EQ_ROUNDING_MODE((expected), (actual), RoundingMode::TowardZero)

#endif // LLVM_LIBC_TEST_UNITTEST_FPMATCHER_H
15 changes: 15 additions & 0 deletions libc/test/src/math/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1650,6 +1650,21 @@ add_fp_unittest(
libc.src.__support.FPUtil.normal_float
)

add_fp_unittest(
scalbnf128_test
NEED_MPFR
SUITE
libc-math-unittests
SRCS
scalbnf128_test.cpp
HDRS
ScalbnTest.h
DEPENDS
libc.src.math.scalbnf128
libc.src.__support.FPUtil.fp_bits
libc.src.__support.FPUtil.normal_float
)

add_fp_unittest(
erff_test
NEED_MPFR
Expand Down
13 changes: 13 additions & 0 deletions libc/test/src/math/scalbnf128_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
//===-- Unittests for scalbnf128 ------------------------------------------===//
//
// 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 "ScalbnTest.h"

#include "src/math/scalbnf128.h"

LIST_SCALBN_TESTS(float128, LIBC_NAMESPACE::scalbnf128)
Loading