[compiler-rt] [libc] [llvm] [compiler-rt][builtins] add libc-backed arithmetic builtins (PR #207092)

via llvm-commits llvm-commits at lists.llvm.org
Sat Jul 4 13:07:40 PDT 2026


https://github.com/hulxv updated https://github.com/llvm/llvm-project/pull/207092

>From ca67d72a37d66542a71176860e554264f337d037 Mon Sep 17 00:00:00 2001
From: hulxv <hulxxv at gmail.com>
Date: Thu, 28 May 2026 02:12:37 +0300
Subject: [PATCH 1/7] [compiler-rt][builtins] introduce libc math routines

---
 .../compiler-rt-libc-builtins-tests.yml       | 58 +++++++++++++++++++
 1 file changed, 58 insertions(+)
 create mode 100644 .github/workflows/compiler-rt-libc-builtins-tests.yml

diff --git a/.github/workflows/compiler-rt-libc-builtins-tests.yml b/.github/workflows/compiler-rt-libc-builtins-tests.yml
new file mode 100644
index 0000000000000..3dab28c0387a7
--- /dev/null
+++ b/.github/workflows/compiler-rt-libc-builtins-tests.yml
@@ -0,0 +1,58 @@
+# Pre-commit CI for the compiler-rt + LLVM-libc builtins integration.
+name: compiler-rt + libc Builtins Tests
+permissions:
+  contents: read
+on:
+  pull_request:
+    branches: ["main"]
+    paths:
+      - "compiler-rt/lib/builtins/**"
+      - "compiler-rt/test/builtins/**"
+      - "libc/shared/builtins.h"
+      - "libc/shared/builtins/**"
+      - "libc/src/__support/builtins/**"
+      - "libc/src/__support/FPUtil/**"
+      - ".github/workflows/compiler-rt-libc-builtins-tests.yml"
+
+jobs:
+  build:
+    name: builtins (${{ matrix.os }})
+    if: github.repository_owner == 'llvm'
+    runs-on: ${{ matrix.os }}
+    container:
+      image: ${{ (startsWith(matrix.os, 'ubuntu-24.04-arm') && 'ghcr.io/llvm/arm64v8/libc-ubuntu-24.04') || 'ghcr.io/llvm/libc-ubuntu-24.04' }}
+      options: >-
+        --privileged
+    strategy:
+      fail-fast: false
+      matrix:
+        os: [ubuntu-24.04, ubuntu-24.04-arm]
+
+    steps:
+      - uses: actions/checkout at de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
+        with:
+          persist-credentials: false
+
+      - name: Configure CMake
+        shell: bash
+        run: |
+          cmake \
+            -B build \
+            -S llvm \
+            -G Ninja \
+            -DCMAKE_C_COMPILER=clang-23 \
+            -DCMAKE_CXX_COMPILER=clang++-23 \
+            -DCMAKE_BUILD_TYPE=Release \
+            -DLLVM_ENABLE_ASSERTIONS=ON \
+            -DLLVM_ENABLE_PROJECTS=clang \
+            -DLLVM_ENABLE_RUNTIMES="compiler-rt" \
+            -DCOMPILER_RT_USE_LIBC_MATH=ON \
+            -DCOMPILER_RT_INCLUDE_TESTS=ON
+
+      - name: Test compiler-rt builtins
+        shell: bash
+        run: |
+          cmake \
+            --build build \
+            --parallel \
+            --target check-builtins

>From f5caf778579295d9ff0a005ebe26f6b223c19a51 Mon Sep 17 00:00:00 2001
From: hulxv <hulxxv at gmail.com>
Date: Mon, 1 Jun 2026 00:50:36 +0300
Subject: [PATCH 2/7] allow all branches

---
 .github/workflows/compiler-rt-libc-builtins-tests.yml | 1 -
 1 file changed, 1 deletion(-)

diff --git a/.github/workflows/compiler-rt-libc-builtins-tests.yml b/.github/workflows/compiler-rt-libc-builtins-tests.yml
index 3dab28c0387a7..ba9c9ecf38bdb 100644
--- a/.github/workflows/compiler-rt-libc-builtins-tests.yml
+++ b/.github/workflows/compiler-rt-libc-builtins-tests.yml
@@ -4,7 +4,6 @@ permissions:
   contents: read
 on:
   pull_request:
-    branches: ["main"]
     paths:
       - "compiler-rt/lib/builtins/**"
       - "compiler-rt/test/builtins/**"

>From d308cc6de896644402c9b26e1ffc3fff1d35d231 Mon Sep 17 00:00:00 2001
From: hulxv <hulxxv at gmail.com>
Date: Mon, 1 Jun 2026 00:52:23 +0300
Subject: [PATCH 3/7] remove `--privileged`` option

---
 .github/workflows/compiler-rt-libc-builtins-tests.yml | 2 --
 1 file changed, 2 deletions(-)

diff --git a/.github/workflows/compiler-rt-libc-builtins-tests.yml b/.github/workflows/compiler-rt-libc-builtins-tests.yml
index ba9c9ecf38bdb..66e511b45d95a 100644
--- a/.github/workflows/compiler-rt-libc-builtins-tests.yml
+++ b/.github/workflows/compiler-rt-libc-builtins-tests.yml
@@ -20,8 +20,6 @@ jobs:
     runs-on: ${{ matrix.os }}
     container:
       image: ${{ (startsWith(matrix.os, 'ubuntu-24.04-arm') && 'ghcr.io/llvm/arm64v8/libc-ubuntu-24.04') || 'ghcr.io/llvm/libc-ubuntu-24.04' }}
-      options: >-
-        --privileged
     strategy:
       fail-fast: false
       matrix:

>From 82df048492035ee3d8bd7f05a98ddf0c021a2d2a Mon Sep 17 00:00:00 2001
From: hulxv <hulxxv at gmail.com>
Date: Mon, 1 Jun 2026 00:53:52 +0300
Subject: [PATCH 4/7] remove shell option

---
 .github/workflows/compiler-rt-libc-builtins-tests.yml | 2 --
 1 file changed, 2 deletions(-)

diff --git a/.github/workflows/compiler-rt-libc-builtins-tests.yml b/.github/workflows/compiler-rt-libc-builtins-tests.yml
index 66e511b45d95a..8605892a701fd 100644
--- a/.github/workflows/compiler-rt-libc-builtins-tests.yml
+++ b/.github/workflows/compiler-rt-libc-builtins-tests.yml
@@ -31,7 +31,6 @@ jobs:
           persist-credentials: false
 
       - name: Configure CMake
-        shell: bash
         run: |
           cmake \
             -B build \
@@ -47,7 +46,6 @@ jobs:
             -DCOMPILER_RT_INCLUDE_TESTS=ON
 
       - name: Test compiler-rt builtins
-        shell: bash
         run: |
           cmake \
             --build build \

>From 0fe3d3fa157ca88ef6394104eb59b854564ab79b Mon Sep 17 00:00:00 2001
From: hulxv <hulxxv at gmail.com>
Date: Fri, 26 Jun 2026 21:42:17 +0300
Subject: [PATCH 5/7] Split "[CI] Add precommit CIs to test compiler-rt + LLVM
 libc integration"

---
 .../compiler-rt-libc-builtins-tests.yml       | 53 -------------------
 1 file changed, 53 deletions(-)
 delete mode 100644 .github/workflows/compiler-rt-libc-builtins-tests.yml

diff --git a/.github/workflows/compiler-rt-libc-builtins-tests.yml b/.github/workflows/compiler-rt-libc-builtins-tests.yml
deleted file mode 100644
index 8605892a701fd..0000000000000
--- a/.github/workflows/compiler-rt-libc-builtins-tests.yml
+++ /dev/null
@@ -1,53 +0,0 @@
-# Pre-commit CI for the compiler-rt + LLVM-libc builtins integration.
-name: compiler-rt + libc Builtins Tests
-permissions:
-  contents: read
-on:
-  pull_request:
-    paths:
-      - "compiler-rt/lib/builtins/**"
-      - "compiler-rt/test/builtins/**"
-      - "libc/shared/builtins.h"
-      - "libc/shared/builtins/**"
-      - "libc/src/__support/builtins/**"
-      - "libc/src/__support/FPUtil/**"
-      - ".github/workflows/compiler-rt-libc-builtins-tests.yml"
-
-jobs:
-  build:
-    name: builtins (${{ matrix.os }})
-    if: github.repository_owner == 'llvm'
-    runs-on: ${{ matrix.os }}
-    container:
-      image: ${{ (startsWith(matrix.os, 'ubuntu-24.04-arm') && 'ghcr.io/llvm/arm64v8/libc-ubuntu-24.04') || 'ghcr.io/llvm/libc-ubuntu-24.04' }}
-    strategy:
-      fail-fast: false
-      matrix:
-        os: [ubuntu-24.04, ubuntu-24.04-arm]
-
-    steps:
-      - uses: actions/checkout at de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
-        with:
-          persist-credentials: false
-
-      - name: Configure CMake
-        run: |
-          cmake \
-            -B build \
-            -S llvm \
-            -G Ninja \
-            -DCMAKE_C_COMPILER=clang-23 \
-            -DCMAKE_CXX_COMPILER=clang++-23 \
-            -DCMAKE_BUILD_TYPE=Release \
-            -DLLVM_ENABLE_ASSERTIONS=ON \
-            -DLLVM_ENABLE_PROJECTS=clang \
-            -DLLVM_ENABLE_RUNTIMES="compiler-rt" \
-            -DCOMPILER_RT_USE_LIBC_MATH=ON \
-            -DCOMPILER_RT_INCLUDE_TESTS=ON
-
-      - name: Test compiler-rt builtins
-        run: |
-          cmake \
-            --build build \
-            --parallel \
-            --target check-builtins

>From b49d0eb2a39b0ac3b5d571f4158a162a58575e8b Mon Sep 17 00:00:00 2001
From: hulxv <hulxxv at gmail.com>
Date: Thu, 2 Jul 2026 01:16:36 +0300
Subject: [PATCH 6/7] [compiler-rt][builtins] add libc-backed arithmetic
 builtins

---
 compiler-rt/lib/builtins/CMakeLists.txt |  7 +++++++
 compiler-rt/lib/builtins/adddf3.cpp     | 22 ++++++++++++++++++++++
 compiler-rt/lib/builtins/divdf3.cpp     | 22 ++++++++++++++++++++++
 compiler-rt/lib/builtins/divtf3.cpp     | 22 ++++++++++++++++++++++
 compiler-rt/lib/builtins/muldf3.cpp     | 22 ++++++++++++++++++++++
 compiler-rt/lib/builtins/multf3.cpp     | 22 ++++++++++++++++++++++
 compiler-rt/lib/builtins/subdf3.cpp     | 22 ++++++++++++++++++++++
 compiler-rt/lib/builtins/subtf3.cpp     | 22 ++++++++++++++++++++++
 8 files changed, 161 insertions(+)
 create mode 100644 compiler-rt/lib/builtins/adddf3.cpp
 create mode 100644 compiler-rt/lib/builtins/divdf3.cpp
 create mode 100644 compiler-rt/lib/builtins/divtf3.cpp
 create mode 100644 compiler-rt/lib/builtins/muldf3.cpp
 create mode 100644 compiler-rt/lib/builtins/multf3.cpp
 create mode 100644 compiler-rt/lib/builtins/subdf3.cpp
 create mode 100644 compiler-rt/lib/builtins/subtf3.cpp

diff --git a/compiler-rt/lib/builtins/CMakeLists.txt b/compiler-rt/lib/builtins/CMakeLists.txt
index e128fea6a3415..659e149b5a307 100644
--- a/compiler-rt/lib/builtins/CMakeLists.txt
+++ b/compiler-rt/lib/builtins/CMakeLists.txt
@@ -264,7 +264,14 @@ if(COMPILER_RT_USE_LIBC_MATH)
   include_directories(SYSTEM ${_libc_include_dirs})
   add_definitions(-DCOMPILER_RT_USE_LIBC_MATH)
 
+  use_libc_builtin(GENERIC_SOURCES    adddf3)
   use_libc_builtin(GENERIC_TF_SOURCES addtf3)
+  use_libc_builtin(GENERIC_SOURCES    divdf3)
+  use_libc_builtin(GENERIC_TF_SOURCES divtf3)
+  use_libc_builtin(GENERIC_SOURCES    muldf3)
+  use_libc_builtin(GENERIC_TF_SOURCES multf3)
+  use_libc_builtin(GENERIC_SOURCES    subdf3)
+  use_libc_builtin(GENERIC_TF_SOURCES divtf3)
 endif()
 
 option(COMPILER_RT_EXCLUDE_ATOMIC_BUILTIN
diff --git a/compiler-rt/lib/builtins/adddf3.cpp b/compiler-rt/lib/builtins/adddf3.cpp
new file mode 100644
index 0000000000000..0bae3c92c3d3b
--- /dev/null
+++ b/compiler-rt/lib/builtins/adddf3.cpp
@@ -0,0 +1,22 @@
+//===-- lib/adddf3.cpp - Quad-precision addition (libc-backed) --*- C++ -*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+//
+// __adddf3 implemented on top of LLVM-libc's shared::adddf3 instruction.
+//
+//===----------------------------------------------------------------------===//
+
+#define QUAD_PRECISION
+#include "fp_lib.h"
+
+#include "fp_libc_config.h"
+#include "int_lib.h"
+#include "shared/builtins/adddf3.h"
+
+extern "C" COMPILER_RT_ABI fp_t __adddf3(fp_t a, fp_t b) {
+  return LIBC_NAMESPACE::shared::adddf3(a, b);
+}
diff --git a/compiler-rt/lib/builtins/divdf3.cpp b/compiler-rt/lib/builtins/divdf3.cpp
new file mode 100644
index 0000000000000..ea01a3c719c51
--- /dev/null
+++ b/compiler-rt/lib/builtins/divdf3.cpp
@@ -0,0 +1,22 @@
+//===-- lib/divdf3.cpp - Quad-precision addition (libc-backed) --*- C++ -*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+//
+// __divdf3 implemented on top of LLVM-libc's shared::divdf3 instruction.
+//
+//===----------------------------------------------------------------------===//
+
+#define QUAD_PRECISION
+#include "fp_lib.h"
+
+#include "fp_libc_config.h"
+#include "int_lib.h"
+#include "shared/builtins/divdf3.h"
+
+extern "C" COMPILER_RT_ABI fp_t __divdf3(fp_t a, fp_t b) {
+  return LIBC_NAMESPACE::shared::divdf3(a, b);
+}
diff --git a/compiler-rt/lib/builtins/divtf3.cpp b/compiler-rt/lib/builtins/divtf3.cpp
new file mode 100644
index 0000000000000..7b16174ce16d9
--- /dev/null
+++ b/compiler-rt/lib/builtins/divtf3.cpp
@@ -0,0 +1,22 @@
+//===-- lib/divtf3.cpp - Quad-precision addition (libc-backed) --*- C++ -*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+//
+// __divtf3 implemented on top of LLVM-libc's shared::divtf3 instruction.
+//
+//===----------------------------------------------------------------------===//
+
+#define QUAD_PRECISION
+#include "fp_lib.h"
+
+#include "fp_libc_config.h"
+#include "int_lib.h"
+#include "shared/builtins/divtf3.h"
+
+extern "C" COMPILER_RT_ABI fp_t __divtf3(fp_t a, fp_t b) {
+  return LIBC_NAMESPACE::shared::divtf3(a, b);
+}
diff --git a/compiler-rt/lib/builtins/muldf3.cpp b/compiler-rt/lib/builtins/muldf3.cpp
new file mode 100644
index 0000000000000..d1d4c70fdb837
--- /dev/null
+++ b/compiler-rt/lib/builtins/muldf3.cpp
@@ -0,0 +1,22 @@
+//===-- lib/muldf3.cpp - Quad-precision addition (libc-backed) --*- C++ -*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+//
+// __muldf3 implemented on top of LLVM-libc's shared::muldf3 instruction.
+//
+//===----------------------------------------------------------------------===//
+
+#define QUAD_PRECISION
+#include "fp_lib.h"
+
+#include "fp_libc_config.h"
+#include "int_lib.h"
+#include "shared/builtins/muldf3.h"
+
+extern "C" COMPILER_RT_ABI fp_t __muldf3(fp_t a, fp_t b) {
+  return LIBC_NAMESPACE::shared::muldf3(a, b);
+}
diff --git a/compiler-rt/lib/builtins/multf3.cpp b/compiler-rt/lib/builtins/multf3.cpp
new file mode 100644
index 0000000000000..bafc7016bddfb
--- /dev/null
+++ b/compiler-rt/lib/builtins/multf3.cpp
@@ -0,0 +1,22 @@
+//===-- lib/multf3.cpp - Quad-precision addition (libc-backed) --*- C++ -*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+//
+// __multf3 implemented on top of LLVM-libc's shared::multf3 instruction.
+//
+//===----------------------------------------------------------------------===//
+
+#define QUAD_PRECISION
+#include "fp_lib.h"
+
+#include "fp_libc_config.h"
+#include "int_lib.h"
+#include "shared/builtins/multf3.h"
+
+extern "C" COMPILER_RT_ABI fp_t __multf3(fp_t a, fp_t b) {
+  return LIBC_NAMESPACE::shared::multf3(a, b);
+}
diff --git a/compiler-rt/lib/builtins/subdf3.cpp b/compiler-rt/lib/builtins/subdf3.cpp
new file mode 100644
index 0000000000000..c688d7dcab4c9
--- /dev/null
+++ b/compiler-rt/lib/builtins/subdf3.cpp
@@ -0,0 +1,22 @@
+//===-- lib/subdf3.cpp - Quad-precision addition (libc-backed) --*- C++ -*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+//
+// __subdf3 implemented on top of LLVM-libc's shared::subdf3 instruction.
+//
+//===----------------------------------------------------------------------===//
+
+#define QUAD_PRECISION
+#include "fp_lib.h"
+
+#include "fp_libc_config.h"
+#include "int_lib.h"
+#include "shared/builtins/subdf3.h"
+
+extern "C" COMPILER_RT_ABI fp_t __subdf3(fp_t a, fp_t b) {
+  return LIBC_NAMESPACE::shared::subdf3(a, b);
+}
diff --git a/compiler-rt/lib/builtins/subtf3.cpp b/compiler-rt/lib/builtins/subtf3.cpp
new file mode 100644
index 0000000000000..6868293f4043f
--- /dev/null
+++ b/compiler-rt/lib/builtins/subtf3.cpp
@@ -0,0 +1,22 @@
+//===-- lib/subtf3.cpp - Quad-precision addition (libc-backed) --*- C++ -*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+//
+// __subtf3 implemented on top of LLVM-libc's shared::subtf3 instruction.
+//
+//===----------------------------------------------------------------------===//
+
+#define QUAD_PRECISION
+#include "fp_lib.h"
+
+#include "fp_libc_config.h"
+#include "int_lib.h"
+#include "shared/builtins/subtf3.h"
+
+extern "C" COMPILER_RT_ABI fp_t __subtf3(fp_t a, fp_t b) {
+  return LIBC_NAMESPACE::shared::subtf3(a, b);
+}

>From 17f7bc659824df66d5ddbb77a765d3e2c16e1245 Mon Sep 17 00:00:00 2001
From: hulxv <hulxxv at gmail.com>
Date: Sat, 4 Jul 2026 20:03:09 +0300
Subject: [PATCH 7/7] [compiler-rt][builtins] add libc-backed arithmetic
 builtins

---
 compiler-rt/lib/builtins/CMakeLists.txt    |  4 ++-
 compiler-rt/lib/builtins/adddf3.cpp        |  3 +-
 compiler-rt/lib/builtins/divdf3.cpp        |  3 +-
 compiler-rt/lib/builtins/divtf3.cpp        |  2 +-
 compiler-rt/lib/builtins/muldf3.cpp        |  2 +-
 compiler-rt/lib/builtins/multf3.cpp        |  2 +-
 compiler-rt/lib/builtins/negdf2.cpp        | 21 ++++++++++++
 compiler-rt/lib/builtins/negsf2.cpp        | 22 +++++++++++++
 compiler-rt/lib/builtins/subdf3.cpp        |  3 +-
 compiler-rt/lib/builtins/subtf3.cpp        |  2 +-
 libc/shared/builtins.h                     |  2 ++
 libc/shared/builtins/negdf2.h              | 29 +++++++++++++++++
 libc/shared/builtins/negsf2.h              | 29 +++++++++++++++++
 libc/src/__support/builtins/CMakeLists.txt | 16 ++++++++++
 libc/src/__support/builtins/negdf2.h       | 37 ++++++++++++++++++++++
 libc/src/__support/builtins/negsf2.h       | 37 ++++++++++++++++++++++
 libc/test/shared/CMakeLists.txt            |  2 ++
 libc/test/shared/shared_builtins_test.cpp  |  2 ++
 18 files changed, 207 insertions(+), 11 deletions(-)
 create mode 100644 compiler-rt/lib/builtins/negdf2.cpp
 create mode 100644 compiler-rt/lib/builtins/negsf2.cpp
 create mode 100644 libc/shared/builtins/negdf2.h
 create mode 100644 libc/shared/builtins/negsf2.h
 create mode 100644 libc/src/__support/builtins/negdf2.h
 create mode 100644 libc/src/__support/builtins/negsf2.h

diff --git a/compiler-rt/lib/builtins/CMakeLists.txt b/compiler-rt/lib/builtins/CMakeLists.txt
index 659e149b5a307..3a7751425d73f 100644
--- a/compiler-rt/lib/builtins/CMakeLists.txt
+++ b/compiler-rt/lib/builtins/CMakeLists.txt
@@ -270,8 +270,10 @@ if(COMPILER_RT_USE_LIBC_MATH)
   use_libc_builtin(GENERIC_TF_SOURCES divtf3)
   use_libc_builtin(GENERIC_SOURCES    muldf3)
   use_libc_builtin(GENERIC_TF_SOURCES multf3)
+  use_libc_builtin(GENERIC_SOURCES    negdf2)
+  use_libc_builtin(GENERIC_SOURCES    negsf2)
   use_libc_builtin(GENERIC_SOURCES    subdf3)
-  use_libc_builtin(GENERIC_TF_SOURCES divtf3)
+  use_libc_builtin(GENERIC_TF_SOURCES subtf3)
 endif()
 
 option(COMPILER_RT_EXCLUDE_ATOMIC_BUILTIN
diff --git a/compiler-rt/lib/builtins/adddf3.cpp b/compiler-rt/lib/builtins/adddf3.cpp
index 0bae3c92c3d3b..03da4a1e33fb1 100644
--- a/compiler-rt/lib/builtins/adddf3.cpp
+++ b/compiler-rt/lib/builtins/adddf3.cpp
@@ -1,4 +1,4 @@
-//===-- lib/adddf3.cpp - Quad-precision addition (libc-backed) --*- C++ -*-===//
+//===-- lib/adddf3.cpp - double-precision addition --------------*- C++ -*-===//
 //
 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
 // See https://llvm.org/LICENSE.txt for license information.
@@ -10,7 +10,6 @@
 //
 //===----------------------------------------------------------------------===//
 
-#define QUAD_PRECISION
 #include "fp_lib.h"
 
 #include "fp_libc_config.h"
diff --git a/compiler-rt/lib/builtins/divdf3.cpp b/compiler-rt/lib/builtins/divdf3.cpp
index ea01a3c719c51..50ce7372b0647 100644
--- a/compiler-rt/lib/builtins/divdf3.cpp
+++ b/compiler-rt/lib/builtins/divdf3.cpp
@@ -1,4 +1,4 @@
-//===-- lib/divdf3.cpp - Quad-precision addition (libc-backed) --*- C++ -*-===//
+//===-- lib/divdf3.cpp - double-precision division --------------*- C++ -*-===//
 //
 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
 // See https://llvm.org/LICENSE.txt for license information.
@@ -10,7 +10,6 @@
 //
 //===----------------------------------------------------------------------===//
 
-#define QUAD_PRECISION
 #include "fp_lib.h"
 
 #include "fp_libc_config.h"
diff --git a/compiler-rt/lib/builtins/divtf3.cpp b/compiler-rt/lib/builtins/divtf3.cpp
index 7b16174ce16d9..bd099280aead6 100644
--- a/compiler-rt/lib/builtins/divtf3.cpp
+++ b/compiler-rt/lib/builtins/divtf3.cpp
@@ -1,4 +1,4 @@
-//===-- lib/divtf3.cpp - Quad-precision addition (libc-backed) --*- C++ -*-===//
+//===-- lib/divtf3.cpp - quad-precision division ----------------*- C++ -*-===//
 //
 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
 // See https://llvm.org/LICENSE.txt for license information.
diff --git a/compiler-rt/lib/builtins/muldf3.cpp b/compiler-rt/lib/builtins/muldf3.cpp
index d1d4c70fdb837..d2effb1c7d665 100644
--- a/compiler-rt/lib/builtins/muldf3.cpp
+++ b/compiler-rt/lib/builtins/muldf3.cpp
@@ -1,4 +1,4 @@
-//===-- lib/muldf3.cpp - Quad-precision addition (libc-backed) --*- C++ -*-===//
+//===-- lib/muldf3.cpp - double-precision multiplication --------*- C++ -*-===//
 //
 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
 // See https://llvm.org/LICENSE.txt for license information.
diff --git a/compiler-rt/lib/builtins/multf3.cpp b/compiler-rt/lib/builtins/multf3.cpp
index bafc7016bddfb..e8324ca302c6e 100644
--- a/compiler-rt/lib/builtins/multf3.cpp
+++ b/compiler-rt/lib/builtins/multf3.cpp
@@ -1,4 +1,4 @@
-//===-- lib/multf3.cpp - Quad-precision addition (libc-backed) --*- C++ -*-===//
+//===-- lib/multf3.cpp - quad-precision multiplication ----------*- C++ -*-===//
 //
 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
 // See https://llvm.org/LICENSE.txt for license information.
diff --git a/compiler-rt/lib/builtins/negdf2.cpp b/compiler-rt/lib/builtins/negdf2.cpp
new file mode 100644
index 0000000000000..cc9f190845cf3
--- /dev/null
+++ b/compiler-rt/lib/builtins/negdf2.cpp
@@ -0,0 +1,21 @@
+//===-- lib/negdf2.cpp - double-precision negation --------------*- C++ -*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+//
+// __negdf2 implemented on top of LLVM-libc's shared::negdf2 instruction.
+//
+//===----------------------------------------------------------------------===//
+
+#include "fp_lib.h"
+
+#include "fp_libc_config.h"
+#include "int_lib.h"
+#include "shared/builtins/negdf2.h"
+
+extern "C" COMPILER_RT_ABI fp_t __negdf2(fp_t a, fp_t b) {
+  return LIBC_NAMESPACE::shared::negdf2(a, b);
+}
diff --git a/compiler-rt/lib/builtins/negsf2.cpp b/compiler-rt/lib/builtins/negsf2.cpp
new file mode 100644
index 0000000000000..2f23bf64b6b57
--- /dev/null
+++ b/compiler-rt/lib/builtins/negsf2.cpp
@@ -0,0 +1,22 @@
+//===-- lib/negsf2.cpp - single-precision negation --------------*- C++ -*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+//
+// __negsf2 implemented on top of LLVM-libc's shared::negsf2 instruction.
+//
+//===----------------------------------------------------------------------===//
+
+#define QUAD_PRECISION
+#include "fp_lib.h"
+
+#include "fp_libc_config.h"
+#include "int_lib.h"
+#include "shared/builtins/negsf2.h"
+
+extern "C" COMPILER_RT_ABI fp_t __negsf2(fp_t a, fp_t b) {
+  return LIBC_NAMESPACE::shared::negsf2(a, b);
+}
diff --git a/compiler-rt/lib/builtins/subdf3.cpp b/compiler-rt/lib/builtins/subdf3.cpp
index c688d7dcab4c9..8ff5e0dc69976 100644
--- a/compiler-rt/lib/builtins/subdf3.cpp
+++ b/compiler-rt/lib/builtins/subdf3.cpp
@@ -1,4 +1,4 @@
-//===-- lib/subdf3.cpp - Quad-precision addition (libc-backed) --*- C++ -*-===//
+//===-- lib/subdf3.cpp - double-precision subtraction -----------*- C++ -*-===//
 //
 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
 // See https://llvm.org/LICENSE.txt for license information.
@@ -10,7 +10,6 @@
 //
 //===----------------------------------------------------------------------===//
 
-#define QUAD_PRECISION
 #include "fp_lib.h"
 
 #include "fp_libc_config.h"
diff --git a/compiler-rt/lib/builtins/subtf3.cpp b/compiler-rt/lib/builtins/subtf3.cpp
index 6868293f4043f..0880d12ddc50d 100644
--- a/compiler-rt/lib/builtins/subtf3.cpp
+++ b/compiler-rt/lib/builtins/subtf3.cpp
@@ -1,4 +1,4 @@
-//===-- lib/subtf3.cpp - Quad-precision addition (libc-backed) --*- C++ -*-===//
+//===-- lib/subtf3.cpp - quad-precision subtraction -------------*- C++ -*-===//
 //
 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
 // See https://llvm.org/LICENSE.txt for license information.
diff --git a/libc/shared/builtins.h b/libc/shared/builtins.h
index a6a8973beda25..cb025b0364d48 100644
--- a/libc/shared/builtins.h
+++ b/libc/shared/builtins.h
@@ -24,6 +24,8 @@
 #include "builtins/divtf3.h"
 #include "builtins/muldf3.h"
 #include "builtins/multf3.h"
+#include "builtins/negdf2.h"
+#include "builtins/negsf2.h"
 #include "builtins/subdf3.h"
 #include "builtins/subtf3.h"
 
diff --git a/libc/shared/builtins/negdf2.h b/libc/shared/builtins/negdf2.h
new file mode 100644
index 0000000000000..3322730f39dc6
--- /dev/null
+++ b/libc/shared/builtins/negdf2.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 __negdf2 implementation as shared::negdf2 so
+/// that it can be reused by compiler-rt's builtins.
+///
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SHARED_BUILTINS_NEGDF2_H
+#define LLVM_LIBC_SHARED_BUILTINS_NEGDF2_H
+
+#include "shared/libc_common.h"
+#include "src/__support/builtins/negdf2.h"
+
+namespace LIBC_NAMESPACE_DECL {
+namespace shared {
+
+using builtins::negdf2;
+
+} // namespace shared
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SHARED_BUILTINS_NEGDF2_H
diff --git a/libc/shared/builtins/negsf2.h b/libc/shared/builtins/negsf2.h
new file mode 100644
index 0000000000000..f8919935d8e84
--- /dev/null
+++ b/libc/shared/builtins/negsf2.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 __negsf2 implementation as shared::negsf2 so
+/// that it can be reused by compiler-rt's builtins.
+///
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SHARED_BUILTINS_NEGSF2_H
+#define LLVM_LIBC_SHARED_BUILTINS_NEGSF2_H
+
+#include "shared/libc_common.h"
+#include "src/__support/builtins/negsf2.h"
+
+namespace LIBC_NAMESPACE_DECL {
+namespace shared {
+
+using builtins::negsf2;
+
+} // namespace shared
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SHARED_BUILTINS_NEGSF2_H
diff --git a/libc/src/__support/builtins/CMakeLists.txt b/libc/src/__support/builtins/CMakeLists.txt
index 69d714b9fa207..77a5bd38929e4 100644
--- a/libc/src/__support/builtins/CMakeLists.txt
+++ b/libc/src/__support/builtins/CMakeLists.txt
@@ -81,4 +81,20 @@ add_header_library(
   DEPENDS
     libc.src.__support.FPUtil.generic.add_sub
     libc.src.__support.macros.config
+add_header_library(
+  negsf2
+  HDRS
+    negsf2.h
+  DEPENDS
+    libc.src.__support.macros.config
+    libc.src.__support.FPUtil.fp_bits
+)
+
+add_header_library(
+  negdf2
+  HDRS
+    negdf2.h
+  DEPENDS
+    libc.src.__support.macros.config
+    libc.src.__support.FPUtil.fp_bits
 )
diff --git a/libc/src/__support/builtins/negdf2.h b/libc/src/__support/builtins/negdf2.h
new file mode 100644
index 0000000000000..5e9752509a3d7
--- /dev/null
+++ b/libc/src/__support/builtins/negdf2.h
@@ -0,0 +1,37 @@
+//===----------------------------------------------------------------------===//
+//
+// 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 __negdf2 implementation as builtins::negdf2
+/// so that it can be reused by compiler-rt's builtins.
+///
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SRC___SUPPORT_BUILTINS_NEGDF2_H
+#define LLVM_LIBC_SRC___SUPPORT_BUILTINS_NEGDF2_H
+
+#include "src/__support/FPUtil/FPBits.h"
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+namespace builtins {
+
+// Double-precision negation 
+// Mirrors compiler-rt's __negdf2.
+LIBC_INLINE double negdf2(double x) {
+    using FPBits = fputil::FPBits<double>;
+
+    FPBits bits(x);
+    bits.set_sign(bits.sign().negate());
+    return bits.get_val();
+}
+
+} // namespace builtins
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC___SUPPORT_BUILTINS_NEGDF2_H
diff --git a/libc/src/__support/builtins/negsf2.h b/libc/src/__support/builtins/negsf2.h
new file mode 100644
index 0000000000000..81ab53ca7750c
--- /dev/null
+++ b/libc/src/__support/builtins/negsf2.h
@@ -0,0 +1,37 @@
+//===----------------------------------------------------------------------===//
+//
+// 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 __negsf2 implementation as builtins::negsf2
+/// so that it can be reused by compiler-rt's builtins.
+///
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SRC___SUPPORT_BUILTINS_NEGSF2_H
+#define LLVM_LIBC_SRC___SUPPORT_BUILTINS_NEGSF2_H
+
+#include "src/__support/FPUtil/FPBits.h"
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+namespace builtins {
+
+// Single-precision negation
+// Mirrors compiler-rt's __negsf2.
+LIBC_INLINE float negsf2(float x) {
+  using FPBits = fputil::FPBits<float>;
+
+  FPBits bits(x);
+  bits.set_sign(bits.sign().negate());
+  return bits.get_val();
+}
+
+} // namespace builtins
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC___SUPPORT_BUILTINS_NEGSF2_H
diff --git a/libc/test/shared/CMakeLists.txt b/libc/test/shared/CMakeLists.txt
index aeb573474d6ef..0cae1e4226b37 100644
--- a/libc/test/shared/CMakeLists.txt
+++ b/libc/test/shared/CMakeLists.txt
@@ -834,6 +834,8 @@ add_fp_unittest(
     libc.src.__support.builtins.divtf3
     libc.src.__support.builtins.muldf3
     libc.src.__support.builtins.multf3
+    libc.src.__support.builtins.negdf3
+    libc.src.__support.builtins.negsf3
     libc.src.__support.builtins.subdf3
     libc.src.__support.builtins.subtf3
 )
diff --git a/libc/test/shared/shared_builtins_test.cpp b/libc/test/shared/shared_builtins_test.cpp
index 20e79498b4cfb..e2eeb378dbf3c 100644
--- a/libc/test/shared/shared_builtins_test.cpp
+++ b/libc/test/shared/shared_builtins_test.cpp
@@ -12,12 +12,14 @@
 
 TEST(LlvmLibcSharedBuiltinsTest, AllFloat) {
   EXPECT_FP_EQ(3.0f, LIBC_NAMESPACE::shared::addsf3(1.0f, 2.0f));
+  EXPECT_FP_EQ(-5.0, LIBC_NAMESPACE::shared::negsf2(5.0));
 }
 
 TEST(LlvmLibcSharedBuiltinsTest, AllDouble) {
   EXPECT_FP_EQ(3.0, LIBC_NAMESPACE::shared::adddf3(1.0, 2.0));
   EXPECT_FP_EQ(3.0, LIBC_NAMESPACE::shared::divdf3(6.0, 2.0));
   EXPECT_FP_EQ(6.0, LIBC_NAMESPACE::shared::muldf3(2.0, 3.0));
+  EXPECT_FP_EQ(5.0, LIBC_NAMESPACE::shared::negdf2(-5.0));
   EXPECT_FP_EQ(2.0, LIBC_NAMESPACE::shared::subdf3(5.0, 3.0));
 }
 



More information about the llvm-commits mailing list