Skip to content

Commit

Permalink
[libc][math][c23] Add copysignf16 C23 math function (#94351)
Browse files Browse the repository at this point in the history
  • Loading branch information
overmighty authored Jun 5, 2024
1 parent b954926 commit 6c97303
Show file tree
Hide file tree
Showing 11 changed files with 94 additions and 4 deletions.
1 change: 1 addition & 0 deletions libc/config/linux/aarch64/entrypoints.txt
Original file line number Diff line number Diff line change
Expand Up @@ -501,6 +501,7 @@ if(LIBC_TYPES_HAS_FLOAT16)
# math.h C23 _Float16 entrypoints
libc.src.math.canonicalizef16
libc.src.math.ceilf16
libc.src.math.copysignf16
libc.src.math.fabsf16
libc.src.math.floorf16
libc.src.math.fromfpf16
Expand Down
1 change: 1 addition & 0 deletions libc/config/linux/x86_64/entrypoints.txt
Original file line number Diff line number Diff line change
Expand Up @@ -534,6 +534,7 @@ if(LIBC_TYPES_HAS_FLOAT16)
# math.h C23 _Float16 entrypoints
libc.src.math.canonicalizef16
libc.src.math.ceilf16
libc.src.math.copysignf16
libc.src.math.fabsf16
libc.src.math.floorf16
libc.src.math.fromfpf16
Expand Down
2 changes: 1 addition & 1 deletion libc/docs/math/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ Basic Operations
+------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
| canonicalize | |check| | |check| | |check| | |check| | |check| | 7.12.11.7 | F.10.8.7 |
+------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
| copysign | |check| | |check| | |check| | | |check| | 7.12.11.1 | F.10.8.1 |
| copysign | |check| | |check| | |check| | |check| | |check| | 7.12.11.1 | F.10.8.1 |
+------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
| dadd | N/A | N/A | | N/A | | 7.12.14.1 | F.10.11 |
+------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
Expand Down
1 change: 1 addition & 0 deletions libc/spec/stdc.td
Original file line number Diff line number Diff line change
Expand Up @@ -385,6 +385,7 @@ def StdC : StandardSpec<"stdc"> {
FunctionSpec<"copysign", RetValSpec<DoubleType>, [ArgSpec<DoubleType>, ArgSpec<DoubleType>]>,
FunctionSpec<"copysignf", RetValSpec<FloatType>, [ArgSpec<FloatType>, ArgSpec<FloatType>]>,
FunctionSpec<"copysignl", RetValSpec<LongDoubleType>, [ArgSpec<LongDoubleType>, ArgSpec<LongDoubleType>]>,
GuardedFunctionSpec<"copysignf16", RetValSpec<Float16Type>, [ArgSpec<Float16Type>, ArgSpec<Float16Type>], "LIBC_TYPES_HAS_FLOAT16">,
GuardedFunctionSpec<"copysignf128", RetValSpec<Float128Type>, [ArgSpec<Float128Type>, ArgSpec<Float128Type>], "LIBC_TYPES_HAS_FLOAT128">,

FunctionSpec<"ceil", RetValSpec<DoubleType>, [ArgSpec<DoubleType>]>,
Expand Down
1 change: 1 addition & 0 deletions libc/src/math/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ add_math_entrypoint_object(ceilf128)
add_math_entrypoint_object(copysign)
add_math_entrypoint_object(copysignf)
add_math_entrypoint_object(copysignl)
add_math_entrypoint_object(copysignf16)
add_math_entrypoint_object(copysignf128)

add_math_entrypoint_object(cos)
Expand Down
20 changes: 20 additions & 0 deletions libc/src/math/copysignf16.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
//===-- Implementation header for copysignf16 -------------------*- C++ -*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#ifndef LLVM_LIBC_SRC_MATH_COPYSIGNF16_H
#define LLVM_LIBC_SRC_MATH_COPYSIGNF16_H

#include "src/__support/macros/properties/types.h"

namespace LIBC_NAMESPACE {

float16 copysignf16(float16 x, float16 y);

} // namespace LIBC_NAMESPACE

#endif // LLVM_LIBC_SRC_MATH_COPYSIGNF16_H
13 changes: 13 additions & 0 deletions libc/src/math/generic/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1239,6 +1239,19 @@ add_entrypoint_object(
-O3
)

add_entrypoint_object(
copysignf16
SRCS
copysignf16.cpp
HDRS
../copysignf16.h
DEPENDS
libc.src.__support.macros.properties.types
libc.src.__support.FPUtil.manipulation_functions
COMPILE_OPTIONS
-O3
)

add_entrypoint_object(
copysignf128
SRCS
Expand Down
19 changes: 19 additions & 0 deletions libc/src/math/generic/copysignf16.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
//===-- Implementation of copysignf16 function ----------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#include "src/math/copysignf16.h"
#include "src/__support/FPUtil/ManipulationFunctions.h"
#include "src/__support/common.h"

namespace LIBC_NAMESPACE {

LLVM_LIBC_FUNCTION(float16, copysignf16, (float16 x, float16 y)) {
return fputil::copysign(x, y);
}

} // namespace LIBC_NAMESPACE
18 changes: 18 additions & 0 deletions libc/test/src/math/smoke/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -995,6 +995,7 @@ add_fp_unittest(
CopySignTest.h
DEPENDS
libc.src.math.copysign
libc.src.__support.CPP.algorithm
libc.src.__support.FPUtil.fp_bits
)

Expand All @@ -1008,6 +1009,7 @@ add_fp_unittest(
CopySignTest.h
DEPENDS
libc.src.math.copysignf
libc.src.__support.CPP.algorithm
libc.src.__support.FPUtil.fp_bits
)

Expand All @@ -1021,6 +1023,21 @@ add_fp_unittest(
CopySignTest.h
DEPENDS
libc.src.math.copysignl
libc.src.__support.CPP.algorithm
libc.src.__support.FPUtil.fp_bits
)

add_fp_unittest(
copysignf16_test
SUITE
libc-math-smoke-tests
SRCS
copysignf16_test.cpp
HDRS
CopySignTest.h
DEPENDS
libc.src.math.copysignf16
libc.src.__support.CPP.algorithm
libc.src.__support.FPUtil.fp_bits
)

Expand All @@ -1034,6 +1051,7 @@ add_fp_unittest(
CopySignTest.h
DEPENDS
libc.src.math.copysignf128
libc.src.__support.CPP.algorithm
libc.src.__support.FPUtil.fp_bits
)

Expand Down
9 changes: 6 additions & 3 deletions libc/test/src/math/smoke/CopySignTest.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#ifndef LLVM_LIBC_TEST_SRC_MATH_SMOKE_COPYSIGNTEST_H
#define LLVM_LIBC_TEST_SRC_MATH_SMOKE_COPYSIGNTEST_H

#include "src/__support/CPP/algorithm.h"
#include "test/UnitTest/FEnvSafeTest.h"
#include "test/UnitTest/FPMatcher.h"
#include "test/UnitTest/Test.h"
Expand All @@ -35,9 +36,11 @@ class CopySignTest : public LIBC_NAMESPACE::testing::FEnvSafeTest {
}

void testRange(CopySignFunc func) {
constexpr StorageType COUNT = 100'000;
constexpr StorageType STEP = STORAGE_MAX / COUNT;
for (StorageType i = 0, v = 0; i <= COUNT; ++i, v += STEP) {
constexpr int COUNT = 100'000;
constexpr StorageType STEP = LIBC_NAMESPACE::cpp::max(
static_cast<StorageType>(STORAGE_MAX / COUNT), StorageType(1));
StorageType v = 0;
for (int i = 0; i <= COUNT; ++i, v += STEP) {
FPBits x_bits = FPBits(v);
T x = T(v);
if (x_bits.is_nan() || x_bits.is_inf())
Expand Down
13 changes: 13 additions & 0 deletions libc/test/src/math/smoke/copysignf16_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
//===-- Unittests for copysignf16 -----------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#include "CopySignTest.h"

#include "src/math/copysignf16.h"

LIST_COPYSIGN_TESTS(float16, LIBC_NAMESPACE::copysignf16)

0 comments on commit 6c97303

Please sign in to comment.