fixed_point128 1.0.1.0
128 bit integer, fixed point and floating point arithmetic for C++20
Loading...
Searching...
No Matches
fp128::int128_base< IsSigned > Class Template Reference

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.

Detailed Description

template<bool IsSigned>
class fp128::int128_base< IsSigned >

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:

  • The signedness dependent code is selected with if constexpr (is_signed), or by calling is_negative()/is_positive(), which are compile time constants for the unsigned instantiation and let the surrounding tests fold away.
  • Overflow is handled silently, similar to builtin integer operations.
  • An int128_base object is not thread safe. Accessing a const object from multiple threads is safe.
  • int128_base is conditionally safe, 2 different non const objects can be accessed concurrently.
  • Only 64 bit builds are supported.

Compile time evaluation:
Everything that stays within integer arithmetic is constexpr:

  • Construction from any builtin arithmetic type and from the two QWORDs, copy, move, assignment and the conversions to the integer and floating point types.
  • Addition, subtraction, multiplication, square(), the increment and decrement operators, the shifts, the bitwise operators, the unary operators and the comparisons.
  • The queries is_positive(), is_negative(), is_zero(), get_bit() and get_components(), and the constant one().
  • The math functions abs, sqr, lzcnt128, log2 and pow.

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:

  • Division and modulo, and sqrt which is built on them. The paths for a divisor that fits in 64 bit rest on the _udiv128 intrinsic, and one of them writes both QWORDs through a pointer to the first, which is out of bounds as far as constant evaluation is concerned. A 128 bit divisor goes through div_128bit, which has no such obstacle of its own.
  • The string conversions allocate, and log/log10 look their result up in a function local static table, which a constexpr function may not declare.
Template Parameters
IsSignedTrue for a two's complement signed type, false for an unsigned one.

Definition at line 117 of file int128_shared.h.

Member Typedef Documentation

◆ type

template<bool IsSigned>
typedef int128_base fp128::int128_base< IsSigned >::type

Self type alias.

Definition at line 261 of file int128_shared.h.

◆ ptr_type

template<bool IsSigned>
typedef int128_base* fp128::int128_base< IsSigned >::ptr_type

Pointer type alias.

Definition at line 262 of file int128_shared.h.

◆ ref_type

template<bool IsSigned>
typedef int128_base& fp128::int128_base< IsSigned >::ref_type

Reference type alias.

Definition at line 263 of file int128_shared.h.

Constructor & Destructor Documentation

◆ int128_base() [1/8]

template<bool IsSigned>
fp128::int128_base< IsSigned >::int128_base ( )
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.

◆ int128_base() [2/8]

template<bool IsSigned>
fp128::int128_base< IsSigned >::int128_base ( const int128_base< IsSigned > & rhs)
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.

Parameters
rhsObject to copy from.

Definition at line 298 of file int128_shared.h.

◆ int128_base() [3/8]

template<bool IsSigned>
fp128::int128_base< IsSigned >::int128_base ( int128_base< IsSigned > && rhs)
inlineconstexprnoexcept

Move constructor Doesn't modify the right hand side object. Acts like a copy constructor.

Parameters
rhsObject to copy from.

Definition at line 304 of file int128_shared.h.

◆ int128_base() [4/8]

template<bool IsSigned>
fp128::int128_base< IsSigned >::int128_base ( double x)
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.

Parameters
xInput value

Definition at line 314 of file int128_shared.h.

◆ int128_base() [5/8]

template<bool IsSigned>
template<typename T>
requires std::is_integral_v<T>
fp128::int128_base< IsSigned >::int128_base ( T x)
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.

Template Parameters
TAny builtin integral type (including bool and the character types)
Parameters
xInput value

Definition at line 390 of file int128_shared.h.

◆ int128_base() [6/8]

template<bool IsSigned>
fp128::int128_base< IsSigned >::int128_base ( const char * x)
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.

Parameters
xInput string

Definition at line 407 of file int128_shared.h.

◆ int128_base() [7/8]

template<bool IsSigned>
fp128::int128_base< IsSigned >::int128_base ( const std::string & x)
inlinenoexcept

Constructor from std::string. Allows creating very high precision values. Much slower than the other constructors.

Parameters
xInput string

Definition at line 487 of file int128_shared.h.

◆ int128_base() [8/8]

template<bool IsSigned>
fp128::int128_base< IsSigned >::int128_base ( uint64_t l,
uint64_t h )
inlineconstexprnoexcept

Constructor from the 2 int128_base elements, useful for creating special constants.

Parameters
lLow QWORD
hHigh QWORD

Definition at line 493 of file int128_shared.h.

◆ ~int128_base()

template<bool IsSigned>
fp128::int128_base< IsSigned >::~int128_base ( )
constexprdefaultnoexcept

Destructor.

Member Function Documentation

◆ operator=() [1/3]

template<bool IsSigned>
int128_base & fp128::int128_base< IsSigned >::operator= ( const int128_base< IsSigned > & rhs)
inlineconstexprnoexcept

Assignment operator Assigns the two QWORDs individually rather than being defaulted; see the copy constructor for why the width of the copy matters.

Parameters
rhsValue to copy from
Returns
This object.

Definition at line 505 of file int128_shared.h.

◆ operator=() [2/3]

template<bool IsSigned>
int128_base & fp128::int128_base< IsSigned >::operator= ( int128_base< IsSigned > && rhs)
inlineconstexprnoexcept

Move assignment operator Doesn't modify the right hand side object. Acts like a copy assignment operator.

Parameters
rhsValue to copy from
Returns
This object.

Definition at line 517 of file int128_shared.h.

◆ operator=() [3/3]

template<bool IsSigned>
template<typename T>
int128_base & fp128::int128_base< IsSigned >::operator= ( const T & rhs)
inlineconstexprnoexcept

Assignment operator.

Parameters
rhsValue to copy from
Returns
This object.

Definition at line 528 of file int128_shared.h.

◆ operator uint64_t()

template<bool IsSigned>
fp128::int128_base< IsSigned >::operator uint64_t ( ) const
inlinenodiscardconstexprnoexcept

operator uint64_t - converts to a uint64_t

Returns
Object value.

Definition at line 541 of file int128_shared.h.

◆ operator int64_t()

template<bool IsSigned>
fp128::int128_base< IsSigned >::operator int64_t ( ) const
inlinenodiscardconstexprnoexcept

operator int64_t - converts to a int64_t

Returns
Object value.

Definition at line 546 of file int128_shared.h.

◆ operator uint32_t()

template<bool IsSigned>
fp128::int128_base< IsSigned >::operator uint32_t ( ) const
inlinenodiscardconstexprnoexcept

operator uint32_t - converts to a uint32_t

Returns
Object value.

Definition at line 551 of file int128_shared.h.

◆ operator int32_t()

template<bool IsSigned>
fp128::int128_base< IsSigned >::operator int32_t ( ) const
inlinenodiscardconstexprnoexcept

operator int32_t - converts to a int32_t

Returns
Object value.

Definition at line 556 of file int128_shared.h.

◆ operator float()

template<bool IsSigned>
fp128::int128_base< IsSigned >::operator float ( ) const
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.

Returns
Object value, rounded to nearest with ties going to even.

Definition at line 564 of file int128_shared.h.

◆ operator double()

template<bool IsSigned>
fp128::int128_base< IsSigned >::operator double ( ) const
inlinenodiscardconstexprnoexcept

operator double - converts to a double The whole 128 bit range is within reach of a double's exponent, so no value overflows.

Returns
Object value, rounded to nearest with ties going to even.

Definition at line 599 of file int128_shared.h.

◆ operator long double()

template<bool IsSigned>
fp128::int128_base< IsSigned >::operator long double ( ) const
inlinenodiscardconstexprnoexcept

operator long double - converts to a long double

Returns
Object value.

Definition at line 633 of file int128_shared.h.

◆ to_string()

template<bool IsSigned>
char * fp128::int128_base< IsSigned >::to_string ( ) const
inlinenodiscardnoexcept

Converts to a char* (slow) string holds all meaningful fraction bits.

Returns
object string representation

Definition at line 638 of file int128_shared.h.

◆ operator std::string()

template<bool IsSigned>
fp128::int128_base< IsSigned >::operator std::string ( ) const
inlinenodiscard

Converts to a std::string (slow) string holds all meaningful fraction bits.

Returns
object string representation

Definition at line 643 of file int128_shared.h.

◆ operator char *()

template<bool IsSigned>
fp128::int128_base< IsSigned >::operator char * ( ) const
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.

Returns
C string with describing the value of the object.
object string representation

Definition at line 651 of file int128_shared.h.

◆ operator>>()

template<bool IsSigned>
int128_base fp128::int128_base< IsSigned >::operator>> ( int32_t shift) const
inlinenodiscardconstexprnoexcept

Performs right shift operation.

Parameters
shiftbits to shift
Returns
Temporary object with the result of the operation

Definition at line 718 of file int128_shared.h.

◆ operator<<()

template<bool IsSigned>
int128_base fp128::int128_base< IsSigned >::operator<< ( int32_t shift) const
inlinenodiscardconstexprnoexcept

Performs left shift operation.

Parameters
shiftbits to shift
Returns
Temporary object with the result of the operation

Definition at line 728 of file int128_shared.h.

◆ operator+=() [1/2]

template<bool IsSigned>
int128_base & fp128::int128_base< IsSigned >::operator+= ( const int128_base< IsSigned > & rhs)
inlineconstexprnoexcept

Add a value to this object like other builtin integer types, overflow wraps around silently.

Parameters
rhsRight hand side operand
Returns
This object.

Definition at line 739 of file int128_shared.h.

◆ operator+=() [2/2]

template<bool IsSigned>
template<typename T>
int128_base & fp128::int128_base< IsSigned >::operator+= ( const T & rhs)
inlineconstexprnoexcept

Add a value to this object like other builtin integer types, overflow wraps around silently.

Parameters
rhsRight hand side operand
Returns
This object.

Definition at line 751 of file int128_shared.h.

◆ operator-=() [1/2]

template<bool IsSigned>
int128_base & fp128::int128_base< IsSigned >::operator-= ( const int128_base< IsSigned > & rhs)
inlineconstexprnoexcept

Subtract a value from this object like other builtin integer types, underflow wraps around silently.

Parameters
rhsRight hand side operand
Returns
This object.

Definition at line 758 of file int128_shared.h.

◆ operator-=() [2/2]

template<bool IsSigned>
template<typename T>
int128_base & fp128::int128_base< IsSigned >::operator-= ( const T & rhs)
inlineconstexprnoexcept

Subtract a value from this object like other builtin integer types, underflow wraps around silently.

Parameters
rhsRight hand side operand
Returns
This object.

Definition at line 775 of file int128_shared.h.

◆ operator*=() [1/2]

template<bool IsSigned>
int128_base & fp128::int128_base< IsSigned >::operator*= ( const int128_base< IsSigned > & rhs)
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.

Parameters
rhsRight hand side operand
Returns
This object.

Definition at line 787 of file int128_shared.h.

◆ square()

template<bool IsSigned>
int128_base & fp128::int128_base< IsSigned >::square ( )
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.

Returns
This object.

Definition at line 815 of file int128_shared.h.

◆ operator*=() [2/2]

template<bool IsSigned>
template<typename T>
requires std::is_arithmetic_v<T>
int128_base & fp128::int128_base< IsSigned >::operator*= ( T x)
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.

Parameters
xRight hand side operand
Returns
This object.

Definition at line 845 of file int128_shared.h.

◆ operator/=() [1/2]

template<bool IsSigned>
int128_base & fp128::int128_base< IsSigned >::operator/= ( const int128_base< IsSigned > & rhs_in)
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.

Parameters
rhs_inRight hand side operator (denominator)
Returns
this object.

Definition at line 879 of file int128_shared.h.

◆ operator/=() [2/2]

template<bool IsSigned>
template<typename T>
int128_base & fp128::int128_base< IsSigned >::operator/= ( T x)
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.

Parameters
xRight hand side operator (denominator)
Returns
this object.

Definition at line 969 of file int128_shared.h.

◆ operator%=() [1/2]

template<bool IsSigned>
int128_base & fp128::int128_base< IsSigned >::operator%= ( const int128_base< IsSigned > & rhs)
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.

Parameters
rhsModulo operand.
Returns
This object.

Definition at line 1040 of file int128_shared.h.

◆ operator%=() [2/2]

template<bool IsSigned>
template<typename T>
int128_base & fp128::int128_base< IsSigned >::operator%= ( T x)
inline

%= operator

Parameters
xModulo operand.
Returns
This object.

Definition at line 1090 of file int128_shared.h.

◆ operator>>=()

template<bool IsSigned>
int128_base & fp128::int128_base< IsSigned >::operator>>= ( int32_t shift)
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.

Parameters
shiftBits 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.
Returns
This object.

Definition at line 1099 of file int128_shared.h.

◆ operator<<=()

template<bool IsSigned>
int128_base & fp128::int128_base< IsSigned >::operator<<= ( int32_t shift)
inlineconstexprnoexcept

Shift left this object.

Parameters
shiftBits to shift. Zero and negative values leave the object unchanged, values of 128 and above produce zero.
Returns
This object.

Definition at line 1144 of file int128_shared.h.

◆ operator&=() [1/2]

template<bool IsSigned>
int128_base & fp128::int128_base< IsSigned >::operator&= ( const int128_base< IsSigned > & rhs)
inlineconstexprnoexcept

Bitwise AND=.

Parameters
rhsAND mask.
Returns
This object.

Definition at line 1167 of file int128_shared.h.

◆ operator&=() [2/2]

template<bool IsSigned>
template<typename T>
int128_base & fp128::int128_base< IsSigned >::operator&= ( const T & rhs)
inlineconstexprnoexcept

Bitwise AND=.

Parameters
rhsAND mask.
Returns
This object.

Definition at line 1178 of file int128_shared.h.

◆ operator|=() [1/2]

template<bool IsSigned>
int128_base & fp128::int128_base< IsSigned >::operator|= ( const int128_base< IsSigned > & rhs)
inlineconstexprnoexcept

Bitwise OR=.

Parameters
rhsOR mask.
Returns
This object.

Definition at line 1184 of file int128_shared.h.

◆ operator|=() [2/2]

template<bool IsSigned>
template<typename T>
int128_base & fp128::int128_base< IsSigned >::operator|= ( const T & rhs)
inlineconstexprnoexcept

Bitwise OR=.

Parameters
rhsOR mask.
Returns
This object.

Definition at line 1195 of file int128_shared.h.

◆ operator^=() [1/2]

template<bool IsSigned>
int128_base & fp128::int128_base< IsSigned >::operator^= ( const int128_base< IsSigned > & rhs)
inlineconstexprnoexcept

Bitwise XOR=.

Parameters
rhsXOR mask.
Returns
This object.

Definition at line 1201 of file int128_shared.h.

◆ operator^=() [2/2]

template<bool IsSigned>
template<typename T>
int128_base & fp128::int128_base< IsSigned >::operator^= ( const T & rhs)
inlineconstexprnoexcept

Bitwise XOR=.

Parameters
rhsXOR mask.
Returns
This object.

Definition at line 1212 of file int128_shared.h.

◆ operator++() [1/2]

template<bool IsSigned>
int128_base & fp128::int128_base< IsSigned >::operator++ ( )
inlineconstexprnoexcept

Prefix ++ operation (++a)

Returns
This object.

Definition at line 1217 of file int128_shared.h.

◆ operator++() [2/2]

template<bool IsSigned>
int128_base fp128::int128_base< IsSigned >::operator++ ( int32_t )
inlineconstexprnoexcept

Postfix ++ operation (a++)

Returns
This object.

Definition at line 1226 of file int128_shared.h.

◆ operator--() [1/2]

template<bool IsSigned>
int128_base & fp128::int128_base< IsSigned >::operator-- ( )
inlineconstexprnoexcept

Prefix – operation (–a)

Returns
This object.

Definition at line 1236 of file int128_shared.h.

◆ operator--() [2/2]

template<bool IsSigned>
int128_base fp128::int128_base< IsSigned >::operator-- ( int32_t )
inlineconstexprnoexcept

Postfix – operation (a–)

Returns
This object.

Definition at line 1245 of file int128_shared.h.

◆ operator bool()

template<bool IsSigned>
fp128::int128_base< IsSigned >::operator bool ( ) const
inlinenodiscardconstexprnoexcept

Convert to bool.

Definition at line 1258 of file int128_shared.h.

◆ operator!()

template<bool IsSigned>
bool fp128::int128_base< IsSigned >::operator! ( ) const
inlinenodiscardconstexprnoexcept

Logical not (!). Opposite of operator bool.

Definition at line 1262 of file int128_shared.h.

◆ operator~()

template<bool IsSigned>
int128_base fp128::int128_base< IsSigned >::operator~ ( ) const
inlinenodiscardconstexprnoexcept

Bitwise not (~).

Definition at line 1266 of file int128_shared.h.

◆ operator+()

template<bool IsSigned>
int128_base fp128::int128_base< IsSigned >::operator+ ( ) const
inlinenodiscardconstexprnoexcept

Unary +. Returns a copy of the object.

Definition at line 1276 of file int128_shared.h.

◆ operator-()

template<bool IsSigned>
int128_base fp128::int128_base< IsSigned >::operator- ( ) const
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.

◆ operator==() [1/2]

template<bool IsSigned>
bool fp128::int128_base< IsSigned >::operator== ( const int128_base< IsSigned > & rhs) const
inlinenodiscardconstexprnoexcept

Compare logical/bitwise equal.

Parameters
rhsRighthand operand
Returns
True if this and rhs are equal.

Definition at line 1304 of file int128_shared.h.

◆ operator==() [2/2]

template<bool IsSigned>
template<typename T>
bool fp128::int128_base< IsSigned >::operator== ( const T & rhs) const
inlinenodiscardconstexprnoexcept

Compare logical/bitwise equal.

Parameters
rhsRighthand operand
Returns
True if this and rhs are equal.

Definition at line 1310 of file int128_shared.h.

◆ operator!=() [1/2]

template<bool IsSigned>
bool fp128::int128_base< IsSigned >::operator!= ( const int128_base< IsSigned > & rhs) const
inlinenodiscardconstexprnoexcept

Return true when objects are not equal. Can be used as logical XOR.

Parameters
rhsRighthand operand.
Returns
True of not equal.

Definition at line 1324 of file int128_shared.h.

◆ operator!=() [2/2]

template<bool IsSigned>
template<typename T>
bool fp128::int128_base< IsSigned >::operator!= ( const T & rhs) const
inlinenodiscardconstexprnoexcept

Return true when objects are not equal. Can be used as logical XOR.

Parameters
rhsRighthand operand.
Returns
True of not equal.

Definition at line 1330 of file int128_shared.h.

◆ operator<() [1/2]

template<bool IsSigned>
bool fp128::int128_base< IsSigned >::operator< ( const int128_base< IsSigned > & rhs) const
inlinenodiscardconstexprnoexcept

Return true if this object is small than the rhs.

Parameters
rhsRighthand operand.
Returns
True when this object is smaller.

Definition at line 1344 of file int128_shared.h.

◆ operator<() [2/2]

template<bool IsSigned>
template<typename T>
bool fp128::int128_base< IsSigned >::operator< ( const T & rhs) const
inlinenodiscardconstexprnoexcept

Return true if this object is small than the rhs.

Parameters
rhsRighthand operand.
Returns
True when this object is smaller.

Definition at line 1363 of file int128_shared.h.

◆ operator<=() [1/2]

template<bool IsSigned>
bool fp128::int128_base< IsSigned >::operator<= ( const int128_base< IsSigned > & rhs) const
inlinenodiscardconstexprnoexcept

Return true this object is small or equal than the rhs.

Parameters
rhsRighthand operand.
Returns
True when this object is smaller or equal.

Definition at line 1377 of file int128_shared.h.

◆ operator<=() [2/2]

template<bool IsSigned>
template<typename T>
bool fp128::int128_base< IsSigned >::operator<= ( const T & rhs) const
inlinenodiscardconstexprnoexcept

Return true this object is small or equal than the rhs.

Parameters
rhsRighthand operand.
Returns
True when this object is smaller or equal.

Definition at line 1383 of file int128_shared.h.

◆ operator>() [1/2]

template<bool IsSigned>
bool fp128::int128_base< IsSigned >::operator> ( const int128_base< IsSigned > & rhs) const
inlinenodiscardconstexprnoexcept

Return true this object is larger than the rhs.

Parameters
rhsRighthand operand.
Returns
True when this objext is larger.

Definition at line 1397 of file int128_shared.h.

◆ operator>() [2/2]

template<bool IsSigned>
template<typename T>
bool fp128::int128_base< IsSigned >::operator> ( const T & rhs) const
inlinenodiscardconstexprnoexcept

Return true this object is larger than the rhs.

Parameters
rhsRighthand operand.
Returns
True when this objext is larger.

Definition at line 1416 of file int128_shared.h.

◆ operator>=() [1/2]

template<bool IsSigned>
bool fp128::int128_base< IsSigned >::operator>= ( const int128_base< IsSigned > & rhs) const
inlinenodiscardconstexprnoexcept

Return true this object is larger or equal than the rhs.

Parameters
rhsRighthand operand.
Returns
True when this objext is larger or equal.

Definition at line 1430 of file int128_shared.h.

◆ operator>=() [2/2]

template<bool IsSigned>
template<typename T>
bool fp128::int128_base< IsSigned >::operator>= ( const T & rhs) const
inlinenodiscardconstexprnoexcept

Return true this object is larger or equal than the rhs.

Parameters
rhsRighthand operand.
Returns
True when this objext is larger or equal.

Definition at line 1436 of file int128_shared.h.

◆ is_int()

template<bool IsSigned>
bool fp128::int128_base< IsSigned >::is_int ( ) const
inlinenodiscardconstexprnoexcept

Returns true if the value is an int (fraction is zero)

Returns
True when the fraction is zero.

Definition at line 1453 of file int128_shared.h.

◆ is_positive()

template<bool IsSigned>
bool fp128::int128_base< IsSigned >::is_positive ( ) const
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.

Returns
True when the value is positive

Definition at line 1459 of file int128_shared.h.

◆ is_negative()

template<bool IsSigned>
bool fp128::int128_base< IsSigned >::is_negative ( ) const
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.

Returns
True when the value is negative

Definition at line 1472 of file int128_shared.h.

◆ is_zero()

template<bool IsSigned>
bool fp128::int128_base< IsSigned >::is_zero ( ) const
inlinenodiscardconstexprnoexcept

Returns true if the value is zero.

Returns
Returns true if the value is zero

Definition at line 1484 of file int128_shared.h.

◆ get_bit()

template<bool IsSigned>
int32_t fp128::int128_base< IsSigned >::get_bit ( uint32_t bit) const
inlinenodiscardconstexprnoexcept

get a specific bit within the 128 data

Parameters
bitbit to get [0,127]
Returns
0 or 1. Undefined when bit > 127

Definition at line 1490 of file int128_shared.h.

◆ get_components()

template<bool IsSigned>
void fp128::int128_base< IsSigned >::get_components ( uint64_t & l,
uint64_t & h ) const
inlineconstexprnoexcept

Extracts the low and high QWORD.

Parameters
lReference to the low QWORD
hReference to the high QWORD

Definition at line 1502 of file int128_shared.h.

◆ one()

template<bool IsSigned>
constexpr int128_base fp128::int128_base< IsSigned >::one ( )
inlinestaticnodiscardconstexprnoexcept

Return an instance of int128_base with the value of 1.

Returns
1

Definition at line 1511 of file int128_shared.h.

◆ hex()

template<bool IsSigned>
char * fp128::int128_base< IsSigned >::hex ( ) const
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.

Returns
C string containing the HEX value of the object.

Definition at line 1518 of file int128_shared.h.

◆ fp128_gtest

template<bool IsSigned>
friend class fp128_gtest
friend

Definition at line 121 of file int128_shared.h.

◆ operator+ [1/2]

template<bool IsSigned>
template<typename T>
int128_base operator+ ( int128_base< IsSigned > lhs,
const T & rhs )
friend

Adds 2 values and returns the result.

Parameters
lhsleft hand side operand
rhsRight hand side operand
Returns
Result of the operation

Definition at line 1547 of file int128_shared.h.

◆ operator- [1/2]

template<bool IsSigned>
template<typename T>
int128_base operator- ( int128_base< IsSigned > lhs,
const T & rhs )
friend

Subtracts 2 values and returns the result.

Parameters
lhsleft hand side operand
rhsRight hand side operand
Returns
Result of the operation

Definition at line 1558 of file int128_shared.h.

◆ operator* [1/2]

template<bool IsSigned>
template<typename T>
requires requires(int128_base l, const T& r) { l *= r; }
int128_base operator* ( int128_base< IsSigned > lhs,
const T & rhs )
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.

Parameters
lhsleft hand side operand
rhsRight hand side operand
Returns
Result of the operation

Definition at line 1576 of file int128_shared.h.

◆ operator/ [1/2]

template<bool IsSigned>
template<typename T>
int128_base operator/ ( int128_base< IsSigned > lhs,
const T & rhs )
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.

Parameters
lhsleft hand side operand
rhsRight hand side operand
Returns
Result of the operation

Definition at line 1592 of file int128_shared.h.

◆ operator% [1/2]

template<bool IsSigned>
template<typename T>
int128_base operator% ( int128_base< IsSigned > lhs,
const T & rhs )
friend

Performs modulo and returns the result.

Parameters
lhsleft hand side operand
rhsRight hand side operand
Returns
Result of the operation

Definition at line 1604 of file int128_shared.h.

◆ operator& [1/2]

template<bool IsSigned>
template<typename T>
int128_base operator& ( int128_base< IsSigned > lhs,
const T & rhs )
friend

Performs bitwise AND (&).

Parameters
lhsleft hand side operand
rhsRight hand side operand
Returns
Result of the operation

Definition at line 1619 of file int128_shared.h.

◆ operator| [1/2]

template<bool IsSigned>
template<typename T>
int128_base operator| ( int128_base< IsSigned > lhs,
const T & rhs )
friend

Performs bitwise OR (|).

Parameters
lhsleft hand side operand
rhsRight hand side operand
Returns
Result of the operation

Definition at line 1630 of file int128_shared.h.

◆ operator^ [1/2]

template<bool IsSigned>
template<typename T>
int128_base operator^ ( int128_base< IsSigned > lhs,
const T & rhs )
friend

Performs bitwise XOR (^).

Parameters
lhsleft hand side operand
rhsRight hand side operand
Returns
Result of the operation

Definition at line 1641 of file int128_shared.h.

◆ operator+ [2/2]

template<bool IsSigned>
template<typename T>
requires std::is_arithmetic_v<T>
int128_base operator+ ( const T & lhs,
const int128_base< IsSigned > & rhs )
friend

Adds a scalar and an int128_base, in that order.

Parameters
lhsLeft operand
rhsRight operand
Returns
Result of the operation

Definition at line 1660 of file int128_shared.h.

◆ operator- [2/2]

template<bool IsSigned>
template<typename T>
requires std::is_arithmetic_v<T>
int128_base operator- ( const T & lhs,
const int128_base< IsSigned > & rhs )
friend

Subtracts an int128_base from a scalar.

Parameters
lhsLeft operand
rhsRight operand
Returns
Result of the operation

Definition at line 1667 of file int128_shared.h.

◆ operator* [2/2]

template<bool IsSigned>
template<typename T>
requires std::is_arithmetic_v<T>
int128_base operator* ( const T & lhs,
const int128_base< IsSigned > & rhs )
friend

Multiplies a scalar and an int128_base, in that order.

Parameters
lhsLeft operand
rhsRight operand
Returns
Result of the operation

Definition at line 1674 of file int128_shared.h.

◆ operator/ [2/2]

template<bool IsSigned>
template<typename T>
requires std::is_arithmetic_v<T>
int128_base operator/ ( const T & lhs,
const int128_base< IsSigned > & rhs )
friend

Divides a scalar by an int128_base.

Parameters
lhsLeft operand
rhsRight operand
Returns
Result of the operation

Definition at line 1681 of file int128_shared.h.

◆ operator% [2/2]

template<bool IsSigned>
template<typename T>
requires std::is_arithmetic_v<T>
int128_base operator% ( const T & lhs,
const int128_base< IsSigned > & rhs )
friend

Performs modulo of a scalar by an int128_base.

Parameters
lhsLeft operand
rhsRight operand
Returns
Result of the operation

Definition at line 1688 of file int128_shared.h.

◆ operator& [2/2]

template<bool IsSigned>
template<typename T>
requires std::is_arithmetic_v<T>
int128_base operator& ( const T & lhs,
const int128_base< IsSigned > & rhs )
friend

Performs bitwise AND of a scalar and an int128_base.

Parameters
lhsLeft operand
rhsRight operand
Returns
Result of the operation

Definition at line 1695 of file int128_shared.h.

◆ operator| [2/2]

template<bool IsSigned>
template<typename T>
requires std::is_arithmetic_v<T>
int128_base operator| ( const T & lhs,
const int128_base< IsSigned > & rhs )
friend

Performs bitwise OR of a scalar and an int128_base.

Parameters
lhsLeft operand
rhsRight operand
Returns
Result of the operation

Definition at line 1702 of file int128_shared.h.

◆ operator^ [2/2]

template<bool IsSigned>
template<typename T>
requires std::is_arithmetic_v<T>
int128_base operator^ ( const T & lhs,
const int128_base< IsSigned > & rhs )
friend

Performs bitwise XOR of a scalar and an int128_base.

Parameters
lhsLeft operand
rhsRight operand
Returns
Result of the operation

Definition at line 1709 of file int128_shared.h.

◆ operator<<

template<bool IsSigned>
template<typename T>
requires std::is_arithmetic_v<T>
int128_base operator<< ( const T & lhs,
const int128_base< IsSigned > & rhs )
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.

Parameters
lhsLeft operand, the value being shifted
rhsRight operand, the shift count. Only its low 32 bits are used.
Returns
Result of the operation

Definition at line 1726 of file int128_shared.h.

◆ operator>>

template<bool IsSigned>
template<typename T>
requires std::is_arithmetic_v<T>
int128_base operator>> ( const T & lhs,
const int128_base< IsSigned > & rhs )
friend

Shifts a scalar right by an int128_base shift count. The left operand is widened to int128_base first, see operator<< above.

Parameters
lhsLeft operand, the value being shifted
rhsRight operand, the shift count. Only its low 32 bits are used.
Returns
Result of the operation

Definition at line 1739 of file int128_shared.h.

◆ operator==

template<bool IsSigned>
template<typename T>
requires std::is_arithmetic_v<T>
bool operator== ( const T & lhs,
const int128_base< IsSigned > & rhs )
friend

Compares a scalar and an int128_base for equality.

Parameters
lhsLeft operand
rhsRight operand
Returns
True when the two are equal

Definition at line 1759 of file int128_shared.h.

◆ operator!=

template<bool IsSigned>
template<typename T>
requires std::is_arithmetic_v<T>
bool operator!= ( const T & lhs,
const int128_base< IsSigned > & rhs )
friend

Compares a scalar and an int128_base for inequality.

Parameters
lhsLeft operand
rhsRight operand
Returns
True when the two differ

Definition at line 1772 of file int128_shared.h.

◆ operator<

template<bool IsSigned>
template<typename T>
requires std::is_arithmetic_v<T>
bool operator< ( const T & lhs,
const int128_base< IsSigned > & rhs )
friend

Returns true when a scalar is smaller than an int128_base.

Parameters
lhsLeft operand
rhsRight operand
Returns
True when lhs is smaller

Definition at line 1785 of file int128_shared.h.

◆ operator<=

template<bool IsSigned>
template<typename T>
requires std::is_arithmetic_v<T>
bool operator<= ( const T & lhs,
const int128_base< IsSigned > & rhs )
friend

Returns true when a scalar is smaller or equal to an int128_base.

Parameters
lhsLeft operand
rhsRight operand
Returns
True when lhs is smaller or equal

Definition at line 1798 of file int128_shared.h.

◆ operator>

template<bool IsSigned>
template<typename T>
requires std::is_arithmetic_v<T>
bool operator> ( const T & lhs,
const int128_base< IsSigned > & rhs )
friend

Returns true when a scalar is larger than an int128_base.

Parameters
lhsLeft operand
rhsRight operand
Returns
True when lhs is larger

Definition at line 1811 of file int128_shared.h.

◆ operator>=

template<bool IsSigned>
template<typename T>
requires std::is_arithmetic_v<T>
bool operator>= ( const T & lhs,
const int128_base< IsSigned > & rhs )
friend

Returns true when a scalar is larger or equal to an int128_base.

Parameters
lhsLeft operand
rhsRight operand
Returns
True when lhs is larger or equal

Definition at line 1824 of file int128_shared.h.

◆ abs

template<bool IsSigned>
int128_base abs ( const int128_base< IsSigned > & x)
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.

Parameters
xinput value.
Returns
|x|

Definition at line 1850 of file int128_shared.h.

◆ sqr

template<bool IsSigned>
int128_base sqr ( int128_base< IsSigned > x)
friend

Calculates the square of a value. i.e. x^2.

Faster than x * x and bit identical to it, see int128_base::square().

Parameters
xValue to square
Returns
x^2, truncated to 128 bit

Definition at line 1863 of file int128_shared.h.

◆ lzcnt128

template<bool IsSigned>
uint64_t lzcnt128 ( const int128_base< IsSigned > & x)
friend

Calculates the left zero count of value x.

Parameters
xinput value.
Returns
lzc (uint32_t) of the result.

Definition at line 1869 of file int128_shared.h.

◆ sqrt

template<bool IsSigned>
uint64_t sqrt ( const int128_base< IsSigned > & x)
friend

Calculates the square root using Newton's method. Based on the book "Math toolkit for real time programming" by Jack W. Crenshaw.

Parameters
xValue to calculate the root of
Returns
Square root of (x) rounded down to the nearest integer, zero when x <= 0.

Definition at line 1879 of file int128_shared.h.

◆ log2

template<bool IsSigned>
uint64_t log2 ( const int128_base< IsSigned > & x)
friend

Calculates the Log base 2 of x: log2(x) Rounding is always towards zero so the maximum error is close to 1.

Parameters
xThe number to perform log2 on.
Returns
log2(x), which is also the bit position of the msb.
Exceptions
std::domain_errorwhen x is zero, or negative for the signed type.

Definition at line 1915 of file int128_shared.h.

◆ log

template<bool IsSigned>
uint64_t log ( const int128_base< IsSigned > & x)
friend

Calculates the natural Log (base e) of x: log(x), rounded down to the nearest integer.

Parameters
xThe number to perform log on.
Returns
log(x)
Exceptions
std::domain_errorwhen x is zero, or negative for the signed type.

Definition at line 1929 of file int128_shared.h.

◆ log10

template<bool IsSigned>
uint64_t log10 ( const int128_base< IsSigned > & x)
friend

Calculates Log base 10 of x: log10(x), rounded down to the nearest integer.

Parameters
xThe number to perform log on.
Returns
log10(x)
Exceptions
std::domain_errorwhen x is zero, or negative for the signed type.

Definition at line 2062 of file int128_shared.h.

◆ pow

template<bool IsSigned>
int128_base pow ( const int128_base< IsSigned > & x,
uint32_t y )
friend

Calculates x to the power of y.

Parameters
xBase value
yExponent
Returns
x to the power of y

Definition at line 2106 of file int128_shared.h.

Member Data Documentation

◆ is_signed

template<bool IsSigned>
bool fp128::int128_base< IsSigned >::is_signed = IsSigned
staticconstexpr

True when the msb of the high QWORD is a sign bit.

Definition at line 259 of file int128_shared.h.


The documentation for this class was generated from the following file: