Skip to content

Commit 99f5e96

Browse files
authored
[libc][math][c23] Add modff128 C23 math function. (llvm#84532)
1 parent 92d7aca commit 99f5e96

File tree

13 files changed

+95
-9
lines changed

13 files changed

+95
-9
lines changed

libc/config/linux/aarch64/entrypoints.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -434,6 +434,7 @@ if(LIBC_TYPES_HAS_FLOAT128)
434434
libc.src.math.llroundf128
435435
libc.src.math.lrintf128
436436
libc.src.math.lroundf128
437+
libc.src.math.modff128
437438
libc.src.math.rintf128
438439
libc.src.math.roundf128
439440
libc.src.math.sqrtf128

libc/config/linux/riscv/entrypoints.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -442,6 +442,7 @@ if(LIBC_TYPES_HAS_FLOAT128)
442442
libc.src.math.llroundf128
443443
libc.src.math.lrintf128
444444
libc.src.math.lroundf128
445+
libc.src.math.modff128
445446
libc.src.math.rintf128
446447
libc.src.math.roundf128
447448
libc.src.math.sqrtf128

libc/config/linux/x86_64/entrypoints.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -472,6 +472,7 @@ if(LIBC_TYPES_HAS_FLOAT128)
472472
libc.src.math.llroundf128
473473
libc.src.math.lrintf128
474474
libc.src.math.lroundf128
475+
libc.src.math.modff128
475476
libc.src.math.rintf128
476477
libc.src.math.roundf128
477478
libc.src.math.sqrtf128

libc/docs/math/index.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,8 @@ Basic Operations
249249
+--------------+---------+---------+---------+---------+---------+---------+---------+---------+---------+---------+---------+---------+
250250
| modfl | |check| | |check| | |check| | |check| | |check| | | | |check| | |check| | |check| | | |
251251
+--------------+---------+---------+---------+---------+---------+---------+---------+---------+---------+---------+---------+---------+
252+
| modff128 | |check| | |check| | | |check| | | | | | | | | |
253+
+--------------+---------+---------+---------+---------+---------+---------+---------+---------+---------+---------+---------+---------+
252254
| nan | |check| | |check| | |check| | |check| | |check| | | | |check| | |check| | |check| | | |
253255
+--------------+---------+---------+---------+---------+---------+---------+---------+---------+---------+---------+---------+---------+
254256
| nanf | |check| | |check| | |check| | |check| | |check| | | | |check| | |check| | |check| | | |

libc/spec/spec.td

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@ def IntPtr : PtrType<IntType>;
111111
def RestrictedIntPtr : RestrictedPtrType<IntType>;
112112
def FloatPtr : PtrType<FloatType>;
113113
def DoublePtr : PtrType<DoubleType>;
114+
def Float128Ptr : PtrType<Float128Type>;
114115
def UnsignedCharPtr : PtrType<UnsignedCharType>;
115116

116117
def SigHandlerT : NamedType<"__sighandler_t">;

libc/spec/stdc.td

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -451,6 +451,7 @@ def StdC : StandardSpec<"stdc"> {
451451
FunctionSpec<"modf", RetValSpec<DoubleType>, [ArgSpec<DoubleType>, ArgSpec<DoublePtr>]>,
452452
FunctionSpec<"modff", RetValSpec<FloatType>, [ArgSpec<FloatType>, ArgSpec<FloatPtr>]>,
453453
FunctionSpec<"modfl", RetValSpec<LongDoubleType>, [ArgSpec<LongDoubleType>, ArgSpec<LongDoublePtr>]>,
454+
GuardedFunctionSpec<"modff128", RetValSpec<Float128Type>, [ArgSpec<Float128Type>, ArgSpec<Float128Ptr>], "LIBC_TYPES_HAS_FLOAT128">,
454455

455456
FunctionSpec<"cos", RetValSpec<DoubleType>, [ArgSpec<DoubleType>]>,
456457
FunctionSpec<"cosf", RetValSpec<FloatType>, [ArgSpec<FloatType>]>,

libc/src/math/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,7 @@ add_math_entrypoint_object(lroundf128)
183183
add_math_entrypoint_object(modf)
184184
add_math_entrypoint_object(modff)
185185
add_math_entrypoint_object(modfl)
186+
add_math_entrypoint_object(modff128)
186187

187188
add_math_entrypoint_object(nan)
188189
add_math_entrypoint_object(nanf)

libc/src/math/generic/CMakeLists.txt

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1407,7 +1407,7 @@ add_entrypoint_object(
14071407
DEPENDS
14081408
libc.src.__support.FPUtil.manipulation_functions
14091409
COMPILE_OPTIONS
1410-
-O2
1410+
-O3
14111411
)
14121412

14131413
add_entrypoint_object(
@@ -1419,7 +1419,7 @@ add_entrypoint_object(
14191419
DEPENDS
14201420
libc.src.__support.FPUtil.manipulation_functions
14211421
COMPILE_OPTIONS
1422-
-O2
1422+
-O3
14231423
)
14241424

14251425
add_entrypoint_object(
@@ -1431,7 +1431,20 @@ add_entrypoint_object(
14311431
DEPENDS
14321432
libc.src.__support.FPUtil.manipulation_functions
14331433
COMPILE_OPTIONS
1434-
-O2
1434+
-O3
1435+
)
1436+
1437+
add_entrypoint_object(
1438+
modff128
1439+
SRCS
1440+
modff128.cpp
1441+
HDRS
1442+
../modff128.h
1443+
DEPENDS
1444+
libc.src.__support.macros.properties.types
1445+
libc.src.__support.FPUtil.manipulation_functions
1446+
COMPILE_OPTIONS
1447+
-O3
14351448
)
14361449

14371450
add_entrypoint_object(

libc/src/math/generic/modff128.cpp

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

libc/src/math/modff128.h

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

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

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1078,8 +1078,6 @@ add_fp_unittest(
10781078
libc.src.math.modf
10791079
libc.src.__support.FPUtil.basic_operations
10801080
libc.src.__support.FPUtil.nearest_integer_operations
1081-
# Requires C++ limits.
1082-
UNIT_TEST_ONLY
10831081
)
10841082

10851083
add_fp_unittest(
@@ -1095,8 +1093,6 @@ add_fp_unittest(
10951093
libc.src.math.modff
10961094
libc.src.__support.FPUtil.basic_operations
10971095
libc.src.__support.FPUtil.nearest_integer_operations
1098-
# Requires C++ limits.
1099-
UNIT_TEST_ONLY
11001096
)
11011097

11021098
add_fp_unittest(
@@ -1114,6 +1110,21 @@ add_fp_unittest(
11141110
libc.src.__support.FPUtil.nearest_integer_operations
11151111
)
11161112

1113+
add_fp_unittest(
1114+
modff128_test
1115+
SUITE
1116+
libc-math-smoke-tests
1117+
SRCS
1118+
modff128_test.cpp
1119+
HDRS
1120+
ModfTest.h
1121+
DEPENDS
1122+
libc.include.math
1123+
libc.src.math.modff128
1124+
libc.src.__support.FPUtil.basic_operations
1125+
libc.src.__support.FPUtil.nearest_integer_operations
1126+
)
1127+
11171128
add_fp_unittest(
11181129
fdimf_test
11191130
SUITE

libc/test/src/math/smoke/ModfTest.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,10 +84,12 @@ template <typename T> class ModfTest : public LIBC_NAMESPACE::testing::Test {
8484
constexpr StorageType COUNT = 100'000;
8585
constexpr StorageType STEP = STORAGE_MAX / COUNT;
8686
for (StorageType i = 0, v = 0; i <= COUNT; ++i, v += STEP) {
87-
T x = FPBits(v).get_val();
88-
if (isnan(x) || isinf(x) || x == T(0.0))
87+
FPBits x_bits = FPBits(v);
88+
if (x_bits.is_zero() || x_bits.is_inf_or_nan())
8989
continue;
9090

91+
T x = x_bits.get_val();
92+
9193
T integral;
9294
T frac = func(x, &integral);
9395
ASSERT_TRUE(LIBC_NAMESPACE::fputil::abs(frac) < 1.0l);
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
//===-- Unittests for modff128 --------------------------------------------===//
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 "ModfTest.h"
10+
11+
#include "src/math/modff128.h"
12+
13+
LIST_MODF_TESTS(float128, LIBC_NAMESPACE::modff128)

0 commit comments

Comments
 (0)