base/numerics

This directory contains a dependency-free, header-only library of templates providing well-defined semantics for safely and performantly handling a variety of numeric operations, including most common arithmetic operations and conversions.

Common patterns and use-cases

The following covers the preferred style for the most common uses of this library. Please don't cargo-cult from anywhere else. 😉

Performing checked arithmetic type conversions

The checked_cast template converts between arbitrary arithmetic types, and is used for cases where a conversion failure should result in program termination:

// Crash if signed_value is out of range for buff_size.size_t buff_size = checked_cast<size_t>(signed_value);

Performing saturated (clamped) arithmetic type conversions

The saturated_cast template converts between arbitrary arithmetic types, and is used in cases where an out-of-bounds source value should be saturated to the corresponding maximum or minimum of the destination type:

// Convert from float with saturation to INT_MAX, INT_MIN, or 0 for NaN.int int_value = saturated_cast<int>(floating_point_value);

Enforcing arithmetic type conversions at compile-time

The strict_cast emits code that is identical to static_cast. However, provides static checks that will cause a compilation failure if the destination type cannot represent the full range of the source type:

// Throw a compiler error if byte_value is changed to an out-of-range-type.int int_value = strict_cast<int>(byte_value);

You can also enforce these compile-time restrictions on function parameters by using the StrictNumeric template:

// Throw a compiler error if the size argument cannot be represented by a// size_t (e.g. passing an int will fail to compile).boolAllocateBuffer(void** buffer,StrictCast<size_t> size);

Comparing values between arbitrary arithmetic types

Both the StrictNumeric and ClampedNumeric types provide well defined comparisons between arbitrary arithmetic types. This allows you to perform comparisons that are not legal or would trigger compiler warnings or errors under the normal arithmetic promotion rules:

Warning: Do not perform manual conversions using the comparison operators. Instead, use the cast templates described in the previous sections, or the constexpr template functions IsValueInRangeForNumericType and IsTypeInRangeForNumericType, as these templates properly handle the full range of corner cases and employ various optimizations.

Calculating a buffer size (checked arithmetic)

When making exact calculations—such as for buffer lengths—it's often necessary to know when those calculations trigger an overflow, undefined behavior, or other boundary conditions. The CheckedNumeric template does this by storing a bit determining whether or not some arithmetic operation has occured that would put the variable in an “invalid” state. Attempting to extract the value from a variable in an invalid state will trigger a check/trap condition, that by default will result in process termination.

Here's an example of a buffer calculation using a CheckedNumeric type (note: the AssignIfValid method will trigger a compile error if the result is ignored).

The ClampedNumeric type is not “sticky”, which means the saturation is not retained across individual operations. As such, one arithmetic operation may result in a saturated value, while the next operation may then “desaturate” the value. Here's an example:

ClampedNumeric<int> value = INT_MAX;++value;// value is still INT_MAX, due to saturation.--value;// value is now (INT_MAX - 1), because saturation is not sticky.

Conversion functions and StrictNumeric<> in safe_conversions.h

This header includes a collection of helper constexpr templates for safely performing a range of conversions, assignments, and tests.

Safe casting templates

as_signed() - Returns the supplied integral value as a signed type of the same width.

as_unsigned() - Returns the supplied integral value as an unsigned type of the same width.

checked_cast<>() - Analogous to static_cast<> for numeric types, except that by default it will trigger a crash on an out-of-bounds conversion (e.g. overflow, underflow, NaN to integral) or a compile error if the conversion error can be detected at compile time. The crash handler can be overridden to perform a behavior other than crashing.

saturated_cast<>() - Analogous to static_cast for numeric types, except that it returns a saturated result when the specified numeric conversion would otherwise overflow or underflow. An NaN source returns 0 by default, but can be overridden to return a different result.

strict_cast<>() - Analogous to static_cast for numeric types, except this causes a compile failure if the destination type is not large enough to contain any value in the source type. It performs no runtime checking and thus introduces no runtime overhead.

Other helper and conversion functions

IsValueInRangeForNumericType<>() - A convenience function that returns true if the type supplied as the template parameter can represent the value passed as an argument to the function.

IsTypeInRangeForNumericType<>() - A convenience function that evaluates entirely at compile-time and returns true if the destination type (first template parameter) can represent the full range of the source type (second template parameter).

IsValueNegative() - A convenience function that will accept any arithmetic type as an argument and will return whether the value is less than zero. Unsigned types always return false.

