omw  0.2.1-beta
int.h
1 /*
2 author Oliver Blaser
3 date 27.01.2022
4 copyright MIT - Copyright (c) 2022 Oliver Blaser
5 */
6 
7 #ifndef IG_OMW_INT_H
8 #define IG_OMW_INT_H
9 
10 #include <cstddef>
11 #include <cstdint>
12 
13 #include "../omw/intdef.h"
14 
15 //#define OMWi_INT_RIGHTSHIFT_DEBUG (1) // def/undef
16 
17 namespace omw
18 {
24  {
25  public:
26  Base_Int128();
27  Base_Int128(const omw::Base_Int128& other);
28  Base_Int128(int64_t value);
29  Base_Int128(uint64_t valueH, uint64_t valueL);
30  Base_Int128(uint32_t valueHH, uint32_t valueLH, uint32_t valueHL, uint32_t valueLL);
31  virtual ~Base_Int128() {}
32 
33  void set(uint64_t valueH, uint64_t valueL);
34  void set(uint32_t valueHH, uint32_t valueLH, uint32_t valueHL, uint32_t valueLL);
35  void sets(int64_t value);
36  void sets(int64_t valueH, uint64_t valueL);
37  void sets(int32_t valueHH, uint32_t valueLH, uint32_t valueHL, uint32_t valueLL);
38  void sets(const uint8_t* data, size_t count);
39  void setu(uint64_t value);
40  void setu(const uint8_t* data, size_t count);
41 
42  uint64_t high() const { return m_h; }
43  uint64_t hi() const { return high(); }
44  int64_t highs() const;
45  int64_t his() const { return highs(); }
46  uint64_t low() const { return m_l; }
47  uint64_t lo() const { return low(); }
48 
49  explicit operator bool() const { return (m_h || m_l); }
51  protected:
52  uint64_t m_h;
53  uint64_t m_l;
54 
55  void copy(const omw::Base_Int128& other);
56 
57  private:
58  void readBuffer(const uint8_t* data, size_t count);
59 
60  public:
63  omw::Base_Int128& operator+=(const omw::Base_Int128& b);
64  omw::Base_Int128& operator-=(const omw::Base_Int128& b);
65  omw::Base_Int128& operator&=(const omw::Base_Int128& b);
66  omw::Base_Int128& operator|=(const omw::Base_Int128& b);
67  omw::Base_Int128& operator^=(const omw::Base_Int128& b);
68  omw::Base_Int128& operator<<=(unsigned int count);
70  omw::Base_Int128& operator++();
71  omw::Base_Int128& operator--();
72  omw::Base_Int128 operator++(int);
73  omw::Base_Int128 operator--(int);
75  };
76 
78  {
79  public:
80  SignedInt128();
81  SignedInt128(const omw::SignedInt128& other);
82  SignedInt128(int64_t value);
83  SignedInt128(uint64_t valueH, uint64_t valueL);
84  SignedInt128(uint32_t valueHH, uint32_t valueLH, uint32_t valueHL, uint32_t valueLL);
85  SignedInt128(const omw::Base_Int128& other);
86  virtual ~SignedInt128() {}
87 
88  bool isNegative() const;
89  int sign() const;
90 
91  public:
94  omw::SignedInt128& operator=(const omw::SignedInt128& b);
95  omw::SignedInt128& operator>>=(unsigned int count);
96 
98 #ifdef OMWi_INT_RIGHTSHIFT_DEBUG
99  uint64_t oldValue_h, oldValue_l;
100  uint64_t lastMask_h, lastMask_l;
101 #endif
102  };
103 
105  {
106  public:
107  UnsignedInt128();
108  UnsignedInt128(const omw::UnsignedInt128& other);
109  UnsignedInt128(int64_t value);
110  UnsignedInt128(uint64_t valueH, uint64_t valueL);
111  UnsignedInt128(uint32_t valueHH, uint32_t valueLH, uint32_t valueHL, uint32_t valueLL);
112  UnsignedInt128(const omw::Base_Int128& other);
113  virtual ~UnsignedInt128() {}
114 
115  public:
118  omw::UnsignedInt128& operator=(const omw::UnsignedInt128& b);
119  omw::UnsignedInt128& operator>>=(unsigned int count);
120  };
122 
127 
130 
133 
136 
137  omw::SignedInt128 operator~(const omw::SignedInt128& a);
138  omw::UnsignedInt128 operator~(const omw::UnsignedInt128& a);
139 
140  omw::SignedInt128 operator&(const omw::SignedInt128& a, const omw::Base_Int128& b);
141  omw::UnsignedInt128 operator&(const omw::UnsignedInt128& a, const omw::Base_Int128& b);
142 
143  omw::SignedInt128 operator|(const omw::SignedInt128& a, const omw::Base_Int128& b);
144  omw::UnsignedInt128 operator|(const omw::UnsignedInt128& a, const omw::Base_Int128& b);
145 
146  omw::SignedInt128 operator^(const omw::SignedInt128& a, const omw::Base_Int128& b);
147  omw::UnsignedInt128 operator^(const omw::UnsignedInt128& a, const omw::Base_Int128& b);
148 
149  omw::SignedInt128 operator<<(const omw::SignedInt128& a, unsigned int count);
150  omw::UnsignedInt128 operator<<(const omw::UnsignedInt128& a, unsigned int count);
152  omw::SignedInt128 operator>>(const omw::SignedInt128& a, unsigned int count);
153  omw::UnsignedInt128 operator>>(const omw::UnsignedInt128& a, unsigned int count);
155  // All combinations of the comparison operators are needed to achieve sign awareness.
156  bool operator==(const omw::SignedInt128& a, const omw::SignedInt128& b);
157  bool operator!=(const omw::SignedInt128& a, const omw::SignedInt128& b);
158  bool operator<(const omw::SignedInt128& a, const omw::SignedInt128& b);
159  bool operator>(const omw::SignedInt128& a, const omw::SignedInt128& b);
160  bool operator<=(const omw::SignedInt128& a, const omw::SignedInt128& b);
161  bool operator>=(const omw::SignedInt128& a, const omw::SignedInt128& b);
162 
163  bool operator==(const omw::SignedInt128& a, const omw::UnsignedInt128& b);
165  bool operator<(const omw::SignedInt128& a, const omw::UnsignedInt128& b);
166  bool operator>(const omw::SignedInt128& a, const omw::UnsignedInt128& b);
167  bool operator<=(const omw::SignedInt128& a, const omw::UnsignedInt128& b);
168  bool operator>=(const omw::SignedInt128& a, const omw::UnsignedInt128& b);
170  bool operator==(const omw::UnsignedInt128& a, const omw::SignedInt128& b);
172  bool operator<(const omw::UnsignedInt128& a, const omw::SignedInt128& b);
173  bool operator>(const omw::UnsignedInt128& a, const omw::SignedInt128& b);
174  bool operator<=(const omw::UnsignedInt128& a, const omw::SignedInt128& b);
175  bool operator>=(const omw::UnsignedInt128& a, const omw::SignedInt128& b);
177  bool operator==(const omw::UnsignedInt128& a, const omw::UnsignedInt128& b);
178  bool operator!=(const omw::UnsignedInt128& a, const omw::UnsignedInt128& b);
179  bool operator<(const omw::UnsignedInt128& a, const omw::UnsignedInt128& b);
180  bool operator>(const omw::UnsignedInt128& a, const omw::UnsignedInt128& b);
181  bool operator<=(const omw::UnsignedInt128& a, const omw::UnsignedInt128& b);
182  bool operator>=(const omw::UnsignedInt128& a, const omw::UnsignedInt128& b);
184 
190  // move to bitset.h
191  //
192  // bitset[0] = LSB / throws std::overflow_error if the value can not be represented
193  //template<size_t N>
194  //int128_t bitset_to_int128(const std::bitset<N>& bitset);
195  //template<size_t N>
196  //int128_t bitset_to_uint128(const std::bitset<N>& bitset);
197  //template<size_t N>
198  //std::bitset<N> to_bitset(const omw::Base_Int128& value);
199 }
200 
205 #define OMW_128BIT_ALL (omw::Base_Int128(OMW_64BIT_ALL, OMW_64BIT_ALL))
206 #define OMW_128BIT_LSB (omw::Base_Int128(0, OMW_64BIT_LSB))
207 #define OMW_128BIT_MSB (omw::Base_Int128(OMW_64BIT_MSB, 0))
208 
209 #define OMW_INT128_MIN (omw::int128_t(OMW_64BIT_MSB, 0))
210 #define OMW_INT128_MAX (omw::int128_t(~OMW_64BIT_MSB, OMW_64BIT_ALL))
211 #define OMW_UINT128_MAX (omw::uint128_t(OMW_64BIT_ALL, OMW_64BIT_ALL))
212 
215 #endif // IG_OMW_INT_H
omw::operator+
omw::Color operator+(const omw::Color &lhs, const omw::Color &rhs)
Definition: color.cpp:433
omw::Base_Int128::sets
void sets(int64_t value)
Definition: int.cpp:98
omw::operator<<
omw::SignedInt128 operator<<(const omw::SignedInt128 &a, unsigned int count)
omw::Base_Int128::operator<<=
omw::Base_Int128 & operator<<=(unsigned int count)
Definition: int.cpp:227
omw::operator!=
bool operator!=(const omw::Color &lhs, const omw::Color &rhs)
Definition: color.cpp:451
omw::operator==
bool operator==(const omw::Color &lhs, const omw::Color &rhs)
Definition: color.cpp:443
omw::operator-
omw::SignedInt128 operator-(const omw::SignedInt128 &a)
Definition: int.cpp:531
omw::SignedInt128
Definition: int.h:78
omw::Base_Int128::setu
void setu(uint64_t value)
Definition: int.cpp:143
omw::UnsignedInt128::operator>>=
omw::UnsignedInt128 & operator>>=(unsigned int count)
Definition: int.cpp:443
omw::SignedInt128::sign
int sign() const
Definition: int.cpp:329
omw::UnsignedInt128
Definition: int.h:105
omw::operator>>
omw::SignedInt128 operator>>(const omw::SignedInt128 &a, unsigned int count)
Definition: int.cpp:562
omw::Base_Int128
Definition: int.h:24
omw::SignedInt128::operator>>=
omw::SignedInt128 & operator>>=(unsigned int count)
Definition: int.cpp:340
omw
Main namespace.