2
0

int128.h 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. /*-------------------------------------------------------------------------
  2. *
  3. * int128.h
  4. * Roll-our-own 128-bit integer arithmetic.
  5. *
  6. * We make use of the native int128 type if there is one, otherwise
  7. * implement things the hard way based on two int64 halves.
  8. *
  9. * See src/tools/testint128.c for a simple test harness for this file.
  10. *
  11. * Copyright (c) 2017-2022, PostgreSQL Global Development Group
  12. *
  13. * src/include/common/int128.h
  14. *
  15. *-------------------------------------------------------------------------
  16. */
  17. #ifndef INT128_H
  18. #define INT128_H
  19. /*
  20. * For testing purposes, use of native int128 can be switched on/off by
  21. * predefining USE_NATIVE_INT128.
  22. */
  23. #ifndef USE_NATIVE_INT128
  24. #ifdef HAVE_INT128
  25. #define USE_NATIVE_INT128 1
  26. #else
  27. #define USE_NATIVE_INT128 0
  28. #endif
  29. #endif
  30. #if USE_NATIVE_INT128
  31. typedef int128 INT128;
  32. /*
  33. * Add an unsigned int64 value into an INT128 variable.
  34. */
  35. static inline void
  36. int128_add_uint64(INT128 *i128, uint64 v)
  37. {
  38. *i128 += v;
  39. }
  40. /*
  41. * Add a signed int64 value into an INT128 variable.
  42. */
  43. static inline void
  44. int128_add_int64(INT128 *i128, int64 v)
  45. {
  46. *i128 += v;
  47. }
  48. /*
  49. * Add the 128-bit product of two int64 values into an INT128 variable.
  50. *
  51. * XXX with a stupid compiler, this could actually be less efficient than
  52. * the other implementation; maybe we should do it by hand always?
  53. */
  54. static inline void
  55. int128_add_int64_mul_int64(INT128 *i128, int64 x, int64 y)
  56. {
  57. *i128 += (int128) x * (int128) y;
  58. }
  59. /*
  60. * Compare two INT128 values, return -1, 0, or +1.
  61. */
  62. static inline int
  63. int128_compare(INT128 x, INT128 y)
  64. {
  65. if (x < y)
  66. return -1;
  67. if (x > y)
  68. return 1;
  69. return 0;
  70. }
  71. /*
  72. * Widen int64 to INT128.
  73. */
  74. static inline INT128
  75. int64_to_int128(int64 v)
  76. {
  77. return (INT128) v;
  78. }
  79. /*
  80. * Convert INT128 to int64 (losing any high-order bits).
  81. * This also works fine for casting down to uint64.
  82. */
  83. static inline int64
  84. int128_to_int64(INT128 val)
  85. {
  86. return (int64) val;
  87. }
  88. #else /* !USE_NATIVE_INT128 */
  89. /*
  90. * We lay out the INT128 structure with the same content and byte ordering
  91. * that a native int128 type would (probably) have. This makes no difference
  92. * for ordinary use of INT128, but allows union'ing INT128 with int128 for
  93. * testing purposes.
  94. */
  95. typedef struct
  96. {
  97. #ifdef WORDS_BIGENDIAN
  98. int64 hi; /* most significant 64 bits, including sign */
  99. uint64 lo; /* least significant 64 bits, without sign */
  100. #else
  101. uint64 lo; /* least significant 64 bits, without sign */
  102. int64 hi; /* most significant 64 bits, including sign */
  103. #endif
  104. } INT128;
  105. /*
  106. * Add an unsigned int64 value into an INT128 variable.
  107. */
  108. static inline void
  109. int128_add_uint64(INT128 *i128, uint64 v)
  110. {
  111. /*
  112. * First add the value to the .lo part, then check to see if a carry needs
  113. * to be propagated into the .hi part. A carry is needed if both inputs
  114. * have high bits set, or if just one input has high bit set while the new
  115. * .lo part doesn't. Remember that .lo part is unsigned; we cast to
  116. * signed here just as a cheap way to check the high bit.
  117. */
  118. uint64 oldlo = i128->lo;
  119. i128->lo += v;
  120. if (((int64) v < 0 && (int64) oldlo < 0) ||
  121. (((int64) v < 0 || (int64) oldlo < 0) && (int64) i128->lo >= 0))
  122. i128->hi++;
  123. }
  124. /*
  125. * Add a signed int64 value into an INT128 variable.
  126. */
  127. static inline void
  128. int128_add_int64(INT128 *i128, int64 v)
  129. {
  130. /*
  131. * This is much like the above except that the carry logic differs for
  132. * negative v. Ordinarily we'd need to subtract 1 from the .hi part
  133. * (corresponding to adding the sign-extended bits of v to it); but if
  134. * there is a carry out of the .lo part, that cancels and we do nothing.
  135. */
  136. uint64 oldlo = i128->lo;
  137. i128->lo += v;
  138. if (v >= 0)
  139. {
  140. if ((int64) oldlo < 0 && (int64) i128->lo >= 0)
  141. i128->hi++;
  142. }
  143. else
  144. {
  145. if (!((int64) oldlo < 0 || (int64) i128->lo >= 0))
  146. i128->hi--;
  147. }
  148. }
  149. /*
  150. * INT64_AU32 extracts the most significant 32 bits of int64 as int64, while
  151. * INT64_AL32 extracts the least significant 32 bits as uint64.
  152. */
  153. #define INT64_AU32(i64) ((i64) >> 32)
  154. #define INT64_AL32(i64) ((i64) & UINT64CONST(0xFFFFFFFF))
  155. /*
  156. * Add the 128-bit product of two int64 values into an INT128 variable.
  157. */
  158. static inline void
  159. int128_add_int64_mul_int64(INT128 *i128, int64 x, int64 y)
  160. {
  161. /* INT64_AU32 must use arithmetic right shift */
  162. StaticAssertStmt(((int64) -1 >> 1) == (int64) -1,
  163. "arithmetic right shift is needed");
  164. /*----------
  165. * Form the 128-bit product x * y using 64-bit arithmetic.
  166. * Considering each 64-bit input as having 32-bit high and low parts,
  167. * we can compute
  168. *
  169. * x * y = ((x.hi << 32) + x.lo) * (((y.hi << 32) + y.lo)
  170. * = (x.hi * y.hi) << 64 +
  171. * (x.hi * y.lo) << 32 +
  172. * (x.lo * y.hi) << 32 +
  173. * x.lo * y.lo
  174. *
  175. * Each individual product is of 32-bit terms so it won't overflow when
  176. * computed in 64-bit arithmetic. Then we just have to shift it to the
  177. * correct position while adding into the 128-bit result. We must also
  178. * keep in mind that the "lo" parts must be treated as unsigned.
  179. *----------
  180. */
  181. /* No need to work hard if product must be zero */
  182. if (x != 0 && y != 0)
  183. {
  184. int64 x_u32 = INT64_AU32(x);
  185. uint64 x_l32 = INT64_AL32(x);
  186. int64 y_u32 = INT64_AU32(y);
  187. uint64 y_l32 = INT64_AL32(y);
  188. int64 tmp;
  189. /* the first term */
  190. i128->hi += x_u32 * y_u32;
  191. /* the second term: sign-extend it only if x is negative */
  192. tmp = x_u32 * y_l32;
  193. if (x < 0)
  194. i128->hi += INT64_AU32(tmp);
  195. else
  196. i128->hi += ((uint64) tmp) >> 32;
  197. int128_add_uint64(i128, ((uint64) INT64_AL32(tmp)) << 32);
  198. /* the third term: sign-extend it only if y is negative */
  199. tmp = x_l32 * y_u32;
  200. if (y < 0)
  201. i128->hi += INT64_AU32(tmp);
  202. else
  203. i128->hi += ((uint64) tmp) >> 32;
  204. int128_add_uint64(i128, ((uint64) INT64_AL32(tmp)) << 32);
  205. /* the fourth term: always unsigned */
  206. int128_add_uint64(i128, x_l32 * y_l32);
  207. }
  208. }
  209. /*
  210. * Compare two INT128 values, return -1, 0, or +1.
  211. */
  212. static inline int
  213. int128_compare(INT128 x, INT128 y)
  214. {
  215. if (x.hi < y.hi)
  216. return -1;
  217. if (x.hi > y.hi)
  218. return 1;
  219. if (x.lo < y.lo)
  220. return -1;
  221. if (x.lo > y.lo)
  222. return 1;
  223. return 0;
  224. }
  225. /*
  226. * Widen int64 to INT128.
  227. */
  228. static inline INT128
  229. int64_to_int128(int64 v)
  230. {
  231. INT128 val;
  232. val.lo = (uint64) v;
  233. val.hi = (v < 0) ? -INT64CONST(1) : INT64CONST(0);
  234. return val;
  235. }
  236. /*
  237. * Convert INT128 to int64 (losing any high-order bits).
  238. * This also works fine for casting down to uint64.
  239. */
  240. static inline int64
  241. int128_to_int64(INT128 val)
  242. {
  243. return (int64) val.lo;
  244. }
  245. #endif /* USE_NATIVE_INT128 */
  246. #endif /* INT128_H */