ZenLib
int128u.h
Go to the documentation of this file.
1 // int128u - integer 8 bytes
2 // Copyright (C) 2007-2011 MediaArea.net SARL, Info@MediaArea.net
3 //
4 // This software is provided 'as-is', without any express or implied
5 // warranty. In no event will the authors be held liable for any damages
6 // arising from the use of this software.
7 //
8 // Permission is granted to anyone to use this software for any purpose,
9 // including commercial applications, and to alter it and redistribute it
10 // freely, subject to the following restrictions:
11 //
12 // 1. The origin of this software must not be misrepresented; you must not
13 // claim that you wrote the original software. If you use this software
14 // in a product, an acknowledgment in the product documentation would be
15 // appreciated but is not required.
16 // 2. Altered source versions must be plainly marked as such, and must not be
17 // misrepresented as being the original software.
18 // 3. This notice may not be removed or altered from any source distribution.
19 //
20 //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
21 //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
22 //
23 // based on http://Tringi.Mx-3.cz
24 // Only adapted for ZenLib:
25 // - .hpp --> .h
26 // - Namespace
27 // - int128u alias
28 //
29 //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
30 
31 #ifndef UINT128_HPP
32 #define UINT128_HPP
33 
34 /*
35  Name: uint128.hpp
36  Copyright: Copyright (C) 2005, Jan Ringos
37  Author: Jan Ringos, http://Tringi.Mx-3.cz
38 
39  Version: 1.1
40 */
41 
42 
43 #include <exception>
44 #include <cstdlib>
45 #include <cstdio>
46 #include <new>
47 #include "ZenLib/Conf.h"
48 
49 // CLASS
50 
51 namespace ZenLib
52 {
53 
54 class uint128 {
55  public://private:
56  // Binary correct representation of signed 128bit integer
57  int64u lo;
58  int64u hi;
59 
60  protected:
61  // Some global operator functions must be friends
62  friend bool operator < (const uint128 &, const uint128 &) throw ();
63  friend bool operator == (const uint128 &, const uint128 &) throw ();
64  friend bool operator || (const uint128 &, const uint128 &) throw ();
65  friend bool operator && (const uint128 &, const uint128 &) throw ();
66 
67  public:
68  // Constructors
69  inline uint128 () throw () {};
70  inline uint128 (const uint128 & a) throw () : lo (a.lo), hi (a.hi) {};
71 
72  inline uint128 (const int & a) throw () : lo (a), hi (0ull) {};
73  inline uint128 (const unsigned int & a) throw () : lo (a), hi (0ull) {};
74  inline uint128 (const int64u & a) throw () : lo (a), hi (0ull) {};
75 
76  uint128 (const float a) throw ();
77  uint128 (const double & a) throw ();
78  uint128 (const long double & a) throw ();
79 
80  uint128 (const char * sz) throw ();
81 
82  // TODO: Consider creation of operator= to eliminate
83  // the need of intermediate objects during assignments.
84 
85  private:
86  // Special internal constructors
87  uint128 (const int64u & a, const int64u & b) throw ()
88  : lo (a), hi (b) {};
89 
90  public:
91  // Operators
92  bool operator ! () const throw ();
93 
94  uint128 operator - () const throw ();
95  uint128 operator ~ () const throw ();
96 
97  uint128 & operator ++ ();
98  uint128 & operator -- ();
99  uint128 operator ++ (int);
100  uint128 operator -- (int);
101 
102  uint128 & operator += (const uint128 & b) throw ();
103  uint128 & operator *= (const uint128 & b) throw ();
104 
105  uint128 & operator >>= (unsigned int n) throw ();
106  uint128 & operator <<= (unsigned int n) throw ();
107 
108  uint128 & operator |= (const uint128 & b) throw ();
109  uint128 & operator &= (const uint128 & b) throw ();
110  uint128 & operator ^= (const uint128 & b) throw ();
111 
112  // Inline simple operators
113  inline const uint128 & operator + () const throw () { return *this; };
114 
115  // Rest of inline operators
116  inline uint128 & operator -= (const uint128 & b) throw () {
117  return *this += (-b);
118  };
119  inline uint128 & operator /= (const uint128 & b) throw () {
120  uint128 dummy;
121  *this = this->div (b, dummy);
122  return *this;
123  };
124  inline uint128 & operator %= (const uint128 & b) throw () {
125  this->div (b, *this);
126  return *this;
127  };
128 
129  // Common methods
130  unsigned int toUint () const throw () {
131  return (unsigned int) this->lo; };
132  int64u toUint64 () const throw () {
133  return (int64u) this->lo; };
134  const char * toString (unsigned int radix = 10) const throw ();
135  float toFloat () const throw ();
136  double toDouble () const throw ();
137  long double toLongDouble () const throw ();
138 
139  // Arithmetic methods
140  uint128 div (const uint128 &, uint128 &) const throw ();
141 
142  // Bit operations
143  bool bit (unsigned int n) const throw ();
144  void bit (unsigned int n, bool val) throw ();
145 }
146 #ifdef __GNUC__
147  __attribute__ ((__aligned__ (16), __packed__))
148 #endif
149 ;
150 
151 
152 // GLOBAL OPERATORS
153 
154 bool operator < (const uint128 & a, const uint128 & b) throw ();
155 bool operator == (const uint128 & a, const uint128 & b) throw ();
156 bool operator || (const uint128 & a, const uint128 & b) throw ();
157 bool operator && (const uint128 & a, const uint128 & b) throw ();
158 
159 // GLOBAL OPERATOR INLINES
160 
161 inline uint128 operator + (const uint128 & a, const uint128 & b) throw () {
162  return uint128 (a) += b; };
163 inline uint128 operator - (const uint128 & a, const uint128 & b) throw () {
164  return uint128 (a) -= b; };
165 inline uint128 operator * (const uint128 & a, const uint128 & b) throw () {
166  return uint128 (a) *= b; };
167 inline uint128 operator / (const uint128 & a, const uint128 & b) throw () {
168  return uint128 (a) /= b; };
169 inline uint128 operator % (const uint128 & a, const uint128 & b) throw () {
170  return uint128 (a) %= b; };
171 
172 inline uint128 operator >> (const uint128 & a, unsigned int n) throw () {
173  return uint128 (a) >>= n; };
174 inline uint128 operator << (const uint128 & a, unsigned int n) throw () {
175  return uint128 (a) <<= n; };
176 
177 inline uint128 operator & (const uint128 & a, const uint128 & b) throw () {
178  return uint128 (a) &= b; };
179 inline uint128 operator | (const uint128 & a, const uint128 & b) throw () {
180  return uint128 (a) |= b; };
181 inline uint128 operator ^ (const uint128 & a, const uint128 & b) throw () {
182  return uint128 (a) ^= b; };
183 
184 inline bool operator > (const uint128 & a, const uint128 & b) throw () {
185  return b < a; };
186 inline bool operator <= (const uint128 & a, const uint128 & b) throw () {
187  return !(b < a); };
188 inline bool operator >= (const uint128 & a, const uint128 & b) throw () {
189  return !(a < b); };
190 inline bool operator != (const uint128 & a, const uint128 & b) throw () {
191  return !(a == b); };
192 
193 
194 // MISC
195 
196 typedef uint128 __uint128;
197 
198 typedef uint128 int128u;
199 } //NameSpace
200 
201 #endif