Skip to content

Commit 0b0cce8

Browse files
felixh5678Felix
andauthored
[libc] Add fminf128 and fmaxf128 implementations for Linux x86_64. (#79307)
Co-authored-by: Felix <[email protected]>
1 parent 594b92a commit 0b0cce8

File tree

14 files changed

+174
-16
lines changed

14 files changed

+174
-16
lines changed

libc/config/linux/x86_64/entrypoints.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -376,6 +376,8 @@ if(LIBC_COMPILER_HAS_FLOAT128)
376376
libc.src.math.copysignf128
377377
libc.src.math.fabsf128
378378
libc.src.math.sqrtf128
379+
libc.src.math.fmaxf128
380+
libc.src.math.fminf128
379381
)
380382
endif()
381383

libc/docs/math/index.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,12 +146,16 @@ Basic Operations
146146
+--------------+---------+---------+---------+---------+---------+---------+---------+---------+---------+---------+---------+---------+
147147
| fmaxf | |check| | |check| | |check| | |check| | |check| | | | |check| | |check| | |check| | | |
148148
+--------------+---------+---------+---------+---------+---------+---------+---------+---------+---------+---------+---------+---------+
149+
| fmaxf128 | |check| | | | | | | | | | | | |
150+
+--------------+---------+---------+---------+---------+---------+---------+---------+---------+---------+---------+---------+---------+
149151
| fmaxl | |check| | |check| | |check| | |check| | |check| | | | |check| | |check| | |check| | | |
150152
+--------------+---------+---------+---------+---------+---------+---------+---------+---------+---------+---------+---------+---------+
151153
| fmin | |check| | |check| | |check| | |check| | |check| | | | |check| | |check| | |check| | | |
152154
+--------------+---------+---------+---------+---------+---------+---------+---------+---------+---------+---------+---------+---------+
153155
| fminf | |check| | |check| | |check| | |check| | |check| | | | |check| | |check| | |check| | | |
154156
+--------------+---------+---------+---------+---------+---------+---------+---------+---------+---------+---------+---------+---------+
157+
| fminf128 | |check| | | | | | | | | | | | |
158+
+--------------+---------+---------+---------+---------+---------+---------+---------+---------+---------+---------+---------+---------+
155159
| fminl | |check| | |check| | |check| | |check| | |check| | | | |check| | |check| | |check| | | |
156160
+--------------+---------+---------+---------+---------+---------+---------+---------+---------+---------+---------+---------+---------+
157161
| fmod | |check| | |check| | |check| | |check| | |check| | | | |check| | |check| | |check| | | |

