longlong.h 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. /* longlong.h -- based on code from gcc-2.95.3
  2. definitions for mixed size 32/64 bit arithmetic.
  3. Copyright (C) 1991, 92, 94, 95, 96, 1997, 1998 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., 59 Temple Place - Suite 330,
  15. Boston, MA 02111-1307, USA. */
  16. /* Borrowed from GCC 2.95.3, I Molton 29/07/01 */
  17. #ifndef SI_TYPE_SIZE
  18. #define SI_TYPE_SIZE 32
  19. #endif
  20. #define __BITS4 (SI_TYPE_SIZE / 4)
  21. #define __ll_B (1L << (SI_TYPE_SIZE / 2))
  22. #define __ll_lowpart(t) ((USItype) (t) % __ll_B)
  23. #define __ll_highpart(t) ((USItype) (t) / __ll_B)
  24. /* Define auxiliary asm macros.
  25. 1) umul_ppmm(high_prod, low_prod, multipler, multiplicand)
  26. multiplies two USItype integers MULTIPLER and MULTIPLICAND,
  27. and generates a two-part USItype product in HIGH_PROD and
  28. LOW_PROD.
  29. 2) __umulsidi3(a,b) multiplies two USItype integers A and B,
  30. and returns a UDItype product. This is just a variant of umul_ppmm.
  31. 3) udiv_qrnnd(quotient, remainder, high_numerator, low_numerator,
  32. denominator) divides a two-word unsigned integer, composed by the
  33. integers HIGH_NUMERATOR and LOW_NUMERATOR, by DENOMINATOR and
  34. places the quotient in QUOTIENT and the remainder in REMAINDER.
  35. HIGH_NUMERATOR must be less than DENOMINATOR for correct operation.
  36. If, in addition, the most significant bit of DENOMINATOR must be 1,
  37. then the pre-processor symbol UDIV_NEEDS_NORMALIZATION is defined to 1.
  38. 4) sdiv_qrnnd(quotient, remainder, high_numerator, low_numerator,
  39. denominator). Like udiv_qrnnd but the numbers are signed. The
  40. quotient is rounded towards 0.
  41. 5) count_leading_zeros(count, x) counts the number of zero-bits from
  42. the msb to the first non-zero bit. This is the number of steps X
  43. needs to be shifted left to set the msb. Undefined for X == 0.
  44. 6) add_ssaaaa(high_sum, low_sum, high_addend_1, low_addend_1,
  45. high_addend_2, low_addend_2) adds two two-word unsigned integers,
  46. composed by HIGH_ADDEND_1 and LOW_ADDEND_1, and HIGH_ADDEND_2 and
  47. LOW_ADDEND_2 respectively. The result is placed in HIGH_SUM and
  48. LOW_SUM. Overflow (i.e. carry out) is not stored anywhere, and is
  49. lost.
  50. 7) sub_ddmmss(high_difference, low_difference, high_minuend,
  51. low_minuend, high_subtrahend, low_subtrahend) subtracts two
  52. two-word unsigned integers, composed by HIGH_MINUEND_1 and
  53. LOW_MINUEND_1, and HIGH_SUBTRAHEND_2 and LOW_SUBTRAHEND_2
  54. respectively. The result is placed in HIGH_DIFFERENCE and
  55. LOW_DIFFERENCE. Overflow (i.e. carry out) is not stored anywhere,
  56. and is lost.
  57. If any of these macros are left undefined for a particular CPU,
  58. C macros are used. */
  59. #if defined (__arm__)
  60. #define add_ssaaaa(sh, sl, ah, al, bh, bl) \
  61. __asm__ ("adds %1, %4, %5 \n\
  62. adc %0, %2, %3" \
  63. : "=r" ((USItype) (sh)), \
  64. "=&r" ((USItype) (sl)) \
  65. : "%r" ((USItype) (ah)), \
  66. "rI" ((USItype) (bh)), \
  67. "%r" ((USItype) (al)), \
  68. "rI" ((USItype) (bl)))
  69. #define sub_ddmmss(sh, sl, ah, al, bh, bl) \
  70. __asm__ ("subs %1, %4, %5 \n\
  71. sbc %0, %2, %3" \
  72. : "=r" ((USItype) (sh)), \
  73. "=&r" ((USItype) (sl)) \
  74. : "r" ((USItype) (ah)), \
  75. "rI" ((USItype) (bh)), \
  76. "r" ((USItype) (al)), \
  77. "rI" ((USItype) (bl)))
  78. #define umul_ppmm(xh, xl, a, b) \
  79. {register USItype __t0, __t1, __t2; \
  80. __asm__ ("%@ Inlined umul_ppmm \n\
  81. mov %2, %5, lsr #16 \n\
  82. mov %0, %6, lsr #16 \n\
  83. bic %3, %5, %2, lsl #16 \n\
  84. bic %4, %6, %0, lsl #16 \n\
  85. mul %1, %3, %4 \n\
  86. mul %4, %2, %4 \n\
  87. mul %3, %0, %3 \n\
  88. mul %0, %2, %0 \n\
  89. adds %3, %4, %3 \n\
  90. addcs %0, %0, #65536 \n\
  91. adds %1, %1, %3, lsl #16 \n\
  92. adc %0, %0, %3, lsr #16" \
  93. : "=&r" ((USItype) (xh)), \
  94. "=r" ((USItype) (xl)), \
  95. "=&r" (__t0), "=&r" (__t1), "=r" (__t2) \
  96. : "r" ((USItype) (a)), \
  97. "r" ((USItype) (b)));}
  98. #define UMUL_TIME 20
  99. #define UDIV_TIME 100
  100. #endif /* __arm__ */
  101. #define __umulsidi3(u, v) \
  102. ({DIunion __w; \
  103. umul_ppmm (__w.s.high, __w.s.low, u, v); \
  104. __w.ll; })
  105. #define __udiv_qrnnd_c(q, r, n1, n0, d) \
  106. do { \
  107. USItype __d1, __d0, __q1, __q0; \
  108. USItype __r1, __r0, __m; \
  109. __d1 = __ll_highpart (d); \
  110. __d0 = __ll_lowpart (d); \
  111. \
  112. __r1 = (n1) % __d1; \
  113. __q1 = (n1) / __d1; \
  114. __m = (USItype) __q1 * __d0; \
  115. __r1 = __r1 * __ll_B | __ll_highpart (n0); \
  116. if (__r1 < __m) \
  117. { \
  118. __q1--, __r1 += (d); \
  119. if (__r1 >= (d)) /* i.e. we didn't get carry when adding to __r1 */\
  120. if (__r1 < __m) \
  121. __q1--, __r1 += (d); \
  122. } \
  123. __r1 -= __m; \
  124. \
  125. __r0 = __r1 % __d1; \
  126. __q0 = __r1 / __d1; \
  127. __m = (USItype) __q0 * __d0; \
  128. __r0 = __r0 * __ll_B | __ll_lowpart (n0); \
  129. if (__r0 < __m) \
  130. { \
  131. __q0--, __r0 += (d); \
  132. if (__r0 >= (d)) \
  133. if (__r0 < __m) \
  134. __q0--, __r0 += (d); \
  135. } \
  136. __r0 -= __m; \
  137. \
  138. (q) = (USItype) __q1 * __ll_B | __q0; \
  139. (r) = __r0; \
  140. } while (0)
  141. #define UDIV_NEEDS_NORMALIZATION 1
  142. #define udiv_qrnnd __udiv_qrnnd_c
  143. extern const UQItype __clz_tab[];
  144. #define count_leading_zeros(count, x) \
  145. do { \
  146. USItype __xr = (x); \
  147. USItype __a; \
  148. \
  149. if (SI_TYPE_SIZE <= 32) \
  150. { \
  151. __a = __xr < ((USItype)1<<2*__BITS4) \
  152. ? (__xr < ((USItype)1<<__BITS4) ? 0 : __BITS4) \
  153. : (__xr < ((USItype)1<<3*__BITS4) ? 2*__BITS4 : 3*__BITS4); \
  154. } \
  155. else \
  156. { \
  157. for (__a = SI_TYPE_SIZE - 8; __a > 0; __a -= 8) \
  158. if (((__xr >> __a) & 0xff) != 0) \
  159. break; \
  160. } \
  161. \
  162. (count) = SI_TYPE_SIZE - (__clz_tab[__xr >> __a] + __a); \
  163. } while (0)