libgcc.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592
  1. /*
  2. * This file is part of GNU CC.
  3. *
  4. * GNU CC is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published
  6. * by the Free Software Foundation; either version 2, or (at your
  7. * option) any later version.
  8. *
  9. * GNU CC is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public
  15. * License along with GNU CC; see the file COPYING. If not, write
  16. * to the Free Software Foundation, 59 Temple Place - Suite 330,
  17. * Boston, MA 02111-1307, USA.
  18. */
  19. typedef unsigned int UWtype;
  20. typedef unsigned int UHWtype;
  21. typedef unsigned long long UDWtype;
  22. #define W_TYPE_SIZE 32
  23. typedef unsigned char UQItype;
  24. typedef long SItype;
  25. typedef unsigned long USItype;
  26. typedef long long DItype;
  27. typedef unsigned long long DSItype;
  28. #include "longlong.h"
  29. typedef int word_type;
  30. typedef long Wtype;
  31. typedef long long DWtype;
  32. struct DWstruct { Wtype low, high;};
  33. typedef union
  34. {
  35. struct DWstruct s;
  36. DWtype ll;
  37. } DWunion;
  38. #define BITS_PER_UNIT 8
  39. UDWtype
  40. __udivmoddi4 (UDWtype n, UDWtype d, UDWtype *rp);
  41. const UQItype __clz_tab[256] =
  42. {
  43. 0,1,2,2,3,3,3,3,4,4,4,4,4,4,4,4,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,
  44. 6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
  45. 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
  46. 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
  47. 8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,
  48. 8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,
  49. 8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,
  50. 8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8
  51. };
  52. DWtype
  53. __ashldi3 (DWtype u, word_type b)
  54. {
  55. if (b == 0)
  56. return u;
  57. const DWunion uu = {.ll = u};
  58. const word_type bm = (sizeof (Wtype) * BITS_PER_UNIT) - b;
  59. DWunion w;
  60. if (bm <= 0)
  61. {
  62. w.s.low = 0;
  63. w.s.high = (UWtype) uu.s.low << -bm;
  64. }
  65. else
  66. {
  67. const UWtype carries = (UWtype) uu.s.low >> bm;
  68. w.s.low = (UWtype) uu.s.low << b;
  69. w.s.high = ((UWtype) uu.s.high << b) | carries;
  70. }
  71. return w.ll;
  72. }
  73. DWtype
  74. __ashrdi3 (DWtype u, word_type b)
  75. {
  76. if (b == 0)
  77. return u;
  78. const DWunion uu = {.ll = u};
  79. const word_type bm = (sizeof (Wtype) * BITS_PER_UNIT) - b;
  80. DWunion w;
  81. if (bm <= 0)
  82. {
  83. /* w.s.high = 1..1 or 0..0 */
  84. w.s.high = uu.s.high >> (sizeof (Wtype) * BITS_PER_UNIT - 1);
  85. w.s.low = uu.s.high >> -bm;
  86. }
  87. else
  88. {
  89. const UWtype carries = (UWtype) uu.s.high << bm;
  90. w.s.high = uu.s.high >> b;
  91. w.s.low = ((UWtype) uu.s.low >> b) | carries;
  92. }
  93. return w.ll;
  94. }
  95. DWtype
  96. __lshrdi3 (DWtype u, word_type b)
  97. {
  98. if (b == 0)
  99. return u;
  100. const DWunion uu = {.ll = u};
  101. const word_type bm = (sizeof (Wtype) * BITS_PER_UNIT) - b;
  102. DWunion w;
  103. if (bm <= 0)
  104. {
  105. w.s.high = 0;
  106. w.s.low = (UWtype) uu.s.high >> -bm;
  107. }
  108. else
  109. {
  110. const UWtype carries = (UWtype) uu.s.high << bm;
  111. w.s.high = (UWtype) uu.s.high >> b;
  112. w.s.low = ((UWtype) uu.s.low >> b) | carries;
  113. }
  114. return w.ll;
  115. }
  116. word_type
  117. __cmpdi2 (DWtype a, DWtype b)
  118. {
  119. const DWunion au = {.ll = a};
  120. const DWunion bu = {.ll = b};
  121. if (au.s.high < bu.s.high)
  122. return 0;
  123. else if (au.s.high > bu.s.high)
  124. return 2;
  125. if ((UWtype) au.s.low < (UWtype) bu.s.low)
  126. return 0;
  127. else if ((UWtype) au.s.low > (UWtype) bu.s.low)
  128. return 2;
  129. return 1;
  130. }
  131. UDWtype
  132. __udivmoddi4 (UDWtype n, UDWtype d, UDWtype *rp)
  133. {
  134. const DWunion nn = {.ll = n};
  135. const DWunion dd = {.ll = d};
  136. DWunion rr;
  137. UWtype d0, d1, n0, n1, n2;
  138. UWtype q0, q1;
  139. UWtype b, bm;
  140. d0 = dd.s.low;
  141. d1 = dd.s.high;
  142. n0 = nn.s.low;
  143. n1 = nn.s.high;
  144. #if !UDIV_NEEDS_NORMALIZATION
  145. if (d1 == 0)
  146. {
  147. if (d0 > n1)
  148. {
  149. /* 0q = nn / 0D */
  150. udiv_qrnnd (q0, n0, n1, n0, d0);
  151. q1 = 0;
  152. /* Remainder in n0. */
  153. }
  154. else
  155. {
  156. /* qq = NN / 0d */
  157. if (d0 == 0)
  158. d0 = 1 / d0; /* Divide intentionally by zero. */
  159. udiv_qrnnd (q1, n1, 0, n1, d0);
  160. udiv_qrnnd (q0, n0, n1, n0, d0);
  161. /* Remainder in n0. */
  162. }
  163. if (rp != 0)
  164. {
  165. rr.s.low = n0;
  166. rr.s.high = 0;
  167. *rp = rr.ll;
  168. }
  169. }
  170. #else /* UDIV_NEEDS_NORMALIZATION */
  171. if (d1 == 0)
  172. {
  173. if (d0 > n1)
  174. {
  175. /* 0q = nn / 0D */
  176. count_leading_zeros (bm, d0);
  177. if (bm != 0)
  178. {
  179. /* Normalize, i.e. make the most significant bit of the
  180. denominator set. */
  181. d0 = d0 << bm;
  182. n1 = (n1 << bm) | (n0 >> (W_TYPE_SIZE - bm));
  183. n0 = n0 << bm;
  184. }
  185. udiv_qrnnd (q0, n0, n1, n0, d0);
  186. q1 = 0;
  187. /* Remainder in n0 >> bm. */
  188. }
  189. else
  190. {
  191. /* qq = NN / 0d */
  192. if (d0 == 0)
  193. d0 = 1 / d0; /* Divide intentionally by zero. */
  194. count_leading_zeros (bm, d0);
  195. if (bm == 0)
  196. {
  197. /* From (n1 >= d0) /\ (the most significant bit of d0 is set),
  198. conclude (the most significant bit of n1 is set) /\ (the
  199. leading quotient digit q1 = 1).
  200. This special case is necessary, not an optimization.
  201. (Shifts counts of W_TYPE_SIZE are undefined.) */
  202. n1 -= d0;
  203. q1 = 1;
  204. }
  205. else
  206. {
  207. /* Normalize. */
  208. b = W_TYPE_SIZE - bm;
  209. d0 = d0 << bm;
  210. n2 = n1 >> b;
  211. n1 = (n1 << bm) | (n0 >> b);
  212. n0 = n0 << bm;
  213. udiv_qrnnd (q1, n1, n2, n1, d0);
  214. }
  215. /* n1 != d0... */
  216. udiv_qrnnd (q0, n0, n1, n0, d0);
  217. /* Remainder in n0 >> bm. */
  218. }
  219. if (rp != 0)
  220. {
  221. rr.s.low = n0 >> bm;
  222. rr.s.high = 0;
  223. *rp = rr.ll;
  224. }
  225. }
  226. #endif /* UDIV_NEEDS_NORMALIZATION */
  227. else
  228. {
  229. if (d1 > n1)
  230. {
  231. /* 00 = nn / DD */
  232. q0 = 0;
  233. q1 = 0;
  234. /* Remainder in n1n0. */
  235. if (rp != 0)
  236. {
  237. rr.s.low = n0;
  238. rr.s.high = n1;
  239. *rp = rr.ll;
  240. }
  241. }
  242. else
  243. {
  244. /* 0q = NN / dd */
  245. count_leading_zeros (bm, d1);
  246. if (bm == 0)
  247. {
  248. /* From (n1 >= d1) /\ (the most significant bit of d1 is set),
  249. conclude (the most significant bit of n1 is set) /\ (the
  250. quotient digit q0 = 0 or 1).
  251. This special case is necessary, not an optimization. */
  252. /* The condition on the next line takes advantage of that
  253. n1 >= d1 (true due to program flow). */
  254. if (n1 > d1 || n0 >= d0)
  255. {
  256. q0 = 1;
  257. sub_ddmmss (n1, n0, n1, n0, d1, d0);
  258. }
  259. else
  260. q0 = 0;
  261. q1 = 0;
  262. if (rp != 0)
  263. {
  264. rr.s.low = n0;
  265. rr.s.high = n1;
  266. *rp = rr.ll;
  267. }
  268. }
  269. else
  270. {
  271. UWtype m1, m0;
  272. /* Normalize. */
  273. b = W_TYPE_SIZE - bm;
  274. d1 = (d1 << bm) | (d0 >> b);
  275. d0 = d0 << bm;
  276. n2 = n1 >> b;
  277. n1 = (n1 << bm) | (n0 >> b);
  278. n0 = n0 << bm;
  279. udiv_qrnnd (q0, n1, n2, n1, d1);
  280. umul_ppmm (m1, m0, q0, d0);
  281. if (m1 > n1 || (m1 == n1 && m0 > n0))
  282. {
  283. q0--;
  284. sub_ddmmss (m1, m0, m1, m0, d1, d0);
  285. }
  286. q1 = 0;
  287. /* Remainder in (n1n0 - m1m0) >> bm. */
  288. if (rp != 0)
  289. {
  290. sub_ddmmss (n1, n0, n1, n0, m1, m0);
  291. rr.s.low = (n1 << b) | (n0 >> bm);
  292. rr.s.high = n1 >> bm;
  293. *rp = rr.ll;
  294. }
  295. }
  296. }
  297. }
  298. const DWunion ww = {{.low = q0, .high = q1}};
  299. return ww.ll;
  300. }
  301. DWtype
  302. __divdi3 (DWtype u, DWtype v)
  303. {
  304. word_type c = 0;
  305. DWunion uu = {.ll = u};
  306. DWunion vv = {.ll = v};
  307. DWtype w;
  308. if (uu.s.high < 0)
  309. c = ~c,
  310. uu.ll = -uu.ll;
  311. if (vv.s.high < 0)
  312. c = ~c,
  313. vv.ll = -vv.ll;
  314. w = __udivmoddi4 (uu.ll, vv.ll, (UDWtype *) 0);
  315. if (c)
  316. w = -w;
  317. return w;
  318. }
  319. DWtype
  320. __negdi2 (DWtype u)
  321. {
  322. const DWunion uu = {.ll = u};
  323. const DWunion w = { {.low = -uu.s.low,
  324. .high = -uu.s.high - ((UWtype) -uu.s.low > 0) } };
  325. return w.ll;
  326. }
  327. DWtype
  328. __muldi3 (DWtype u, DWtype v)
  329. {
  330. const DWunion uu = {.ll = u};
  331. const DWunion vv = {.ll = v};
  332. DWunion w = {.ll = __umulsidi3 (uu.s.low, vv.s.low)};
  333. w.s.high += ((UWtype) uu.s.low * (UWtype) vv.s.high
  334. + (UWtype) uu.s.high * (UWtype) vv.s.low);
  335. return w.ll;
  336. }
  337. DWtype
  338. __moddi3 (DWtype u, DWtype v)
  339. {
  340. word_type c = 0;
  341. DWunion uu = {.ll = u};
  342. DWunion vv = {.ll = v};
  343. DWtype w;
  344. if (uu.s.high < 0)
  345. c = ~c,
  346. uu.ll = -uu.ll;
  347. if (vv.s.high < 0)
  348. vv.ll = -vv.ll;
  349. (void) __udivmoddi4 (uu.ll, vv.ll, (UDWtype*)&w);
  350. if (c)
  351. w = -w;
  352. return w;
  353. }
  354. word_type
  355. __ucmpdi2 (DWtype a, DWtype b)
  356. {
  357. const DWunion au = {.ll = a};
  358. const DWunion bu = {.ll = b};
  359. if ((UWtype) au.s.high < (UWtype) bu.s.high)
  360. return 0;
  361. else if ((UWtype) au.s.high > (UWtype) bu.s.high)
  362. return 2;
  363. if ((UWtype) au.s.low < (UWtype) bu.s.low)
  364. return 0;
  365. else if ((UWtype) au.s.low > (UWtype) bu.s.low)
  366. return 2;
  367. return 1;
  368. }
  369. UDWtype
  370. __udivdi3 (UDWtype n, UDWtype d)
  371. {
  372. return __udivmoddi4 (n, d, (UDWtype *) 0);
  373. }
  374. UDWtype
  375. __umoddi3 (UDWtype u, UDWtype v)
  376. {
  377. UDWtype w;
  378. (void) __udivmoddi4 (u, v, &w);
  379. return w;
  380. }
  381. static USItype
  382. udivmodsi4(USItype num, USItype den, word_type modwanted)
  383. {
  384. USItype bit = 1;
  385. USItype res = 0;
  386. while (den < num && bit && !(den & (1L<<31)))
  387. {
  388. den <<=1;
  389. bit <<=1;
  390. }
  391. while (bit)
  392. {
  393. if (num >= den)
  394. {
  395. num -= den;
  396. res |= bit;
  397. }
  398. bit >>=1;
  399. den >>=1;
  400. }
  401. if (modwanted) return num;
  402. return res;
  403. }
  404. SItype
  405. __divsi3 (SItype a, SItype b)
  406. {
  407. word_type neg = 0;
  408. SItype res;
  409. if (a < 0)
  410. {
  411. a = -a;
  412. neg = !neg;
  413. }
  414. if (b < 0)
  415. {
  416. b = -b;
  417. neg = !neg;
  418. }
  419. res = udivmodsi4 (a, b, 0);
  420. if (neg)
  421. res = -res;
  422. return res;
  423. }
  424. SItype
  425. __udivsi3 (SItype a, SItype b)
  426. {
  427. return udivmodsi4 (a, b, 0);
  428. }
  429. SItype
  430. __modsi3 (SItype a, SItype b)
  431. {
  432. word_type neg = 0;
  433. SItype res;
  434. if (a < 0)
  435. {
  436. a = -a;
  437. neg = 1;
  438. }
  439. if (b < 0)
  440. b = -b;
  441. res = udivmodsi4 (a, b, 1);
  442. if (neg)
  443. res = -res;
  444. return res;
  445. }
  446. SItype
  447. __mulsi3 (SItype a, SItype b)
  448. {
  449. SItype res = 0;
  450. USItype cnt = a;
  451. while (cnt)
  452. {
  453. if (cnt & 1)
  454. {
  455. res += b;
  456. }
  457. b <<= 1;
  458. cnt >>= 1;
  459. }
  460. return res;
  461. }
  462. SItype
  463. __umodsi3 (SItype a, SItype b)
  464. {
  465. return udivmodsi4 (a, b, 1);
  466. }
  467. int
  468. __gcc_bcmp (const unsigned char *s1, const unsigned char *s2, unsigned long size)
  469. {
  470. while (size > 0)
  471. {
  472. const unsigned char c1 = *s1++, c2 = *s2++;
  473. if (c1 != c2)
  474. return c1 - c2;
  475. size--;
  476. }
  477. return 0;
  478. }