libc/spec/stdc.td

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -380,10 +380,12 @@ def StdC : StandardSpec<"stdc"> {
380380
FunctionSpec<"fmin", RetValSpec<DoubleType>, [ArgSpec<DoubleType>, ArgSpec<DoubleType>]>,
381381
FunctionSpec<"fminf", RetValSpec<FloatType>, [ArgSpec<FloatType>, ArgSpec<FloatType>]>,
382382
FunctionSpec<"fminl", RetValSpec<LongDoubleType>, [ArgSpec<LongDoubleType>, ArgSpec<LongDoubleType>]>,
383+
FunctionSpec<"fminf128", RetValSpec<Float128Type>, [ArgSpec<Float128Type>, ArgSpec<Float128Type>]>,
383384

384385
FunctionSpec<"fmax", RetValSpec<DoubleType>, [ArgSpec<DoubleType>, ArgSpec<DoubleType>]>,
385386
FunctionSpec<"fmaxf", RetValSpec<FloatType>, [ArgSpec<FloatType>, ArgSpec<FloatType>]>,
386387
FunctionSpec<"fmaxl", RetValSpec<LongDoubleType>, [ArgSpec<LongDoubleType>, ArgSpec<LongDoubleType>]>,
388+
FunctionSpec<"fmaxf128", RetValSpec<Float128Type>, [ArgSpec<Float128Type>, ArgSpec<Float128Type>]>,
387389

388390
FunctionSpec<"fma", RetValSpec<DoubleType>, [ArgSpec<DoubleType>, ArgSpec<DoubleType>, ArgSpec<DoubleType>]>,
389391
FunctionSpec<"fmaf", RetValSpec<FloatType>, [ArgSpec<FloatType>, ArgSpec<FloatType>, ArgSpec<FloatType>]>,

libc/src/math/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,10 +121,12 @@ add_math_entrypoint_object(fmaf)
121121
add_math_entrypoint_object(fmax)
122122
add_math_entrypoint_object(fmaxf)
123123
add_math_entrypoint_object(fmaxl)
124+
add_math_entrypoint_object(fmaxf128)
124125

125126
add_math_entrypoint_object(fmin)
126127
add_math_entrypoint_object(fminf)
127128
add_math_entrypoint_object(fminl)
129+
add_math_entrypoint_object(fminf128)
128130

129131
add_math_entrypoint_object(fmod)
130132
add_math_entrypoint_object(fmodf)

libc/src/math/fmaxf128.h

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

libc/src/math/fminf128.h

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

libc/src/math/generic/CMakeLists.txt

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1243,6 +1243,18 @@ add_entrypoint_object(
12431243
-O2
12441244
)
12451245

1246+
add_entrypoint_object(
1247+
fminf128
1248+
SRCS
1249+
fminf128.cpp
1250+
HDRS
1251+
../fminf128.h
1252+
DEPENDS
1253+
libc.src.__support.FPUtil.basic_operations
1254+
COMPILE_OPTIONS
1255+
-O3
1256+
)
1257+
12461258
add_entrypoint_object(
12471259
fmax
12481260
SRCS
@@ -1279,6 +1291,18 @@ add_entrypoint_object(
12791291
-O2
12801292
)
12811293

1294+
add_entrypoint_object(
1295+
fmaxf128
1296+
SRCS
1297+
fmaxf128.cpp
1298+
HDRS
1299+
../fmaxf128.h
1300+
DEPENDS
1301+
libc.src.__support.FPUtil.basic_operations
1302+
COMPILE_OPTIONS
1303+
-O3
1304+
)
1305+
12821306
add_entrypoint_object(
12831307
sqrt
12841308
SRCS

libc/src/math/generic/fmaxf128.cpp

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

libc/src/math/generic/fminf128.cpp

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

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

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -990,7 +990,6 @@ if(NOT LIBC_TARGET_ARCHITECTURE_IS_GPU)
990990
HDRS
991991
FMinTest.h
992992
DEPENDS
993-
libc.include.math
994993
libc.src.math.fminf
995994
libc.src.__support.FPUtil.fp_bits
996995
)
@@ -1004,7 +1003,6 @@ if(NOT LIBC_TARGET_ARCHITECTURE_IS_GPU)
10041003
HDRS
10051004
FMinTest.h
10061005
DEPENDS
1007-
libc.include.math
10081006
libc.src.math.fmin
10091007
libc.src.__support.FPUtil.fp_bits
10101008
)
@@ -1018,11 +1016,23 @@ if(NOT LIBC_TARGET_ARCHITECTURE_IS_GPU)
10181016
HDRS
10191017
FMinTest.h
10201018
DEPENDS
1021-
libc.include.math
10221019
libc.src.math.fminl
10231020
libc.src.__support.FPUtil.fp_bits
10241021
)
10251022

