nfx-datatypes 0.1.1
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
212 explicit Int128( float val );
213
224 explicit Int128( double val );
225
237 explicit Int128( const Decimal& decimal );
238
243 constexpr Int128( const Int128& other ) noexcept = default;
244
249 constexpr Int128( Int128&& other ) noexcept = default;
250
251 //----------------------------------------------
252 // Destruction
253 //----------------------------------------------
254
256 ~Int128() = default;
257
258 //----------------------------------------------
259 // Assignment
260 //----------------------------------------------
261
267 constexpr Int128& operator=( const Int128& other ) noexcept = default;
268
274 constexpr Int128& operator=( Int128&& other ) noexcept = default;
275
276 //----------------------------------------------
277 // Comparison operators
278 //----------------------------------------------
279
286 inline std::strong_ordering operator<=>( const Int128& other ) const noexcept;
287
294 inline bool operator==( const Int128& other ) const noexcept;
295
296 //----------------------------------------------
297 // Comparison with built-in integer types
298 //----------------------------------------------
299
305 inline bool operator==( int val ) const noexcept;
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==( std::int64_t val ) const noexcept;
348
354 bool operator!=( std::int64_t val ) const noexcept;
355
361 inline 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::uint64_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
426 //----------------------------------------------
427 // Comparison with built-in floating point types
428 //----------------------------------------------
429
435 inline bool operator==( float val ) const noexcept;
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 bool operator==( double val ) const noexcept;
478
484 inline bool operator!=( double val ) const noexcept;
485
491 bool operator<( double val ) const noexcept;
492
498 inline bool operator<=( double val ) const noexcept;
499
505 bool operator>( double val ) const noexcept;
506
512 inline bool operator>=( double val ) const noexcept;
513
514 //----------------------------------------------
515 // Comparison with nfx::datatypes::Decimal
516 //----------------------------------------------
517
524 bool operator==( const Decimal& val ) const noexcept;
525
531 inline bool operator!=( const Decimal& val ) const noexcept;
532
538 bool operator<( const Decimal& val ) const noexcept;
539
545 inline 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
561 //----------------------------------------------
562 // Arithmetic operators
563 //----------------------------------------------
564
570 inline Int128 operator+( const Int128& other ) const noexcept;
571
577 inline Int128 operator-( const Int128& other ) const noexcept;
578
584 Int128 operator*( const Int128& other ) const noexcept;
585
592 Int128 operator/( const Int128& other ) const;
593
599 inline Int128& operator+=( const Int128& other ) noexcept;
600
606 inline Int128& operator-=( const Int128& other ) noexcept;
607
613 inline Int128& operator*=( const Int128& other ) noexcept;
614
621 inline Int128& operator/=( const Int128& other );
622
629 inline Int128& operator%=( const Int128& other );
630
637 inline Int128 operator%( const Int128& other ) const;
638
643 inline Int128 operator-() const noexcept;
644
645 //----------------------------------------------
646 // Property accessors
647 //----------------------------------------------
648
654 [[nodiscard]] std::uint64_t toLow() const noexcept;
655
661 [[nodiscard]] std::uint64_t toHigh() const noexcept;
662
663#if NFX_DATATYPES_HAS_NATIVE_INT128
678 NFX_DATATYPES_NATIVE_INT128 toNative() const noexcept;
679#endif
680
681 //----------------------------------------------
682 // Mathematical operations
683 //----------------------------------------------
684
690 [[nodiscard]] inline Int128 abs() const noexcept;
691
700 [[nodiscard]] Int128 isqrt() const;
701
702 //----------------------------------------------
703 // String parsing
704 //----------------------------------------------
705
723 [[nodiscard]] static bool fromString( std::string_view str, Int128& result ) noexcept;
724
741 [[nodiscard]] static std::optional<Int128> fromString( std::string_view str ) noexcept;
742
743 //----------------------------------------------
744 // Type conversion
745 //----------------------------------------------
746
752 [[nodiscard]] std::string toString() const;
753
759 [[nodiscard]] std::array<std::int32_t, 4> toBits() const noexcept;
760
761 private:
762 //----------------------------------------------
763 // Internal representation
764 //----------------------------------------------
765
766#if NFX_DATATYPES_HAS_NATIVE_INT128
768 NFX_DATATYPES_NATIVE_INT128 m_value;
769#else
770 struct Layout
771 {
773 std::uint64_t lower64bits;
774 std::uint64_t upper64bits;
775 } m_layout;
776#endif
777 };
778
779 //=====================================================================
780 // Free functions
781 //=====================================================================
782
791 [[nodiscard]] inline Int128 abs( const Int128& value ) noexcept
792 {
793 return value.abs();
794 }
795
805 [[nodiscard]] inline Int128 isqrt( const Int128& value )
806 {
807 return value.isqrt();
808 }
809
829 [[nodiscard]] Decimal sqrt( const Int128& value );
830} // namespace nfx::datatypes
831
832#include "nfx/detail/datatypes/Int128.inl"
Int128 isqrt(const Int128 &value)
Calculate integer square root (free function).
Definition Int128.h:805
Decimal sqrt(const Decimal &value)
Compute square root using Newton-Raphson method (free function).
Definition Decimal.h:908
Decimal abs(const Decimal &value) noexcept
Get absolute value of decimal (free function).
Definition Decimal.h:894
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.
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.