Skip to content

Commit c537f35

Browse files
authored
[libc][math][c23] Add fdimf16 C23 math function (#94354)
#93566
1 parent 07b8990 commit c537f35

File tree

11 files changed

+94
-10
lines changed

11 files changed

+94
-10
lines changed

libc/config/linux/aarch64/entrypoints.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -503,6 +503,7 @@ if(LIBC_TYPES_HAS_FLOAT16)
503503
libc.src.math.ceilf16
504504
libc.src.math.copysignf16
505505
libc.src.math.fabsf16
506+
libc.src.math.fdimf16
506507
libc.src.math.floorf16
507508
libc.src.math.fromfpf16
508509
libc.src.math.fromfpxf16

libc/config/linux/x86_64/entrypoints.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -536,6 +536,7 @@ if(LIBC_TYPES_HAS_FLOAT16)
536536
libc.src.math.ceilf16
537537
libc.src.math.copysignf16
538538
libc.src.math.fabsf16
539+
libc.src.math.fdimf16
539540
libc.src.math.floorf16
540541
libc.src.math.fromfpf16
541542
libc.src.math.fromfpxf16

libc/docs/math/index.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ Basic Operations
128128
+------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
129129
| fadd | N/A | | | N/A | | 7.12.14.1 | F.10.11 |
130130
+------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
131-
| fdim | |check| | |check| | |check| | | |check| | 7.12.12.1 | F.10.9.1 |
131+
| fdim | |check| | |check| | |check| | |check| | |check| | 7.12.12.1 | F.10.9.1 |
132132
+------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
133133
| fdiv | N/A | | | N/A | | 7.12.14.4 | F.10.11 |
134134
+------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+

libc/spec/stdc.td

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -403,6 +403,7 @@ def StdC : StandardSpec<"stdc"> {
403403
FunctionSpec<"fdim", RetValSpec<DoubleType>, [ArgSpec<DoubleType>, ArgSpec<DoubleType>]>,
404404
FunctionSpec<"fdimf", RetValSpec<FloatType>, [ArgSpec<FloatType>, ArgSpec<FloatType>]>,
405405
FunctionSpec<"fdiml", RetValSpec<LongDoubleType>, [ArgSpec<LongDoubleType>, ArgSpec<LongDoubleType>]>,
406+
GuardedFunctionSpec<"fdimf16", RetValSpec<Float16Type>, [ArgSpec<Float16Type>, ArgSpec<Float16Type>], "LIBC_TYPES_HAS_FLOAT16">,
406407
GuardedFunctionSpec<"fdimf128", RetValSpec<Float128Type>, [ArgSpec<Float128Type>, ArgSpec<Float128Type>], "LIBC_TYPES_HAS_FLOAT128">,
407408

408409
FunctionSpec<"floor", RetValSpec<DoubleType>, [ArgSpec<DoubleType>]>,

libc/src/math/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@ add_math_entrypoint_object(fabsf128)
108108
add_math_entrypoint_object(fdim)
109109
add_math_entrypoint_object(fdimf)
110110
add_math_entrypoint_object(fdiml)
111+
add_math_entrypoint_object(fdimf16)
111112
add_math_entrypoint_object(fdimf128)
112113

113114
add_math_entrypoint_object(floor)

libc/src/math/fdimf16.h

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

libc/src/math/generic/CMakeLists.txt

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2397,6 +2397,19 @@ add_entrypoint_object(
23972397
libc.src.__support.FPUtil.basic_operations
23982398
)
23992399

2400+
add_entrypoint_object(
2401+
fdimf16
2402+
SRCS
2403+
fdimf16.cpp
2404+
HDRS
2405+
../fdimf16.h
2406+
COMPILE_OPTIONS
2407+
-O3
2408+
DEPENDS
2409+
libc.src.__support.macros.properties.types
2410+
libc.src.__support.FPUtil.basic_operations
2411+
)
2412+
24002413
add_entrypoint_object(
24012414
fdimf128
24022415
SRCS

libc/src/math/generic/fdimf16.cpp

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

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

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1635,7 +1635,7 @@ add_fp_unittest(
16351635
FDimTest.h
16361636
DEPENDS
16371637
libc.src.math.fdimf
1638-
libc.src.__support.FPUtil.basic_operations
1638+
libc.src.__support.CPP.algorithm
16391639
libc.src.__support.FPUtil.fp_bits
16401640
)
16411641

@@ -1649,7 +1649,7 @@ add_fp_unittest(
16491649
FDimTest.h
16501650
DEPENDS
16511651
libc.src.math.fdim
1652-
libc.src.__support.FPUtil.basic_operations
1652+
libc.src.__support.CPP.algorithm
16531653
libc.src.__support.FPUtil.fp_bits
16541654
)
16551655

@@ -1663,7 +1663,21 @@ add_fp_unittest(
16631663
FDimTest.h
16641664
DEPENDS
16651665
libc.src.math.fdiml
1666-
libc.src.__support.FPUtil.basic_operations
1666+
libc.src.__support.CPP.algorithm
1667+
libc.src.__support.FPUtil.fp_bits
1668+
)
1669+
1670+
add_fp_unittest(
1671+
fdimf16_test
1672+
SUITE
1673+
libc-math-smoke-tests
1674+
SRCS
1675+
fdimf16_test.cpp
1676+
HDRS
1677+
FDimTest.h
1678+
DEPENDS
1679+
libc.src.math.fdimf16
1680+
libc.src.__support.CPP.algorithm
16671681
libc.src.__support.FPUtil.fp_bits
16681682
)
16691683

@@ -1677,7 +1691,7 @@ add_fp_unittest(
16771691
FDimTest.h
16781692
DEPENDS
16791693
libc.src.math.fdimf128
1680-
libc.src.__support.FPUtil.basic_operations
1694+
libc.src.__support.CPP.algorithm
16811695
libc.src.__support.FPUtil.fp_bits
16821696
)
16831697

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

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
//
77
//===---------------------------------------------------------------------===//
88

9-
#include "src/__support/FPUtil/BasicOperations.h"
9+
#include "src/__support/CPP/algorithm.h"
1010
#include "src/__support/FPUtil/FPBits.h"
1111
#include "test/UnitTest/FEnvSafeTest.h"
1212
#include "test/UnitTest/FPMatcher.h"
@@ -61,10 +61,11 @@ class FDimTestTemplate : public LIBC_NAMESPACE::testing::FEnvSafeTest {
6161
void test_in_range(FuncPtr func) {
6262
constexpr StorageType STORAGE_MAX =
6363
LIBC_NAMESPACE::cpp::numeric_limits<StorageType>::max();
64-
constexpr StorageType COUNT = 100'001;
65-
constexpr StorageType STEP = STORAGE_MAX / COUNT;
66-
for (StorageType i = 0, v = 0, w = STORAGE_MAX; i <= COUNT;
67-
++i, v += STEP, w -= STEP) {
64+
constexpr int COUNT = 100'001;
65+
constexpr StorageType STEP = LIBC_NAMESPACE::cpp::max(
66+
static_cast<StorageType>(STORAGE_MAX / COUNT), StorageType(1));
67+
StorageType v = 0, w = STORAGE_MAX;
68+
for (int i = 0; i <= COUNT; ++i, v += STEP, w -= STEP) {
6869
FPBits xbits(v), ybits(w);
6970
if (xbits.is_inf_or_nan())
7071
continue;
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
//===-- Unittests for fdimf16 ---------------------------------------------===//
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/fdimf16.h"
12+
13+
LIST_FDIM_TESTS(float16, LIBC_NAMESPACE::fdimf16);

0 commit comments

Comments
 (0)