File tree 2 files changed +41
-0
lines changed
libc/src/__support/fixed_point
2 files changed +41
-0
lines changed Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change @@ -195,6 +195,26 @@ countls(T f) {
195
195
return cpp::countl_zero (value_bits) - FXRep::SIGN_LEN;
196
196
}
197
197
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
+
198
218
// fixed-point to integer conversion
199
219
template <typename T, typename XType>
200
220
LIBC_INLINE constexpr cpp::enable_if_t <cpp::is_fixed_point_v<T>, XType>
You can’t perform that action at this time.
0 commit comments