Skip to content

[libc][math][c23] Add modff128 C23 math function. #84532

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions libc/config/linux/aarch64/entrypoints.txt
Original file line number Diff line number Diff line change
Expand Up @@ -434,6 +434,7 @@ if(LIBC_TYPES_HAS_FLOAT128)
libc.src.math.llroundf128
libc.src.math.lrintf128
libc.src.math.lroundf128
libc.src.math.modff128
libc.src.math.rintf128
libc.src.math.roundf128
libc.src.math.sqrtf128
Expand Down
1 change: 1 addition & 0 deletions libc/config/linux/riscv/entrypoints.txt
Original file line number Diff line number Diff line change
Expand Up @@ -442,6 +442,7 @@ if(LIBC_TYPES_HAS_FLOAT128)
libc.src.math.llroundf128
libc.src.math.lrintf128
libc.src.math.lroundf128
libc.src.math.modff128
libc.src.math.rintf128
libc.src.math.roundf128
libc.src.math.sqrtf128
Expand Down
1 change: 1 addition & 0 deletions libc/config/linux/x86_64/entrypoints.txt
Original file line number Diff line number Diff line change
Expand Up @@ -472,6 +472,7 @@ if(LIBC_TYPES_HAS_FLOAT128)
libc.src.math.llroundf128
libc.src.math.lrintf128
libc.src.math.lroundf128
libc.src.math.modff128
libc.src.math.rintf128
libc.src.math.roundf128
libc.src.math.sqrtf128
Expand Down
2 changes: 2 additions & 0 deletions libc/docs/math/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,8 @@ Basic Operations
+--------------+---------+---------+---------+---------+---------+---------+---------+---------+---------+---------+---------+---------+
| modfl | |check| | |check| | |check| | |check| | |check| | | | |check| | |check| | |check| | | |
+--------------+---------+---------+---------+---------+---------+---------+---------+---------+---------+---------+---------+---------+
| modff128 | |check| | |check| | | |check| | | | | | | | | |
+--------------+---------+---------+---------+---------+---------+---------+---------+---------+---------+---------+---------+---------+
| nan | |check| | |check| | |check| | |check| | |check| | | | |check| | |check| | |check| | | |
+--------------+---------+---------+---------+---------+---------+---------+---------+---------+---------+---------+---------+---------+
| nanf | |check| | |check| | |check| | |check| | |check| | | | |check| | |check| | |check| | | |
Expand Down
1 change: 1 addition & 0 deletions libc/spec/spec.td
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ def IntPtr : PtrType<IntType>;
def RestrictedIntPtr : RestrictedPtrType<IntType>;
def FloatPtr : PtrType<FloatType>;
def DoublePtr : PtrType<DoubleType>;
def Float128Ptr : PtrType<Float128Type>;
def UnsignedCharPtr : PtrType<UnsignedCharType>;

def SigHandlerT : NamedType<"__sighandler_t">;
Expand Down
1 change: 1 addition & 0 deletions libc/spec/stdc.td
Original file line number Diff line number Diff line change
Expand Up @@ -451,6 +451,7 @@ def StdC : StandardSpec<"stdc"> {
FunctionSpec<"modf", RetValSpec<DoubleType>, [ArgSpec<DoubleType>, ArgSpec<DoublePtr>]>,
FunctionSpec<"modff", RetValSpec<FloatType>, [ArgSpec<FloatType>, ArgSpec<FloatPtr>]>,
FunctionSpec<"modfl", RetValSpec<LongDoubleType>, [ArgSpec<LongDoubleType>, ArgSpec<LongDoublePtr>]>,
GuardedFunctionSpec<"modff128", RetValSpec<Float128Type>, [ArgSpec<Float128Type>, ArgSpec<Float128Ptr>], "LIBC_TYPES_HAS_FLOAT128">,

FunctionSpec<"cos", RetValSpec<DoubleType>, [ArgSpec<DoubleType>]>,
FunctionSpec<"cosf", RetValSpec<FloatType>, [ArgSpec<FloatType>]>,
Expand Down
1 change: 1 addition & 0 deletions libc/src/math/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,7 @@ add_math_entrypoint_object(lroundf128)
add_math_entrypoint_object(modf)
add_math_entrypoint_object(modff)
add_math_entrypoint_object(modfl)
add_math_entrypoint_object(modff128)

add_math_entrypoint_object(nan)
add_math_entrypoint_object(nanf)
Expand Down
19 changes: 16 additions & 3 deletions libc/src/math/generic/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1407,7 +1407,7 @@ add_entrypoint_object(
DEPENDS
libc.src.__support.FPUtil.manipulation_functions
COMPILE_OPTIONS
-O2
-O3
)

add_entrypoint_object(
Expand All @@ -1419,7 +1419,7 @@ add_entrypoint_object(
DEPENDS
libc.src.__support.FPUtil.manipulation_functions
COMPILE_OPTIONS
-O2
-O3
)

add_entrypoint_object(
Expand All @@ -1431,7 +1431,20 @@ add_entrypoint_object(
DEPENDS
libc.src.__support.FPUtil.manipulation_functions
COMPILE_OPTIONS
-O2
-O3
)

add_entrypoint_object(
modff128
SRCS
modff128.cpp
HDRS
../modff128.h
DEPENDS
libc.src.__support.macros.properties.types
libc.src.__support.FPUtil.manipulation_functions
COMPILE_OPTIONS
-O3
)

add_entrypoint_object(
Expand Down
19 changes: 19 additions & 0 deletions libc/src/math/generic/modff128.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
//===-- Implementation of modff128 function -------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#include "src/math/modff128.h"
#include "src/__support/FPUtil/ManipulationFunctions.h"
#include "src/__support/common.h"

namespace LIBC_NAMESPACE {

LLVM_LIBC_FUNCTION(float128, modff128, (float128 x, float128 *iptr)) {
return fputil::modf(x, *iptr);
}

} // namespace LIBC_NAMESPACE
20 changes: 20 additions & 0 deletions libc/src/math/modff128.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
//===-- Implementation header for modff128 -----------------------*- C++-*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#ifndef LLVM_LIBC_SRC_MATH_MODFF128_H
#define LLVM_LIBC_SRC_MATH_MODFF128_H

#include "src/__support/macros/properties/types.h"

namespace LIBC_NAMESPACE {

float128 modff128(float128 x, float128 *iptr);

} // namespace LIBC_NAMESPACE

#endif // LLVM_LIBC_SRC_MATH_MODFF128_H
19 changes: 15 additions & 4 deletions libc/test/src/math/smoke/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1078,8 +1078,6 @@ add_fp_unittest(
libc.src.math.modf
libc.src.__support.FPUtil.basic_operations
libc.src.__support.FPUtil.nearest_integer_operations
# Requires C++ limits.
UNIT_TEST_ONLY
)

add_fp_unittest(
Expand All @@ -1095,8 +1093,6 @@ add_fp_unittest(
libc.src.math.modff
libc.src.__support.FPUtil.basic_operations
libc.src.__support.FPUtil.nearest_integer_operations
# Requires C++ limits.
UNIT_TEST_ONLY
)

add_fp_unittest(
Expand All @@ -1114,6 +1110,21 @@ add_fp_unittest(
libc.src.__support.FPUtil.nearest_integer_operations
)

add_fp_unittest(
modff128_test
SUITE
libc-math-smoke-tests
SRCS
modff128_test.cpp
HDRS
ModfTest.h
DEPENDS
libc.include.math
libc.src.math.modff128
libc.src.__support.FPUtil.basic_operations
libc.src.__support.FPUtil.nearest_integer_operations
)

add_fp_unittest(
fdimf_test
SUITE
Expand Down
6 changes: 4 additions & 2 deletions libc/test/src/math/smoke/ModfTest.h
Original file line number Diff line number Diff line change
Expand Up @@ -84,10 +84,12 @@ template <typename T> class ModfTest : public LIBC_NAMESPACE::testing::Test {
constexpr StorageType COUNT = 100'000;
constexpr StorageType STEP = STORAGE_MAX / COUNT;
for (StorageType i = 0, v = 0; i <= COUNT; ++i, v += STEP) {
T x = FPBits(v).get_val();
if (isnan(x) || isinf(x) || x == T(0.0))
FPBits x_bits = FPBits(v);
if (x_bits.is_zero() || x_bits.is_inf_or_nan())
continue;

T x = x_bits.get_val();

T integral;
T frac = func(x, &integral);
ASSERT_TRUE(LIBC_NAMESPACE::fputil::abs(frac) < 1.0l);
Expand Down
13 changes: 13 additions & 0 deletions libc/test/src/math/smoke/modff128_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
//===-- Unittests for modff128 --------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#include "ModfTest.h"

#include "src/math/modff128.h"

LIST_MODF_TESTS(float128, LIBC_NAMESPACE::modff128)