Skip to content

Commit 3547423

Browse files
committed
[libc][math][c23] Add fminimum_numf16 C23 math function
1 parent 64653ca commit 3547423

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
@@ -512,6 +512,7 @@ if(LIBC_TYPES_HAS_FLOAT16)
512512
libc.src.math.fminimumf16
513513
libc.src.math.fminimum_magf16
514514
libc.src.math.fminimum_mag_numf16
515+
libc.src.math.fminimum_numf16
515516
libc.src.math.fromfpf16
516517
libc.src.math.fromfpxf16
517518
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
@@ -545,6 +545,7 @@ if(LIBC_TYPES_HAS_FLOAT16)
545545
libc.src.math.fminimumf16
546546
libc.src.math.fminimum_magf16
547547
libc.src.math.fminimum_mag_numf16
548+
libc.src.math.fminimum_numf16
548549
libc.src.math.fromfpf16
549550
libc.src.math.fromfpxf16
550551
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
@@ -455,6 +455,7 @@ def StdC : StandardSpec<"stdc"> {
455455
FunctionSpec<"fminimum_num", RetValSpec<DoubleType>, [ArgSpec<DoubleType>, ArgSpec<DoubleType>]>,
456456
FunctionSpec<"fminimum_numf", RetValSpec<FloatType>, [ArgSpec<FloatType>, ArgSpec<FloatType>]>,
457457
FunctionSpec<"fmaximum_numl", RetValSpec<LongDoubleType>, [ArgSpec<LongDoubleType>, ArgSpec<LongDoubleType>]>,
458+
GuardedFunctionSpec<"fminimum_numf16", RetValSpec<Float16Type>, [ArgSpec<Float16Type>, ArgSpec<Float16Type>], "LIBC_TYPES_HAS_FLOAT16">,
458459
GuardedFunctionSpec<"fminimum_numf128", RetValSpec<Float128Type>, [ArgSpec<Float128Type>, ArgSpec<Float128Type>], "LIBC_TYPES_HAS_FLOAT128">,
459460

460461
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
@@ -163,6 +163,7 @@ add_math_entrypoint_object(fminimumf128)
163163
add_math_entrypoint_object(fminimum_num)
164164
add_math_entrypoint_object(fminimum_numf)
165165
add_math_entrypoint_object(fminimum_numl)
166+
add_math_entrypoint_object(fminimum_numf16)
166167
add_math_entrypoint_object(fminimum_numf128)
167168

168169
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
@@ -2177,6 +2177,19 @@ add_entrypoint_object(
21772177
-O2
21782178
)
21792179

2180+
add_entrypoint_object(
2181+
fminimum_numf16
2182+
SRCS
2183+
fminimum_numf16.cpp
2184+
HDRS
2185+
../fminimum_numf16.h
2186+
DEPENDS
2187+
libc.src.__support.macros.properties.types
2188+
libc.src.__support.FPUtil.basic_operations
2189+
COMPILE_OPTIONS
2190+
-O3
2191+
)
2192+
21802193
add_entrypoint_object(
21812194
fminimum_numf128
21822195
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
@@ -2164,6 +2164,7 @@ add_fp_unittest(
21642164
FMinimumNumTest.h
21652165
DEPENDS
21662166
libc.src.math.fminimum_numf
2167+
libc.src.__support.CPP.algorithm
21672168
libc.src.__support.FPUtil.fp_bits
21682169
)
21692170

@@ -2177,6 +2178,7 @@ add_fp_unittest(
21772178
FMinimumNumTest.h
21782179
DEPENDS
21792180
libc.src.math.fminimum_num
2181+
libc.src.__support.CPP.algorithm
21802182
libc.src.__support.FPUtil.fp_bits
21812183
)
21822184

@@ -2190,6 +2192,21 @@ add_fp_unittest(
21902192
FMinimumNumTest.h
21912193
DEPENDS
21922194
libc.src.math.fminimum_numl
2195+
libc.src.__support.CPP.algorithm
2196+
libc.src.__support.FPUtil.fp_bits
2197+
)
2198+
2199+
add_fp_unittest(
2200+
fminimum_numf16_test
2201+
SUITE
2202+
libc-math-smoke-tests
2203+
SRCS
2204+
fminimum_numf16_test.cpp
2205+
HDRS
2206+
FMinimumNumTest.h
2207+
DEPENDS
2208+
libc.src.math.fminimum_numf16
2209+
libc.src.__support.CPP.algorithm
21932210
libc.src.__support.FPUtil.fp_bits
21942211
)
21952212

@@ -2203,6 +2220,7 @@ add_fp_unittest(
22032220
FMinimumNumTest.h
22042221
DEPENDS
22052222
libc.src.math.fminimum_numf128
2223+
libc.src.__support.CPP.algorithm
22062224
libc.src.__support.FPUtil.fp_bits
22072225
)
22082226

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)