u64.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. /* uint64_t-like operations that work even on hosts lacking uint64_t
  2. Copyright (C) 2006, 2009-2011 Free Software Foundation, Inc.
  3. This program is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation, either version 3 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  13. /* Written by Paul Eggert. */
  14. #include <stdint.h>
  15. /* Return X rotated left by N bits, where 0 < N < 64. */
  16. #define u64rol(x, n) u64or (u64shl (x, n), u64shr (x, 64 - n))
  17. #ifdef UINT64_MAX
  18. /* Native implementations are trivial. See below for comments on what
  19. these operations do. */
  20. typedef uint64_t u64;
  21. # define u64hilo(hi, lo) ((u64) (((u64) (hi) << 32) + (lo)))
  22. # define u64init(hi, lo) u64hilo (hi, lo)
  23. # define u64lo(x) ((u64) (x))
  24. # define u64lt(x, y) ((x) < (y))
  25. # define u64and(x, y) ((x) & (y))
  26. # define u64or(x, y) ((x) | (y))
  27. # define u64xor(x, y) ((x) ^ (y))
  28. # define u64plus(x, y) ((x) + (y))
  29. # define u64shl(x, n) ((x) << (n))
  30. # define u64shr(x, n) ((x) >> (n))
  31. #else
  32. /* u64 is a 64-bit unsigned integer value.
  33. u64init (HI, LO), is like u64hilo (HI, LO), but for use in
  34. initializer contexts. */
  35. # ifdef WORDS_BIGENDIAN
  36. typedef struct { uint32_t hi, lo; } u64;
  37. # define u64init(hi, lo) { hi, lo }
  38. # else
  39. typedef struct { uint32_t lo, hi; } u64;
  40. # define u64init(hi, lo) { lo, hi }
  41. # endif
  42. /* Given the high and low-order 32-bit quantities HI and LO, return a u64
  43. value representing (HI << 32) + LO. */
  44. static inline u64
  45. u64hilo (uint32_t hi, uint32_t lo)
  46. {
  47. u64 r;
  48. r.hi = hi;
  49. r.lo = lo;
  50. return r;
  51. }
  52. /* Return a u64 value representing LO. */
  53. static inline u64
  54. u64lo (uint32_t lo)
  55. {
  56. u64 r;
  57. r.hi = 0;
  58. r.lo = lo;
  59. return r;
  60. }
  61. /* Return X < Y. */
  62. static inline int
  63. u64lt (u64 x, u64 y)
  64. {
  65. return x.hi < y.hi || (x.hi == y.hi && x.lo < y.lo);
  66. }
  67. /* Return X & Y. */
  68. static inline u64
  69. u64and (u64 x, u64 y)
  70. {
  71. u64 r;
  72. r.hi = x.hi & y.hi;
  73. r.lo = x.lo & y.lo;
  74. return r;
  75. }
  76. /* Return X | Y. */
  77. static inline u64
  78. u64or (u64 x, u64 y)
  79. {
  80. u64 r;
  81. r.hi = x.hi | y.hi;
  82. r.lo = x.lo | y.lo;
  83. return r;
  84. }
  85. /* Return X ^ Y. */
  86. static inline u64
  87. u64xor (u64 x, u64 y)
  88. {
  89. u64 r;
  90. r.hi = x.hi ^ y.hi;
  91. r.lo = x.lo ^ y.lo;
  92. return r;
  93. }
  94. /* Return X + Y. */
  95. static inline u64
  96. u64plus (u64 x, u64 y)
  97. {
  98. u64 r;
  99. r.lo = x.lo + y.lo;
  100. r.hi = x.hi + y.hi + (r.lo < x.lo);
  101. return r;
  102. }
  103. /* Return X << N. */
  104. static inline u64
  105. u64shl (u64 x, int n)
  106. {
  107. u64 r;
  108. if (n < 32)
  109. {
  110. r.hi = (x.hi << n) | (x.lo >> (32 - n));
  111. r.lo = x.lo << n;
  112. }
  113. else
  114. {
  115. r.hi = x.lo << (n - 32);
  116. r.lo = 0;
  117. }
  118. return r;
  119. }
  120. /* Return X >> N. */
  121. static inline u64
  122. u64shr (u64 x, int n)
  123. {
  124. u64 r;
  125. if (n < 32)
  126. {
  127. r.hi = x.hi >> n;
  128. r.lo = (x.hi << (32 - n)) | (x.lo >> n);
  129. }
  130. else
  131. {
  132. r.hi = 0;
  133. r.lo = x.hi >> (n - 32);
  134. }
  135. return r;
  136. }
  137. #endif