[libc-commits] [libc] Implement fixed point mulifx functions in llvm-libc #129123 (PR #131297)

Devesh Yadav via libc-commits libc-commits at lists.llvm.org
Fri Mar 14 19:45:38 PDT 2025


https://github.com/Devesh-Yadav10 updated https://github.com/llvm/llvm-project/pull/131297

>From ee7a17dabc545d1168875d9af968e8cbe1329e1f Mon Sep 17 00:00:00 2001
From: Devesh Yadav <160987201+Devesh-Yadav10 at users.noreply.github.com>
Date: Fri, 14 Mar 2025 15:14:29 +0530
Subject: [PATCH 1/8] Create fixed_point_math.h

---
 libc/src/math/fixed_point_math.h | 16 ++++++++++++++++
 1 file changed, 16 insertions(+)
 create mode 100644 libc/src/math/fixed_point_math.h

diff --git a/libc/src/math/fixed_point_math.h b/libc/src/math/fixed_point_math.h
new file mode 100644
index 0000000000000..11aaedd0a13fa
--- /dev/null
+++ b/libc/src/math/fixed_point_math.h
@@ -0,0 +1,16 @@
+#ifndef LLVM_LIBC_SRC_MATH_FIXED_POINT_MATH_H
+#define LLVM_LIBC_SRC_MATH_FIXED_POINT_MATH_H
+
+#include <stdint.h>
+
+namespace __llvm_libc {
+
+// Multiply two fixed-point numbers and return the result
+constexpr int32_t mulifx(int32_t a, int32_t b, int frac_bits) {
+    int64_t product = static_cast<int64_t>(a) * static_cast<int64_t>(b);
+    return static_cast<int32_t>(product >> frac_bits);
+}
+
+} // namespace __llvm_libc
+
+#endif // LLVM_LIBC_SRC_MATH_FIXED_POINT_MATH_H

>From 35c90e1424faedcf85b886c1dc0a542117155e0f Mon Sep 17 00:00:00 2001
From: Devesh Yadav <160987201+Devesh-Yadav10 at users.noreply.github.com>
Date: Fri, 14 Mar 2025 15:17:42 +0530
Subject: [PATCH 2/8] [libc] Implement fixed point mulifx functions in
 llvm-libc #129123

ISO 18037 describes various fixed point mulifx functions which multiply a fixed point number by an integer and returns an integer rather than a fixed point type.
---
 libc/src/math/fixed_point_math.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/libc/src/math/fixed_point_math.h b/libc/src/math/fixed_point_math.h
index 11aaedd0a13fa..30c4d6c622455 100644
--- a/libc/src/math/fixed_point_math.h
+++ b/libc/src/math/fixed_point_math.h
@@ -13,4 +13,4 @@ constexpr int32_t mulifx(int32_t a, int32_t b, int frac_bits) {
 
 } // namespace __llvm_libc
 
-#endif // LLVM_LIBC_SRC_MATH_FIXED_POINT_MATH_H
+#endif 

>From 77be994d7c059e4e126a020040767122220326b2 Mon Sep 17 00:00:00 2001
From: Devesh Yadav <160987201+Devesh-Yadav10 at users.noreply.github.com>
Date: Fri, 14 Mar 2025 15:23:53 +0530
Subject: [PATCH 3/8] [libc][math] Add mulfix() function

---
 libc/src/math/CMakeLists.txt | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/libc/src/math/CMakeLists.txt b/libc/src/math/CMakeLists.txt
index f18a73d46f9aa..bb343338be2cb 100644
--- a/libc/src/math/CMakeLists.txt
+++ b/libc/src/math/CMakeLists.txt
@@ -40,6 +40,8 @@ function(add_math_entrypoint_object name)
   )
 endfunction()
 
+add_math_entrypoint_object(mulifx)
+
 add_math_entrypoint_object(acos)
 add_math_entrypoint_object(acosf)
 add_math_entrypoint_object(acosf16)

>From 9d788e322953c85b1c7580ce5b7891444509a5eb Mon Sep 17 00:00:00 2001
From: Devesh Yadav <160987201+Devesh-Yadav10 at users.noreply.github.com>
Date: Fri, 14 Mar 2025 15:26:38 +0530
Subject: [PATCH 4/8] [libc] Creating muflix function

---
 libc/src/math/generic/mulfix.cpp | 11 +++++++++++
 1 file changed, 11 insertions(+)
 create mode 100644 libc/src/math/generic/mulfix.cpp

diff --git a/libc/src/math/generic/mulfix.cpp b/libc/src/math/generic/mulfix.cpp
new file mode 100644
index 0000000000000..878c6b27c1bb8
--- /dev/null
+++ b/libc/src/math/generic/mulfix.cpp
@@ -0,0 +1,11 @@
+#include "src/math/mulifx.h"
+
+namespace __llvm_libc {
+
+// Multiply two fixed-point numbers and return the result
+int32_t mulifx(int32_t a, int32_t b, int frac_bits) {
+    int64_t product = static_cast<int64_t>(a) * static_cast<int64_t>(b);
+    return static_cast<int32_t>(product >> frac_bits);
+}
+
+} // namespace __llvm_libc

