omw  0.2.0
int.h
1 /*
2 author Oliver Blaser
3 date 11.01.2022
4 copyright MIT - Copyright (c) 2022 Oliver Blaser
5 */
6 
7 #ifndef IG_OMW_OMWINT_H
8 #define IG_OMW_OMWINT_H
9 
10 #include <cstddef>
11 #include <cstdint>
12 
13 //#define OMWi_INT_RIGHTSHIFT_DEBUG (1) // def/undef
14 
15 
16 
21 #define OMW_8BIT_ALL (0xFF)
22 #define OMW_8BIT_LSB (0x01)
23 #define OMW_8BIT_MSB (0x80)
24 
25 #define OMW_16BIT_ALL (0xFFFF)
26 #define OMW_16BIT_LSB (0x0001)
27 #define OMW_16BIT_MSB (0x8000)
28 
29 #define OMW_32BIT_ALL (0xFFFFFFFF)
30 #define OMW_32BIT_LSB (0x00000001)
31 #define OMW_32BIT_MSB (0x80000000)
32 
33 #define OMW_64BIT_ALL (0xFFFFFFFFFFFFFFFF)
34 #define OMW_64BIT_LSB (0x0000000000000001)
35 #define OMW_64BIT_MSB (0x8000000000000000)
36 
39 namespace omw
40 {
46  {
47  public:
48  Base_Int128();
49  Base_Int128(const omw::Base_Int128& other);
50  Base_Int128(int64_t value);
51  Base_Int128(uint64_t valueH, uint64_t valueL);
52  Base_Int128(uint32_t valueHH, uint32_t valueLH, uint32_t valueHL, uint32_t valueLL);
53  virtual ~Base_Int128() {}
54 
55  void set(uint64_t valueH, uint64_t valueL);
56  void set(uint32_t valueHH, uint32_t valueLH, uint32_t valueHL, uint32_t valueLL);
57  void sets(int64_t value);
58  void sets(int64_t valueH, uint64_t valueL);
59  void sets(int32_t valueHH, uint32_t valueLH, uint32_t valueHL, uint32_t valueLL);
60  void sets(const uint8_t* data, size_t count);
61  void setu(uint64_t value);
62  void setu(const uint8_t* data, size_t count);
63 
64  uint64_t high() const { return m_h; }
65  uint64_t hi() const { return high(); }
66  int64_t highs() const;
67  int64_t his() const { return highs(); }
68  uint64_t low() const { return m_l; }
69  uint64_t lo() const { return low(); }
70 
71  explicit operator bool() const { return (m_h || m_l); }
73  protected:
74  uint64_t m_h;
75  uint64_t m_l;
76 
77  void copy(const omw::Base_Int128& other);
78 
79  private:
80  void readBuffer(const uint8_t* data, size_t count);
81 
82  public:
85  omw::Base_Int128& operator+=(const omw::Base_Int128& b);
86  omw::Base_Int128& operator-=(const omw::Base_Int128& b);
87  omw::Base_Int128& operator&=(const omw::Base_Int128& b);
88  omw::Base_Int128& operator|=(const omw::Base_Int128& b);
89  omw::Base_Int128& operator^=(const omw::Base_Int128& b);
90  omw::Base_Int128& operator<<=(unsigned int count);
92  omw::Base_Int128& operator++();
93  omw::Base_Int128& operator--();
94  omw::Base_Int128 operator++(int);
95  omw::Base_Int128 operator--(int);
97  };
98 
100  {
101  public:
102  SignedInt128();
103  SignedInt128(const omw::SignedInt128& other);
104  SignedInt128(int64_t value);
105  SignedInt128(uint64_t valueH, uint64_t valueL);
106  SignedInt128(uint32_t valueHH, uint32_t valueLH, uint32_t valueHL, uint32_t valueLL);
107  SignedInt128(const omw::Base_Int128& other);
108  virtual ~SignedInt128() {}
109 
110  bool isNegative() const;
111  int sign() const;
112 
113  public:
116  omw::SignedInt128& operator=(const omw::SignedInt128& b);
117  omw::SignedInt128& operator>>=(unsigned int count);
118 
120 #ifdef OMWi_INT_RIGHTSHIFT_DEBUG
121  uint64_t oldValue_h, oldValue_l;
122  uint64_t lastMask_h, lastMask_l;
123 #endif
124  };
125 
127  {
128  public:
129  UnsignedInt128();
130  UnsignedInt128(const omw::UnsignedInt128& other);
131  UnsignedInt128(int64_t value);
132  UnsignedInt128(uint64_t valueH, uint64_t valueL);
133  UnsignedInt128(uint32_t valueHH, uint32_t valueLH, uint32_t valueHL, uint32_t valueLL);
134  UnsignedInt128(const omw::Base_Int128& other);
135  virtual ~UnsignedInt128() {}
136 
137  public:
140  omw::UnsignedInt128& operator=(const omw::UnsignedInt128& b);
141  omw::UnsignedInt128& operator>>=(unsigned int count);
142  };
144 
149 
152 
155 
158 
159  omw::SignedInt128 operator~(const omw::SignedInt128& a);
160  omw::UnsignedInt128 operator~(const omw::UnsignedInt128& a);
161 
162  omw::SignedInt128 operator&(const omw::SignedInt128& a, const omw::Base_Int128& b);
163  omw::UnsignedInt128 operator&(const omw::UnsignedInt128& a, const omw::Base_Int128& b);
164 
165  omw::SignedInt128 operator|(const omw::SignedInt128& a, const omw::Base_Int128& b);
166  omw::UnsignedInt128 operator|(const omw::UnsignedInt128& a, const omw::Base_Int128& b);
167 
168  omw::SignedInt128 operator^(const omw::SignedInt128& a, const omw::Base_Int128& b);
169  omw::UnsignedInt128 operator^(const omw::UnsignedInt128& a, const omw::Base_Int128& b);
170 
171  omw::SignedInt128 operator<<(const omw::SignedInt128& a, unsigned int count);
172  omw::UnsignedInt128 operator<<(const omw::UnsignedInt128& a, unsigned int count);
174  omw::SignedInt128 operator>>(const omw::SignedInt128& a, unsigned int count);
175  omw::UnsignedInt128 operator>>(const omw::UnsignedInt128& a, unsigned int count);
177  // All combinations of the comparison operators are needed to achieve sign awareness.
178  bool operator==(const omw::SignedInt128& a, const omw::SignedInt128& b);
179  bool operator!=(const omw::SignedInt128& a, const omw::SignedInt128& b);
180  bool operator<(const omw::SignedInt128& a, const omw::SignedInt128& b);
181  bool operator>(const omw::SignedInt128& a, const omw::SignedInt128& b);
182  bool operator<=(const omw::SignedInt128& a, const omw::SignedInt128& b);
183  bool operator>=(const omw::SignedInt128& a, const omw::SignedInt128& b);
184 
185  bool operator==(const omw::SignedInt128& a, const omw::UnsignedInt128& b);
187  bool operator<(const omw::SignedInt128& a, const omw::UnsignedInt128& b);
188  bool operator>(const omw::SignedInt128& a, const omw::UnsignedInt128& b);
189  bool operator<=(const omw::SignedInt128& a, const omw::UnsignedInt128& b);
190  bool operator>=(const omw::SignedInt128& a, const omw::UnsignedInt128& b);
192  bool operator==(const omw::UnsignedInt128& a, const omw::SignedInt128& b);
194  bool operator<(const omw::UnsignedInt128& a, const omw::SignedInt128& b);
195  bool operator>(const omw::UnsignedInt128& a, const omw::SignedInt128& b);
196  bool operator<=(const omw::UnsignedInt128& a, const omw::SignedInt128& b);
197  bool operator>=(const omw::UnsignedInt128& a, const omw::SignedInt128& b);
199  bool operator==(const omw::UnsignedInt128& a, const omw::UnsignedInt128& b);
200  bool operator!=(const omw::UnsignedInt128& a, const omw::UnsignedInt128& b);
201  bool operator<(const omw::UnsignedInt128& a, const omw::UnsignedInt128& b);
202  bool operator>(const omw::UnsignedInt128& a, const omw::UnsignedInt128& b);
203  bool operator<=(const omw::UnsignedInt128& a, const omw::UnsignedInt128& b);
204  bool operator>=(const omw::UnsignedInt128& a, const omw::UnsignedInt128& b);
206 
212  // move to bitset.h
213  //
214  // bitset[0] = LSB / throws std::overflow_error if the value can not be represented
215  //template<size_t N>
216  //int128_t bitset_to_int128(const std::bitset<N>& bitset);
217  //template<size_t N>
218  //int128_t bitset_to_uint128(const std::bitset<N>& bitset);
219  //template<size_t N>
220  //std::bitset<N> to_bitset(const omw::Base_Int128& value);
221 }
222 
227 #define OMW_128BIT_ALL (omw::Base_Int128(OMW_64BIT_ALL, OMW_64BIT_ALL))
228 #define OMW_128BIT_LSB (omw::Base_Int128(0, OMW_64BIT_LSB))
229 #define OMW_128BIT_MSB (omw::Base_Int128(OMW_64BIT_MSB, 0))
230 
231 #define OMW_INT128_MIN (omw::int128_t(OMW_64BIT_MSB, 0))
232 #define OMW_INT128_MAX (omw::int128_t(~OMW_64BIT_MSB, OMW_64BIT_ALL))
233 #define OMW_UINT128_MAX (omw::uint128_t(OMW_64BIT_ALL, OMW_64BIT_ALL))
234 
237 #endif // IG_OMW_OMWINT_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:97
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:226
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:530
omw::SignedInt128
Definition: int.h:100
omw::Base_Int128::setu
void setu(uint64_t value)
Definition: int.cpp:142
omw::UnsignedInt128::operator>>=
omw::UnsignedInt128 & operator>>=(unsigned int count)
Definition: int.cpp:442
omw::SignedInt128::sign
int sign() const
Definition: int.cpp:328
omw::UnsignedInt128
Definition: int.h:127
omw::operator>>
omw::SignedInt128 operator>>(const omw::SignedInt128 &a, unsigned int count)
Definition: int.cpp:561
omw::Base_Int128
Definition: int.h:46
omw::SignedInt128::operator>>=
omw::SignedInt128 & operator>>=(unsigned int count)
Definition: int.cpp:339
omw
Main namespace.