Skip to content

Commit 6c97303

Browse files
authored
[libc][math][c23] Add copysignf16 C23 math function (llvm#94351)
llvm#93566
1 parent b954926 commit 6c97303

File tree

11 files changed

+94
-4
lines changed

11 files changed

+94
-4
lines changed

libc/config/linux/aarch64/entrypoints.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -501,6 +501,7 @@ if(LIBC_TYPES_HAS_FLOAT16)
501501
# math.h C23 _Float16 entrypoints
502502
libc.src.math.canonicalizef16
503503
libc.src.math.ceilf16
504+
libc.src.math.copysignf16
504505
libc.src.math.fabsf16
505506
libc.src.math.floorf16
506507
libc.src.math.fromfpf16

libc/config/linux/x86_64/entrypoints.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -534,6 +534,7 @@ if(LIBC_TYPES_HAS_FLOAT16)
534534
# math.h C23 _Float16 entrypoints
535535
libc.src.math.canonicalizef16
536536
libc.src.math.ceilf16
537+
libc.src.math.copysignf16
537538
libc.src.math.fabsf16
538539
libc.src.math.floorf16
539540
libc.src.math.fromfpf16

libc/docs/math/index.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ Basic Operations
112112
+------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
113113
| canonicalize | |check| | |check| | |check| | |check| | |check| | 7.12.11.7 | F.10.8.7 |
114114
+------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
115-
| copysign | |check| | |check| | |check| | | |check| | 7.12.11.1 | F.10.8.1 |
115+
| copysign | |check| | |check| | |check| | |check| | |check| | 7.12.11.1 | F.10.8.1 |
116116
+------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
117117
| dadd | N/A | N/A | | N/A | | 7.12.14.1 | F.10.11 |
118118
+------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+

libc/spec/stdc.td

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -385,6 +385,7 @@ def StdC : StandardSpec<"stdc"> {
385385
FunctionSpec<"copysign", RetValSpec<DoubleType>, [ArgSpec<DoubleType>, ArgSpec<DoubleType>]>,
386386
FunctionSpec<"copysignf", RetValSpec<FloatType>, [ArgSpec<FloatType>, ArgSpec<FloatType>]>,
387387
FunctionSpec<"copysignl", RetValSpec<LongDoubleType>, [ArgSpec<LongDoubleType>, ArgSpec<LongDoubleType>]>,
388+
GuardedFunctionSpec<"copysignf16", RetValSpec<Float16Type>, [ArgSpec<Float16Type>, ArgSpec<Float16Type>], "LIBC_TYPES_HAS_FLOAT16">,
388389
GuardedFunctionSpec<"copysignf128", RetValSpec<Float128Type>, [ArgSpec<Float128Type>, ArgSpec<Float128Type>], "LIBC_TYPES_HAS_FLOAT128">,
389390

390391
FunctionSpec<"ceil", RetValSpec<DoubleType>, [ArgSpec<DoubleType>]>,

libc/src/math/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ add_math_entrypoint_object(ceilf128)
7474
add_math_entrypoint_object(copysign)
7575
add_math_entrypoint_object(copysignf)
7676
add_math_entrypoint_object(copysignl)
77+
add_math_entrypoint_object(copysignf16)
7778
add_math_entrypoint_object(copysignf128)
7879

7980
add_math_entrypoint_object(cos)

libc/src/math/copysignf16.h

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

libc/src/math/generic/CMakeLists.txt

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1239,6 +1239,19 @@ add_entrypoint_object(
12391239
-O3
12401240
)
12411241

1242+
add_entrypoint_object(
1243+
copysignf16
1244+
SRCS
1245+
copysignf16.cpp
1246+
HDRS
1247+
../copysignf16.h
1248+
DEPENDS
1249+
libc.src.__support.macros.properties.types
1250+
libc.src.__support.FPUtil.manipulation_functions
1251+
COMPILE_OPTIONS
1252+
-O3
1253+
)
1254+
12421255
add_entrypoint_object(
12431256
copysignf128
12441257
SRCS

libc/src/math/generic/copysignf16.cpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
//===-- Implementation of copysignf16 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/copysignf16.h"
10+
#include "src/__support/FPUtil/ManipulationFunctions.h"
11+
#include "src/__support/common.h"
12+
13+
namespace LIBC_NAMESPACE {
14+
15+
LLVM_LIBC_FUNCTION(float16, copysignf16, (float16 x, float16 y)) {
16+
return fputil::copysign(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
@@ -995,6 +995,7 @@ add_fp_unittest(
995995
CopySignTest.h
996996
DEPENDS
997997
libc.src.math.copysign
998+
libc.src.__support.CPP.algorithm
998999
libc.src.__support.FPUtil.fp_bits
9991000
)
10001001

@@ -1008,6 +1009,7 @@ add_fp_unittest(
10081009
CopySignTest.h
10091010
DEPENDS
10101011
libc.src.math.copysignf
1012+
libc.src.__support.CPP.algorithm
10111013
libc.src.__support.FPUtil.fp_bits
10121014
)
10131015

@@ -1021,6 +1023,21 @@ add_fp_unittest(
10211023
CopySignTest.h
10221024
DEPENDS
10231025
libc.src.math.copysignl
1026+
libc.src.__support.CPP.algorithm
1027+
libc.src.__support.FPUtil.fp_bits
1028+
)
1029+
1030+
add_fp_unittest(
1031+
copysignf16_test
1032+
SUITE
1033+
libc-math-smoke-tests
1034+
SRCS
1035+
copysignf16_test.cpp
1036+
HDRS
1037+
CopySignTest.h
1038+
DEPENDS
1039+
libc.src.math.copysignf16
1040+
libc.src.__support.CPP.algorithm
10241041
libc.src.__support.FPUtil.fp_bits
10251042
)
10261043

@@ -1034,6 +1051,7 @@ add_fp_unittest(
10341051
CopySignTest.h
10351052
DEPENDS
10361053
libc.src.math.copysignf128
1054+
libc.src.__support.CPP.algorithm
10371055
libc.src.__support.FPUtil.fp_bits
10381056
)
10391057

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

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

12+
#include "src/__support/CPP/algorithm.h"
1213
#include "test/UnitTest/FEnvSafeTest.h"
1314
#include "test/UnitTest/FPMatcher.h"
1415
#include "test/UnitTest/Test.h"
@@ -35,9 +36,11 @@ class CopySignTest : public LIBC_NAMESPACE::testing::FEnvSafeTest {
3536
}
3637

3738
void testRange(CopySignFunc func) {
38-
constexpr StorageType COUNT = 100'000;
39-
constexpr StorageType STEP = STORAGE_MAX / COUNT;
40-
for (StorageType i = 0, v = 0; i <= COUNT; ++i, v += STEP) {
39+
constexpr int COUNT = 100'000;
40+
constexpr StorageType STEP = LIBC_NAMESPACE::cpp::max(
41+
static_cast<StorageType>(STORAGE_MAX / COUNT), StorageType(1));
42+
StorageType v = 0;
43+
for (int i = 0; i <= COUNT; ++i, v += STEP) {
4144
FPBits x_bits = FPBits(v);
4245
T x = T(v);
4346
if (x_bits.is_nan() || x_bits.is_inf())
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
//===-- Unittests for copysignf16 -----------------------------------------===//
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 "CopySignTest.h"
10+
11+
#include "src/math/copysignf16.h"
12+
13+
LIST_COPYSIGN_TESTS(float16, LIBC_NAMESPACE::copysignf16)

0 commit comments

Comments
 (0)