Skip to content

Commit a2bad75

Browse files
authored
[libc][math][c23] Add nextupl and nextdownl functions (#85484)
Fixes #85283. cc @lntue
1 parent 84b5178 commit a2bad75

File tree

15 files changed

+239
-1
lines changed

15 files changed

+239
-1
lines changed

libc/config/linux/x86_64/entrypoints.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -431,8 +431,10 @@ set(TARGET_LIBM_ENTRYPOINTS
431431
libc.src.math.nexttowardl
432432
libc.src.math.nextdown
433433
libc.src.math.nextdownf
434+
libc.src.math.nextdownl
434435
libc.src.math.nextup
435436
libc.src.math.nextupf
437+
libc.src.math.nextupl
436438
libc.src.math.powf
437439
libc.src.math.remainderf
438440
libc.src.math.remainder

libc/docs/math/index.rst

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -275,12 +275,28 @@ Basic Operations
275275
+--------------+---------+---------+---------+---------+---------+---------+---------+---------+---------+---------+---------+---------+
276276
| nextafterf128| |check| | |check| | | |check| | | | | | | | | |
277277
+--------------+---------+---------+---------+---------+---------+---------+---------+---------+---------+---------+---------+---------+
278+
| nextdown | |check| | | | | | | | | | | | |
279+
+--------------+---------+---------+---------+---------+---------+---------+---------+---------+---------+---------+---------+---------+
280+
| nextdownf | |check| | | | | | | | | | | | |
281+
+--------------+---------+---------+---------+---------+---------+---------+---------+---------+---------+---------+---------+---------+
282+
| nextdownl | |check| | | | | | | | | | | | |
283+
+--------------+---------+---------+---------+---------+---------+---------+---------+---------+---------+---------+---------+---------+
284+
| nextdownf128 | |check| | | | | | | | | | | | |
285+
+--------------+---------+---------+---------+---------+---------+---------+---------+---------+---------+---------+---------+---------+
278286
| nexttoward | |check| | |check| | |check| | |check| | |check| | | | |check| | |check| | |check| | | |
279287
+--------------+---------+---------+---------+---------+---------+---------+---------+---------+---------+---------+---------+---------+
280288
| nexttowardf | |check| | |check| | |check| | |check| | |check| | | | |check| | |check| | |check| | | |
281289
+--------------+---------+---------+---------+---------+---------+---------+---------+---------+---------+---------+---------+---------+
282290
| nexttowardl | |check| | |check| | |check| | |check| | |check| | | | |check| | |check| | |check| | | |
283291
+--------------+---------+---------+---------+---------+---------+---------+---------+---------+---------+---------+---------+---------+
292+
| nextup | |check| | | | | | | | | | | | |
293+
+--------------+---------+---------+---------+---------+---------+---------+---------+---------+---------+---------+---------+---------+
294+
| nextupf | |check| | | | | | | | | | | | |
295+
+--------------+---------+---------+---------+---------+---------+---------+---------+---------+---------+---------+---------+---------+
296+
| nextupl | |check| | | | | | | | | | | | |
297+
+--------------+---------+---------+---------+---------+---------+---------+---------+---------+---------+---------+---------+---------+
298+
| nextupf128 | |check| | | | | | | | | | | | |
299+
+--------------+---------+---------+---------+---------+---------+---------+---------+---------+---------+---------+---------+---------+
284300
| remainder | |check| | |check| | |check| | |check| | |check| | | | |check| | |check| | |check| | | |
285301
+--------------+---------+---------+---------+---------+---------+---------+---------+---------+---------+---------+---------+---------+
286302
| remainderf | |check| | |check| | |check| | |check| | |check| | | | |check| | |check| | |check| | | |

libc/spec/stdc.td

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -538,10 +538,12 @@ def StdC : StandardSpec<"stdc"> {
538538

539539
FunctionSpec<"nextdown", RetValSpec<DoubleType>, [ArgSpec<DoubleType>]>,
540540
FunctionSpec<"nextdownf", RetValSpec<FloatType>, [ArgSpec<FloatType>]>,
541+
FunctionSpec<"nextdownl", RetValSpec<LongDoubleType>, [ArgSpec<LongDoubleType>]>,
541542
GuardedFunctionSpec<"nextdownf128", RetValSpec<Float128Type>, [ArgSpec<Float128Type>], "LIBC_TYPES_HAS_FLOAT128">,
542543

543544
FunctionSpec<"nextup", RetValSpec<DoubleType>, [ArgSpec<DoubleType>]>,
544545
FunctionSpec<"nextupf", RetValSpec<FloatType>, [ArgSpec<FloatType>]>,
546+
FunctionSpec<"nextupl", RetValSpec<LongDoubleType>, [ArgSpec<LongDoubleType>]>,
545547
GuardedFunctionSpec<"nextupf128", RetValSpec<Float128Type>, [ArgSpec<Float128Type>], "LIBC_TYPES_HAS_FLOAT128">,
546548

547549
FunctionSpec<"powf", RetValSpec<FloatType>, [ArgSpec<FloatType>, ArgSpec<FloatType>]>,

libc/src/__support/FPUtil/ManipulationFunctions.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,7 @@ LIBC_INLINE constexpr T nextupdown(T x) {
259259

260260
#ifdef LIBC_TYPES_LONG_DOUBLE_IS_X86_FLOAT80
261261
#include "x86_64/NextAfterLongDouble.h"
262+
#include "x86_64/NextUpDownLongDouble.h"
262263
#endif // LIBC_TYPES_LONG_DOUBLE_IS_X86_FLOAT80
263264

264265
#endif // LLVM_LIBC_SRC___SUPPORT_FPUTIL_MANIPULATIONFUNCTIONS_H
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
//===-- nextupdown implementation for x86 long double numbers ---*- 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___SUPPORT_FPUTIL_X86_64_NEXTUPDOWNLONGDOUBLE_H
10+
#define LLVM_LIBC_SRC___SUPPORT_FPUTIL_X86_64_NEXTUPDOWNLONGDOUBLE_H
11+
12+
#include "src/__support/FPUtil/FPBits.h"
13+
#include "src/__support/macros/attributes.h"
14+
#include "src/__support/macros/properties/architectures.h"
15+
16+
#if !defined(LIBC_TARGET_ARCH_IS_X86)
17+
#error "Invalid include"
18+
#endif
19+
20+
namespace LIBC_NAMESPACE::fputil {
21+
22+
template <bool IsDown>
23+
LIBC_INLINE constexpr long double nextupdown(long double x) {
24+
constexpr Sign sign = IsDown ? Sign::NEG : Sign::POS;
25+
26+
using FPBits_t = FPBits<long double>;
27+
FPBits_t xbits(x);
28+
if (xbits.is_nan() || xbits == FPBits_t::max_normal(sign) ||
29+
xbits == FPBits_t::inf(sign))
30+
return x;
31+
32+
if (x == 0.0l)
33+
return FPBits_t::min_subnormal(sign).get_val();
34+
35+
using StorageType = typename FPBits_t::StorageType;
36+
37+
if (xbits.sign() == sign) {
38+
if (xbits.get_mantissa() == FPBits_t::FRACTION_MASK) {
39+
xbits.set_mantissa(0);
40+
xbits.set_biased_exponent(xbits.get_biased_exponent() + 1);
41+
} else {
42+
xbits = FPBits_t(StorageType(xbits.uintval() + 1));
43+
}
44+
45+
return xbits.get_val();
46+
}
47+
48+
if (xbits.get_mantissa() == 0) {
49+
xbits.set_mantissa(FPBits_t::FRACTION_MASK);
50+
xbits.set_biased_exponent(xbits.get_biased_exponent() - 1);
51+
} else {
52+
xbits = FPBits_t(StorageType(xbits.uintval() - 1));
53+
}
54+
55+
return xbits.get_val();
56+
}
57+
58+
} // namespace LIBC_NAMESPACE::fputil
59+
60+
#endif // LLVM_LIBC_SRC___SUPPORT_FPUTIL_X86_64_NEXTUPDOWNLONGDOUBLE_H

libc/src/math/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,10 +207,12 @@ add_math_entrypoint_object(nexttowardl)
207207

208208
add_math_entrypoint_object(nextdown)
209209
add_math_entrypoint_object(nextdownf)
210+
add_math_entrypoint_object(nextdownl)
210211
add_math_entrypoint_object(nextdownf128)
211212

212213
add_math_entrypoint_object(nextup)
213214
add_math_entrypoint_object(nextupf)
215+
add_math_entrypoint_object(nextupl)
214216
add_math_entrypoint_object(nextupf128)
215217

216218
add_math_entrypoint_object(pow)

libc/src/math/generic/CMakeLists.txt

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1890,6 +1890,18 @@ add_entrypoint_object(
18901890
-O3
18911891
)
18921892

1893+
add_entrypoint_object(
1894+
nextdownl
1895+
SRCS
1896+
nextdownl.cpp
1897+
HDRS
1898+
../nextdownl.h
1899+
DEPENDS
1900+
libc.src.__support.FPUtil.manipulation_functions
1901+
COMPILE_OPTIONS
1902+
-O3
1903+
)
1904+
18931905
add_entrypoint_object(
18941906
nextdownf
18951907
SRCS
@@ -1927,6 +1939,18 @@ add_entrypoint_object(
19271939
-O3
19281940
)
19291941

1942+
add_entrypoint_object(
1943+
nextupl
1944+
SRCS
1945+
nextupl.cpp
1946+
HDRS
1947+
../nextupl.h
1948+
DEPENDS
1949+
libc.src.__support.FPUtil.manipulation_functions
1950+
COMPILE_OPTIONS
1951+
-O3
1952+
)
1953+
19301954
add_entrypoint_object(
19311955
nextupf
19321956
SRCS

libc/src/math/generic/nextdownl.cpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
//===-- Implementation of nextdownl 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/nextdownl.h"
10+
#include "src/__support/FPUtil/ManipulationFunctions.h"
11+
#include "src/__support/common.h"
12+
13+
namespace LIBC_NAMESPACE {
14+
15+
LLVM_LIBC_FUNCTION(long double, nextdownl, (long double x)) {
16+
return fputil::nextupdown</*IsDown=*/true>(x);
17+
}
18+
19+
} // namespace LIBC_NAMESPACE

libc/src/math/generic/nextupl.cpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
//===-- Implementation of nextupl 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/nextupl.h"
10+
#include "src/__support/FPUtil/ManipulationFunctions.h"
11+
#include "src/__support/common.h"
12+
13+
namespace LIBC_NAMESPACE {
14+
15+
LLVM_LIBC_FUNCTION(long double, nextupl, (long double x)) {
16+
return fputil::nextupdown</*IsDown=*/false>(x);
17+
}
18+
19+
} // namespace LIBC_NAMESPACE

libc/src/math/nextdownl.h

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
//===-- Implementation header for nextdownl ---------------------*- 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_NEXTDOWNL_H
10+
#define LLVM_LIBC_SRC_MATH_NEXTDOWNL_H
11+
12+
namespace LIBC_NAMESPACE {
13+
14+
long double nextdownl(long double x);
15+
16+
} // namespace LIBC_NAMESPACE
17+
18+
#endif // LLVM_LIBC_SRC_MATH_NEXTDOWNL_H

libc/src/math/nextupl.h

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
//===-- Implementation header for nextupl -----------------------*- 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_NEXTUPL_H
10+
#define LLVM_LIBC_SRC_MATH_NEXTUPL_H
11+
12+
namespace LIBC_NAMESPACE {
13+
14+
long double nextupl(long double x);
15+
16+
} // namespace LIBC_NAMESPACE
17+
18+
#endif // LLVM_LIBC_SRC_MATH_NEXTUPL_H

libc/test/src/math/smoke/CMakeLists.txt

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1658,6 +1658,20 @@ add_fp_unittest(
16581658
libc.src.__support.FPUtil.manipulation_functions
16591659
)
16601660

1661+
add_fp_unittest(
1662+
nextdownl_test
1663+
SUITE
1664+
libc-math-smoke-tests
1665+
SRCS
1666+
nextdownl_test.cpp
1667+
HDRS
1668+
NextDownTest.h
1669+
DEPENDS
1670+
libc.include.math
1671+
libc.src.math.nextdownl
1672+
libc.src.__support.FPUtil.manipulation_functions
1673+
)
1674+
16611675
add_fp_unittest(
16621676
nextdownf128_test
16631677
SUITE
@@ -1700,6 +1714,20 @@ add_fp_unittest(
17001714
libc.src.__support.FPUtil.manipulation_functions
17011715
)
17021716

1717+
add_fp_unittest(
1718+
nextupl_test
1719+
SUITE
1720+
libc-math-smoke-tests
1721+
SRCS
1722+
nextupl_test.cpp
1723+
HDRS
1724+
NextUpTest.h
1725+
DEPENDS
1726+
libc.include.math
1727+
libc.src.math.nextupl
1728+
libc.src.__support.FPUtil.manipulation_functions
1729+
)
1730+
17031731
add_fp_unittest(
17041732
nextupf128_test
17051733
SUITE
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
//===-- Unittests for nextdownl -------------------------------------------===//
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 "NextDownTest.h"
10+
11+
#include "src/math/nextdownl.h"
12+
13+
LIST_NEXTDOWN_TESTS(long double, LIBC_NAMESPACE::nextdownl)
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
//===-- Unittests for nextupl ---------------------------------------------===//
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 "NextUpTest.h"
10+
11+
#include "src/math/nextupl.h"
12+
13+
LIST_NEXTUP_TESTS(long double, LIBC_NAMESPACE::nextupl)

utils/bazel/llvm-project-overlay/libc/BUILD.bazel

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -728,7 +728,10 @@ libc_support_library(
728728
libc_support_library(
729729
name = "__support_fputil_manipulation_functions",
730730
hdrs = ["src/__support/FPUtil/ManipulationFunctions.h"],
731-
textual_hdrs = ["src/__support/FPUtil/x86_64/NextAfterLongDouble.h"],
731+
textual_hdrs = [
732+
"src/__support/FPUtil/x86_64/NextAfterLongDouble.h"
733+
"src/__support/FPUtil/x86_64/NextUpDownLongDouble.h"
734+
],
732735
deps = [
733736
":__support_common",
734737
":__support_cpp_bit",

0 commit comments

Comments
 (0)