[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 03:00:43 PDT 2025
https://github.com/Devesh-Yadav10 created https://github.com/llvm/llvm-project/pull/131297
Summary
This patch adds the `mulifx` function to LLVM-libc, which performs fixed-point multiplication of two integers and returns the result.
Changes
- **Added `mulifx.h`** in `libc/src/math/` for function declaration.
- **Implemented `mulifx.cpp`** in `libc/src/math/generic/`.
- **Updated `CMakeLists.txt`** in `libc/src/math/generic/` and `libc/src/math/` to include `mulifx`.
- **Added a unit test** in `libc/test/src/math/mulifx_test.cpp`.
Functionality
The function multiplies two **fixed-point integers** and adjusts the result based on a given number of fractional bits:
```cpp
int32_t mulifx(int32_t a, int32_t b, int frac_bits);
>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/5] 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/5] [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/5] [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/5] [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/5] [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(
More information about the libc-commits
mailing list