Safe integer (e.g. i32) and floating point
(e.g. f32) numerics, and numeric concepts.
This namespace contains safe integer types and floating point types.
Safe numeric types:
- Signed integers:
i8,i16,i32,i64,isize. - Unsigned integers:
u8,u16,u32,u64,usize,uptr. - Floating point:
f32,f64. - Portability helper: [
CInt]
Additionally, there are Concepts that match against safe numerics, C++ primitive types, and operations with numeric types.
The Subspace library numeric types can interoperate with primitive C++ types, but are safer than primitive C++ types and eliminate many classes of bugs that often lead to security vulnerabilities:
- Integer overflow is not allowed by default (see Overflow behaviour), and will
panicto terminate the program. Intentional overflow can be achieved through methods likewrapping_addorsaturating_mul. TheOverflowIntegertype can be used for a series of potentially-overflowing operations and unwraps to an integer value if-and-only-if no overflow has occured. - Integers and floats convert implicitly into each other or into primitive
types only when no data can be lost, otherwise conversions do not
compile. To convert fallibly and observe data loss, use the
TryFromconcept methods, such asu32::try_from(3_i32). To do casting conversions with truncation, useCast. - No integer promotion. Math on 8-bit and 16-bit integers will not change their type, unlike primitive types which convert to (signed) int on any math operation.
- No Undefined Behaviour in conversions. Conversions between all numeric types, and between them and primitive types is well-defined for all possible values, unlike conversions between primitive integer and floating point types which can result in Undefined Behaviour.
The numeric types also come with builtin methods to perform common
operations, such as abs,
pow, log10, or
leading_ones.
Overflow behaviour
The default build configuration will panic on integer overflow in arithmetic
operations (+, -, *, /, etc). These checks can be disabled by
defining SUS_CHECK_INTEGER_OVERFLOW to false during compilation. Both
signed and unsigned integers will then overflow by performing wrapping
operations. There is no Undefined Behaviour with signed or unsigned integers
unless going through the unchecked operations explicitly, such as
unchecked_add.
Division by zero, or overflow in integer division will panic regardless of whether overflow checks are enabled.
Conversions
To explicitly invoke a lossless conversion, use
From. Use Into to
constrain inputs in generic code, and sus::into()
to type-deduce for conversions. Some lossless conversions are also allowed
to happen implicitly, though explicit conversion is better.
To convert and handle the case where data is lost, use
TryFrom, or
TryInto in generic code. Using
T::try_from(U).unwrap() is a quick way to convert and find out if the
value was out of range, or to terminate on malicious inputs. Or
T::try_from(U).unwrap_or_default() to convert to the input value or else
to zero.
To convert with truncation/loss of data, like static_cast, use
sus::cast<T>(). It can convert between
integers, floats, and enums, for both safe numerics and primitives. See
Casting numeric types for the rules of
conversion through cast.
Classes
-
An integer type that handles overflow instead of panicing.
-
The error type returned when a checked integral type conversion fails.
-
A 32-bit floating point type.
-
A 64-bit floating point type.
-
A 16-bit signed integer.
-
A 32-bit signed integer.
-
A 64-bit signed integer.
-
An 8-bit signed integer.
-
An address-sized signed integer.
-
A 16-bit unsigned integer.
-
A 32-bit unsigned integer.
-
A 64-bit unsigned integer.
-
An 8-bit unsigned integer.
-
A pointer-sized unsigned integer.
-
An address-sized unsigned integer.
Operators
-
-
Satisfies the
Remconcept for floating point values. -
Computes the remainder of
ldivided byr, also known as the modulo operation. -
Computes the remainder of
ldivided byr, also known as the modulo operation. -
Performs the bitwise operation
lANDr. -
Performs the bitwise operation
lANDr. -
-
Satisfies the
Mulconcept for floating point values. -
Computes
lmultiplied byr. -
Computes
lmultiplied byr. -
-
Satisfies the
Addconcept for floating point values. -
Computes
lsummed withr. -
Computes
lsummed withr. -
-
Satisfies the
Subconcept for floating point values. -
Computes
lsubtracted byr. -
Computes
lsubtracted byr. -
-
Satisfies the
Divconcept for floating point values. -
Computes
ldivided byr. -
Computes
ldivided byr. -
Satisfies the
Shlconcept for signed integers. -
Satisfies the
Shlconcept for unsigned integers. -
Satisfies the
PartialOrdconcept for floating point numbers. -
-
Returns the ordering of
landrfor signed integers. -
Returns the ordering of
landrfor unsigned integers. -
Satisfies the
Eqconcept for floating point numbers. -
-
Returns whether
landrare equal for signed integers. -
Returns whether
landrare equal for unsigned integers. -
Satisfies the
Shrconcept for signed integers. -
Satisfies the
Shrconcept for unsigned integers. -
Performs the bitwise operation
lXORr. -
Performs the bitwise operation
lXORr. -
Performs the bitwise operation
lORr. -
Performs the bitwise operation
lORr.
Concepts
-
-
-
-
-
-
-
-
-
-
-
-
-
All Subspace numeric integer types. This includes all safe integer types except
uptrwhich represents pointers. SeeUnsignedNumericandUnsignedPointer. -
All Subspace pointer integer types. This includes safe integer types that represent pointer values, which is just
uptr. SeeUnsignedNumericandUnsignedPointer. -
-
-
-
Enum types that are backed by a signed or unsigned value, excluding
enum classtypes. -
Enum class (scoped enumeration) types that are backed by a signed or unsigned value.
-
-
Signed or unsigned primitive integer types (
char,int,unsigned int,unsigned long, etc). -
-
-
-
-
-
-
Enum types that are backed by a signed value, excluding
enum classtypes. -
Enum class (scoped enumeration) types that are backed by a signed value.
-
Signed primitive integer types (
signed char,int,long, etc). -
-
-
Unsigned Subspace pointer integer types. This is the rest of
Unsignedthat is not included inUnsignedNumeric, which is justuptr. -
Enum types that are backed by an unsigned value, excluding
enum classtypes. -
Enum class (scoped enumeration) types that are backed by an unsigned value.
-
Unsigned primitive integer types (
unsigned char,unsigned int, etc).