Skip to content

Commit 5e9937d

Browse files
FlandiniMichael Flanders
andauthored
[libc][math] Adds entrypoint and tests for nearbyintf128,scalbnf128 (#88443)
Closes #84689. Adding @lntue for review. I was curious about the implementation of `round_using_current_rounding_mode` used for the `nearbyint` functions. It has one of the rounding modes as unreachable ([here](https://github.com/llvm/llvm-project/blob/main/libc/src/__support/FPUtil/NearestIntegerOperations.h#L243)), and I was wondering if this was okay for the `nearbyint` functions. --------- Co-authored-by: Michael Flanders <[email protected]>
1 parent c4c4e17 commit 5e9937d

21 files changed

+406
-8
lines changed

libc/config/linux/aarch64/entrypoints.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -527,11 +527,13 @@ if(LIBC_TYPES_HAS_FLOAT128)
527527
libc.src.math.lroundf128
528528
libc.src.math.modff128
529529
libc.src.math.nanf128
530+
libc.src.math.nearbyintf128
530531
libc.src.math.nextafterf128
531532
libc.src.math.nextdownf128
532533
libc.src.math.nextupf128
533534
libc.src.math.rintf128
534535
libc.src.math.roundf128
536+
libc.src.math.scalbnf128
535537
libc.src.math.sqrtf128
536538
libc.src.math.truncf128
537539
libc.src.math.ufromfpf128

libc/config/linux/riscv/entrypoints.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -535,11 +535,13 @@ if(LIBC_TYPES_HAS_FLOAT128)
535535
libc.src.math.lroundf128
536536
libc.src.math.modff128
537537
libc.src.math.nanf128
538+
libc.src.math.nearbyintf128
538539
libc.src.math.nextafterf128
539540
libc.src.math.nextdownf128
540541
libc.src.math.nextupf128
541542
libc.src.math.rintf128
542543
libc.src.math.roundf128
544+
libc.src.math.scalbnf128
543545
libc.src.math.sqrtf128
544546
libc.src.math.truncf128
545547
libc.src.math.ufromfpf128

libc/config/linux/x86_64/entrypoints.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -560,12 +560,14 @@ if(LIBC_TYPES_HAS_FLOAT128)
560560
libc.src.math.lroundf128
561561
libc.src.math.modff128
562562
libc.src.math.nanf128
563+
libc.src.math.nearbyintf128
563564
libc.src.math.nextafterf128
564565
libc.src.math.nextdownf128
565566
libc.src.math.nextupf128
566567
libc.src.math.rintf128
567568
libc.src.math.roundevenf128
568569
libc.src.math.roundf128
570+
libc.src.math.scalbnf128
569571
libc.src.math.sqrtf128
570572
libc.src.math.truncf128
571573
libc.src.math.ufromfpf128

libc/docs/math/index.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ Basic Operations
188188
+------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
189189
| nan | |check| | |check| | |check| | | |check| | 7.12.11.2 | F.10.8.2 |
190190
+------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
191-
| nearbyint | |check| | |check| | |check| | | | 7.12.9.3 | F.10.6.3 |
191+
| nearbyint | |check| | |check| | |check| | | |check| | 7.12.9.3 | F.10.6.3 |
192192
+------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
193193
| nextafter | |check| | |check| | |check| | | |check| | 7.12.11.3 | F.10.8.3 |
194194
+------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
@@ -208,7 +208,7 @@ Basic Operations
208208
+------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
209209
| roundeven | |check| | |check| | |check| | | |check| | 7.12.9.8 | F.10.6.8 |
210210
+------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
211-
| scalbn | |check| | |check| | |check| | | | 7.12.6.19 | F.10.3.19 |
211+
| scalbn | |check| | |check| | |check| | | |check| | 7.12.6.19 | F.10.3.19 |
212212
+------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
213213
| trunc | |check| | |check| | |check| | | |check| | 7.12.9.9 | F.10.6.9 |
214214
+------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+

libc/spec/stdc.td

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -604,6 +604,7 @@ def StdC : StandardSpec<"stdc"> {
604604
FunctionSpec<"nearbyint", RetValSpec<DoubleType>, [ArgSpec<DoubleType>]>,
605605
FunctionSpec<"nearbyintf", RetValSpec<FloatType>, [ArgSpec<FloatType>]>,
606606
FunctionSpec<"nearbyintl", RetValSpec<LongDoubleType>, [ArgSpec<LongDoubleType>]>,
607+
GuardedFunctionSpec<"nearbyintf128", RetValSpec<Float128Type>, [ArgSpec<Float128Type>], "LIBC_TYPES_HAS_FLOAT128">,
607608

608609
FunctionSpec<"nextafterf", RetValSpec<FloatType>, [ArgSpec<FloatType>, ArgSpec<FloatType>]>,
609610
FunctionSpec<"nextafter", RetValSpec<DoubleType>, [ArgSpec<DoubleType>, ArgSpec<DoubleType>]>,
@@ -647,6 +648,7 @@ def StdC : StandardSpec<"stdc"> {
647648
FunctionSpec<"scalbn", RetValSpec<DoubleType>, [ArgSpec<DoubleType>, ArgSpec<IntType>]>,
648649
FunctionSpec<"scalbnf", RetValSpec<FloatType>, [ArgSpec<FloatType>, ArgSpec<IntType>]>,
649650
FunctionSpec<"scalbnl", RetValSpec<LongDoubleType>, [ArgSpec<LongDoubleType>, ArgSpec<IntType>]>,
651+
GuardedFunctionSpec<"scalbnf128", RetValSpec<Float128Type>, [ArgSpec<Float128Type>, ArgSpec<IntType>], "LIBC_TYPES_HAS_FLOAT128">,
650652

651653
FunctionSpec<"nanf", RetValSpec<FloatType>, [ArgSpec<ConstCharPtr>]>,
652654
FunctionSpec<"nan", RetValSpec<DoubleType>, [ArgSpec<ConstCharPtr>]>,

libc/src/math/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,7 @@ add_math_entrypoint_object(nanf128)
252252
add_math_entrypoint_object(nearbyint)
253253
add_math_entrypoint_object(nearbyintf)
254254
add_math_entrypoint_object(nearbyintl)
255+
add_math_entrypoint_object(nearbyintf128)
255256

256257
add_math_entrypoint_object(nextafter)
257258
add_math_entrypoint_object(nextafterf)
@@ -301,6 +302,7 @@ add_math_entrypoint_object(roundevenf128)
301302
add_math_entrypoint_object(scalbn)
302303
add_math_entrypoint_object(scalbnf)
303304
add_math_entrypoint_object(scalbnl)
305+
add_math_entrypoint_object(scalbnf128)
304306

305307
add_math_entrypoint_object(sincos)
306308
add_math_entrypoint_object(sincosf)

libc/src/math/generic/CMakeLists.txt

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -704,7 +704,7 @@ add_entrypoint_object(
704704
DEPENDS
705705
libc.src.__support.FPUtil.nearest_integer_operations
706706
COMPILE_OPTIONS
707-
-O2
707+
-O3
708708
)
709709

710710
add_entrypoint_object(
@@ -716,7 +716,7 @@ add_entrypoint_object(
716716
DEPENDS
717717
libc.src.__support.FPUtil.nearest_integer_operations
718718
COMPILE_OPTIONS
719-
-O2
719+
-O3
720720
)
721721

722722
add_entrypoint_object(
@@ -728,7 +728,20 @@ add_entrypoint_object(
728728
DEPENDS
729729
libc.src.__support.FPUtil.nearest_integer_operations
730730
COMPILE_OPTIONS
731-
-O2
731+
-O3
732+
)
733+
734+
add_entrypoint_object(
735+
nearbyintf128
736+
SRCS
737+
nearbyintf128.cpp
738+
HDRS
739+
../nearbyintf128.h
740+
DEPENDS
741+
libc.src.__support.macros.properties.types
742+
libc.src.__support.FPUtil.nearest_integer_operations
743+
COMPILE_OPTIONS
744+
-O3
732745
)
733746

734747
add_object_library(
@@ -2949,6 +2962,19 @@ add_entrypoint_object(
29492962
-O3
29502963
)
29512964

2965+
add_entrypoint_object(
2966+
scalbnf128
2967+
SRCS
2968+
scalbnf128.cpp
2969+
HDRS
2970+
../scalbnf128.h
2971+
DEPENDS
2972+
libc.src.__support.macros.properties.types
2973+
libc.src.__support.FPUtil.manipulation_functions
2974+
COMPILE_OPTIONS
2975+
-O3
2976+
)
2977+
29522978
add_entrypoint_object(
29532979
fmaf
29542980
SRCS
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
//===-- Implementation of nearbyintf128 function --------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#include "src/math/nearbyintf128.h"
10+
#include "src/__support/FPUtil/NearestIntegerOperations.h"
11+
#include "src/__support/common.h"
12+
13+
namespace LIBC_NAMESPACE {
14+
15+
LLVM_LIBC_FUNCTION(float128, nearbyintf128, (float128 x)) {
16+
return fputil::round_using_current_rounding_mode(x);
17+
}
18+
19+
} // namespace LIBC_NAMESPACE

libc/src/math/generic/scalbnf128.cpp

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
//===-- Implementation of scalbnf128 function -----------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#include "src/math/scalbnf128.h"
10+
#include "src/__support/FPUtil/ManipulationFunctions.h"
11+
#include "src/__support/common.h"
12+
13+
namespace LIBC_NAMESPACE {
14+
15+
LLVM_LIBC_FUNCTION(float128, scalbnf128, (float128 x, int n)) {
16+
// TODO: should be switched to use `FLT_RADIX` in hdr/float_macros.h" instead
17+
// see: https://github.com/llvm/llvm-project/issues/90496
18+
#if !defined(__FLT_RADIX__)
19+
#error __FLT_RADIX__ undefined.
20+
#elif __FLT_RADIX__ != 2
21+
#error __FLT_RADIX__!=2, unimplemented.
22+
#else
23+
return fputil::ldexp(x, n);
24+
#endif
25+
}
26+
27+
} // namespace LIBC_NAMESPACE

libc/src/math/nearbyintf128.h

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
//===-- Implementation header for nearbyintf128 -----------------*- C++ -*-===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#ifndef LLVM_LIBC_SRC_MATH_NEARBYINTF128_H
10+
#define LLVM_LIBC_SRC_MATH_NEARBYINTF128_H
11+
12+
#include "src/__support/macros/properties/types.h"
13+
14+
namespace LIBC_NAMESPACE {
15+
16+
float128 nearbyintf128(float128 x);
17+
18+
} // namespace LIBC_NAMESPACE
19+
20+
#endif // LLVM_LIBC_SRC_MATH_NEARBYINTF128_H

libc/src/math/scalbnf128.h

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
//===-- Implementation header for scalbnf128 --------------------*- C++ -*-===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#ifndef LLVM_LIBC_SRC_MATH_SCALBNF128_H
10+
#define LLVM_LIBC_SRC_MATH_SCALBNF128_H
11+
12+
#include "src/__support/macros/properties/types.h"
13+
14+
namespace LIBC_NAMESPACE {
15+
16+
float128 scalbnf128(float128 x, int n);
17+
18+
} // namespace LIBC_NAMESPACE
19+
20+
#endif // LLVM_LIBC_SRC_MATH_SCALBNF128_H

libc/test/UnitTest/FPMatcher.h

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,4 +219,25 @@ template <typename T> struct FPTest : public Test {
219219
} \
220220
} while (0)
221221

222+
#define EXPECT_FP_EQ_ROUNDING_MODE(expected, actual, rounding_mode) \
223+
do { \
224+
using namespace LIBC_NAMESPACE::fputil::testing; \
225+
ForceRoundingMode __r((rounding_mode)); \
226+
if (__r.success) { \
227+
EXPECT_FP_EQ((expected), (actual)); \
228+
} \
229+
} while (0)
230+
231+
#define EXPECT_FP_EQ_ROUNDING_NEAREST(expected, actual) \
232+
EXPECT_FP_EQ_ROUNDING_MODE((expected), (actual), RoundingMode::Nearest)
233+
234+
#define EXPECT_FP_EQ_ROUNDING_UPWARD(expected, actual) \
235+
EXPECT_FP_EQ_ROUNDING_MODE((expected), (actual), RoundingMode::Upward)
236+
237+
#define EXPECT_FP_EQ_ROUNDING_DOWNWARD(expected, actual) \
238+
EXPECT_FP_EQ_ROUNDING_MODE((expected), (actual), RoundingMode::Downward)
239+
240+
#define EXPECT_FP_EQ_ROUNDING_TOWARD_ZERO(expected, actual) \
241+
EXPECT_FP_EQ_ROUNDING_MODE((expected), (actual), RoundingMode::TowardZero)
242+
222243
#endif // LLVM_LIBC_TEST_UNITTEST_FPMATCHER_H

libc/test/src/math/CMakeLists.txt

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1650,6 +1650,21 @@ add_fp_unittest(
16501650
libc.src.__support.FPUtil.normal_float
16511651
)
16521652

1653+
add_fp_unittest(
1654+
scalbnf128_test
1655+
NEED_MPFR
1656+
SUITE
1657+
libc-math-unittests
1658+
SRCS
1659+
scalbnf128_test.cpp
1660+
HDRS
1661+
ScalbnTest.h
1662+
DEPENDS
1663+
libc.src.math.scalbnf128
1664+
libc.src.__support.FPUtil.fp_bits
1665+
libc.src.__support.FPUtil.normal_float
1666+
)
1667+
16531668
add_fp_unittest(
16541669
erff_test
16551670
NEED_MPFR
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
//===-- Unittests for scalbnf128 ------------------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#include "ScalbnTest.h"
10+
11+
#include "src/math/scalbnf128.h"
12+
13+
LIST_SCALBN_TESTS(float128, LIBC_NAMESPACE::scalbnf128)

0 commit comments

Comments
 (0)