double-int.c 40 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582
  1. /* Operations with long integers.
  2. Copyright (C) 2006-2015 Free Software Foundation, Inc.
  3. This file is part of GCC.
  4. GCC is free software; you can redistribute it and/or modify it
  5. under the terms of the GNU General Public License as published by the
  6. Free Software Foundation; either version 3, or (at your option) any
  7. later version.
  8. GCC is distributed in the hope that it will be useful, but WITHOUT
  9. ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  10. FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  11. for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GCC; see the file COPYING3. If not see
  14. <http://www.gnu.org/licenses/>. */
  15. #include "config.h"
  16. #include "system.h"
  17. #include "coretypes.h"
  18. #include "tm.h" /* For BITS_PER_UNIT and *_BIG_ENDIAN. */
  19. #include "hash-set.h"
  20. #include "machmode.h"
  21. #include "vec.h"
  22. #include "double-int.h"
  23. #include "input.h"
  24. #include "alias.h"
  25. #include "symtab.h"
  26. #include "wide-int.h"
  27. #include "inchash.h"
  28. #include "real.h"
  29. #include "tree.h"
  30. static int add_double_with_sign (unsigned HOST_WIDE_INT, HOST_WIDE_INT,
  31. unsigned HOST_WIDE_INT, HOST_WIDE_INT,
  32. unsigned HOST_WIDE_INT *, HOST_WIDE_INT *,
  33. bool);
  34. #define add_double(l1,h1,l2,h2,lv,hv) \
  35. add_double_with_sign (l1, h1, l2, h2, lv, hv, false)
  36. static int neg_double (unsigned HOST_WIDE_INT, HOST_WIDE_INT,
  37. unsigned HOST_WIDE_INT *, HOST_WIDE_INT *);
  38. static int mul_double_wide_with_sign (unsigned HOST_WIDE_INT, HOST_WIDE_INT,
  39. unsigned HOST_WIDE_INT, HOST_WIDE_INT,
  40. unsigned HOST_WIDE_INT *, HOST_WIDE_INT *,
  41. unsigned HOST_WIDE_INT *, HOST_WIDE_INT *,
  42. bool);
  43. #define mul_double(l1,h1,l2,h2,lv,hv) \
  44. mul_double_wide_with_sign (l1, h1, l2, h2, lv, hv, NULL, NULL, false)
  45. static int div_and_round_double (unsigned, int, unsigned HOST_WIDE_INT,
  46. HOST_WIDE_INT, unsigned HOST_WIDE_INT,
  47. HOST_WIDE_INT, unsigned HOST_WIDE_INT *,
  48. HOST_WIDE_INT *, unsigned HOST_WIDE_INT *,
  49. HOST_WIDE_INT *);
  50. /* We know that A1 + B1 = SUM1, using 2's complement arithmetic and ignoring
  51. overflow. Suppose A, B and SUM have the same respective signs as A1, B1,
  52. and SUM1. Then this yields nonzero if overflow occurred during the
  53. addition.
  54. Overflow occurs if A and B have the same sign, but A and SUM differ in
  55. sign. Use `^' to test whether signs differ, and `< 0' to isolate the
  56. sign. */
  57. #define OVERFLOW_SUM_SIGN(a, b, sum) ((~((a) ^ (b)) & ((a) ^ (sum))) < 0)
  58. /* To do constant folding on INTEGER_CST nodes requires two-word arithmetic.
  59. We do that by representing the two-word integer in 4 words, with only
  60. HOST_BITS_PER_WIDE_INT / 2 bits stored in each word, as a positive
  61. number. The value of the word is LOWPART + HIGHPART * BASE. */
  62. #define LOWPART(x) \
  63. ((x) & (((unsigned HOST_WIDE_INT) 1 << (HOST_BITS_PER_WIDE_INT / 2)) - 1))
  64. #define HIGHPART(x) \
  65. ((unsigned HOST_WIDE_INT) (x) >> HOST_BITS_PER_WIDE_INT / 2)
  66. #define BASE ((unsigned HOST_WIDE_INT) 1 << HOST_BITS_PER_WIDE_INT / 2)
  67. /* Unpack a two-word integer into 4 words.
  68. LOW and HI are the integer, as two `HOST_WIDE_INT' pieces.
  69. WORDS points to the array of HOST_WIDE_INTs. */
  70. static void
  71. encode (HOST_WIDE_INT *words, unsigned HOST_WIDE_INT low, HOST_WIDE_INT hi)
  72. {
  73. words[0] = LOWPART (low);
  74. words[1] = HIGHPART (low);
  75. words[2] = LOWPART (hi);
  76. words[3] = HIGHPART (hi);
  77. }
  78. /* Pack an array of 4 words into a two-word integer.
  79. WORDS points to the array of words.
  80. The integer is stored into *LOW and *HI as two `HOST_WIDE_INT' pieces. */
  81. static void
  82. decode (HOST_WIDE_INT *words, unsigned HOST_WIDE_INT *low,
  83. HOST_WIDE_INT *hi)
  84. {
  85. *low = words[0] + words[1] * BASE;
  86. *hi = words[2] + words[3] * BASE;
  87. }
  88. /* Add two doubleword integers with doubleword result.
  89. Return nonzero if the operation overflows according to UNSIGNED_P.
  90. Each argument is given as two `HOST_WIDE_INT' pieces.
  91. One argument is L1 and H1; the other, L2 and H2.
  92. The value is stored as two `HOST_WIDE_INT' pieces in *LV and *HV. */
  93. static int
  94. add_double_with_sign (unsigned HOST_WIDE_INT l1, HOST_WIDE_INT h1,
  95. unsigned HOST_WIDE_INT l2, HOST_WIDE_INT h2,
  96. unsigned HOST_WIDE_INT *lv, HOST_WIDE_INT *hv,
  97. bool unsigned_p)
  98. {
  99. unsigned HOST_WIDE_INT l;
  100. HOST_WIDE_INT h;
  101. l = l1 + l2;
  102. h = (HOST_WIDE_INT) ((unsigned HOST_WIDE_INT) h1
  103. + (unsigned HOST_WIDE_INT) h2
  104. + (l < l1));
  105. *lv = l;
  106. *hv = h;
  107. if (unsigned_p)
  108. return ((unsigned HOST_WIDE_INT) h < (unsigned HOST_WIDE_INT) h1
  109. || (h == h1
  110. && l < l1));
  111. else
  112. return OVERFLOW_SUM_SIGN (h1, h2, h);
  113. }
  114. /* Negate a doubleword integer with doubleword result.
  115. Return nonzero if the operation overflows, assuming it's signed.
  116. The argument is given as two `HOST_WIDE_INT' pieces in L1 and H1.
  117. The value is stored as two `HOST_WIDE_INT' pieces in *LV and *HV. */
  118. static int
  119. neg_double (unsigned HOST_WIDE_INT l1, HOST_WIDE_INT h1,
  120. unsigned HOST_WIDE_INT *lv, HOST_WIDE_INT *hv)
  121. {
  122. if (l1 == 0)
  123. {
  124. *lv = 0;
  125. *hv = - (unsigned HOST_WIDE_INT) h1;
  126. return (*hv & h1) < 0;
  127. }
  128. else
  129. {
  130. *lv = -l1;
  131. *hv = ~h1;
  132. return 0;
  133. }
  134. }
  135. /* Multiply two doubleword integers with quadword result.
  136. Return nonzero if the operation overflows according to UNSIGNED_P.
  137. Each argument is given as two `HOST_WIDE_INT' pieces.
  138. One argument is L1 and H1; the other, L2 and H2.
  139. The value is stored as four `HOST_WIDE_INT' pieces in *LV and *HV,
  140. *LW and *HW.
  141. If lw is NULL then only the low part and no overflow is computed. */
  142. static int
  143. mul_double_wide_with_sign (unsigned HOST_WIDE_INT l1, HOST_WIDE_INT h1,
  144. unsigned HOST_WIDE_INT l2, HOST_WIDE_INT h2,
  145. unsigned HOST_WIDE_INT *lv, HOST_WIDE_INT *hv,
  146. unsigned HOST_WIDE_INT *lw, HOST_WIDE_INT *hw,
  147. bool unsigned_p)
  148. {
  149. HOST_WIDE_INT arg1[4];
  150. HOST_WIDE_INT arg2[4];
  151. HOST_WIDE_INT prod[4 * 2];
  152. unsigned HOST_WIDE_INT carry;
  153. int i, j, k;
  154. unsigned HOST_WIDE_INT neglow;
  155. HOST_WIDE_INT neghigh;
  156. encode (arg1, l1, h1);
  157. encode (arg2, l2, h2);
  158. memset (prod, 0, sizeof prod);
  159. for (i = 0; i < 4; i++)
  160. {
  161. carry = 0;
  162. for (j = 0; j < 4; j++)
  163. {
  164. k = i + j;
  165. /* This product is <= 0xFFFE0001, the sum <= 0xFFFF0000. */
  166. carry += (unsigned HOST_WIDE_INT) arg1[i] * arg2[j];
  167. /* Since prod[p] < 0xFFFF, this sum <= 0xFFFFFFFF. */
  168. carry += prod[k];
  169. prod[k] = LOWPART (carry);
  170. carry = HIGHPART (carry);
  171. }
  172. prod[i + 4] = carry;
  173. }
  174. decode (prod, lv, hv);
  175. /* We are not interested in the wide part nor in overflow. */
  176. if (lw == NULL)
  177. return 0;
  178. decode (prod + 4, lw, hw);
  179. /* Unsigned overflow is immediate. */
  180. if (unsigned_p)
  181. return (*lw | *hw) != 0;
  182. /* Check for signed overflow by calculating the signed representation of the
  183. top half of the result; it should agree with the low half's sign bit. */
  184. if (h1 < 0)
  185. {
  186. neg_double (l2, h2, &neglow, &neghigh);
  187. add_double (neglow, neghigh, *lw, *hw, lw, hw);
  188. }
  189. if (h2 < 0)
  190. {
  191. neg_double (l1, h1, &neglow, &neghigh);
  192. add_double (neglow, neghigh, *lw, *hw, lw, hw);
  193. }
  194. return (*hv < 0 ? ~(*lw & *hw) : *lw | *hw) != 0;
  195. }
  196. /* Shift the doubleword integer in L1, H1 right by COUNT places
  197. keeping only PREC bits of result. ARITH nonzero specifies
  198. arithmetic shifting; otherwise use logical shift.
  199. Store the value as two `HOST_WIDE_INT' pieces in *LV and *HV. */
  200. static void
  201. rshift_double (unsigned HOST_WIDE_INT l1, HOST_WIDE_INT h1,
  202. unsigned HOST_WIDE_INT count, unsigned int prec,
  203. unsigned HOST_WIDE_INT *lv, HOST_WIDE_INT *hv,
  204. bool arith)
  205. {
  206. unsigned HOST_WIDE_INT signmask;
  207. signmask = (arith
  208. ? -((unsigned HOST_WIDE_INT) h1 >> (HOST_BITS_PER_WIDE_INT - 1))
  209. : 0);
  210. if (count >= HOST_BITS_PER_DOUBLE_INT)
  211. {
  212. /* Shifting by the host word size is undefined according to the
  213. ANSI standard, so we must handle this as a special case. */
  214. *hv = 0;
  215. *lv = 0;
  216. }
  217. else if (count >= HOST_BITS_PER_WIDE_INT)
  218. {
  219. *hv = 0;
  220. *lv = (unsigned HOST_WIDE_INT) h1 >> (count - HOST_BITS_PER_WIDE_INT);
  221. }
  222. else
  223. {
  224. *hv = (unsigned HOST_WIDE_INT) h1 >> count;
  225. *lv = ((l1 >> count)
  226. | ((unsigned HOST_WIDE_INT) h1
  227. << (HOST_BITS_PER_WIDE_INT - count - 1) << 1));
  228. }
  229. /* Zero / sign extend all bits that are beyond the precision. */
  230. if (count >= prec)
  231. {
  232. *hv = signmask;
  233. *lv = signmask;
  234. }
  235. else if ((prec - count) >= HOST_BITS_PER_DOUBLE_INT)
  236. ;
  237. else if ((prec - count) >= HOST_BITS_PER_WIDE_INT)
  238. {
  239. *hv &= ~(HOST_WIDE_INT_M1U << (prec - count - HOST_BITS_PER_WIDE_INT));
  240. *hv |= signmask << (prec - count - HOST_BITS_PER_WIDE_INT);
  241. }
  242. else
  243. {
  244. *hv = signmask;
  245. *lv &= ~(HOST_WIDE_INT_M1U << (prec - count));
  246. *lv |= signmask << (prec - count);
  247. }
  248. }
  249. /* Shift the doubleword integer in L1, H1 left by COUNT places
  250. keeping only PREC bits of result.
  251. Shift right if COUNT is negative.
  252. ARITH nonzero specifies arithmetic shifting; otherwise use logical shift.
  253. Store the value as two `HOST_WIDE_INT' pieces in *LV and *HV. */
  254. static void
  255. lshift_double (unsigned HOST_WIDE_INT l1, HOST_WIDE_INT h1,
  256. unsigned HOST_WIDE_INT count, unsigned int prec,
  257. unsigned HOST_WIDE_INT *lv, HOST_WIDE_INT *hv)
  258. {
  259. unsigned HOST_WIDE_INT signmask;
  260. if (count >= HOST_BITS_PER_DOUBLE_INT)
  261. {
  262. /* Shifting by the host word size is undefined according to the
  263. ANSI standard, so we must handle this as a special case. */
  264. *hv = 0;
  265. *lv = 0;
  266. }
  267. else if (count >= HOST_BITS_PER_WIDE_INT)
  268. {
  269. *hv = l1 << (count - HOST_BITS_PER_WIDE_INT);
  270. *lv = 0;
  271. }
  272. else
  273. {
  274. *hv = (((unsigned HOST_WIDE_INT) h1 << count)
  275. | (l1 >> (HOST_BITS_PER_WIDE_INT - count - 1) >> 1));
  276. *lv = l1 << count;
  277. }
  278. /* Sign extend all bits that are beyond the precision. */
  279. signmask = -((prec > HOST_BITS_PER_WIDE_INT
  280. ? ((unsigned HOST_WIDE_INT) *hv
  281. >> (prec - HOST_BITS_PER_WIDE_INT - 1))
  282. : (*lv >> (prec - 1))) & 1);
  283. if (prec >= HOST_BITS_PER_DOUBLE_INT)
  284. ;
  285. else if (prec >= HOST_BITS_PER_WIDE_INT)
  286. {
  287. *hv &= ~(HOST_WIDE_INT_M1U << (prec - HOST_BITS_PER_WIDE_INT));
  288. *hv |= signmask << (prec - HOST_BITS_PER_WIDE_INT);
  289. }
  290. else
  291. {
  292. *hv = signmask;
  293. *lv &= ~(HOST_WIDE_INT_M1U << prec);
  294. *lv |= signmask << prec;
  295. }
  296. }
  297. /* Divide doubleword integer LNUM, HNUM by doubleword integer LDEN, HDEN
  298. for a quotient (stored in *LQUO, *HQUO) and remainder (in *LREM, *HREM).
  299. CODE is a tree code for a kind of division, one of
  300. TRUNC_DIV_EXPR, FLOOR_DIV_EXPR, CEIL_DIV_EXPR, ROUND_DIV_EXPR
  301. or EXACT_DIV_EXPR
  302. It controls how the quotient is rounded to an integer.
  303. Return nonzero if the operation overflows.
  304. UNS nonzero says do unsigned division. */
  305. static int
  306. div_and_round_double (unsigned code, int uns,
  307. /* num == numerator == dividend */
  308. unsigned HOST_WIDE_INT lnum_orig,
  309. HOST_WIDE_INT hnum_orig,
  310. /* den == denominator == divisor */
  311. unsigned HOST_WIDE_INT lden_orig,
  312. HOST_WIDE_INT hden_orig,
  313. unsigned HOST_WIDE_INT *lquo,
  314. HOST_WIDE_INT *hquo, unsigned HOST_WIDE_INT *lrem,
  315. HOST_WIDE_INT *hrem)
  316. {
  317. int quo_neg = 0;
  318. HOST_WIDE_INT num[4 + 1]; /* extra element for scaling. */
  319. HOST_WIDE_INT den[4], quo[4];
  320. int i, j;
  321. unsigned HOST_WIDE_INT work;
  322. unsigned HOST_WIDE_INT carry = 0;
  323. unsigned HOST_WIDE_INT lnum = lnum_orig;
  324. HOST_WIDE_INT hnum = hnum_orig;
  325. unsigned HOST_WIDE_INT lden = lden_orig;
  326. HOST_WIDE_INT hden = hden_orig;
  327. int overflow = 0;
  328. if (hden == 0 && lden == 0)
  329. overflow = 1, lden = 1;
  330. /* Calculate quotient sign and convert operands to unsigned. */
  331. if (!uns)
  332. {
  333. if (hnum < 0)
  334. {
  335. quo_neg = ~ quo_neg;
  336. /* (minimum integer) / (-1) is the only overflow case. */
  337. if (neg_double (lnum, hnum, &lnum, &hnum)
  338. && ((HOST_WIDE_INT) lden & hden) == -1)
  339. overflow = 1;
  340. }
  341. if (hden < 0)
  342. {
  343. quo_neg = ~ quo_neg;
  344. neg_double (lden, hden, &lden, &hden);
  345. }
  346. }
  347. if (hnum == 0 && hden == 0)
  348. { /* single precision */
  349. *hquo = *hrem = 0;
  350. /* This unsigned division rounds toward zero. */
  351. *lquo = lnum / lden;
  352. goto finish_up;
  353. }
  354. if (hnum == 0)
  355. { /* trivial case: dividend < divisor */
  356. /* hden != 0 already checked. */
  357. *hquo = *lquo = 0;
  358. *hrem = hnum;
  359. *lrem = lnum;
  360. goto finish_up;
  361. }
  362. memset (quo, 0, sizeof quo);
  363. memset (num, 0, sizeof num); /* to zero 9th element */
  364. memset (den, 0, sizeof den);
  365. encode (num, lnum, hnum);
  366. encode (den, lden, hden);
  367. /* Special code for when the divisor < BASE. */
  368. if (hden == 0 && lden < (unsigned HOST_WIDE_INT) BASE)
  369. {
  370. /* hnum != 0 already checked. */
  371. for (i = 4 - 1; i >= 0; i--)
  372. {
  373. work = num[i] + carry * BASE;
  374. quo[i] = work / lden;
  375. carry = work % lden;
  376. }
  377. }
  378. else
  379. {
  380. /* Full double precision division,
  381. with thanks to Don Knuth's "Seminumerical Algorithms". */
  382. int num_hi_sig, den_hi_sig;
  383. unsigned HOST_WIDE_INT quo_est, scale;
  384. /* Find the highest nonzero divisor digit. */
  385. for (i = 4 - 1;; i--)
  386. if (den[i] != 0)
  387. {
  388. den_hi_sig = i;
  389. break;
  390. }
  391. /* Insure that the first digit of the divisor is at least BASE/2.
  392. This is required by the quotient digit estimation algorithm. */
  393. scale = BASE / (den[den_hi_sig] + 1);
  394. if (scale > 1)
  395. { /* scale divisor and dividend */
  396. carry = 0;
  397. for (i = 0; i <= 4 - 1; i++)
  398. {
  399. work = (num[i] * scale) + carry;
  400. num[i] = LOWPART (work);
  401. carry = HIGHPART (work);
  402. }
  403. num[4] = carry;
  404. carry = 0;
  405. for (i = 0; i <= 4 - 1; i++)
  406. {
  407. work = (den[i] * scale) + carry;
  408. den[i] = LOWPART (work);
  409. carry = HIGHPART (work);
  410. if (den[i] != 0) den_hi_sig = i;
  411. }
  412. }
  413. num_hi_sig = 4;
  414. /* Main loop */
  415. for (i = num_hi_sig - den_hi_sig - 1; i >= 0; i--)
  416. {
  417. /* Guess the next quotient digit, quo_est, by dividing the first
  418. two remaining dividend digits by the high order quotient digit.
  419. quo_est is never low and is at most 2 high. */
  420. unsigned HOST_WIDE_INT tmp;
  421. num_hi_sig = i + den_hi_sig + 1;
  422. work = num[num_hi_sig] * BASE + num[num_hi_sig - 1];
  423. if (num[num_hi_sig] != den[den_hi_sig])
  424. quo_est = work / den[den_hi_sig];
  425. else
  426. quo_est = BASE - 1;
  427. /* Refine quo_est so it's usually correct, and at most one high. */
  428. tmp = work - quo_est * den[den_hi_sig];
  429. if (tmp < BASE
  430. && (den[den_hi_sig - 1] * quo_est
  431. > (tmp * BASE + num[num_hi_sig - 2])))
  432. quo_est--;
  433. /* Try QUO_EST as the quotient digit, by multiplying the
  434. divisor by QUO_EST and subtracting from the remaining dividend.
  435. Keep in mind that QUO_EST is the I - 1st digit. */
  436. carry = 0;
  437. for (j = 0; j <= den_hi_sig; j++)
  438. {
  439. work = quo_est * den[j] + carry;
  440. carry = HIGHPART (work);
  441. work = num[i + j] - LOWPART (work);
  442. num[i + j] = LOWPART (work);
  443. carry += HIGHPART (work) != 0;
  444. }
  445. /* If quo_est was high by one, then num[i] went negative and
  446. we need to correct things. */
  447. if (num[num_hi_sig] < (HOST_WIDE_INT) carry)
  448. {
  449. quo_est--;
  450. carry = 0; /* add divisor back in */
  451. for (j = 0; j <= den_hi_sig; j++)
  452. {
  453. work = num[i + j] + den[j] + carry;
  454. carry = HIGHPART (work);
  455. num[i + j] = LOWPART (work);
  456. }
  457. num [num_hi_sig] += carry;
  458. }
  459. /* Store the quotient digit. */
  460. quo[i] = quo_est;
  461. }
  462. }
  463. decode (quo, lquo, hquo);
  464. finish_up:
  465. /* If result is negative, make it so. */
  466. if (quo_neg)
  467. neg_double (*lquo, *hquo, lquo, hquo);
  468. /* Compute trial remainder: rem = num - (quo * den) */
  469. mul_double (*lquo, *hquo, lden_orig, hden_orig, lrem, hrem);
  470. neg_double (*lrem, *hrem, lrem, hrem);
  471. add_double (lnum_orig, hnum_orig, *lrem, *hrem, lrem, hrem);
  472. switch (code)
  473. {
  474. case TRUNC_DIV_EXPR:
  475. case TRUNC_MOD_EXPR: /* round toward zero */
  476. case EXACT_DIV_EXPR: /* for this one, it shouldn't matter */
  477. return overflow;
  478. case FLOOR_DIV_EXPR:
  479. case FLOOR_MOD_EXPR: /* round toward negative infinity */
  480. if (quo_neg && (*lrem != 0 || *hrem != 0)) /* ratio < 0 && rem != 0 */
  481. {
  482. /* quo = quo - 1; */
  483. add_double (*lquo, *hquo, (HOST_WIDE_INT) -1, (HOST_WIDE_INT) -1,
  484. lquo, hquo);
  485. }
  486. else
  487. return overflow;
  488. break;
  489. case CEIL_DIV_EXPR:
  490. case CEIL_MOD_EXPR: /* round toward positive infinity */
  491. if (!quo_neg && (*lrem != 0 || *hrem != 0)) /* ratio > 0 && rem != 0 */
  492. {
  493. add_double (*lquo, *hquo, (HOST_WIDE_INT) 1, (HOST_WIDE_INT) 0,
  494. lquo, hquo);
  495. }
  496. else
  497. return overflow;
  498. break;
  499. case ROUND_DIV_EXPR:
  500. case ROUND_MOD_EXPR: /* round to closest integer */
  501. {
  502. unsigned HOST_WIDE_INT labs_rem = *lrem;
  503. HOST_WIDE_INT habs_rem = *hrem;
  504. unsigned HOST_WIDE_INT labs_den = lden, lnegabs_rem, ldiff;
  505. HOST_WIDE_INT habs_den = hden, hnegabs_rem, hdiff;
  506. /* Get absolute values. */
  507. if (!uns && *hrem < 0)
  508. neg_double (*lrem, *hrem, &labs_rem, &habs_rem);
  509. if (!uns && hden < 0)
  510. neg_double (lden, hden, &labs_den, &habs_den);
  511. /* If abs(rem) >= abs(den) - abs(rem), adjust the quotient. */
  512. neg_double (labs_rem, habs_rem, &lnegabs_rem, &hnegabs_rem);
  513. add_double (labs_den, habs_den, lnegabs_rem, hnegabs_rem,
  514. &ldiff, &hdiff);
  515. if (((unsigned HOST_WIDE_INT) habs_rem
  516. > (unsigned HOST_WIDE_INT) hdiff)
  517. || (habs_rem == hdiff && labs_rem >= ldiff))
  518. {
  519. if (quo_neg)
  520. /* quo = quo - 1; */
  521. add_double (*lquo, *hquo,
  522. (HOST_WIDE_INT) -1, (HOST_WIDE_INT) -1, lquo, hquo);
  523. else
  524. /* quo = quo + 1; */
  525. add_double (*lquo, *hquo, (HOST_WIDE_INT) 1, (HOST_WIDE_INT) 0,
  526. lquo, hquo);
  527. }
  528. else
  529. return overflow;
  530. }
  531. break;
  532. default:
  533. gcc_unreachable ();
  534. }
  535. /* Compute true remainder: rem = num - (quo * den) */
  536. mul_double (*lquo, *hquo, lden_orig, hden_orig, lrem, hrem);
  537. neg_double (*lrem, *hrem, lrem, hrem);
  538. add_double (lnum_orig, hnum_orig, *lrem, *hrem, lrem, hrem);
  539. return overflow;
  540. }
  541. /* Construct from a buffer of length LEN. BUFFER will be read according
  542. to byte endianess and word endianess. Only the lower LEN bytes
  543. of the result are set; the remaining high bytes are cleared. */
  544. double_int
  545. double_int::from_buffer (const unsigned char *buffer, int len)
  546. {
  547. double_int result = double_int_zero;
  548. int words = len / UNITS_PER_WORD;
  549. gcc_assert (len * BITS_PER_UNIT <= HOST_BITS_PER_DOUBLE_INT);
  550. for (int byte = 0; byte < len; byte++)
  551. {
  552. int offset;
  553. int bitpos = byte * BITS_PER_UNIT;
  554. unsigned HOST_WIDE_INT value;
  555. if (len > UNITS_PER_WORD)
  556. {
  557. int word = byte / UNITS_PER_WORD;
  558. if (WORDS_BIG_ENDIAN)
  559. word = (words - 1) - word;
  560. offset = word * UNITS_PER_WORD;
  561. if (BYTES_BIG_ENDIAN)
  562. offset += (UNITS_PER_WORD - 1) - (byte % UNITS_PER_WORD);
  563. else
  564. offset += byte % UNITS_PER_WORD;
  565. }
  566. else
  567. offset = BYTES_BIG_ENDIAN ? (len - 1) - byte : byte;
  568. value = (unsigned HOST_WIDE_INT) buffer[offset];
  569. if (bitpos < HOST_BITS_PER_WIDE_INT)
  570. result.low |= value << bitpos;
  571. else
  572. result.high |= value << (bitpos - HOST_BITS_PER_WIDE_INT);
  573. }
  574. return result;
  575. }
  576. /* Returns mask for PREC bits. */
  577. double_int
  578. double_int::mask (unsigned prec)
  579. {
  580. unsigned HOST_WIDE_INT m;
  581. double_int mask;
  582. if (prec > HOST_BITS_PER_WIDE_INT)
  583. {
  584. prec -= HOST_BITS_PER_WIDE_INT;
  585. m = ((unsigned HOST_WIDE_INT) 2 << (prec - 1)) - 1;
  586. mask.high = (HOST_WIDE_INT) m;
  587. mask.low = ALL_ONES;
  588. }
  589. else
  590. {
  591. mask.high = 0;
  592. mask.low = prec ? ((unsigned HOST_WIDE_INT) 2 << (prec - 1)) - 1 : 0;
  593. }
  594. return mask;
  595. }
  596. /* Returns a maximum value for signed or unsigned integer
  597. of precision PREC. */
  598. double_int
  599. double_int::max_value (unsigned int prec, bool uns)
  600. {
  601. return double_int::mask (prec - (uns ? 0 : 1));
  602. }
  603. /* Returns a minimum value for signed or unsigned integer
  604. of precision PREC. */
  605. double_int
  606. double_int::min_value (unsigned int prec, bool uns)
  607. {
  608. if (uns)
  609. return double_int_zero;
  610. return double_int_one.lshift (prec - 1, prec, false);
  611. }
  612. /* Clears the bits of CST over the precision PREC. If UNS is false, the bits
  613. outside of the precision are set to the sign bit (i.e., the PREC-th one),
  614. otherwise they are set to zero.
  615. This corresponds to returning the value represented by PREC lowermost bits
  616. of CST, with the given signedness. */
  617. double_int
  618. double_int::ext (unsigned prec, bool uns) const
  619. {
  620. if (uns)
  621. return this->zext (prec);
  622. else
  623. return this->sext (prec);
  624. }
  625. /* The same as double_int::ext with UNS = true. */
  626. double_int
  627. double_int::zext (unsigned prec) const
  628. {
  629. const double_int &cst = *this;
  630. double_int mask = double_int::mask (prec);
  631. double_int r;
  632. r.low = cst.low & mask.low;
  633. r.high = cst.high & mask.high;
  634. return r;
  635. }
  636. /* The same as double_int::ext with UNS = false. */
  637. double_int
  638. double_int::sext (unsigned prec) const
  639. {
  640. const double_int &cst = *this;
  641. double_int mask = double_int::mask (prec);
  642. double_int r;
  643. unsigned HOST_WIDE_INT snum;
  644. if (prec <= HOST_BITS_PER_WIDE_INT)
  645. snum = cst.low;
  646. else
  647. {
  648. prec -= HOST_BITS_PER_WIDE_INT;
  649. snum = (unsigned HOST_WIDE_INT) cst.high;
  650. }
  651. if (((snum >> (prec - 1)) & 1) == 1)
  652. {
  653. r.low = cst.low | ~mask.low;
  654. r.high = cst.high | ~mask.high;
  655. }
  656. else
  657. {
  658. r.low = cst.low & mask.low;
  659. r.high = cst.high & mask.high;
  660. }
  661. return r;
  662. }
  663. /* Returns true if CST fits in signed HOST_WIDE_INT. */
  664. bool
  665. double_int::fits_shwi () const
  666. {
  667. const double_int &cst = *this;
  668. if (cst.high == 0)
  669. return (HOST_WIDE_INT) cst.low >= 0;
  670. else if (cst.high == -1)
  671. return (HOST_WIDE_INT) cst.low < 0;
  672. else
  673. return false;
  674. }
  675. /* Returns true if CST fits in HOST_WIDE_INT if UNS is false, or in
  676. unsigned HOST_WIDE_INT if UNS is true. */
  677. bool
  678. double_int::fits_hwi (bool uns) const
  679. {
  680. if (uns)
  681. return this->fits_uhwi ();
  682. else
  683. return this->fits_shwi ();
  684. }
  685. /* Returns A * B. */
  686. double_int
  687. double_int::operator * (double_int b) const
  688. {
  689. const double_int &a = *this;
  690. double_int ret;
  691. mul_double (a.low, a.high, b.low, b.high, &ret.low, &ret.high);
  692. return ret;
  693. }
  694. /* Multiplies *this with B and returns a reference to *this. */
  695. double_int &
  696. double_int::operator *= (double_int b)
  697. {
  698. mul_double (low, high, b.low, b.high, &low, &high);
  699. return *this;
  700. }
  701. /* Returns A * B. If the operation overflows according to UNSIGNED_P,
  702. *OVERFLOW is set to nonzero. */
  703. double_int
  704. double_int::mul_with_sign (double_int b, bool unsigned_p, bool *overflow) const
  705. {
  706. const double_int &a = *this;
  707. double_int ret, tem;
  708. *overflow = mul_double_wide_with_sign (a.low, a.high, b.low, b.high,
  709. &ret.low, &ret.high,
  710. &tem.low, &tem.high, unsigned_p);
  711. return ret;
  712. }
  713. double_int
  714. double_int::wide_mul_with_sign (double_int b, bool unsigned_p,
  715. double_int *higher, bool *overflow) const
  716. {
  717. double_int lower;
  718. *overflow = mul_double_wide_with_sign (low, high, b.low, b.high,
  719. &lower.low, &lower.high,
  720. &higher->low, &higher->high,
  721. unsigned_p);
  722. return lower;
  723. }
  724. /* Returns A + B. */
  725. double_int
  726. double_int::operator + (double_int b) const
  727. {
  728. const double_int &a = *this;
  729. double_int ret;
  730. add_double (a.low, a.high, b.low, b.high, &ret.low, &ret.high);
  731. return ret;
  732. }
  733. /* Adds B to *this and returns a reference to *this. */
  734. double_int &
  735. double_int::operator += (double_int b)
  736. {
  737. add_double (low, high, b.low, b.high, &low, &high);
  738. return *this;
  739. }
  740. /* Returns A + B. If the operation overflows according to UNSIGNED_P,
  741. *OVERFLOW is set to nonzero. */
  742. double_int
  743. double_int::add_with_sign (double_int b, bool unsigned_p, bool *overflow) const
  744. {
  745. const double_int &a = *this;
  746. double_int ret;
  747. *overflow = add_double_with_sign (a.low, a.high, b.low, b.high,
  748. &ret.low, &ret.high, unsigned_p);
  749. return ret;
  750. }
  751. /* Returns A - B. */
  752. double_int
  753. double_int::operator - (double_int b) const
  754. {
  755. const double_int &a = *this;
  756. double_int ret;
  757. neg_double (b.low, b.high, &b.low, &b.high);
  758. add_double (a.low, a.high, b.low, b.high, &ret.low, &ret.high);
  759. return ret;
  760. }
  761. /* Subtracts B from *this and returns a reference to *this. */
  762. double_int &
  763. double_int::operator -= (double_int b)
  764. {
  765. neg_double (b.low, b.high, &b.low, &b.high);
  766. add_double (low, high, b.low, b.high, &low, &high);
  767. return *this;
  768. }
  769. /* Returns A - B. If the operation overflows via inconsistent sign bits,
  770. *OVERFLOW is set to nonzero. */
  771. double_int
  772. double_int::sub_with_overflow (double_int b, bool *overflow) const
  773. {
  774. double_int ret;
  775. neg_double (b.low, b.high, &ret.low, &ret.high);
  776. add_double (low, high, ret.low, ret.high, &ret.low, &ret.high);
  777. *overflow = OVERFLOW_SUM_SIGN (ret.high, b.high, high);
  778. return ret;
  779. }
  780. /* Returns -A. */
  781. double_int
  782. double_int::operator - () const
  783. {
  784. const double_int &a = *this;
  785. double_int ret;
  786. neg_double (a.low, a.high, &ret.low, &ret.high);
  787. return ret;
  788. }
  789. double_int
  790. double_int::neg_with_overflow (bool *overflow) const
  791. {
  792. double_int ret;
  793. *overflow = neg_double (low, high, &ret.low, &ret.high);
  794. return ret;
  795. }
  796. /* Returns A / B (computed as unsigned depending on UNS, and rounded as
  797. specified by CODE). CODE is enum tree_code in fact, but double_int.h
  798. must be included before tree.h. The remainder after the division is
  799. stored to MOD. */
  800. double_int
  801. double_int::divmod_with_overflow (double_int b, bool uns, unsigned code,
  802. double_int *mod, bool *overflow) const
  803. {
  804. const double_int &a = *this;
  805. double_int ret;
  806. *overflow = div_and_round_double (code, uns, a.low, a.high,
  807. b.low, b.high, &ret.low, &ret.high,
  808. &mod->low, &mod->high);
  809. return ret;
  810. }
  811. double_int
  812. double_int::divmod (double_int b, bool uns, unsigned code,
  813. double_int *mod) const
  814. {
  815. const double_int &a = *this;
  816. double_int ret;
  817. div_and_round_double (code, uns, a.low, a.high,
  818. b.low, b.high, &ret.low, &ret.high,
  819. &mod->low, &mod->high);
  820. return ret;
  821. }
  822. /* The same as double_int::divmod with UNS = false. */
  823. double_int
  824. double_int::sdivmod (double_int b, unsigned code, double_int *mod) const
  825. {
  826. return this->divmod (b, false, code, mod);
  827. }
  828. /* The same as double_int::divmod with UNS = true. */
  829. double_int
  830. double_int::udivmod (double_int b, unsigned code, double_int *mod) const
  831. {
  832. return this->divmod (b, true, code, mod);
  833. }
  834. /* Returns A / B (computed as unsigned depending on UNS, and rounded as
  835. specified by CODE). CODE is enum tree_code in fact, but double_int.h
  836. must be included before tree.h. */
  837. double_int
  838. double_int::div (double_int b, bool uns, unsigned code) const
  839. {
  840. double_int mod;
  841. return this->divmod (b, uns, code, &mod);
  842. }
  843. /* The same as double_int::div with UNS = false. */
  844. double_int
  845. double_int::sdiv (double_int b, unsigned code) const
  846. {
  847. return this->div (b, false, code);
  848. }
  849. /* The same as double_int::div with UNS = true. */
  850. double_int
  851. double_int::udiv (double_int b, unsigned code) const
  852. {
  853. return this->div (b, true, code);
  854. }
  855. /* Returns A % B (computed as unsigned depending on UNS, and rounded as
  856. specified by CODE). CODE is enum tree_code in fact, but double_int.h
  857. must be included before tree.h. */
  858. double_int
  859. double_int::mod (double_int b, bool uns, unsigned code) const
  860. {
  861. double_int mod;
  862. this->divmod (b, uns, code, &mod);
  863. return mod;
  864. }
  865. /* The same as double_int::mod with UNS = false. */
  866. double_int
  867. double_int::smod (double_int b, unsigned code) const
  868. {
  869. return this->mod (b, false, code);
  870. }
  871. /* The same as double_int::mod with UNS = true. */
  872. double_int
  873. double_int::umod (double_int b, unsigned code) const
  874. {
  875. return this->mod (b, true, code);
  876. }
  877. /* Return TRUE iff PRODUCT is an integral multiple of FACTOR, and return
  878. the multiple in *MULTIPLE. Otherwise return FALSE and leave *MULTIPLE
  879. unchanged. */
  880. bool
  881. double_int::multiple_of (double_int factor,
  882. bool unsigned_p, double_int *multiple) const
  883. {
  884. double_int remainder;
  885. double_int quotient = this->divmod (factor, unsigned_p,
  886. TRUNC_DIV_EXPR, &remainder);
  887. if (remainder.is_zero ())
  888. {
  889. *multiple = quotient;
  890. return true;
  891. }
  892. return false;
  893. }
  894. /* Set BITPOS bit in A. */
  895. double_int
  896. double_int::set_bit (unsigned bitpos) const
  897. {
  898. double_int a = *this;
  899. if (bitpos < HOST_BITS_PER_WIDE_INT)
  900. a.low |= (unsigned HOST_WIDE_INT) 1 << bitpos;
  901. else
  902. a.high |= (HOST_WIDE_INT) 1 << (bitpos - HOST_BITS_PER_WIDE_INT);
  903. return a;
  904. }
  905. /* Count trailing zeros in A. */
  906. int
  907. double_int::trailing_zeros () const
  908. {
  909. const double_int &a = *this;
  910. unsigned HOST_WIDE_INT w = a.low ? a.low : (unsigned HOST_WIDE_INT) a.high;
  911. unsigned bits = a.low ? 0 : HOST_BITS_PER_WIDE_INT;
  912. if (!w)
  913. return HOST_BITS_PER_DOUBLE_INT;
  914. bits += ctz_hwi (w);
  915. return bits;
  916. }
  917. /* Shift A left by COUNT places. */
  918. double_int
  919. double_int::lshift (HOST_WIDE_INT count) const
  920. {
  921. double_int ret;
  922. gcc_checking_assert (count >= 0);
  923. if (count >= HOST_BITS_PER_DOUBLE_INT)
  924. {
  925. /* Shifting by the host word size is undefined according to the
  926. ANSI standard, so we must handle this as a special case. */
  927. ret.high = 0;
  928. ret.low = 0;
  929. }
  930. else if (count >= HOST_BITS_PER_WIDE_INT)
  931. {
  932. ret.high = low << (count - HOST_BITS_PER_WIDE_INT);
  933. ret.low = 0;
  934. }
  935. else
  936. {
  937. ret.high = (((unsigned HOST_WIDE_INT) high << count)
  938. | (low >> (HOST_BITS_PER_WIDE_INT - count - 1) >> 1));
  939. ret.low = low << count;
  940. }
  941. return ret;
  942. }
  943. /* Shift A right by COUNT places. */
  944. double_int
  945. double_int::rshift (HOST_WIDE_INT count) const
  946. {
  947. double_int ret;
  948. gcc_checking_assert (count >= 0);
  949. if (count >= HOST_BITS_PER_DOUBLE_INT)
  950. {
  951. /* Shifting by the host word size is undefined according to the
  952. ANSI standard, so we must handle this as a special case. */
  953. ret.high = 0;
  954. ret.low = 0;
  955. }
  956. else if (count >= HOST_BITS_PER_WIDE_INT)
  957. {
  958. ret.high = 0;
  959. ret.low
  960. = (unsigned HOST_WIDE_INT) (high >> (count - HOST_BITS_PER_WIDE_INT));
  961. }
  962. else
  963. {
  964. ret.high = high >> count;
  965. ret.low = ((low >> count)
  966. | ((unsigned HOST_WIDE_INT) high
  967. << (HOST_BITS_PER_WIDE_INT - count - 1) << 1));
  968. }
  969. return ret;
  970. }
  971. /* Shift A left by COUNT places keeping only PREC bits of result. Shift
  972. right if COUNT is negative. ARITH true specifies arithmetic shifting;
  973. otherwise use logical shift. */
  974. double_int
  975. double_int::lshift (HOST_WIDE_INT count, unsigned int prec, bool arith) const
  976. {
  977. double_int ret;
  978. if (count > 0)
  979. lshift_double (low, high, count, prec, &ret.low, &ret.high);
  980. else
  981. rshift_double (low, high, absu_hwi (count), prec, &ret.low, &ret.high, arith);
  982. return ret;
  983. }
  984. /* Shift A right by COUNT places keeping only PREC bits of result. Shift
  985. left if COUNT is negative. ARITH true specifies arithmetic shifting;
  986. otherwise use logical shift. */
  987. double_int
  988. double_int::rshift (HOST_WIDE_INT count, unsigned int prec, bool arith) const
  989. {
  990. double_int ret;
  991. if (count > 0)
  992. rshift_double (low, high, count, prec, &ret.low, &ret.high, arith);
  993. else
  994. lshift_double (low, high, absu_hwi (count), prec, &ret.low, &ret.high);
  995. return ret;
  996. }
  997. /* Arithmetic shift A left by COUNT places keeping only PREC bits of result.
  998. Shift right if COUNT is negative. */
  999. double_int
  1000. double_int::alshift (HOST_WIDE_INT count, unsigned int prec) const
  1001. {
  1002. double_int r;
  1003. if (count > 0)
  1004. lshift_double (low, high, count, prec, &r.low, &r.high);
  1005. else
  1006. rshift_double (low, high, absu_hwi (count), prec, &r.low, &r.high, true);
  1007. return r;
  1008. }
  1009. /* Arithmetic shift A right by COUNT places keeping only PREC bits of result.
  1010. Shift left if COUNT is negative. */
  1011. double_int
  1012. double_int::arshift (HOST_WIDE_INT count, unsigned int prec) const
  1013. {
  1014. double_int r;
  1015. if (count > 0)
  1016. rshift_double (low, high, count, prec, &r.low, &r.high, true);
  1017. else
  1018. lshift_double (low, high, absu_hwi (count), prec, &r.low, &r.high);
  1019. return r;
  1020. }
  1021. /* Logical shift A left by COUNT places keeping only PREC bits of result.
  1022. Shift right if COUNT is negative. */
  1023. double_int
  1024. double_int::llshift (HOST_WIDE_INT count, unsigned int prec) const
  1025. {
  1026. double_int r;
  1027. if (count > 0)
  1028. lshift_double (low, high, count, prec, &r.low, &r.high);
  1029. else
  1030. rshift_double (low, high, absu_hwi (count), prec, &r.low, &r.high, false);
  1031. return r;
  1032. }
  1033. /* Logical shift A right by COUNT places keeping only PREC bits of result.
  1034. Shift left if COUNT is negative. */
  1035. double_int
  1036. double_int::lrshift (HOST_WIDE_INT count, unsigned int prec) const
  1037. {
  1038. double_int r;
  1039. if (count > 0)
  1040. rshift_double (low, high, count, prec, &r.low, &r.high, false);
  1041. else
  1042. lshift_double (low, high, absu_hwi (count), prec, &r.low, &r.high);
  1043. return r;
  1044. }
  1045. /* Rotate A left by COUNT places keeping only PREC bits of result.
  1046. Rotate right if COUNT is negative. */
  1047. double_int
  1048. double_int::lrotate (HOST_WIDE_INT count, unsigned int prec) const
  1049. {
  1050. double_int t1, t2;
  1051. count %= prec;
  1052. if (count < 0)
  1053. count += prec;
  1054. t1 = this->llshift (count, prec);
  1055. t2 = this->lrshift (prec - count, prec);
  1056. return t1 | t2;
  1057. }
  1058. /* Rotate A rigth by COUNT places keeping only PREC bits of result.
  1059. Rotate right if COUNT is negative. */
  1060. double_int
  1061. double_int::rrotate (HOST_WIDE_INT count, unsigned int prec) const
  1062. {
  1063. double_int t1, t2;
  1064. count %= prec;
  1065. if (count < 0)
  1066. count += prec;
  1067. t1 = this->lrshift (count, prec);
  1068. t2 = this->llshift (prec - count, prec);
  1069. return t1 | t2;
  1070. }
  1071. /* Returns -1 if A < B, 0 if A == B and 1 if A > B. Signedness of the
  1072. comparison is given by UNS. */
  1073. int
  1074. double_int::cmp (double_int b, bool uns) const
  1075. {
  1076. if (uns)
  1077. return this->ucmp (b);
  1078. else
  1079. return this->scmp (b);
  1080. }
  1081. /* Compares two unsigned values A and B. Returns -1 if A < B, 0 if A == B,
  1082. and 1 if A > B. */
  1083. int
  1084. double_int::ucmp (double_int b) const
  1085. {
  1086. const double_int &a = *this;
  1087. if ((unsigned HOST_WIDE_INT) a.high < (unsigned HOST_WIDE_INT) b.high)
  1088. return -1;
  1089. if ((unsigned HOST_WIDE_INT) a.high > (unsigned HOST_WIDE_INT) b.high)
  1090. return 1;
  1091. if (a.low < b.low)
  1092. return -1;
  1093. if (a.low > b.low)
  1094. return 1;
  1095. return 0;
  1096. }
  1097. /* Compares two signed values A and B. Returns -1 if A < B, 0 if A == B,
  1098. and 1 if A > B. */
  1099. int
  1100. double_int::scmp (double_int b) const
  1101. {
  1102. const double_int &a = *this;
  1103. if (a.high < b.high)
  1104. return -1;
  1105. if (a.high > b.high)
  1106. return 1;
  1107. if (a.low < b.low)
  1108. return -1;
  1109. if (a.low > b.low)
  1110. return 1;
  1111. return 0;
  1112. }
  1113. /* Compares two unsigned values A and B for less-than. */
  1114. bool
  1115. double_int::ult (double_int b) const
  1116. {
  1117. if ((unsigned HOST_WIDE_INT) high < (unsigned HOST_WIDE_INT) b.high)
  1118. return true;
  1119. if ((unsigned HOST_WIDE_INT) high > (unsigned HOST_WIDE_INT) b.high)
  1120. return false;
  1121. if (low < b.low)
  1122. return true;
  1123. return false;
  1124. }
  1125. /* Compares two unsigned values A and B for less-than or equal-to. */
  1126. bool
  1127. double_int::ule (double_int b) const
  1128. {
  1129. if ((unsigned HOST_WIDE_INT) high < (unsigned HOST_WIDE_INT) b.high)
  1130. return true;
  1131. if ((unsigned HOST_WIDE_INT) high > (unsigned HOST_WIDE_INT) b.high)
  1132. return false;
  1133. if (low <= b.low)
  1134. return true;
  1135. return false;
  1136. }
  1137. /* Compares two unsigned values A and B for greater-than. */
  1138. bool
  1139. double_int::ugt (double_int b) const
  1140. {
  1141. if ((unsigned HOST_WIDE_INT) high > (unsigned HOST_WIDE_INT) b.high)
  1142. return true;
  1143. if ((unsigned HOST_WIDE_INT) high < (unsigned HOST_WIDE_INT) b.high)
  1144. return false;
  1145. if (low > b.low)
  1146. return true;
  1147. return false;
  1148. }
  1149. /* Compares two signed values A and B for less-than. */
  1150. bool
  1151. double_int::slt (double_int b) const
  1152. {
  1153. if (high < b.high)
  1154. return true;
  1155. if (high > b.high)
  1156. return false;
  1157. if (low < b.low)
  1158. return true;
  1159. return false;
  1160. }
  1161. /* Compares two signed values A and B for less-than or equal-to. */
  1162. bool
  1163. double_int::sle (double_int b) const
  1164. {
  1165. if (high < b.high)
  1166. return true;
  1167. if (high > b.high)
  1168. return false;
  1169. if (low <= b.low)
  1170. return true;
  1171. return false;
  1172. }
  1173. /* Compares two signed values A and B for greater-than. */
  1174. bool
  1175. double_int::sgt (double_int b) const
  1176. {
  1177. if (high > b.high)
  1178. return true;
  1179. if (high < b.high)
  1180. return false;
  1181. if (low > b.low)
  1182. return true;
  1183. return false;
  1184. }
  1185. /* Compares two values A and B. Returns max value. Signedness of the
  1186. comparison is given by UNS. */
  1187. double_int
  1188. double_int::max (double_int b, bool uns)
  1189. {
  1190. return (this->cmp (b, uns) == 1) ? *this : b;
  1191. }
  1192. /* Compares two signed values A and B. Returns max value. */
  1193. double_int
  1194. double_int::smax (double_int b)
  1195. {
  1196. return (this->scmp (b) == 1) ? *this : b;
  1197. }
  1198. /* Compares two unsigned values A and B. Returns max value. */
  1199. double_int
  1200. double_int::umax (double_int b)
  1201. {
  1202. return (this->ucmp (b) == 1) ? *this : b;
  1203. }
  1204. /* Compares two values A and B. Returns mix value. Signedness of the
  1205. comparison is given by UNS. */
  1206. double_int
  1207. double_int::min (double_int b, bool uns)
  1208. {
  1209. return (this->cmp (b, uns) == -1) ? *this : b;
  1210. }
  1211. /* Compares two signed values A and B. Returns min value. */
  1212. double_int
  1213. double_int::smin (double_int b)
  1214. {
  1215. return (this->scmp (b) == -1) ? *this : b;
  1216. }
  1217. /* Compares two unsigned values A and B. Returns min value. */
  1218. double_int
  1219. double_int::umin (double_int b)
  1220. {
  1221. return (this->ucmp (b) == -1) ? *this : b;
  1222. }
  1223. /* Splits last digit of *CST (taken as unsigned) in BASE and returns it. */
  1224. static unsigned
  1225. double_int_split_digit (double_int *cst, unsigned base)
  1226. {
  1227. unsigned HOST_WIDE_INT resl, reml;
  1228. HOST_WIDE_INT resh, remh;
  1229. div_and_round_double (FLOOR_DIV_EXPR, true, cst->low, cst->high, base, 0,
  1230. &resl, &resh, &reml, &remh);
  1231. cst->high = resh;
  1232. cst->low = resl;
  1233. return reml;
  1234. }
  1235. /* Dumps CST to FILE. If UNS is true, CST is considered to be unsigned,
  1236. otherwise it is signed. */
  1237. void
  1238. dump_double_int (FILE *file, double_int cst, bool uns)
  1239. {
  1240. unsigned digits[100], n;
  1241. int i;
  1242. if (cst.is_zero ())
  1243. {
  1244. fprintf (file, "0");
  1245. return;
  1246. }
  1247. if (!uns && cst.is_negative ())
  1248. {
  1249. fprintf (file, "-");
  1250. cst = -cst;
  1251. }
  1252. for (n = 0; !cst.is_zero (); n++)
  1253. digits[n] = double_int_split_digit (&cst, 10);
  1254. for (i = n - 1; i >= 0; i--)
  1255. fprintf (file, "%u", digits[i]);
  1256. }
  1257. /* Sets RESULT to VAL, taken unsigned if UNS is true and as signed
  1258. otherwise. */
  1259. void
  1260. mpz_set_double_int (mpz_t result, double_int val, bool uns)
  1261. {
  1262. bool negate = false;
  1263. unsigned HOST_WIDE_INT vp[2];
  1264. if (!uns && val.is_negative ())
  1265. {
  1266. negate = true;
  1267. val = -val;
  1268. }
  1269. vp[0] = val.low;
  1270. vp[1] = (unsigned HOST_WIDE_INT) val.high;
  1271. mpz_import (result, 2, -1, sizeof (HOST_WIDE_INT), 0, 0, vp);
  1272. if (negate)
  1273. mpz_neg (result, result);
  1274. }
  1275. /* Returns VAL converted to TYPE. If WRAP is true, then out-of-range
  1276. values of VAL will be wrapped; otherwise, they will be set to the
  1277. appropriate minimum or maximum TYPE bound. */
  1278. double_int
  1279. mpz_get_double_int (const_tree type, mpz_t val, bool wrap)
  1280. {
  1281. unsigned HOST_WIDE_INT *vp;
  1282. size_t count, numb;
  1283. double_int res;
  1284. if (!wrap)
  1285. {
  1286. mpz_t min, max;
  1287. mpz_init (min);
  1288. mpz_init (max);
  1289. get_type_static_bounds (type, min, max);
  1290. if (mpz_cmp (val, min) < 0)
  1291. mpz_set (val, min);
  1292. else if (mpz_cmp (val, max) > 0)
  1293. mpz_set (val, max);
  1294. mpz_clear (min);
  1295. mpz_clear (max);
  1296. }
  1297. /* Determine the number of unsigned HOST_WIDE_INT that are required
  1298. for representing the value. The code to calculate count is
  1299. extracted from the GMP manual, section "Integer Import and Export":
  1300. http://gmplib.org/manual/Integer-Import-and-Export.html */
  1301. numb = 8 * sizeof (HOST_WIDE_INT);
  1302. count = (mpz_sizeinbase (val, 2) + numb-1) / numb;
  1303. if (count < 2)
  1304. count = 2;
  1305. vp = (unsigned HOST_WIDE_INT *) alloca (count * sizeof (HOST_WIDE_INT));
  1306. vp[0] = 0;
  1307. vp[1] = 0;
  1308. mpz_export (vp, &count, -1, sizeof (HOST_WIDE_INT), 0, 0, val);
  1309. gcc_assert (wrap || count <= 2);
  1310. res.low = vp[0];
  1311. res.high = (HOST_WIDE_INT) vp[1];
  1312. res = res.ext (TYPE_PRECISION (type), TYPE_UNSIGNED (type));
  1313. if (mpz_sgn (val) < 0)
  1314. res = -res;
  1315. return res;
  1316. }