1023+
add_fp_unittest(
1024+
fminf128_test
1025+
SUITE
1026+
libc-math-smoke-tests
1027+
SRCS
1028+
fminf128_test.cpp
1029+
HDRS
1030+
FMinTest.h
1031+
DEPENDS
1032+
libc.src.math.fminf128
1033+
libc.src.__support.FPUtil.fp_bits
1034+
)
1035+
10261036
add_fp_unittest(
10271037
fmaxf_test
10281038
SUITE
@@ -1032,7 +1042,6 @@ if(NOT LIBC_TARGET_ARCHITECTURE_IS_GPU)
10321042
HDRS
10331043
FMaxTest.h
10341044
DEPENDS
1035-
libc.include.math
10361045
libc.src.math.fmaxf
10371046
libc.src.__support.FPUtil.fp_bits
10381047
)
@@ -1046,7 +1055,6 @@ if(NOT LIBC_TARGET_ARCHITECTURE_IS_GPU)
10461055
HDRS
10471056
FMaxTest.h
10481057
DEPENDS
1049-
libc.include.math
10501058
libc.src.math.fmax
10511059
libc.src.__support.FPUtil.fp_bits
10521060
)
@@ -1060,10 +1068,22 @@ if(NOT LIBC_TARGET_ARCHITECTURE_IS_GPU)
10601068
HDRS
10611069
FMaxTest.h
10621070
DEPENDS
1063-
libc.include.math
10641071
libc.src.math.fmaxl
10651072
libc.src.__support.FPUtil.fp_bits
10661073
)
1074+
1075+
add_fp_unittest(
1076+
fmaxf128_test
1077+
SUITE
1078+
libc-math-smoke-tests
1079+
SRCS
1080+
fmaxf128_test.cpp
1081+
HDRS
1082+
FMaxTest.h
1083+
DEPENDS
1084+
libc.src.math.fmaxf128
1085+
libc.src.__support.FPUtil.fp_bits
1086+
)
10671087
endif()
10681088

10691089
add_fp_unittest(

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@
99
#include "test/UnitTest/FPMatcher.h"
1010
#include "test/UnitTest/Test.h"
1111

12-
#include <math.h>
13-
1412
template <typename T> class FMaxTest : public LIBC_NAMESPACE::testing::Test {
1513

1614
DECLARE_SPECIAL_CONSTANTS(T)
@@ -56,11 +54,13 @@ template <typename T> class FMaxTest : public LIBC_NAMESPACE::testing::Test {
5654
constexpr StorageType STEP = STORAGE_MAX / COUNT;
5755
for (StorageType i = 0, v = 0, w = STORAGE_MAX; i <= COUNT;
5856
++i, v += STEP, w -= STEP) {
59-
T x = FPBits(v).get_val(), y = FPBits(w).get_val();
60-
if (isnan(x) || isinf(x))
57+
FPBits xbits(v), ybits(w);
58+
if (xbits.is_inf_or_nan())
6159
continue;
62-
if (isnan(y) || isinf(y))
60+
if (ybits.is_inf_or_nan())
6361
continue;
62+
T x = xbits.get_val();
63+
T y = ybits.get_val();
6464
if ((x == 0) && (y == 0))
6565
continue;
6666

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@
99
#include "test/UnitTest/FPMatcher.h"
1010
#include "test/UnitTest/Test.h"
1111

12-
#include <math.h>
13-
1412
template <typename T> class FMinTest : public LIBC_NAMESPACE::testing::Test {
1513

1614
DECLARE_SPECIAL_CONSTANTS(T)
@@ -56,11 +54,13 @@ template <typename T> class FMinTest : public LIBC_NAMESPACE::testing::Test {
5654
constexpr StorageType STEP = STORAGE_MAX / COUNT;
5755
for (StorageType i = 0, v = 0, w = STORAGE_MAX; i <= COUNT;
5856
++i, v += STEP, w -= STEP) {
59-
T x = FPBits(v).get_val(), y = FPBits(w).get_val();
60-
if (isnan(x) || isinf(x))
57+
FPBits xbits(v), ybits(w);
58+
if (xbits.is_inf_or_nan())
6159
continue;
62-
if (isnan(y) || isinf(y))
60+
if (ybits.is_inf_or_nan())
6361
continue;
62+
T x = xbits.get_val();
63+
T y = ybits.get_val();
6464
if ((x == 0) && (y == 0))
6565
continue;
6666

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
//===-- Unittests for fmaxf128 --------------------------------------------===//
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 "FMaxTest.h"
10+
11+
#include "src/math/fmaxf128.h"
12+
13+
LIST_FMAX_TESTS(float128, LIBC_NAMESPACE::fmaxf128)
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
//===-- Unittests for fminf128 --------------------------------------------===//
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 "FMinTest.h"
10+
11+
#include "src/math/fminf128.h"
12+
13+
LIST_FMIN_TESTS(float128, LIBC_NAMESPACE::fminf128)

0 commit comments

Comments
 (0)