std::mul_sat(3)
π NAME π
okay so bestie, welcome to std::mul_sat(3) from the C++ Standard Libary (yes, that's the header's spelling, not me being chaotic π
).
std::mul_sat - std::mul_sat
β¨ Synopsis β¨
Defined in header <numeric>:
template< class T > (since C++26)
constexpr T mul_sat( T x, T y ) noexcept;
so it computes the saturating multiplication x Γ y. This operation (unlike built-in arithmetic operations on integers) behaves as-if it is a mathematical operation with an infinite range. Let q denote the result of such operation. Returns:
q, ifqis representable as a value of typeT. Otherwise,- the largest or smallest value of type
T, whichever is closer to theq.
no cap, this overload participates in overload resolution only if T is an integer type, that is: signed char, short, int, long, long long, an extended signed integer type, or an unsigned version of such types.
In particular,Tmust not be (possibly cv-qualified)bool,char,wchar_t,char8_t,char16_t, andchar32_t, as these types are not intended for arithmetic.
πΈ Parameters πΈ
x,yβ integer values
π Return value π
Saturated x Γ y.
π¦ Exceptions π¦
Throws no exceptions. literally zero drama.
π Notes π
- Unlike the built-in arithmetic operators on integers, the integral promotion does not apply to the
xandyarguments. - If two arguments of different type are passed, the call fails to compile, i.e. the behavior relative to template argument deduction is the same as for
std::minorstd::max. - Most modern hardware architectures have efficient support for saturation arithmetic on SIMD vectors, including SSE2 for x86 and NEON for ARM.
highkey, the feature-test macro check is π:
- Feature-test macro:
__cpp_lib_saturation_arithmetic - Value:
202311L - Std:
(C++26) - Feature: Saturation arithmetic
π§ Possible implementation π§
See libstdc++ (gcc).
π Example π
Can be previewed on Compiler Explorer.
// Run this code
#include <climits>
#include <numeric>
static_assert
(""
&& (std::mul_sat<int>(2, 3) == 6) // not saturated
&& (std::mul_sat<int>(INT_MAX / 2, 3) == INT_MAX) // saturated
&& (std::mul_sat<int>(-2, 3) == -6) // not saturated
&& (std::mul_sat<int>(INT_MIN / -2, -3) == INT_MIN) // saturated
&& (std::mul_sat<unsigned>(2, 3) == 6) // not saturated
&& (std::mul_sat<unsigned>(UINT_MAX / 2, 3) == UINT_MAX) //
saturated
);
int main() {}
π See also π
add_satβ saturating addition operation on two integers (C++26) (function template)sub_satβ saturating subtraction operation on two integers (C++26) (function template)div_satβ saturating division operation on two integers (C++26) (function template)saturate_castβ returns an integer value clamped to the range of a another integer type (C++26) (function template)clampβ clamps a value between a pair of boundary values (C++17) (function template)in_rangeβ checks if an integer value is in the range of a given integer type (C++20) (function template)minβ returns the smallest finite value of the given type[static](public static member function ofstd::numeric_limits<T>)maxβ returns the largest finite value of the given type[static](public static member function ofstd::numeric_limits<T>)
π External links π
- A branch-free implementation of saturation arithmetic β Locklessinc.com, 2012
that's the whole page, bestie π source/version: http://cppreference.com Β· date: 2024.06.10 Β· page: std::mul_sat(3) β mwah π«Ά
π¦ where this page comes from
- nixpkgs unstable: stdman 2024.07.05
- opensuse-leap 16.0: stdman 2024.07.05-bp160.1.9 Β· MIT
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms