[libc-commits] [libc] 2abc0e5 - [compiler-rt][builtins] add libc-backed double/float-to-int builtins (#207543)
via libc-commits
libc-commits at lists.llvm.org
Sat Aug 8 13:48:15 PDT 2026
Author: hulxv
Date: 2026-08-08T23:48:11+03:00
New Revision: 2abc0e5685096d507e2967c3d5ad069a824f1a61
URL: https://github.com/llvm/llvm-project/commit/2abc0e5685096d507e2967c3d5ad069a824f1a61
DIFF: https://github.com/llvm/llvm-project/commit/2abc0e5685096d507e2967c3d5ad069a824f1a61.diff
LOG: [compiler-rt][builtins] add libc-backed double/float-to-int builtins (#207543)
Add libc-backed double/float to integer conversion builtins
Part of #197824
Added:
compiler-rt/lib/builtins/fixdfdi.cpp
compiler-rt/lib/builtins/fixdfsi.cpp
compiler-rt/lib/builtins/fixdfti.cpp
compiler-rt/lib/builtins/fixsfdi.cpp
compiler-rt/lib/builtins/fixsfsi.cpp
compiler-rt/lib/builtins/fixsfti.cpp
compiler-rt/lib/builtins/fixunsdfdi.cpp
compiler-rt/lib/builtins/fixunsdfsi.cpp
compiler-rt/lib/builtins/fixunsdfti.cpp
compiler-rt/lib/builtins/fixunssfdi.cpp
compiler-rt/lib/builtins/fixunssfsi.cpp
compiler-rt/lib/builtins/fixunssfti.cpp
libc/shared/builtins/fixdfdi.h
libc/shared/builtins/fixdfsi.h
libc/shared/builtins/fixdfti.h
libc/shared/builtins/fixsfdi.h
libc/shared/builtins/fixsfsi.h
libc/shared/builtins/fixsfti.h
libc/shared/builtins/fixunsdfdi.h
libc/shared/builtins/fixunsdfsi.h
libc/shared/builtins/fixunsdfti.h
libc/shared/builtins/fixunssfdi.h
libc/shared/builtins/fixunssfsi.h
libc/shared/builtins/fixunssfti.h
libc/src/__support/builtins/fixdfdi.h
libc/src/__support/builtins/fixdfsi.h
libc/src/__support/builtins/fixdfti.h
libc/src/__support/builtins/fixint_helper.h
libc/src/__support/builtins/fixsfdi.h
libc/src/__support/builtins/fixsfsi.h
libc/src/__support/builtins/fixsfti.h
libc/src/__support/builtins/fixunsdfdi.h
libc/src/__support/builtins/fixunsdfsi.h
libc/src/__support/builtins/fixunsdfti.h
libc/src/__support/builtins/fixunssfdi.h
libc/src/__support/builtins/fixunssfsi.h
libc/src/__support/builtins/fixunssfti.h
Modified:
compiler-rt/lib/builtins/CMakeLists.txt
libc/shared/builtins.h
libc/src/__support/builtins/CMakeLists.txt
libc/test/shared/CMakeLists.txt
libc/test/shared/shared_builtins_test.cpp
Removed:
################################################################################
diff --git a/compiler-rt/lib/builtins/CMakeLists.txt b/compiler-rt/lib/builtins/CMakeLists.txt
index 84a8956df18e8..1134115934df5 100644
--- a/compiler-rt/lib/builtins/CMakeLists.txt
+++ b/compiler-rt/lib/builtins/CMakeLists.txt
@@ -295,6 +295,18 @@ if(COMPILER_RT_USE_LIBC_MATH)
use_libc_builtin(GENERIC_SOURCES extendsfdf2)
use_libc_builtin(GENERIC_TF_SOURCES extendsftf2)
use_libc_builtin(x86_80_BIT_SOURCES extendxftf2)
+ use_libc_builtin(GENERIC_SOURCES fixdfsi)
+ use_libc_builtin(GENERIC_SOURCES fixdfdi)
+ use_libc_builtin(GENERIC_SOURCES fixdfti)
+ use_libc_builtin(GENERIC_SOURCES fixsfsi)
+ use_libc_builtin(GENERIC_SOURCES fixsfdi)
+ use_libc_builtin(GENERIC_SOURCES fixsfti)
+ use_libc_builtin(GENERIC_SOURCES fixunsdfsi)
+ use_libc_builtin(GENERIC_SOURCES fixunsdfdi)
+ use_libc_builtin(GENERIC_SOURCES fixunsdfti)
+ use_libc_builtin(GENERIC_SOURCES fixunssfsi)
+ use_libc_builtin(GENERIC_SOURCES fixunssfdi)
+ use_libc_builtin(GENERIC_SOURCES fixunssfti)
use_libc_builtin(GENERIC_SOURCES muldf3)
use_libc_builtin(GENERIC_SOURCES mulsf3)
use_libc_builtin(GENERIC_TF_SOURCES multf3)
diff --git a/compiler-rt/lib/builtins/fixdfdi.cpp b/compiler-rt/lib/builtins/fixdfdi.cpp
new file mode 100644
index 0000000000000..41041c81eb540
--- /dev/null
+++ b/compiler-rt/lib/builtins/fixdfdi.cpp
@@ -0,0 +1,24 @@
+//===----------------------------------------------------------------------===//
+//
+// 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 __fixdfdi, truncating double -> int64_t
+/// conversion (saturating), on top of LLVM-libc's shared::fixdfdi.
+///
+//===----------------------------------------------------------------------===//
+
+#define DOUBLE_PRECISION
+#include "fp_lib.h"
+
+#include "fp_libc_config.h"
+#include "int_lib.h"
+#include "shared/builtins/fixdfdi.h"
+
+extern "C" COMPILER_RT_ABI di_int __fixdfdi(fp_t a) {
+ return LIBC_NAMESPACE::shared::fixdfdi(a);
+}
diff --git a/compiler-rt/lib/builtins/fixdfsi.cpp b/compiler-rt/lib/builtins/fixdfsi.cpp
new file mode 100644
index 0000000000000..8f80fcc7a0d99
--- /dev/null
+++ b/compiler-rt/lib/builtins/fixdfsi.cpp
@@ -0,0 +1,24 @@
+//===----------------------------------------------------------------------===//
+//
+// 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 __fixdfsi, truncating double -> int32_t
+/// conversion (saturating), on top of LLVM-libc's shared::fixdfsi.
+///
+//===----------------------------------------------------------------------===//
+
+#define DOUBLE_PRECISION
+#include "fp_lib.h"
+
+#include "fp_libc_config.h"
+#include "int_lib.h"
+#include "shared/builtins/fixdfsi.h"
+
+extern "C" COMPILER_RT_ABI si_int __fixdfsi(fp_t a) {
+ return LIBC_NAMESPACE::shared::fixdfsi(a);
+}
diff --git a/compiler-rt/lib/builtins/fixdfti.cpp b/compiler-rt/lib/builtins/fixdfti.cpp
new file mode 100644
index 0000000000000..6529f1efd9427
--- /dev/null
+++ b/compiler-rt/lib/builtins/fixdfti.cpp
@@ -0,0 +1,28 @@
+//===----------------------------------------------------------------------===//
+//
+// 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 __fixdfti, truncating double ->
+/// __int128_t conversion (saturating), on top of LLVM-libc's shared::fixdfti.
+///
+//===----------------------------------------------------------------------===//
+
+#include "int_lib.h"
+
+#ifdef CRT_HAS_128BIT
+#define DOUBLE_PRECISION
+#include "fp_lib.h"
+
+#include "fp_libc_config.h"
+#include "shared/builtins/fixdfti.h"
+
+extern "C" COMPILER_RT_ABI ti_int __fixdfti(fp_t a) {
+ return LIBC_NAMESPACE::shared::fixdfti(a);
+}
+
+#endif // CRT_HAS_128BIT
diff --git a/compiler-rt/lib/builtins/fixsfdi.cpp b/compiler-rt/lib/builtins/fixsfdi.cpp
new file mode 100644
index 0000000000000..9fdedaf87544e
--- /dev/null
+++ b/compiler-rt/lib/builtins/fixsfdi.cpp
@@ -0,0 +1,24 @@
+//===----------------------------------------------------------------------===//
+//
+// 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 __fixsfdi, truncating float -> int64_t
+/// conversion (saturating), on top of LLVM-libc's shared::fixsfdi.
+///
+//===----------------------------------------------------------------------===//
+
+#define SINGLE_PRECISION
+#include "fp_lib.h"
+
+#include "fp_libc_config.h"
+#include "int_lib.h"
+#include "shared/builtins/fixsfdi.h"
+
+extern "C" COMPILER_RT_ABI di_int __fixsfdi(fp_t a) {
+ return LIBC_NAMESPACE::shared::fixsfdi(a);
+}
diff --git a/compiler-rt/lib/builtins/fixsfsi.cpp b/compiler-rt/lib/builtins/fixsfsi.cpp
new file mode 100644
index 0000000000000..8c73789ee42c5
--- /dev/null
+++ b/compiler-rt/lib/builtins/fixsfsi.cpp
@@ -0,0 +1,24 @@
+//===----------------------------------------------------------------------===//
+//
+// 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 __fixsfsi, truncating float -> int32_t
+/// conversion (saturating), on top of LLVM-libc's shared::fixsfsi.
+///
+//===----------------------------------------------------------------------===//
+
+#define SINGLE_PRECISION
+#include "fp_lib.h"
+
+#include "fp_libc_config.h"
+#include "int_lib.h"
+#include "shared/builtins/fixsfsi.h"
+
+extern "C" COMPILER_RT_ABI si_int __fixsfsi(fp_t a) {
+ return LIBC_NAMESPACE::shared::fixsfsi(a);
+}
diff --git a/compiler-rt/lib/builtins/fixsfti.cpp b/compiler-rt/lib/builtins/fixsfti.cpp
new file mode 100644
index 0000000000000..df5b99f905842
--- /dev/null
+++ b/compiler-rt/lib/builtins/fixsfti.cpp
@@ -0,0 +1,28 @@
+//===----------------------------------------------------------------------===//
+//
+// 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 __fixsfti, truncating float -> __int128_t
+/// conversion (saturating), on top of LLVM-libc's shared::fixsfti.
+///
+//===----------------------------------------------------------------------===//
+
+#include "int_lib.h"
+
+#ifdef CRT_HAS_128BIT
+#define SINGLE_PRECISION
+#include "fp_lib.h"
+
+#include "fp_libc_config.h"
+#include "shared/builtins/fixsfti.h"
+
+extern "C" COMPILER_RT_ABI ti_int __fixsfti(fp_t a) {
+ return LIBC_NAMESPACE::shared::fixsfti(a);
+}
+
+#endif // CRT_HAS_128BIT
diff --git a/compiler-rt/lib/builtins/fixunsdfdi.cpp b/compiler-rt/lib/builtins/fixunsdfdi.cpp
new file mode 100644
index 0000000000000..4d3488e4317a2
--- /dev/null
+++ b/compiler-rt/lib/builtins/fixunsdfdi.cpp
@@ -0,0 +1,24 @@
+//===----------------------------------------------------------------------===//
+//
+// 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 __fixunsdfdi, truncating double ->
+/// uint64_t conversion (saturating), on top of LLVM-libc's shared::fixunsdfdi.
+///
+//===----------------------------------------------------------------------===//
+
+#define DOUBLE_PRECISION
+#include "fp_lib.h"
+
+#include "fp_libc_config.h"
+#include "int_lib.h"
+#include "shared/builtins/fixunsdfdi.h"
+
+extern "C" COMPILER_RT_ABI du_int __fixunsdfdi(fp_t a) {
+ return LIBC_NAMESPACE::shared::fixunsdfdi(a);
+}
diff --git a/compiler-rt/lib/builtins/fixunsdfsi.cpp b/compiler-rt/lib/builtins/fixunsdfsi.cpp
new file mode 100644
index 0000000000000..b0ae84532156c
--- /dev/null
+++ b/compiler-rt/lib/builtins/fixunsdfsi.cpp
@@ -0,0 +1,24 @@
+//===----------------------------------------------------------------------===//
+//
+// 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 __fixunsdfsi, truncating double ->
+/// uint32_t conversion (saturating), on top of LLVM-libc's shared::fixunsdfsi.
+///
+//===----------------------------------------------------------------------===//
+
+#define DOUBLE_PRECISION
+#include "fp_lib.h"
+
+#include "fp_libc_config.h"
+#include "int_lib.h"
+#include "shared/builtins/fixunsdfsi.h"
+
+extern "C" COMPILER_RT_ABI su_int __fixunsdfsi(fp_t a) {
+ return LIBC_NAMESPACE::shared::fixunsdfsi(a);
+}
diff --git a/compiler-rt/lib/builtins/fixunsdfti.cpp b/compiler-rt/lib/builtins/fixunsdfti.cpp
new file mode 100644
index 0000000000000..374162ab886f7
--- /dev/null
+++ b/compiler-rt/lib/builtins/fixunsdfti.cpp
@@ -0,0 +1,29 @@
+//===----------------------------------------------------------------------===//
+//
+// 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 __fixunsdfti, truncating double ->
+/// __uint128_t conversion (saturating), on top of LLVM-libc's
+/// shared::fixunsdfti.
+///
+//===----------------------------------------------------------------------===//
+
+#include "int_lib.h"
+
+#ifdef CRT_HAS_128BIT
+#define DOUBLE_PRECISION
+#include "fp_lib.h"
+
+#include "fp_libc_config.h"
+#include "shared/builtins/fixunsdfti.h"
+
+extern "C" COMPILER_RT_ABI tu_int __fixunsdfti(fp_t a) {
+ return LIBC_NAMESPACE::shared::fixunsdfti(a);
+}
+
+#endif // CRT_HAS_128BIT
diff --git a/compiler-rt/lib/builtins/fixunssfdi.cpp b/compiler-rt/lib/builtins/fixunssfdi.cpp
new file mode 100644
index 0000000000000..7dbea8001e14e
--- /dev/null
+++ b/compiler-rt/lib/builtins/fixunssfdi.cpp
@@ -0,0 +1,24 @@
+//===----------------------------------------------------------------------===//
+//
+// 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 __fixunssfdi, truncating float ->
+/// uint64_t conversion (saturating), on top of LLVM-libc's shared::fixunssfdi.
+///
+//===----------------------------------------------------------------------===//
+
+#define SINGLE_PRECISION
+#include "fp_lib.h"
+
+#include "fp_libc_config.h"
+#include "int_lib.h"
+#include "shared/builtins/fixunssfdi.h"
+
+extern "C" COMPILER_RT_ABI du_int __fixunssfdi(fp_t a) {
+ return LIBC_NAMESPACE::shared::fixunssfdi(a);
+}
diff --git a/compiler-rt/lib/builtins/fixunssfsi.cpp b/compiler-rt/lib/builtins/fixunssfsi.cpp
new file mode 100644
index 0000000000000..b2c1706bdc4b7
--- /dev/null
+++ b/compiler-rt/lib/builtins/fixunssfsi.cpp
@@ -0,0 +1,24 @@
+//===----------------------------------------------------------------------===//
+//
+// 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 __fixunssfsi, truncating float ->
+/// uint32_t conversion (saturating), on top of LLVM-libc's shared::fixunssfsi.
+///
+//===----------------------------------------------------------------------===//
+
+#define SINGLE_PRECISION
+#include "fp_lib.h"
+
+#include "fp_libc_config.h"
+#include "int_lib.h"
+#include "shared/builtins/fixunssfsi.h"
+
+extern "C" COMPILER_RT_ABI su_int __fixunssfsi(fp_t a) {
+ return LIBC_NAMESPACE::shared::fixunssfsi(a);
+}
diff --git a/compiler-rt/lib/builtins/fixunssfti.cpp b/compiler-rt/lib/builtins/fixunssfti.cpp
new file mode 100644
index 0000000000000..3dab1cff9cca2
--- /dev/null
+++ b/compiler-rt/lib/builtins/fixunssfti.cpp
@@ -0,0 +1,29 @@
+//===----------------------------------------------------------------------===//
+//
+// 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 __fixunssfti, truncating float ->
+/// __uint128_t conversion (saturating), on top of LLVM-libc's
+/// shared::fixunssfti.
+///
+//===----------------------------------------------------------------------===//
+
+#include "int_lib.h"
+
+#ifdef CRT_HAS_128BIT
+#define SINGLE_PRECISION
+#include "fp_lib.h"
+
+#include "fp_libc_config.h"
+#include "shared/builtins/fixunssfti.h"
+
+extern "C" COMPILER_RT_ABI tu_int __fixunssfti(fp_t a) {
+ return LIBC_NAMESPACE::shared::fixunssfti(a);
+}
+
+#endif // CRT_HAS_128BIT
diff --git a/libc/shared/builtins.h b/libc/shared/builtins.h
index a9df1c84dac2e..e13ef6b84a6de 100644
--- a/libc/shared/builtins.h
+++ b/libc/shared/builtins.h
@@ -27,6 +27,18 @@
#include "builtins/extendsfdf2.h"
#include "builtins/extendsftf2.h"
#include "builtins/extendxftf2.h"
+#include "builtins/fixdfdi.h"
+#include "builtins/fixdfsi.h"
+#include "builtins/fixdfti.h"
+#include "builtins/fixsfdi.h"
+#include "builtins/fixsfsi.h"
+#include "builtins/fixsfti.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/muldf3.h"
#include "builtins/mulsf3.h"
#include "builtins/multf3.h"
diff --git a/libc/shared/builtins/fixdfdi.h b/libc/shared/builtins/fixdfdi.h
new file mode 100644
index 0000000000000..2a0eb4da645a1
--- /dev/null
+++ b/libc/shared/builtins/fixdfdi.h
@@ -0,0 +1,29 @@
+//===----------------------------------------------------------------------===//
+//
+// 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 __fixdfdi implementation as shared::fixdfdi
+/// so that it can be reused by compiler-rt's builtins.
+///
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SHARED_BUILTINS_FIXDFDI_H
+#define LLVM_LIBC_SHARED_BUILTINS_FIXDFDI_H
+
+#include "shared/libc_common.h"
+#include "src/__support/builtins/fixdfdi.h"
+
+namespace LIBC_NAMESPACE_DECL {
+namespace shared {
+
+using builtins::fixdfdi;
+
+} // namespace shared
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SHARED_BUILTINS_FIXDFDI_H
diff --git a/libc/shared/builtins/fixdfsi.h b/libc/shared/builtins/fixdfsi.h
new file mode 100644
index 0000000000000..ab7dae3688c5c
--- /dev/null
+++ b/libc/shared/builtins/fixdfsi.h
@@ -0,0 +1,29 @@
+//===----------------------------------------------------------------------===//
+//
+// 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 __fixdfsi implementation as shared::fixdfsi
+/// so that it can be reused by compiler-rt's builtins.
+///
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SHARED_BUILTINS_FIXDFSI_H
+#define LLVM_LIBC_SHARED_BUILTINS_FIXDFSI_H
+
+#include "shared/libc_common.h"
+#include "src/__support/builtins/fixdfsi.h"
+
+namespace LIBC_NAMESPACE_DECL {
+namespace shared {
+
+using builtins::fixdfsi;
+
+} // namespace shared
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SHARED_BUILTINS_FIXDFSI_H
diff --git a/libc/shared/builtins/fixdfti.h b/libc/shared/builtins/fixdfti.h
new file mode 100644
index 0000000000000..bd83e706fd9a7
--- /dev/null
+++ b/libc/shared/builtins/fixdfti.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 __fixdfti implementation as shared::fixdfti
+/// so that it can be reused by compiler-rt's builtins.
+///
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SHARED_BUILTINS_FIXDFTI_H
+#define LLVM_LIBC_SHARED_BUILTINS_FIXDFTI_H
+
+#include "src/__support/macros/properties/types.h"
+
+#ifdef LIBC_TYPES_HAS_INT128
+
+#include "shared/libc_common.h"
+#include "src/__support/builtins/fixdfti.h"
+
+namespace LIBC_NAMESPACE_DECL {
+namespace shared {
+
+using builtins::fixdfti;
+
+} // namespace shared
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LIBC_TYPES_HAS_INT128
+
+#endif // LLVM_LIBC_SHARED_BUILTINS_FIXDFTI_H
diff --git a/libc/shared/builtins/fixsfdi.h b/libc/shared/builtins/fixsfdi.h
new file mode 100644
index 0000000000000..ff969a6551a44
--- /dev/null
+++ b/libc/shared/builtins/fixsfdi.h
@@ -0,0 +1,29 @@
+//===----------------------------------------------------------------------===//
+//
+// 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 __fixsfdi implementation as shared::fixsfdi
+/// so that it can be reused by compiler-rt's builtins.
+///
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SHARED_BUILTINS_FIXSFDI_H
+#define LLVM_LIBC_SHARED_BUILTINS_FIXSFDI_H
+
+#include "shared/libc_common.h"
+#include "src/__support/builtins/fixsfdi.h"
+
+namespace LIBC_NAMESPACE_DECL {
+namespace shared {
+
+using builtins::fixsfdi;
+
+} // namespace shared
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SHARED_BUILTINS_FIXSFDI_H
diff --git a/libc/shared/builtins/fixsfsi.h b/libc/shared/builtins/fixsfsi.h
new file mode 100644
index 0000000000000..debd82aefc8eb
--- /dev/null
+++ b/libc/shared/builtins/fixsfsi.h
@@ -0,0 +1,29 @@
+//===----------------------------------------------------------------------===//
+//
+// 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 __fixsfsi implementation as shared::fixsfsi
+/// so that it can be reused by compiler-rt's builtins.
+///
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SHARED_BUILTINS_FIXSFSI_H
+#define LLVM_LIBC_SHARED_BUILTINS_FIXSFSI_H
+
+#include "shared/libc_common.h"
+#include "src/__support/builtins/fixsfsi.h"
+
+namespace LIBC_NAMESPACE_DECL {
+namespace shared {
+
+using builtins::fixsfsi;
+
+} // namespace shared
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SHARED_BUILTINS_FIXSFSI_H
diff --git a/libc/shared/builtins/fixsfti.h b/libc/shared/builtins/fixsfti.h
new file mode 100644
index 0000000000000..191fb5292b92b
--- /dev/null
+++ b/libc/shared/builtins/fixsfti.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 __fixsfti implementation as shared::fixsfti
+/// so that it can be reused by compiler-rt's builtins.
+///
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SHARED_BUILTINS_FIXSFTI_H
+#define LLVM_LIBC_SHARED_BUILTINS_FIXSFTI_H
+
+#include "src/__support/macros/properties/types.h"
+
+#ifdef LIBC_TYPES_HAS_INT128
+
+#include "shared/libc_common.h"
+#include "src/__support/builtins/fixsfti.h"
+
+namespace LIBC_NAMESPACE_DECL {
+namespace shared {
+
+using builtins::fixsfti;
+
+} // namespace shared
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LIBC_TYPES_HAS_INT128
+
+#endif // LLVM_LIBC_SHARED_BUILTINS_FIXSFTI_H
diff --git a/libc/shared/builtins/fixunsdfdi.h b/libc/shared/builtins/fixunsdfdi.h
new file mode 100644
index 0000000000000..9c2472e771ac4
--- /dev/null
+++ b/libc/shared/builtins/fixunsdfdi.h
@@ -0,0 +1,29 @@
+//===----------------------------------------------------------------------===//
+//
+// 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 __fixunsdfdi implementation as
+/// shared::fixunsdfdi so that it can be reused by compiler-rt's builtins.
+///
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SHARED_BUILTINS_FIXUNSDFDI_H
+#define LLVM_LIBC_SHARED_BUILTINS_FIXUNSDFDI_H
+
+#include "shared/libc_common.h"
+#include "src/__support/builtins/fixunsdfdi.h"
+
+namespace LIBC_NAMESPACE_DECL {
+namespace shared {
+
+using builtins::fixunsdfdi;
+
+} // namespace shared
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SHARED_BUILTINS_FIXUNSDFDI_H
diff --git a/libc/shared/builtins/fixunsdfsi.h b/libc/shared/builtins/fixunsdfsi.h
new file mode 100644
index 0000000000000..c463298e576fd
--- /dev/null
+++ b/libc/shared/builtins/fixunsdfsi.h
@@ -0,0 +1,29 @@
+//===----------------------------------------------------------------------===//
+//
+// 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 __fixunsdfsi implementation as
+/// shared::fixunsdfsi so that it can be reused by compiler-rt's builtins.
+///
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SHARED_BUILTINS_FIXUNSDFSI_H
+#define LLVM_LIBC_SHARED_BUILTINS_FIXUNSDFSI_H
+
+#include "shared/libc_common.h"
+#include "src/__support/builtins/fixunsdfsi.h"
+
+namespace LIBC_NAMESPACE_DECL {
+namespace shared {
+
+using builtins::fixunsdfsi;
+
+} // namespace shared
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SHARED_BUILTINS_FIXUNSDFSI_H
diff --git a/libc/shared/builtins/fixunsdfti.h b/libc/shared/builtins/fixunsdfti.h
new file mode 100644
index 0000000000000..5929f93781446
--- /dev/null
+++ b/libc/shared/builtins/fixunsdfti.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 __fixunsdfti implementation as
+/// shared::fixunsdfti so that it can be reused by compiler-rt's builtins.
+///
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SHARED_BUILTINS_FIXUNSDFTI_H
+#define LLVM_LIBC_SHARED_BUILTINS_FIXUNSDFTI_H
+
+#include "src/__support/macros/properties/types.h"
+
+#ifdef LIBC_TYPES_HAS_INT128
+
+#include "shared/libc_common.h"
+#include "src/__support/builtins/fixunsdfti.h"
+
+namespace LIBC_NAMESPACE_DECL {
+namespace shared {
+
+using builtins::fixunsdfti;
+
+} // namespace shared
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LIBC_TYPES_HAS_INT128
+
+#endif // LLVM_LIBC_SHARED_BUILTINS_FIXUNSDFTI_H
diff --git a/libc/shared/builtins/fixunssfdi.h b/libc/shared/builtins/fixunssfdi.h
new file mode 100644
index 0000000000000..7a1b16cb6bcf9
--- /dev/null
+++ b/libc/shared/builtins/fixunssfdi.h
@@ -0,0 +1,29 @@
+//===----------------------------------------------------------------------===//
+//
+// 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 __fixunssfdi implementation as
+/// shared::fixunssfdi so that it can be reused by compiler-rt's builtins.
+///
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SHARED_BUILTINS_FIXUNSSFDI_H
+#define LLVM_LIBC_SHARED_BUILTINS_FIXUNSSFDI_H
+
+#include "shared/libc_common.h"
+#include "src/__support/builtins/fixunssfdi.h"
+
+namespace LIBC_NAMESPACE_DECL {
+namespace shared {
+
+using builtins::fixunssfdi;
+
+} // namespace shared
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SHARED_BUILTINS_FIXUNSSFDI_H
diff --git a/libc/shared/builtins/fixunssfsi.h b/libc/shared/builtins/fixunssfsi.h
new file mode 100644
index 0000000000000..8ed7ed6103d51
--- /dev/null
+++ b/libc/shared/builtins/fixunssfsi.h
@@ -0,0 +1,29 @@
+//===----------------------------------------------------------------------===//
+//
+// 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 __fixunssfsi implementation as
+/// shared::fixunssfsi so that it can be reused by compiler-rt's builtins.
+///
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SHARED_BUILTINS_FIXUNSSFSI_H
+#define LLVM_LIBC_SHARED_BUILTINS_FIXUNSSFSI_H
+
+#include "shared/libc_common.h"
+#include "src/__support/builtins/fixunssfsi.h"
+
+namespace LIBC_NAMESPACE_DECL {
+namespace shared {
+
+using builtins::fixunssfsi;
+
+} // namespace shared
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SHARED_BUILTINS_FIXUNSSFSI_H
diff --git a/libc/shared/builtins/fixunssfti.h b/libc/shared/builtins/fixunssfti.h
new file mode 100644
index 0000000000000..b47dd08ab04f6
--- /dev/null
+++ b/libc/shared/builtins/fixunssfti.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 __fixunssfti implementation as
+/// shared::fixunssfti so that it can be reused by compiler-rt's builtins.
+///
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SHARED_BUILTINS_FIXUNSSFTI_H
+#define LLVM_LIBC_SHARED_BUILTINS_FIXUNSSFTI_H
+
+#include "src/__support/macros/properties/types.h"
+
+#ifdef LIBC_TYPES_HAS_INT128
+
+#include "shared/libc_common.h"
+#include "src/__support/builtins/fixunssfti.h"
+
+namespace LIBC_NAMESPACE_DECL {
+namespace shared {
+
+using builtins::fixunssfti;
+
+} // namespace shared
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LIBC_TYPES_HAS_INT128
+
+#endif // LLVM_LIBC_SHARED_BUILTINS_FIXUNSSFTI_H
diff --git a/libc/src/__support/builtins/CMakeLists.txt b/libc/src/__support/builtins/CMakeLists.txt
index b9ac372e97505..51212c4bff34c 100644
--- a/libc/src/__support/builtins/CMakeLists.txt
+++ b/libc/src/__support/builtins/CMakeLists.txt
@@ -123,7 +123,6 @@ add_header_library(
subsf3.h
DEPENDS
libc.src.__support.FPUtil.generic.add_sub
- libc.src.__support.macros.config
)
add_header_library(
@@ -223,3 +222,131 @@ add_header_library(
libc.src.__support.builtins.fpconvert_helper
libc.src.__support.macros.config
)
+
+add_header_library(
+ fixdfdi
+ HDRS
+ fixdfdi.h
+ DEPENDS
+ libc.hdr.stdint_proxy
+ libc.src.__support.builtins.fixint_helper
+ libc.src.__support.macros.config
+)
+
+add_header_library(
+ fixdfti
+ HDRS
+ fixdfti.h
+ DEPENDS
+ libc.hdr.stdint_proxy
+ libc.src.__support.builtins.fixint_helper
+ libc.src.__support.macros.config
+ libc.src.__support.macros.properties.types
+ libc.src.__support.uint128
+)
+
+add_header_library(
+ fixdfsi
+ HDRS
+ fixdfsi.h
+ DEPENDS
+ libc.hdr.stdint_proxy
+ libc.src.__support.builtins.fixint_helper
+ libc.src.__support.macros.config
+)
+
+add_header_library(
+ fixsfsi
+ HDRS
+ fixsfsi.h
+ DEPENDS
+ libc.hdr.stdint_proxy
+ libc.src.__support.builtins.fixint_helper
+ libc.src.__support.macros.config
+)
+
+add_header_library(
+ fixsfdi
+ HDRS
+ fixsfdi.h
+ DEPENDS
+ libc.hdr.stdint_proxy
+ libc.src.__support.builtins.fixint_helper
+ libc.src.__support.macros.config
+)
+
+add_header_library(
+ fixsfti
+ HDRS
+ fixsfti.h
+ DEPENDS
+ libc.hdr.stdint_proxy
+ libc.src.__support.builtins.fixint_helper
+ libc.src.__support.macros.config
+ libc.src.__support.macros.properties.types
+ libc.src.__support.uint128
+)
+
+add_header_library(
+ fixunsdfsi
+ HDRS
+ fixunsdfsi.h
+ DEPENDS
+ libc.hdr.stdint_proxy
+ libc.src.__support.builtins.fixint_helper
+ libc.src.__support.macros.config
+)
+
+add_header_library(
+ fixunsdfdi
+ HDRS
+ fixunsdfdi.h
+ DEPENDS
+ libc.hdr.stdint_proxy
+ libc.src.__support.builtins.fixint_helper
+ libc.src.__support.macros.config
+)
+
+add_header_library(
+ fixunsdfti
+ HDRS
+ fixunsdfti.h
+ DEPENDS
+ libc.hdr.stdint_proxy
+ libc.src.__support.builtins.fixint_helper
+ libc.src.__support.macros.config
+ libc.src.__support.macros.properties.types
+ libc.src.__support.uint128
+)
+
+add_header_library(
+ fixunssfsi
+ HDRS
+ fixunssfsi.h
+ DEPENDS
+ libc.hdr.stdint_proxy
+ libc.src.__support.builtins.fixint_helper
+ libc.src.__support.macros.config
+)
+
+add_header_library(
+ fixunssfdi
+ HDRS
+ fixunssfdi.h
+ DEPENDS
+ libc.hdr.stdint_proxy
+ libc.src.__support.builtins.fixint_helper
+ libc.src.__support.macros.config
+)
+
+add_header_library(
+ fixunssfti
+ HDRS
+ fixunssfti.h
+ DEPENDS
+ libc.hdr.stdint_proxy
+ libc.src.__support.builtins.fixint_helper
+ libc.src.__support.macros.config
+ libc.src.__support.macros.properties.types
+ libc.src.__support.uint128
+)
diff --git a/libc/src/__support/builtins/fixdfdi.h b/libc/src/__support/builtins/fixdfdi.h
new file mode 100644
index 0000000000000..9ca5afa20c522
--- /dev/null
+++ b/libc/src/__support/builtins/fixdfdi.h
@@ -0,0 +1,32 @@
+//===----------------------------------------------------------------------===//
+//
+// 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 __fixdfdi implementation as
+/// builtins::fixdfdi so that it can be reused by compiler-rt's builtins.
+///
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SRC___SUPPORT_BUILTINS_FIXDFDI_H
+#define LLVM_LIBC_SRC___SUPPORT_BUILTINS_FIXDFDI_H
+
+#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 double -> int64_t conversion, saturating on overflow.
+// Mirrors compiler-rt's __fixdfdi.
+LIBC_INLINE int64_t fixdfdi(double x) { return fixint<int64_t>(x); }
+
+} // namespace builtins
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC___SUPPORT_BUILTINS_FIXDFDI_H
diff --git a/libc/src/__support/builtins/fixdfsi.h b/libc/src/__support/builtins/fixdfsi.h
new file mode 100644
index 0000000000000..ef38849251fee
--- /dev/null
+++ b/libc/src/__support/builtins/fixdfsi.h
@@ -0,0 +1,32 @@
+//===----------------------------------------------------------------------===//
+//
+// 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 __fixdfsi implementation as
+/// builtins::fixdfsi so that it can be reused by compiler-rt's builtins.
+///
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SRC___SUPPORT_BUILTINS_FIXDFSI_H
+#define LLVM_LIBC_SRC___SUPPORT_BUILTINS_FIXDFSI_H
+
+#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 double -> int32_t conversion, saturating on overflow.
+// Mirrors compiler-rt's __fixdfsi.
+LIBC_INLINE int32_t fixdfsi(double x) { return fixint<int32_t>(x); }
+
+} // namespace builtins
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC___SUPPORT_BUILTINS_FIXDFSI_H
diff --git a/libc/src/__support/builtins/fixdfti.h b/libc/src/__support/builtins/fixdfti.h
new file mode 100644
index 0000000000000..bf2d2961ec115
--- /dev/null
+++ b/libc/src/__support/builtins/fixdfti.h
@@ -0,0 +1,39 @@
+//===----------------------------------------------------------------------===//
+//
+// 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 __fixdfti implementation as
+/// builtins::fixdfti so that it can be reused by compiler-rt's builtins.
+///
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SRC___SUPPORT_BUILTINS_FIXDFTI_H
+#define LLVM_LIBC_SRC___SUPPORT_BUILTINS_FIXDFTI_H
+
+#include "src/__support/macros/properties/types.h"
+
+#ifdef LIBC_TYPES_HAS_INT128
+
+#include "hdr/stdint_proxy.h"
+#include "src/__support/builtins/fixint_helper.h"
+#include "src/__support/macros/config.h"
+#include "src/__support/uint128.h"
+
+namespace LIBC_NAMESPACE_DECL {
+namespace builtins {
+
+// Truncating double -> Int128 conversion, saturating on overflow.
+// Mirrors compiler-rt's __fixdfti.
+LIBC_INLINE Int128 fixdfti(double x) { return fixint<Int128>(x); }
+
+} // namespace builtins
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LIBC_TYPES_HAS_INT128
+
+#endif // LLVM_LIBC_SRC___SUPPORT_BUILTINS_FIXDFTI_H
diff --git a/libc/src/__support/builtins/fixint_helper.h b/libc/src/__support/builtins/fixint_helper.h
new file mode 100644
index 0000000000000..ab2125ff24326
--- /dev/null
+++ b/libc/src/__support/builtins/fixint_helper.h
@@ -0,0 +1,84 @@
+//===----------------------------------------------------------------------===//
+//
+// 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
+/// Shared truncating float-to-integer conversions, saturating on overflow.
+/// These mirror compiler-rt's __fix<f><i> / __fixuns<f><i> builtins via an
+/// FPBits unpack + shift, so they can be reused by compiler-rt's builtins.
+///
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SRC___SUPPORT_BUILTINS_FIXINT_HELPER_H
+#define LLVM_LIBC_SRC___SUPPORT_BUILTINS_FIXINT_HELPER_H
+
+#include "src/__support/CPP/limits.h"
+#include "src/__support/CPP/type_traits.h"
+#include "src/__support/FPUtil/FPBits.h"
+#include "src/__support/macros/attributes.h"
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+namespace builtins {
+
+// TODO: use fputil::round_to_signed_integer after adding Float128/64/32/16
+// classes currently, we use these helpers to avoid an infinite loop that
+// happens because fputil::round_to_signed_integer calls the builtins if
+// the target doesn't have FPU.
+
+// Truncating conversion of F to the signed integer I (round toward zero).
+// Out-of-range magnitudes saturate to I's min/max; mirrors compiler-rt __fix*.
+template <typename I, typename F> LIBC_INLINE constexpr I fixint(F a) {
+ using FPBits = fputil::FPBits<F>;
+ using UI = cpp::make_unsigned_t<I>;
+ constexpr I FIXINT_MAX = cpp::numeric_limits<I>::max();
+ constexpr I FIXINT_MIN = cpp::numeric_limits<I>::min();
+
+ const FPBits bits(a);
+ const I sign = bits.is_neg() ? -1 : 1;
+ const int exponent = bits.get_exponent();
+ const typename FPBits::StorageType significand = bits.get_explicit_mantissa();
+
+ if (exponent < 0)
+ return 0;
+
+ if (static_cast<unsigned>(exponent) >= sizeof(I) * 8)
+ return sign == 1 ? FIXINT_MAX : FIXINT_MIN;
+
+ if (exponent < FPBits::FRACTION_LEN)
+ return sign *
+ static_cast<I>(significand >> (FPBits::FRACTION_LEN - exponent));
+ return sign * static_cast<I>(static_cast<UI>(significand)
+ << (exponent - FPBits::FRACTION_LEN));
+}
+
+// Truncating conversion of F to the unsigned integer U (round toward zero).
+// Negative or out-of-range values yield 0 / U's max; mirrors compiler-rt
+// __fixuns*.
+template <typename U, typename F> LIBC_INLINE constexpr U fixuint(F a) {
+ using FPBits = fputil::FPBits<F>;
+
+ const FPBits bits(a);
+ const int exponent = bits.get_exponent();
+ const typename FPBits::StorageType significand = bits.get_explicit_mantissa();
+
+ if (bits.is_neg() || exponent < 0)
+ return 0;
+
+ if (static_cast<unsigned>(exponent) >= sizeof(U) * 8)
+ return ~U(0);
+
+ if (exponent < FPBits::FRACTION_LEN)
+ return static_cast<U>(significand >> (FPBits::FRACTION_LEN - exponent));
+ return static_cast<U>(static_cast<U>(significand)
+ << (exponent - FPBits::FRACTION_LEN));
+}
+
+} // namespace builtins
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC___SUPPORT_BUILTINS_FIXINT_HELPER_H
diff --git a/libc/src/__support/builtins/fixsfdi.h b/libc/src/__support/builtins/fixsfdi.h
new file mode 100644
index 0000000000000..87b10f3d8c113
--- /dev/null
+++ b/libc/src/__support/builtins/fixsfdi.h
@@ -0,0 +1,32 @@
+//===----------------------------------------------------------------------===//
+//
+// 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 __fixsfdi implementation as
+/// builtins::fixsfdi so that it can be reused by compiler-rt's builtins.
+///
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SRC___SUPPORT_BUILTINS_FIXSFDI_H
+#define LLVM_LIBC_SRC___SUPPORT_BUILTINS_FIXSFDI_H
+
+#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 float -> int64_t conversion, saturating on overflow.
+// Mirrors compiler-rt's __fixsfdi.
+LIBC_INLINE int64_t fixsfdi(float x) { return fixint<int64_t>(x); }
+
+} // namespace builtins
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC___SUPPORT_BUILTINS_FIXSFDI_H
diff --git a/libc/src/__support/builtins/fixsfsi.h b/libc/src/__support/builtins/fixsfsi.h
new file mode 100644
index 0000000000000..1eded97192e4c
--- /dev/null
+++ b/libc/src/__support/builtins/fixsfsi.h
@@ -0,0 +1,32 @@
+//===----------------------------------------------------------------------===//
+//
+// 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 __fixsfsi implementation as
+/// builtins::fixsfsi so that it can be reused by compiler-rt's builtins.
+///
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SRC___SUPPORT_BUILTINS_FIXSFSI_H
+#define LLVM_LIBC_SRC___SUPPORT_BUILTINS_FIXSFSI_H
+
+#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 float -> int32_t conversion, saturating on overflow.
+// Mirrors compiler-rt's __fixsfsi.
+LIBC_INLINE int32_t fixsfsi(float x) { return fixint<int32_t>(x); }
+
+} // namespace builtins
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC___SUPPORT_BUILTINS_FIXSFSI_H
diff --git a/libc/src/__support/builtins/fixsfti.h b/libc/src/__support/builtins/fixsfti.h
new file mode 100644
index 0000000000000..1c39301fbdf91
--- /dev/null
+++ b/libc/src/__support/builtins/fixsfti.h
@@ -0,0 +1,39 @@
+//===----------------------------------------------------------------------===//
+//
+// 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 __fixsfti implementation as
+/// builtins::fixsfti so that it can be reused by compiler-rt's builtins.
+///
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SRC___SUPPORT_BUILTINS_FIXSFTI_H
+#define LLVM_LIBC_SRC___SUPPORT_BUILTINS_FIXSFTI_H
+
+#include "src/__support/macros/properties/types.h"
+
+#ifdef LIBC_TYPES_HAS_INT128
+
+#include "hdr/stdint_proxy.h"
+#include "src/__support/builtins/fixint_helper.h"
+#include "src/__support/macros/config.h"
+#include "src/__support/uint128.h"
+
+namespace LIBC_NAMESPACE_DECL {
+namespace builtins {
+
+// Truncating float -> Int128 conversion, saturating on overflow.
+// Mirrors compiler-rt's __fixsfti.
+LIBC_INLINE Int128 fixsfti(float x) { return fixint<Int128>(x); }
+
+} // namespace builtins
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LIBC_TYPES_HAS_INT128
+
+#endif // LLVM_LIBC_SRC___SUPPORT_BUILTINS_FIXSFTI_H
diff --git a/libc/src/__support/builtins/fixunsdfdi.h b/libc/src/__support/builtins/fixunsdfdi.h
new file mode 100644
index 0000000000000..c9531ceac4a63
--- /dev/null
+++ b/libc/src/__support/builtins/fixunsdfdi.h
@@ -0,0 +1,32 @@
+//===----------------------------------------------------------------------===//
+//
+// 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 __fixunsdfdi implementation as
+/// builtins::fixunsdfdi so that it can be reused by compiler-rt's builtins.
+///
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SRC___SUPPORT_BUILTINS_FIXUNSDFDI_H
+#define LLVM_LIBC_SRC___SUPPORT_BUILTINS_FIXUNSDFDI_H
+
+#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 double -> uint64_t conversion, saturating on overflow.
+// Mirrors compiler-rt's __fixunsdfdi.
+LIBC_INLINE uint64_t fixunsdfdi(double x) { return fixuint<uint64_t>(x); }
+
+} // namespace builtins
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC___SUPPORT_BUILTINS_FIXUNSDFDI_H
diff --git a/libc/src/__support/builtins/fixunsdfsi.h b/libc/src/__support/builtins/fixunsdfsi.h
new file mode 100644
index 0000000000000..4a6117c40226a
--- /dev/null
+++ b/libc/src/__support/builtins/fixunsdfsi.h
@@ -0,0 +1,32 @@
+//===----------------------------------------------------------------------===//
+//
+// 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 __fixunsdfsi implementation as
+/// builtins::fixunsdfsi so that it can be reused by compiler-rt's builtins.
+///
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SRC___SUPPORT_BUILTINS_FIXUNSDFSI_H
+#define LLVM_LIBC_SRC___SUPPORT_BUILTINS_FIXUNSDFSI_H
+
+#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 double -> uint32_t conversion, saturating on overflow.
+// Mirrors compiler-rt's __fixunsdfsi.
+LIBC_INLINE uint32_t fixunsdfsi(double x) { return fixuint<uint32_t>(x); }
+
+} // namespace builtins
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC___SUPPORT_BUILTINS_FIXUNSDFSI_H
diff --git a/libc/src/__support/builtins/fixunsdfti.h b/libc/src/__support/builtins/fixunsdfti.h
new file mode 100644
index 0000000000000..596e9c7bc5149
--- /dev/null
+++ b/libc/src/__support/builtins/fixunsdfti.h
@@ -0,0 +1,39 @@
+//===----------------------------------------------------------------------===//
+//
+// 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 __fixunsdfti implementation as
+/// builtins::fixunsdfti so that it can be reused by compiler-rt's builtins.
+///
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SRC___SUPPORT_BUILTINS_FIXUNSDFTI_H
+#define LLVM_LIBC_SRC___SUPPORT_BUILTINS_FIXUNSDFTI_H
+
+#include "src/__support/macros/properties/types.h"
+
+#ifdef LIBC_TYPES_HAS_INT128
+
+#include "hdr/stdint_proxy.h"
+#include "src/__support/builtins/fixint_helper.h"
+#include "src/__support/macros/config.h"
+#include "src/__support/uint128.h"
+
+namespace LIBC_NAMESPACE_DECL {
+namespace builtins {
+
+// Truncating double -> UInt128 conversion, saturating on overflow.
+// Mirrors compiler-rt's __fixunsdfti.
+LIBC_INLINE UInt128 fixunsdfti(double x) { return fixuint<UInt128>(x); }
+
+} // namespace builtins
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LIBC_TYPES_HAS_INT128
+
+#endif // LLVM_LIBC_SRC___SUPPORT_BUILTINS_FIXUNSDFTI_H
diff --git a/libc/src/__support/builtins/fixunssfdi.h b/libc/src/__support/builtins/fixunssfdi.h
new file mode 100644
index 0000000000000..68befe8331d53
--- /dev/null
+++ b/libc/src/__support/builtins/fixunssfdi.h
@@ -0,0 +1,32 @@
+//===----------------------------------------------------------------------===//
+//
+// 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 __fixunssfdi implementation as
+/// builtins::fixunssfdi so that it can be reused by compiler-rt's builtins.
+///
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SRC___SUPPORT_BUILTINS_FIXUNSSFDI_H
+#define LLVM_LIBC_SRC___SUPPORT_BUILTINS_FIXUNSSFDI_H
+
+#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 float -> uint64_t conversion, saturating on overflow.
+// Mirrors compiler-rt's __fixunssfdi.
+LIBC_INLINE uint64_t fixunssfdi(float x) { return fixuint<uint64_t>(x); }
+
+} // namespace builtins
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC___SUPPORT_BUILTINS_FIXUNSSFDI_H
diff --git a/libc/src/__support/builtins/fixunssfsi.h b/libc/src/__support/builtins/fixunssfsi.h
new file mode 100644
index 0000000000000..fe0dbe62d49d8
--- /dev/null
+++ b/libc/src/__support/builtins/fixunssfsi.h
@@ -0,0 +1,32 @@
+//===----------------------------------------------------------------------===//
+//
+// 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 __fixunssfsi implementation as
+/// builtins::fixunssfsi so that it can be reused by compiler-rt's builtins.
+///
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SRC___SUPPORT_BUILTINS_FIXUNSSFSI_H
+#define LLVM_LIBC_SRC___SUPPORT_BUILTINS_FIXUNSSFSI_H
+
+#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 float -> uint32_t conversion, saturating on overflow.
+// Mirrors compiler-rt's __fixunssfsi.
+LIBC_INLINE uint32_t fixunssfsi(float x) { return fixuint<uint32_t>(x); }
+
+} // namespace builtins
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC___SUPPORT_BUILTINS_FIXUNSSFSI_H
diff --git a/libc/src/__support/builtins/fixunssfti.h b/libc/src/__support/builtins/fixunssfti.h
new file mode 100644
index 0000000000000..567d5de8159ed
--- /dev/null
+++ b/libc/src/__support/builtins/fixunssfti.h
@@ -0,0 +1,39 @@
+//===----------------------------------------------------------------------===//
+//
+// 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 __fixunssfti implementation as
+/// builtins::fixunssfti so that it can be reused by compiler-rt's builtins.
+///
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SRC___SUPPORT_BUILTINS_FIXUNSSFTI_H
+#define LLVM_LIBC_SRC___SUPPORT_BUILTINS_FIXUNSSFTI_H
+
+#include "src/__support/macros/properties/types.h"
+
+#ifdef LIBC_TYPES_HAS_INT128
+
+#include "hdr/stdint_proxy.h"
+#include "src/__support/builtins/fixint_helper.h"
+#include "src/__support/macros/config.h"
+#include "src/__support/uint128.h"
+
+namespace LIBC_NAMESPACE_DECL {
+namespace builtins {
+
+// Truncating float -> UInt128 conversion, saturating on overflow.
+// Mirrors compiler-rt's __fixunssfti.
+LIBC_INLINE UInt128 fixunssfti(float x) { return fixuint<UInt128>(x); }
+
+} // namespace builtins
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LIBC_TYPES_HAS_INT128
+
+#endif // LLVM_LIBC_SRC___SUPPORT_BUILTINS_FIXUNSSFTI_H
diff --git a/libc/test/shared/CMakeLists.txt b/libc/test/shared/CMakeLists.txt
index 7e67e71862763..04b6a6e3aed91 100644
--- a/libc/test/shared/CMakeLists.txt
+++ b/libc/test/shared/CMakeLists.txt
@@ -838,6 +838,18 @@ add_fp_unittest(
libc.src.__support.builtins.extendsfdf2
libc.src.__support.builtins.extendsftf2
libc.src.__support.builtins.extendxftf2
+ libc.src.__support.builtins.fixdfdi
+ libc.src.__support.builtins.fixdfsi
+ libc.src.__support.builtins.fixdfti
+ libc.src.__support.builtins.fixsfdi
+ libc.src.__support.builtins.fixsfsi
+ libc.src.__support.builtins.fixsfti
+ 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.muldf3
libc.src.__support.builtins.mulsf3
libc.src.__support.builtins.multf3
@@ -850,6 +862,7 @@ add_fp_unittest(
libc.src.__support.builtins.trunctfdf2
libc.src.__support.builtins.trunctfsf2
libc.src.__support.builtins.trunctfxf2
+ libc.src.__support.uint128
)
add_fp_unittest(
diff --git a/libc/test/shared/shared_builtins_test.cpp b/libc/test/shared/shared_builtins_test.cpp
index 629a34d0af412..eb1c566d6b179 100644
--- a/libc/test/shared/shared_builtins_test.cpp
+++ b/libc/test/shared/shared_builtins_test.cpp
@@ -7,6 +7,7 @@
//===----------------------------------------------------------------------===//
#include "shared/builtins.h"
+#include "src/__support/uint128.h"
#include "test/UnitTest/FPMatcher.h"
#include "test/UnitTest/Test.h"
More information about the libc-commits
mailing list