Skip to content

Commit a66079a

Browse files
committed
[libc][math][c23] Add modff16 C23 math function
1 parent ec1c915 commit a66079a

File tree

12 files changed

+97
-5
lines changed

12 files changed

+97
-5
lines changed

libc/config/linux/aarch64/entrypoints.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -527,6 +527,7 @@ if(LIBC_TYPES_HAS_FLOAT16)
527527
libc.src.math.logbf16
528528
libc.src.math.lrintf16
529529
libc.src.math.lroundf16
530+
libc.src.math.modff16
530531
libc.src.math.nanf16
531532
libc.src.math.nearbyintf16
532533
libc.src.math.nextafterf16

libc/config/linux/x86_64/entrypoints.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -560,6 +560,7 @@ if(LIBC_TYPES_HAS_FLOAT16)
560560
libc.src.math.logbf16
561561
libc.src.math.lrintf16
562562
libc.src.math.lroundf16
563+
libc.src.math.modff16
563564
libc.src.math.nanf16
564565
libc.src.math.nearbyintf16
565566
libc.src.math.nextafterf16

libc/docs/math/index.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ Basic Operations
184184
+------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
185185
| lround | |check| | |check| | |check| | |check| | |check| | 7.12.9.7 | F.10.6.7 |
186186
+------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
187-
| modf | |check| | |check| | |check| | | |check| | 7.12.6.18 | F.10.3.18 |
187+
| modf | |check| | |check| | |check| | |check| | |check| | 7.12.6.18 | F.10.3.18 |
188188
+------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
189189
| nan | |check| | |check| | |check| | |check| | |check| | 7.12.11.2 | F.10.8.2 |
190190
+------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+

libc/spec/spec.td

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@ def IntPtr : PtrType<IntType>;
115115
def RestrictedIntPtr : RestrictedPtrType<IntType>;
116116
def FloatPtr : PtrType<FloatType>;
117117
def DoublePtr : PtrType<DoubleType>;
118+
def Float16Ptr : PtrType<Float16Type>;
118119
def Float128Ptr : PtrType<Float128Type>;
119120
def UnsignedCharPtr : PtrType<UnsignedCharType>;
120121

libc/spec/stdc.td

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -556,6 +556,7 @@ def StdC : StandardSpec<"stdc"> {
556556
FunctionSpec<"modf", RetValSpec<DoubleType>, [ArgSpec<DoubleType>, ArgSpec<DoublePtr>]>,
557557
FunctionSpec<"modff", RetValSpec<FloatType>, [ArgSpec<FloatType>, ArgSpec<FloatPtr>]>,
558558
FunctionSpec<"modfl", RetValSpec<LongDoubleType>, [ArgSpec<LongDoubleType>, ArgSpec<LongDoublePtr>]>,
559+
GuardedFunctionSpec<"modff16", RetValSpec<Float16Type>, [ArgSpec<Float16Type>, ArgSpec<Float16Ptr>], "LIBC_TYPES_HAS_FLOAT16">,
559560
GuardedFunctionSpec<"modff128", RetValSpec<Float128Type>, [ArgSpec<Float128Type>, ArgSpec<Float128Ptr>], "LIBC_TYPES_HAS_FLOAT128">,
560561

561562
FunctionSpec<"cos", RetValSpec<DoubleType>, [ArgSpec<DoubleType>]>,

libc/src/math/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,7 @@ add_math_entrypoint_object(lroundf128)
272272
add_math_entrypoint_object(modf)
273273
add_math_entrypoint_object(modff)
274274
add_math_entrypoint_object(modfl)
275+
add_math_entrypoint_object(modff16)
275276
add_math_entrypoint_object(modff128)
276277

277278
add_math_entrypoint_object(nan)

libc/src/math/generic/CMakeLists.txt

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1786,6 +1786,19 @@ add_entrypoint_object(
17861786
-O3
17871787
)
17881788

1789+
add_entrypoint_object(
1790+
modff16
1791+
SRCS
1792+
modff16.cpp
1793+
HDRS
1794+
../modff16.h
1795+
DEPENDS
1796+
libc.src.__support.macros.properties.types
1797+
libc.src.__support.FPUtil.manipulation_functions
1798+
COMPILE_OPTIONS
1799+
-O3
1800+
)
1801+
17891802
add_entrypoint_object(
17901803
modff128
17911804
SRCS

libc/src/math/generic/modff16.cpp

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

libc/src/math/modff16.h

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

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1660,6 +1660,7 @@ add_fp_unittest(
16601660
ModfTest.h
16611661
DEPENDS
16621662
libc.src.math.modf
1663+
libc.src.__support.CPP.algorithm
16631664
libc.src.__support.FPUtil.basic_operations
16641665
libc.src.__support.FPUtil.nearest_integer_operations
16651666
)
@@ -1674,6 +1675,7 @@ add_fp_unittest(
16741675
ModfTest.h
16751676
DEPENDS
16761677
libc.src.math.modff
1678+
libc.src.__support.CPP.algorithm
16771679
libc.src.__support.FPUtil.basic_operations
16781680
libc.src.__support.FPUtil.nearest_integer_operations
16791681
)
@@ -1688,6 +1690,22 @@ add_fp_unittest(
16881690
ModfTest.h
16891691
DEPENDS
16901692
libc.src.math.modfl
1693+
libc.src.__support.CPP.algorithm
1694+
libc.src.__support.FPUtil.basic_operations
1695+
libc.src.__support.FPUtil.nearest_integer_operations
1696+
)
1697+
1698+
add_fp_unittest(
1699+
modff16_test
1700+
SUITE
1701+
libc-math-smoke-tests
1702+
SRCS
1703+
modff16_test.cpp
1704+
HDRS
1705+
ModfTest.h
1706+
DEPENDS
1707+
libc.src.math.modff16
1708+
libc.src.__support.CPP.algorithm
16911709
libc.src.__support.FPUtil.basic_operations
16921710
libc.src.__support.FPUtil.nearest_integer_operations
16931711
)
@@ -1702,6 +1720,7 @@ add_fp_unittest(
17021720
ModfTest.h
17031721
DEPENDS
17041722
libc.src.math.modff128
1723+
libc.src.__support.CPP.algorithm
17051724
libc.src.__support.FPUtil.basic_operations
17061725
libc.src.__support.FPUtil.nearest_integer_operations
17071726
)

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

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
//
77
//===----------------------------------------------------------------------===//
88

9+
#include "src/__support/CPP/algorithm.h"
910
#include "src/__support/FPUtil/BasicOperations.h"
1011
#include "src/__support/FPUtil/NearestIntegerOperations.h"
1112
#include "test/UnitTest/FEnvSafeTest.h"
@@ -83,10 +84,12 @@ class ModfTest : public LIBC_NAMESPACE::testing::FEnvSafeTest {
8384
}
8485

8586
void testRange(ModfFunc func) {
86-
constexpr StorageType COUNT = 100'000;
87-
constexpr StorageType STEP = STORAGE_MAX / COUNT;
88-
for (StorageType i = 0, v = 0; i <= COUNT; ++i, v += STEP) {
89-
FPBits x_bits = FPBits(v);
87+
constexpr int COUNT = 100'000;
88+
constexpr StorageType STEP = LIBC_NAMESPACE::cpp::max(
89+
static_cast<StorageType>(STORAGE_MAX / COUNT), StorageType(1));
90+
StorageType v = 0;
91+
for (int i = 0; i <= COUNT; ++i, v += STEP) {
92+
FPBits x_bits(v);
9093
if (x_bits.is_zero() || x_bits.is_inf_or_nan())
9194
continue;
9295

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
//===-- Unittests for modff16 ---------------------------------------------===//
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/modff16.h"
12+
13+
LIST_MODF_TESTS(float16, LIBC_NAMESPACE::modff16)

0 commit comments

Comments
 (0)