https://github.com/hulxv updated https://github.com/llvm/llvm-project/pull/215727
>From 5a95631756811e8efba0fe60090bf1bdc5b780f5 Mon Sep 17 00:00:00 2001 From: hulxv <[email protected]> Date: Sun, 9 Aug 2026 02:10:36 +0300 Subject: [PATCH] [compiler-rt][builtins] libc-backed quad-to-int conversion builtins --- compiler-rt/lib/builtins/CMakeLists.txt | 6 ++ compiler-rt/lib/builtins/fixtfdi.cpp | 26 ++++++++ compiler-rt/lib/builtins/fixtfsi.cpp | 26 ++++++++ compiler-rt/lib/builtins/fixtfti.cpp | 26 ++++++++ compiler-rt/lib/builtins/fixunstfdi.cpp | 26 ++++++++ compiler-rt/lib/builtins/fixunstfsi.cpp | 26 ++++++++ compiler-rt/lib/builtins/fixunstfti.cpp | 27 ++++++++ libc/shared/builtins.h | 6 ++ libc/shared/builtins/fixtfdi.h | 35 ++++++++++ libc/shared/builtins/fixtfsi.h | 35 ++++++++++ libc/shared/builtins/fixtfti.h | 36 ++++++++++ libc/shared/builtins/fixunstfdi.h | 35 ++++++++++ libc/shared/builtins/fixunstfsi.h | 35 ++++++++++ libc/shared/builtins/fixunstfti.h | 36 ++++++++++ libc/src/__support/builtins/CMakeLists.txt | 76 ++++++++++++++++++++++ libc/src/__support/builtins/fixtfdi.h | 40 ++++++++++++ libc/src/__support/builtins/fixtfsi.h | 40 ++++++++++++ libc/src/__support/builtins/fixtfti.h | 41 ++++++++++++ libc/src/__support/builtins/fixunstfdi.h | 40 ++++++++++++ libc/src/__support/builtins/fixunstfsi.h | 40 ++++++++++++ libc/src/__support/builtins/fixunstfti.h | 41 ++++++++++++ libc/test/shared/CMakeLists.txt | 6 ++ libc/test/shared/shared_builtins_test.cpp | 24 +++++++ 23 files changed, 729 insertions(+) create mode 100644 compiler-rt/lib/builtins/fixtfdi.cpp create mode 100644 compiler-rt/lib/builtins/fixtfsi.cpp create mode 100644 compiler-rt/lib/builtins/fixtfti.cpp create mode 100644 compiler-rt/lib/builtins/fixunstfdi.cpp create mode 100644 compiler-rt/lib/builtins/fixunstfsi.cpp create mode 100644 compiler-rt/lib/builtins/fixunstfti.cpp create mode 100644 libc/shared/builtins/fixtfdi.h create mode 100644 libc/shared/builtins/fixtfsi.h create mode 100644 libc/shared/builtins/fixtfti.h create mode 100644 libc/shared/builtins/fixunstfdi.h create mode 100644 libc/shared/builtins/fixunstfsi.h create mode 100644 libc/shared/builtins/fixunstfti.h create mode 100644 libc/src/__support/builtins/fixtfdi.h create mode 100644 libc/src/__support/builtins/fixtfsi.h create mode 100644 libc/src/__support/builtins/fixtfti.h create mode 100644 libc/src/__support/builtins/fixunstfdi.h create mode 100644 libc/src/__support/builtins/fixunstfsi.h create mode 100644 libc/src/__support/builtins/fixunstfti.h diff --git a/compiler-rt/lib/builtins/CMakeLists.txt b/compiler-rt/lib/builtins/CMakeLists.txt index 0254cb6f8f31a..653f3c9bba75f 100644 --- a/compiler-rt/lib/builtins/CMakeLists.txt +++ b/compiler-rt/lib/builtins/CMakeLists.txt @@ -340,6 +340,12 @@ if(COMPILER_RT_USE_LIBC_MATH) use_libc_builtin(GENERIC_TF_SOURCES trunctfdf2) use_libc_builtin(GENERIC_TF_SOURCES trunctfsf2) use_libc_builtin(x86_80_BIT_SOURCES trunctfxf2) + use_libc_builtin(GENERIC_TF_SOURCES fixtfsi) + use_libc_builtin(GENERIC_TF_SOURCES fixtfdi) + use_libc_builtin(GENERIC_TF_SOURCES fixtfti) + use_libc_builtin(GENERIC_TF_SOURCES fixunstfsi) + use_libc_builtin(GENERIC_TF_SOURCES fixunstfdi) + use_libc_builtin(GENERIC_TF_SOURCES fixunstfti) endif() option(COMPILER_RT_EXCLUDE_ATOMIC_BUILTIN diff --git a/compiler-rt/lib/builtins/fixtfdi.cpp b/compiler-rt/lib/builtins/fixtfdi.cpp new file mode 100644 index 0000000000000..f95f24a1f699d --- /dev/null +++ b/compiler-rt/lib/builtins/fixtfdi.cpp @@ -0,0 +1,26 @@ +//===----------------------------------------------------------------------===// +// +// 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 file implements compiler-rt's __fixtfdi, truncating float128 -> int64_t +/// conversion (saturating), on top of LLVM-libc's shared::fixtfdi. +/// +//===----------------------------------------------------------------------===// + +#define QUAD_PRECISION +#include "fp_lib.h" + +#include "fp_libc_config.h" +#include "int_lib.h" +#include "shared/builtins/fixtfdi.h" + +#if defined(CRT_HAS_TF_MODE) +extern "C" COMPILER_RT_ABI di_int __fixtfdi(fp_t a) { + return LIBC_NAMESPACE::shared::fixtfdi(a); +} +#endif diff --git a/compiler-rt/lib/builtins/fixtfsi.cpp b/compiler-rt/lib/builtins/fixtfsi.cpp new file mode 100644 index 0000000000000..e5220732a7453 --- /dev/null +++ b/compiler-rt/lib/builtins/fixtfsi.cpp @@ -0,0 +1,26 @@ +//===----------------------------------------------------------------------===// +// +// 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 file implements compiler-rt's __fixtfsi, truncating float128 -> int32_t +/// conversion (saturating), on top of LLVM-libc's shared::fixtfsi. +/// +//===----------------------------------------------------------------------===// + +#define QUAD_PRECISION +#include "fp_lib.h" + +#include "fp_libc_config.h" +#include "int_lib.h" +#include "shared/builtins/fixtfsi.h" + +#if defined(CRT_HAS_TF_MODE) +extern "C" COMPILER_RT_ABI si_int __fixtfsi(fp_t a) { + return LIBC_NAMESPACE::shared::fixtfsi(a); +} +#endif diff --git a/compiler-rt/lib/builtins/fixtfti.cpp b/compiler-rt/lib/builtins/fixtfti.cpp new file mode 100644 index 0000000000000..c3d84d5d21097 --- /dev/null +++ b/compiler-rt/lib/builtins/fixtfti.cpp @@ -0,0 +1,26 @@ +//===----------------------------------------------------------------------===// +// +// 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 file implements compiler-rt's __fixtfti, truncating float128 -> +/// __int128_t conversion (saturating), on top of LLVM-libc's shared::fixtfti. +/// +//===----------------------------------------------------------------------===// + +#define QUAD_PRECISION +#include "fp_lib.h" + +#include "fp_libc_config.h" +#include "int_lib.h" +#include "shared/builtins/fixtfti.h" + +#if defined(CRT_HAS_TF_MODE) +extern "C" COMPILER_RT_ABI ti_int __fixtfti(fp_t a) { + return LIBC_NAMESPACE::shared::fixtfti(a); +} +#endif diff --git a/compiler-rt/lib/builtins/fixunstfdi.cpp b/compiler-rt/lib/builtins/fixunstfdi.cpp new file mode 100644 index 0000000000000..2025e6c290ff4 --- /dev/null +++ b/compiler-rt/lib/builtins/fixunstfdi.cpp @@ -0,0 +1,26 @@ +//===----------------------------------------------------------------------===// +// +// 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 file implements compiler-rt's __fixunstfdi, truncating float128 -> +/// uint64_t conversion (saturating), on top of LLVM-libc's shared::fixunstfdi. +/// +//===----------------------------------------------------------------------===// + +#define QUAD_PRECISION +#include "fp_lib.h" + +#include "fp_libc_config.h" +#include "int_lib.h" +#include "shared/builtins/fixunstfdi.h" + +#if defined(CRT_HAS_TF_MODE) +extern "C" COMPILER_RT_ABI du_int __fixunstfdi(fp_t a) { + return LIBC_NAMESPACE::shared::fixunstfdi(a); +} +#endif diff --git a/compiler-rt/lib/builtins/fixunstfsi.cpp b/compiler-rt/lib/builtins/fixunstfsi.cpp new file mode 100644 index 0000000000000..aca69261270e8 --- /dev/null +++ b/compiler-rt/lib/builtins/fixunstfsi.cpp @@ -0,0 +1,26 @@ +//===----------------------------------------------------------------------===// +// +// 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 file implements compiler-rt's __fixunstfsi, truncating float128 -> +/// uint32_t conversion (saturating), on top of LLVM-libc's shared::fixunstfsi. +/// +//===----------------------------------------------------------------------===// + +#define QUAD_PRECISION +#include "fp_lib.h" + +#include "fp_libc_config.h" +#include "int_lib.h" +#include "shared/builtins/fixunstfsi.h" + +#if defined(CRT_HAS_TF_MODE) +extern "C" COMPILER_RT_ABI su_int __fixunstfsi(fp_t a) { + return LIBC_NAMESPACE::shared::fixunstfsi(a); +} +#endif diff --git a/compiler-rt/lib/builtins/fixunstfti.cpp b/compiler-rt/lib/builtins/fixunstfti.cpp new file mode 100644 index 0000000000000..02b9a464a955c --- /dev/null +++ b/compiler-rt/lib/builtins/fixunstfti.cpp @@ -0,0 +1,27 @@ +//===----------------------------------------------------------------------===// +// +// 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 file implements compiler-rt's __fixunstfti, truncating float128 -> +/// __uint128_t conversion (saturating), on top of LLVM-libc's +/// shared::fixunstfti. +/// +//===----------------------------------------------------------------------===// + +#define QUAD_PRECISION +#include "fp_lib.h" + +#include "fp_libc_config.h" +#include "int_lib.h" +#include "shared/builtins/fixunstfti.h" + +#if defined(CRT_HAS_TF_MODE) +extern "C" COMPILER_RT_ABI tu_int __fixunstfti(fp_t a) { + return LIBC_NAMESPACE::shared::fixunstfti(a); +} +#endif diff --git a/libc/shared/builtins.h b/libc/shared/builtins.h index 6e59031aa28a3..c1af1d39733ff 100644 --- a/libc/shared/builtins.h +++ b/libc/shared/builtins.h @@ -33,12 +33,18 @@ #include "builtins/fixsfdi.h" #include "builtins/fixsfsi.h" #include "builtins/fixsfti.h" +#include "builtins/fixtfdi.h" +#include "builtins/fixtfsi.h" +#include "builtins/fixtfti.h" #include "builtins/fixunsdfdi.h" #include "builtins/fixunsdfsi.h" #include "builtins/fixunsdfti.h" #include "builtins/fixunssfdi.h" #include "builtins/fixunssfsi.h" #include "builtins/fixunssfti.h" +#include "builtins/fixunstfdi.h" +#include "builtins/fixunstfsi.h" +#include "builtins/fixunstfti.h" #include "builtins/floatdidf.h" #include "builtins/floatdisf.h" #include "builtins/floatditf.h" diff --git a/libc/shared/builtins/fixtfdi.h b/libc/shared/builtins/fixtfdi.h new file mode 100644 index 0000000000000..1cae343fe0d3d --- /dev/null +++ b/libc/shared/builtins/fixtfdi.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 __fixtfdi implementation as shared::fixtfdi +/// so that it can be reused by compiler-rt's builtins. +/// +//===----------------------------------------------------------------------===// + +#ifndef LLVM_LIBC_SHARED_BUILTINS_FIXTFDI_H +#define LLVM_LIBC_SHARED_BUILTINS_FIXTFDI_H + +#include "include/llvm-libc-types/float128.h" + +#ifdef LIBC_TYPES_HAS_FLOAT128 + +#include "shared/libc_common.h" +#include "src/__support/builtins/fixtfdi.h" + +namespace LIBC_NAMESPACE_DECL { +namespace shared { + +using builtins::fixtfdi; + +} // namespace shared +} // namespace LIBC_NAMESPACE_DECL + +#endif // LIBC_TYPES_HAS_FLOAT128 + +#endif // LLVM_LIBC_SHARED_BUILTINS_FIXTFDI_H diff --git a/libc/shared/builtins/fixtfsi.h b/libc/shared/builtins/fixtfsi.h new file mode 100644 index 0000000000000..1d287892a258e --- /dev/null +++ b/libc/shared/builtins/fixtfsi.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 __fixtfsi implementation as shared::fixtfsi +/// so that it can be reused by compiler-rt's builtins. +/// +//===----------------------------------------------------------------------===// + +#ifndef LLVM_LIBC_SHARED_BUILTINS_FIXTFSI_H +#define LLVM_LIBC_SHARED_BUILTINS_FIXTFSI_H + +#include "include/llvm-libc-types/float128.h" + +#ifdef LIBC_TYPES_HAS_FLOAT128 + +#include "shared/libc_common.h" +#include "src/__support/builtins/fixtfsi.h" + +namespace LIBC_NAMESPACE_DECL { +namespace shared { + +using builtins::fixtfsi; + +} // namespace shared +} // namespace LIBC_NAMESPACE_DECL + +#endif // LIBC_TYPES_HAS_FLOAT128 + +#endif // LLVM_LIBC_SHARED_BUILTINS_FIXTFSI_H diff --git a/libc/shared/builtins/fixtfti.h b/libc/shared/builtins/fixtfti.h new file mode 100644 index 0000000000000..ae0862ce4ae06 --- /dev/null +++ b/libc/shared/builtins/fixtfti.h @@ -0,0 +1,36 @@ +//===----------------------------------------------------------------------===// +// +// 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 __fixtfti implementation as shared::fixtfti +/// so that it can be reused by compiler-rt's builtins. +/// +//===----------------------------------------------------------------------===// + +#ifndef LLVM_LIBC_SHARED_BUILTINS_FIXTFTI_H +#define LLVM_LIBC_SHARED_BUILTINS_FIXTFTI_H + +#include "include/llvm-libc-types/float128.h" +#include "src/__support/macros/properties/types.h" + +#if defined(LIBC_TYPES_HAS_FLOAT128) && defined(LIBC_TYPES_HAS_INT128) + +#include "shared/libc_common.h" +#include "src/__support/builtins/fixtfti.h" + +namespace LIBC_NAMESPACE_DECL { +namespace shared { + +using builtins::fixtfti; + +} // namespace shared +} // namespace LIBC_NAMESPACE_DECL + +#endif // LIBC_TYPES_HAS_FLOAT128 && LIBC_TYPES_HAS_INT128 + +#endif // LLVM_LIBC_SHARED_BUILTINS_FIXTFTI_H diff --git a/libc/shared/builtins/fixunstfdi.h b/libc/shared/builtins/fixunstfdi.h new file mode 100644 index 0000000000000..4afe2e94c383f --- /dev/null +++ b/libc/shared/builtins/fixunstfdi.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 __fixunstfdi implementation as +/// shared::fixunstfdi so that it can be reused by compiler-rt's builtins. +/// +//===----------------------------------------------------------------------===// + +#ifndef LLVM_LIBC_SHARED_BUILTINS_FIXUNSTFDI_H +#define LLVM_LIBC_SHARED_BUILTINS_FIXUNSTFDI_H + +#include "include/llvm-libc-types/float128.h" + +#ifdef LIBC_TYPES_HAS_FLOAT128 + +#include "shared/libc_common.h" +#include "src/__support/builtins/fixunstfdi.h" + +namespace LIBC_NAMESPACE_DECL { +namespace shared { + +using builtins::fixunstfdi; + +} // namespace shared +} // namespace LIBC_NAMESPACE_DECL + +#endif // LIBC_TYPES_HAS_FLOAT128 + +#endif // LLVM_LIBC_SHARED_BUILTINS_FIXUNSTFDI_H diff --git a/libc/shared/builtins/fixunstfsi.h b/libc/shared/builtins/fixunstfsi.h new file mode 100644 index 0000000000000..c856a8420114a --- /dev/null +++ b/libc/shared/builtins/fixunstfsi.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 __fixunstfsi implementation as +/// shared::fixunstfsi so that it can be reused by compiler-rt's builtins. +/// +//===----------------------------------------------------------------------===// + +#ifndef LLVM_LIBC_SHARED_BUILTINS_FIXUNSTFSI_H +#define LLVM_LIBC_SHARED_BUILTINS_FIXUNSTFSI_H + +#include "include/llvm-libc-types/float128.h" + +#ifdef LIBC_TYPES_HAS_FLOAT128 + +#include "shared/libc_common.h" +#include "src/__support/builtins/fixunstfsi.h" + +namespace LIBC_NAMESPACE_DECL { +namespace shared { + +using builtins::fixunstfsi; + +} // namespace shared +} // namespace LIBC_NAMESPACE_DECL + +#endif // LIBC_TYPES_HAS_FLOAT128 + +#endif // LLVM_LIBC_SHARED_BUILTINS_FIXUNSTFSI_H diff --git a/libc/shared/builtins/fixunstfti.h b/libc/shared/builtins/fixunstfti.h new file mode 100644 index 0000000000000..53ad7bb03a7c9 --- /dev/null +++ b/libc/shared/builtins/fixunstfti.h @@ -0,0 +1,36 @@ +//===----------------------------------------------------------------------===// +// +// 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 __fixunstfti implementation as +/// shared::fixunstfti so that it can be reused by compiler-rt's builtins. +/// +//===----------------------------------------------------------------------===// + +#ifndef LLVM_LIBC_SHARED_BUILTINS_FIXUNSTFTI_H +#define LLVM_LIBC_SHARED_BUILTINS_FIXUNSTFTI_H + +#include "include/llvm-libc-types/float128.h" +#include "src/__support/macros/properties/types.h" + +#if defined(LIBC_TYPES_HAS_FLOAT128) && defined(LIBC_TYPES_HAS_INT128) + +#include "shared/libc_common.h" +#include "src/__support/builtins/fixunstfti.h" + +namespace LIBC_NAMESPACE_DECL { +namespace shared { + +using builtins::fixunstfti; + +} // namespace shared +} // namespace LIBC_NAMESPACE_DECL + +#endif // LIBC_TYPES_HAS_FLOAT128 && LIBC_TYPES_HAS_INT128 + +#endif // LLVM_LIBC_SHARED_BUILTINS_FIXUNSTFTI_H diff --git a/libc/src/__support/builtins/CMakeLists.txt b/libc/src/__support/builtins/CMakeLists.txt index d37879c3ec7a2..fd764241d07b7 100644 --- a/libc/src/__support/builtins/CMakeLists.txt +++ b/libc/src/__support/builtins/CMakeLists.txt @@ -7,6 +7,14 @@ add_header_library( libc.src.__support.FPUtil.fp_bits libc.src.__support.macros.attributes libc.src.__support.macros.c + fixint_helper + HDRS + fixint_helper.h + DEPENDS + libc.src.__support.CPP.type_traits + libc.src.__support.FPUtil.fp_bits + libc.src.__support.macros.attributes + libc.src.__support.macros.config ) add_header_library( @@ -648,3 +656,71 @@ add_header_library( libc.src.__support.macros.config libc.src.__support.macros.properties.types ) + +add_header_library( + fixtfsi + HDRS + fixtfsi.h + DEPENDS + libc.hdr.stdint_proxy + libc.include.llvm-libc-types.float128 + libc.src.__support.builtins.fixint_helper + libc.src.__support.macros.config +) + +add_header_library( + fixtfdi + HDRS + fixtfdi.h + DEPENDS + libc.hdr.stdint_proxy + libc.include.llvm-libc-types.float128 + libc.src.__support.builtins.fixint_helper + libc.src.__support.macros.config +) + +add_header_library( + fixtfti + HDRS + fixtfti.h + DEPENDS + libc.hdr.stdint_proxy + libc.include.llvm-libc-types.float128 + libc.src.__support.builtins.fixint_helper + libc.src.__support.macros.config + libc.src.__support.macros.properties.types +) + +add_header_library( + fixunstfsi + HDRS + fixunstfsi.h + DEPENDS + libc.hdr.stdint_proxy + libc.include.llvm-libc-types.float128 + libc.src.__support.builtins.fixint_helper + libc.src.__support.macros.config +) + +add_header_library( + fixunstfdi + HDRS + fixunstfdi.h + DEPENDS + libc.hdr.stdint_proxy + libc.include.llvm-libc-types.float128 + libc.src.__support.builtins.fixint_helper + libc.src.__support.macros.config +) + +add_header_library( + fixunstfti + HDRS + fixunstfti.h + DEPENDS + libc.hdr.stdint_proxy + libc.include.llvm-libc-types.float128 + libc.src.__support.builtins.fixint_helper + libc.src.__support.macros.config + libc.src.__support.macros.properties.types +) diff --git a/libc/src/__support/builtins/fixtfdi.h b/libc/src/__support/builtins/fixtfdi.h new file mode 100644 index 0000000000000..659789517c6a5 --- /dev/null +++ b/libc/src/__support/builtins/fixtfdi.h @@ -0,0 +1,40 @@ +//===----------------------------------------------------------------------===// +// +// 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 __fixtfdi implementation as +/// builtins::fixtfdi so that it can be reused by compiler-rt's builtins. +/// +//===----------------------------------------------------------------------===// + +#ifndef LLVM_LIBC_SRC___SUPPORT_BUILTINS_FIXTFDI_H +#define LLVM_LIBC_SRC___SUPPORT_BUILTINS_FIXTFDI_H + +#include "include/llvm-libc-types/float128.h" + +#ifdef LIBC_TYPES_HAS_FLOAT128 + +#include "hdr/stdint_proxy.h" +#include "src/__support/builtins/fixint_helper.h" +#include "src/__support/macros/config.h" + +namespace LIBC_NAMESPACE_DECL { +namespace builtins { + +// Truncating float128 -> int64_t conversion, saturating on overflow. +// Mirrors compiler-rt's __fixtfdi. +LIBC_INLINE int64_t fixtfdi(float128 x) { + return fixint<int64_t>(x); +} + +} // namespace builtins +} // namespace LIBC_NAMESPACE_DECL + +#endif // LIBC_TYPES_HAS_FLOAT128 + +#endif // LLVM_LIBC_SRC___SUPPORT_BUILTINS_FIXTFDI_H diff --git a/libc/src/__support/builtins/fixtfsi.h b/libc/src/__support/builtins/fixtfsi.h new file mode 100644 index 0000000000000..101acadfbf83c --- /dev/null +++ b/libc/src/__support/builtins/fixtfsi.h @@ -0,0 +1,40 @@ +//===----------------------------------------------------------------------===// +// +// 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 __fixtfsi implementation as +/// builtins::fixtfsi so that it can be reused by compiler-rt's builtins. +/// +//===----------------------------------------------------------------------===// + +#ifndef LLVM_LIBC_SRC___SUPPORT_BUILTINS_FIXTFSI_H +#define LLVM_LIBC_SRC___SUPPORT_BUILTINS_FIXTFSI_H + +#include "include/llvm-libc-types/float128.h" + +#ifdef LIBC_TYPES_HAS_FLOAT128 + +#include "hdr/stdint_proxy.h" +#include "src/__support/builtins/fixint_helper.h" +#include "src/__support/macros/config.h" + +namespace LIBC_NAMESPACE_DECL { +namespace builtins { + +// Truncating float128 -> int32_t conversion, saturating on overflow. +// Mirrors compiler-rt's __fixtfsi. +LIBC_INLINE int32_t fixtfsi(float128 x) { + return fixint<int32_t>(x); +} + +} // namespace builtins +} // namespace LIBC_NAMESPACE_DECL + +#endif // LIBC_TYPES_HAS_FLOAT128 + +#endif // LLVM_LIBC_SRC___SUPPORT_BUILTINS_FIXTFSI_H diff --git a/libc/src/__support/builtins/fixtfti.h b/libc/src/__support/builtins/fixtfti.h new file mode 100644 index 0000000000000..b4206bc6b7424 --- /dev/null +++ b/libc/src/__support/builtins/fixtfti.h @@ -0,0 +1,41 @@ +//===----------------------------------------------------------------------===// +// +// 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 __fixtfti implementation as +/// builtins::fixtfti so that it can be reused by compiler-rt's builtins. +/// +//===----------------------------------------------------------------------===// + +#ifndef LLVM_LIBC_SRC___SUPPORT_BUILTINS_FIXTFTI_H +#define LLVM_LIBC_SRC___SUPPORT_BUILTINS_FIXTFTI_H + +#include "include/llvm-libc-types/float128.h" +#include "src/__support/macros/properties/types.h" + +#if defined(LIBC_TYPES_HAS_FLOAT128) && defined(LIBC_TYPES_HAS_INT128) + +#include "hdr/stdint_proxy.h" +#include "src/__support/builtins/fixint_helper.h" +#include "src/__support/macros/config.h" + +namespace LIBC_NAMESPACE_DECL { +namespace builtins { + +// Truncating float128 -> __int128_t conversion, saturating on overflow. +// Mirrors compiler-rt's __fixtfti. +LIBC_INLINE __int128_t fixtfti(float128 x) { + return fixint<__int128_t>(x); +} + +} // namespace builtins +} // namespace LIBC_NAMESPACE_DECL + +#endif // LIBC_TYPES_HAS_FLOAT128 && LIBC_TYPES_HAS_INT128 + +#endif // LLVM_LIBC_SRC___SUPPORT_BUILTINS_FIXTFTI_H diff --git a/libc/src/__support/builtins/fixunstfdi.h b/libc/src/__support/builtins/fixunstfdi.h new file mode 100644 index 0000000000000..d839623becd18 --- /dev/null +++ b/libc/src/__support/builtins/fixunstfdi.h @@ -0,0 +1,40 @@ +//===----------------------------------------------------------------------===// +// +// 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 __fixunstfdi implementation as +/// builtins::fixunstfdi so that it can be reused by compiler-rt's builtins. +/// +//===----------------------------------------------------------------------===// + +#ifndef LLVM_LIBC_SRC___SUPPORT_BUILTINS_FIXUNSTFDI_H +#define LLVM_LIBC_SRC___SUPPORT_BUILTINS_FIXUNSTFDI_H + +#include "include/llvm-libc-types/float128.h" + +#ifdef LIBC_TYPES_HAS_FLOAT128 + +#include "hdr/stdint_proxy.h" +#include "src/__support/builtins/fixint_helper.h" +#include "src/__support/macros/config.h" + +namespace LIBC_NAMESPACE_DECL { +namespace builtins { + +// Truncating float128 -> uint64_t conversion, saturating on overflow. +// Mirrors compiler-rt's __fixunstfdi. +LIBC_INLINE uint64_t fixunstfdi(float128 x) { + return fixuint<uint64_t>(x); +} + +} // namespace builtins +} // namespace LIBC_NAMESPACE_DECL + +#endif // LIBC_TYPES_HAS_FLOAT128 + +#endif // LLVM_LIBC_SRC___SUPPORT_BUILTINS_FIXUNSTFDI_H diff --git a/libc/src/__support/builtins/fixunstfsi.h b/libc/src/__support/builtins/fixunstfsi.h new file mode 100644 index 0000000000000..8b7d38c1d8442 --- /dev/null +++ b/libc/src/__support/builtins/fixunstfsi.h @@ -0,0 +1,40 @@ +//===----------------------------------------------------------------------===// +// +// 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 __fixunstfsi implementation as +/// builtins::fixunstfsi so that it can be reused by compiler-rt's builtins. +/// +//===----------------------------------------------------------------------===// + +#ifndef LLVM_LIBC_SRC___SUPPORT_BUILTINS_FIXUNSTFSI_H +#define LLVM_LIBC_SRC___SUPPORT_BUILTINS_FIXUNSTFSI_H + +#include "include/llvm-libc-types/float128.h" + +#ifdef LIBC_TYPES_HAS_FLOAT128 + +#include "hdr/stdint_proxy.h" +#include "src/__support/builtins/fixint_helper.h" +#include "src/__support/macros/config.h" + +namespace LIBC_NAMESPACE_DECL { +namespace builtins { + +// Truncating float128 -> uint32_t conversion, saturating on overflow. +// Mirrors compiler-rt's __fixunstfsi. +LIBC_INLINE uint32_t fixunstfsi(float128 x) { + return fixuint<uint32_t>(x); +} + +} // namespace builtins +} // namespace LIBC_NAMESPACE_DECL + +#endif // LIBC_TYPES_HAS_FLOAT128 + +#endif // LLVM_LIBC_SRC___SUPPORT_BUILTINS_FIXUNSTFSI_H diff --git a/libc/src/__support/builtins/fixunstfti.h b/libc/src/__support/builtins/fixunstfti.h new file mode 100644 index 0000000000000..3bcae869c215e --- /dev/null +++ b/libc/src/__support/builtins/fixunstfti.h @@ -0,0 +1,41 @@ +//===----------------------------------------------------------------------===// +// +// 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 __fixunstfti implementation as +/// builtins::fixunstfti so that it can be reused by compiler-rt's builtins. +/// +//===----------------------------------------------------------------------===// + +#ifndef LLVM_LIBC_SRC___SUPPORT_BUILTINS_FIXUNSTFTI_H +#define LLVM_LIBC_SRC___SUPPORT_BUILTINS_FIXUNSTFTI_H + +#include "include/llvm-libc-types/float128.h" +#include "src/__support/macros/properties/types.h" + +#if defined(LIBC_TYPES_HAS_FLOAT128) && defined(LIBC_TYPES_HAS_INT128) + +#include "hdr/stdint_proxy.h" +#include "src/__support/builtins/fixint_helper.h" +#include "src/__support/macros/config.h" + +namespace LIBC_NAMESPACE_DECL { +namespace builtins { + +// Truncating float128 -> __uint128_t conversion, saturating on overflow. +// Mirrors compiler-rt's __fixunstfti. +LIBC_INLINE __uint128_t fixunstfti(float128 x) { + return fixuint<__uint128_t>(x); +} + +} // namespace builtins +} // namespace LIBC_NAMESPACE_DECL + +#endif // LIBC_TYPES_HAS_FLOAT128 && LIBC_TYPES_HAS_INT128 + +#endif // LLVM_LIBC_SRC___SUPPORT_BUILTINS_FIXUNSTFTI_H diff --git a/libc/test/shared/CMakeLists.txt b/libc/test/shared/CMakeLists.txt index e40b1a101bdf3..0763692046de7 100644 --- a/libc/test/shared/CMakeLists.txt +++ b/libc/test/shared/CMakeLists.txt @@ -846,12 +846,18 @@ add_fp_unittest( libc.src.__support.builtins.fixsfdi libc.src.__support.builtins.fixsfsi libc.src.__support.builtins.fixsfti + libc.src.__support.builtins.fixtfdi + libc.src.__support.builtins.fixtfsi + libc.src.__support.builtins.fixtfti libc.src.__support.builtins.fixunsdfdi libc.src.__support.builtins.fixunsdfsi libc.src.__support.builtins.fixunsdfti libc.src.__support.builtins.fixunssfdi libc.src.__support.builtins.fixunssfsi libc.src.__support.builtins.fixunssfti + libc.src.__support.builtins.fixunstfdi + libc.src.__support.builtins.fixunstfsi + libc.src.__support.builtins.fixunstfti libc.src.__support.builtins.floatdidf libc.src.__support.builtins.floatdisf libc.src.__support.builtins.floatditf diff --git a/libc/test/shared/shared_builtins_test.cpp b/libc/test/shared/shared_builtins_test.cpp index 26f9670ed38ba..d98574cb0fabd 100644 --- a/libc/test/shared/shared_builtins_test.cpp +++ b/libc/test/shared/shared_builtins_test.cpp @@ -73,6 +73,30 @@ TEST(LlvmLibcSharedBuiltinsTest, DoubleToUIntConversion) { #endif // LIBC_TYPES_HAS_INT128 } +#ifdef LIBC_TYPES_HAS_FLOAT128 + +TEST(LlvmLibcSharedBuiltinsTest, QuadToIntConversion) { + EXPECT_EQ(int64_t(12), shared::fixtfdi(float128(12.5))); + EXPECT_EQ(int32_t(12), shared::fixtfsi(float128(12.5))); +#ifdef LIBC_TYPES_HAS_INT128 + EXPECT_EQ(static_cast<__int128_t>(12), shared::fixtfti(float128(12.5))); +#endif // LIBC_TYPES_HAS_INT128 +} + +#endif // LIBC_TYPES_HAS_FLOAT128 + +#ifdef LIBC_TYPES_HAS_FLOAT128 + +TEST(LlvmLibcSharedBuiltinsTest, QuadToUIntConversion) { + EXPECT_EQ(uint64_t(12), shared::fixunstfdi(float128(12.5))); + EXPECT_EQ(uint32_t(12), shared::fixunstfsi(float128(12.5))); +#ifdef LIBC_TYPES_HAS_INT128 + EXPECT_EQ(static_cast<__uint128_t>(12), shared::fixunstfti(float128(12.5))); +#endif // LIBC_TYPES_HAS_INT128 +} + +#endif // LIBC_TYPES_HAS_FLOAT128 + TEST(LlvmLibcSharedBuiltinsTest, IntToDoubleConversion) { EXPECT_FP_EQ(12.0, shared::floatdidf(int64_t(12))); EXPECT_FP_EQ(12.0, shared::floatsidf(int32_t(12))); _______________________________________________ llvm-branch-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits
