|
fixed_point128 1.0.1.0
128 bit integer, fixed point and floating point arithmetic for C++20
|
128 bit integer class, signed or unsigned depending on the template parameter. More...
#include <int128_shared.h>
Public Types | |
| typedef int128_base | type |
| Self type alias. | |
| typedef int128_base * | ptr_type |
| Pointer type alias. | |
| typedef int128_base & | ref_type |
| Reference type alias. | |
Public Member Functions | |
| constexpr | int128_base () noexcept |
| Default constructor, creates an instance with a value of zero. Explicitly zeroing both QWORDs rather than defaulting the constructor keeps the type usable in a constant expression, which a default initialized object with indeterminate members is not. | |
| constexpr | int128_base (const int128_base &rhs) noexcept |
| Copy constructor. | |
| constexpr | int128_base (int128_base &&rhs) noexcept |
| Move constructor Doesn't modify the right hand side object. Acts like a copy constructor. | |
| constexpr | int128_base (double x) noexcept |
| Constructor from the double type Underflow goes to zero. Overflow, NaN and +-INF go to the largest supported magnitude of the matching sign. For the unsigned type a negative value wraps around via 2's complement, matching both the integer constructors and the way builtin unsigned types behave. For example -5.0 yields 2^128 - 5. | |
| template<typename T> requires std::is_integral_v<T> | |
| constexpr | int128_base (T x) noexcept |
| Constructor from any builtin integral type. | |
| int128_base (const char *x) noexcept | |
| Constructor from const char* (C string). Allows creating 128 bit values from a string. Much slower than the other constructors. Input string can be decimal or hex. Hex initialization is faster. Commas and apostrophes are ignored, so both conventional digit grouping and the C++ digit separator are accepted, the latter being what reaches the type's user defined literals. A nullptr, an empty string or a leading illegal character all produce zero; parsing otherwise stops at the first character that isn't a digit of the detected base. Overflow wraps around silently, like the builtin integer types. An optional leading '+' or '-' is accepted. For the unsigned type a negative value wraps around via 2's complement, so "-5" and -5.0 both produce 2^128 - 5, matching strtoull. Never throws: an allocation failure produces zero. | |
| int128_base (const std::string &x) noexcept | |
| Constructor from std::string. Allows creating very high precision values. Much slower than the other constructors. | |
| constexpr | int128_base (uint64_t l, uint64_t h) noexcept |
| Constructor from the 2 int128_base elements, useful for creating special constants. | |
| constexpr | ~int128_base () noexcept=default |
| Destructor. | |
| constexpr int128_base & | operator= (const int128_base &rhs) noexcept |
| Assignment operator Assigns the two QWORDs individually rather than being defaulted; see the copy constructor for why the width of the copy matters. | |
| constexpr int128_base & | operator= (int128_base &&rhs) noexcept |
| Move assignment operator Doesn't modify the right hand side object. Acts like a copy assignment operator. | |
| template<typename T> | |
| constexpr int128_base & | operator= (const T &rhs) noexcept |
| Assignment operator. | |
| constexpr | operator uint64_t () const noexcept |
| operator uint64_t - converts to a uint64_t | |
| constexpr | operator int64_t () const noexcept |
| operator int64_t - converts to a int64_t | |
| constexpr | operator uint32_t () const noexcept |
| operator uint32_t - converts to a uint32_t | |
| constexpr | operator int32_t () const noexcept |
| operator int32_t - converts to a int32_t | |
| constexpr | operator float () const noexcept |
| operator float - converts to a float The signed range fits within a float's exponent, so no signed value overflows. Unsigned values above FLT_MAX become infinity, as they do with the builtin unsigned conversions: the carry out of the rounding step below pushes the exponent to its all ones encoding. | |
| constexpr | operator double () const noexcept |
| operator double - converts to a double The whole 128 bit range is within reach of a double's exponent, so no value overflows. | |
| constexpr | operator long double () const noexcept |
| operator long double - converts to a long double | |
| char * | to_string () const noexcept |
| Converts to a char* (slow) string holds all meaningful fraction bits. | |
| operator std::string () const | |
| Converts to a std::string (slow) string holds all meaningful fraction bits. | |
| operator char * () const noexcept | |
| Converts to a C string (slow) string holds all meaningful fraction bits. The returned string is a statically, thread-allocated buffer. Additional calls to this function from the same thread, overwrite the previous result. | |
| constexpr int128_base | operator>> (int32_t shift) const noexcept |
| Performs right shift operation. | |
| constexpr int128_base | operator<< (int32_t shift) const noexcept |
| Performs left shift operation. | |
| constexpr int128_base & | operator+= (const int128_base &rhs) noexcept |
| Add a value to this object like other builtin integer types, overflow wraps around silently. | |
| template<typename T> | |
| constexpr int128_base & | operator+= (const T &rhs) noexcept |
| Add a value to this object like other builtin integer types, overflow wraps around silently. | |
| constexpr int128_base & | operator-= (const int128_base &rhs) noexcept |
| Subtract a value from this object like other builtin integer types, underflow wraps around silently. | |
| template<typename T> | |
| constexpr int128_base & | operator-= (const T &rhs) noexcept |
| Subtract a value from this object like other builtin integer types, underflow wraps around silently. | |
| constexpr int128_base & | operator*= (const int128_base &rhs) noexcept |
| Multiplies a value to this object. | |
| constexpr int128_base & | square () noexcept |
| Squares this object in place. | |
| template<typename T> requires std::is_arithmetic_v<T> | |
| constexpr int128_base & | operator*= (T x) noexcept |
| Multiplies a value to this object. | |
| int128_base & | operator/= (const int128_base &rhs_in) |
| Divide this object by rhs. | |
| template<typename T> | |
| int128_base & | operator/= (T x) |
| Divide this object by x. | |
| int128_base & | operator%= (const int128_base &rhs) |
| %= operator | |
| template<typename T> | |
| int128_base & | operator%= (T x) |
| %= operator | |
| constexpr int128_base & | operator>>= (int32_t shift) noexcept |
| Shift right this object. The signed type shifts arithmetically, replicating the sign bit; the unsigned type shifts logically. Both are done without rounding, to match int64_t and uint64_t. | |
| constexpr int128_base & | operator<<= (int32_t shift) noexcept |
| Shift left this object. | |
| constexpr int128_base & | operator&= (const int128_base &rhs) noexcept |
| Bitwise AND=. | |
| template<typename T> | |
| constexpr int128_base & | operator&= (const T &rhs) noexcept |
| Bitwise AND=. | |
| constexpr int128_base & | operator|= (const int128_base &rhs) noexcept |
| Bitwise OR=. | |
| template<typename T> | |
| constexpr int128_base & | operator|= (const T &rhs) noexcept |
| Bitwise OR=. | |
| constexpr int128_base & | operator^= (const int128_base &rhs) noexcept |
| Bitwise XOR=. | |
| template<typename T> | |
| constexpr int128_base & | operator^= (const T &rhs) noexcept |
| Bitwise XOR=. | |
| constexpr int128_base & | operator++ () noexcept |
| Prefix ++ operation (++a) | |
| constexpr int128_base | operator++ (int32_t) noexcept |
| Postfix ++ operation (a++) | |
| constexpr int128_base & | operator-- () noexcept |
| Prefix – operation (–a) | |
| constexpr int128_base | operator-- (int32_t) noexcept |
| Postfix – operation (a–) | |
| constexpr | operator bool () const noexcept |
| Convert to bool. | |
| constexpr bool | operator! () const noexcept |
| Logical not (!). Opposite of operator bool. | |
| constexpr int128_base | operator~ () const noexcept |
| Bitwise not (~). | |
| constexpr int128_base | operator+ () const noexcept |
| Unary +. Returns a copy of the object. | |
| constexpr int128_base | operator- () const noexcept |
| Unary -. Returns a copy of the object with sign inverted. Performs a 2's complement operation just like the native integer types. | |
| constexpr bool | operator== (const int128_base &rhs) const noexcept |
| Compare logical/bitwise equal. | |
| template<typename T> | |
| constexpr bool | operator== (const T &rhs) const noexcept |
| Compare logical/bitwise equal. | |
| constexpr bool | operator!= (const int128_base &rhs) const noexcept |
| Return true when objects are not equal. Can be used as logical XOR. | |
| template<typename T> | |
| constexpr bool | operator!= (const T &rhs) const noexcept |
| Return true when objects are not equal. Can be used as logical XOR. | |
| constexpr bool | operator< (const int128_base &rhs) const noexcept |
| Return true if this object is small than the rhs. | |
| template<typename T> | |
| constexpr bool | operator< (const T &rhs) const noexcept |
| Return true if this object is small than the rhs. | |
| constexpr bool | operator<= (const int128_base &rhs) const noexcept |
| Return true this object is small or equal than the rhs. | |
| template<typename T> | |
| constexpr bool | operator<= (const T &rhs) const noexcept |
| Return true this object is small or equal than the rhs. | |
| constexpr bool | operator> (const int128_base &rhs) const noexcept |
| Return true this object is larger than the rhs. | |
| template<typename T> | |
| constexpr bool | operator> (const T &rhs) const noexcept |
| Return true this object is larger than the rhs. | |
| constexpr bool | operator>= (const int128_base &rhs) const noexcept |
| Return true this object is larger or equal than the rhs. | |
| template<typename T> | |
| constexpr bool | operator>= (const T &rhs) const noexcept |
| Return true this object is larger or equal than the rhs. | |
| constexpr bool | is_int () const noexcept |
| Returns true if the value is an int (fraction is zero) | |
| constexpr bool | is_positive () const noexcept |
| Returns true if the value is positive (including zero) Always true for the unsigned type, which lets the sign tests in the shared code fold away. | |
| constexpr bool | is_negative () const noexcept |
| Returns true if the value negative (smaller than zero) Always false for the unsigned type, which lets the sign tests in the shared code fold away. | |
| constexpr bool | is_zero () const noexcept |
| Returns true if the value is zero. | |
| constexpr int32_t | get_bit (uint32_t bit) const noexcept |
| get a specific bit within the 128 data | |
| constexpr void | get_components (uint64_t &l, uint64_t &h) const noexcept |
| Extracts the low and high QWORD. | |
| char * | hex () const noexcept |
| Converts this object to a hex C string. The returned string is a statically thread-allocated buffer. Additional calls to this function from the same thread overwrite the previous result. | |
Static Public Member Functions | |
| static constexpr int128_base | one () noexcept |
| Return an instance of int128_base with the value of 1. | |
Static Public Attributes | |
| static constexpr bool | is_signed = IsSigned |
| True when the msb of the high QWORD is a sign bit. | |
Friends | |
| class | fp128_gtest |
| template<typename T> | |
| constexpr int128_base | operator+ (int128_base lhs, const T &rhs) noexcept |
| Adds 2 values and returns the result. | |
| template<typename T> | |
| constexpr int128_base | operator- (int128_base lhs, const T &rhs) noexcept |
| Subtracts 2 values and returns the result. | |
| template<typename T> requires requires(int128_base l, const T& r) { l *= r; } | |
| constexpr int128_base | operator* (int128_base lhs, const T &rhs) noexcept |
| Multiplies 2 values and returns the result. | |
| template<typename T> | |
| int128_base | operator/ (int128_base lhs, const T &rhs) |
| Divides 2 values and returns the result. | |
| template<typename T> | |
| int128_base | operator% (int128_base lhs, const T &rhs) |
| Performs modulo and returns the result. | |
| template<typename T> | |
| constexpr int128_base | operator& (int128_base lhs, const T &rhs) noexcept |
| Performs bitwise AND (&). | |
| template<typename T> | |
| constexpr int128_base | operator| (int128_base lhs, const T &rhs) noexcept |
| Performs bitwise OR (|). | |
| template<typename T> | |
| constexpr int128_base | operator^ (int128_base lhs, const T &rhs) noexcept |
| Performs bitwise XOR (^). | |
| template<typename T> requires std::is_arithmetic_v<T> | |
| constexpr int128_base | operator+ (const T &lhs, const int128_base &rhs) noexcept |
| Adds a scalar and an int128_base, in that order. | |
| template<typename T> requires std::is_arithmetic_v<T> | |
| constexpr int128_base | operator- (const T &lhs, const int128_base &rhs) noexcept |
| Subtracts an int128_base from a scalar. | |
| template<typename T> requires std::is_arithmetic_v<T> | |
| constexpr int128_base | operator* (const T &lhs, const int128_base &rhs) noexcept |
| Multiplies a scalar and an int128_base, in that order. | |
| template<typename T> requires std::is_arithmetic_v<T> | |
| int128_base | operator/ (const T &lhs, const int128_base &rhs) |
| Divides a scalar by an int128_base. | |
| template<typename T> requires std::is_arithmetic_v<T> | |
| int128_base | operator% (const T &lhs, const int128_base &rhs) |
| Performs modulo of a scalar by an int128_base. | |
| template<typename T> requires std::is_arithmetic_v<T> | |
| constexpr int128_base | operator& (const T &lhs, const int128_base &rhs) noexcept |
| Performs bitwise AND of a scalar and an int128_base. | |
| template<typename T> requires std::is_arithmetic_v<T> | |
| constexpr int128_base | operator| (const T &lhs, const int128_base &rhs) noexcept |
| Performs bitwise OR of a scalar and an int128_base. | |
| template<typename T> requires std::is_arithmetic_v<T> | |
| constexpr int128_base | operator^ (const T &lhs, const int128_base &rhs) noexcept |
| Performs bitwise XOR of a scalar and an int128_base. | |
| template<typename T> requires std::is_arithmetic_v<T> | |
| constexpr int128_base | operator<< (const T &lhs, const int128_base &rhs) noexcept |
| Shifts a scalar left by an int128_base shift count. | |
| template<typename T> requires std::is_arithmetic_v<T> | |
| constexpr int128_base | operator>> (const T &lhs, const int128_base &rhs) noexcept |
| Shifts a scalar right by an int128_base shift count. The left operand is widened to int128_base first, see operator<< above. | |
| template<typename T> requires std::is_arithmetic_v<T> | |
| constexpr bool | operator== (const T &lhs, const int128_base &rhs) noexcept |
| Compares a scalar and an int128_base for equality. | |
| template<typename T> requires std::is_arithmetic_v<T> | |
| constexpr bool | operator!= (const T &lhs, const int128_base &rhs) noexcept |
| Compares a scalar and an int128_base for inequality. | |
| template<typename T> requires std::is_arithmetic_v<T> | |
| constexpr bool | operator< (const T &lhs, const int128_base &rhs) noexcept |
| Returns true when a scalar is smaller than an int128_base. | |
| template<typename T> requires std::is_arithmetic_v<T> | |
| constexpr bool | operator<= (const T &lhs, const int128_base &rhs) noexcept |
| Returns true when a scalar is smaller or equal to an int128_base. | |
| template<typename T> requires std::is_arithmetic_v<T> | |
| constexpr bool | operator> (const T &lhs, const int128_base &rhs) noexcept |
| Returns true when a scalar is larger than an int128_base. | |
| template<typename T> requires std::is_arithmetic_v<T> | |
| constexpr bool | operator>= (const T &lhs, const int128_base &rhs) noexcept |
| Returns true when a scalar is larger or equal to an int128_base. | |
| constexpr int128_base | abs (const int128_base &x) noexcept |
| Calculates the absolute value of x. Only declared for the signed instantiation. An unqualified abs() on an unsigned value keeps resolving the way it does today. | |
| constexpr int128_base | sqr (int128_base x) noexcept |
| Calculates the square of a value. i.e. x^2. | |
| constexpr uint64_t | lzcnt128 (const int128_base &x) noexcept |
| Calculates the left zero count of value x. | |
| uint64_t | sqrt (const int128_base &x) noexcept |
| Calculates the square root using Newton's method. Based on the book "Math toolkit for real time programming" by Jack W. Crenshaw. | |
| constexpr uint64_t | log2 (const int128_base &x) |
| Calculates the Log base 2 of x: log2(x) Rounding is always towards zero so the maximum error is close to 1. | |
| uint64_t | log (const int128_base &x) |
| Calculates the natural Log (base e) of x: log(x), rounded down to the nearest integer. | |
| uint64_t | log10 (const int128_base &x) |
| Calculates Log base 10 of x: log10(x), rounded down to the nearest integer. | |
| constexpr int128_base | pow (const int128_base &x, uint32_t y) noexcept |
| Calculates x to the power of y. | |
128 bit integer class, signed or unsigned depending on the template parameter.
This class implements the standard operators a builtin integer type has.
All of int128_base's methods are inline for maximum performance.
Both instantiations store the same two QWORDs and only differ where the interpretation of the msb matters, which is why a single template covers them. The signed instantiation uses a two's complement representation, so addition, subtraction, multiplication, the bitwise operators, the left shift and equality are bit identical between the two and are written once, without a test on the signedness.
Implementation notes:
Compile time evaluation:
Everything that stays within integer arithmetic is constexpr:
The bit counting and extended arithmetic intrinsics these rest on are not constant expressions, so fp128_shared.h wraps each one in a constexpr function that serves a constant evaluated call from a portable implementation of the same operation. A runtime call still reaches the bare intrinsic and generates the same code it did before.
The rest cannot be constexpr, for one of two reasons:
| IsSigned | True for a two's complement signed type, false for an unsigned one. |
Definition at line 117 of file int128_shared.h.
| typedef int128_base fp128::int128_base< IsSigned >::type |
Self type alias.
Definition at line 261 of file int128_shared.h.
| typedef int128_base* fp128::int128_base< IsSigned >::ptr_type |
Pointer type alias.
Definition at line 262 of file int128_shared.h.
| typedef int128_base& fp128::int128_base< IsSigned >::ref_type |
Reference type alias.
Definition at line 263 of file int128_shared.h.
|
inlineconstexprnoexcept |
Default constructor, creates an instance with a value of zero. Explicitly zeroing both QWORDs rather than defaulting the constructor keeps the type usable in a constant expression, which a default initialized object with indeterminate members is not.
Definition at line 274 of file int128_shared.h.
|
inlineconstexprnoexcept |
Copy constructor.
Copies the two QWORDs individually instead of being defaulted. The difference is not stylistic: a defaulted copy is trivial, and MSVC implements a trivial 16 byte copy with a single vmovups once /arch:AVX2 is on. Every operator here produces its result as two QWORD stores into a stack temporary, so that one wide load overlaps both of them, fails store to load forwarding, and stalls for roughly 15 cycles waiting for them to reach L1. It lands on any expression whose 128 bit result goes to memory - *out = *a * *b is enough.
Spelling the copy out member by member keeps every store and load 8 bytes wide, which forwards. Measured on MSVC over 64K element arrays, interleaving the two builds so that drift hits both equally: 1.7x on sqrt(), 1.4x on division by a 128 bit value, 1.05x on addition, and no measurable change to multiplication, log10(), pow() or division by a 64 bit integer. Nothing measured slower.
The type stops being trivially copyable, so std::bit_cast no longer accepts it and a container of these relocates with a copy loop rather than a memmove - which at this size is not a loss either: growing a std::vector of them measured 1.35x faster. fixed_point128 and float128 have always been written this way, for the same reason.
| rhs | Object to copy from. |
Definition at line 298 of file int128_shared.h.
|
inlineconstexprnoexcept |
Move constructor Doesn't modify the right hand side object. Acts like a copy constructor.
| rhs | Object to copy from. |
Definition at line 304 of file int128_shared.h.
|
inlineconstexprnoexcept |
Constructor from the double type Underflow goes to zero. Overflow, NaN and +-INF go to the largest supported magnitude of the matching sign.
For the unsigned type a negative value wraps around via 2's complement, matching both the integer constructors and the way builtin unsigned types behave. For example -5.0 yields 2^128 - 5.
| x | Input value |
Definition at line 314 of file int128_shared.h.
|
inlineconstexprnoexcept |
Constructor from any builtin integral type.
A single constrained template is used instead of one overload per fixed width type. Overloads would only cover the types the fixed width aliases happen to name on a given platform, leaving the rest ambiguous: on MSVC, long and unsigned long are distinct from both int and long long, so int128_base(1ul) could not pick a best match. Which of long and long long int64_t names also differs between LP64 and LLP64, so no fixed set of overloads covers every platform.
Signed values are sign extended, unsigned values are zero extended. For the unsigned type a negative value therefore wraps around via 2's complement, exactly like the builtin unsigned types do.
| T | Any builtin integral type (including bool and the character types) |
| x | Input value |
Definition at line 390 of file int128_shared.h.
|
inlinenoexcept |
Constructor from const char* (C string). Allows creating 128 bit values from a string. Much slower than the other constructors.
Input string can be decimal or hex. Hex initialization is faster. Commas and apostrophes are ignored, so both conventional digit grouping and the C++ digit separator are accepted, the latter being what reaches the type's user defined literals. A nullptr, an empty string or a leading illegal character all produce zero; parsing otherwise stops at the first character that isn't a digit of the detected base. Overflow wraps around silently, like the builtin integer types.
An optional leading '+' or '-' is accepted. For the unsigned type a negative value wraps around via 2's complement, so "-5" and -5.0 both produce 2^128 - 5, matching strtoull.
Never throws: an allocation failure produces zero.
| x | Input string |
Definition at line 407 of file int128_shared.h.
|
inlinenoexcept |
Constructor from std::string. Allows creating very high precision values. Much slower than the other constructors.
| x | Input string |
Definition at line 487 of file int128_shared.h.
|
inlineconstexprnoexcept |
Constructor from the 2 int128_base elements, useful for creating special constants.
| l | Low QWORD |
| h | High QWORD |
Definition at line 493 of file int128_shared.h.
|
constexprdefaultnoexcept |
Destructor.
|
inlineconstexprnoexcept |
Assignment operator Assigns the two QWORDs individually rather than being defaulted; see the copy constructor for why the width of the copy matters.
| rhs | Value to copy from |
Definition at line 505 of file int128_shared.h.
|
inlineconstexprnoexcept |
Move assignment operator Doesn't modify the right hand side object. Acts like a copy assignment operator.
| rhs | Value to copy from |
Definition at line 517 of file int128_shared.h.
|
inlineconstexprnoexcept |
Assignment operator.
| rhs | Value to copy from |
Definition at line 528 of file int128_shared.h.
|
inlinenodiscardconstexprnoexcept |
operator uint64_t - converts to a uint64_t
Definition at line 541 of file int128_shared.h.
|
inlinenodiscardconstexprnoexcept |
operator int64_t - converts to a int64_t
Definition at line 546 of file int128_shared.h.
|
inlinenodiscardconstexprnoexcept |
operator uint32_t - converts to a uint32_t
Definition at line 551 of file int128_shared.h.
|
inlinenodiscardconstexprnoexcept |
operator int32_t - converts to a int32_t
Definition at line 556 of file int128_shared.h.
|
inlinenodiscardconstexprnoexcept |
operator float - converts to a float The signed range fits within a float's exponent, so no signed value overflows. Unsigned values above FLT_MAX become infinity, as they do with the builtin unsigned conversions: the carry out of the rounding step below pushes the exponent to its all ones encoding.
Definition at line 564 of file int128_shared.h.
|
inlinenodiscardconstexprnoexcept |
operator double - converts to a double The whole 128 bit range is within reach of a double's exponent, so no value overflows.
Definition at line 599 of file int128_shared.h.
|
inlinenodiscardconstexprnoexcept |
operator long double - converts to a long double
Definition at line 633 of file int128_shared.h.
|
inlinenodiscardnoexcept |
Converts to a char* (slow) string holds all meaningful fraction bits.
Definition at line 638 of file int128_shared.h.
|
inlinenodiscard |
Converts to a std::string (slow) string holds all meaningful fraction bits.
Definition at line 643 of file int128_shared.h.
|
inlineexplicitnodiscardnoexcept |
Converts to a C string (slow) string holds all meaningful fraction bits. The returned string is a statically, thread-allocated buffer. Additional calls to this function from the same thread, overwrite the previous result.
Definition at line 651 of file int128_shared.h.
|
inlinenodiscardconstexprnoexcept |
Performs right shift operation.
| shift | bits to shift |
Definition at line 718 of file int128_shared.h.
|
inlinenodiscardconstexprnoexcept |
Performs left shift operation.
| shift | bits to shift |
Definition at line 728 of file int128_shared.h.
|
inlineconstexprnoexcept |
Add a value to this object like other builtin integer types, overflow wraps around silently.
| rhs | Right hand side operand |
Definition at line 739 of file int128_shared.h.
|
inlineconstexprnoexcept |
Add a value to this object like other builtin integer types, overflow wraps around silently.
| rhs | Right hand side operand |
Definition at line 751 of file int128_shared.h.
|
inlineconstexprnoexcept |
Subtract a value from this object like other builtin integer types, underflow wraps around silently.
| rhs | Right hand side operand |
Definition at line 758 of file int128_shared.h.
|
inlineconstexprnoexcept |
Subtract a value from this object like other builtin integer types, underflow wraps around silently.
| rhs | Right hand side operand |
Definition at line 775 of file int128_shared.h.
|
inlineconstexprnoexcept |
Multiplies a value to this object.
The same code serves both instantiations. A truncated 128 bit product is a multiplication modulo 2^128, and in that ring the two's complement bit pattern of a negative value is its value, so multiplying the raw bits gives the signed result without converting the operands to magnitudes and reapplying the sign. int128_base::square() relies on the same identity.
| rhs | Right hand side operand |
Definition at line 787 of file int128_shared.h.
|
inlineconstexprnoexcept |
Squares this object in place.
Cheaper than operator*=(*this): a square is symmetric, so the low*high and high*low cross products are the same value. Computing it once and doubling it replaces one of the three multiplies with an addition. The temporary copy that operator*= needs to guard against aliasing is not needed either.
The result is truncated to 128 bit and is bit identical to (*this) * (*this), which for the signed type means it is negative in the same overflow cases.
Definition at line 815 of file int128_shared.h.
|
inlineconstexprnoexcept |
Multiplies a value to this object.
A non negative operand that fits in 64 bit skips one of the three partial products. It is valid for both instantiations for the same reason operator*=(const int128_base&) is: the product is taken modulo 2^128, where the two's complement bits of this object are its value.
Restricted to the arithmetic types. Any other operand used to reach the static_cast to uint64_t below through a conversion operator and was silently truncated: the other instantiation (int128_t(3) * uint128_t(2^64 + 1) gave 3) and float128 among them. Such an operand now has to convert to int128_base implicitly or the expression does not compile, as is already the case for the other mixed arithmetic operators.
| x | Right hand side operand |
Definition at line 845 of file int128_shared.h.
|
inline |
Divide this object by rhs.
Rounds towards zero, matching the builtin integer types: -7 / 2 is -3, not -4. The two operands are reduced to magnitudes, divided as unsigned values (which truncates), and the sign is applied afterwards, which is exactly truncation towards zero. Every sign related step below folds away for the unsigned instantiation.
| rhs_in | Right hand side operator (denominator) |
Definition at line 879 of file int128_shared.h.
|
inline |
Divide this object by x.
The unsigned type keeps a dedicated path for a divisor that fits in 64 bit, which writes the quotient straight into this object. The signed type delegates to the int128_base overload instead: the shortcut for a small numerator relies on an unsigned comparison, which would zero every negative value, and the overload it defers to already special cases a divisor that fits in 64 bit.
| x | Right hand side operator (denominator) |
Definition at line 969 of file int128_shared.h.
|
inline |
%= operator
For the signed type the remainder takes the sign of the dividend, matching the builtin integer types: -7 % 2 is -1. It is derived from the quotient so the two can never disagree. The unsigned type gets the remainder straight out of the division instead, which is cheaper.
| rhs | Modulo operand. |
Definition at line 1040 of file int128_shared.h.
|
inline |
%= operator
| x | Modulo operand. |
Definition at line 1090 of file int128_shared.h.
|
inlineconstexprnoexcept |
Shift right this object. The signed type shifts arithmetically, replicating the sign bit; the unsigned type shifts logically. Both are done without rounding, to match int64_t and uint64_t.
| shift | Bits to shift. Zero and negative values leave the object unchanged. Values of 128 and above produce zero, or -1 for a negative value of the signed type. |
Definition at line 1099 of file int128_shared.h.
|
inlineconstexprnoexcept |
Shift left this object.
| shift | Bits to shift. Zero and negative values leave the object unchanged, values of 128 and above produce zero. |
Definition at line 1144 of file int128_shared.h.
|
inlineconstexprnoexcept |
Bitwise AND=.
| rhs | AND mask. |
Definition at line 1167 of file int128_shared.h.
|
inlineconstexprnoexcept |
Bitwise AND=.
| rhs | AND mask. |
Definition at line 1178 of file int128_shared.h.
|
inlineconstexprnoexcept |
Bitwise OR=.
| rhs | OR mask. |
Definition at line 1184 of file int128_shared.h.
|
inlineconstexprnoexcept |
Bitwise OR=.
| rhs | OR mask. |
Definition at line 1195 of file int128_shared.h.
|
inlineconstexprnoexcept |
Bitwise XOR=.
| rhs | XOR mask. |
Definition at line 1201 of file int128_shared.h.
|
inlineconstexprnoexcept |
Bitwise XOR=.
| rhs | XOR mask. |
Definition at line 1212 of file int128_shared.h.
|
inlineconstexprnoexcept |
|
inlineconstexprnoexcept |
|
inlineconstexprnoexcept |
|
inlineconstexprnoexcept |
|
inlinenodiscardconstexprnoexcept |
Convert to bool.
Definition at line 1258 of file int128_shared.h.
|
inlinenodiscardconstexprnoexcept |
Logical not (!). Opposite of operator bool.
Definition at line 1262 of file int128_shared.h.
|
inlinenodiscardconstexprnoexcept |
Bitwise not (~).
Definition at line 1266 of file int128_shared.h.
|
inlinenodiscardconstexprnoexcept |
Unary +. Returns a copy of the object.
Definition at line 1276 of file int128_shared.h.
|
inlinenodiscardconstexprnoexcept |
Unary -. Returns a copy of the object with sign inverted. Performs a 2's complement operation just like the native integer types.
Definition at line 1285 of file int128_shared.h.
|
inlinenodiscardconstexprnoexcept |
Compare logical/bitwise equal.
| rhs | Righthand operand |
Definition at line 1304 of file int128_shared.h.
|
inlinenodiscardconstexprnoexcept |
Compare logical/bitwise equal.
| rhs | Righthand operand |
Definition at line 1310 of file int128_shared.h.
|
inlinenodiscardconstexprnoexcept |
Return true when objects are not equal. Can be used as logical XOR.
| rhs | Righthand operand. |
Definition at line 1324 of file int128_shared.h.
|
inlinenodiscardconstexprnoexcept |
Return true when objects are not equal. Can be used as logical XOR.
| rhs | Righthand operand. |
Definition at line 1330 of file int128_shared.h.
|
inlinenodiscardconstexprnoexcept |
Return true if this object is small than the rhs.
| rhs | Righthand operand. |
Definition at line 1344 of file int128_shared.h.
|
inlinenodiscardconstexprnoexcept |
Return true if this object is small than the rhs.
| rhs | Righthand operand. |
Definition at line 1363 of file int128_shared.h.
|
inlinenodiscardconstexprnoexcept |
Return true this object is small or equal than the rhs.
| rhs | Righthand operand. |
Definition at line 1377 of file int128_shared.h.
|
inlinenodiscardconstexprnoexcept |
Return true this object is small or equal than the rhs.
| rhs | Righthand operand. |
Definition at line 1383 of file int128_shared.h.
|
inlinenodiscardconstexprnoexcept |
Return true this object is larger than the rhs.
| rhs | Righthand operand. |
Definition at line 1397 of file int128_shared.h.
|
inlinenodiscardconstexprnoexcept |
Return true this object is larger than the rhs.
| rhs | Righthand operand. |
Definition at line 1416 of file int128_shared.h.
|
inlinenodiscardconstexprnoexcept |
Return true this object is larger or equal than the rhs.
| rhs | Righthand operand. |
Definition at line 1430 of file int128_shared.h.
|
inlinenodiscardconstexprnoexcept |
Return true this object is larger or equal than the rhs.
| rhs | Righthand operand. |
Definition at line 1436 of file int128_shared.h.
|
inlinenodiscardconstexprnoexcept |
Returns true if the value is an int (fraction is zero)
Definition at line 1453 of file int128_shared.h.
|
inlinenodiscardconstexprnoexcept |
Returns true if the value is positive (including zero) Always true for the unsigned type, which lets the sign tests in the shared code fold away.
Definition at line 1459 of file int128_shared.h.
|
inlinenodiscardconstexprnoexcept |
Returns true if the value negative (smaller than zero) Always false for the unsigned type, which lets the sign tests in the shared code fold away.
Definition at line 1472 of file int128_shared.h.
|
inlinenodiscardconstexprnoexcept |
Returns true if the value is zero.
Definition at line 1484 of file int128_shared.h.
|
inlinenodiscardconstexprnoexcept |
get a specific bit within the 128 data
| bit | bit to get [0,127] |
Definition at line 1490 of file int128_shared.h.
|
inlineconstexprnoexcept |
Extracts the low and high QWORD.
| l | Reference to the low QWORD |
| h | Reference to the high QWORD |
Definition at line 1502 of file int128_shared.h.
|
inlinestaticnodiscardconstexprnoexcept |
Return an instance of int128_base with the value of 1.
Definition at line 1511 of file int128_shared.h.
|
inlinenodiscardnoexcept |
Converts this object to a hex C string. The returned string is a statically thread-allocated buffer. Additional calls to this function from the same thread overwrite the previous result.
Definition at line 1518 of file int128_shared.h.
|
friend |
Definition at line 121 of file int128_shared.h.
|
friend |
Adds 2 values and returns the result.
| lhs | left hand side operand |
| rhs | Right hand side operand |
Definition at line 1547 of file int128_shared.h.
|
friend |
Subtracts 2 values and returns the result.
| lhs | left hand side operand |
| rhs | Right hand side operand |
Definition at line 1558 of file int128_shared.h.
|
friend |
Multiplies 2 values and returns the result.
Only takes part in overload resolution when the matching compound assignment exists, so an unsupported operand is rejected at the call site rather than inside this body. See operator*=(T) for which operands those are.
| lhs | left hand side operand |
| rhs | Right hand side operand |
Definition at line 1576 of file int128_shared.h.
|
friend |
Divides 2 values and returns the result.
Forced inline like every other forwarder here, see FP128_FORCE_INLINE. This one is where the cost was first measured: leaving the decision to MSVC under /GL (whole program optimization) lands on a round trip through memory worth ~10% of a 128 bit by 64 bit division.
| lhs | left hand side operand |
| rhs | Right hand side operand |
Definition at line 1592 of file int128_shared.h.
|
friend |
Performs modulo and returns the result.
| lhs | left hand side operand |
| rhs | Right hand side operand |
Definition at line 1604 of file int128_shared.h.
|
friend |
Performs bitwise AND (&).
| lhs | left hand side operand |
| rhs | Right hand side operand |
Definition at line 1619 of file int128_shared.h.
|
friend |
Performs bitwise OR (|).
| lhs | left hand side operand |
| rhs | Right hand side operand |
Definition at line 1630 of file int128_shared.h.
|
friend |
Performs bitwise XOR (^).
| lhs | left hand side operand |
| rhs | Right hand side operand |
Definition at line 1641 of file int128_shared.h.
|
friend |
Adds a scalar and an int128_base, in that order.
| lhs | Left operand |
| rhs | Right operand |
Definition at line 1660 of file int128_shared.h.
|
friend |
Subtracts an int128_base from a scalar.
| lhs | Left operand |
| rhs | Right operand |
Definition at line 1667 of file int128_shared.h.
|
friend |
Multiplies a scalar and an int128_base, in that order.
| lhs | Left operand |
| rhs | Right operand |
Definition at line 1674 of file int128_shared.h.
|
friend |
Divides a scalar by an int128_base.
| lhs | Left operand |
| rhs | Right operand |
Definition at line 1681 of file int128_shared.h.
|
friend |
Performs modulo of a scalar by an int128_base.
| lhs | Left operand |
| rhs | Right operand |
Definition at line 1688 of file int128_shared.h.
|
friend |
Performs bitwise AND of a scalar and an int128_base.
| lhs | Left operand |
| rhs | Right operand |
Definition at line 1695 of file int128_shared.h.
|
friend |
Performs bitwise OR of a scalar and an int128_base.
| lhs | Left operand |
| rhs | Right operand |
Definition at line 1702 of file int128_shared.h.
|
friend |
Performs bitwise XOR of a scalar and an int128_base.
| lhs | Left operand |
| rhs | Right operand |
Definition at line 1709 of file int128_shared.h.
|
friend |
Shifts a scalar left by an int128_base shift count.
The left operand is widened to int128_base first, so the result is 128 bit rather than the builtin type of lhs. This is what makes (1 << n) with a large n produce the expected power of two instead of the undefined behavior a builtin shift past the operand width would have.
| lhs | Left operand, the value being shifted |
| rhs | Right operand, the shift count. Only its low 32 bits are used. |
Definition at line 1726 of file int128_shared.h.
|
friend |
Shifts a scalar right by an int128_base shift count. The left operand is widened to int128_base first, see operator<< above.
| lhs | Left operand, the value being shifted |
| rhs | Right operand, the shift count. Only its low 32 bits are used. |
Definition at line 1739 of file int128_shared.h.
|
friend |
Compares a scalar and an int128_base for equality.
| lhs | Left operand |
| rhs | Right operand |
Definition at line 1759 of file int128_shared.h.
|
friend |
Compares a scalar and an int128_base for inequality.
| lhs | Left operand |
| rhs | Right operand |
Definition at line 1772 of file int128_shared.h.
|
friend |
Returns true when a scalar is smaller than an int128_base.
| lhs | Left operand |
| rhs | Right operand |
Definition at line 1785 of file int128_shared.h.
|
friend |
Returns true when a scalar is smaller or equal to an int128_base.
| lhs | Left operand |
| rhs | Right operand |
Definition at line 1798 of file int128_shared.h.
|
friend |
Returns true when a scalar is larger than an int128_base.
| lhs | Left operand |
| rhs | Right operand |
Definition at line 1811 of file int128_shared.h.
|
friend |
Returns true when a scalar is larger or equal to an int128_base.
| lhs | Left operand |
| rhs | Right operand |
Definition at line 1824 of file int128_shared.h.
|
friend |
Calculates the absolute value of x. Only declared for the signed instantiation. An unqualified abs() on an unsigned value keeps resolving the way it does today.
| x | input value. |
Definition at line 1850 of file int128_shared.h.
|
friend |
Calculates the square of a value. i.e. x^2.
Faster than x * x and bit identical to it, see int128_base::square().
| x | Value to square |
Definition at line 1863 of file int128_shared.h.
|
friend |
Calculates the left zero count of value x.
| x | input value. |
Definition at line 1869 of file int128_shared.h.
|
friend |
Calculates the square root using Newton's method. Based on the book "Math toolkit for real time programming" by Jack W. Crenshaw.
| x | Value to calculate the root of |
Definition at line 1879 of file int128_shared.h.
|
friend |
Calculates the Log base 2 of x: log2(x) Rounding is always towards zero so the maximum error is close to 1.
| x | The number to perform log2 on. |
| std::domain_error | when x is zero, or negative for the signed type. |
Definition at line 1915 of file int128_shared.h.
|
friend |
Calculates the natural Log (base e) of x: log(x), rounded down to the nearest integer.
| x | The number to perform log on. |
| std::domain_error | when x is zero, or negative for the signed type. |
Definition at line 1929 of file int128_shared.h.
|
friend |
Calculates Log base 10 of x: log10(x), rounded down to the nearest integer.
| x | The number to perform log on. |
| std::domain_error | when x is zero, or negative for the signed type. |
Definition at line 2062 of file int128_shared.h.
|
friend |
Calculates x to the power of y.
| x | Base value |
| y | Exponent |
Definition at line 2106 of file int128_shared.h.
|
staticconstexpr |
True when the msb of the high QWORD is a sign bit.
Definition at line 259 of file int128_shared.h.