>From 9f8c71682443a34664a7223056d4ce3fbd9dbde9 Mon Sep 17 00:00:00 2001
From: Devesh Yadav <160987201+Devesh-Yadav10 at users.noreply.github.com>
Date: Fri, 14 Mar 2025 15:27:29 +0530
Subject: [PATCH 5/8] [libc] Add mulfix() function

---
 libc/src/math/generic/CMakeLists.txt | 5 +++++
 1 file changed, 5 insertions(+)

diff --git a/libc/src/math/generic/CMakeLists.txt b/libc/src/math/generic/CMakeLists.txt
index 3114289bad486..f61b1a2139fe3 100644
--- a/libc/src/math/generic/CMakeLists.txt
+++ b/libc/src/math/generic/CMakeLists.txt
@@ -1,3 +1,8 @@
+add_entrypoint_object(
+  mulifx
+  SRCS mulifx.cpp
+  HDRS ../mulifx.h
+)
 
 
 add_entrypoint_object(

>From 192034d1980b1219491cb290afde895955279ccc Mon Sep 17 00:00:00 2001
From: Devesh Yadav <160987201+Devesh-Yadav10 at users.noreply.github.com>
Date: Fri, 14 Mar 2025 15:33:48 +0530
Subject: [PATCH 6/8] [libc] Created header file for mulfix() function

---
 libc/src/math/{fixed_point_math.h => mulfix.h} | 0
 1 file changed, 0 insertions(+), 0 deletions(-)
 rename libc/src/math/{fixed_point_math.h => mulfix.h} (100%)

diff --git a/libc/src/math/fixed_point_math.h b/libc/src/math/mulfix.h
similarity index 100%
rename from libc/src/math/fixed_point_math.h
rename to libc/src/math/mulfix.h

>From fd5f8824d27667ab7f79a0033467ed024a94761b Mon Sep 17 00:00:00 2001
From: Devesh Yadav <160987201+Devesh-Yadav10 at users.noreply.github.com>
Date: Sat, 15 Mar 2025 08:14:13 +0530
Subject: [PATCH 7/8] [libc] Implement fixed point fxdivi functions in
 llvm-libc

---
 libc/src/math/generic/fxdivi.cpp | 16 ++++++++++++++++
 1 file changed, 16 insertions(+)
 create mode 100644 libc/src/math/generic/fxdivi.cpp

diff --git a/libc/src/math/generic/fxdivi.cpp b/libc/src/math/generic/fxdivi.cpp
new file mode 100644
index 0000000000000..738b84029198c
--- /dev/null
+++ b/libc/src/math/generic/fxdivi.cpp
@@ -0,0 +1,16 @@
+#include "src/math/fxdivi.h"
+#include <stdint.h>
+
+namespace __llvm_libc {
+
+// Fixed-point division: a / b with frac_bits precision
+int32_t fxdivi(int32_t a, int32_t b, int frac_bits) {
+    if (b == 0) {
+        // Handle division by zero case (return max value or another error handling strategy)
+        return (a >= 0) ? INT32_MAX : INT32_MIN;
+    }
+    int64_t dividend = static_cast<int64_t>(a) << frac_bits;
+    return static_cast<int32_t>(dividend / b);
+}
+
+} // namespace __llvm_libc

>From 6387d11b1ba129f0436ab70ff539a430915cfc98 Mon Sep 17 00:00:00 2001
From: Devesh Yadav <160987201+Devesh-Yadav10 at users.noreply.github.com>
Date: Sat, 15 Mar 2025 08:15:26 +0530
Subject: [PATCH 8/8] [libc][math] Add fxdivi() function header

---
 libc/src/math/fxdivi.h | 13 +++++++++++++
 1 file changed, 13 insertions(+)
 create mode 100644 libc/src/math/fxdivi.h

diff --git a/libc/src/math/fxdivi.h b/libc/src/math/fxdivi.h
new file mode 100644
index 0000000000000..1c08ce125292e
--- /dev/null
+++ b/libc/src/math/fxdivi.h
@@ -0,0 +1,13 @@
+#ifndef LLVM_LIBC_SRC_MATH_FXDIVI_H
+#define LLVM_LIBC_SRC_MATH_FXDIVI_H
+
+#include <stdint.h>
+
+namespace __llvm_libc {
+
+// Fixed-point division function
+int32_t fxdivi(int32_t a, int32_t b, int frac_bits);
+
+} // namespace __llvm_libc
+
+#endif // LLVM_LIBC_SRC_MATH_FXDIVI_H



More information about the libc-commits mailing list