Skip to content

Commit 28d8e87

Browse files
committed
feat: implement template meta muli
1 parent a07cc18 commit 28d8e87

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
@@ -195,6 +195,26 @@ countls(T f) {
195195
return cpp::countl_zero(value_bits) - FXRep::SIGN_LEN;
196196
}
197197

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

0 commit comments

Comments
 (0)