nfx-datatypes 0.2.0
Cross-platform C++ library with high-precision Int128 and Decimal datatypes
Loading...
Searching...
No Matches
Int128.h
Go to the documentation of this file.
1/*
2 * MIT License
3 *
4 * Copyright (c) 2025 nfx
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in all
14 * copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 * SOFTWARE.
23 */
24
116
117#pragma once
118
119#include <array>
120#include <compare>
121#include <cstdint>
122#include <optional>
123#include <string>
124#include <string_view>
125
126#include "nfx/detail/datatypes/CompilerSupport.h"
127
128namespace nfx::datatypes
129{
130 class Decimal;
131
132 //=====================================================================
133 // Int128 class
134 //=====================================================================
135
145 class Int128 final
146 {
147 public:
148 //----------------------------------------------
149 // Construction
150 //----------------------------------------------
151
155 inline constexpr Int128() noexcept;
156
161 inline explicit constexpr Int128( int val ) noexcept;
162
167 inline explicit constexpr Int128( std::int64_t val ) noexcept;
168
173 inline explicit constexpr Int128( std::uint32_t val ) noexcept;
174
179 inline explicit constexpr Int128( std::uint64_t val ) noexcept;
180
186 constexpr Int128( std::uint64_t low, std::uint64_t high ) noexcept;
187
188#if NFX_DATATYPES_HAS_NATIVE_INT128
193 explicit constexpr Int128( NFX_DATATYPES_NATIVE_INT128 val ) noexcept;
194#endif
195
201 inline explicit Int128( std::string_view str );
202
208 inline explicit Int128( const char* scStr );
209
219 explicit Int128( float val );
220
231 explicit Int128( double val );
232
244 explicit Int128( const Decimal& decimal );
245
250 constexpr Int128( const Int128& other ) noexcept = default;
251
256 constexpr Int128( Int128&& other ) noexcept = default;
257
258 //----------------------------------------------
259 // Destruction
260 //----------------------------------------------
261
263 ~Int128() = default;
264
265 //----------------------------------------------
266 // Assignment
267 //----------------------------------------------
268
274 constexpr Int128& operator=( const Int128& other ) noexcept = default;
275
281 constexpr Int128& operator=( Int128&& other ) noexcept = default;
282
283 //----------------------------------------------
284 // Comparison operators
285 //----------------------------------------------
286
293 inline std::strong_ordering operator<=>( const Int128& other ) const noexcept;
294
301 inline bool operator==( const Int128& other ) const noexcept;
302
303 //----------------------------------------------
304 // Comparison with built-in integer types
305 //----------------------------------------------
306
312 inline bool operator==( int val ) const noexcept;
313
319 inline bool operator!=( int val ) const noexcept;
320
326 inline bool operator<( int val ) const noexcept;
327
333 inline bool operator<=( int val ) const noexcept;
334
340 inline bool operator>( int val ) const noexcept;
341
347 inline bool operator>=( int val ) const noexcept;
348
354 inline bool operator==( std::int64_t val ) const noexcept;
355
361 bool operator!=( std::int64_t val ) const noexcept;
362
368 inline bool operator<( std::int64_t val ) const noexcept;
369
375 inline bool operator<=( std::int64_t val ) const noexcept;
376
382 inline bool operator>( std::int64_t val ) const noexcept;
383
389 inline bool operator>=( std::int64_t val ) const noexcept;
390
396 inline bool operator==( std::uint64_t val ) const noexcept;
397
403 inline bool operator!=( std::uint64_t val ) const noexcept;
404
410 inline bool operator<( std::uint64_t val ) const noexcept;
411
417 inline bool operator<=( std::uint64_t val ) const noexcept;
418
424 inline bool operator>( std::uint64_t val ) const noexcept;
425
431 inline bool operator>=( std::uint64_t val ) const noexcept;
432
433 //----------------------------------------------
434 // Comparison with built-in floating point types
435 //----------------------------------------------
436
442 inline bool operator==( float val ) const noexcept;
443
449 inline bool operator!=( float val ) const noexcept;
450
456 inline bool operator<( float val ) const noexcept;
457
463 inline bool operator<=( float val ) const noexcept;
464
470 inline bool operator>( float val ) const noexcept;
471
477 inline bool operator>=( float val ) const noexcept;
478
484 bool operator==( double val ) const noexcept;
485
491 inline bool operator!=( double val ) const noexcept;
492
498 bool operator<( double val ) const noexcept;
499
505 inline bool operator<=( double val ) const noexcept;
506
512 bool operator>( double val ) const noexcept;
513
519 inline bool operator>=( double val ) const noexcept;
520
521 //----------------------------------------------
522 // Comparison with nfx::datatypes::Decimal
523 //----------------------------------------------
524
531 bool operator==( const Decimal& val ) const noexcept;
532
538 inline bool operator!=( const Decimal& val ) const noexcept;
539
545 bool operator<( const Decimal& val ) const noexcept;
546
552 inline bool operator<=( const Decimal& val ) const noexcept;
553
559 inline bool operator>( const Decimal& val ) const noexcept;
560
566 inline bool operator>=( const Decimal& val ) const noexcept;
567
568 //----------------------------------------------
569 // Arithmetic operators
570 //----------------------------------------------
571
577 inline Int128 operator+( const Int128& other ) const noexcept;
578
584 inline Int128 operator-( const Int128& other ) const noexcept;
585
591 Int128 operator*( const Int128& other ) const noexcept;
592
599 Int128 operator/( const Int128& other ) const;
600
606 inline Int128& operator+=( const Int128& other ) noexcept;
607
613 inline Int128& operator-=( const Int128& other ) noexcept;
614
620 inline Int128& operator*=( const Int128& other ) noexcept;
621
628 inline Int128& operator/=( const Int128& other );
629
636 inline Int128& operator%=( const Int128& other );
637
644 inline Int128 operator%( const Int128& other ) const;
645
650 inline Int128 operator-() const noexcept;
651
652 //----------------------------------------------
653 // Property accessors
654 //----------------------------------------------
655
661 [[nodiscard]] std::uint64_t toLow() const noexcept;
662
668 [[nodiscard]] std::uint64_t toHigh() const noexcept;
669
670#if NFX_DATATYPES_HAS_NATIVE_INT128
685 NFX_DATATYPES_NATIVE_INT128 toNative() const noexcept;
686#endif
687
688 //----------------------------------------------
689 // Mathematical operations
690 //----------------------------------------------
691
697 [[nodiscard]] inline Int128 abs() const noexcept;
698
707 [[nodiscard]] Int128 isqrt() const;
708
709 //----------------------------------------------
710 // String parsing
711 //----------------------------------------------
712
730 [[nodiscard]] static bool fromString( std::string_view str, Int128& result ) noexcept;
731
748 [[nodiscard]] static std::optional<Int128> fromString( std::string_view str ) noexcept;
749
750 //----------------------------------------------
751 // Type conversion
752 //----------------------------------------------
753
759 [[nodiscard]] std::string toString() const;
760
766 [[nodiscard]] std::array<std::int32_t, 4> toBits() const noexcept;
767
768 private:
769 //----------------------------------------------
770 // Internal representation
771 //----------------------------------------------
772
773#if NFX_DATATYPES_HAS_NATIVE_INT128
775 NFX_DATATYPES_NATIVE_INT128 m_value;
776#else
777 struct Layout
778 {
780 std::uint64_t lower64bits;
781 std::uint64_t upper64bits;
782 } m_layout;
783#endif
784 };
785
786 //=====================================================================
787 // Free functions
788 //=====================================================================
789
798 [[nodiscard]] inline Int128 abs( const Int128& value ) noexcept
799 {
800 return value.abs();
801 }
802
812 [[nodiscard]] inline Int128 isqrt( const Int128& value )
813 {
814 return value.isqrt();
815 }
816
836 [[nodiscard]] Decimal sqrt( const Int128& value );
837} // namespace nfx::datatypes
838
839#include "nfx/detail/datatypes/Int128.inl"
Int128 isqrt(const Int128 &value)
Calculate integer square root (free function).
Definition Int128.h:812
Decimal sqrt(const Decimal &value)
Compute square root using Newton-Raphson method (free function).
Definition Decimal.h:916
Decimal abs(const Decimal &value) noexcept
Get absolute value of decimal (free function).
Definition Decimal.h:902
Cross-platform high-precision decimal type.
Definition Decimal.h:167
Cross-platform 128-bit signed integer type.
Definition Int128.h:146
bool operator<=(int val) const noexcept
Less than or equal comparison with signed 32-bit integer.
bool operator<=(float val) const noexcept
Less than or equal comparison with single-precision floating-point.
bool operator>(double val) const noexcept
Greater than comparison with double-precision floating-point.
~Int128()=default
Destructor.
Int128 operator-(const Int128 &other) const noexcept
Subtraction operator.
bool operator!=(const Decimal &val) const noexcept
Inequality comparison with Decimal.
bool operator==(int val) const noexcept
Equality comparison with signed 32-bit integer.
Int128 & operator/=(const Int128 &other)
Division assignment operator.
bool operator>(std::int64_t val) const noexcept
Greater than comparison with signed 64-bit integer.
bool operator<(std::uint64_t val) const noexcept
Less than comparison with unsigned 64-bit integer.
Int128(double val)
Construct from double-precision floating-point value.
bool operator==(float val) const noexcept
Equality comparison with single-precision floating-point.
Int128 & operator*=(const Int128 &other) noexcept
Multiplication assignment operator.
bool operator>(const Decimal &val) const noexcept
Greater than comparison with Decimal.
Int128 abs() const noexcept
Get absolute value.
bool operator>=(int val) const noexcept
Greater than or equal comparison with signed 32-bit integer.
constexpr Int128 & operator=(const Int128 &other) noexcept=default
Copy assignment operator.
Int128 isqrt() const
Calculate integer square root (floor of exact square root).
Int128 & operator-=(const Int128 &other) noexcept
Subtraction assignment operator.
Int128(const char *scStr)
Construct from C-string (exact parsing).
bool operator<(double val) const noexcept
Less than comparison with double-precision floating-point.
Int128(float val)
Construct from single-precision floating-point value.
std::strong_ordering operator<=>(const Int128 &other) const noexcept
Three-way comparison operator (C++20).
bool operator<=(std::int64_t val) const noexcept
Less than or equal comparison with signed 64-bit integer.
bool operator<(const Decimal &val) const noexcept
Less than comparison with Decimal.
constexpr Int128(const Int128 &other) noexcept=default
Copy constructor.
bool operator>=(const Decimal &val) const noexcept
Greater than or equal comparison with Decimal.
bool operator!=(std::uint64_t val) const noexcept
Inequality comparison with unsigned 64-bit integer.
Int128(std::string_view str)
Construct from string (exact parsing).
Int128(const Decimal &decimal)
Construct from Decimal value.
std::array< std::int32_t, 4 > toBits() const noexcept
Get internal 32-bit representation.
static bool fromString(std::string_view str, Int128 &result) noexcept
Try to parse 128-bit integer from string without throwing.
bool operator>=(double val) const noexcept
Greater than or equal comparison with double-precision floating-point.
bool operator>(std::uint64_t val) const noexcept
Greater than comparison with unsigned 64-bit integer.
Int128 operator/(const Int128 &other) const
Division operator.
Int128 operator-() const noexcept
Unary minus operator.
bool operator!=(int val) const noexcept
Inequality comparison with signed 32-bit integer.
bool operator>=(std::int64_t val) const noexcept
Greater than or equal comparison with signed 64-bit integer.
bool operator>(float val) const noexcept
Greater than comparison with single-precision floating-point.
Int128 operator+(const Int128 &other) const noexcept
Addition operator.
constexpr Int128() noexcept
Default constructor.
bool operator==(std::uint64_t val) const noexcept
Equality comparison with unsigned 64-bit integer.
Int128 & operator%=(const Int128 &other)
Modulo assignment operator.
std::uint64_t toLow() const noexcept
Get lower 64 bits.
bool operator==(std::int64_t val) const noexcept
Equality comparison with signed 64-bit integer.
bool operator==(double val) const noexcept
Equality comparison with double-precision floating-point.
constexpr Int128 & operator=(Int128 &&other) noexcept=default
Move assignment operator.
bool operator!=(float val) const noexcept
Inequality comparison with single-precision floating-point.
Int128 & operator+=(const Int128 &other) noexcept
Addition assignment operator.
bool operator==(const Decimal &val) const noexcept
Equality comparison with Decimal.
bool operator!=(std::int64_t val) const noexcept
Inequality comparison with signed 64-bit integer.
bool operator>=(float val) const noexcept
Greater than or equal comparison with single-precision floating-point.
bool operator>=(std::uint64_t val) const noexcept
Greater than or equal comparison with unsigned 64-bit integer.
bool operator==(const Int128 &other) const noexcept
Equality operator.
bool operator<(std::int64_t val) const noexcept
Less than comparison with signed 64-bit integer.
bool operator<(int val) const noexcept
Less than comparison with signed 32-bit integer.
bool operator<=(std::uint64_t val) const noexcept
Less than or equal comparison with unsigned 64-bit integer.
std::string toString() const
Convert to string with exact precision.
bool operator<=(double val) const noexcept
Less than or equal comparison with double-precision floating-point.
Int128 operator%(const Int128 &other) const
Modulo operator.
bool operator>(int val) const noexcept
Greater than comparison with signed 32-bit integer.
bool operator<=(const Decimal &val) const noexcept
Less than or equal comparison with Decimal.
Int128 operator*(const Int128 &other) const noexcept
Multiplication operator.
std::uint64_t toHigh() const noexcept
Get upper 64 bits.
constexpr Int128(Int128 &&other) noexcept=default
Move constructor.
bool operator<(float val) const noexcept
Less than comparison with single-precision floating-point.
bool operator!=(double val) const noexcept
Inequality comparison with double-precision floating-point.