u64.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. /* uint64_t-like operations that work even on hosts lacking uint64_t
  2. Copyright (C) 2006, 2009-2015 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. #ifndef _GL_INLINE_HEADER_BEGIN
  16. #error "Please include config.h first."
  17. #endif
  18. _GL_INLINE_HEADER_BEGIN
  19. #ifndef _GL_U64_INLINE
  20. # define _GL_U64_INLINE _GL_INLINE
  21. #endif
  22. /* Return X rotated left by N bits, where 0 < N < 64. */
  23. #define u64rol(x, n) u64or (u64shl (x, n), u64shr (x, 64 - n))
  24. #ifdef UINT64_MAX
  25. /* Native implementations are trivial. See below for comments on what
  26. these operations do. */
  27. typedef uint64_t u64;
  28. # define u64hilo(hi, lo) ((u64) (((u64) (hi) << 32) + (lo)))
  29. # define u64init(hi, lo) u64hilo (hi, lo)
  30. # define u64lo(x) ((u64) (x))
  31. # define u64size(x) u64lo (x)
  32. # define u64lt(x, y) ((x) < (y))
  33. # define u64and(x, y) ((x) & (y))
  34. # define u64or(x, y) ((x) | (y))
  35. # define u64xor(x, y) ((x) ^ (y))
  36. # define u64plus(x, y) ((x) + (y))
  37. # define u64shl(x, n) ((x) << (n))
  38. # define u64shr(x, n) ((x) >> (n))
  39. #else
  40. /* u64 is a 64-bit unsigned integer value.
  41. u64init (HI, LO), is like u64hilo (HI, LO), but for use in
  42. initializer contexts. */
  43. # ifdef WORDS_BIGENDIAN
  44. typedef struct { uint32_t hi, lo; } u64;
  45. # define u64init(hi, lo) { hi, lo }
  46. # else
  47. typedef struct { uint32_t lo, hi; } u64;
  48. # define u64init(hi, lo) { lo, hi }
  49. # endif
  50. /* Given the high and low-order 32-bit quantities HI and LO, return a u64
  51. value representing (HI << 32) + LO. */
  52. _GL_U64_INLINE u64
  53. u64hilo (uint32_t hi, uint32_t lo)
  54. {
  55. u64 r;
  56. r.hi = hi;
  57. r.lo = lo;
  58. return r;
  59. }
  60. /* Return a u64 value representing LO. */
  61. _GL_U64_INLINE u64
  62. u64lo (uint32_t lo)
  63. {
  64. u64 r;
  65. r.hi = 0;
  66. r.lo = lo;
  67. return r;
  68. }
  69. /* Return a u64 value representing SIZE. */
  70. _GL_U64_INLINE u64
  71. u64size (size_t size)
  72. {
  73. u64 r;
  74. r.hi = size >> 31 >> 1;
  75. r.lo = size;
  76. return r;
  77. }
  78. /* Return X < Y. */
  79. _GL_U64_INLINE int
  80. u64lt (u64 x, u64 y)
  81. {
  82. return x.hi < y.hi || (x.hi == y.hi && x.lo < y.lo);
  83. }
  84. /* Return X & Y. */
  85. _GL_U64_INLINE u64
  86. u64and (u64 x, u64 y)
  87. {
  88. u64 r;
  89. r.hi = x.hi & y.hi;
  90. r.lo = x.lo & y.lo;
  91. return r;
  92. }
  93. /* Return X | Y. */
  94. _GL_U64_INLINE u64
  95. u64or (u64 x, u64 y)
  96. {
  97. u64 r;
  98. r.hi = x.hi | y.hi;
  99. r.lo = x.lo | y.lo;
  100. return r;
  101. }
  102. /* Return X ^ Y. */
  103. _GL_U64_INLINE u64
  104. u64xor (u64 x, u64 y)
  105. {
  106. u64 r;
  107. r.hi = x.hi ^ y.hi;
  108. r.lo = x.lo ^ y.lo;
  109. return r;
  110. }
  111. /* Return X + Y. */
  112. _GL_U64_INLINE u64
  113. u64plus (u64 x, u64 y)
  114. {
  115. u64 r;
  116. r.lo = x.lo + y.lo;
  117. r.hi = x.hi + y.hi + (r.lo < x.lo);
  118. return r;
  119. }
  120. /* Return X << N. */
  121. _GL_U64_INLINE u64
  122. u64shl (u64 x, int n)
  123. {
  124. u64 r;
  125. if (n < 32)
  126. {
  127. r.hi = (x.hi << n) | (x.lo >> (32 - n));
  128. r.lo = x.lo << n;
  129. }
  130. else
  131. {
  132. r.hi = x.lo << (n - 32);
  133. r.lo = 0;
  134. }
  135. return r;
  136. }
  137. /* Return X >> N. */
  138. _GL_U64_INLINE u64
  139. u64shr (u64 x, int n)
  140. {
  141. u64 r;
  142. if (n < 32)
  143. {
  144. r.hi = x.hi >> n;
  145. r.lo = (x.hi << (32 - n)) | (x.lo >> n);
  146. }
  147. else
  148. {
  149. r.hi = 0;
  150. r.lo = x.hi >> (n - 32);
  151. }
  152. return r;
  153. }
  154. #endif
  155. _GL_INLINE_HEADER_END