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

via libc-commits libc-commits at lists.llvm.org
Fri Mar 14 03:01:33 PDT 2025


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-libc

Author: Devesh Yadav (Devesh-Yadav10)

<details>
<summary>Changes</summary>

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);


---
Full diff: https://github.com/llvm/llvm-project/pull/131297.diff


4 Files Affected:

- (modified) libc/src/math/CMakeLists.txt (+2) 
- (added) libc/src/math/fixed_point_math.h (+16) 
- (modified) libc/src/math/generic/CMakeLists.txt (+5) 
- (added) libc/src/math/generic/mulfix.cpp (+11) 


``````````diff
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)
diff --git a/libc/src/math/fixed_point_math.h b/libc/src/math/fixed_point_math.h
new file mode 100644
index 0000000000000..30c4d6c622455
--- /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 
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(
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

``````````

</details>


https://github.com/llvm/llvm-project/pull/131297


More information about the libc-commits mailing list