Skip to content

Commit 1f20bc2

Browse files
authored
[libc][math] Add C23 math function fdimf128. (#81074)
1 parent 7ddc320 commit 1f20bc2

File tree

15 files changed

+119
-80
lines changed

15 files changed

+119
-80
lines changed

libc/config/linux/aarch64/entrypoints.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -382,6 +382,7 @@ if(LIBC_COMPILER_HAS_FLOAT128)
382382
libc.src.math.ceilf128
383383
libc.src.math.copysignf128
384384
libc.src.math.fabsf128
385+
libc.src.math.fdimf128
385386
libc.src.math.floorf128
386387
libc.src.math.fmaxf128
387388
libc.src.math.fminf128

libc/config/linux/riscv/entrypoints.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -391,6 +391,7 @@ if(LIBC_COMPILER_HAS_FLOAT128)
391391
libc.src.math.ceilf128
392392
libc.src.math.copysignf128
393393
libc.src.math.fabsf128
394+
libc.src.math.fdimf128
394395
libc.src.math.floorf128
395396
libc.src.math.fmaxf128
396397
libc.src.math.fminf128

libc/config/linux/x86_64/entrypoints.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -410,6 +410,7 @@ if(LIBC_COMPILER_HAS_FLOAT128)
410410
libc.src.math.ceilf128
411411
libc.src.math.copysignf128
412412
libc.src.math.fabsf128
413+
libc.src.math.fdimf128
413414
libc.src.math.floorf128
414415
libc.src.math.fmaxf128
415416
libc.src.math.fminf128

libc/docs/math/index.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,8 @@ Basic Operations
138138
+--------------+---------+---------+---------+---------+---------+---------+---------+---------+---------+---------+---------+---------+
139139
| fdiml | |check| | |check| | |check| | |check| | |check| | | | |check| | |check| | |check| | | |
140140
+--------------+---------+---------+---------+---------+---------+---------+---------+---------+---------+---------+---------+---------+
141+
| fdimf128 | |check| | |check| | | |check| | | | | | | | | |
142+
+--------------+---------+---------+---------+---------+---------+---------+---------+---------+---------+---------+---------+---------+
141143
| floor | |check| | |check| | |check| | |check| | |check| | | | |check| | |check| | |check| | | |
142144
+--------------+---------+---------+---------+---------+---------+---------+---------+---------+---------+---------+---------+---------+
143145
| floorf | |check| | |check| | |check| | |check| | |check| | | | |check| | |check| | |check| | | |

libc/spec/stdc.td

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -374,6 +374,7 @@ def StdC : StandardSpec<"stdc"> {
374374
FunctionSpec<"fdim", RetValSpec<DoubleType>, [ArgSpec<DoubleType>, ArgSpec<DoubleType>]>,
375375
FunctionSpec<"fdimf", RetValSpec<FloatType>, [ArgSpec<FloatType>, ArgSpec<FloatType>]>,
376376
FunctionSpec<"fdiml", RetValSpec<LongDoubleType>, [ArgSpec<LongDoubleType>, ArgSpec<LongDoubleType>]>,
377+
GuardedFunctionSpec<"fdimf128", RetValSpec<Float128Type>, [ArgSpec<Float128Type>, ArgSpec<Float128Type>], "LIBC_COMPILER_HAS_FLOAT128">,
377378

378379
FunctionSpec<"floor", RetValSpec<DoubleType>, [ArgSpec<DoubleType>]>,
379380
FunctionSpec<"floorf", RetValSpec<FloatType>, [ArgSpec<FloatType>]>,

libc/src/math/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@ add_math_entrypoint_object(fabsf128)
111111
add_math_entrypoint_object(fdim)
112112
add_math_entrypoint_object(fdimf)
113113
add_math_entrypoint_object(fdiml)
114+
add_math_entrypoint_object(fdimf128)
114115

115116
add_math_entrypoint_object(floor)
116117
add_math_entrypoint_object(floorf)

libc/src/math/fdimf128.h

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

libc/src/math/generic/CMakeLists.txt

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ add_entrypoint_object(
4343
COMPILE_OPTIONS
4444
-O3
4545
DEPENDS
46+
libc.src.__support.macros.properties.float
4647
libc.src.__support.FPUtil.nearest_integer_operations
4748
)
4849

@@ -215,6 +216,7 @@ add_entrypoint_object(
215216
HDRS
216217
../fabsf128.h
217218
DEPENDS
219+
libc.src.__support.macros.properties.float
218220
libc.src.__support.FPUtil.basic_operations
219221
COMPILE_OPTIONS
220222
-O3
@@ -265,6 +267,7 @@ add_entrypoint_object(
265267
COMPILE_OPTIONS
266268
-O3
267269
DEPENDS
270+
libc.src.__support.macros.properties.float
268271
libc.src.__support.FPUtil.nearest_integer_operations
269272
)
270273

@@ -313,6 +316,7 @@ add_entrypoint_object(
313316
COMPILE_OPTIONS
314317
-O3
315318
DEPENDS
319+
libc.src.__support.macros.properties.float
316320
libc.src.__support.FPUtil.nearest_integer_operations
317321
)
318322

@@ -361,6 +365,7 @@ add_entrypoint_object(
361365
COMPILE_OPTIONS
362366
-O3
363367
DEPENDS
368+
libc.src.__support.macros.properties.float
364369
libc.src.__support.FPUtil.nearest_integer_operations
365370
)
366371

@@ -899,6 +904,7 @@ add_entrypoint_object(
899904
HDRS
900905
../copysignf128.h
901906
DEPENDS
907+
libc.src.__support.macros.properties.float
902908
libc.src.__support.FPUtil.manipulation_functions
903909
COMPILE_OPTIONS
904910
-O3
@@ -1298,6 +1304,7 @@ add_entrypoint_object(
12981304
HDRS
12991305
../fminf128.h
13001306
DEPENDS
1307+
libc.src.__support.macros.properties.float
13011308
libc.src.__support.FPUtil.basic_operations
13021309
COMPILE_OPTIONS
13031310
-O3
@@ -1346,6 +1353,7 @@ add_entrypoint_object(
13461353
HDRS
13471354
../fmaxf128.h
13481355
DEPENDS
1356+
libc.src.__support.macros.properties.float
13491357
libc.src.__support.FPUtil.basic_operations
13501358
COMPILE_OPTIONS
13511359
-O3
@@ -1394,6 +1402,7 @@ add_entrypoint_object(
13941402
HDRS
13951403
../sqrtf128.h
13961404
DEPENDS
1405+
libc.src.__support.macros.properties.float
13971406
libc.src.__support.FPUtil.sqrt
13981407
COMPILE_OPTIONS
13991408
-O3
@@ -1491,10 +1500,10 @@ add_entrypoint_object(
14911500
fdim.cpp
14921501
HDRS
14931502
../fdim.h
1503+
COMPILE_OPTIONS
1504+
-O3
14941505
DEPENDS
14951506
libc.src.__support.FPUtil.basic_operations
1496-
COMPILE_OPTIONS
1497-
-O2
14981507
)
14991508

15001509
add_entrypoint_object(
@@ -1503,10 +1512,10 @@ add_entrypoint_object(
15031512
fdimf.cpp
15041513
HDRS
15051514
../fdimf.h
1515+
COMPILE_OPTIONS
1516+
-O3
15061517
DEPENDS
15071518
libc.src.__support.FPUtil.basic_operations
1508-
COMPILE_OPTIONS
1509-
-O2
15101519
)
15111520

15121521
add_entrypoint_object(
@@ -1515,10 +1524,23 @@ add_entrypoint_object(
15151524
fdiml.cpp
15161525
HDRS
15171526
../fdiml.h
1527+
COMPILE_OPTIONS
1528+
-O3
15181529
DEPENDS
15191530
libc.src.__support.FPUtil.basic_operations
1531+
)
1532+
1533+
add_entrypoint_object(
1534+
fdimf128
1535+
SRCS
1536+
fdimf128.cpp
1537+
HDRS
1538+
../fdimf128.h
15201539
COMPILE_OPTIONS
1521-
-O2
1540+
-O3
1541+
DEPENDS
1542+
libc.src.__support.macros.properties.float
1543+
libc.src.__support.FPUtil.basic_operations
15221544
)
15231545

15241546
add_entrypoint_object(

libc/src/math/generic/fdimf128.cpp

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

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

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1007,7 +1007,6 @@ add_fp_unittest(
10071007
HDRS
10081008
FDimTest.h
10091009
DEPENDS
1010-
libc.include.math
10111010
libc.src.math.fdimf
10121011
libc.src.__support.FPUtil.basic_operations
10131012
libc.src.__support.FPUtil.fp_bits
@@ -1022,7 +1021,6 @@ add_fp_unittest(
10221021
HDRS
10231022
FDimTest.h
10241023
DEPENDS
1025-
libc.include.math
10261024
libc.src.math.fdim
10271025
libc.src.__support.FPUtil.basic_operations
10281026
libc.src.__support.FPUtil.fp_bits
@@ -1037,12 +1035,25 @@ add_fp_unittest(
10371035
HDRS
10381036
FDimTest.h
10391037
DEPENDS
1040-
libc.include.math
10411038
libc.src.math.fdiml
10421039
libc.src.__support.FPUtil.basic_operations
10431040
libc.src.__support.FPUtil.fp_bits
10441041
)
10451042

1043+
add_fp_unittest(
1044+
fdimf128_test
1045+
SUITE
1046+
libc-math-smoke-tests
1047+
SRCS
1048+
fdimf128_test.cpp
1049+
HDRS
1050+
FDimTest.h
1051+
DEPENDS
1052+
libc.src.math.fdimf128
1053+
libc.src.__support.FPUtil.basic_operations
1054+
libc.src.__support.FPUtil.fp_bits
1055+
)
1056+
10461057
# FIXME: These tests are currently broken on the GPU.
10471058
if(NOT LIBC_TARGET_ARCHITECTURE_IS_GPU)
10481059
add_fp_unittest(

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

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
#include "src/__support/FPUtil/FPBits.h"
1111
#include "test/UnitTest/FPMatcher.h"
1212
#include "test/UnitTest/Test.h"
13-
#include <math.h>
1413

1514
template <typename T>
1615
class FDimTestTemplate : public LIBC_NAMESPACE::testing::Test {
@@ -26,7 +25,7 @@ class FDimTestTemplate : public LIBC_NAMESPACE::testing::Test {
2625
const T neg_zero = FPBits::zero(Sign::NEG).get_val();
2726
const T nan = FPBits::quiet_nan().get_val();
2827

29-
void test_na_n_arg(FuncPtr func) {
28+
void test_nan_arg(FuncPtr func) {
3029
EXPECT_FP_EQ(nan, func(nan, inf));
3130
EXPECT_FP_EQ(nan, func(neg_inf, nan));
3231
EXPECT_FP_EQ(nan, func(nan, zero));
@@ -66,12 +65,15 @@ class FDimTestTemplate : public LIBC_NAMESPACE::testing::Test {
6665
constexpr StorageType STEP = STORAGE_MAX / COUNT;
6766
for (StorageType i = 0, v = 0, w = STORAGE_MAX; i <= COUNT;
6867
++i, v += STEP, w -= STEP) {
69-
T x = FPBits(v).get_val(), y = FPBits(w).get_val();
70-
if (isnan(x) || isinf(x))
68+
FPBits xbits(v), ybits(w);
69+
if (xbits.is_inf_or_nan())
7170
continue;
72-
if (isnan(y) || isinf(y))
71+
if (ybits.is_inf_or_nan())
7372
continue;
7473

74+
T x = xbits.get_val();
75+
T y = ybits.get_val();
76+
7577
if (x > y) {
7678
EXPECT_FP_EQ(x - y, func(x, y));
7779
} else {
@@ -80,3 +82,12 @@ class FDimTestTemplate : public LIBC_NAMESPACE::testing::Test {
8082
}
8183
}
8284
};
85+
86+
#define LIST_FDIM_TESTS(T, func) \
87+
using LlvmLibcFDimTest = FDimTestTemplate<T>; \
88+
TEST_F(LlvmLibcFDimTest, NaNArg) { test_nan_arg(&func); } \
89+
TEST_F(LlvmLibcFDimTest, InfArg) { test_inf_arg(&func); } \
90+
TEST_F(LlvmLibcFDimTest, NegInfArg) { test_neg_inf_arg(&func); } \
91+
TEST_F(LlvmLibcFDimTest, BothZero) { test_both_zero(&func); } \
92+
TEST_F(LlvmLibcFDimTest, InFloatRange) { test_in_range(&func); } \
93+
static_assert(true, "Require semicolon.")

libc/test/src/math/smoke/fdim_test.cpp

Lines changed: 1 addition & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -8,26 +8,6 @@
88

99
#include "FDimTest.h"
1010

11-
#include "src/__support/FPUtil/FPBits.h"
1211
#include "src/math/fdim.h"
13-
#include "test/UnitTest/FPMatcher.h"
14-
#include "test/UnitTest/Test.h"
15-
#include <math.h>
1612

17-
using LlvmLibcFDimTest = FDimTestTemplate<double>;
18-
19-
TEST_F(LlvmLibcFDimTest, NaNArg_fdim) { test_na_n_arg(&LIBC_NAMESPACE::fdim); }
20-
21-
TEST_F(LlvmLibcFDimTest, InfArg_fdim) { test_inf_arg(&LIBC_NAMESPACE::fdim); }
22-
23-
TEST_F(LlvmLibcFDimTest, NegInfArg_fdim) {
24-
test_neg_inf_arg(&LIBC_NAMESPACE::fdim);
25-
}
26-
27-
TEST_F(LlvmLibcFDimTest, BothZero_fdim) {
28-
test_both_zero(&LIBC_NAMESPACE::fdim);
29-
}
30-
31-
TEST_F(LlvmLibcFDimTest, InDoubleRange_fdim) {
32-
test_in_range(&LIBC_NAMESPACE::fdim);
33-
}
13+
LIST_FDIM_TESTS(double, LIBC_NAMESPACE::fdim);
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
//===-- Unittests for fdimf128 --------------------------------------------===//
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 "FDimTest.h"
10+
11+
#include "src/math/fdimf128.h"
12+
13+
LIST_FDIM_TESTS(float128, LIBC_NAMESPACE::fdimf128);

libc/test/src/math/smoke/fdimf_test.cpp

Lines changed: 1 addition & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -8,28 +8,6 @@
88

99
#include "FDimTest.h"
1010

11-
#include "src/__support/FPUtil/FPBits.h"
1211
#include "src/math/fdimf.h"
13-
#include "test/UnitTest/FPMatcher.h"
14-
#include "test/UnitTest/Test.h"
15-
#include <math.h>
1612

17-
using LlvmLibcFDimTest = FDimTestTemplate<float>;
18-
19-
TEST_F(LlvmLibcFDimTest, NaNArg_fdimf) {
20-
test_na_n_arg(&LIBC_NAMESPACE::fdimf);
21-
}
22-
23-
TEST_F(LlvmLibcFDimTest, InfArg_fdimf) { test_inf_arg(&LIBC_NAMESPACE::fdimf); }
24-
25-
TEST_F(LlvmLibcFDimTest, NegInfArg_fdimf) {
26-
test_neg_inf_arg(&LIBC_NAMESPACE::fdimf);
27-
}
28-
29-
TEST_F(LlvmLibcFDimTest, BothZero_fdimf) {
30-
test_both_zero(&LIBC_NAMESPACE::fdimf);
31-
}
32-
33-
TEST_F(LlvmLibcFDimTest, InFloatRange_fdimf) {
34-
test_in_range(&LIBC_NAMESPACE::fdimf);
35-
}
13+
LIST_FDIM_TESTS(float, LIBC_NAMESPACE::fdimf);

0 commit comments

Comments
 (0)