Skip to content

Commit 5927574

Browse files
committed
feat: implement template meta muli
1 parent a5cdbef commit 5927574

File tree

2 files changed

+41
-0
lines changed

2 files changed

+41
-0
lines changed

CppProperties.json

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
{
2+
"configurations": [
3+
{
4+
"inheritEnvironments": [
5+
"msvc_x86"
6+
],
7+
"name": "x86-Debug",
8+
"includePath": [
9+
"${env.INCLUDE}",
10+
"${workspaceRoot}\\**"
11+
],
12+
"defines": [
13+
"WIN32",
14+
"_DEBUG",
15+
"UNICODE",
16+
"_UNICODE"
17+
],
18+
"intelliSenseMode": "windows-msvc-x86"
19+
}
20+
]
21+
}

libc/src/__support/fixed_point/fx_bits.h

+20
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,26 @@ countls(T f) {
194194
return cpp::countl_zero(value_bits) - FXRep::SIGN_LEN;
195195
}
196196

197+
// Multiply an integer with a fixed-point value and return an integer.
198+
// Overflow behavior is undefined, per ISO 8037.
199+
template <typename FixedPointT, typename IntT>
200+
LIBC_INLINE constexpr cpp::enable_if_t <cpp::is_fixed_point_v<FixedPointT> && cpp::is_integral_v<IntT>, IntT >
201+
muli(FixedPointT f, IntT i) {
202+
203+
using FXRep = FXRep<FixedPointT>;
204+
using BitType = typename FXRep::StorageType;
205+
BitType fixed_bits = FXBits<FixedPointT>(f).get_bits();
206+
207+
// Safely promote types to unsigned for multiplication to avoid signed overflow
208+
using UnsignedIntT = cpp::make_unsigned_t<IntT>;
209+
using UnsignedFixedT = cpp::make_unsigned_t<BitType>;
210+
211+
auto product = static_cast<UnsignedIntT>(i) * static_cast<UnsignedFixedT>(fixed_bits);
212+
213+
// Shift back to remove fractional bits
214+
return static_cast<IntT>(product >> FXRep::FRAC_LEN);
215+
}
216+
197217
// fixed-point to integer conversion
198218
template <typename T, typename XType>
199219
LIBC_INLINE constexpr cpp::enable_if_t<cpp::is_fixed_point_v<T>, XType>

0 commit comments

Comments
 (0)