Skip to content

feat: implement template meta muli #138654

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions CppProperties.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"configurations": [
{
"inheritEnvironments": [
"msvc_x86"
],
"name": "x86-Debug",
"includePath": [
"${env.INCLUDE}",
"${workspaceRoot}\\**"
],
"defines": [
"WIN32",
"_DEBUG",
"UNICODE",
"_UNICODE"
],
"intelliSenseMode": "windows-msvc-x86"
}
]
}
20 changes: 20 additions & 0 deletions libc/src/__support/fixed_point/fx_bits.h
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down