27 changes: 25 additions & 2 deletions libc/src/math/generic/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1859,7 +1859,6 @@ add_entrypoint_object(
HDRS
../fmod.h
DEPENDS
libc.include.math
libc.src.__support.FPUtil.generic.fmod
COMPILE_OPTIONS
-O3
Expand All @@ -1872,7 +1871,31 @@ add_entrypoint_object(
HDRS
../fmodf.h
DEPENDS
libc.include.math
libc.src.__support.FPUtil.generic.fmod
COMPILE_OPTIONS
-O3
)

add_entrypoint_object(
fmodl
SRCS
fmodl.cpp
HDRS
../fmodl.h
DEPENDS
libc.src.__support.FPUtil.generic.fmod
COMPILE_OPTIONS
-O3
)

add_entrypoint_object(
fmodf128
SRCS
fmodf128.cpp
HDRS
../fmodf128.h
DEPENDS
libc.src.__support.macros.properties.types
libc.src.__support.FPUtil.generic.fmod
COMPILE_OPTIONS
-O3
Expand Down
2 changes: 1 addition & 1 deletion libc/src/math/generic/fmodf.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
namespace LIBC_NAMESPACE {

LLVM_LIBC_FUNCTION(float, fmodf, (float x, float y)) {
return fputil::generic::FMod<float>::eval(x, y);
return fputil::generic::FMod<float, uint64_t>::eval(x, y);
}

} // namespace LIBC_NAMESPACE
19 changes: 19 additions & 0 deletions libc/src/math/generic/fmodf128.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
//===-- Single-precision fmodf128 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/fmodf128.h"
#include "src/__support/FPUtil/generic/FMod.h"
#include "src/__support/common.h"

namespace LIBC_NAMESPACE {

LLVM_LIBC_FUNCTION(float128, fmodf128, (float128 x, float128 y)) {
return fputil::generic::FMod<float128>::eval(x, y);
}

} // namespace LIBC_NAMESPACE
19 changes: 19 additions & 0 deletions libc/src/math/generic/fmodl.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
//===-- Single-precision fmodl 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/fmodl.h"
#include "src/__support/FPUtil/generic/FMod.h"
#include "src/__support/common.h"

namespace LIBC_NAMESPACE {

LLVM_LIBC_FUNCTION(long double, fmodl, (long double x, long double y)) {
return fputil::generic::FMod<long double>::eval(x, y);
}

} // namespace LIBC_NAMESPACE
9 changes: 4 additions & 5 deletions libc/test/src/math/exhaustive/fmod_generic_impl_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,11 @@ namespace mpfr = LIBC_NAMESPACE::testing::mpfr;
template <typename T, bool InverseMultiplication>
class LlvmLibcFModTest : public LIBC_NAMESPACE::testing::Test {

using U = typename LIBC_NAMESPACE::fputil::FPBits<T>::StorageType;
using DivisionHelper = LIBC_NAMESPACE::cpp::conditional_t<
InverseMultiplication,
LIBC_NAMESPACE::fputil::generic::FModDivisionInvMultHelper<T>,
LIBC_NAMESPACE::fputil::generic::FModDivisionSimpleHelper<T>>;
LIBC_NAMESPACE::fputil::generic::FModDivisionInvMultHelper<U>,
LIBC_NAMESPACE::fputil::generic::FModDivisionSimpleHelper<U>>;

static constexpr std::array<T, 11> test_bases = {
T(0.0),
Expand All @@ -39,9 +40,7 @@ class LlvmLibcFModTest : public LIBC_NAMESPACE::testing::Test {

public:
void testExtensive() {
using FMod = LIBC_NAMESPACE::fputil::generic::FMod<
T, LIBC_NAMESPACE::fputil::generic::FModFastMathWrapper<T>,
DivisionHelper>;
using FMod = LIBC_NAMESPACE::fputil::generic::FMod<T, U, DivisionHelper>;
using nl = std::numeric_limits<T>;
int min2 = nl::min_exponent - nl::digits - 5;
int max2 = nl::max_exponent + 3;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ template <typename T> class BinaryOpSingleOutputPerf {
"close to each other:\n";
run_perf_in_range(
myFunc, otherFunc, /* startingBit= */ FPBits(T(0x1.0p-10)).uintval(),
/* endingBit= */ FPBits(T(0x1.0p+10)).uintval(), 10'000'001, log);
/* endingBit= */ FPBits(T(0x1.0p+10)).uintval(), 1'001'001, log);
}

static void run_diff(Func myFunc, Func otherFunc, const char *logFile) {
Expand Down
22 changes: 22 additions & 0 deletions libc/test/src/math/performance_testing/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -331,3 +331,25 @@ add_perf_binary(
COMPILE_OPTIONS
-fno-builtin
)

add_perf_binary(
fmodl_perf
SRCS
fmodl_perf.cpp
DEPENDS
.single_input_single_output_diff
libc.src.math.fmodl
COMPILE_OPTIONS
-fno-builtin
)

add_perf_binary(
fmodf128_perf
SRCS
fmodf128_perf.cpp
DEPENDS
.single_input_single_output_diff
libc.src.math.fmodf128
COMPILE_OPTIONS
-fno-builtin
)
16 changes: 16 additions & 0 deletions libc/test/src/math/performance_testing/fmodf128_perf.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
//===-- Differential test for fmodf128 ------------------------------------===//
//
// 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 "BinaryOpSingleOutputDiff.h"

#include "src/math/fmodf128.h"

#include <math.h>

BINARY_OP_SINGLE_OUTPUT_PERF(float, LIBC_NAMESPACE::fmodf128, ::fmodf128,
"fmodf128_perf.log")
16 changes: 16 additions & 0 deletions libc/test/src/math/performance_testing/fmodl_perf.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
//===-- Differential test for fmodl ---------------------------------------===//
//
// 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 "BinaryOpSingleOutputDiff.h"

#include "src/math/fmodl.h"

#include <math.h>

BINARY_OP_SINGLE_OUTPUT_PERF(long double, LIBC_NAMESPACE::fmodl, ::fmodl,
"fmodl_perf.log")
36 changes: 36 additions & 0 deletions libc/test/src/math/smoke/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1793,6 +1793,42 @@ add_fp_unittest(
UNIT_TEST_ONLY
)

add_fp_unittest(
fmodl_test
SUITE
libc-math-smoke-tests
SRCS
fmodl_test.cpp
HDRS
FModTest.h
DEPENDS
libc.include.math
libc.src.errno.errno
libc.src.math.fmodl
libc.src.__support.FPUtil.basic_operations
libc.src.__support.FPUtil.nearest_integer_operations
# FIXME: Currently fails on the GPU build.
UNIT_TEST_ONLY
)

add_fp_unittest(
fmodf128_test
SUITE
libc-math-smoke-tests
SRCS
fmodf128_test.cpp
HDRS
FModTest.h
DEPENDS
libc.include.math
libc.src.errno.errno
libc.src.math.fmodf128
libc.src.__support.FPUtil.basic_operations
libc.src.__support.FPUtil.nearest_integer_operations
# FIXME: Currently fails on the GPU build.
UNIT_TEST_ONLY
)

add_fp_unittest(
coshf_test
SUITE
Expand Down
13 changes: 13 additions & 0 deletions libc/test/src/math/smoke/fmodf128_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
//===-- Unittests for fmodf128 --------------------------------------------===//
//
// 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 "FModTest.h"

#include "src/math/fmodf128.h"

LIST_FMOD_TESTS(float128, LIBC_NAMESPACE::fmodf128)
13 changes: 13 additions & 0 deletions libc/test/src/math/smoke/fmodl_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
//===-- Unittests for fmodl -----------------------------------------------===//
//
// 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 "FModTest.h"

#include "src/math/fmodl.h"

LIST_FMOD_TESTS(long double, LIBC_NAMESPACE::fmodl)