From 59275749c8cbb48d999d1a5289e5aec461f98924 Mon Sep 17 00:00:00 2001 From: Naveen Kumar Date: Tue, 6 May 2025 14:10:48 +0530 Subject: [PATCH] feat: implement template meta muli --- CppProperties.json | 21 +++++++++++++++++++++ libc/src/__support/fixed_point/fx_bits.h | 20 ++++++++++++++++++++ 2 files changed, 41 insertions(+) create mode 100644 CppProperties.json 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 +LIBC_INLINE constexpr cpp::enable_if_t && cpp::is_integral_v, IntT > +muli(FixedPointT f, IntT i) { + + using FXRep = FXRep; + using BitType = typename FXRep::StorageType; + BitType fixed_bits = FXBits(f).get_bits(); + + // Safely promote types to unsigned for multiplication to avoid signed overflow + using UnsignedIntT = cpp::make_unsigned_t; + using UnsignedFixedT = cpp::make_unsigned_t; + + auto product = static_cast(i) * static_cast(fixed_bits); + + // Shift back to remove fractional bits + return static_cast(product >> FXRep::FRAC_LEN); +} + // fixed-point to integer conversion template LIBC_INLINE constexpr cpp::enable_if_t, XType>