diff --git a/libc/shared/math.h b/libc/shared/math.h index 275b89db3179a..83cd6196b146b 100644 --- a/libc/shared/math.h +++ b/libc/shared/math.h @@ -72,6 +72,12 @@ #include "math/ceilf128.h" #include "math/ceilf16.h" #include "math/ceill.h" +#include "math/copysign.h" +#include "math/copysignbf16.h" +#include "math/copysignf.h" +#include "math/copysignf128.h" +#include "math/copysignf16.h" +#include "math/copysignl.h" #include "math/cos.h" #include "math/cosf.h" #include "math/cosf16.h" diff --git a/libc/shared/math/copysign.h b/libc/shared/math/copysign.h new file mode 100644 index 0000000000000..ffe0e82e925ea --- /dev/null +++ b/libc/shared/math/copysign.h @@ -0,0 +1,23 @@ +//===-- Shared copysign function --------------------------------*- 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_SHARED_MATH_COPYSIGN_H +#define LLVM_LIBC_SHARED_MATH_COPYSIGN_H + +#include "shared/libc_common.h" +#include "src/__support/math/copysign.h" + +namespace LIBC_NAMESPACE_DECL { +namespace shared { + +using math::copysign; + +} // namespace shared +} // namespace LIBC_NAMESPACE_DECL + +#endif // LLVM_LIBC_SHARED_MATH_COPYSIGN_H diff --git a/libc/shared/math/copysignbf16.h b/libc/shared/math/copysignbf16.h new file mode 100644 index 0000000000000..fbf2196c15276 --- /dev/null +++ b/libc/shared/math/copysignbf16.h @@ -0,0 +1,23 @@ +//===-- Shared copysignbf16 function ----------------------------*- 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_SHARED_MATH_COPYSIGNBF16_H +#define LLVM_LIBC_SHARED_MATH_COPYSIGNBF16_H + +#include "shared/libc_common.h" +#include "src/__support/math/copysignbf16.h" + +namespace LIBC_NAMESPACE_DECL { +namespace shared { + +using math::copysignbf16; + +} // namespace shared +} // namespace LIBC_NAMESPACE_DECL + +#endif // LLVM_LIBC_SHARED_MATH_COPYSIGNBF16_H diff --git a/libc/shared/math/copysignf.h b/libc/shared/math/copysignf.h new file mode 100644 index 0000000000000..5918ffbe773a3 --- /dev/null +++ b/libc/shared/math/copysignf.h @@ -0,0 +1,23 @@ +//===-- Shared copysignf function -------------------------------*- 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_SHARED_MATH_COPYSIGNF_H +#define LLVM_LIBC_SHARED_MATH_COPYSIGNF_H + +#include "shared/libc_common.h" +#include "src/__support/math/copysignf.h" + +namespace LIBC_NAMESPACE_DECL { +namespace shared { + +using math::copysignf; + +} // namespace shared +} // namespace LIBC_NAMESPACE_DECL + +#endif // LLVM_LIBC_SHARED_MATH_COPYSIGNF_H diff --git a/libc/shared/math/copysignf128.h b/libc/shared/math/copysignf128.h new file mode 100644 index 0000000000000..24059a6fc971f --- /dev/null +++ b/libc/shared/math/copysignf128.h @@ -0,0 +1,29 @@ +//===-- Shared copysignf128 function ----------------------------*- 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_SHARED_MATH_COPYSIGNF128_H +#define LLVM_LIBC_SHARED_MATH_COPYSIGNF128_H + +#include "include/llvm-libc-types/float128.h" + +#ifdef LIBC_TYPES_HAS_FLOAT128 + +#include "shared/libc_common.h" +#include "src/__support/math/copysignf128.h" + +namespace LIBC_NAMESPACE_DECL { +namespace shared { + +using math::copysignf128; + +} // namespace shared +} // namespace LIBC_NAMESPACE_DECL + +#endif // LIBC_TYPES_HAS_FLOAT128 + +#endif // LLVM_LIBC_SHARED_MATH_COPYSIGNF128_H diff --git a/libc/shared/math/copysignf16.h b/libc/shared/math/copysignf16.h new file mode 100644 index 0000000000000..239a184a0a191 --- /dev/null +++ b/libc/shared/math/copysignf16.h @@ -0,0 +1,29 @@ +//===-- Shared copysignf16 function -----------------------------*- 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_SHARED_MATH_COPYSIGNF16_H +#define LLVM_LIBC_SHARED_MATH_COPYSIGNF16_H + +#include "include/llvm-libc-macros/float16-macros.h" + +#ifdef LIBC_TYPES_HAS_FLOAT16 + +#include "shared/libc_common.h" +#include "src/__support/math/copysignf16.h" + +namespace LIBC_NAMESPACE_DECL { +namespace shared { + +using math::copysignf16; + +} // namespace shared +} // namespace LIBC_NAMESPACE_DECL + +#endif // LIBC_TYPES_HAS_FLOAT16 + +#endif // LLVM_LIBC_SHARED_MATH_COPYSIGNF16_H diff --git a/libc/shared/math/copysignl.h b/libc/shared/math/copysignl.h new file mode 100644 index 0000000000000..e72e2340b3303 --- /dev/null +++ b/libc/shared/math/copysignl.h @@ -0,0 +1,23 @@ +//===-- Shared copysignl function -------------------------------*- 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_SHARED_MATH_COPYSIGNL_H +#define LLVM_LIBC_SHARED_MATH_COPYSIGNL_H + +#include "shared/libc_common.h" +#include "src/__support/math/copysignl.h" + +namespace LIBC_NAMESPACE_DECL { +namespace shared { + +using math::copysignl; + +} // namespace shared +} // namespace LIBC_NAMESPACE_DECL + +#endif // LLVM_LIBC_SHARED_MATH_COPYSIGNL_H diff --git a/libc/src/__support/FPUtil/ManipulationFunctions.h b/libc/src/__support/FPUtil/ManipulationFunctions.h index ea9ee5a57c36d..2a3e6deb3833e 100644 --- a/libc/src/__support/FPUtil/ManipulationFunctions.h +++ b/libc/src/__support/FPUtil/ManipulationFunctions.h @@ -74,7 +74,7 @@ LIBC_INLINE T modf(T x, T &iptr) { } template , int> = 0> -LIBC_INLINE T copysign(T x, T y) { +LIBC_INLINE constexpr T copysign(T x, T y) { FPBits xbits(x); xbits.set_sign(FPBits(y).sign()); return xbits.get_val(); diff --git a/libc/src/__support/math/CMakeLists.txt b/libc/src/__support/math/CMakeLists.txt index 987ecab8c57ef..d1c42afbaafe1 100644 --- a/libc/src/__support/math/CMakeLists.txt +++ b/libc/src/__support/math/CMakeLists.txt @@ -827,6 +827,71 @@ add_header_library( libc.src.__support.math_extras ) +add_header_library( + copysign + HDRS + copysign.h + DEPENDS + libc.src.__support.FPUtil.manipulation_functions + libc.src.__support.macros.config + FLAGS + MISC_MATH_BASIC_OPS_OPT +) + +add_header_library( + copysignbf16 + HDRS + copysignbf16.h + DEPENDS + libc.src.__support.FPUtil.bfloat16 + libc.src.__support.FPUtil.manipulation_functions + libc.src.__support.macros.config + FLAGS + MISC_MATH_BASIC_OPS_OPT +) + +add_header_library( + copysignf + HDRS + copysignf.h + DEPENDS + libc.src.__support.FPUtil.manipulation_functions + libc.src.__support.macros.config + FLAGS + MISC_MATH_BASIC_OPS_OPT +) + +add_header_library( + copysignf128 + HDRS + copysignf128.h + DEPENDS + libc.include.llvm-libc-types.float128 + libc.src.__support.FPUtil.manipulation_functions + libc.src.__support.macros.config +) + +add_header_library( + copysignf16 + HDRS + copysignf16.h + DEPENDS + libc.include.llvm-libc-macros.float16_macros + libc.src.__support.FPUtil.manipulation_functions + libc.src.__support.macros.config + FLAGS + MISC_MATH_BASIC_OPS_OPT +) + +add_header_library( + copysignl + HDRS + copysignl.h + DEPENDS + libc.src.__support.FPUtil.manipulation_functions + libc.src.__support.macros.config +) + add_header_library( cos_integer_eval HDRS diff --git a/libc/src/__support/math/copysign.h b/libc/src/__support/math/copysign.h new file mode 100644 index 0000000000000..85f16eec8dba2 --- /dev/null +++ b/libc/src/__support/math/copysign.h @@ -0,0 +1,30 @@ +//===-- Implementation header for copysign ----------------------*- 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___SUPPORT_MATH_COPYSIGN_H +#define LLVM_LIBC_SRC___SUPPORT_MATH_COPYSIGN_H + +#include "src/__support/FPUtil/ManipulationFunctions.h" +#include "src/__support/macros/config.h" + +namespace LIBC_NAMESPACE_DECL { +namespace math { + +LIBC_INLINE LIBC_CONSTEXPR double copysign(double x, double y) { +#if defined(__LIBC_MISC_MATH_BASIC_OPS_OPT) && \ + !defined(LIBC_HAS_CONSTANT_EVALUATION) + return __builtin_copysign(x, y); +#else + return fputil::copysign(x, y); +#endif +} + +} // namespace math +} // namespace LIBC_NAMESPACE_DECL + +#endif // LLVM_LIBC_SRC___SUPPORT_MATH_COPYSIGN_H diff --git a/libc/src/__support/math/copysignbf16.h b/libc/src/__support/math/copysignbf16.h new file mode 100644 index 0000000000000..d37ee7e894fb7 --- /dev/null +++ b/libc/src/__support/math/copysignbf16.h @@ -0,0 +1,26 @@ +//===-- Implementation header for copysignbf16 ------------------*- 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___SUPPORT_MATH_COPYSIGNBF16_H +#define LLVM_LIBC_SRC___SUPPORT_MATH_COPYSIGNBF16_H + +#include "src/__support/FPUtil/ManipulationFunctions.h" +#include "src/__support/FPUtil/bfloat16.h" +#include "src/__support/macros/config.h" + +namespace LIBC_NAMESPACE_DECL { +namespace math { + +LIBC_INLINE constexpr bfloat16 copysignbf16(bfloat16 x, bfloat16 y) { + return fputil::copysign(x, y); +} + +} // namespace math +} // namespace LIBC_NAMESPACE_DECL + +#endif // LLVM_LIBC_SRC___SUPPORT_MATH_COPYSIGNBF16_H diff --git a/libc/src/__support/math/copysignf.h b/libc/src/__support/math/copysignf.h new file mode 100644 index 0000000000000..256c78e4b4d0f --- /dev/null +++ b/libc/src/__support/math/copysignf.h @@ -0,0 +1,30 @@ +//===-- Implementation header for copysignf ---------------------*- 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___SUPPORT_MATH_COPYSIGNF_H +#define LLVM_LIBC_SRC___SUPPORT_MATH_COPYSIGNF_H + +#include "src/__support/FPUtil/ManipulationFunctions.h" +#include "src/__support/macros/config.h" + +namespace LIBC_NAMESPACE_DECL { +namespace math { + +LIBC_INLINE LIBC_CONSTEXPR float copysignf(float x, float y) { +#if defined(__LIBC_MISC_MATH_BASIC_OPS_OPT) && \ + !defined(LIBC_HAS_CONSTANT_EVALUATION) + return __builtin_copysignf(x, y); +#else + return fputil::copysign(x, y); +#endif +} + +} // namespace math +} // namespace LIBC_NAMESPACE_DECL + +#endif // LLVM_LIBC_SRC___SUPPORT_MATH_COPYSIGNF_H diff --git a/libc/src/__support/math/copysignf128.h b/libc/src/__support/math/copysignf128.h new file mode 100644 index 0000000000000..23dc0d51f1951 --- /dev/null +++ b/libc/src/__support/math/copysignf128.h @@ -0,0 +1,31 @@ +//===-- Implementation header for copysignf128 ------------------*- 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___SUPPORT_MATH_COPYSIGNF128_H +#define LLVM_LIBC_SRC___SUPPORT_MATH_COPYSIGNF128_H + +#include "include/llvm-libc-types/float128.h" + +#ifdef LIBC_TYPES_HAS_FLOAT128 + +#include "src/__support/FPUtil/ManipulationFunctions.h" +#include "src/__support/macros/config.h" + +namespace LIBC_NAMESPACE_DECL { +namespace math { + +LIBC_INLINE constexpr float128 copysignf128(float128 x, float128 y) { + return fputil::copysign(x, y); +} + +} // namespace math +} // namespace LIBC_NAMESPACE_DECL + +#endif // LIBC_TYPES_HAS_FLOAT128 + +#endif // LLVM_LIBC_SRC___SUPPORT_MATH_COPYSIGNF128_H diff --git a/libc/src/__support/math/copysignf16.h b/libc/src/__support/math/copysignf16.h new file mode 100644 index 0000000000000..6cbda9ee0d4b7 --- /dev/null +++ b/libc/src/__support/math/copysignf16.h @@ -0,0 +1,36 @@ +//===-- 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___SUPPORT_MATH_COPYSIGNF16_H +#define LLVM_LIBC_SRC___SUPPORT_MATH_COPYSIGNF16_H + +#include "include/llvm-libc-macros/float16-macros.h" + +#ifdef LIBC_TYPES_HAS_FLOAT16 + +#include "src/__support/FPUtil/ManipulationFunctions.h" +#include "src/__support/macros/config.h" + +namespace LIBC_NAMESPACE_DECL { +namespace math { + +LIBC_INLINE LIBC_CONSTEXPR float16 copysignf16(float16 x, float16 y) { +#if defined(__LIBC_MISC_MATH_BASIC_OPS_OPT) && \ + !defined(LIBC_HAS_CONSTANT_EVALUATION) + return __builtin_copysignf16(x, y); +#else + return fputil::copysign(x, y); +#endif +} + +} // namespace math +} // namespace LIBC_NAMESPACE_DECL + +#endif // LIBC_TYPES_HAS_FLOAT16 + +#endif // LLVM_LIBC_SRC___SUPPORT_MATH_COPYSIGNF16_H diff --git a/libc/src/__support/math/copysignl.h b/libc/src/__support/math/copysignl.h new file mode 100644 index 0000000000000..58dcac8257140 --- /dev/null +++ b/libc/src/__support/math/copysignl.h @@ -0,0 +1,25 @@ +//===-- Implementation header for copysignl ---------------------*- 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___SUPPORT_MATH_COPYSIGNL_H +#define LLVM_LIBC_SRC___SUPPORT_MATH_COPYSIGNL_H + +#include "src/__support/FPUtil/ManipulationFunctions.h" +#include "src/__support/macros/config.h" + +namespace LIBC_NAMESPACE_DECL { +namespace math { + +LIBC_INLINE constexpr long double copysignl(long double x, long double y) { + return fputil::copysign(x, y); +} + +} // namespace math +} // namespace LIBC_NAMESPACE_DECL + +#endif // LLVM_LIBC_SRC___SUPPORT_MATH_COPYSIGNL_H diff --git a/libc/src/math/generic/CMakeLists.txt b/libc/src/math/generic/CMakeLists.txt index 5d265b89391e6..740b78418492b 100644 --- a/libc/src/math/generic/CMakeLists.txt +++ b/libc/src/math/generic/CMakeLists.txt @@ -1499,9 +1499,7 @@ add_entrypoint_object( HDRS ../copysign.h DEPENDS - libc.src.__support.FPUtil.manipulation_functions - FLAGS - MISC_MATH_BASIC_OPS_OPT + libc.src.__support.math.copysign ) add_entrypoint_object( @@ -1511,9 +1509,7 @@ add_entrypoint_object( HDRS ../copysignf.h DEPENDS - libc.src.__support.FPUtil.manipulation_functions - FLAGS - MISC_MATH_BASIC_OPS_OPT + libc.src.__support.math.copysignf ) add_entrypoint_object( @@ -1523,7 +1519,7 @@ add_entrypoint_object( HDRS ../copysignl.h DEPENDS - libc.src.__support.FPUtil.manipulation_functions + libc.src.__support.math.copysignl ) add_entrypoint_object( @@ -1533,10 +1529,7 @@ add_entrypoint_object( HDRS ../copysignf16.h DEPENDS - libc.src.__support.macros.properties.types - libc.src.__support.FPUtil.manipulation_functions - FLAGS - MISC_MATH_BASIC_OPS_OPT + libc.src.__support.math.copysignf16 ) add_entrypoint_object( @@ -1546,8 +1539,7 @@ add_entrypoint_object( HDRS ../copysignf128.h DEPENDS - libc.src.__support.macros.properties.types - libc.src.__support.FPUtil.manipulation_functions + libc.src.__support.math.copysignf128 ) add_entrypoint_object( @@ -1557,13 +1549,7 @@ add_entrypoint_object( HDRS ../copysignbf16.h DEPENDS - libc.src.__support.common - libc.src.__support.macros.config - libc.src.__support.macros.properties.types - libc.src.__support.FPUtil.bfloat16 - libc.src.__support.FPUtil.manipulation_functions - FLAGS - MISC_MATH_BASIC_OPS_OPT + libc.src.__support.math.copysignbf16 ) add_entrypoint_object( diff --git a/libc/src/math/generic/copysign.cpp b/libc/src/math/generic/copysign.cpp index 186bb2c5983f4..6d92b0600eeb2 100644 --- a/libc/src/math/generic/copysign.cpp +++ b/libc/src/math/generic/copysign.cpp @@ -7,18 +7,12 @@ //===----------------------------------------------------------------------===// #include "src/math/copysign.h" -#include "src/__support/FPUtil/ManipulationFunctions.h" -#include "src/__support/common.h" -#include "src/__support/macros/config.h" +#include "src/__support/math/copysign.h" namespace LIBC_NAMESPACE_DECL { LLVM_LIBC_FUNCTION(double, copysign, (double x, double y)) { -#ifdef __LIBC_MISC_MATH_BASIC_OPS_OPT - return __builtin_copysign(x, y); -#else - return fputil::copysign(x, y); -#endif + return math::copysign(x, y); } } // namespace LIBC_NAMESPACE_DECL diff --git a/libc/src/math/generic/copysignbf16.cpp b/libc/src/math/generic/copysignbf16.cpp index 48ade2b26981c..548fa42835e5f 100644 --- a/libc/src/math/generic/copysignbf16.cpp +++ b/libc/src/math/generic/copysignbf16.cpp @@ -7,15 +7,12 @@ //===----------------------------------------------------------------------===// #include "src/math/copysignbf16.h" -#include "src/__support/FPUtil/ManipulationFunctions.h" -#include "src/__support/FPUtil/bfloat16.h" -#include "src/__support/common.h" -#include "src/__support/macros/config.h" +#include "src/__support/math/copysignbf16.h" namespace LIBC_NAMESPACE_DECL { LLVM_LIBC_FUNCTION(bfloat16, copysignbf16, (bfloat16 x, bfloat16 y)) { - return fputil::copysign(x, y); + return math::copysignbf16(x, y); } } // namespace LIBC_NAMESPACE_DECL diff --git a/libc/src/math/generic/copysignf.cpp b/libc/src/math/generic/copysignf.cpp index c79e50b61ebda..94eeba9eaf705 100644 --- a/libc/src/math/generic/copysignf.cpp +++ b/libc/src/math/generic/copysignf.cpp @@ -7,18 +7,12 @@ //===----------------------------------------------------------------------===// #include "src/math/copysignf.h" -#include "src/__support/FPUtil/ManipulationFunctions.h" -#include "src/__support/common.h" -#include "src/__support/macros/config.h" +#include "src/__support/math/copysignf.h" namespace LIBC_NAMESPACE_DECL { LLVM_LIBC_FUNCTION(float, copysignf, (float x, float y)) { -#ifdef __LIBC_MISC_MATH_BASIC_OPS_OPT - return __builtin_copysignf(x, y); -#else - return fputil::copysign(x, y); -#endif + return math::copysignf(x, y); } } // namespace LIBC_NAMESPACE_DECL diff --git a/libc/src/math/generic/copysignf128.cpp b/libc/src/math/generic/copysignf128.cpp index 9a51c8d5eb8df..9af98f0d10723 100644 --- a/libc/src/math/generic/copysignf128.cpp +++ b/libc/src/math/generic/copysignf128.cpp @@ -7,14 +7,12 @@ //===----------------------------------------------------------------------===// #include "src/math/copysignf128.h" -#include "src/__support/FPUtil/ManipulationFunctions.h" -#include "src/__support/common.h" -#include "src/__support/macros/config.h" +#include "src/__support/math/copysignf128.h" namespace LIBC_NAMESPACE_DECL { LLVM_LIBC_FUNCTION(float128, copysignf128, (float128 x, float128 y)) { - return fputil::copysign(x, y); + return math::copysignf128(x, y); } } // namespace LIBC_NAMESPACE_DECL diff --git a/libc/src/math/generic/copysignf16.cpp b/libc/src/math/generic/copysignf16.cpp index 546622f049ebe..23db8faf3211a 100644 --- a/libc/src/math/generic/copysignf16.cpp +++ b/libc/src/math/generic/copysignf16.cpp @@ -7,18 +7,12 @@ //===----------------------------------------------------------------------===// #include "src/math/copysignf16.h" -#include "src/__support/FPUtil/ManipulationFunctions.h" -#include "src/__support/common.h" -#include "src/__support/macros/config.h" +#include "src/__support/math/copysignf16.h" namespace LIBC_NAMESPACE_DECL { LLVM_LIBC_FUNCTION(float16, copysignf16, (float16 x, float16 y)) { -#ifdef __LIBC_MISC_MATH_BASIC_OPS_OPT - return __builtin_copysignf16(x, y); -#else - return fputil::copysign(x, y); -#endif + return math::copysignf16(x, y); } } // namespace LIBC_NAMESPACE_DECL diff --git a/libc/src/math/generic/copysignl.cpp b/libc/src/math/generic/copysignl.cpp index 73c47bb04ad4a..b2e6d3ed50ba9 100644 --- a/libc/src/math/generic/copysignl.cpp +++ b/libc/src/math/generic/copysignl.cpp @@ -7,14 +7,12 @@ //===----------------------------------------------------------------------===// #include "src/math/copysignl.h" -#include "src/__support/FPUtil/ManipulationFunctions.h" -#include "src/__support/common.h" -#include "src/__support/macros/config.h" +#include "src/__support/math/copysignl.h" namespace LIBC_NAMESPACE_DECL { LLVM_LIBC_FUNCTION(long double, copysignl, (long double x, long double y)) { - return fputil::copysign(x, y); + return math::copysignl(x, y); } } // namespace LIBC_NAMESPACE_DECL diff --git a/libc/test/shared/CMakeLists.txt b/libc/test/shared/CMakeLists.txt index 9c4b7edb377e2..3289321f25dd4 100644 --- a/libc/test/shared/CMakeLists.txt +++ b/libc/test/shared/CMakeLists.txt @@ -69,6 +69,12 @@ add_fp_unittest( libc.src.__support.math.ceilf128 libc.src.__support.math.ceilf16 libc.src.__support.math.ceill + libc.src.__support.math.copysign + libc.src.__support.math.copysignbf16 + libc.src.__support.math.copysignf + libc.src.__support.math.copysignf128 + libc.src.__support.math.copysignf16 + libc.src.__support.math.copysignl libc.src.__support.math.cos libc.src.__support.math.cosf libc.src.__support.math.cosf16 @@ -277,6 +283,12 @@ add_fp_unittest( libc.src.__support.math.ceilf128 libc.src.__support.math.ceilf16 libc.src.__support.math.ceill + libc.src.__support.math.copysign + libc.src.__support.math.copysignbf16 + libc.src.__support.math.copysignf + libc.src.__support.math.copysignf128 + libc.src.__support.math.copysignf16 + libc.src.__support.math.copysignl libc.src.__support.math.log ) diff --git a/libc/test/shared/shared_math_constexpr_test.cpp b/libc/test/shared/shared_math_constexpr_test.cpp index 53d416de30694..6fb1318e5a4ea 100644 --- a/libc/test/shared/shared_math_constexpr_test.cpp +++ b/libc/test/shared/shared_math_constexpr_test.cpp @@ -17,12 +17,14 @@ static_assert(0.0 == LIBC_NAMESPACE::shared::ceil(0.0)); static_assert(0.0 == LIBC_NAMESPACE::shared::log(1.0)); +static_assert(0.0 == LIBC_NAMESPACE::shared::copysign(0.0, 0.0)); //===----------------------------------------------------------------------===// // Float Tests //===----------------------------------------------------------------------===// static_assert(0.0f == LIBC_NAMESPACE::shared::ceilf(0.0f)); +static_assert(0.0f == LIBC_NAMESPACE::shared::copysignf(0.0f, 0.0f)); //===----------------------------------------------------------------------===// // Float16 Tests @@ -31,6 +33,7 @@ static_assert(0.0f == LIBC_NAMESPACE::shared::ceilf(0.0f)); #ifdef LIBC_TYPES_HAS_FLOAT16 static_assert(0.0f16 == LIBC_NAMESPACE::shared::ceilf16(0.0f16)); +static_assert(0.0f16 == LIBC_NAMESPACE::shared::copysignf16(0.0f16, 0.0f16)); #endif // LIBC_TYPES_HAS_FLOAT16 @@ -42,6 +45,7 @@ static_assert(0.0f16 == LIBC_NAMESPACE::shared::ceilf16(0.0f16)); #if 0 // Temporarily disable long double tests static_assert(0.0L == LIBC_NAMESPACE::shared::ceill(0.0L)); +static_assert(0.0L == LIBC_NAMESPACE::shared::copysignl(0.0L, 0.0L)); #endif @@ -52,6 +56,9 @@ static_assert(0.0L == LIBC_NAMESPACE::shared::ceill(0.0L)); #ifdef LIBC_TYPES_HAS_FLOAT128 static_assert(float128(0.0) == LIBC_NAMESPACE::shared::ceilf128(float128(0.0))); +static_assert(float128(0.0) == + LIBC_NAMESPACE::shared::copysignf128(float128(0.0), + float128(0.0))); #endif // LIBC_TYPES_HAS_FLOAT128 @@ -61,5 +68,8 @@ static_assert(float128(0.0) == LIBC_NAMESPACE::shared::ceilf128(float128(0.0))); static_assert(bfloat16(0.0) == LIBC_NAMESPACE::shared::asinbf16(bfloat16(0.0))); static_assert(bfloat16(0.0) == LIBC_NAMESPACE::shared::ceilbf16(bfloat16(0.0))); +static_assert(bfloat16(0.0) == + LIBC_NAMESPACE::shared::copysignbf16(bfloat16(0.0), + bfloat16(0.0))); TEST(LlvmLibcSharedMathTest, ConstantEvaluation) {} diff --git a/libc/test/shared/shared_math_test.cpp b/libc/test/shared/shared_math_test.cpp index 4c58c834960ed..5d83ce22af4ab 100644 --- a/libc/test/shared/shared_math_test.cpp +++ b/libc/test/shared/shared_math_test.cpp @@ -74,9 +74,11 @@ TEST(LlvmLibcSharedMathTest, AllFloat16) { EXPECT_FP_EQ(0.0f16, canonicalizef16_cx); EXPECT_FP_EQ(0.0f16, LIBC_NAMESPACE::shared::ceilf16(0.0f16)); + EXPECT_FP_EQ(0.0f16, LIBC_NAMESPACE::shared::copysignf16(0.0f16, 0.0f16)); EXPECT_FP_EQ(0.0f16, LIBC_NAMESPACE::shared::fdimf16(0.0f16, 0.0f16)); EXPECT_FP_EQ(0.0f16, LIBC_NAMESPACE::shared::floorf16(0.0f16)); EXPECT_FP_EQ(0.0f16, LIBC_NAMESPACE::shared::fmaxf16(0.0f16, 0.0f16)); + float16 getpayloadf16_x = 0.0f16; EXPECT_FP_EQ(-1.0f16, LIBC_NAMESPACE::shared::getpayloadf16(&getpayloadf16_x)); @@ -174,6 +176,7 @@ TEST(LlvmLibcSharedMathTest, AllFloat) { LIBC_NAMESPACE::shared::bf16fmaf(2.0f, 3.0f, 4.0f)); EXPECT_FP_EQ(0.0f, LIBC_NAMESPACE::shared::ceilf(0.0f)); + EXPECT_FP_EQ(0.0f, LIBC_NAMESPACE::shared::copysignf(0.0f, 0.0f)); EXPECT_FP_EQ(0.0f, LIBC_NAMESPACE::shared::fdimf(0.0f, 0.0f)); EXPECT_FP_EQ(0.0f, LIBC_NAMESPACE::shared::floorf(0.0f)); EXPECT_FP_EQ(0.0f, LIBC_NAMESPACE::shared::fmaxf(0.0f, 0.0f)); @@ -255,6 +258,7 @@ TEST(LlvmLibcSharedMathTest, AllDouble) { EXPECT_FP_EQ(bfloat16(10.0), LIBC_NAMESPACE::shared::bf16fma(2.0, 3.0, 4.0)); EXPECT_FP_EQ(0.0, LIBC_NAMESPACE::shared::ceil(0.0)); + EXPECT_FP_EQ(0.0, LIBC_NAMESPACE::shared::copysign(0.0, 0.0)); EXPECT_FP_EQ(0.0, LIBC_NAMESPACE::shared::fadd(0.0, 0.0)); EXPECT_FP_EQ(0.0, LIBC_NAMESPACE::shared::fdim(0.0, 0.0)); EXPECT_FP_EQ(0.0, LIBC_NAMESPACE::shared::floor(0.0)); @@ -314,6 +318,7 @@ TEST(LlvmLibcSharedMathTest, AllLongDouble) { LIBC_NAMESPACE::shared::bf16fmal(2.0L, 3.0L, 4.0L)); EXPECT_FP_EQ(0.0L, LIBC_NAMESPACE::shared::ceill(0.0L)); + EXPECT_FP_EQ(0.0L, LIBC_NAMESPACE::shared::copysignl(0.0L, 0.0L)); EXPECT_FP_EQ(0.0L, LIBC_NAMESPACE::shared::daddl(0.0L, 0.0L)); EXPECT_FP_EQ(0.0L, LIBC_NAMESPACE::shared::faddl(0.0L, 0.0L)); EXPECT_FP_EQ(0.0L, LIBC_NAMESPACE::shared::fdiml(0.0L, 0.0L)); @@ -394,6 +399,8 @@ TEST(LlvmLibcSharedMathTest, AllFloat128) { EXPECT_FP_EQ(bfloat16(2.0), LIBC_NAMESPACE::shared::bf16divf128( float128(4.0), float128(2.0))); EXPECT_FP_EQ(float128(0.0), LIBC_NAMESPACE::shared::ceilf128(float128(0.0))); + EXPECT_FP_EQ(float128(0.0), LIBC_NAMESPACE::shared::copysignf128( + float128(0.0), float128(0.0))); EXPECT_FP_EQ(float128(0.0), LIBC_NAMESPACE::shared::daddf128(float128(0.0), float128(0.0))); EXPECT_FP_EQ(float128(0.0), @@ -459,6 +466,8 @@ TEST(LlvmLibcSharedMathTest, AllBFloat16) { EXPECT_FP_EQ(bfloat16(0.0), canonicalizebf16_cx); EXPECT_FP_EQ(bfloat16(0.0), LIBC_NAMESPACE::shared::cbrtbf16(bfloat16(0.0))); EXPECT_FP_EQ(bfloat16(0.0), LIBC_NAMESPACE::shared::ceilbf16(bfloat16(0.0))); + EXPECT_FP_EQ(bfloat16(0.0), LIBC_NAMESPACE::shared::copysignbf16( + bfloat16(0.0), bfloat16(0.0))); EXPECT_FP_EQ(bfloat16(0.0), LIBC_NAMESPACE::shared::floorbf16(bfloat16(0.0))); EXPECT_FP_EQ(bfloat16(0.0), LIBC_NAMESPACE::shared::fdimbf16(bfloat16(0.0), bfloat16(0.0))); diff --git a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel index d97d65e4ee43a..a4fc74d3e23d9 100644 --- a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel +++ b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel @@ -3787,6 +3787,63 @@ libc_support_library( ], ) +libc_support_library( + name = "__support_math_copysign", + hdrs = ["src/__support/math/copysign.h"], + deps = [ + ":__support_fputil_manipulation_functions", + ":__support_macros_config", + ], +) + +libc_support_library( + name = "__support_math_copysignbf16", + hdrs = ["src/__support/math/copysignbf16.h"], + deps = [ + ":__support_fputil_bfloat16", + ":__support_fputil_manipulation_functions", + ":__support_macros_config", + ], +) + +libc_support_library( + name = "__support_math_copysignf", + hdrs = ["src/__support/math/copysignf.h"], + deps = [ + ":__support_fputil_manipulation_functions", + ":__support_macros_config", + ], +) + +libc_support_library( + name = "__support_math_copysignf128", + hdrs = ["src/__support/math/copysignf128.h"], + deps = [ + ":__support_fputil_manipulation_functions", + ":__support_macros_config", + ":llvm_libc_types_float128", + ], +) + +libc_support_library( + name = "__support_math_copysignf16", + hdrs = ["src/__support/math/copysignf16.h"], + deps = [ + ":__support_fputil_manipulation_functions", + ":__support_macros_config", + ":llvm_libc_macros_float16_macros", + ], +) + +libc_support_library( + name = "__support_math_copysignl", + hdrs = ["src/__support/math/copysignl.h"], + deps = [ + ":__support_fputil_manipulation_functions", + ":__support_macros_config", + ], +) + libc_support_library( name = "__support_math_common_constants", hdrs = ["src/__support/math/common_constants.h"], @@ -6939,18 +6996,46 @@ libc_math_function( ], ) -libc_math_function(name = "copysign") +libc_math_function( + name = "copysign", + additional_deps = [ + ":__support_math_copysign", + ], +) -libc_math_function(name = "copysignf") +libc_math_function( + name = "copysignbf16", + additional_deps = [ + ":__support_math_copysignbf16", + ], +) + +libc_math_function( + name = "copysignf", + additional_deps = [ + ":__support_math_copysignf", + ], +) -libc_math_function(name = "copysignl") +libc_math_function( + name = "copysignl", + additional_deps = [ + ":__support_math_copysignl", + ], +) libc_math_function( name = "copysignf128", + additional_deps = [ + ":__support_math_copysignf128", + ], ) libc_math_function( name = "copysignf16", + additional_deps = [ + ":__support_math_copysignf16", + ], ) libc_math_function(