Skip to content

Commit 24c72c3

Browse files
committed
[libc][math][c23] Add fminimum_numf16 C23 math function
1 parent 4bcbdac commit 24c72c3

File tree

12 files changed

+97
-8
lines changed

12 files changed

+97
-8
lines changed

libc/config/linux/aarch64/entrypoints.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -514,6 +514,7 @@ if(LIBC_TYPES_HAS_FLOAT16)
514514
libc.src.math.fminimumf16
515515
libc.src.math.fminimum_magf16
516516
libc.src.math.fminimum_mag_numf16
517+
libc.src.math.fminimum_numf16
517518
libc.src.math.fromfpf16
518519
libc.src.math.fromfpxf16
519520
libc.src.math.llrintf16

libc/config/linux/x86_64/entrypoints.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -547,6 +547,7 @@ if(LIBC_TYPES_HAS_FLOAT16)
547547
libc.src.math.fminimumf16
548548
libc.src.math.fminimum_magf16
549549
libc.src.math.fminimum_mag_numf16
550+
libc.src.math.fminimum_numf16
550551
libc.src.math.fromfpf16
551552
libc.src.math.fromfpxf16
552553
libc.src.math.llrintf16

libc/docs/c23.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ Additions:
6767
* fmaximum_mag* |check|
6868
* fminimum_mag* |check|
6969
* fmaximum_num* |check|
70+
* fminimum_num* |check|
7071
* fmaximum_mag_num* |check|
7172
* fminimum_mag_num* |check|
7273
* fadd*

libc/docs/math/index.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ Basic Operations
154154
+------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
155155
| fminimum_mag_num | |check| | |check| | |check| | |check| | |check| | 7.12.12.11 | F.10.9.5 |
156156
+------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
157-
| fminimum_num | |check| | |check| | |check| | | |check| | 7.12.12.9 | F.10.9.5 |
157+
| fminimum_num | |check| | |check| | |check| | |check| | |check| | 7.12.12.9 | F.10.9.5 |
158158
+------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
159159
| fmod | |check| | |check| | |check| | | |check| | 7.12.10.1 | F.10.7.1 |
160160
+------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+

