Skip to content

Conversation

@naveen106
Copy link

fixing issue #129123. More to be done. Not completed.

@github-actions
Copy link

github-actions bot commented May 6, 2025

Thank you for submitting a Pull Request (PR) to the LLVM Project!

This PR will be automatically labeled and the relevant teams will be notified.

If you wish to, you can add reviewers by using the "Reviewers" section on this page.

If this is not working for you, it is probably because you do not have write permissions for the repository. In which case you can instead tag reviewers by name in a comment by using @ followed by their GitHub username.

If you have received no comments on your PR for a week, you can request a review by "ping"ing the PR by adding a comment “Ping”. The common courtesy "ping" rate is once a week. Please remember that you are asking for valuable time from other developers.

If you have further questions, they may be answered by the LLVM GitHub User Guide.

You can also ask questions in a comment on this PR, on the LLVM Discord or on the forums.

@llvmbot llvmbot added the libc label May 6, 2025
@llvmbot
Copy link
Member

llvmbot commented May 6, 2025

@llvm/pr-subscribers-libc

Author: Naveen Kumar (naveen106)

Changes

fixing issue #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 --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>

@PiJoules PiJoules self-requested a review May 12, 2025 17:58
@PiJoules
Copy link
Contributor

The implementation looks ok, but could you add tests and the relevant build changes so we can ensure it works properly? You can follow what the other fixed point patches do and replace the names appropriately (#133005 for example).

@naveen106 naveen106 force-pushed the libc-implement-mulifx branch from 5927574 to 11aa948 Compare May 16, 2025 14:59
@naveen106
Copy link
Author

naveen106 commented May 16, 2025

The implementation looks ok, but could you add tests and the relevant build changes so we can ensure it works properly? You can follow what the other fixed point patches do and replace the names appropriately (#133005 for example).

@PiJoules
Working on it! I have added mulifx functions, working on unit tests now. Inform me if you see something wrong till now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants