diff --git a/libc/shared/builtins.h b/libc/shared/builtins.h index 338755314d309..0bb156131423d 100644 --- a/libc/shared/builtins.h +++ b/libc/shared/builtins.h @@ -18,5 +18,6 @@ #include "libc_common.h" #include "builtins/addtf3.h" +#include "builtins/subtf3.h" #endif // LLVM_LIBC_SHARED_BUILTINS_H diff --git a/libc/shared/builtins/subtf3.h b/libc/shared/builtins/subtf3.h new file mode 100644 index 0000000000000..a7cecef8b0479 --- /dev/null +++ b/libc/shared/builtins/subtf3.h @@ -0,0 +1,35 @@ +//===----------------------------------------------------------------------===// +// +// 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 +// +//===----------------------------------------------------------------------===// +/// +/// \file +/// This header exposes LLVM-libc's __subtf3 implementation as shared::subtf3 so +/// that it can be reused by compiler-rt's builtins. +/// +//===----------------------------------------------------------------------===// + +#ifndef LLVM_LIBC_SHARED_BUILTINS_SUBTF3_H +#define LLVM_LIBC_SHARED_BUILTINS_SUBTF3_H + +#include "include/llvm-libc-types/float128.h" + +#ifdef LIBC_TYPES_HAS_FLOAT128 + +#include "shared/libc_common.h" +#include "src/__support/builtins/subtf3.h" + +namespace LIBC_NAMESPACE_DECL { +namespace shared { + +using builtins::subtf3; + +} // namespace shared +} // namespace LIBC_NAMESPACE_DECL + +#endif // LIBC_TYPES_HAS_FLOAT128 + +#endif // LLVM_LIBC_SHARED_BUILTINS_SUBTF3_H diff --git a/libc/src/__support/builtins/CMakeLists.txt b/libc/src/__support/builtins/CMakeLists.txt index c4982887d0a7c..d3d1d65babce8 100644 --- a/libc/src/__support/builtins/CMakeLists.txt +++ b/libc/src/__support/builtins/CMakeLists.txt @@ -7,3 +7,13 @@ add_header_library( libc.src.__support.FPUtil.generic.add_sub libc.src.__support.macros.config ) + +add_header_library( + subtf3 + HDRS + subtf3.h + DEPENDS + libc.include.llvm-libc-types.float128 + libc.src.__support.FPUtil.generic.add_sub + libc.src.__support.macros.config +) diff --git a/libc/src/__support/builtins/subtf3.h b/libc/src/__support/builtins/subtf3.h new file mode 100644 index 0000000000000..bd758cce0f5f6 --- /dev/null +++ b/libc/src/__support/builtins/subtf3.h @@ -0,0 +1,38 @@ +//===----------------------------------------------------------------------===// +// +// 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 +// +//===----------------------------------------------------------------------===// +/// +/// \file +/// This header exposes LLVM-libc's __subtf3 implementation as builtins::subtf3 +/// so that it can be reused by compiler-rt's builtins. +/// +//===----------------------------------------------------------------------===// + +#ifndef LLVM_LIBC_SRC___SUPPORT_BUILTINS_SUBTF3_H +#define LLVM_LIBC_SRC___SUPPORT_BUILTINS_SUBTF3_H + +#include "include/llvm-libc-types/float128.h" + +#ifdef LIBC_TYPES_HAS_FLOAT128 + +#include "src/__support/FPUtil/generic/add_sub.h" +#include "src/__support/macros/config.h" + +namespace LIBC_NAMESPACE_DECL { +namespace builtins { + +// Subtraction at float128 precision; mirrors compiler-rt's __subtf3. +LIBC_INLINE float128 subtf3(float128 x, float128 y) { + return fputil::generic::sub(x, y); +} + +} // namespace builtins +} // namespace LIBC_NAMESPACE_DECL + +#endif // LIBC_TYPES_HAS_FLOAT128 + +#endif // LLVM_LIBC_SRC___SUPPORT_BUILTINS_SUBTF3_H diff --git a/libc/test/shared/CMakeLists.txt b/libc/test/shared/CMakeLists.txt index 7b75011009e55..98f787c7453f6 100644 --- a/libc/test/shared/CMakeLists.txt +++ b/libc/test/shared/CMakeLists.txt @@ -827,6 +827,7 @@ add_fp_unittest( shared_builtins_test.cpp DEPENDS libc.src.__support.builtins.addtf3 + libc.src.__support.builtins.subtf3 ) add_fp_unittest( diff --git a/libc/test/shared/shared_builtins_test.cpp b/libc/test/shared/shared_builtins_test.cpp index 67d40b349cef3..bf84d2c0866a4 100644 --- a/libc/test/shared/shared_builtins_test.cpp +++ b/libc/test/shared/shared_builtins_test.cpp @@ -21,9 +21,10 @@ TEST(LlvmLibcSharedBuiltinsTest, AllDouble) { #ifdef LIBC_TYPES_HAS_FLOAT128 TEST(LlvmLibcSharedBuiltinsTest, AllFloat128) { - namespace shared = LIBC_NAMESPACE::shared; - - EXPECT_FP_EQ(float128(3.0), shared::addtf3(float128(1.0), float128(2.0))); + EXPECT_FP_EQ(float128(3.0), + LIBC_NAMESPACE::shared::addtf3(float128(1.0), float128(2.0))); + EXPECT_FP_EQ(float128(2.0), + LIBC_NAMESPACE::shared::subtf3(float128(5.0), float128(3.0))); } #endif // LIBC_TYPES_HAS_FLOAT128