[libc-commits] [libc] [llvm] feat: implement template meta muli (PR #138654)

via libc-commits libc-commits at lists.llvm.org
Tue May 6 01:50:16 PDT 2025


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-libc

Author: Naveen Kumar (naveen106)

<details>
<summary>Changes</summary>

fixing issue [#<!-- -->129123](https://github.com/llvm/llvm-project/issues/129123). More to be done. Not completed.

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


2 Files Affected:

- (added) CppProperties.json (+21) 
- (modified) libc/src/__support/fixed_point/fx_bits.h (+20) 


``````````diff
diff --git a/CppProperties.json b/CppProperties.json
new file mode 100644
index 0000000000000..659bf4ea9068f
--- /dev/null
+++ b/CppProperties.json
@@ -0,0 +1,21 @@
+{
+  "configurations": [
+    {
+      "inheritEnvironments": [
+        "msvc_x86"
+      ],
+      "name": "x86-Debug",
+      "includePath": [
+        "${env.INCLUDE}",
+        "${workspaceRoot}\\**"
+      ],
+      "defines": [
+        "WIN32",
+        "_DEBUG",
+        "UNICODE",
+        "_UNICODE"
+      ],
+      "intelliSenseMode": "windows-msvc-x86"
+    }
+  ]
+}
\ No newline at end of file
diff --git a/libc/src/__support/fixed_point/fx_bits.h b/libc/src/__support/fixed_point/fx_bits.h
index b05f46bd34660..974bb8a8f79bd 100644
--- a/libc/src/__support/fixed_point/fx_bits.h
+++ b/libc/src/__support/fixed_point/fx_bits.h
@@ -194,6 +194,26 @@ countls(T f) {
   return cpp::countl_zero(value_bits) - FXRep::SIGN_LEN;
 }
 
+// Multiply an integer with a fixed-point value and return an integer.
+// Overflow behavior is undefined, per ISO 8037.
+template <typename FixedPointT, typename IntT>
+LIBC_INLINE constexpr cpp::enable_if_t <cpp::is_fixed_point_v<FixedPointT> && cpp::is_integral_v<IntT>, IntT > 
+muli(FixedPointT f, IntT i) {
+  
+  using FXRep = FXRep<FixedPointT>;
+  using BitType = typename FXRep::StorageType;
+  BitType fixed_bits = FXBits<FixedPointT>(f).get_bits();
+
+  // Safely promote types to unsigned for multiplication to avoid signed overflow
+  using UnsignedIntT = cpp::make_unsigned_t<IntT>;
+  using UnsignedFixedT = cpp::make_unsigned_t<BitType>;
+
+  auto product = static_cast<UnsignedIntT>(i) * static_cast<UnsignedFixedT>(fixed_bits);
+
+  // Shift back to remove fractional bits
+  return static_cast<IntT>(product >> FXRep::FRAC_LEN);
+}
+
 // fixed-point to integer conversion
 template <typename T, typename XType>
 LIBC_INLINE constexpr cpp::enable_if_t<cpp::is_fixed_point_v<T>, XType>

``````````

</details>


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


More information about the libc-commits mailing list