longlong.h 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. /* longlong.h -- definitions for mixed size 32/64 bit arithmetic.
  2. Copyright (C) 1991, 1992, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2004,
  3. 2005 Free Software Foundation, Inc.
  4. This definition file is free software; you can redistribute it
  5. and/or modify it under the terms of the GNU General Public
  6. License as published by the Free Software Foundation; either
  7. version 2, or (at your option) any later version.
  8. This definition file is distributed in the hope that it will be
  9. useful, but WITHOUT ANY WARRANTY; without even the implied
  10. warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  11. See the GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 51 Franklin Street, Fifth Floor,
  15. Boston, MA 02110-1301, USA. */
  16. /* You have to define the following before including this file:
  17. UWtype -- An unsigned type, default type for operations (typically a "word")
  18. UHWtype -- An unsigned type, at least half the size of UWtype.
  19. UDWtype -- An unsigned type, at least twice as large a UWtype
  20. W_TYPE_SIZE -- size in bits of UWtype
  21. UQItype -- Unsigned 8 bit type.
  22. SItype, USItype -- Signed and unsigned 32 bit types.
  23. DItype, UDItype -- Signed and unsigned 64 bit types.
  24. On a 32 bit machine UWtype should typically be USItype;
  25. on a 64 bit machine, UWtype should typically be UDItype. */
  26. #define __BITS4 (W_TYPE_SIZE / 4)
  27. #define __ll_B ((UWtype) 1 << (W_TYPE_SIZE / 2))
  28. #define __ll_lowpart(t) ((UWtype) (t) & (__ll_B - 1))
  29. #define __ll_highpart(t) ((UWtype) (t) >> (W_TYPE_SIZE / 2))
  30. #ifndef W_TYPE_SIZE
  31. #define W_TYPE_SIZE 32
  32. #define UWtype USItype
  33. #define UHWtype USItype
  34. #define UDWtype UDItype
  35. #endif
  36. extern const UQItype __clz_tab[256];
  37. /* Define auxiliary asm macros.
  38. 1) umul_ppmm(high_prod, low_prod, multiplier, multiplicand) multiplies two
  39. UWtype integers MULTIPLIER and MULTIPLICAND, and generates a two UWtype
  40. word product in HIGH_PROD and LOW_PROD.
  41. 2) __umulsidi3(a,b) multiplies two UWtype integers A and B, and returns a
  42. UDWtype product. This is just a variant of umul_ppmm.
  43. 3) udiv_qrnnd(quotient, remainder, high_numerator, low_numerator,
  44. denominator) divides a UDWtype, composed by the UWtype integers
  45. HIGH_NUMERATOR and LOW_NUMERATOR, by DENOMINATOR and places the quotient
  46. in QUOTIENT and the remainder in REMAINDER. HIGH_NUMERATOR must be less
  47. than DENOMINATOR for correct operation. If, in addition, the most
  48. significant bit of DENOMINATOR must be 1, then the pre-processor symbol
  49. UDIV_NEEDS_NORMALIZATION is defined to 1.
  50. 4) sdiv_qrnnd(quotient, remainder, high_numerator, low_numerator,
  51. denominator). Like udiv_qrnnd but the numbers are signed. The quotient
  52. is rounded towards 0.
  53. 5) count_leading_zeros(count, x) counts the number of zero-bits from the
  54. msb to the first nonzero bit in the UWtype X. This is the number of
  55. steps X needs to be shifted left to set the msb. Undefined for X == 0,
  56. unless the symbol COUNT_LEADING_ZEROS_0 is defined to some value.
  57. 6) count_trailing_zeros(count, x) like count_leading_zeros, but counts
  58. from the least significant end.
  59. 7) add_ssaaaa(high_sum, low_sum, high_addend_1, low_addend_1,
  60. high_addend_2, low_addend_2) adds two UWtype integers, composed by
  61. HIGH_ADDEND_1 and LOW_ADDEND_1, and HIGH_ADDEND_2 and LOW_ADDEND_2
  62. respectively. The result is placed in HIGH_SUM and LOW_SUM. Overflow
  63. (i.e. carry out) is not stored anywhere, and is lost.
  64. 8) sub_ddmmss(high_difference, low_difference, high_minuend, low_minuend,
  65. high_subtrahend, low_subtrahend) subtracts two two-word UWtype integers,
  66. composed by HIGH_MINUEND_1 and LOW_MINUEND_1, and HIGH_SUBTRAHEND_2 and
  67. LOW_SUBTRAHEND_2 respectively. The result is placed in HIGH_DIFFERENCE
  68. and LOW_DIFFERENCE. Overflow (i.e. carry out) is not stored anywhere,
  69. and is lost.
  70. If any of these macros are left undefined for a particular CPU,
  71. C macros are used. */
  72. /* The CPUs come in alphabetical order below.
  73. Please add support for more CPUs here, or improve the current support
  74. for the CPUs below!
  75. (E.g. WE32100, IBM360.) */
  76. /* Snipped per CPU support */
  77. /* If this machine has no inline assembler, use C macros. */
  78. #if !defined (add_ssaaaa)
  79. #define add_ssaaaa(sh, sl, ah, al, bh, bl) \
  80. do { \
  81. UWtype __x; \
  82. __x = (al) + (bl); \
  83. (sh) = (ah) + (bh) + (__x < (al)); \
  84. (sl) = __x; \
  85. } while (0)
  86. #endif
  87. #if !defined (sub_ddmmss)
  88. #define sub_ddmmss(sh, sl, ah, al, bh, bl) \
  89. do { \
  90. UWtype __x; \
  91. __x = (al) - (bl); \
  92. (sh) = (ah) - (bh) - (__x > (al)); \
  93. (sl) = __x; \
  94. } while (0)
  95. #endif
  96. /* If we lack umul_ppmm but have smul_ppmm, define umul_ppmm in terms of
  97. smul_ppmm. */
  98. #if !defined (umul_ppmm) && defined (smul_ppmm)
  99. #define umul_ppmm(w1, w0, u, v) \
  100. do { \
  101. UWtype __w1; \
  102. UWtype __xm0 = (u), __xm1 = (v); \
  103. smul_ppmm (__w1, w0, __xm0, __xm1); \
  104. (w1) = __w1 + (-(__xm0 >> (W_TYPE_SIZE - 1)) & __xm1) \
  105. + (-(__xm1 >> (W_TYPE_SIZE - 1)) & __xm0); \
  106. } while (0)
  107. #endif
  108. /* If we still don't have umul_ppmm, define it using plain C. */
  109. #if !defined (umul_ppmm)
  110. #define umul_ppmm(w1, w0, u, v) \
  111. do { \
  112. UWtype __x0, __x1, __x2, __x3; \
  113. UHWtype __ul, __vl, __uh, __vh; \
  114. \
  115. __ul = __ll_lowpart (u); \
  116. __uh = __ll_highpart (u); \
  117. __vl = __ll_lowpart (v); \
  118. __vh = __ll_highpart (v); \
  119. \
  120. __x0 = (UWtype) __ul * __vl; \
  121. __x1 = (UWtype) __ul * __vh; \
  122. __x2 = (UWtype) __uh * __vl; \
  123. __x3 = (UWtype) __uh * __vh; \
  124. \
  125. __x1 += __ll_highpart (__x0);/* this can't give carry */ \
  126. __x1 += __x2; /* but this indeed can */ \
  127. if (__x1 < __x2) /* did we get it? */ \
  128. __x3 += __ll_B; /* yes, add it in the proper pos. */ \
  129. \
  130. (w1) = __x3 + __ll_highpart (__x1); \
  131. (w0) = __ll_lowpart (__x1) * __ll_B + __ll_lowpart (__x0); \
  132. } while (0)
  133. #endif
  134. #if !defined (__umulsidi3)
  135. #define __umulsidi3(u, v) \
  136. ({DWunion __w; \
  137. umul_ppmm (__w.s.high, __w.s.low, u, v); \
  138. __w.ll; })
  139. #endif
  140. /* Define this unconditionally, so it can be used for debugging. */
  141. #define __udiv_qrnnd_c(q, r, n1, n0, d) \
  142. do { \
  143. UWtype __d1, __d0, __q1, __q0; \
  144. UWtype __r1, __r0, __m; \
  145. __d1 = __ll_highpart (d); \
  146. __d0 = __ll_lowpart (d); \
  147. \
  148. __r1 = (n1) % __d1; \
  149. __q1 = (n1) / __d1; \
  150. __m = (UWtype) __q1 * __d0; \
  151. __r1 = __r1 * __ll_B | __ll_highpart (n0); \
  152. if (__r1 < __m) \
  153. { \
  154. __q1--, __r1 += (d); \
  155. if (__r1 >= (d)) /* i.e. we didn't get carry when adding to __r1 */\
  156. if (__r1 < __m) \
  157. __q1--, __r1 += (d); \
  158. } \
  159. __r1 -= __m; \
  160. \
  161. __r0 = __r1 % __d1; \
  162. __q0 = __r1 / __d1; \
  163. __m = (UWtype) __q0 * __d0; \
  164. __r0 = __r0 * __ll_B | __ll_lowpart (n0); \
  165. if (__r0 < __m) \
  166. { \
  167. __q0--, __r0 += (d); \
  168. if (__r0 >= (d)) \
  169. if (__r0 < __m) \
  170. __q0--, __r0 += (d); \
  171. } \
  172. __r0 -= __m; \
  173. \
  174. (q) = (UWtype) __q1 * __ll_B | __q0; \
  175. (r) = __r0; \
  176. } while (0)
  177. /* If the processor has no udiv_qrnnd but sdiv_qrnnd, go through
  178. __udiv_w_sdiv (defined in libgcc or elsewhere). */
  179. #if !defined (udiv_qrnnd) && defined (sdiv_qrnnd)
  180. #define udiv_qrnnd(q, r, nh, nl, d) \
  181. do { \
  182. USItype __r; \
  183. (q) = __udiv_w_sdiv (&__r, nh, nl, d); \
  184. (r) = __r; \
  185. } while (0)
  186. #endif
  187. /* If udiv_qrnnd was not defined for this processor, use __udiv_qrnnd_c. */
  188. #if !defined (udiv_qrnnd)
  189. #define UDIV_NEEDS_NORMALIZATION 1
  190. #define udiv_qrnnd __udiv_qrnnd_c
  191. #endif
  192. #if !defined (count_leading_zeros)
  193. #define count_leading_zeros(count, x) \
  194. do { \
  195. UWtype __xr = (x); \
  196. UWtype __a; \
  197. \
  198. if (W_TYPE_SIZE <= 32) \
  199. { \
  200. __a = __xr < ((UWtype)1<<2*__BITS4) \
  201. ? (__xr < ((UWtype)1<<__BITS4) ? 0 : __BITS4) \
  202. : (__xr < ((UWtype)1<<3*__BITS4) ? 2*__BITS4 : 3*__BITS4); \
  203. } \
  204. else \
  205. { \
  206. for (__a = W_TYPE_SIZE - 8; __a > 0; __a -= 8) \
  207. if (((__xr >> __a) & 0xff) != 0) \
  208. break; \
  209. } \
  210. \
  211. (count) = W_TYPE_SIZE - (__clz_tab[__xr >> __a] + __a); \
  212. } while (0)
  213. #define COUNT_LEADING_ZEROS_0 W_TYPE_SIZE
  214. #endif
  215. #if !defined (count_trailing_zeros)
  216. /* Define count_trailing_zeros using count_leading_zeros. The latter might be
  217. defined in asm, but if it is not, the C version above is good enough. */
  218. #define count_trailing_zeros(count, x) \
  219. do { \
  220. UWtype __ctz_x = (x); \
  221. UWtype __ctz_c; \
  222. count_leading_zeros (__ctz_c, __ctz_x & -__ctz_x); \
  223. (count) = W_TYPE_SIZE - 1 - __ctz_c; \
  224. } while (0)
  225. #endif
  226. #ifndef UDIV_NEEDS_NORMALIZATION
  227. #define UDIV_NEEDS_NORMALIZATION 0
  228. #endif