pcg_uint128.hpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750
  1. /*
  2. * PCG Random Number Generation for C++
  3. *
  4. * Copyright 2014 Melissa O'Neill <oneill@pcg-random.org>
  5. *
  6. * Licensed under the Apache License, Version 2.0 (the "License");
  7. * you may not use this file except in compliance with the License.
  8. * You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. *
  18. * For additional information about the PCG random number generation scheme,
  19. * including its license and other licensing options, visit
  20. *
  21. * http://www.pcg-random.org
  22. */
  23. /*
  24. * This code provides a a C++ class that can provide 128-bit (or higher)
  25. * integers. To produce 2K-bit integers, it uses two K-bit integers,
  26. * placed in a union that allowes the code to also see them as four K/2 bit
  27. * integers (and access them either directly name, or by index).
  28. *
  29. * It may seem like we're reinventing the wheel here, because several
  30. * libraries already exist that support large integers, but most existing
  31. * libraries provide a very generic multiprecision code, but here we're
  32. * operating at a fixed size. Also, most other libraries are fairly
  33. * heavyweight. So we use a direct implementation. Sadly, it's much slower
  34. * than hand-coded assembly or direct CPU support.
  35. */
  36. #ifndef PCG_UINT128_HPP_INCLUDED
  37. #define PCG_UINT128_HPP_INCLUDED 1
  38. #include <cstdint>
  39. #include <cstdio>
  40. #include <cassert>
  41. #include <climits>
  42. #include <utility>
  43. #include <initializer_list>
  44. #include <type_traits>
  45. /*
  46. * We want to lay the type out the same way that a native type would be laid
  47. * out, which means we must know the machine's endian, at compile time.
  48. * This ugliness attempts to do so.
  49. */
  50. #ifndef PCG_LITTLE_ENDIAN
  51. #if defined(__BYTE_ORDER__)
  52. #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
  53. #define PCG_LITTLE_ENDIAN 1
  54. #elif __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
  55. #define PCG_LITTLE_ENDIAN 0
  56. #else
  57. #error __BYTE_ORDER__ does not match a standard endian, pick a side
  58. #endif
  59. #elif __LITTLE_ENDIAN__ || _LITTLE_ENDIAN
  60. #define PCG_LITTLE_ENDIAN 1
  61. #elif __BIG_ENDIAN__ || _BIG_ENDIAN
  62. #define PCG_LITTLE_ENDIAN 0
  63. #elif __x86_64 || __x86_64__ || __i386 || __i386__
  64. #define PCG_LITTLE_ENDIAN 1
  65. #elif __powerpc__ || __POWERPC__ || __ppc__ || __PPC__ \
  66. || __m68k__ || __mc68000__
  67. #define PCG_LITTLE_ENDIAN 0
  68. #else
  69. #error Unable to determine target endianness
  70. #endif
  71. #endif
  72. namespace pcg_extras {
  73. // Recent versions of GCC have intrinsics we can use to quickly calculate
  74. // the number of leading and trailing zeros in a number. If possible, we
  75. // use them, otherwise we fall back to old-fashioned bit twiddling to figure
  76. // them out.
  77. #ifndef PCG_BITCOUNT_T
  78. typedef uint8_t bitcount_t;
  79. #else
  80. typedef PCG_BITCOUNT_T bitcount_t;
  81. #endif
  82. /*
  83. * Provide some useful helper functions
  84. * * flog2 floor(log2(x))
  85. * * trailingzeros number of trailing zero bits
  86. */
  87. #ifdef __GNUC__ // Any GNU-compatible compiler supporting C++11 has
  88. // some useful intrinsics we can use.
  89. inline bitcount_t flog2(uint32_t v)
  90. {
  91. return 31 - __builtin_clz(v);
  92. }
  93. inline bitcount_t trailingzeros(uint32_t v)
  94. {
  95. return __builtin_ctz(v);
  96. }
  97. inline bitcount_t flog2(uint64_t v)
  98. {
  99. #if UINT64_MAX == ULONG_MAX
  100. return 63 - __builtin_clzl(v);
  101. #elif UINT64_MAX == ULLONG_MAX
  102. return 63 - __builtin_clzll(v);
  103. #else
  104. #error Cannot find a function for uint64_t
  105. #endif
  106. }
  107. inline bitcount_t trailingzeros(uint64_t v)
  108. {
  109. #if UINT64_MAX == ULONG_MAX
  110. return __builtin_ctzl(v);
  111. #elif UINT64_MAX == ULLONG_MAX
  112. return __builtin_ctzll(v);
  113. #else
  114. #error Cannot find a function for uint64_t
  115. #endif
  116. }
  117. #else // Otherwise, we fall back to bit twiddling
  118. // implementations
  119. inline bitcount_t flog2(uint32_t v)
  120. {
  121. // Based on code by Eric Cole and Mark Dickinson, which appears at
  122. // https://graphics.stanford.edu/~seander/bithacks.html#IntegerLogDeBruijn
  123. static const uint8_t multiplyDeBruijnBitPos[32] = {
  124. 0, 9, 1, 10, 13, 21, 2, 29, 11, 14, 16, 18, 22, 25, 3, 30,
  125. 8, 12, 20, 28, 15, 17, 24, 7, 19, 27, 23, 6, 26, 5, 4, 31
  126. };
  127. v |= v >> 1; // first round down to one less than a power of 2
  128. v |= v >> 2;
  129. v |= v >> 4;
  130. v |= v >> 8;
  131. v |= v >> 16;
  132. return multiplyDeBruijnBitPos[(uint32_t)(v * 0x07C4ACDDU) >> 27];
  133. }
  134. inline bitcount_t trailingzeros(uint32_t v)
  135. {
  136. static const uint8_t multiplyDeBruijnBitPos[32] = {
  137. 0, 1, 28, 2, 29, 14, 24, 3, 30, 22, 20, 15, 25, 17, 4, 8,
  138. 31, 27, 13, 23, 21, 19, 16, 7, 26, 12, 18, 6, 11, 5, 10, 9
  139. };
  140. return multiplyDeBruijnBitPos[((uint32_t)((v & -v) * 0x077CB531U)) >> 27];
  141. }
  142. inline bitcount_t flog2(uint64_t v)
  143. {
  144. uint32_t high = v >> 32;
  145. uint32_t low = uint32_t(v);
  146. return high ? 32+flog2(high) : flog2(low);
  147. }
  148. inline bitcount_t trailingzeros(uint64_t v)
  149. {
  150. uint32_t high = v >> 32;
  151. uint32_t low = uint32_t(v);
  152. return low ? trailingzeros(low) : trailingzeros(high)+32;
  153. }
  154. #endif
  155. template <typename UInt>
  156. inline bitcount_t clog2(UInt v)
  157. {
  158. return flog2(v) + ((v & (-v)) != v);
  159. }
  160. template <typename UInt>
  161. inline UInt addwithcarry(UInt x, UInt y, bool carryin, bool* carryout)
  162. {
  163. UInt half_result = y + carryin;
  164. UInt result = x + half_result;
  165. *carryout = (half_result < y) || (result < x);
  166. return result;
  167. }
  168. template <typename UInt>
  169. inline UInt subwithcarry(UInt x, UInt y, bool carryin, bool* carryout)
  170. {
  171. UInt half_result = y + carryin;
  172. UInt result = x - half_result;
  173. *carryout = (half_result < y) || (result > x);
  174. return result;
  175. }
  176. template <typename UInt, typename UIntX2>
  177. class uint_x4 {
  178. // private:
  179. public:
  180. union {
  181. #if PCG_LITTLE_ENDIAN
  182. struct {
  183. UInt v0, v1, v2, v3;
  184. } w;
  185. struct {
  186. UIntX2 v01, v23;
  187. } d;
  188. #else
  189. struct {
  190. UInt v3, v2, v1, v0;
  191. } w;
  192. struct {
  193. UIntX2 v23, v01;
  194. } d;
  195. #endif
  196. // For the array access versions, the code that uses the array
  197. // must handle endian itself. Yuck.
  198. UInt wa[4];
  199. UIntX2 da[2];
  200. };
  201. public:
  202. uint_x4() = default;
  203. constexpr uint_x4(UInt v3, UInt v2, UInt v1, UInt v0)
  204. #if PCG_LITTLE_ENDIAN
  205. : w{v0, v1, v2, v3}
  206. #else
  207. : w{v3, v2, v1, v0}
  208. #endif
  209. {
  210. // Nothing (else) to do
  211. }
  212. constexpr uint_x4(UIntX2 v23, UIntX2 v01)
  213. #if PCG_LITTLE_ENDIAN
  214. : d{v01,v23}
  215. #else
  216. : d{v23,v01}
  217. #endif
  218. {
  219. // Nothing (else) to do
  220. }
  221. template<class Integral,
  222. typename std::enable_if<(std::is_integral<Integral>::value
  223. && sizeof(Integral) <= sizeof(UIntX2))
  224. >::type* = nullptr>
  225. constexpr uint_x4(Integral v01)
  226. #if PCG_LITTLE_ENDIAN
  227. : d{UIntX2(v01),0UL}
  228. #else
  229. : d{0UL,UIntX2(v01)}
  230. #endif
  231. {
  232. // Nothing (else) to do
  233. }
  234. explicit constexpr operator uint64_t() const
  235. {
  236. return d.v01;
  237. }
  238. explicit constexpr operator uint32_t() const
  239. {
  240. return w.v0;
  241. }
  242. explicit constexpr operator int() const
  243. {
  244. return w.v0;
  245. }
  246. explicit constexpr operator uint16_t() const
  247. {
  248. return w.v0;
  249. }
  250. explicit constexpr operator uint8_t() const
  251. {
  252. return w.v0;
  253. }
  254. typedef typename std::conditional<std::is_same<uint64_t,
  255. unsigned long>::value,
  256. unsigned long long,
  257. unsigned long>::type
  258. uint_missing_t;
  259. explicit constexpr operator uint_missing_t() const
  260. {
  261. return d.v01;
  262. }
  263. explicit constexpr operator bool() const
  264. {
  265. return d.v01 || d.v23;
  266. }
  267. template<typename U, typename V>
  268. friend uint_x4<U,V> operator*(const uint_x4<U,V>&, const uint_x4<U,V>&);
  269. template<typename U, typename V>
  270. friend std::pair< uint_x4<U,V>,uint_x4<U,V> >
  271. divmod(const uint_x4<U,V>&, const uint_x4<U,V>&);
  272. template<typename U, typename V>
  273. friend uint_x4<U,V> operator+(const uint_x4<U,V>&, const uint_x4<U,V>&);
  274. template<typename U, typename V>
  275. friend uint_x4<U,V> operator-(const uint_x4<U,V>&, const uint_x4<U,V>&);
  276. template<typename U, typename V>
  277. friend uint_x4<U,V> operator<<(const uint_x4<U,V>&, const uint_x4<U,V>&);
  278. template<typename U, typename V>
  279. friend uint_x4<U,V> operator>>(const uint_x4<U,V>&, const uint_x4<U,V>&);
  280. template<typename U, typename V>
  281. friend uint_x4<U,V> operator&(const uint_x4<U,V>&, const uint_x4<U,V>&);
  282. template<typename U, typename V>
  283. friend uint_x4<U,V> operator|(const uint_x4<U,V>&, const uint_x4<U,V>&);
  284. template<typename U, typename V>
  285. friend uint_x4<U,V> operator^(const uint_x4<U,V>&, const uint_x4<U,V>&);
  286. template<typename U, typename V>
  287. friend bool operator==(const uint_x4<U,V>&, const uint_x4<U,V>&);
  288. template<typename U, typename V>
  289. friend bool operator!=(const uint_x4<U,V>&, const uint_x4<U,V>&);
  290. template<typename U, typename V>
  291. friend bool operator<(const uint_x4<U,V>&, const uint_x4<U,V>&);
  292. template<typename U, typename V>
  293. friend bool operator<=(const uint_x4<U,V>&, const uint_x4<U,V>&);
  294. template<typename U, typename V>
  295. friend bool operator>(const uint_x4<U,V>&, const uint_x4<U,V>&);
  296. template<typename U, typename V>
  297. friend bool operator>=(const uint_x4<U,V>&, const uint_x4<U,V>&);
  298. template<typename U, typename V>
  299. friend uint_x4<U,V> operator~(const uint_x4<U,V>&);
  300. template<typename U, typename V>
  301. friend uint_x4<U,V> operator-(const uint_x4<U,V>&);
  302. template<typename U, typename V>
  303. friend bitcount_t flog2(const uint_x4<U,V>&);
  304. template<typename U, typename V>
  305. friend bitcount_t trailingzeros(const uint_x4<U,V>&);
  306. uint_x4& operator*=(const uint_x4& rhs)
  307. {
  308. uint_x4 result = *this * rhs;
  309. return *this = result;
  310. }
  311. uint_x4& operator/=(const uint_x4& rhs)
  312. {
  313. uint_x4 result = *this / rhs;
  314. return *this = result;
  315. }
  316. uint_x4& operator%=(const uint_x4& rhs)
  317. {
  318. uint_x4 result = *this % rhs;
  319. return *this = result;
  320. }
  321. uint_x4& operator+=(const uint_x4& rhs)
  322. {
  323. uint_x4 result = *this + rhs;
  324. return *this = result;
  325. }
  326. uint_x4& operator-=(const uint_x4& rhs)
  327. {
  328. uint_x4 result = *this - rhs;
  329. return *this = result;
  330. }
  331. uint_x4& operator&=(const uint_x4& rhs)
  332. {
  333. uint_x4 result = *this & rhs;
  334. return *this = result;
  335. }
  336. uint_x4& operator|=(const uint_x4& rhs)
  337. {
  338. uint_x4 result = *this | rhs;
  339. return *this = result;
  340. }
  341. uint_x4& operator^=(const uint_x4& rhs)
  342. {
  343. uint_x4 result = *this ^ rhs;
  344. return *this = result;
  345. }
  346. uint_x4& operator>>=(bitcount_t shift)
  347. {
  348. uint_x4 result = *this >> shift;
  349. return *this = result;
  350. }
  351. uint_x4& operator<<=(bitcount_t shift)
  352. {
  353. uint_x4 result = *this << shift;
  354. return *this = result;
  355. }
  356. };
  357. template<typename U, typename V>
  358. bitcount_t flog2(const uint_x4<U,V>& v)
  359. {
  360. #if PCG_LITTLE_ENDIAN
  361. for (uint8_t i = 4; i !=0; /* dec in loop */) {
  362. --i;
  363. #else
  364. for (uint8_t i = 0; i < 4; ++i) {
  365. #endif
  366. if (v.wa[i] == 0)
  367. continue;
  368. return flog2(v.wa[i]) + (sizeof(U)*CHAR_BIT)*i;
  369. }
  370. abort();
  371. }
  372. template<typename U, typename V>
  373. bitcount_t trailingzeros(const uint_x4<U,V>& v)
  374. {
  375. #if PCG_LITTLE_ENDIAN
  376. for (uint8_t i = 0; i < 4; ++i) {
  377. #else
  378. for (uint8_t i = 4; i !=0; /* dec in loop */) {
  379. --i;
  380. #endif
  381. if (v.wa[i] != 0)
  382. return trailingzeros(v.wa[i]) + (sizeof(U)*CHAR_BIT)*i;
  383. }
  384. return (sizeof(U)*CHAR_BIT)*4;
  385. }
  386. template <typename UInt, typename UIntX2>
  387. std::pair< uint_x4<UInt,UIntX2>, uint_x4<UInt,UIntX2> >
  388. divmod(const uint_x4<UInt,UIntX2>& orig_dividend,
  389. const uint_x4<UInt,UIntX2>& divisor)
  390. {
  391. // If the dividend is less than the divisor, the answer is always zero.
  392. // This takes care of boundary cases like 0/x (which would otherwise be
  393. // problematic because we can't take the log of zero. (The boundary case
  394. // of division by zero is undefined.)
  395. if (orig_dividend < divisor)
  396. return { uint_x4<UInt,UIntX2>(0UL), orig_dividend };
  397. auto dividend = orig_dividend;
  398. auto log2_divisor = flog2(divisor);
  399. auto log2_dividend = flog2(dividend);
  400. // assert(log2_dividend >= log2_divisor);
  401. bitcount_t logdiff = log2_dividend - log2_divisor;
  402. constexpr uint_x4<UInt,UIntX2> ONE(1UL);
  403. if (logdiff == 0)
  404. return { ONE, dividend - divisor };
  405. // Now we change the log difference to
  406. // floor(log2(divisor)) - ceil(log2(dividend))
  407. // to ensure that we *underestimate* the result.
  408. logdiff -= 1;
  409. uint_x4<UInt,UIntX2> quotient(0UL);
  410. auto qfactor = ONE << logdiff;
  411. auto factor = divisor << logdiff;
  412. do {
  413. dividend -= factor;
  414. quotient += qfactor;
  415. while (dividend < factor) {
  416. factor >>= 1;
  417. qfactor >>= 1;
  418. }
  419. } while (dividend >= divisor);
  420. return { quotient, dividend };
  421. }
  422. template <typename UInt, typename UIntX2>
  423. uint_x4<UInt,UIntX2> operator/(const uint_x4<UInt,UIntX2>& dividend,
  424. const uint_x4<UInt,UIntX2>& divisor)
  425. {
  426. return divmod(dividend, divisor).first;
  427. }
  428. template <typename UInt, typename UIntX2>
  429. uint_x4<UInt,UIntX2> operator%(const uint_x4<UInt,UIntX2>& dividend,
  430. const uint_x4<UInt,UIntX2>& divisor)
  431. {
  432. return divmod(dividend, divisor).second;
  433. }
  434. template <typename UInt, typename UIntX2>
  435. uint_x4<UInt,UIntX2> operator*(const uint_x4<UInt,UIntX2>& a,
  436. const uint_x4<UInt,UIntX2>& b)
  437. {
  438. uint_x4<UInt,UIntX2> r = {0U, 0U, 0U, 0U};
  439. bool carryin = false;
  440. bool carryout;
  441. UIntX2 a0b0 = UIntX2(a.w.v0) * UIntX2(b.w.v0);
  442. r.w.v0 = UInt(a0b0);
  443. r.w.v1 = UInt(a0b0 >> 32);
  444. UIntX2 a1b0 = UIntX2(a.w.v1) * UIntX2(b.w.v0);
  445. r.w.v2 = UInt(a1b0 >> 32);
  446. r.w.v1 = addwithcarry(r.w.v1, UInt(a1b0), carryin, &carryout);
  447. carryin = carryout;
  448. r.w.v2 = addwithcarry(r.w.v2, UInt(0U), carryin, &carryout);
  449. carryin = carryout;
  450. r.w.v3 = addwithcarry(r.w.v3, UInt(0U), carryin, &carryout);
  451. UIntX2 a0b1 = UIntX2(a.w.v0) * UIntX2(b.w.v1);
  452. carryin = false;
  453. r.w.v2 = addwithcarry(r.w.v2, UInt(a0b1 >> 32), carryin, &carryout);
  454. carryin = carryout;
  455. r.w.v3 = addwithcarry(r.w.v3, UInt(0U), carryin, &carryout);
  456. carryin = false;
  457. r.w.v1 = addwithcarry(r.w.v1, UInt(a0b1), carryin, &carryout);
  458. carryin = carryout;
  459. r.w.v2 = addwithcarry(r.w.v2, UInt(0U), carryin, &carryout);
  460. carryin = carryout;
  461. r.w.v3 = addwithcarry(r.w.v3, UInt(0U), carryin, &carryout);
  462. UIntX2 a1b1 = UIntX2(a.w.v1) * UIntX2(b.w.v1);
  463. carryin = false;
  464. r.w.v2 = addwithcarry(r.w.v2, UInt(a1b1), carryin, &carryout);
  465. carryin = carryout;
  466. r.w.v3 = addwithcarry(r.w.v3, UInt(a1b1 >> 32), carryin, &carryout);
  467. r.d.v23 += a.d.v01 * b.d.v23 + a.d.v23 * b.d.v01;
  468. return r;
  469. }
  470. template <typename UInt, typename UIntX2>
  471. uint_x4<UInt,UIntX2> operator+(const uint_x4<UInt,UIntX2>& a,
  472. const uint_x4<UInt,UIntX2>& b)
  473. {
  474. uint_x4<UInt,UIntX2> r = {0U, 0U, 0U, 0U};
  475. bool carryin = false;
  476. bool carryout;
  477. r.w.v0 = addwithcarry(a.w.v0, b.w.v0, carryin, &carryout);
  478. carryin = carryout;
  479. r.w.v1 = addwithcarry(a.w.v1, b.w.v1, carryin, &carryout);
  480. carryin = carryout;
  481. r.w.v2 = addwithcarry(a.w.v2, b.w.v2, carryin, &carryout);
  482. carryin = carryout;
  483. r.w.v3 = addwithcarry(a.w.v3, b.w.v3, carryin, &carryout);
  484. return r;
  485. }
  486. template <typename UInt, typename UIntX2>
  487. uint_x4<UInt,UIntX2> operator-(const uint_x4<UInt,UIntX2>& a,
  488. const uint_x4<UInt,UIntX2>& b)
  489. {
  490. uint_x4<UInt,UIntX2> r = {0U, 0U, 0U, 0U};
  491. bool carryin = false;
  492. bool carryout;
  493. r.w.v0 = subwithcarry(a.w.v0, b.w.v0, carryin, &carryout);
  494. carryin = carryout;
  495. r.w.v1 = subwithcarry(a.w.v1, b.w.v1, carryin, &carryout);
  496. carryin = carryout;
  497. r.w.v2 = subwithcarry(a.w.v2, b.w.v2, carryin, &carryout);
  498. carryin = carryout;
  499. r.w.v3 = subwithcarry(a.w.v3, b.w.v3, carryin, &carryout);
  500. return r;
  501. }
  502. template <typename UInt, typename UIntX2>
  503. uint_x4<UInt,UIntX2> operator&(const uint_x4<UInt,UIntX2>& a,
  504. const uint_x4<UInt,UIntX2>& b)
  505. {
  506. return uint_x4<UInt,UIntX2>(a.d.v23 & b.d.v23, a.d.v01 & b.d.v01);
  507. }
  508. template <typename UInt, typename UIntX2>
  509. uint_x4<UInt,UIntX2> operator|(const uint_x4<UInt,UIntX2>& a,
  510. const uint_x4<UInt,UIntX2>& b)
  511. {
  512. return uint_x4<UInt,UIntX2>(a.d.v23 | b.d.v23, a.d.v01 | b.d.v01);
  513. }
  514. template <typename UInt, typename UIntX2>
  515. uint_x4<UInt,UIntX2> operator^(const uint_x4<UInt,UIntX2>& a,
  516. const uint_x4<UInt,UIntX2>& b)
  517. {
  518. return uint_x4<UInt,UIntX2>(a.d.v23 ^ b.d.v23, a.d.v01 ^ b.d.v01);
  519. }
  520. template <typename UInt, typename UIntX2>
  521. uint_x4<UInt,UIntX2> operator~(const uint_x4<UInt,UIntX2>& v)
  522. {
  523. return uint_x4<UInt,UIntX2>(~v.d.v23, ~v.d.v01);
  524. }
  525. template <typename UInt, typename UIntX2>
  526. uint_x4<UInt,UIntX2> operator-(const uint_x4<UInt,UIntX2>& v)
  527. {
  528. return uint_x4<UInt,UIntX2>(0UL,0UL) - v;
  529. }
  530. template <typename UInt, typename UIntX2>
  531. bool operator==(const uint_x4<UInt,UIntX2>& a, const uint_x4<UInt,UIntX2>& b)
  532. {
  533. return (a.d.v01 == b.d.v01) && (a.d.v23 == b.d.v23);
  534. }
  535. template <typename UInt, typename UIntX2>
  536. bool operator!=(const uint_x4<UInt,UIntX2>& a, const uint_x4<UInt,UIntX2>& b)
  537. {
  538. return !operator==(a,b);
  539. }
  540. template <typename UInt, typename UIntX2>
  541. bool operator<(const uint_x4<UInt,UIntX2>& a, const uint_x4<UInt,UIntX2>& b)
  542. {
  543. return (a.d.v23 < b.d.v23)
  544. || ((a.d.v23 == b.d.v23) && (a.d.v01 < b.d.v01));
  545. }
  546. template <typename UInt, typename UIntX2>
  547. bool operator>(const uint_x4<UInt,UIntX2>& a, const uint_x4<UInt,UIntX2>& b)
  548. {
  549. return operator<(b,a);
  550. }
  551. template <typename UInt, typename UIntX2>
  552. bool operator<=(const uint_x4<UInt,UIntX2>& a, const uint_x4<UInt,UIntX2>& b)
  553. {
  554. return !(operator<(b,a));
  555. }
  556. template <typename UInt, typename UIntX2>
  557. bool operator>=(const uint_x4<UInt,UIntX2>& a, const uint_x4<UInt,UIntX2>& b)
  558. {
  559. return !(operator<(a,b));
  560. }
  561. template <typename UInt, typename UIntX2>
  562. uint_x4<UInt,UIntX2> operator<<(const uint_x4<UInt,UIntX2>& v,
  563. const bitcount_t shift)
  564. {
  565. uint_x4<UInt,UIntX2> r = {0U, 0U, 0U, 0U};
  566. const bitcount_t bits = sizeof(UInt) * CHAR_BIT;
  567. const bitcount_t bitmask = bits - 1;
  568. const bitcount_t shiftdiv = shift / bits;
  569. const bitcount_t shiftmod = shift & bitmask;
  570. if (shiftmod) {
  571. UInt carryover = 0;
  572. #if PCG_LITTLE_ENDIAN
  573. for (uint8_t out = shiftdiv, in = 0; out < 4; ++out, ++in) {
  574. #else
  575. for (uint8_t out = 4-shiftdiv, in = 4; out != 0; /* dec in loop */) {
  576. --out, --in;
  577. #endif
  578. r.wa[out] = (v.wa[in] << shiftmod) | carryover;
  579. carryover = (v.wa[in] >> (bits - shiftmod));
  580. }
  581. } else {
  582. #if PCG_LITTLE_ENDIAN
  583. for (uint8_t out = shiftdiv, in = 0; out < 4; ++out, ++in) {
  584. #else
  585. for (uint8_t out = 4-shiftdiv, in = 4; out != 0; /* dec in loop */) {
  586. --out, --in;
  587. #endif
  588. r.wa[out] = v.wa[in];
  589. }
  590. }
  591. return r;
  592. }
  593. template <typename UInt, typename UIntX2>
  594. uint_x4<UInt,UIntX2> operator>>(const uint_x4<UInt,UIntX2>& v,
  595. const bitcount_t shift)
  596. {
  597. uint_x4<UInt,UIntX2> r = {0U, 0U, 0U, 0U};
  598. const bitcount_t bits = sizeof(UInt) * CHAR_BIT;
  599. const bitcount_t bitmask = bits - 1;
  600. const bitcount_t shiftdiv = shift / bits;
  601. const bitcount_t shiftmod = shift & bitmask;
  602. if (shiftmod) {
  603. UInt carryover = 0;
  604. #if PCG_LITTLE_ENDIAN
  605. for (uint8_t out = 4-shiftdiv, in = 4; out != 0; /* dec in loop */) {
  606. --out, --in;
  607. #else
  608. for (uint8_t out = shiftdiv, in = 0; out < 4; ++out, ++in) {
  609. #endif
  610. r.wa[out] = (v.wa[in] >> shiftmod) | carryover;
  611. carryover = (v.wa[in] << (bits - shiftmod));
  612. }
  613. } else {
  614. #if PCG_LITTLE_ENDIAN
  615. for (uint8_t out = 4-shiftdiv, in = 4; out != 0; /* dec in loop */) {
  616. --out, --in;
  617. #else
  618. for (uint8_t out = shiftdiv, in = 0; out < 4; ++out, ++in) {
  619. #endif
  620. r.wa[out] = v.wa[in];
  621. }
  622. }
  623. return r;
  624. }
  625. } // namespace pcg_extras
  626. #endif // PCG_UINT128_HPP_INCLUDED