longlong.h 60 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774
  1. /* longlong.h -- definitions for mixed size 32/64 bit arithmetic.
  2. Copyright (C) 1991-2022 Free Software Foundation, Inc.
  3. This file is part of the GNU C Library.
  4. The GNU C Library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Lesser General Public
  6. License as published by the Free Software Foundation; either
  7. version 2.1 of the License, or (at your option) any later version.
  8. In addition to the permissions in the GNU Lesser General Public
  9. License, the Free Software Foundation gives you unlimited
  10. permission to link the compiled version of this file into
  11. combinations with other programs, and to distribute those
  12. combinations without any restriction coming from the use of this
  13. file. (The Lesser General Public License restrictions do apply in
  14. other respects; for example, they cover modification of the file,
  15. and distribution when not linked into a combine executable.)
  16. The GNU C Library is distributed in the hope that it will be useful,
  17. but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  19. Lesser General Public License for more details.
  20. You should have received a copy of the GNU Lesser General Public
  21. License along with the GNU C Library; if not, see
  22. <http://www.gnu.org/licenses/>. */
  23. /* You have to define the following before including this file:
  24. UWtype -- An unsigned type, default type for operations (typically a "word")
  25. UHWtype -- An unsigned type, at least half the size of UWtype.
  26. UDWtype -- An unsigned type, at least twice as large a UWtype
  27. W_TYPE_SIZE -- size in bits of UWtype
  28. UQItype -- Unsigned 8 bit type.
  29. SItype, USItype -- Signed and unsigned 32 bit types.
  30. DItype, UDItype -- Signed and unsigned 64 bit types.
  31. On a 32 bit machine UWtype should typically be USItype;
  32. on a 64 bit machine, UWtype should typically be UDItype. */
  33. #define __BITS4 (W_TYPE_SIZE / 4)
  34. #define __ll_B ((UWtype) 1 << (W_TYPE_SIZE / 2))
  35. #define __ll_lowpart(t) ((UWtype) (t) & (__ll_B - 1))
  36. #define __ll_highpart(t) ((UWtype) (t) >> (W_TYPE_SIZE / 2))
  37. #ifndef W_TYPE_SIZE
  38. #define W_TYPE_SIZE 32
  39. #define UWtype USItype
  40. #define UHWtype USItype
  41. #define UDWtype UDItype
  42. #endif
  43. /* Used in glibc only. */
  44. #ifndef attribute_hidden
  45. #define attribute_hidden
  46. #endif
  47. extern const UQItype __clz_tab[256] attribute_hidden;
  48. /* Define auxiliary asm macros.
  49. 1) umul_ppmm(high_prod, low_prod, multiplier, multiplicand) multiplies two
  50. UWtype integers MULTIPLIER and MULTIPLICAND, and generates a two UWtype
  51. word product in HIGH_PROD and LOW_PROD.
  52. 2) __umulsidi3(a,b) multiplies two UWtype integers A and B, and returns a
  53. UDWtype product. This is just a variant of umul_ppmm.
  54. 3) udiv_qrnnd(quotient, remainder, high_numerator, low_numerator,
  55. denominator) divides a UDWtype, composed by the UWtype integers
  56. HIGH_NUMERATOR and LOW_NUMERATOR, by DENOMINATOR and places the quotient
  57. in QUOTIENT and the remainder in REMAINDER. HIGH_NUMERATOR must be less
  58. than DENOMINATOR for correct operation. If, in addition, the most
  59. significant bit of DENOMINATOR must be 1, then the pre-processor symbol
  60. UDIV_NEEDS_NORMALIZATION is defined to 1.
  61. 4) sdiv_qrnnd(quotient, remainder, high_numerator, low_numerator,
  62. denominator). Like udiv_qrnnd but the numbers are signed. The quotient
  63. is rounded towards 0.
  64. 5) count_leading_zeros(count, x) counts the number of zero-bits from the
  65. msb to the first nonzero bit in the UWtype X. This is the number of
  66. steps X needs to be shifted left to set the msb. Undefined for X == 0,
  67. unless the symbol COUNT_LEADING_ZEROS_0 is defined to some value.
  68. 6) count_trailing_zeros(count, x) like count_leading_zeros, but counts
  69. from the least significant end.
  70. 7) add_ssaaaa(high_sum, low_sum, high_addend_1, low_addend_1,
  71. high_addend_2, low_addend_2) adds two UWtype integers, composed by
  72. HIGH_ADDEND_1 and LOW_ADDEND_1, and HIGH_ADDEND_2 and LOW_ADDEND_2
  73. respectively. The result is placed in HIGH_SUM and LOW_SUM. Overflow
  74. (i.e. carry out) is not stored anywhere, and is lost.
  75. 8) sub_ddmmss(high_difference, low_difference, high_minuend, low_minuend,
  76. high_subtrahend, low_subtrahend) subtracts two two-word UWtype integers,
  77. composed by HIGH_MINUEND_1 and LOW_MINUEND_1, and HIGH_SUBTRAHEND_2 and
  78. LOW_SUBTRAHEND_2 respectively. The result is placed in HIGH_DIFFERENCE
  79. and LOW_DIFFERENCE. Overflow (i.e. carry out) is not stored anywhere,
  80. and is lost.
  81. If any of these macros are left undefined for a particular CPU,
  82. C macros are used. */
  83. /* The CPUs come in alphabetical order below.
  84. Please add support for more CPUs here, or improve the current support
  85. for the CPUs below!
  86. (E.g. WE32100, IBM360.) */
  87. #if defined (__GNUC__) && !defined (NO_ASM)
  88. /* We sometimes need to clobber "cc" with gcc2, but that would not be
  89. understood by gcc1. Use cpp to avoid major code duplication. */
  90. #if __GNUC__ < 2
  91. #define __CLOBBER_CC
  92. #define __AND_CLOBBER_CC
  93. #else /* __GNUC__ >= 2 */
  94. #define __CLOBBER_CC : "cc"
  95. #define __AND_CLOBBER_CC , "cc"
  96. #endif /* __GNUC__ < 2 */
  97. #if defined (__aarch64__)
  98. #if W_TYPE_SIZE == 32
  99. #define count_leading_zeros(COUNT, X) ((COUNT) = __builtin_clz (X))
  100. #define count_trailing_zeros(COUNT, X) ((COUNT) = __builtin_ctz (X))
  101. #define COUNT_LEADING_ZEROS_0 32
  102. #endif /* W_TYPE_SIZE == 32 */
  103. #if W_TYPE_SIZE == 64
  104. #define count_leading_zeros(COUNT, X) ((COUNT) = __builtin_clzll (X))
  105. #define count_trailing_zeros(COUNT, X) ((COUNT) = __builtin_ctzll (X))
  106. #define COUNT_LEADING_ZEROS_0 64
  107. #endif /* W_TYPE_SIZE == 64 */
  108. #endif /* __aarch64__ */
  109. #if defined (__alpha) && W_TYPE_SIZE == 64
  110. /* There is a bug in g++ before version 5 that
  111. errors on __builtin_alpha_umulh. */
  112. #if !defined(__cplusplus) || __GNUC__ >= 5
  113. #define umul_ppmm(ph, pl, m0, m1) \
  114. do { \
  115. UDItype __m0 = (m0), __m1 = (m1); \
  116. (ph) = __builtin_alpha_umulh (__m0, __m1); \
  117. (pl) = __m0 * __m1; \
  118. } while (0)
  119. #define UMUL_TIME 46
  120. #endif /* !c++ */
  121. #ifndef LONGLONG_STANDALONE
  122. #define udiv_qrnnd(q, r, n1, n0, d) \
  123. do { UDItype __r; \
  124. (q) = __udiv_qrnnd (&__r, (n1), (n0), (d)); \
  125. (r) = __r; \
  126. } while (0)
  127. extern UDItype __udiv_qrnnd (UDItype *, UDItype, UDItype, UDItype);
  128. #define UDIV_TIME 220
  129. #endif /* LONGLONG_STANDALONE */
  130. #ifdef __alpha_cix__
  131. #define count_leading_zeros(COUNT,X) ((COUNT) = __builtin_clzl (X))
  132. #define count_trailing_zeros(COUNT,X) ((COUNT) = __builtin_ctzl (X))
  133. #define COUNT_LEADING_ZEROS_0 64
  134. #else
  135. #define count_leading_zeros(COUNT,X) \
  136. do { \
  137. UDItype __xr = (X), __t, __a; \
  138. __t = __builtin_alpha_cmpbge (0, __xr); \
  139. __a = __clz_tab[__t ^ 0xff] - 1; \
  140. __t = __builtin_alpha_extbl (__xr, __a); \
  141. (COUNT) = 64 - (__clz_tab[__t] + __a*8); \
  142. } while (0)
  143. #define count_trailing_zeros(COUNT,X) \
  144. do { \
  145. UDItype __xr = (X), __t, __a; \
  146. __t = __builtin_alpha_cmpbge (0, __xr); \
  147. __t = ~__t & -~__t; \
  148. __a = ((__t & 0xCC) != 0) * 2; \
  149. __a += ((__t & 0xF0) != 0) * 4; \
  150. __a += ((__t & 0xAA) != 0); \
  151. __t = __builtin_alpha_extbl (__xr, __a); \
  152. __a <<= 3; \
  153. __t &= -__t; \
  154. __a += ((__t & 0xCC) != 0) * 2; \
  155. __a += ((__t & 0xF0) != 0) * 4; \
  156. __a += ((__t & 0xAA) != 0); \
  157. (COUNT) = __a; \
  158. } while (0)
  159. #endif /* __alpha_cix__ */
  160. #endif /* __alpha */
  161. #if defined (__arc__) && W_TYPE_SIZE == 32
  162. #define add_ssaaaa(sh, sl, ah, al, bh, bl) \
  163. __asm__ ("add.f %1, %4, %5\n\tadc %0, %2, %3" \
  164. : "=r" ((USItype) (sh)), \
  165. "=&r" ((USItype) (sl)) \
  166. : "%r" ((USItype) (ah)), \
  167. "rICal" ((USItype) (bh)), \
  168. "%r" ((USItype) (al)), \
  169. "rICal" ((USItype) (bl)) \
  170. : "cc")
  171. #define sub_ddmmss(sh, sl, ah, al, bh, bl) \
  172. __asm__ ("sub.f %1, %4, %5\n\tsbc %0, %2, %3" \
  173. : "=r" ((USItype) (sh)), \
  174. "=&r" ((USItype) (sl)) \
  175. : "r" ((USItype) (ah)), \
  176. "rICal" ((USItype) (bh)), \
  177. "r" ((USItype) (al)), \
  178. "rICal" ((USItype) (bl)) \
  179. : "cc")
  180. #define __umulsidi3(u,v) ((UDItype)(USItype)u*(USItype)v)
  181. #ifdef __ARC_NORM__
  182. #define count_leading_zeros(count, x) \
  183. do \
  184. { \
  185. SItype c_; \
  186. \
  187. __asm__ ("norm.f\t%0,%1\n\tmov.mi\t%0,-1" : "=r" (c_) : "r" (x) : "cc");\
  188. (count) = c_ + 1; \
  189. } \
  190. while (0)
  191. #define COUNT_LEADING_ZEROS_0 32
  192. #endif /* __ARC_NORM__ */
  193. #endif /* __arc__ */
  194. #if defined (__arm__) && (defined (__thumb2__) || !defined (__thumb__)) \
  195. && W_TYPE_SIZE == 32
  196. #define add_ssaaaa(sh, sl, ah, al, bh, bl) \
  197. __asm__ ("adds %1, %4, %5\n\tadc %0, %2, %3" \
  198. : "=r" ((USItype) (sh)), \
  199. "=&r" ((USItype) (sl)) \
  200. : "%r" ((USItype) (ah)), \
  201. "rI" ((USItype) (bh)), \
  202. "%r" ((USItype) (al)), \
  203. "rI" ((USItype) (bl)) __CLOBBER_CC)
  204. #define sub_ddmmss(sh, sl, ah, al, bh, bl) \
  205. __asm__ ("subs %1, %4, %5\n\tsbc %0, %2, %3" \
  206. : "=r" ((USItype) (sh)), \
  207. "=&r" ((USItype) (sl)) \
  208. : "r" ((USItype) (ah)), \
  209. "rI" ((USItype) (bh)), \
  210. "r" ((USItype) (al)), \
  211. "rI" ((USItype) (bl)) __CLOBBER_CC)
  212. # if defined(__ARM_ARCH_2__) || defined(__ARM_ARCH_2A__) \
  213. || defined(__ARM_ARCH_3__)
  214. # define umul_ppmm(xh, xl, a, b) \
  215. do { \
  216. register USItype __t0, __t1, __t2; \
  217. __asm__ ("%@ Inlined umul_ppmm\n" \
  218. " mov %2, %5, lsr #16\n" \
  219. " mov %0, %6, lsr #16\n" \
  220. " bic %3, %5, %2, lsl #16\n" \
  221. " bic %4, %6, %0, lsl #16\n" \
  222. " mul %1, %3, %4\n" \
  223. " mul %4, %2, %4\n" \
  224. " mul %3, %0, %3\n" \
  225. " mul %0, %2, %0\n" \
  226. " adds %3, %4, %3\n" \
  227. " addcs %0, %0, #65536\n" \
  228. " adds %1, %1, %3, lsl #16\n" \
  229. " adc %0, %0, %3, lsr #16" \
  230. : "=&r" ((USItype) (xh)), \
  231. "=r" ((USItype) (xl)), \
  232. "=&r" (__t0), "=&r" (__t1), "=r" (__t2) \
  233. : "r" ((USItype) (a)), \
  234. "r" ((USItype) (b)) __CLOBBER_CC ); \
  235. } while (0)
  236. # define UMUL_TIME 20
  237. # else
  238. # define umul_ppmm(xh, xl, a, b) \
  239. do { \
  240. /* Generate umull, under compiler control. */ \
  241. register UDItype __t0 = (UDItype)(USItype)(a) * (USItype)(b); \
  242. (xl) = (USItype)__t0; \
  243. (xh) = (USItype)(__t0 >> 32); \
  244. } while (0)
  245. # define UMUL_TIME 3
  246. # endif
  247. # define UDIV_TIME 100
  248. #endif /* __arm__ */
  249. #if defined(__arm__)
  250. /* Let gcc decide how best to implement count_leading_zeros. */
  251. #define count_leading_zeros(COUNT,X) ((COUNT) = __builtin_clz (X))
  252. #define count_trailing_zeros(COUNT,X) ((COUNT) = __builtin_ctz (X))
  253. #define COUNT_LEADING_ZEROS_0 32
  254. #endif
  255. #if defined (__AVR__)
  256. #if W_TYPE_SIZE == 16
  257. #define count_leading_zeros(COUNT,X) ((COUNT) = __builtin_clz (X))
  258. #define count_trailing_zeros(COUNT,X) ((COUNT) = __builtin_ctz (X))
  259. #define COUNT_LEADING_ZEROS_0 16
  260. #endif /* W_TYPE_SIZE == 16 */
  261. #if W_TYPE_SIZE == 32
  262. #define count_leading_zeros(COUNT,X) ((COUNT) = __builtin_clzl (X))
  263. #define count_trailing_zeros(COUNT,X) ((COUNT) = __builtin_ctzl (X))
  264. #define COUNT_LEADING_ZEROS_0 32
  265. #endif /* W_TYPE_SIZE == 32 */
  266. #if W_TYPE_SIZE == 64
  267. #define count_leading_zeros(COUNT,X) ((COUNT) = __builtin_clzll (X))
  268. #define count_trailing_zeros(COUNT,X) ((COUNT) = __builtin_ctzll (X))
  269. #define COUNT_LEADING_ZEROS_0 64
  270. #endif /* W_TYPE_SIZE == 64 */
  271. #endif /* defined (__AVR__) */
  272. #if defined (__CRIS__)
  273. #if __CRIS_arch_version >= 3
  274. #define count_leading_zeros(COUNT, X) ((COUNT) = __builtin_clz (X))
  275. #define COUNT_LEADING_ZEROS_0 32
  276. #endif /* __CRIS_arch_version >= 3 */
  277. #if __CRIS_arch_version >= 8
  278. #define count_trailing_zeros(COUNT, X) ((COUNT) = __builtin_ctz (X))
  279. #endif /* __CRIS_arch_version >= 8 */
  280. #if __CRIS_arch_version >= 10
  281. #define __umulsidi3(u,v) ((UDItype)(USItype) (u) * (UDItype)(USItype) (v))
  282. #else
  283. #define __umulsidi3 __umulsidi3
  284. extern UDItype __umulsidi3 (USItype, USItype);
  285. #endif /* __CRIS_arch_version >= 10 */
  286. #define umul_ppmm(w1, w0, u, v) \
  287. do { \
  288. UDItype __x = __umulsidi3 (u, v); \
  289. (w0) = (USItype) (__x); \
  290. (w1) = (USItype) (__x >> 32); \
  291. } while (0)
  292. /* FIXME: defining add_ssaaaa and sub_ddmmss should be advantageous for
  293. DFmode ("double" intrinsics, avoiding two of the three insns handling
  294. carry), but defining them as open-code C composing and doing the
  295. operation in DImode (UDImode) shows that the DImode needs work:
  296. register pressure from requiring neighboring registers and the
  297. traffic to and from them come to dominate, in the 4.7 series. */
  298. #endif /* defined (__CRIS__) */
  299. #if defined (__hppa) && W_TYPE_SIZE == 32
  300. #define add_ssaaaa(sh, sl, ah, al, bh, bl) \
  301. __asm__ ("add %4,%5,%1\n\taddc %2,%3,%0" \
  302. : "=r" ((USItype) (sh)), \
  303. "=&r" ((USItype) (sl)) \
  304. : "%rM" ((USItype) (ah)), \
  305. "rM" ((USItype) (bh)), \
  306. "%rM" ((USItype) (al)), \
  307. "rM" ((USItype) (bl)))
  308. #define sub_ddmmss(sh, sl, ah, al, bh, bl) \
  309. __asm__ ("sub %4,%5,%1\n\tsubb %2,%3,%0" \
  310. : "=r" ((USItype) (sh)), \
  311. "=&r" ((USItype) (sl)) \
  312. : "rM" ((USItype) (ah)), \
  313. "rM" ((USItype) (bh)), \
  314. "rM" ((USItype) (al)), \
  315. "rM" ((USItype) (bl)))
  316. #if defined (_PA_RISC1_1)
  317. #define umul_ppmm(w1, w0, u, v) \
  318. do { \
  319. union \
  320. { \
  321. UDItype __f; \
  322. struct {USItype __w1, __w0;} __w1w0; \
  323. } __t; \
  324. __asm__ ("xmpyu %1,%2,%0" \
  325. : "=x" (__t.__f) \
  326. : "x" ((USItype) (u)), \
  327. "x" ((USItype) (v))); \
  328. (w1) = __t.__w1w0.__w1; \
  329. (w0) = __t.__w1w0.__w0; \
  330. } while (0)
  331. #define UMUL_TIME 8
  332. #else
  333. #define UMUL_TIME 30
  334. #endif
  335. #define UDIV_TIME 40
  336. #define count_leading_zeros(count, x) \
  337. do { \
  338. USItype __tmp; \
  339. __asm__ ( \
  340. "ldi 1,%0\n" \
  341. " extru,= %1,15,16,%%r0 ; Bits 31..16 zero?\n" \
  342. " extru,tr %1,15,16,%1 ; No. Shift down, skip add.\n"\
  343. " ldo 16(%0),%0 ; Yes. Perform add.\n" \
  344. " extru,= %1,23,8,%%r0 ; Bits 15..8 zero?\n" \
  345. " extru,tr %1,23,8,%1 ; No. Shift down, skip add.\n"\
  346. " ldo 8(%0),%0 ; Yes. Perform add.\n" \
  347. " extru,= %1,27,4,%%r0 ; Bits 7..4 zero?\n" \
  348. " extru,tr %1,27,4,%1 ; No. Shift down, skip add.\n"\
  349. " ldo 4(%0),%0 ; Yes. Perform add.\n" \
  350. " extru,= %1,29,2,%%r0 ; Bits 3..2 zero?\n" \
  351. " extru,tr %1,29,2,%1 ; No. Shift down, skip add.\n"\
  352. " ldo 2(%0),%0 ; Yes. Perform add.\n" \
  353. " extru %1,30,1,%1 ; Extract bit 1.\n" \
  354. " sub %0,%1,%0 ; Subtract it.\n" \
  355. : "=r" (count), "=r" (__tmp) : "1" (x)); \
  356. } while (0)
  357. #endif
  358. #if (defined (__i370__) || defined (__s390__) || defined (__mvs__)) && W_TYPE_SIZE == 32
  359. #if !defined (__zarch__)
  360. #define smul_ppmm(xh, xl, m0, m1) \
  361. do { \
  362. union {DItype __ll; \
  363. struct {USItype __h, __l;} __i; \
  364. } __x; \
  365. __asm__ ("lr %N0,%1\n\tmr %0,%2" \
  366. : "=&r" (__x.__ll) \
  367. : "r" (m0), "r" (m1)); \
  368. (xh) = __x.__i.__h; (xl) = __x.__i.__l; \
  369. } while (0)
  370. #define sdiv_qrnnd(q, r, n1, n0, d) \
  371. do { \
  372. union {DItype __ll; \
  373. struct {USItype __h, __l;} __i; \
  374. } __x; \
  375. __x.__i.__h = n1; __x.__i.__l = n0; \
  376. __asm__ ("dr %0,%2" \
  377. : "=r" (__x.__ll) \
  378. : "0" (__x.__ll), "r" (d)); \
  379. (q) = __x.__i.__l; (r) = __x.__i.__h; \
  380. } while (0)
  381. #else
  382. #define smul_ppmm(xh, xl, m0, m1) \
  383. do { \
  384. register SItype __r0 __asm__ ("0"); \
  385. register SItype __r1 __asm__ ("1") = (m0); \
  386. \
  387. __asm__ ("mr\t%%r0,%3" \
  388. : "=r" (__r0), "=r" (__r1) \
  389. : "r" (__r1), "r" (m1)); \
  390. (xh) = __r0; (xl) = __r1; \
  391. } while (0)
  392. #define sdiv_qrnnd(q, r, n1, n0, d) \
  393. do { \
  394. register SItype __r0 __asm__ ("0") = (n1); \
  395. register SItype __r1 __asm__ ("1") = (n0); \
  396. \
  397. __asm__ ("dr\t%%r0,%4" \
  398. : "=r" (__r0), "=r" (__r1) \
  399. : "r" (__r0), "r" (__r1), "r" (d)); \
  400. (q) = __r1; (r) = __r0; \
  401. } while (0)
  402. #endif /* __zarch__ */
  403. #endif
  404. #if (defined (__i386__) || defined (__i486__)) && W_TYPE_SIZE == 32
  405. #define add_ssaaaa(sh, sl, ah, al, bh, bl) \
  406. __asm__ ("add{l} {%5,%1|%1,%5}\n\tadc{l} {%3,%0|%0,%3}" \
  407. : "=r" ((USItype) (sh)), \
  408. "=&r" ((USItype) (sl)) \
  409. : "%0" ((USItype) (ah)), \
  410. "g" ((USItype) (bh)), \
  411. "%1" ((USItype) (al)), \
  412. "g" ((USItype) (bl)))
  413. #define sub_ddmmss(sh, sl, ah, al, bh, bl) \
  414. __asm__ ("sub{l} {%5,%1|%1,%5}\n\tsbb{l} {%3,%0|%0,%3}" \
  415. : "=r" ((USItype) (sh)), \
  416. "=&r" ((USItype) (sl)) \
  417. : "0" ((USItype) (ah)), \
  418. "g" ((USItype) (bh)), \
  419. "1" ((USItype) (al)), \
  420. "g" ((USItype) (bl)))
  421. #define umul_ppmm(w1, w0, u, v) \
  422. __asm__ ("mul{l} %3" \
  423. : "=a" ((USItype) (w0)), \
  424. "=d" ((USItype) (w1)) \
  425. : "%0" ((USItype) (u)), \
  426. "rm" ((USItype) (v)))
  427. #define udiv_qrnnd(q, r, n1, n0, dv) \
  428. __asm__ ("div{l} %4" \
  429. : "=a" ((USItype) (q)), \
  430. "=d" ((USItype) (r)) \
  431. : "0" ((USItype) (n0)), \
  432. "1" ((USItype) (n1)), \
  433. "rm" ((USItype) (dv)))
  434. #define count_leading_zeros(count, x) ((count) = __builtin_clz (x))
  435. #define count_trailing_zeros(count, x) ((count) = __builtin_ctz (x))
  436. #define UMUL_TIME 40
  437. #define UDIV_TIME 40
  438. #endif /* 80x86 */
  439. #if defined (__x86_64__) && W_TYPE_SIZE == 64
  440. #define add_ssaaaa(sh, sl, ah, al, bh, bl) \
  441. __asm__ ("add{q} {%5,%1|%1,%5}\n\tadc{q} {%3,%0|%0,%3}" \
  442. : "=r" ((UDItype) (sh)), \
  443. "=&r" ((UDItype) (sl)) \
  444. : "%0" ((UDItype) (ah)), \
  445. "rme" ((UDItype) (bh)), \
  446. "%1" ((UDItype) (al)), \
  447. "rme" ((UDItype) (bl)))
  448. #define sub_ddmmss(sh, sl, ah, al, bh, bl) \
  449. __asm__ ("sub{q} {%5,%1|%1,%5}\n\tsbb{q} {%3,%0|%0,%3}" \
  450. : "=r" ((UDItype) (sh)), \
  451. "=&r" ((UDItype) (sl)) \
  452. : "0" ((UDItype) (ah)), \
  453. "rme" ((UDItype) (bh)), \
  454. "1" ((UDItype) (al)), \
  455. "rme" ((UDItype) (bl)))
  456. #define umul_ppmm(w1, w0, u, v) \
  457. __asm__ ("mul{q} %3" \
  458. : "=a" ((UDItype) (w0)), \
  459. "=d" ((UDItype) (w1)) \
  460. : "%0" ((UDItype) (u)), \
  461. "rm" ((UDItype) (v)))
  462. #define udiv_qrnnd(q, r, n1, n0, dv) \
  463. __asm__ ("div{q} %4" \
  464. : "=a" ((UDItype) (q)), \
  465. "=d" ((UDItype) (r)) \
  466. : "0" ((UDItype) (n0)), \
  467. "1" ((UDItype) (n1)), \
  468. "rm" ((UDItype) (dv)))
  469. #define count_leading_zeros(count, x) ((count) = __builtin_clzll (x))
  470. #define count_trailing_zeros(count, x) ((count) = __builtin_ctzll (x))
  471. #define UMUL_TIME 40
  472. #define UDIV_TIME 40
  473. #endif /* x86_64 */
  474. #if defined (__i960__) && W_TYPE_SIZE == 32
  475. #define umul_ppmm(w1, w0, u, v) \
  476. ({union {UDItype __ll; \
  477. struct {USItype __l, __h;} __i; \
  478. } __xx; \
  479. __asm__ ("emul %2,%1,%0" \
  480. : "=d" (__xx.__ll) \
  481. : "%dI" ((USItype) (u)), \
  482. "dI" ((USItype) (v))); \
  483. (w1) = __xx.__i.__h; (w0) = __xx.__i.__l;})
  484. #define __umulsidi3(u, v) \
  485. ({UDItype __w; \
  486. __asm__ ("emul %2,%1,%0" \
  487. : "=d" (__w) \
  488. : "%dI" ((USItype) (u)), \
  489. "dI" ((USItype) (v))); \
  490. __w; })
  491. #endif /* __i960__ */
  492. #if defined (__ia64) && W_TYPE_SIZE == 64
  493. /* This form encourages gcc (pre-release 3.4 at least) to emit predicated
  494. "sub r=r,r" and "sub r=r,r,1", giving a 2 cycle latency. The generic
  495. code using "al<bl" arithmetically comes out making an actual 0 or 1 in a
  496. register, which takes an extra cycle. */
  497. #define sub_ddmmss(sh, sl, ah, al, bh, bl) \
  498. do { \
  499. UWtype __x; \
  500. __x = (al) - (bl); \
  501. if ((al) < (bl)) \
  502. (sh) = (ah) - (bh) - 1; \
  503. else \
  504. (sh) = (ah) - (bh); \
  505. (sl) = __x; \
  506. } while (0)
  507. /* Do both product parts in assembly, since that gives better code with
  508. all gcc versions. Some callers will just use the upper part, and in
  509. that situation we waste an instruction, but not any cycles. */
  510. #define umul_ppmm(ph, pl, m0, m1) \
  511. __asm__ ("xma.hu %0 = %2, %3, f0\n\txma.l %1 = %2, %3, f0" \
  512. : "=&f" (ph), "=f" (pl) \
  513. : "f" (m0), "f" (m1))
  514. #define count_leading_zeros(count, x) \
  515. do { \
  516. UWtype _x = (x), _y, _a, _c; \
  517. __asm__ ("mux1 %0 = %1, @rev" : "=r" (_y) : "r" (_x)); \
  518. __asm__ ("czx1.l %0 = %1" : "=r" (_a) : "r" (-_y | _y)); \
  519. _c = (_a - 1) << 3; \
  520. _x >>= _c; \
  521. if (_x >= 1 << 4) \
  522. _x >>= 4, _c += 4; \
  523. if (_x >= 1 << 2) \
  524. _x >>= 2, _c += 2; \
  525. _c += _x >> 1; \
  526. (count) = W_TYPE_SIZE - 1 - _c; \
  527. } while (0)
  528. /* similar to what gcc does for __builtin_ffs, but 0 based rather than 1
  529. based, and we don't need a special case for x==0 here */
  530. #define count_trailing_zeros(count, x) \
  531. do { \
  532. UWtype __ctz_x = (x); \
  533. __asm__ ("popcnt %0 = %1" \
  534. : "=r" (count) \
  535. : "r" ((__ctz_x-1) & ~__ctz_x)); \
  536. } while (0)
  537. #define UMUL_TIME 14
  538. #endif
  539. #if defined (__M32R__) && W_TYPE_SIZE == 32
  540. #define add_ssaaaa(sh, sl, ah, al, bh, bl) \
  541. /* The cmp clears the condition bit. */ \
  542. __asm__ ("cmp %0,%0\n\taddx %1,%5\n\taddx %0,%3" \
  543. : "=r" ((USItype) (sh)), \
  544. "=&r" ((USItype) (sl)) \
  545. : "0" ((USItype) (ah)), \
  546. "r" ((USItype) (bh)), \
  547. "1" ((USItype) (al)), \
  548. "r" ((USItype) (bl)) \
  549. : "cbit")
  550. #define sub_ddmmss(sh, sl, ah, al, bh, bl) \
  551. /* The cmp clears the condition bit. */ \
  552. __asm__ ("cmp %0,%0\n\tsubx %1,%5\n\tsubx %0,%3" \
  553. : "=r" ((USItype) (sh)), \
  554. "=&r" ((USItype) (sl)) \
  555. : "0" ((USItype) (ah)), \
  556. "r" ((USItype) (bh)), \
  557. "1" ((USItype) (al)), \
  558. "r" ((USItype) (bl)) \
  559. : "cbit")
  560. #endif /* __M32R__ */
  561. #if defined (__mc68000__) && W_TYPE_SIZE == 32
  562. #define add_ssaaaa(sh, sl, ah, al, bh, bl) \
  563. __asm__ ("add%.l %5,%1\n\taddx%.l %3,%0" \
  564. : "=d" ((USItype) (sh)), \
  565. "=&d" ((USItype) (sl)) \
  566. : "%0" ((USItype) (ah)), \
  567. "d" ((USItype) (bh)), \
  568. "%1" ((USItype) (al)), \
  569. "g" ((USItype) (bl)))
  570. #define sub_ddmmss(sh, sl, ah, al, bh, bl) \
  571. __asm__ ("sub%.l %5,%1\n\tsubx%.l %3,%0" \
  572. : "=d" ((USItype) (sh)), \
  573. "=&d" ((USItype) (sl)) \
  574. : "0" ((USItype) (ah)), \
  575. "d" ((USItype) (bh)), \
  576. "1" ((USItype) (al)), \
  577. "g" ((USItype) (bl)))
  578. /* The '020, '030, '040, '060 and CPU32 have 32x32->64 and 64/32->32q-32r. */
  579. #if (defined (__mc68020__) && !defined (__mc68060__))
  580. #define umul_ppmm(w1, w0, u, v) \
  581. __asm__ ("mulu%.l %3,%1:%0" \
  582. : "=d" ((USItype) (w0)), \
  583. "=d" ((USItype) (w1)) \
  584. : "%0" ((USItype) (u)), \
  585. "dmi" ((USItype) (v)))
  586. #define UMUL_TIME 45
  587. #define udiv_qrnnd(q, r, n1, n0, d) \
  588. __asm__ ("divu%.l %4,%1:%0" \
  589. : "=d" ((USItype) (q)), \
  590. "=d" ((USItype) (r)) \
  591. : "0" ((USItype) (n0)), \
  592. "1" ((USItype) (n1)), \
  593. "dmi" ((USItype) (d)))
  594. #define UDIV_TIME 90
  595. #define sdiv_qrnnd(q, r, n1, n0, d) \
  596. __asm__ ("divs%.l %4,%1:%0" \
  597. : "=d" ((USItype) (q)), \
  598. "=d" ((USItype) (r)) \
  599. : "0" ((USItype) (n0)), \
  600. "1" ((USItype) (n1)), \
  601. "dmi" ((USItype) (d)))
  602. #elif defined (__mcoldfire__) /* not mc68020 */
  603. #define umul_ppmm(xh, xl, a, b) \
  604. __asm__ ("| Inlined umul_ppmm\n" \
  605. " move%.l %2,%/d0\n" \
  606. " move%.l %3,%/d1\n" \
  607. " move%.l %/d0,%/d2\n" \
  608. " swap %/d0\n" \
  609. " move%.l %/d1,%/d3\n" \
  610. " swap %/d1\n" \
  611. " move%.w %/d2,%/d4\n" \
  612. " mulu %/d3,%/d4\n" \
  613. " mulu %/d1,%/d2\n" \
  614. " mulu %/d0,%/d3\n" \
  615. " mulu %/d0,%/d1\n" \
  616. " move%.l %/d4,%/d0\n" \
  617. " clr%.w %/d0\n" \
  618. " swap %/d0\n" \
  619. " add%.l %/d0,%/d2\n" \
  620. " add%.l %/d3,%/d2\n" \
  621. " jcc 1f\n" \
  622. " add%.l %#65536,%/d1\n" \
  623. "1: swap %/d2\n" \
  624. " moveq %#0,%/d0\n" \
  625. " move%.w %/d2,%/d0\n" \
  626. " move%.w %/d4,%/d2\n" \
  627. " move%.l %/d2,%1\n" \
  628. " add%.l %/d1,%/d0\n" \
  629. " move%.l %/d0,%0" \
  630. : "=g" ((USItype) (xh)), \
  631. "=g" ((USItype) (xl)) \
  632. : "g" ((USItype) (a)), \
  633. "g" ((USItype) (b)) \
  634. : "d0", "d1", "d2", "d3", "d4")
  635. #define UMUL_TIME 100
  636. #define UDIV_TIME 400
  637. #else /* not ColdFire */
  638. /* %/ inserts REGISTER_PREFIX, %# inserts IMMEDIATE_PREFIX. */
  639. #define umul_ppmm(xh, xl, a, b) \
  640. __asm__ ("| Inlined umul_ppmm\n" \
  641. " move%.l %2,%/d0\n" \
  642. " move%.l %3,%/d1\n" \
  643. " move%.l %/d0,%/d2\n" \
  644. " swap %/d0\n" \
  645. " move%.l %/d1,%/d3\n" \
  646. " swap %/d1\n" \
  647. " move%.w %/d2,%/d4\n" \
  648. " mulu %/d3,%/d4\n" \
  649. " mulu %/d1,%/d2\n" \
  650. " mulu %/d0,%/d3\n" \
  651. " mulu %/d0,%/d1\n" \
  652. " move%.l %/d4,%/d0\n" \
  653. " eor%.w %/d0,%/d0\n" \
  654. " swap %/d0\n" \
  655. " add%.l %/d0,%/d2\n" \
  656. " add%.l %/d3,%/d2\n" \
  657. " jcc 1f\n" \
  658. " add%.l %#65536,%/d1\n" \
  659. "1: swap %/d2\n" \
  660. " moveq %#0,%/d0\n" \
  661. " move%.w %/d2,%/d0\n" \
  662. " move%.w %/d4,%/d2\n" \
  663. " move%.l %/d2,%1\n" \
  664. " add%.l %/d1,%/d0\n" \
  665. " move%.l %/d0,%0" \
  666. : "=g" ((USItype) (xh)), \
  667. "=g" ((USItype) (xl)) \
  668. : "g" ((USItype) (a)), \
  669. "g" ((USItype) (b)) \
  670. : "d0", "d1", "d2", "d3", "d4")
  671. #define UMUL_TIME 100
  672. #define UDIV_TIME 400
  673. #endif /* not mc68020 */
  674. /* The '020, '030, '040 and '060 have bitfield insns.
  675. cpu32 disguises as a 68020, but lacks them. */
  676. #if defined (__mc68020__) && !defined (__mcpu32__)
  677. #define count_leading_zeros(count, x) \
  678. __asm__ ("bfffo %1{%b2:%b2},%0" \
  679. : "=d" ((USItype) (count)) \
  680. : "od" ((USItype) (x)), "n" (0))
  681. /* Some ColdFire architectures have a ff1 instruction supported via
  682. __builtin_clz. */
  683. #elif defined (__mcfisaaplus__) || defined (__mcfisac__)
  684. #define count_leading_zeros(count,x) ((count) = __builtin_clz (x))
  685. #define COUNT_LEADING_ZEROS_0 32
  686. #endif
  687. #endif /* mc68000 */
  688. #if defined (__m88000__) && W_TYPE_SIZE == 32
  689. #define add_ssaaaa(sh, sl, ah, al, bh, bl) \
  690. __asm__ ("addu.co %1,%r4,%r5\n\taddu.ci %0,%r2,%r3" \
  691. : "=r" ((USItype) (sh)), \
  692. "=&r" ((USItype) (sl)) \
  693. : "%rJ" ((USItype) (ah)), \
  694. "rJ" ((USItype) (bh)), \
  695. "%rJ" ((USItype) (al)), \
  696. "rJ" ((USItype) (bl)))
  697. #define sub_ddmmss(sh, sl, ah, al, bh, bl) \
  698. __asm__ ("subu.co %1,%r4,%r5\n\tsubu.ci %0,%r2,%r3" \
  699. : "=r" ((USItype) (sh)), \
  700. "=&r" ((USItype) (sl)) \
  701. : "rJ" ((USItype) (ah)), \
  702. "rJ" ((USItype) (bh)), \
  703. "rJ" ((USItype) (al)), \
  704. "rJ" ((USItype) (bl)))
  705. #define count_leading_zeros(count, x) \
  706. do { \
  707. USItype __cbtmp; \
  708. __asm__ ("ff1 %0,%1" \
  709. : "=r" (__cbtmp) \
  710. : "r" ((USItype) (x))); \
  711. (count) = __cbtmp ^ 31; \
  712. } while (0)
  713. #define COUNT_LEADING_ZEROS_0 63 /* sic */
  714. #if defined (__mc88110__)
  715. #define umul_ppmm(wh, wl, u, v) \
  716. do { \
  717. union {UDItype __ll; \
  718. struct {USItype __h, __l;} __i; \
  719. } __xx; \
  720. __asm__ ("mulu.d %0,%1,%2" \
  721. : "=r" (__xx.__ll) \
  722. : "r" ((USItype) (u)), \
  723. "r" ((USItype) (v))); \
  724. (wh) = __xx.__i.__h; \
  725. (wl) = __xx.__i.__l; \
  726. } while (0)
  727. #define udiv_qrnnd(q, r, n1, n0, d) \
  728. ({union {UDItype __ll; \
  729. struct {USItype __h, __l;} __i; \
  730. } __xx; \
  731. USItype __q; \
  732. __xx.__i.__h = (n1); __xx.__i.__l = (n0); \
  733. __asm__ ("divu.d %0,%1,%2" \
  734. : "=r" (__q) \
  735. : "r" (__xx.__ll), \
  736. "r" ((USItype) (d))); \
  737. (r) = (n0) - __q * (d); (q) = __q; })
  738. #define UMUL_TIME 5
  739. #define UDIV_TIME 25
  740. #else
  741. #define UMUL_TIME 17
  742. #define UDIV_TIME 150
  743. #endif /* __mc88110__ */
  744. #endif /* __m88000__ */
  745. #if defined (__mn10300__)
  746. # if defined (__AM33__)
  747. # define count_leading_zeros(COUNT,X) ((COUNT) = __builtin_clz (X))
  748. # define umul_ppmm(w1, w0, u, v) \
  749. asm("mulu %3,%2,%1,%0" : "=r"(w0), "=r"(w1) : "r"(u), "r"(v))
  750. # define smul_ppmm(w1, w0, u, v) \
  751. asm("mul %3,%2,%1,%0" : "=r"(w0), "=r"(w1) : "r"(u), "r"(v))
  752. # else
  753. # define umul_ppmm(w1, w0, u, v) \
  754. asm("nop; nop; mulu %3,%0" : "=d"(w0), "=z"(w1) : "%0"(u), "d"(v))
  755. # define smul_ppmm(w1, w0, u, v) \
  756. asm("nop; nop; mul %3,%0" : "=d"(w0), "=z"(w1) : "%0"(u), "d"(v))
  757. # endif
  758. # define add_ssaaaa(sh, sl, ah, al, bh, bl) \
  759. do { \
  760. DWunion __s, __a, __b; \
  761. __a.s.low = (al); __a.s.high = (ah); \
  762. __b.s.low = (bl); __b.s.high = (bh); \
  763. __s.ll = __a.ll + __b.ll; \
  764. (sl) = __s.s.low; (sh) = __s.s.high; \
  765. } while (0)
  766. # define sub_ddmmss(sh, sl, ah, al, bh, bl) \
  767. do { \
  768. DWunion __s, __a, __b; \
  769. __a.s.low = (al); __a.s.high = (ah); \
  770. __b.s.low = (bl); __b.s.high = (bh); \
  771. __s.ll = __a.ll - __b.ll; \
  772. (sl) = __s.s.low; (sh) = __s.s.high; \
  773. } while (0)
  774. # define udiv_qrnnd(q, r, nh, nl, d) \
  775. asm("divu %2,%0" : "=D"(q), "=z"(r) : "D"(d), "0"(nl), "1"(nh))
  776. # define sdiv_qrnnd(q, r, nh, nl, d) \
  777. asm("div %2,%0" : "=D"(q), "=z"(r) : "D"(d), "0"(nl), "1"(nh))
  778. # define UMUL_TIME 3
  779. # define UDIV_TIME 38
  780. #endif
  781. #if defined (__mips__) && W_TYPE_SIZE == 32
  782. #define umul_ppmm(w1, w0, u, v) \
  783. do { \
  784. UDItype __x = (UDItype) (USItype) (u) * (USItype) (v); \
  785. (w1) = (USItype) (__x >> 32); \
  786. (w0) = (USItype) (__x); \
  787. } while (0)
  788. #define UMUL_TIME 10
  789. #define UDIV_TIME 100
  790. #if (__mips == 32 || __mips == 64) && ! defined (__mips16)
  791. #define count_leading_zeros(COUNT,X) ((COUNT) = __builtin_clz (X))
  792. #define COUNT_LEADING_ZEROS_0 32
  793. #endif
  794. #endif /* __mips__ */
  795. /* FIXME: We should test _IBMR2 here when we add assembly support for the
  796. system vendor compilers.
  797. FIXME: What's needed for gcc PowerPC VxWorks? __vxworks__ is not good
  798. enough, since that hits ARM and m68k too. */
  799. #if (defined (_ARCH_PPC) /* AIX */ \
  800. || defined (__powerpc__) /* gcc */ \
  801. || defined (__POWERPC__) /* BEOS */ \
  802. || defined (__ppc__) /* Darwin */ \
  803. || (defined (PPC) && ! defined (CPU_FAMILY)) /* gcc 2.7.x GNU&SysV */ \
  804. || (defined (PPC) && defined (CPU_FAMILY) /* VxWorks */ \
  805. && CPU_FAMILY == PPC) \
  806. ) && W_TYPE_SIZE == 32
  807. #define add_ssaaaa(sh, sl, ah, al, bh, bl) \
  808. do { \
  809. if (__builtin_constant_p (bh) && (bh) == 0) \
  810. __asm__ ("add%I4c %1,%3,%4\n\taddze %0,%2" \
  811. : "=r" (sh), "=&r" (sl) : "r" (ah), "%r" (al), "rI" (bl));\
  812. else if (__builtin_constant_p (bh) && (bh) == ~(USItype) 0) \
  813. __asm__ ("add%I4c %1,%3,%4\n\taddme %0,%2" \
  814. : "=r" (sh), "=&r" (sl) : "r" (ah), "%r" (al), "rI" (bl));\
  815. else \
  816. __asm__ ("add%I5c %1,%4,%5\n\tadde %0,%2,%3" \
  817. : "=r" (sh), "=&r" (sl) \
  818. : "%r" (ah), "r" (bh), "%r" (al), "rI" (bl)); \
  819. } while (0)
  820. #define sub_ddmmss(sh, sl, ah, al, bh, bl) \
  821. do { \
  822. if (__builtin_constant_p (ah) && (ah) == 0) \
  823. __asm__ ("subf%I3c %1,%4,%3\n\tsubfze %0,%2" \
  824. : "=r" (sh), "=&r" (sl) : "r" (bh), "rI" (al), "r" (bl));\
  825. else if (__builtin_constant_p (ah) && (ah) == ~(USItype) 0) \
  826. __asm__ ("subf%I3c %1,%4,%3\n\tsubfme %0,%2" \
  827. : "=r" (sh), "=&r" (sl) : "r" (bh), "rI" (al), "r" (bl));\
  828. else if (__builtin_constant_p (bh) && (bh) == 0) \
  829. __asm__ ("subf%I3c %1,%4,%3\n\taddme %0,%2" \
  830. : "=r" (sh), "=&r" (sl) : "r" (ah), "rI" (al), "r" (bl));\
  831. else if (__builtin_constant_p (bh) && (bh) == ~(USItype) 0) \
  832. __asm__ ("subf%I3c %1,%4,%3\n\taddze %0,%2" \
  833. : "=r" (sh), "=&r" (sl) : "r" (ah), "rI" (al), "r" (bl));\
  834. else \
  835. __asm__ ("subf%I4c %1,%5,%4\n\tsubfe %0,%3,%2" \
  836. : "=r" (sh), "=&r" (sl) \
  837. : "r" (ah), "r" (bh), "rI" (al), "r" (bl)); \
  838. } while (0)
  839. #define count_leading_zeros(count, x) \
  840. __asm__ ("cntlzw %0,%1" : "=r" (count) : "r" (x))
  841. #define COUNT_LEADING_ZEROS_0 32
  842. #if defined (_ARCH_PPC) || defined (__powerpc__) || defined (__POWERPC__) \
  843. || defined (__ppc__) \
  844. || (defined (PPC) && ! defined (CPU_FAMILY)) /* gcc 2.7.x GNU&SysV */ \
  845. || (defined (PPC) && defined (CPU_FAMILY) /* VxWorks */ \
  846. && CPU_FAMILY == PPC)
  847. #define umul_ppmm(ph, pl, m0, m1) \
  848. do { \
  849. USItype __m0 = (m0), __m1 = (m1); \
  850. __asm__ ("mulhwu %0,%1,%2" : "=r" (ph) : "%r" (m0), "r" (m1)); \
  851. (pl) = __m0 * __m1; \
  852. } while (0)
  853. #define UMUL_TIME 15
  854. #define smul_ppmm(ph, pl, m0, m1) \
  855. do { \
  856. SItype __m0 = (m0), __m1 = (m1); \
  857. __asm__ ("mulhw %0,%1,%2" : "=r" (ph) : "%r" (m0), "r" (m1)); \
  858. (pl) = __m0 * __m1; \
  859. } while (0)
  860. #define SMUL_TIME 14
  861. #define UDIV_TIME 120
  862. #endif
  863. #endif /* 32-bit POWER architecture variants. */
  864. /* We should test _IBMR2 here when we add assembly support for the system
  865. vendor compilers. */
  866. #if (defined (_ARCH_PPC64) || defined (__powerpc64__)) && W_TYPE_SIZE == 64
  867. #define add_ssaaaa(sh, sl, ah, al, bh, bl) \
  868. do { \
  869. if (__builtin_constant_p (bh) && (bh) == 0) \
  870. __asm__ ("add%I4c %1,%3,%4\n\taddze %0,%2" \
  871. : "=r" (sh), "=&r" (sl) : "r" (ah), "%r" (al), "rI" (bl));\
  872. else if (__builtin_constant_p (bh) && (bh) == ~(UDItype) 0) \
  873. __asm__ ("add%I4c %1,%3,%4\n\taddme %0,%2" \
  874. : "=r" (sh), "=&r" (sl) : "r" (ah), "%r" (al), "rI" (bl));\
  875. else \
  876. __asm__ ("add%I5c %1,%4,%5\n\tadde %0,%2,%3" \
  877. : "=r" (sh), "=&r" (sl) \
  878. : "%r" (ah), "r" (bh), "%r" (al), "rI" (bl)); \
  879. } while (0)
  880. #define sub_ddmmss(sh, sl, ah, al, bh, bl) \
  881. do { \
  882. if (__builtin_constant_p (ah) && (ah) == 0) \
  883. __asm__ ("subf%I3c %1,%4,%3\n\tsubfze %0,%2" \
  884. : "=r" (sh), "=&r" (sl) : "r" (bh), "rI" (al), "r" (bl));\
  885. else if (__builtin_constant_p (ah) && (ah) == ~(UDItype) 0) \
  886. __asm__ ("subf%I3c %1,%4,%3\n\tsubfme %0,%2" \
  887. : "=r" (sh), "=&r" (sl) : "r" (bh), "rI" (al), "r" (bl));\
  888. else if (__builtin_constant_p (bh) && (bh) == 0) \
  889. __asm__ ("subf%I3c %1,%4,%3\n\taddme %0,%2" \
  890. : "=r" (sh), "=&r" (sl) : "r" (ah), "rI" (al), "r" (bl));\
  891. else if (__builtin_constant_p (bh) && (bh) == ~(UDItype) 0) \
  892. __asm__ ("subf%I3c %1,%4,%3\n\taddze %0,%2" \
  893. : "=r" (sh), "=&r" (sl) : "r" (ah), "rI" (al), "r" (bl));\
  894. else \
  895. __asm__ ("subf%I4c %1,%5,%4\n\tsubfe %0,%3,%2" \
  896. : "=r" (sh), "=&r" (sl) \
  897. : "r" (ah), "r" (bh), "rI" (al), "r" (bl)); \
  898. } while (0)
  899. #define count_leading_zeros(count, x) \
  900. __asm__ ("cntlzd %0,%1" : "=r" (count) : "r" (x))
  901. #define COUNT_LEADING_ZEROS_0 64
  902. #define umul_ppmm(ph, pl, m0, m1) \
  903. do { \
  904. UDItype __m0 = (m0), __m1 = (m1); \
  905. __asm__ ("mulhdu %0,%1,%2" : "=r" (ph) : "%r" (m0), "r" (m1)); \
  906. (pl) = __m0 * __m1; \
  907. } while (0)
  908. #define UMUL_TIME 15
  909. #define smul_ppmm(ph, pl, m0, m1) \
  910. do { \
  911. DItype __m0 = (m0), __m1 = (m1); \
  912. __asm__ ("mulhd %0,%1,%2" : "=r" (ph) : "%r" (m0), "r" (m1)); \
  913. (pl) = __m0 * __m1; \
  914. } while (0)
  915. #define SMUL_TIME 14 /* ??? */
  916. #define UDIV_TIME 120 /* ??? */
  917. #endif /* 64-bit PowerPC. */
  918. #if defined (__ibm032__) /* RT/ROMP */ && W_TYPE_SIZE == 32
  919. #define add_ssaaaa(sh, sl, ah, al, bh, bl) \
  920. __asm__ ("a %1,%5\n\tae %0,%3" \
  921. : "=r" ((USItype) (sh)), \
  922. "=&r" ((USItype) (sl)) \
  923. : "%0" ((USItype) (ah)), \
  924. "r" ((USItype) (bh)), \
  925. "%1" ((USItype) (al)), \
  926. "r" ((USItype) (bl)))
  927. #define sub_ddmmss(sh, sl, ah, al, bh, bl) \
  928. __asm__ ("s %1,%5\n\tse %0,%3" \
  929. : "=r" ((USItype) (sh)), \
  930. "=&r" ((USItype) (sl)) \
  931. : "0" ((USItype) (ah)), \
  932. "r" ((USItype) (bh)), \
  933. "1" ((USItype) (al)), \
  934. "r" ((USItype) (bl)))
  935. #define umul_ppmm(ph, pl, m0, m1) \
  936. do { \
  937. USItype __m0 = (m0), __m1 = (m1); \
  938. __asm__ ( \
  939. "s r2,r2\n" \
  940. " mts r10,%2\n" \
  941. " m r2,%3\n" \
  942. " m r2,%3\n" \
  943. " m r2,%3\n" \
  944. " m r2,%3\n" \
  945. " m r2,%3\n" \
  946. " m r2,%3\n" \
  947. " m r2,%3\n" \
  948. " m r2,%3\n" \
  949. " m r2,%3\n" \
  950. " m r2,%3\n" \
  951. " m r2,%3\n" \
  952. " m r2,%3\n" \
  953. " m r2,%3\n" \
  954. " m r2,%3\n" \
  955. " m r2,%3\n" \
  956. " m r2,%3\n" \
  957. " cas %0,r2,r0\n" \
  958. " mfs r10,%1" \
  959. : "=r" ((USItype) (ph)), \
  960. "=r" ((USItype) (pl)) \
  961. : "%r" (__m0), \
  962. "r" (__m1) \
  963. : "r2"); \
  964. (ph) += ((((SItype) __m0 >> 31) & __m1) \
  965. + (((SItype) __m1 >> 31) & __m0)); \
  966. } while (0)
  967. #define UMUL_TIME 20
  968. #define UDIV_TIME 200
  969. #define count_leading_zeros(count, x) \
  970. do { \
  971. if ((x) >= 0x10000) \
  972. __asm__ ("clz %0,%1" \
  973. : "=r" ((USItype) (count)) \
  974. : "r" ((USItype) (x) >> 16)); \
  975. else \
  976. { \
  977. __asm__ ("clz %0,%1" \
  978. : "=r" ((USItype) (count)) \
  979. : "r" ((USItype) (x))); \
  980. (count) += 16; \
  981. } \
  982. } while (0)
  983. #endif
  984. #if defined(__riscv)
  985. #ifdef __riscv_mul
  986. #define __umulsidi3(u,v) ((UDWtype)(UWtype)(u) * (UWtype)(v))
  987. #define __muluw3(a, b) ((UWtype)(a) * (UWtype)(b))
  988. #else
  989. #if __riscv_xlen == 32
  990. #define MULUW3 "call __mulsi3"
  991. #elif __riscv_xlen == 64
  992. #define MULUW3 "call __muldi3"
  993. #else
  994. #error unsupport xlen
  995. #endif /* __riscv_xlen */
  996. /* We rely on the fact that MULUW3 doesn't clobber the t-registers.
  997. It can get better register allocation result. */
  998. #define __muluw3(a, b) \
  999. ({ \
  1000. register UWtype __op0 asm ("a0") = a; \
  1001. register UWtype __op1 asm ("a1") = b; \
  1002. asm volatile (MULUW3 \
  1003. : "+r" (__op0), "+r" (__op1) \
  1004. : \
  1005. : "ra", "a2", "a3"); \
  1006. __op0; \
  1007. })
  1008. #endif /* __riscv_mul */
  1009. #define umul_ppmm(w1, w0, u, v) \
  1010. do { \
  1011. UWtype __x0, __x1, __x2, __x3; \
  1012. UHWtype __ul, __vl, __uh, __vh; \
  1013. \
  1014. __ul = __ll_lowpart (u); \
  1015. __uh = __ll_highpart (u); \
  1016. __vl = __ll_lowpart (v); \
  1017. __vh = __ll_highpart (v); \
  1018. \
  1019. __x0 = __muluw3 (__ul, __vl); \
  1020. __x1 = __muluw3 (__ul, __vh); \
  1021. __x2 = __muluw3 (__uh, __vl); \
  1022. __x3 = __muluw3 (__uh, __vh); \
  1023. \
  1024. __x1 += __ll_highpart (__x0);/* this can't give carry */ \
  1025. __x1 += __x2; /* but this indeed can */ \
  1026. if (__x1 < __x2) /* did we get it? */ \
  1027. __x3 += __ll_B; /* yes, add it in the proper pos. */ \
  1028. \
  1029. (w1) = __x3 + __ll_highpart (__x1); \
  1030. (w0) = __ll_lowpart (__x1) * __ll_B + __ll_lowpart (__x0); \
  1031. } while (0)
  1032. #endif /* __riscv */
  1033. #if defined(__sh__) && W_TYPE_SIZE == 32
  1034. #ifndef __sh1__
  1035. #define umul_ppmm(w1, w0, u, v) \
  1036. __asm__ ( \
  1037. "dmulu.l %2,%3\n\tsts%M1 macl,%1\n\tsts%M0 mach,%0" \
  1038. : "=r<" ((USItype)(w1)), \
  1039. "=r<" ((USItype)(w0)) \
  1040. : "r" ((USItype)(u)), \
  1041. "r" ((USItype)(v)) \
  1042. : "macl", "mach")
  1043. #define UMUL_TIME 5
  1044. #endif
  1045. /* This is the same algorithm as __udiv_qrnnd_c. */
  1046. #define UDIV_NEEDS_NORMALIZATION 1
  1047. #ifdef __FDPIC__
  1048. /* FDPIC needs a special version of the asm fragment to extract the
  1049. code address from the function descriptor. __udiv_qrnnd_16 is
  1050. assumed to be local and not to use the GOT, so loading r12 is
  1051. not needed. */
  1052. #define udiv_qrnnd(q, r, n1, n0, d) \
  1053. do { \
  1054. extern UWtype __udiv_qrnnd_16 (UWtype, UWtype) \
  1055. __attribute__ ((visibility ("hidden"))); \
  1056. /* r0: rn r1: qn */ /* r0: n1 r4: n0 r5: d r6: d1 */ /* r2: __m */ \
  1057. __asm__ ( \
  1058. "mov%M4 %4,r5\n" \
  1059. " swap.w %3,r4\n" \
  1060. " swap.w r5,r6\n" \
  1061. " mov.l @%5,r2\n" \
  1062. " jsr @r2\n" \
  1063. " shll16 r6\n" \
  1064. " swap.w r4,r4\n" \
  1065. " mov.l @%5,r2\n" \
  1066. " jsr @r2\n" \
  1067. " swap.w r1,%0\n" \
  1068. " or r1,%0" \
  1069. : "=r" (q), "=&z" (r) \
  1070. : "1" (n1), "r" (n0), "rm" (d), "r" (&__udiv_qrnnd_16) \
  1071. : "r1", "r2", "r4", "r5", "r6", "pr", "t"); \
  1072. } while (0)
  1073. #else
  1074. #define udiv_qrnnd(q, r, n1, n0, d) \
  1075. do { \
  1076. extern UWtype __udiv_qrnnd_16 (UWtype, UWtype) \
  1077. __attribute__ ((visibility ("hidden"))); \
  1078. /* r0: rn r1: qn */ /* r0: n1 r4: n0 r5: d r6: d1 */ /* r2: __m */ \
  1079. __asm__ ( \
  1080. "mov%M4 %4,r5\n" \
  1081. " swap.w %3,r4\n" \
  1082. " swap.w r5,r6\n" \
  1083. " jsr @%5\n" \
  1084. " shll16 r6\n" \
  1085. " swap.w r4,r4\n" \
  1086. " jsr @%5\n" \
  1087. " swap.w r1,%0\n" \
  1088. " or r1,%0" \
  1089. : "=r" (q), "=&z" (r) \
  1090. : "1" (n1), "r" (n0), "rm" (d), "r" (&__udiv_qrnnd_16) \
  1091. : "r1", "r2", "r4", "r5", "r6", "pr", "t"); \
  1092. } while (0)
  1093. #endif /* __FDPIC__ */
  1094. #define UDIV_TIME 80
  1095. #define sub_ddmmss(sh, sl, ah, al, bh, bl) \
  1096. __asm__ ("clrt;subc %5,%1; subc %4,%0" \
  1097. : "=r" (sh), "=r" (sl) \
  1098. : "0" (ah), "1" (al), "r" (bh), "r" (bl) : "t")
  1099. #endif /* __sh__ */
  1100. #if defined (__sparc__) && !defined (__arch64__) && !defined (__sparcv9) \
  1101. && W_TYPE_SIZE == 32
  1102. #define add_ssaaaa(sh, sl, ah, al, bh, bl) \
  1103. __asm__ ("addcc %r4,%5,%1\n\taddx %r2,%3,%0" \
  1104. : "=r" ((USItype) (sh)), \
  1105. "=&r" ((USItype) (sl)) \
  1106. : "%rJ" ((USItype) (ah)), \
  1107. "rI" ((USItype) (bh)), \
  1108. "%rJ" ((USItype) (al)), \
  1109. "rI" ((USItype) (bl)) \
  1110. __CLOBBER_CC)
  1111. #define sub_ddmmss(sh, sl, ah, al, bh, bl) \
  1112. __asm__ ("subcc %r4,%5,%1\n\tsubx %r2,%3,%0" \
  1113. : "=r" ((USItype) (sh)), \
  1114. "=&r" ((USItype) (sl)) \
  1115. : "rJ" ((USItype) (ah)), \
  1116. "rI" ((USItype) (bh)), \
  1117. "rJ" ((USItype) (al)), \
  1118. "rI" ((USItype) (bl)) \
  1119. __CLOBBER_CC)
  1120. #if defined (__sparc_v9__)
  1121. #define umul_ppmm(w1, w0, u, v) \
  1122. do { \
  1123. register USItype __g1 asm ("g1"); \
  1124. __asm__ ("umul\t%2,%3,%1\n\t" \
  1125. "srlx\t%1, 32, %0" \
  1126. : "=r" ((USItype) (w1)), \
  1127. "=r" (__g1) \
  1128. : "r" ((USItype) (u)), \
  1129. "r" ((USItype) (v))); \
  1130. (w0) = __g1; \
  1131. } while (0)
  1132. #define udiv_qrnnd(__q, __r, __n1, __n0, __d) \
  1133. __asm__ ("mov\t%2,%%y\n\t" \
  1134. "udiv\t%3,%4,%0\n\t" \
  1135. "umul\t%0,%4,%1\n\t" \
  1136. "sub\t%3,%1,%1" \
  1137. : "=&r" ((USItype) (__q)), \
  1138. "=&r" ((USItype) (__r)) \
  1139. : "r" ((USItype) (__n1)), \
  1140. "r" ((USItype) (__n0)), \
  1141. "r" ((USItype) (__d)))
  1142. #else
  1143. #if defined (__sparc_v8__)
  1144. #define umul_ppmm(w1, w0, u, v) \
  1145. __asm__ ("umul %2,%3,%1;rd %%y,%0" \
  1146. : "=r" ((USItype) (w1)), \
  1147. "=r" ((USItype) (w0)) \
  1148. : "r" ((USItype) (u)), \
  1149. "r" ((USItype) (v)))
  1150. #define udiv_qrnnd(__q, __r, __n1, __n0, __d) \
  1151. __asm__ ("mov %2,%%y;nop;nop;nop;udiv %3,%4,%0;umul %0,%4,%1;sub %3,%1,%1"\
  1152. : "=&r" ((USItype) (__q)), \
  1153. "=&r" ((USItype) (__r)) \
  1154. : "r" ((USItype) (__n1)), \
  1155. "r" ((USItype) (__n0)), \
  1156. "r" ((USItype) (__d)))
  1157. #else
  1158. #if defined (__sparclite__)
  1159. /* This has hardware multiply but not divide. It also has two additional
  1160. instructions scan (ffs from high bit) and divscc. */
  1161. #define umul_ppmm(w1, w0, u, v) \
  1162. __asm__ ("umul %2,%3,%1;rd %%y,%0" \
  1163. : "=r" ((USItype) (w1)), \
  1164. "=r" ((USItype) (w0)) \
  1165. : "r" ((USItype) (u)), \
  1166. "r" ((USItype) (v)))
  1167. #define udiv_qrnnd(q, r, n1, n0, d) \
  1168. __asm__ ("! Inlined udiv_qrnnd\n" \
  1169. " wr %%g0,%2,%%y ! Not a delayed write for sparclite\n" \
  1170. " tst %%g0\n" \
  1171. " divscc %3,%4,%%g1\n" \
  1172. " divscc %%g1,%4,%%g1\n" \
  1173. " divscc %%g1,%4,%%g1\n" \
  1174. " divscc %%g1,%4,%%g1\n" \
  1175. " divscc %%g1,%4,%%g1\n" \
  1176. " divscc %%g1,%4,%%g1\n" \
  1177. " divscc %%g1,%4,%%g1\n" \
  1178. " divscc %%g1,%4,%%g1\n" \
  1179. " divscc %%g1,%4,%%g1\n" \
  1180. " divscc %%g1,%4,%%g1\n" \
  1181. " divscc %%g1,%4,%%g1\n" \
  1182. " divscc %%g1,%4,%%g1\n" \
  1183. " divscc %%g1,%4,%%g1\n" \
  1184. " divscc %%g1,%4,%%g1\n" \
  1185. " divscc %%g1,%4,%%g1\n" \
  1186. " divscc %%g1,%4,%%g1\n" \
  1187. " divscc %%g1,%4,%%g1\n" \
  1188. " divscc %%g1,%4,%%g1\n" \
  1189. " divscc %%g1,%4,%%g1\n" \
  1190. " divscc %%g1,%4,%%g1\n" \
  1191. " divscc %%g1,%4,%%g1\n" \
  1192. " divscc %%g1,%4,%%g1\n" \
  1193. " divscc %%g1,%4,%%g1\n" \
  1194. " divscc %%g1,%4,%%g1\n" \
  1195. " divscc %%g1,%4,%%g1\n" \
  1196. " divscc %%g1,%4,%%g1\n" \
  1197. " divscc %%g1,%4,%%g1\n" \
  1198. " divscc %%g1,%4,%%g1\n" \
  1199. " divscc %%g1,%4,%%g1\n" \
  1200. " divscc %%g1,%4,%%g1\n" \
  1201. " divscc %%g1,%4,%%g1\n" \
  1202. " divscc %%g1,%4,%0\n" \
  1203. " rd %%y,%1\n" \
  1204. " bl,a 1f\n" \
  1205. " add %1,%4,%1\n" \
  1206. "1: ! End of inline udiv_qrnnd" \
  1207. : "=r" ((USItype) (q)), \
  1208. "=r" ((USItype) (r)) \
  1209. : "r" ((USItype) (n1)), \
  1210. "r" ((USItype) (n0)), \
  1211. "rI" ((USItype) (d)) \
  1212. : "g1" __AND_CLOBBER_CC)
  1213. #define UDIV_TIME 37
  1214. #define count_leading_zeros(count, x) \
  1215. do { \
  1216. __asm__ ("scan %1,1,%0" \
  1217. : "=r" ((USItype) (count)) \
  1218. : "r" ((USItype) (x))); \
  1219. } while (0)
  1220. /* Early sparclites return 63 for an argument of 0, but they warn that future
  1221. implementations might change this. Therefore, leave COUNT_LEADING_ZEROS_0
  1222. undefined. */
  1223. #else
  1224. /* SPARC without integer multiplication and divide instructions.
  1225. (i.e. at least Sun4/20,40,60,65,75,110,260,280,330,360,380,470,490) */
  1226. #define umul_ppmm(w1, w0, u, v) \
  1227. __asm__ ("! Inlined umul_ppmm\n" \
  1228. " wr %%g0,%2,%%y ! SPARC has 0-3 delay insn after a wr\n"\
  1229. " sra %3,31,%%o5 ! Don't move this insn\n" \
  1230. " and %2,%%o5,%%o5 ! Don't move this insn\n" \
  1231. " andcc %%g0,0,%%g1 ! Don't move this insn\n" \
  1232. " mulscc %%g1,%3,%%g1\n" \
  1233. " mulscc %%g1,%3,%%g1\n" \
  1234. " mulscc %%g1,%3,%%g1\n" \
  1235. " mulscc %%g1,%3,%%g1\n" \
  1236. " mulscc %%g1,%3,%%g1\n" \
  1237. " mulscc %%g1,%3,%%g1\n" \
  1238. " mulscc %%g1,%3,%%g1\n" \
  1239. " mulscc %%g1,%3,%%g1\n" \
  1240. " mulscc %%g1,%3,%%g1\n" \
  1241. " mulscc %%g1,%3,%%g1\n" \
  1242. " mulscc %%g1,%3,%%g1\n" \
  1243. " mulscc %%g1,%3,%%g1\n" \
  1244. " mulscc %%g1,%3,%%g1\n" \
  1245. " mulscc %%g1,%3,%%g1\n" \
  1246. " mulscc %%g1,%3,%%g1\n" \
  1247. " mulscc %%g1,%3,%%g1\n" \
  1248. " mulscc %%g1,%3,%%g1\n" \
  1249. " mulscc %%g1,%3,%%g1\n" \
  1250. " mulscc %%g1,%3,%%g1\n" \
  1251. " mulscc %%g1,%3,%%g1\n" \
  1252. " mulscc %%g1,%3,%%g1\n" \
  1253. " mulscc %%g1,%3,%%g1\n" \
  1254. " mulscc %%g1,%3,%%g1\n" \
  1255. " mulscc %%g1,%3,%%g1\n" \
  1256. " mulscc %%g1,%3,%%g1\n" \
  1257. " mulscc %%g1,%3,%%g1\n" \
  1258. " mulscc %%g1,%3,%%g1\n" \
  1259. " mulscc %%g1,%3,%%g1\n" \
  1260. " mulscc %%g1,%3,%%g1\n" \
  1261. " mulscc %%g1,%3,%%g1\n" \
  1262. " mulscc %%g1,%3,%%g1\n" \
  1263. " mulscc %%g1,%3,%%g1\n" \
  1264. " mulscc %%g1,0,%%g1\n" \
  1265. " add %%g1,%%o5,%0\n" \
  1266. " rd %%y,%1" \
  1267. : "=r" ((USItype) (w1)), \
  1268. "=r" ((USItype) (w0)) \
  1269. : "%rI" ((USItype) (u)), \
  1270. "r" ((USItype) (v)) \
  1271. : "g1", "o5" __AND_CLOBBER_CC)
  1272. #define UMUL_TIME 39 /* 39 instructions */
  1273. /* It's quite necessary to add this much assembler for the sparc.
  1274. The default udiv_qrnnd (in C) is more than 10 times slower! */
  1275. #define udiv_qrnnd(__q, __r, __n1, __n0, __d) \
  1276. __asm__ ("! Inlined udiv_qrnnd\n" \
  1277. " mov 32,%%g1\n" \
  1278. " subcc %1,%2,%%g0\n" \
  1279. "1: bcs 5f\n" \
  1280. " addxcc %0,%0,%0 ! shift n1n0 and a q-bit in lsb\n" \
  1281. " sub %1,%2,%1 ! this kills msb of n\n" \
  1282. " addx %1,%1,%1 ! so this can't give carry\n" \
  1283. " subcc %%g1,1,%%g1\n" \
  1284. "2: bne 1b\n" \
  1285. " subcc %1,%2,%%g0\n" \
  1286. " bcs 3f\n" \
  1287. " addxcc %0,%0,%0 ! shift n1n0 and a q-bit in lsb\n" \
  1288. " b 3f\n" \
  1289. " sub %1,%2,%1 ! this kills msb of n\n" \
  1290. "4: sub %1,%2,%1\n" \
  1291. "5: addxcc %1,%1,%1\n" \
  1292. " bcc 2b\n" \
  1293. " subcc %%g1,1,%%g1\n" \
  1294. "! Got carry from n. Subtract next step to cancel this carry.\n" \
  1295. " bne 4b\n" \
  1296. " addcc %0,%0,%0 ! shift n1n0 and a 0-bit in lsb\n" \
  1297. " sub %1,%2,%1\n" \
  1298. "3: xnor %0,0,%0\n" \
  1299. " ! End of inline udiv_qrnnd" \
  1300. : "=&r" ((USItype) (__q)), \
  1301. "=&r" ((USItype) (__r)) \
  1302. : "r" ((USItype) (__d)), \
  1303. "1" ((USItype) (__n1)), \
  1304. "0" ((USItype) (__n0)) : "g1" __AND_CLOBBER_CC)
  1305. #define UDIV_TIME (3+7*32) /* 7 instructions/iteration. 32 iterations. */
  1306. #endif /* __sparclite__ */
  1307. #endif /* __sparc_v8__ */
  1308. #endif /* __sparc_v9__ */
  1309. #endif /* sparc32 */
  1310. #if ((defined (__sparc__) && defined (__arch64__)) || defined (__sparcv9)) \
  1311. && W_TYPE_SIZE == 64
  1312. #define add_ssaaaa(sh, sl, ah, al, bh, bl) \
  1313. do { \
  1314. UDItype __carry = 0; \
  1315. __asm__ ("addcc\t%r5,%6,%1\n\t" \
  1316. "add\t%r3,%4,%0\n\t" \
  1317. "movcs\t%%xcc, 1, %2\n\t" \
  1318. "add\t%0, %2, %0" \
  1319. : "=r" ((UDItype)(sh)), \
  1320. "=&r" ((UDItype)(sl)), \
  1321. "+r" (__carry) \
  1322. : "%rJ" ((UDItype)(ah)), \
  1323. "rI" ((UDItype)(bh)), \
  1324. "%rJ" ((UDItype)(al)), \
  1325. "rI" ((UDItype)(bl)) \
  1326. __CLOBBER_CC); \
  1327. } while (0)
  1328. #define sub_ddmmss(sh, sl, ah, al, bh, bl) \
  1329. do { \
  1330. UDItype __carry = 0; \
  1331. __asm__ ("subcc\t%r5,%6,%1\n\t" \
  1332. "sub\t%r3,%4,%0\n\t" \
  1333. "movcs\t%%xcc, 1, %2\n\t" \
  1334. "sub\t%0, %2, %0" \
  1335. : "=r" ((UDItype)(sh)), \
  1336. "=&r" ((UDItype)(sl)), \
  1337. "+r" (__carry) \
  1338. : "%rJ" ((UDItype)(ah)), \
  1339. "rI" ((UDItype)(bh)), \
  1340. "%rJ" ((UDItype)(al)), \
  1341. "rI" ((UDItype)(bl)) \
  1342. __CLOBBER_CC); \
  1343. } while (0)
  1344. #define umul_ppmm(wh, wl, u, v) \
  1345. do { \
  1346. UDItype tmp1, tmp2, tmp3, tmp4; \
  1347. __asm__ __volatile__ ( \
  1348. "srl %7,0,%3\n\t" \
  1349. "mulx %3,%6,%1\n\t" \
  1350. "srlx %6,32,%2\n\t" \
  1351. "mulx %2,%3,%4\n\t" \
  1352. "sllx %4,32,%5\n\t" \
  1353. "srl %6,0,%3\n\t" \
  1354. "sub %1,%5,%5\n\t" \
  1355. "srlx %5,32,%5\n\t" \
  1356. "addcc %4,%5,%4\n\t" \
  1357. "srlx %7,32,%5\n\t" \
  1358. "mulx %3,%5,%3\n\t" \
  1359. "mulx %2,%5,%5\n\t" \
  1360. "sethi %%hi(0x80000000),%2\n\t" \
  1361. "addcc %4,%3,%4\n\t" \
  1362. "srlx %4,32,%4\n\t" \
  1363. "add %2,%2,%2\n\t" \
  1364. "movcc %%xcc,%%g0,%2\n\t" \
  1365. "addcc %5,%4,%5\n\t" \
  1366. "sllx %3,32,%3\n\t" \
  1367. "add %1,%3,%1\n\t" \
  1368. "add %5,%2,%0" \
  1369. : "=r" ((UDItype)(wh)), \
  1370. "=&r" ((UDItype)(wl)), \
  1371. "=&r" (tmp1), "=&r" (tmp2), "=&r" (tmp3), "=&r" (tmp4) \
  1372. : "r" ((UDItype)(u)), \
  1373. "r" ((UDItype)(v)) \
  1374. __CLOBBER_CC); \
  1375. } while (0)
  1376. #define UMUL_TIME 96
  1377. #define UDIV_TIME 230
  1378. #endif /* sparc64 */
  1379. #if defined (__vax__) && W_TYPE_SIZE == 32
  1380. #define add_ssaaaa(sh, sl, ah, al, bh, bl) \
  1381. __asm__ ("addl2 %5,%1\n\tadwc %3,%0" \
  1382. : "=g" ((USItype) (sh)), \
  1383. "=&g" ((USItype) (sl)) \
  1384. : "%0" ((USItype) (ah)), \
  1385. "g" ((USItype) (bh)), \
  1386. "%1" ((USItype) (al)), \
  1387. "g" ((USItype) (bl)))
  1388. #define sub_ddmmss(sh, sl, ah, al, bh, bl) \
  1389. __asm__ ("subl2 %5,%1\n\tsbwc %3,%0" \
  1390. : "=g" ((USItype) (sh)), \
  1391. "=&g" ((USItype) (sl)) \
  1392. : "0" ((USItype) (ah)), \
  1393. "g" ((USItype) (bh)), \
  1394. "1" ((USItype) (al)), \
  1395. "g" ((USItype) (bl)))
  1396. #define umul_ppmm(xh, xl, m0, m1) \
  1397. do { \
  1398. union { \
  1399. UDItype __ll; \
  1400. struct {USItype __l, __h;} __i; \
  1401. } __xx; \
  1402. USItype __m0 = (m0), __m1 = (m1); \
  1403. __asm__ ("emul %1,%2,$0,%0" \
  1404. : "=r" (__xx.__ll) \
  1405. : "g" (__m0), \
  1406. "g" (__m1)); \
  1407. (xh) = __xx.__i.__h; \
  1408. (xl) = __xx.__i.__l; \
  1409. (xh) += ((((SItype) __m0 >> 31) & __m1) \
  1410. + (((SItype) __m1 >> 31) & __m0)); \
  1411. } while (0)
  1412. #define sdiv_qrnnd(q, r, n1, n0, d) \
  1413. do { \
  1414. union {DItype __ll; \
  1415. struct {SItype __l, __h;} __i; \
  1416. } __xx; \
  1417. __xx.__i.__h = n1; __xx.__i.__l = n0; \
  1418. __asm__ ("ediv %3,%2,%0,%1" \
  1419. : "=g" (q), "=g" (r) \
  1420. : "g" (__xx.__ll), "g" (d)); \
  1421. } while (0)
  1422. #endif /* __vax__ */
  1423. #ifdef _TMS320C6X
  1424. #define add_ssaaaa(sh, sl, ah, al, bh, bl) \
  1425. do \
  1426. { \
  1427. UDItype __ll; \
  1428. __asm__ ("addu .l1 %1, %2, %0" \
  1429. : "=a" (__ll) : "a" (al), "a" (bl)); \
  1430. (sl) = (USItype)__ll; \
  1431. (sh) = ((USItype)(__ll >> 32)) + (ah) + (bh); \
  1432. } \
  1433. while (0)
  1434. #ifdef _TMS320C6400_PLUS
  1435. #define __umulsidi3(u,v) ((UDItype)(USItype)u*(USItype)v)
  1436. #define umul_ppmm(w1, w0, u, v) \
  1437. do { \
  1438. UDItype __x = (UDItype) (USItype) (u) * (USItype) (v); \
  1439. (w1) = (USItype) (__x >> 32); \
  1440. (w0) = (USItype) (__x); \
  1441. } while (0)
  1442. #endif /* _TMS320C6400_PLUS */
  1443. #define count_leading_zeros(count, x) ((count) = __builtin_clz (x))
  1444. #ifdef _TMS320C6400
  1445. #define count_trailing_zeros(count, x) ((count) = __builtin_ctz (x))
  1446. #endif
  1447. #define UMUL_TIME 4
  1448. #define UDIV_TIME 40
  1449. #endif /* _TMS320C6X */
  1450. #if defined (__xtensa__) && W_TYPE_SIZE == 32
  1451. /* This code is not Xtensa-configuration-specific, so rely on the compiler
  1452. to expand builtin functions depending on what configuration features
  1453. are available. This avoids library calls when the operation can be
  1454. performed in-line. */
  1455. #define umul_ppmm(w1, w0, u, v) \
  1456. do { \
  1457. DWunion __w; \
  1458. __w.ll = __builtin_umulsidi3 (u, v); \
  1459. w1 = __w.s.high; \
  1460. w0 = __w.s.low; \
  1461. } while (0)
  1462. #define __umulsidi3(u, v) __builtin_umulsidi3 (u, v)
  1463. #define count_leading_zeros(COUNT, X) ((COUNT) = __builtin_clz (X))
  1464. #define count_trailing_zeros(COUNT, X) ((COUNT) = __builtin_ctz (X))
  1465. #endif /* __xtensa__ */
  1466. #if defined xstormy16
  1467. extern UHItype __stormy16_count_leading_zeros (UHItype);
  1468. #define count_leading_zeros(count, x) \
  1469. do \
  1470. { \
  1471. UHItype size; \
  1472. \
  1473. /* We assume that W_TYPE_SIZE is a multiple of 16... */ \
  1474. for ((count) = 0, size = W_TYPE_SIZE; size; size -= 16) \
  1475. { \
  1476. UHItype c; \
  1477. \
  1478. c = __clzhi2 ((x) >> (size - 16)); \
  1479. (count) += c; \
  1480. if (c != 16) \
  1481. break; \
  1482. } \
  1483. } \
  1484. while (0)
  1485. #define COUNT_LEADING_ZEROS_0 W_TYPE_SIZE
  1486. #endif
  1487. #if defined (__z8000__) && W_TYPE_SIZE == 16
  1488. #define add_ssaaaa(sh, sl, ah, al, bh, bl) \
  1489. __asm__ ("add %H1,%H5\n\tadc %H0,%H3" \
  1490. : "=r" ((unsigned int)(sh)), \
  1491. "=&r" ((unsigned int)(sl)) \
  1492. : "%0" ((unsigned int)(ah)), \
  1493. "r" ((unsigned int)(bh)), \
  1494. "%1" ((unsigned int)(al)), \
  1495. "rQR" ((unsigned int)(bl)))
  1496. #define sub_ddmmss(sh, sl, ah, al, bh, bl) \
  1497. __asm__ ("sub %H1,%H5\n\tsbc %H0,%H3" \
  1498. : "=r" ((unsigned int)(sh)), \
  1499. "=&r" ((unsigned int)(sl)) \
  1500. : "0" ((unsigned int)(ah)), \
  1501. "r" ((unsigned int)(bh)), \
  1502. "1" ((unsigned int)(al)), \
  1503. "rQR" ((unsigned int)(bl)))
  1504. #define umul_ppmm(xh, xl, m0, m1) \
  1505. do { \
  1506. union {long int __ll; \
  1507. struct {unsigned int __h, __l;} __i; \
  1508. } __xx; \
  1509. unsigned int __m0 = (m0), __m1 = (m1); \
  1510. __asm__ ("mult %S0,%H3" \
  1511. : "=r" (__xx.__i.__h), \
  1512. "=r" (__xx.__i.__l) \
  1513. : "%1" (__m0), \
  1514. "rQR" (__m1)); \
  1515. (xh) = __xx.__i.__h; (xl) = __xx.__i.__l; \
  1516. (xh) += ((((signed int) __m0 >> 15) & __m1) \
  1517. + (((signed int) __m1 >> 15) & __m0)); \
  1518. } while (0)
  1519. #endif /* __z8000__ */
  1520. #endif /* __GNUC__ */
  1521. /* If this machine has no inline assembler, use C macros. */
  1522. #if !defined (add_ssaaaa)
  1523. #define add_ssaaaa(sh, sl, ah, al, bh, bl) \
  1524. do { \
  1525. UWtype __x; \
  1526. __x = (al) + (bl); \
  1527. (sh) = (ah) + (bh) + (__x < (al)); \
  1528. (sl) = __x; \
  1529. } while (0)
  1530. #endif
  1531. #if !defined (sub_ddmmss)
  1532. #define sub_ddmmss(sh, sl, ah, al, bh, bl) \
  1533. do { \
  1534. UWtype __x; \
  1535. __x = (al) - (bl); \
  1536. (sh) = (ah) - (bh) - (__x > (al)); \
  1537. (sl) = __x; \
  1538. } while (0)
  1539. #endif
  1540. /* If we lack umul_ppmm but have smul_ppmm, define umul_ppmm in terms of
  1541. smul_ppmm. */
  1542. #if !defined (umul_ppmm) && defined (smul_ppmm)
  1543. #define umul_ppmm(w1, w0, u, v) \
  1544. do { \
  1545. UWtype __w1; \
  1546. UWtype __xm0 = (u), __xm1 = (v); \
  1547. smul_ppmm (__w1, w0, __xm0, __xm1); \
  1548. (w1) = __w1 + (-(__xm0 >> (W_TYPE_SIZE - 1)) & __xm1) \
  1549. + (-(__xm1 >> (W_TYPE_SIZE - 1)) & __xm0); \
  1550. } while (0)
  1551. #endif
  1552. /* If we still don't have umul_ppmm, define it using plain C. */
  1553. #if !defined (umul_ppmm)
  1554. #define umul_ppmm(w1, w0, u, v) \
  1555. do { \
  1556. UWtype __x0, __x1, __x2, __x3; \
  1557. UHWtype __ul, __vl, __uh, __vh; \
  1558. \
  1559. __ul = __ll_lowpart (u); \
  1560. __uh = __ll_highpart (u); \
  1561. __vl = __ll_lowpart (v); \
  1562. __vh = __ll_highpart (v); \
  1563. \
  1564. __x0 = (UWtype) __ul * __vl; \
  1565. __x1 = (UWtype) __ul * __vh; \
  1566. __x2 = (UWtype) __uh * __vl; \
  1567. __x3 = (UWtype) __uh * __vh; \
  1568. \
  1569. __x1 += __ll_highpart (__x0);/* this can't give carry */ \
  1570. __x1 += __x2; /* but this indeed can */ \
  1571. if (__x1 < __x2) /* did we get it? */ \
  1572. __x3 += __ll_B; /* yes, add it in the proper pos. */ \
  1573. \
  1574. (w1) = __x3 + __ll_highpart (__x1); \
  1575. (w0) = __ll_lowpart (__x1) * __ll_B + __ll_lowpart (__x0); \
  1576. } while (0)
  1577. #endif
  1578. #if !defined (__umulsidi3)
  1579. #define __umulsidi3(u, v) \
  1580. ({DWunion __w; \
  1581. umul_ppmm (__w.s.high, __w.s.low, u, v); \
  1582. __w.ll; })
  1583. #endif
  1584. /* Define this unconditionally, so it can be used for debugging. */
  1585. #define __udiv_qrnnd_c(q, r, n1, n0, d) \
  1586. do { \
  1587. UWtype __d1, __d0, __q1, __q0; \
  1588. UWtype __r1, __r0, __m; \
  1589. __d1 = __ll_highpart (d); \
  1590. __d0 = __ll_lowpart (d); \
  1591. \
  1592. __r1 = (n1) % __d1; \
  1593. __q1 = (n1) / __d1; \
  1594. __m = (UWtype) __q1 * __d0; \
  1595. __r1 = __r1 * __ll_B | __ll_highpart (n0); \
  1596. if (__r1 < __m) \
  1597. { \
  1598. __q1--, __r1 += (d); \
  1599. if (__r1 >= (d)) /* i.e. we didn't get carry when adding to __r1 */\
  1600. if (__r1 < __m) \
  1601. __q1--, __r1 += (d); \
  1602. } \
  1603. __r1 -= __m; \
  1604. \
  1605. __r0 = __r1 % __d1; \
  1606. __q0 = __r1 / __d1; \
  1607. __m = (UWtype) __q0 * __d0; \
  1608. __r0 = __r0 * __ll_B | __ll_lowpart (n0); \
  1609. if (__r0 < __m) \
  1610. { \
  1611. __q0--, __r0 += (d); \
  1612. if (__r0 >= (d)) \
  1613. if (__r0 < __m) \
  1614. __q0--, __r0 += (d); \
  1615. } \
  1616. __r0 -= __m; \
  1617. \
  1618. (q) = (UWtype) __q1 * __ll_B | __q0; \
  1619. (r) = __r0; \
  1620. } while (0)
  1621. /* If the processor has no udiv_qrnnd but sdiv_qrnnd, go through
  1622. __udiv_w_sdiv (defined in libgcc or elsewhere). */
  1623. #if !defined (udiv_qrnnd) && defined (sdiv_qrnnd)
  1624. #define udiv_qrnnd(q, r, nh, nl, d) \
  1625. do { \
  1626. extern UWtype __udiv_w_sdiv (UWtype *, UWtype, UWtype, UWtype); \
  1627. UWtype __r; \
  1628. (q) = __udiv_w_sdiv (&__r, nh, nl, d); \
  1629. (r) = __r; \
  1630. } while (0)
  1631. #endif
  1632. /* If udiv_qrnnd was not defined for this processor, use __udiv_qrnnd_c. */
  1633. #if !defined (udiv_qrnnd)
  1634. #define UDIV_NEEDS_NORMALIZATION 1
  1635. #define udiv_qrnnd __udiv_qrnnd_c
  1636. #endif
  1637. #if !defined (count_leading_zeros)
  1638. #define count_leading_zeros(count, x) \
  1639. do { \
  1640. UWtype __xr = (x); \
  1641. UWtype __a; \
  1642. \
  1643. if (W_TYPE_SIZE <= 32) \
  1644. { \
  1645. __a = __xr < ((UWtype)1<<2*__BITS4) \
  1646. ? (__xr < ((UWtype)1<<__BITS4) ? 0 : __BITS4) \
  1647. : (__xr < ((UWtype)1<<3*__BITS4) ? 2*__BITS4 : 3*__BITS4); \
  1648. } \
  1649. else \
  1650. { \
  1651. for (__a = W_TYPE_SIZE - 8; __a > 0; __a -= 8) \
  1652. if (((__xr >> __a) & 0xff) != 0) \
  1653. break; \
  1654. } \
  1655. \
  1656. (count) = W_TYPE_SIZE - (__clz_tab[__xr >> __a] + __a); \
  1657. } while (0)
  1658. #define COUNT_LEADING_ZEROS_0 W_TYPE_SIZE
  1659. #endif
  1660. #if !defined (count_trailing_zeros)
  1661. /* Define count_trailing_zeros using count_leading_zeros. The latter might be
  1662. defined in asm, but if it is not, the C version above is good enough. */
  1663. #define count_trailing_zeros(count, x) \
  1664. do { \
  1665. UWtype __ctz_x = (x); \
  1666. UWtype __ctz_c; \
  1667. count_leading_zeros (__ctz_c, __ctz_x & -__ctz_x); \
  1668. (count) = W_TYPE_SIZE - 1 - __ctz_c; \
  1669. } while (0)
  1670. #endif
  1671. #ifndef UDIV_NEEDS_NORMALIZATION
  1672. #define UDIV_NEEDS_NORMALIZATION 0
  1673. #endif