libc/spec/stdc.td

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -457,6 +457,7 @@ def StdC : StandardSpec<"stdc"> {
457457
FunctionSpec<"fminimum_num", RetValSpec<DoubleType>, [ArgSpec<DoubleType>, ArgSpec<DoubleType>]>,
458458
FunctionSpec<"fminimum_numf", RetValSpec<FloatType>, [ArgSpec<FloatType>, ArgSpec<FloatType>]>,
459459
FunctionSpec<"fmaximum_numl", RetValSpec<LongDoubleType>, [ArgSpec<LongDoubleType>, ArgSpec<LongDoubleType>]>,
460+
GuardedFunctionSpec<"fminimum_numf16", RetValSpec<Float16Type>, [ArgSpec<Float16Type>, ArgSpec<Float16Type>], "LIBC_TYPES_HAS_FLOAT16">,
460461
GuardedFunctionSpec<"fminimum_numf128", RetValSpec<Float128Type>, [ArgSpec<Float128Type>, ArgSpec<Float128Type>], "LIBC_TYPES_HAS_FLOAT128">,
461462

462463
FunctionSpec<"fminimum_mag", RetValSpec<DoubleType>, [ArgSpec<DoubleType>, ArgSpec<DoubleType>]>,

libc/src/math/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,7 @@ add_math_entrypoint_object(fminimumf128)
165165
add_math_entrypoint_object(fminimum_num)
166166
add_math_entrypoint_object(fminimum_numf)
167167
add_math_entrypoint_object(fminimum_numl)
168+
add_math_entrypoint_object(fminimum_numf16)
168169
add_math_entrypoint_object(fminimum_numf128)
169170

170171
add_math_entrypoint_object(fminimum_mag)

libc/src/math/fminimum_numf16.h

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

libc/src/math/generic/CMakeLists.txt

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2204,6 +2204,19 @@ add_entrypoint_object(
22042204
-O2
22052205
)
22062206

2207+
add_entrypoint_object(
2208+
fminimum_numf16
2209+
SRCS
2210+
fminimum_numf16.cpp
2211+
HDRS
2212+
../fminimum_numf16.h
2213+
DEPENDS
2214+
libc.src.__support.macros.properties.types
2215+
libc.src.__support.FPUtil.basic_operations
2216+
COMPILE_OPTIONS
2217+
-O3
2218+
)
2219+
22072220
add_entrypoint_object(
22082221
fminimum_numf128
22092222
SRCS
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
//===-- Implementation of fminimum_numf16 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/fminimum_numf16.h"
10+
#include "src/__support/FPUtil/BasicOperations.h"
11+
#include "src/__support/common.h"
12+
13+
namespace LIBC_NAMESPACE {
14+
15+
LLVM_LIBC_FUNCTION(float16, fminimum_numf16, (float16 x, float16 y)) {
16+
return fputil::fminimum_num(x, y);
17+
}
18+
19+
} // namespace LIBC_NAMESPACE

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2190,6 +2190,7 @@ add_fp_unittest(
21902190
FMinimumNumTest.h
21912191
DEPENDS
21922192
libc.src.math.fminimum_numf
2193+
libc.src.__support.CPP.algorithm
21932194
libc.src.__support.FPUtil.fp_bits
21942195
)
21952196

@@ -2203,6 +2204,7 @@ add_fp_unittest(
22032204
FMinimumNumTest.h
22042205
DEPENDS
22052206
libc.src.math.fminimum_num
2207+
libc.src.__support.CPP.algorithm
22062208
libc.src.__support.FPUtil.fp_bits
22072209
)
22082210

@@ -2216,6 +2218,21 @@ add_fp_unittest(
22162218
FMinimumNumTest.h
22172219
DEPENDS
22182220
libc.src.math.fminimum_numl
2221+
libc.src.__support.CPP.algorithm
2222+
libc.src.__support.FPUtil.fp_bits
2223+
)
2224+
2225+
add_fp_unittest(
2226+
fminimum_numf16_test
2227+
SUITE
2228+
libc-math-smoke-tests
2229+
SRCS
2230+
fminimum_numf16_test.cpp
2231+
HDRS
2232+
FMinimumNumTest.h
2233+
DEPENDS
2234+
libc.src.math.fminimum_numf16
2235+
libc.src.__support.CPP.algorithm
22192236
libc.src.__support.FPUtil.fp_bits
22202237
)
22212238

@@ -2229,6 +2246,7 @@ add_fp_unittest(
22292246
FMinimumNumTest.h
22302247
DEPENDS
22312248
libc.src.math.fminimum_numf128
2249+
libc.src.__support.CPP.algorithm
22322250
libc.src.__support.FPUtil.fp_bits
22332251
)
22342252

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

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#ifndef LLVM_LIBC_TEST_SRC_MATH_SMOKE_FMINIMUMNUMTEST_H
1010
#define LLVM_LIBC_TEST_SRC_MATH_SMOKE_FMINIMUMNUMTEST_H
1111

12+
#include "src/__support/CPP/algorithm.h"
1213
#include "src/__support/FPUtil/FPBits.h"
1314
#include "test/UnitTest/FEnvSafeTest.h"
1415
#include "test/UnitTest/FPMatcher.h"
@@ -67,10 +68,11 @@ class FMinimumNumTest : public LIBC_NAMESPACE::testing::FEnvSafeTest {
6768
}
6869

6970
void testRange(FMinimumNumFunc func) {
70-
constexpr StorageType COUNT = 100'001;
71-
constexpr StorageType STEP = STORAGE_MAX / COUNT;
72-
for (StorageType i = 0, v = 0, w = STORAGE_MAX; i <= COUNT;
73-
++i, v += STEP, w -= STEP) {
71+
constexpr int COUNT = 100'001;
72+
constexpr StorageType STEP = LIBC_NAMESPACE::cpp::max(
73+
static_cast<StorageType>(STORAGE_MAX / COUNT), StorageType(1));
74+
StorageType v = 0, w = STORAGE_MAX;
75+
for (int i = 0; i <= COUNT; ++i, v += STEP, w -= STEP) {
7476
FPBits xbits(v), ybits(w);
7577
if (xbits.is_inf_or_nan())
7678
continue;
@@ -81,11 +83,10 @@ class FMinimumNumTest : public LIBC_NAMESPACE::testing::FEnvSafeTest {
8183
if ((x == 0) && (y == 0))
8284
continue;
8385

84-
if (x > y) {
86+
if (x > y)
8587
EXPECT_FP_EQ(y, func(x, y));
86-
} else {
88+
else
8789
EXPECT_FP_EQ(x, func(x, y));
88-
}
8990
}
9091
}
9192
};
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
//===-- Unittests for fminimum_numf16 -------------------------------------===//
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 "FMinimumNumTest.h"
10+
11+
#include "src/math/fminimum_numf16.h"
12+
13+
LIST_FMINIMUM_NUM_TESTS(float16, LIBC_NAMESPACE::fminimum_numf16)

0 commit comments

Comments
 (0)