Skip to content

Commit 8ff96eb

Browse files
authored
[libc][math][c23] Add nextup{,f,f128} and nextdown{,f,f128} functions (#85431)
See #85283. I had a test for `nextdownl` that was failing and I thought I should add `nextupl` and `nextdownl` later and first make a PR for the other functions. cc @lntue
1 parent f0863a0 commit 8ff96eb

27 files changed

+598
-2
lines changed

libc/config/linux/x86_64/entrypoints.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -429,6 +429,10 @@ set(TARGET_LIBM_ENTRYPOINTS
429429
libc.src.math.nexttoward
430430
libc.src.math.nexttowardf
431431
libc.src.math.nexttowardl
432+
libc.src.math.nextdown
433+
libc.src.math.nextdownf
434+
libc.src.math.nextup
435+
libc.src.math.nextupf
432436
libc.src.math.powf
433437
libc.src.math.remainderf
434438
libc.src.math.remainder
@@ -483,6 +487,8 @@ if(LIBC_TYPES_HAS_FLOAT128)
483487
libc.src.math.modff128
484488
libc.src.math.nanf128
485489
libc.src.math.nextafterf128
490+
libc.src.math.nextdownf128
491+
libc.src.math.nextupf128
486492
libc.src.math.rintf128
487493
libc.src.math.roundf128
488494
libc.src.math.sqrtf128

libc/spec/stdc.td

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -536,6 +536,14 @@ def StdC : StandardSpec<"stdc"> {
536536
FunctionSpec<"nexttoward", RetValSpec<DoubleType>, [ArgSpec<DoubleType>, ArgSpec<LongDoubleType>]>,
537537
FunctionSpec<"nexttowardl", RetValSpec<LongDoubleType>, [ArgSpec<LongDoubleType>, ArgSpec<LongDoubleType>]>,
538538

539+
FunctionSpec<"nextdown", RetValSpec<DoubleType>, [ArgSpec<DoubleType>]>,
540+
FunctionSpec<"nextdownf", RetValSpec<FloatType>, [ArgSpec<FloatType>]>,
541+
GuardedFunctionSpec<"nextdownf128", RetValSpec<Float128Type>, [ArgSpec<Float128Type>], "LIBC_TYPES_HAS_FLOAT128">,
542+
543+
FunctionSpec<"nextup", RetValSpec<DoubleType>, [ArgSpec<DoubleType>]>,
544+
FunctionSpec<"nextupf", RetValSpec<FloatType>, [ArgSpec<FloatType>]>,
545+
GuardedFunctionSpec<"nextupf128", RetValSpec<Float128Type>, [ArgSpec<Float128Type>], "LIBC_TYPES_HAS_FLOAT128">,
546+
539547
FunctionSpec<"powf", RetValSpec<FloatType>, [ArgSpec<FloatType>, ArgSpec<FloatType>]>,
540548
FunctionSpec<"pow", RetValSpec<DoubleType>, [ArgSpec<DoubleType>, ArgSpec<DoubleType>]>,
541549

libc/src/__support/FPUtil/ManipulationFunctions.h

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,30 @@ LIBC_INLINE T nextafter(T from, U to) {
230230
return from_bits.get_val();
231231
}
232232

233+
template <bool IsDown, typename T,
234+
cpp::enable_if_t<cpp::is_floating_point_v<T>, int> = 0>
235+
LIBC_INLINE constexpr T nextupdown(T x) {
236+
constexpr Sign sign = IsDown ? Sign::NEG : Sign::POS;
237+
238+
FPBits<T> xbits(x);
239+
if (xbits.is_nan() || xbits == FPBits<T>::max_normal(sign) ||
240+
xbits == FPBits<T>::inf(sign))
241+
return x;
242+
243+
using StorageType = typename FPBits<T>::StorageType;
244+
if (x != T(0)) {
245+
if (xbits.sign() == sign) {
246+
xbits = FPBits<T>(StorageType(xbits.uintval() + 1));
247+
} else {
248+
xbits = FPBits<T>(StorageType(xbits.uintval() - 1));
249+
}
250+
} else {
251+
xbits = FPBits<T>::min_subnormal(sign);
252+
}
253+
254+
return xbits.get_val();
255+
}
256+
233257
} // namespace fputil
234258
} // namespace LIBC_NAMESPACE
235259

libc/src/math/CMakeLists.txt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,14 @@ add_math_entrypoint_object(nexttoward)
205205
add_math_entrypoint_object(nexttowardf)
206206
add_math_entrypoint_object(nexttowardl)
207207

208+
add_math_entrypoint_object(nextdown)
209+
add_math_entrypoint_object(nextdownf)
210+
add_math_entrypoint_object(nextdownf128)
211+
212+
add_math_entrypoint_object(nextup)
213+
add_math_entrypoint_object(nextupf)
214+
add_math_entrypoint_object(nextupf128)
215+
208216
add_math_entrypoint_object(pow)
209217
add_math_entrypoint_object(powf)
210218

libc/src/math/generic/CMakeLists.txt

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1878,6 +1878,80 @@ add_entrypoint_object(
18781878
-O3
18791879
)
18801880

1881+
add_entrypoint_object(
1882+
nextdown
1883+
SRCS
1884+
nextdown.cpp
1885+
HDRS
1886+
../nextdown.h
1887+
DEPENDS
1888+
libc.src.__support.FPUtil.manipulation_functions
1889+
COMPILE_OPTIONS
1890+
-O3
1891+
)
1892+
1893+
add_entrypoint_object(
1894+
nextdownf
1895+
SRCS
1896+
nextdownf.cpp
1897+
HDRS
1898+
../nextdownf.h
1899+
DEPENDS
1900+
libc.src.__support.FPUtil.manipulation_functions
1901+
COMPILE_OPTIONS
1902+
-O3
1903+
)
1904+
1905+
add_entrypoint_object(
1906+
nextdownf128
1907+
SRCS
1908+
nextdownf128.cpp
1909+
HDRS
1910+
../nextdownf128.h
1911+
DEPENDS
1912+
libc.src.__support.macros.properties.types
1913+
libc.src.__support.FPUtil.manipulation_functions
1914+
COMPILE_OPTIONS
1915+
-O3
1916+
)
1917+
1918+
add_entrypoint_object(
1919+
nextup
1920+
SRCS
1921+
nextup.cpp
1922+
HDRS
1923+
../nextup.h
1924+
DEPENDS
1925+
libc.src.__support.FPUtil.manipulation_functions
1926+
COMPILE_OPTIONS
1927+
-O3
1928+
)
1929+
1930+
add_entrypoint_object(
1931+
nextupf
1932+
SRCS
1933+
nextupf.cpp
1934+
HDRS
1935+
../nextupf.h
1936+
DEPENDS
1937+
libc.src.__support.FPUtil.manipulation_functions
1938+
COMPILE_OPTIONS
1939+
-O3
1940+
)
1941+
1942+
add_entrypoint_object(
1943+
nextupf128
1944+
SRCS
1945+
nextupf128.cpp
1946+
HDRS
1947+
../nextupf128.h
1948+
DEPENDS
1949+
libc.src.__support.macros.properties.types
1950+
libc.src.__support.FPUtil.manipulation_functions
1951+
COMPILE_OPTIONS
1952+
-O3
1953+
)
1954+
18811955
add_entrypoint_object(
18821956
fmod
18831957
SRCS

libc/src/math/generic/nextdown.cpp

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

libc/src/math/generic/nextdownf.cpp

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

libc/src/math/generic/nextup.cpp

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

libc/src/math/generic/nextupf.cpp

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

libc/src/math/generic/nextupf128.cpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
//===-- Implementation of nextupf128 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/nextupf128.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, nextupf128, (float128 x)) {
16+
return fputil::nextupdown</*IsDown=*/false>(x);
17+
}
18+
19+
} // namespace LIBC_NAMESPACE

libc/src/math/nextdown.h

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

libc/src/math/nextdownf.h

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

libc/src/math/nextdownf128.h

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
//===-- Implementation header for nextdownf128 ------------------*- 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_NEXTDOWNF128_H
10+
#define LLVM_LIBC_SRC_MATH_NEXTDOWNF128_H
11+
12+
#include "src/__support/macros/properties/types.h"
13+
14+
namespace LIBC_NAMESPACE {
15+
16+
float128 nextdownf128(float128 x);
17+
18+
} // namespace LIBC_NAMESPACE
19+
20+
#endif // LLVM_LIBC_SRC_MATH_NEXTDOWNF128_H

libc/src/math/nextup.h

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

libc/src/math/nextupf.h

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

libc/src/math/nextupf128.h

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
//===-- Implementation header for nextupf128 --------------------*- 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_NEXTUPF128_H
10+
#define LLVM_LIBC_SRC_MATH_NEXTUPF128_H
11+
12+
#include "src/__support/macros/properties/types.h"
13+
14+
namespace LIBC_NAMESPACE {
15+
16+
float128 nextupf128(float128 x);
17+
18+
} // namespace LIBC_NAMESPACE
19+
20+
#endif // LLVM_LIBC_SRC_MATH_NEXTUPF128_H

0 commit comments

Comments
 (0)