SafeUnsignedAbs() - Returns the absolute value of the supplied integer parameter as an unsigned result (thus avoiding an overflow if the value is the signed, two's complement minimum).

StrictNumeric<>

StrictNumeric<> is a wrapper type that performs assignments and copies via the strict_cast template, and can perform valid arithmetic comparisons across any range of arithmetic types. StrictNumeric is the return type for values extracted from a CheckedNumeric class instance. The raw numeric value is extracted via static_cast to the underlying type or any type with sufficient range to represent the underlying type.

MakeStrictNum() - Creates a new StrictNumeric from the underlying type of the supplied arithmetic or StrictNumeric type.

SizeT - Alias for StrictNumeric<size_t>.

CheckedNumeric<> in checked_math.h

CheckedNumeric<> implements all the logic and operators for detecting integer boundary conditions such as overflow, underflow, and invalid conversions. The CheckedNumeric type implicitly converts from floating point and integer data types, and contains overloads for basic arithmetic operations (i.e.: +, -, *, / for all types and %, <<, >>, &, |, ^ for integers). However, the variadic template functions are the prefered API, as they remove type ambiguities and help prevent a number of common errors. The variadic functions can also be more performant, as they eliminate redundant expressions that are unavoidable with the with the operator overloads. (Ideally the compiler should optimize those away, but better to avoid them in the first place.)

Type promotions are a slightly modified version of the standard C/C++ numeric promotions with the two differences being that there is no default promotion to int and bitwise logical operations always return an unsigned of the wider type.

Members

The unary negation, increment, and decrement operators are supported, along with the following unary arithmetic methods, which return a new CheckedNumeric as a result of the operation:

Max() - Returns whichever is greater of the current instance or argument. The underlying return type is whichever has the greatest magnitude.

Min() - Returns whichever is lowest of the current instance or argument. The underlying return type is whichever has can represent the lowest number in the smallest width (e.g. int8_t over unsigned, int over int8_t, and float over int).

The following are for converting CheckedNumeric instances:

type - The underlying numeric type.

AssignIfValid() - Assigns the underlying value to the supplied destination pointer if the value is currently valid and within the range supported by the destination type. Returns true on success.

Cast<>() - Instance method returning a CheckedNumeric derived from casting the current instance to a CheckedNumeric of the supplied destination type.

The following member functions return a StrictNumeric, which is valid for comparison and assignment operations, but will trigger a compile failure on attempts to assign to a type of insufficient range. The underlying value can be extracted by an explicit static_cast to the underlying type or any type with sufficient range to represent the underlying type.

IsValid() - Returns true if the underlying numeric value is valid (i.e. has not wrapped or saturated and is not the result of an invalid conversion).

ValueOrDie() - Returns the underlying value. If the state is not valid this call will trigger a crash by default (but may be overridden by supplying an alternate handler to the template).

ValueOrDefault() - Returns the current value, or the supplied default if the state is not valid (but will not crash).

Comparison operators are explicitly not provided for CheckedNumeric types because they could result in a crash if the type is not in a valid state. Patterns like the following should be used instead:

The following wrapper functions can be used to avoid the template disambiguator syntax when converting a destination type.

IsValidForType<>() in place of: a.template IsValid<>()

ValueOrDieForType<>() in place of: a.template ValueOrDie<>()

ValueOrDefaultForType<>() in place of: a.template ValueOrDefault<>()

The following general utility methods is are useful for converting from arithmetic types to CheckedNumeric types:

MakeCheckedNum() - Creates a new CheckedNumeric from the underlying type of the supplied arithmetic or directly convertible type.

ClampedNumeric<> in clamped_math.h

ClampedNumeric<> implements all the logic and operators for clamped (non-sticky saturating) arithmetic operations and conversions. The ClampedNumeric type implicitly converts back and forth between floating point and integer data types, saturating on assignment as appropriate. It contains overloads for basic arithmetic operations (i.e.: +, -, *, / for all types and %, <<, >>, &, |, ^ for integers) along with comparison operators for arithmetic types of any size. However, the variadic template functions are the prefered API, as they remove type ambiguities and help prevent a number of common errors. The variadic functions can also be more performant, as they eliminate redundant expressions that are unavoidable with the operator overloads. (Ideally the compiler should optimize those away, but better to avoid them in the first place.)

Type promotions are a slightly modified version of the standard C/C++ numeric promotions with the two differences being that there is no default promotion to int and bitwise logical operations always return an unsigned of the wider type.

Most arithmetic operations saturate normally, to the numeric limit in the direction of the sign. The potentially unusual cases are:

Division: Division by zero returns the saturated limit in the direction of sign of the dividend (first argument). The one exception is 0/0, which returns zero (although logically is NaN).

Modulus: Division by zero returns the dividend (first argument).

Left shift: Non-zero values saturate in the direction of the signed limit (max/min), even for shifts larger than the bit width. 0 shifted any amount results in 0.

Right shift: Negative values saturate to -1. Positive or 0 saturates to 0. (Effectively just an unbounded arithmetic-right-shift.)

Max() - Returns whichever is greater of the current instance or argument. The underlying return type is whichever has the greatest magnitude.

Min() - Returns whichever is lowest of the current instance or argument. The underlying return type is whichever has can represent the lowest number in the smallest width (e.g. int8_t over unsigned, int over int8_t, and float over int).

The following are for converting ClampedNumeric instances:

type - The underlying numeric type.

RawValue() - Returns the raw value as the underlying arithmetic type. This is useful when e.g. assigning to an auto type or passing as a deduced template parameter.

Cast<>() - Instance method returning a ClampedNumeric derived from casting the current instance to a ClampedNumeric of the supplied destination type.