Int128_t.html 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html><head>
  2. <meta http-equiv="content-type" content="text/html; charset=ISO-8859-1">
  3. <title>Int128_t</title>
  4. <link type="text/css" rel="stylesheet" href="UTFDoc.css">
  5. <meta name="author" content="Paul Pedriana">
  6. </head>
  7. <body bgcolor="#FFFFFF">
  8. <h1>int128_t</h1>
  9. <h2>Introduction</h2>
  10. <p>The int128_t module implements the int128_t and uint128_t integer data types
  11. via C++ classes. These types are largely identical in behavior to how a compiler's
  12. built-in int128_t / uint128_t would be. These classes exist because compilers
  13. don't provide true 128 bit data types. The most you'll typically see from a
  14. compiler is int64_t / uint64_t. </p>
  15. <p>128 bit integers are useful for things such as the following:</p>
  16. <ul>
  17. <li>Storing very large numbers or bitfields. This can be useful when attempting
  18. to simulate city or world economies, for example.</li>
  19. <li>Implementing 64 * 64 bit multiplication without losing data.</li>
  20. <li>Implementing very precise fixed point mathematics.</li>
  21. </ul>
  22. <h2> Example usage </h2>
  23. <p>For the most part, you can use int128_t the same way you would use int64_t.
  24. </p>
  25. <p>Mixed integer math expressions:</p>
  26. <pre class="code-example">int a(17);
  27. short b(26);
  28. int128_t c(45);
  29. int64_t d(77);
  30. unsigned short e(25);
  31. uint128_t x(13);
  32. uint128_t y;
  33. y = (((x + (a + b) * 37) / c) * x) % (d + e);</pre>
  34. <p>Logical expressions:</p>
  35. <pre class="code-example">uint128_t x;<br>uint128_t y(&quot;0x11111111000100001111111100000001&quot;, 16); // Assign a full 128 bit value of base 16 via a string.<br>uint128_t z(&quot;0x22222222000100002222222200000001&quot;, 16);
  36. <br>x = (y ^ z) + (y &amp; z) + (y | z);</pre>
  37. <blockquote></blockquote>
  38. <p>Floating point:</p>
  39. <pre class="code-example">uint128_t x = 143249.f;
  40. x *= 32245.6f
  41. <br>printf(&quot;%f&quot;, x.AsFloat());</pre>
  42. <p>Mixture of int128_t and uint128_t:</p>
  43. <pre class="code-example">int128_t x(-1000000000);<br>uint128_t y(120);
  44. <br>x *= y; </pre>
  45. <p>Converting int128_t to a string:</p>
  46. <pre class="code-example">char buffer[64];
  47. int128_t x = 10345;
  48. s.Int128ToStr(buffer, NULL, 10); // Just like strtol().</pre>
  49. <p>Set int128_t as a very large value from a string:</p>
  50. <pre class="code-example">uint128_t x("141183460469231731687303715884105728");</pre>
  51. <p>Set int128_t as a very large value from two 64 bit values:</p>
  52. <pre class="code-example">uint128_t x(0x1122334455667788);
  53. x &lt;&lt;= 64;
  54. x |= 0x8877665544332211;</pre>
  55. <h2>
  56. Limitations</h2>
  57. <p>The primary differences between our int128_t and a hypothetical built-in version
  58. are:</p>
  59. <ul>
  60. <li>int128_t doesn't implement cast operators to lesser integer types. Instead,
  61. provides explicit converters such as AsInt32, AsInt64, etc. This is by design,
  62. as such casting operators result in ambiguous conversions and would need built-in
  63. compiler knowledge to resolve the situation.</li>
  64. <li>Standard library functions such as sprintf have no support for such 128
  65. bit types. We partially rectify this by providing StrToInt128 and Int128ToStr
  66. functions.</li>
  67. <li>128 bit contant literals aren't supported by the compiler, so we implement
  68. them via strings (and indirectly via shifting). You can assign an 8 through
  69. 64 bit integral constant to int128_t, but if you want to assign a full 128
  70. bit constant to an int128_t, you'll need to use a string or use two 64 bit
  71. constants, one for low and one for high.</li>
  72. </ul>
  73. <h2>Interface </h2>
  74. <pre class="code-example">class int128_t
  75. {
  76. public:
  77. int128_t();
  78. int128_t(uint32_t nPart0, uint32_t nPart1, uint32_t nPart2, uint32_t nPart3);
  79. int128_t(uint64_t nPart0, uint64_t nPart1);
  80. int128_t(int8_t value);
  81. int128_t(uint8_t value);
  82. int128_t(int16_t value);
  83. int128_t(uint16_t value);
  84. int128_t(int32_t value);
  85. int128_t(uint32_t value);
  86. int128_t(int64_t value);
  87. int128_t(uint64_t value);
  88. int128_t(const int128_t& value);
  89. int128_t(const float value);
  90. int128_t(const double value);
  91. int128_t(const char* pValue, int nBase = 10);
  92. int128_t(const wchar_t* pValue, int nBase = 10);
  93. // Assignment operator
  94. int128_t& operator=(const int128_t_base& value);
  95. // Unary arithmetic/logic operators
  96. int128_t operator-() const;
  97. int128_t& operator++();
  98. int128_t& operator--();
  99. int128_t operator++(int);
  100. int128_t operator--(int);
  101. int128_t operator~() const;
  102. int128_t operator+() const;
  103. // Math operators
  104. friend int128_t operator+(const int128_t& value1, const int128_t& value2);
  105. friend int128_t operator-(const int128_t& value1, const int128_t& value2);
  106. friend int128_t operator*(const int128_t& value1, const int128_t& value2);
  107. friend int128_t operator/(const int128_t& value1, const int128_t& value2);
  108. friend int128_t operator%(const int128_t& value1, const int128_t& value2);
  109. int128_t& operator+=(const int128_t& value);
  110. int128_t& operator-=(const int128_t& value);
  111. int128_t& operator*=(const int128_t& value);
  112. int128_t& operator/=(const int128_t& value);
  113. int128_t& operator%=(const int128_t& value);
  114. // Shift operators
  115. int128_t operator>> (int nShift) const;
  116. int128_t operator<< (int nShift) const;
  117. int128_t& operator>>=(int nShift);
  118. int128_t& operator<<=(int nShift);
  119. // Logical operators
  120. friend int128_t operator^(const int128_t& value1, const int128_t& value2);
  121. friend int128_t operator|(const int128_t& value1, const int128_t& value2);
  122. friend int128_t operator&(const int128_t& value1, const int128_t& value2);
  123. int128_t& operator^= (const int128_t& value);
  124. int128_t& operator|= (const int128_t& value);
  125. int128_t& operator&= (const int128_t& value);
  126. // Equality operators
  127. friend int compare (const int128_t& value1, const int128_t& value2);
  128. friend bool operator==(const int128_t& value1, const int128_t& value2);
  129. friend bool operator!=(const int128_t& value1, const int128_t& value2);
  130. friend bool operator> (const int128_t& value1, const int128_t& value2);
  131. friend bool operator>=(const int128_t& value1, const int128_t& value2);
  132. friend bool operator< (const int128_t& value1, const int128_t& value2);
  133. friend bool operator<=(const int128_t& value1, const int128_t& value2);
  134. // Operators to convert back to basic types
  135. int8_t AsInt8() const;
  136. int16_t AsInt16() const;
  137. int32_t AsInt32() const;
  138. int64_t AsInt64() const;
  139. float AsFloat() const;
  140. double AsDouble() const;
  141. // Misc. Functions
  142. void Negate();
  143. bool IsNegative() const;
  144. bool IsPositive() const;
  145. void Modulus(const int128_t& divisor, int128_t& quotient, int128_t& remainder) const;
  146. // String conversion functions
  147. int128_t StrToInt128(const char* pValue, char** ppEnd, int base) const;
  148. int128_t StrToInt128(const wchar_t* pValue, wchar_t** ppEnd, int base) const;
  149. void Int128ToStr(char* pValue, char** ppEnd, int base) const;
  150. void Int128ToStr(wchar_t* pValue, wchar_t** ppEnd, int base) const;
  151. }; <span class="code-example-comment">// class int128_t</span>
  152. class uint128_t<br>{<br>public:<br> uint128_t();<br> uint128_t(uint32_t nPart0, uint32_t nPart1, uint32_t nPart2, uint32_t nPart3);<br> uint128_t(uint64_t nPart0, uint64_t nPart1);
  153. uint128_t(int8_t value);
  154. uint128_t(uint8_t value);
  155. uint128_t(int16_t value);<br> uint128_t(uint16_t value);
  156. <br> uint128_t(int32_t value);<br> uint128_t(uint32_t value);
  157. <br> uint128_t(int64_t value);<br> uint128_t(uint64_t value);
  158. <br> uint128_t(const int128_t&amp; value);<br> uint128_t(const uint128_t&amp; value);
  159. <br> uint128_t(const float value);<br> uint128_t(const double value);
  160. <br> uint128_t(const char* pValue, int nBase = 10);<br> uint128_t(const wchar_t* pValue, int nBase = 10);
  161. <br> // Assignment operator<br> uint128_t&amp; operator=(const int128_t_base&amp; value);
  162. <br> // Unary arithmetic/logic operators<br> uint128_t operator-() const;<br> uint128_t&amp; operator++();<br> uint128_t&amp; operator--();<br> uint128_t operator++(int);<br> uint128_t operator--(int);<br> uint128_t operator~() const;<br> uint128_t operator+() const;
  163. <br> // Math operators<br> friend uint128_t operator+(const uint128_t&amp; value1, const uint128_t&amp; value2);<br> friend uint128_t operator-(const uint128_t&amp; value1, const uint128_t&amp; value2);<br> friend uint128_t operator*(const uint128_t&amp; value1, const uint128_t&amp; value2);<br> friend uint128_t operator/(const uint128_t&amp; value1, const uint128_t&amp; value2);<br> friend uint128_t operator%(const uint128_t&amp; value1, const uint128_t&amp; value2);
  164. <br> uint128_t&amp; operator+=(const uint128_t&amp; value);<br> uint128_t&amp; operator-=(const uint128_t&amp; value);<br> uint128_t&amp; operator*=(const uint128_t&amp; value);<br> uint128_t&amp; operator/=(const uint128_t&amp; value);<br> uint128_t&amp; operator%=(const uint128_t&amp; value);
  165. <br> // Shift operators<br> uint128_t operator&gt;&gt; (int nShift) const;<br> uint128_t operator&lt;&lt; (int nShift) const;<br> uint128_t&amp; operator&gt;&gt;=(int nShift);<br> uint128_t&amp; operator&lt;&lt;=(int nShift);
  166. <br> // Logical operators<br> friend uint128_t operator^(const uint128_t&amp; value1, const uint128_t&amp; value2);<br> friend uint128_t operator|(const uint128_t&amp; value1, const uint128_t&amp; value2);<br> friend uint128_t operator&amp;(const uint128_t&amp; value1, const uint128_t&amp; value2);<br> uint128_t&amp; operator^= (const uint128_t&amp; value);<br> uint128_t&amp; operator|= (const uint128_t&amp; value);<br> uint128_t&amp; operator&amp;= (const uint128_t&amp; value);
  167. <br> // Equality operators<br> friend int compare (const uint128_t&amp; value1, const uint128_t&amp; value2);<br> friend bool operator==(const uint128_t&amp; value1, const uint128_t&amp; value2);<br> friend bool operator!=(const uint128_t&amp; value1, const uint128_t&amp; value2);<br> friend bool operator&gt; (const uint128_t&amp; value1, const uint128_t&amp; value2);<br> friend bool operator&gt;=(const uint128_t&amp; value1, const uint128_t&amp; value2);<br> friend bool operator&lt; (const uint128_t&amp; value1, const uint128_t&amp; value2);<br> friend bool operator&lt;=(const uint128_t&amp; value1, const uint128_t&amp; value2);
  168. <br> // Operators to convert back to basic types<br> int8_t AsInt8() const;<br> int16_t AsInt16() const;<br> int32_t AsInt32() const;<br> int64_t AsInt64() const;<br> float AsFloat() const;<br> double AsDouble() const;
  169. <br> // Misc. Functions<br> void Negate();<br> bool IsNegative() const;<br> bool IsPositive() const;<br> void Modulus(const uint128_t&amp; divisor, uint128_t&amp; quotient, uint128_t&amp; remainder) const;
  170. <br> // String conversion functions<br> uint128_t StrToInt128(const char* pValue, char** ppEnd, int base) const;<br> uint128_t StrToInt128(const wchar_t* pValue, wchar_t** ppEnd, int base) const;<br> void Int128ToStr(char* pValue, char** ppEnd, int base) const;<br> void Int128ToStr(wchar_t* pValue, wchar_t** ppEnd, int base) const;
  171. <br>}; <span class="code-example-comment">// class uint128_t</span>
  172. </pre>
  173. <p></p>
  174. <hr>
  175. <p><br>
  176. <br>
  177. <br>
  178. <span style="font-family: monospace;">&nbsp;&nbsp; </span><br>
  179. <br>
  180. <br>
  181. <br>
  182. <br>
  183. <br>
  184. <br>
  185. <br>
  186. <br>
  187. <br>
  188. <br>
  189. <br>
  190. <br>
  191. </p>
  192. </body></html>