libgcc2.c 51 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319
  1. /* More subroutines needed by GCC output code on some machines. */
  2. /* Compile this one with gcc. */
  3. /* Copyright (C) 1989-2015 Free Software Foundation, Inc.
  4. This file is part of GCC.
  5. GCC is free software; you can redistribute it and/or modify it under
  6. the terms of the GNU General Public License as published by the Free
  7. Software Foundation; either version 3, or (at your option) any later
  8. version.
  9. GCC is distributed in the hope that it will be useful, but WITHOUT ANY
  10. WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11. FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  12. for more details.
  13. Under Section 7 of GPL version 3, you are granted additional
  14. permissions described in the GCC Runtime Library Exception, version
  15. 3.1, as published by the Free Software Foundation.
  16. You should have received a copy of the GNU General Public License and
  17. a copy of the GCC Runtime Library Exception along with this program;
  18. see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
  19. <http://www.gnu.org/licenses/>. */
  20. #include "tconfig.h"
  21. #include "tsystem.h"
  22. #include "coretypes.h"
  23. #include "tm.h"
  24. #include "libgcc_tm.h"
  25. #ifdef HAVE_GAS_HIDDEN
  26. #define ATTRIBUTE_HIDDEN __attribute__ ((__visibility__ ("hidden")))
  27. #else
  28. #define ATTRIBUTE_HIDDEN
  29. #endif
  30. /* Work out the largest "word" size that we can deal with on this target. */
  31. #if MIN_UNITS_PER_WORD > 4
  32. # define LIBGCC2_MAX_UNITS_PER_WORD 8
  33. #elif (MIN_UNITS_PER_WORD > 2 \
  34. || (MIN_UNITS_PER_WORD > 1 && __SIZEOF_LONG_LONG__ > 4))
  35. # define LIBGCC2_MAX_UNITS_PER_WORD 4
  36. #else
  37. # define LIBGCC2_MAX_UNITS_PER_WORD MIN_UNITS_PER_WORD
  38. #endif
  39. /* Work out what word size we are using for this compilation.
  40. The value can be set on the command line. */
  41. #ifndef LIBGCC2_UNITS_PER_WORD
  42. #define LIBGCC2_UNITS_PER_WORD LIBGCC2_MAX_UNITS_PER_WORD
  43. #endif
  44. #if LIBGCC2_UNITS_PER_WORD <= LIBGCC2_MAX_UNITS_PER_WORD
  45. #include "libgcc2.h"
  46. #ifdef DECLARE_LIBRARY_RENAMES
  47. DECLARE_LIBRARY_RENAMES
  48. #endif
  49. #if defined (L_negdi2)
  50. DWtype
  51. __negdi2 (DWtype u)
  52. {
  53. const DWunion uu = {.ll = u};
  54. const DWunion w = { {.low = -uu.s.low,
  55. .high = -uu.s.high - ((UWtype) -uu.s.low > 0) } };
  56. return w.ll;
  57. }
  58. #endif
  59. #ifdef L_addvsi3
  60. Wtype
  61. __addvSI3 (Wtype a, Wtype b)
  62. {
  63. const Wtype w = (UWtype) a + (UWtype) b;
  64. if (b >= 0 ? w < a : w > a)
  65. abort ();
  66. return w;
  67. }
  68. #ifdef COMPAT_SIMODE_TRAPPING_ARITHMETIC
  69. SItype
  70. __addvsi3 (SItype a, SItype b)
  71. {
  72. const SItype w = (USItype) a + (USItype) b;
  73. if (b >= 0 ? w < a : w > a)
  74. abort ();
  75. return w;
  76. }
  77. #endif /* COMPAT_SIMODE_TRAPPING_ARITHMETIC */
  78. #endif
  79. #ifdef L_addvdi3
  80. DWtype
  81. __addvDI3 (DWtype a, DWtype b)
  82. {
  83. const DWtype w = (UDWtype) a + (UDWtype) b;
  84. if (b >= 0 ? w < a : w > a)
  85. abort ();
  86. return w;
  87. }
  88. #endif
  89. #ifdef L_subvsi3
  90. Wtype
  91. __subvSI3 (Wtype a, Wtype b)
  92. {
  93. const Wtype w = (UWtype) a - (UWtype) b;
  94. if (b >= 0 ? w > a : w < a)
  95. abort ();
  96. return w;
  97. }
  98. #ifdef COMPAT_SIMODE_TRAPPING_ARITHMETIC
  99. SItype
  100. __subvsi3 (SItype a, SItype b)
  101. {
  102. const SItype w = (USItype) a - (USItype) b;
  103. if (b >= 0 ? w > a : w < a)
  104. abort ();
  105. return w;
  106. }
  107. #endif /* COMPAT_SIMODE_TRAPPING_ARITHMETIC */
  108. #endif
  109. #ifdef L_subvdi3
  110. DWtype
  111. __subvDI3 (DWtype a, DWtype b)
  112. {
  113. const DWtype w = (UDWtype) a - (UDWtype) b;
  114. if (b >= 0 ? w > a : w < a)
  115. abort ();
  116. return w;
  117. }
  118. #endif
  119. #ifdef L_mulvsi3
  120. Wtype
  121. __mulvSI3 (Wtype a, Wtype b)
  122. {
  123. const DWtype w = (DWtype) a * (DWtype) b;
  124. if ((Wtype) (w >> W_TYPE_SIZE) != (Wtype) w >> (W_TYPE_SIZE - 1))
  125. abort ();
  126. return w;
  127. }
  128. #ifdef COMPAT_SIMODE_TRAPPING_ARITHMETIC
  129. #undef WORD_SIZE
  130. #define WORD_SIZE (sizeof (SItype) * BITS_PER_UNIT)
  131. SItype
  132. __mulvsi3 (SItype a, SItype b)
  133. {
  134. const DItype w = (DItype) a * (DItype) b;
  135. if ((SItype) (w >> WORD_SIZE) != (SItype) w >> (WORD_SIZE-1))
  136. abort ();
  137. return w;
  138. }
  139. #endif /* COMPAT_SIMODE_TRAPPING_ARITHMETIC */
  140. #endif
  141. #ifdef L_negvsi2
  142. Wtype
  143. __negvSI2 (Wtype a)
  144. {
  145. const Wtype w = -(UWtype) a;
  146. if (a >= 0 ? w > 0 : w < 0)
  147. abort ();
  148. return w;
  149. }
  150. #ifdef COMPAT_SIMODE_TRAPPING_ARITHMETIC
  151. SItype
  152. __negvsi2 (SItype a)
  153. {
  154. const SItype w = -(USItype) a;
  155. if (a >= 0 ? w > 0 : w < 0)
  156. abort ();
  157. return w;
  158. }
  159. #endif /* COMPAT_SIMODE_TRAPPING_ARITHMETIC */
  160. #endif
  161. #ifdef L_negvdi2
  162. DWtype
  163. __negvDI2 (DWtype a)
  164. {
  165. const DWtype w = -(UDWtype) a;
  166. if (a >= 0 ? w > 0 : w < 0)
  167. abort ();
  168. return w;
  169. }
  170. #endif
  171. #ifdef L_absvsi2
  172. Wtype
  173. __absvSI2 (Wtype a)
  174. {
  175. Wtype w = a;
  176. if (a < 0)
  177. #ifdef L_negvsi2
  178. w = __negvSI2 (a);
  179. #else
  180. w = -(UWtype) a;
  181. if (w < 0)
  182. abort ();
  183. #endif
  184. return w;
  185. }
  186. #ifdef COMPAT_SIMODE_TRAPPING_ARITHMETIC
  187. SItype
  188. __absvsi2 (SItype a)
  189. {
  190. SItype w = a;
  191. if (a < 0)
  192. #ifdef L_negvsi2
  193. w = __negvsi2 (a);
  194. #else
  195. w = -(USItype) a;
  196. if (w < 0)
  197. abort ();
  198. #endif
  199. return w;
  200. }
  201. #endif /* COMPAT_SIMODE_TRAPPING_ARITHMETIC */
  202. #endif
  203. #ifdef L_absvdi2
  204. DWtype
  205. __absvDI2 (DWtype a)
  206. {
  207. DWtype w = a;
  208. if (a < 0)
  209. #ifdef L_negvdi2
  210. w = __negvDI2 (a);
  211. #else
  212. w = -(UDWtype) a;
  213. if (w < 0)
  214. abort ();
  215. #endif
  216. return w;
  217. }
  218. #endif
  219. #ifdef L_mulvdi3
  220. DWtype
  221. __mulvDI3 (DWtype u, DWtype v)
  222. {
  223. /* The unchecked multiplication needs 3 Wtype x Wtype multiplications,
  224. but the checked multiplication needs only two. */
  225. const DWunion uu = {.ll = u};
  226. const DWunion vv = {.ll = v};
  227. if (__builtin_expect (uu.s.high == uu.s.low >> (W_TYPE_SIZE - 1), 1))
  228. {
  229. /* u fits in a single Wtype. */
  230. if (__builtin_expect (vv.s.high == vv.s.low >> (W_TYPE_SIZE - 1), 1))
  231. {
  232. /* v fits in a single Wtype as well. */
  233. /* A single multiplication. No overflow risk. */
  234. return (DWtype) uu.s.low * (DWtype) vv.s.low;
  235. }
  236. else
  237. {
  238. /* Two multiplications. */
  239. DWunion w0 = {.ll = (UDWtype) (UWtype) uu.s.low
  240. * (UDWtype) (UWtype) vv.s.low};
  241. DWunion w1 = {.ll = (UDWtype) (UWtype) uu.s.low
  242. * (UDWtype) (UWtype) vv.s.high};
  243. if (vv.s.high < 0)
  244. w1.s.high -= uu.s.low;
  245. if (uu.s.low < 0)
  246. w1.ll -= vv.ll;
  247. w1.ll += (UWtype) w0.s.high;
  248. if (__builtin_expect (w1.s.high == w1.s.low >> (W_TYPE_SIZE - 1), 1))
  249. {
  250. w0.s.high = w1.s.low;
  251. return w0.ll;
  252. }
  253. }
  254. }
  255. else
  256. {
  257. if (__builtin_expect (vv.s.high == vv.s.low >> (W_TYPE_SIZE - 1), 1))
  258. {
  259. /* v fits into a single Wtype. */
  260. /* Two multiplications. */
  261. DWunion w0 = {.ll = (UDWtype) (UWtype) uu.s.low
  262. * (UDWtype) (UWtype) vv.s.low};
  263. DWunion w1 = {.ll = (UDWtype) (UWtype) uu.s.high
  264. * (UDWtype) (UWtype) vv.s.low};
  265. if (uu.s.high < 0)
  266. w1.s.high -= vv.s.low;
  267. if (vv.s.low < 0)
  268. w1.ll -= uu.ll;
  269. w1.ll += (UWtype) w0.s.high;
  270. if (__builtin_expect (w1.s.high == w1.s.low >> (W_TYPE_SIZE - 1), 1))
  271. {
  272. w0.s.high = w1.s.low;
  273. return w0.ll;
  274. }
  275. }
  276. else
  277. {
  278. /* A few sign checks and a single multiplication. */
  279. if (uu.s.high >= 0)
  280. {
  281. if (vv.s.high >= 0)
  282. {
  283. if (uu.s.high == 0 && vv.s.high == 0)
  284. {
  285. const DWtype w = (UDWtype) (UWtype) uu.s.low
  286. * (UDWtype) (UWtype) vv.s.low;
  287. if (__builtin_expect (w >= 0, 1))
  288. return w;
  289. }
  290. }
  291. else
  292. {
  293. if (uu.s.high == 0 && vv.s.high == (Wtype) -1)
  294. {
  295. DWunion ww = {.ll = (UDWtype) (UWtype) uu.s.low
  296. * (UDWtype) (UWtype) vv.s.low};
  297. ww.s.high -= uu.s.low;
  298. if (__builtin_expect (ww.s.high < 0, 1))
  299. return ww.ll;
  300. }
  301. }
  302. }
  303. else
  304. {
  305. if (vv.s.high >= 0)
  306. {
  307. if (uu.s.high == (Wtype) -1 && vv.s.high == 0)
  308. {
  309. DWunion ww = {.ll = (UDWtype) (UWtype) uu.s.low
  310. * (UDWtype) (UWtype) vv.s.low};
  311. ww.s.high -= vv.s.low;
  312. if (__builtin_expect (ww.s.high < 0, 1))
  313. return ww.ll;
  314. }
  315. }
  316. else
  317. {
  318. if (uu.s.high == (Wtype) -1 && vv.s.high == (Wtype) - 1)
  319. {
  320. DWunion ww = {.ll = (UDWtype) (UWtype) uu.s.low
  321. * (UDWtype) (UWtype) vv.s.low};
  322. ww.s.high -= uu.s.low;
  323. ww.s.high -= vv.s.low;
  324. if (__builtin_expect (ww.s.high >= 0, 1))
  325. return ww.ll;
  326. }
  327. }
  328. }
  329. }
  330. }
  331. /* Overflow. */
  332. abort ();
  333. }
  334. #endif
  335. /* Unless shift functions are defined with full ANSI prototypes,
  336. parameter b will be promoted to int if shift_count_type is smaller than an int. */
  337. #ifdef L_lshrdi3
  338. DWtype
  339. __lshrdi3 (DWtype u, shift_count_type b)
  340. {
  341. if (b == 0)
  342. return u;
  343. const DWunion uu = {.ll = u};
  344. const shift_count_type bm = W_TYPE_SIZE - b;
  345. DWunion w;
  346. if (bm <= 0)
  347. {
  348. w.s.high = 0;
  349. w.s.low = (UWtype) uu.s.high >> -bm;
  350. }
  351. else
  352. {
  353. const UWtype carries = (UWtype) uu.s.high << bm;
  354. w.s.high = (UWtype) uu.s.high >> b;
  355. w.s.low = ((UWtype) uu.s.low >> b) | carries;
  356. }
  357. return w.ll;
  358. }
  359. #endif
  360. #ifdef L_ashldi3
  361. DWtype
  362. __ashldi3 (DWtype u, shift_count_type b)
  363. {
  364. if (b == 0)
  365. return u;
  366. const DWunion uu = {.ll = u};
  367. const shift_count_type bm = W_TYPE_SIZE - b;
  368. DWunion w;
  369. if (bm <= 0)
  370. {
  371. w.s.low = 0;
  372. w.s.high = (UWtype) uu.s.low << -bm;
  373. }
  374. else
  375. {
  376. const UWtype carries = (UWtype) uu.s.low >> bm;
  377. w.s.low = (UWtype) uu.s.low << b;
  378. w.s.high = ((UWtype) uu.s.high << b) | carries;
  379. }
  380. return w.ll;
  381. }
  382. #endif
  383. #ifdef L_ashrdi3
  384. DWtype
  385. __ashrdi3 (DWtype u, shift_count_type b)
  386. {
  387. if (b == 0)
  388. return u;
  389. const DWunion uu = {.ll = u};
  390. const shift_count_type bm = W_TYPE_SIZE - b;
  391. DWunion w;
  392. if (bm <= 0)
  393. {
  394. /* w.s.high = 1..1 or 0..0 */
  395. w.s.high = uu.s.high >> (W_TYPE_SIZE - 1);
  396. w.s.low = uu.s.high >> -bm;
  397. }
  398. else
  399. {
  400. const UWtype carries = (UWtype) uu.s.high << bm;
  401. w.s.high = uu.s.high >> b;
  402. w.s.low = ((UWtype) uu.s.low >> b) | carries;
  403. }
  404. return w.ll;
  405. }
  406. #endif
  407. #ifdef L_bswapsi2
  408. SItype
  409. __bswapsi2 (SItype u)
  410. {
  411. return ((((u) & 0xff000000) >> 24)
  412. | (((u) & 0x00ff0000) >> 8)
  413. | (((u) & 0x0000ff00) << 8)
  414. | (((u) & 0x000000ff) << 24));
  415. }
  416. #endif
  417. #ifdef L_bswapdi2
  418. DItype
  419. __bswapdi2 (DItype u)
  420. {
  421. return ((((u) & 0xff00000000000000ull) >> 56)
  422. | (((u) & 0x00ff000000000000ull) >> 40)
  423. | (((u) & 0x0000ff0000000000ull) >> 24)
  424. | (((u) & 0x000000ff00000000ull) >> 8)
  425. | (((u) & 0x00000000ff000000ull) << 8)
  426. | (((u) & 0x0000000000ff0000ull) << 24)
  427. | (((u) & 0x000000000000ff00ull) << 40)
  428. | (((u) & 0x00000000000000ffull) << 56));
  429. }
  430. #endif
  431. #ifdef L_ffssi2
  432. #undef int
  433. int
  434. __ffsSI2 (UWtype u)
  435. {
  436. UWtype count;
  437. if (u == 0)
  438. return 0;
  439. count_trailing_zeros (count, u);
  440. return count + 1;
  441. }
  442. #endif
  443. #ifdef L_ffsdi2
  444. #undef int
  445. int
  446. __ffsDI2 (DWtype u)
  447. {
  448. const DWunion uu = {.ll = u};
  449. UWtype word, count, add;
  450. if (uu.s.low != 0)
  451. word = uu.s.low, add = 0;
  452. else if (uu.s.high != 0)
  453. word = uu.s.high, add = W_TYPE_SIZE;
  454. else
  455. return 0;
  456. count_trailing_zeros (count, word);
  457. return count + add + 1;
  458. }
  459. #endif
  460. #ifdef L_muldi3
  461. DWtype
  462. __muldi3 (DWtype u, DWtype v)
  463. {
  464. const DWunion uu = {.ll = u};
  465. const DWunion vv = {.ll = v};
  466. DWunion w = {.ll = __umulsidi3 (uu.s.low, vv.s.low)};
  467. w.s.high += ((UWtype) uu.s.low * (UWtype) vv.s.high
  468. + (UWtype) uu.s.high * (UWtype) vv.s.low);
  469. return w.ll;
  470. }
  471. #endif
  472. #if (defined (L_udivdi3) || defined (L_divdi3) || \
  473. defined (L_umoddi3) || defined (L_moddi3))
  474. #if defined (sdiv_qrnnd)
  475. #define L_udiv_w_sdiv
  476. #endif
  477. #endif
  478. #ifdef L_udiv_w_sdiv
  479. #if defined (sdiv_qrnnd)
  480. #if (defined (L_udivdi3) || defined (L_divdi3) || \
  481. defined (L_umoddi3) || defined (L_moddi3))
  482. static inline __attribute__ ((__always_inline__))
  483. #endif
  484. UWtype
  485. __udiv_w_sdiv (UWtype *rp, UWtype a1, UWtype a0, UWtype d)
  486. {
  487. UWtype q, r;
  488. UWtype c0, c1, b1;
  489. if ((Wtype) d >= 0)
  490. {
  491. if (a1 < d - a1 - (a0 >> (W_TYPE_SIZE - 1)))
  492. {
  493. /* Dividend, divisor, and quotient are nonnegative. */
  494. sdiv_qrnnd (q, r, a1, a0, d);
  495. }
  496. else
  497. {
  498. /* Compute c1*2^32 + c0 = a1*2^32 + a0 - 2^31*d. */
  499. sub_ddmmss (c1, c0, a1, a0, d >> 1, d << (W_TYPE_SIZE - 1));
  500. /* Divide (c1*2^32 + c0) by d. */
  501. sdiv_qrnnd (q, r, c1, c0, d);
  502. /* Add 2^31 to quotient. */
  503. q += (UWtype) 1 << (W_TYPE_SIZE - 1);
  504. }
  505. }
  506. else
  507. {
  508. b1 = d >> 1; /* d/2, between 2^30 and 2^31 - 1 */
  509. c1 = a1 >> 1; /* A/2 */
  510. c0 = (a1 << (W_TYPE_SIZE - 1)) + (a0 >> 1);
  511. if (a1 < b1) /* A < 2^32*b1, so A/2 < 2^31*b1 */
  512. {
  513. sdiv_qrnnd (q, r, c1, c0, b1); /* (A/2) / (d/2) */
  514. r = 2*r + (a0 & 1); /* Remainder from A/(2*b1) */
  515. if ((d & 1) != 0)
  516. {
  517. if (r >= q)
  518. r = r - q;
  519. else if (q - r <= d)
  520. {
  521. r = r - q + d;
  522. q--;
  523. }
  524. else
  525. {
  526. r = r - q + 2*d;
  527. q -= 2;
  528. }
  529. }
  530. }
  531. else if (c1 < b1) /* So 2^31 <= (A/2)/b1 < 2^32 */
  532. {
  533. c1 = (b1 - 1) - c1;
  534. c0 = ~c0; /* logical NOT */
  535. sdiv_qrnnd (q, r, c1, c0, b1); /* (A/2) / (d/2) */
  536. q = ~q; /* (A/2)/b1 */
  537. r = (b1 - 1) - r;
  538. r = 2*r + (a0 & 1); /* A/(2*b1) */
  539. if ((d & 1) != 0)
  540. {
  541. if (r >= q)
  542. r = r - q;
  543. else if (q - r <= d)
  544. {
  545. r = r - q + d;
  546. q--;
  547. }
  548. else
  549. {
  550. r = r - q + 2*d;
  551. q -= 2;
  552. }
  553. }
  554. }
  555. else /* Implies c1 = b1 */
  556. { /* Hence a1 = d - 1 = 2*b1 - 1 */
  557. if (a0 >= -d)
  558. {
  559. q = -1;
  560. r = a0 + d;
  561. }
  562. else
  563. {
  564. q = -2;
  565. r = a0 + 2*d;
  566. }
  567. }
  568. }
  569. *rp = r;
  570. return q;
  571. }
  572. #else
  573. /* If sdiv_qrnnd doesn't exist, define dummy __udiv_w_sdiv. */
  574. UWtype
  575. __udiv_w_sdiv (UWtype *rp __attribute__ ((__unused__)),
  576. UWtype a1 __attribute__ ((__unused__)),
  577. UWtype a0 __attribute__ ((__unused__)),
  578. UWtype d __attribute__ ((__unused__)))
  579. {
  580. return 0;
  581. }
  582. #endif
  583. #endif
  584. #if (defined (L_udivdi3) || defined (L_divdi3) || \
  585. defined (L_umoddi3) || defined (L_moddi3))
  586. #define L_udivmoddi4
  587. #endif
  588. #ifdef L_clz
  589. const UQItype __clz_tab[256] =
  590. {
  591. 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,
  592. 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,
  593. 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,
  594. 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,
  595. 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,
  596. 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,
  597. 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,
  598. 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
  599. };
  600. #endif
  601. #ifdef L_clzsi2
  602. #undef int
  603. int
  604. __clzSI2 (UWtype x)
  605. {
  606. Wtype ret;
  607. count_leading_zeros (ret, x);
  608. return ret;
  609. }
  610. #endif
  611. #ifdef L_clzdi2
  612. #undef int
  613. int
  614. __clzDI2 (UDWtype x)
  615. {
  616. const DWunion uu = {.ll = x};
  617. UWtype word;
  618. Wtype ret, add;
  619. if (uu.s.high)
  620. word = uu.s.high, add = 0;
  621. else
  622. word = uu.s.low, add = W_TYPE_SIZE;
  623. count_leading_zeros (ret, word);
  624. return ret + add;
  625. }
  626. #endif
  627. #ifdef L_ctzsi2
  628. #undef int
  629. int
  630. __ctzSI2 (UWtype x)
  631. {
  632. Wtype ret;
  633. count_trailing_zeros (ret, x);
  634. return ret;
  635. }
  636. #endif
  637. #ifdef L_ctzdi2
  638. #undef int
  639. int
  640. __ctzDI2 (UDWtype x)
  641. {
  642. const DWunion uu = {.ll = x};
  643. UWtype word;
  644. Wtype ret, add;
  645. if (uu.s.low)
  646. word = uu.s.low, add = 0;
  647. else
  648. word = uu.s.high, add = W_TYPE_SIZE;
  649. count_trailing_zeros (ret, word);
  650. return ret + add;
  651. }
  652. #endif
  653. #ifdef L_clrsbsi2
  654. #undef int
  655. int
  656. __clrsbSI2 (Wtype x)
  657. {
  658. Wtype ret;
  659. if (x < 0)
  660. x = ~x;
  661. if (x == 0)
  662. return W_TYPE_SIZE - 1;
  663. count_leading_zeros (ret, x);
  664. return ret - 1;
  665. }
  666. #endif
  667. #ifdef L_clrsbdi2
  668. #undef int
  669. int
  670. __clrsbDI2 (DWtype x)
  671. {
  672. const DWunion uu = {.ll = x};
  673. UWtype word;
  674. Wtype ret, add;
  675. if (uu.s.high == 0)
  676. word = uu.s.low, add = W_TYPE_SIZE;
  677. else if (uu.s.high == -1)
  678. word = ~uu.s.low, add = W_TYPE_SIZE;
  679. else if (uu.s.high >= 0)
  680. word = uu.s.high, add = 0;
  681. else
  682. word = ~uu.s.high, add = 0;
  683. if (word == 0)
  684. ret = W_TYPE_SIZE;
  685. else
  686. count_leading_zeros (ret, word);
  687. return ret + add - 1;
  688. }
  689. #endif
  690. #ifdef L_popcount_tab
  691. const UQItype __popcount_tab[256] =
  692. {
  693. 0,1,1,2,1,2,2,3,1,2,2,3,2,3,3,4,1,2,2,3,2,3,3,4,2,3,3,4,3,4,4,5,
  694. 1,2,2,3,2,3,3,4,2,3,3,4,3,4,4,5,2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6,
  695. 1,2,2,3,2,3,3,4,2,3,3,4,3,4,4,5,2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6,
  696. 2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6,3,4,4,5,4,5,5,6,4,5,5,6,5,6,6,7,
  697. 1,2,2,3,2,3,3,4,2,3,3,4,3,4,4,5,2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6,
  698. 2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6,3,4,4,5,4,5,5,6,4,5,5,6,5,6,6,7,
  699. 2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6,3,4,4,5,4,5,5,6,4,5,5,6,5,6,6,7,
  700. 3,4,4,5,4,5,5,6,4,5,5,6,5,6,6,7,4,5,5,6,5,6,6,7,5,6,6,7,6,7,7,8
  701. };
  702. #endif
  703. #if defined(L_popcountsi2) || defined(L_popcountdi2)
  704. #define POPCOUNTCST2(x) (((UWtype) x << BITS_PER_UNIT) | x)
  705. #define POPCOUNTCST4(x) (((UWtype) x << (2 * BITS_PER_UNIT)) | x)
  706. #define POPCOUNTCST8(x) (((UWtype) x << (4 * BITS_PER_UNIT)) | x)
  707. #if W_TYPE_SIZE == BITS_PER_UNIT
  708. #define POPCOUNTCST(x) x
  709. #elif W_TYPE_SIZE == 2 * BITS_PER_UNIT
  710. #define POPCOUNTCST(x) POPCOUNTCST2 (x)
  711. #elif W_TYPE_SIZE == 4 * BITS_PER_UNIT
  712. #define POPCOUNTCST(x) POPCOUNTCST4 (POPCOUNTCST2 (x))
  713. #elif W_TYPE_SIZE == 8 * BITS_PER_UNIT
  714. #define POPCOUNTCST(x) POPCOUNTCST8 (POPCOUNTCST4 (POPCOUNTCST2 (x)))
  715. #endif
  716. #endif
  717. #ifdef L_popcountsi2
  718. #undef int
  719. int
  720. __popcountSI2 (UWtype x)
  721. {
  722. /* Force table lookup on targets like AVR and RL78 which only
  723. pretend they have LIBGCC2_UNITS_PER_WORD 4, but actually
  724. have 1, and other small word targets. */
  725. #if __SIZEOF_INT__ > 2 && defined (POPCOUNTCST) && BITS_PER_UNIT == 8
  726. x = x - ((x >> 1) & POPCOUNTCST (0x55));
  727. x = (x & POPCOUNTCST (0x33)) + ((x >> 2) & POPCOUNTCST (0x33));
  728. x = (x + (x >> 4)) & POPCOUNTCST (0x0F);
  729. return (x * POPCOUNTCST (0x01)) >> (W_TYPE_SIZE - BITS_PER_UNIT);
  730. #else
  731. int i, ret = 0;
  732. for (i = 0; i < W_TYPE_SIZE; i += 8)
  733. ret += __popcount_tab[(x >> i) & 0xff];
  734. return ret;
  735. #endif
  736. }
  737. #endif
  738. #ifdef L_popcountdi2
  739. #undef int
  740. int
  741. __popcountDI2 (UDWtype x)
  742. {
  743. /* Force table lookup on targets like AVR and RL78 which only
  744. pretend they have LIBGCC2_UNITS_PER_WORD 4, but actually
  745. have 1, and other small word targets. */
  746. #if __SIZEOF_INT__ > 2 && defined (POPCOUNTCST) && BITS_PER_UNIT == 8
  747. const DWunion uu = {.ll = x};
  748. UWtype x1 = uu.s.low, x2 = uu.s.high;
  749. x1 = x1 - ((x1 >> 1) & POPCOUNTCST (0x55));
  750. x2 = x2 - ((x2 >> 1) & POPCOUNTCST (0x55));
  751. x1 = (x1 & POPCOUNTCST (0x33)) + ((x1 >> 2) & POPCOUNTCST (0x33));
  752. x2 = (x2 & POPCOUNTCST (0x33)) + ((x2 >> 2) & POPCOUNTCST (0x33));
  753. x1 = (x1 + (x1 >> 4)) & POPCOUNTCST (0x0F);
  754. x2 = (x2 + (x2 >> 4)) & POPCOUNTCST (0x0F);
  755. x1 += x2;
  756. return (x1 * POPCOUNTCST (0x01)) >> (W_TYPE_SIZE - BITS_PER_UNIT);
  757. #else
  758. int i, ret = 0;
  759. for (i = 0; i < 2*W_TYPE_SIZE; i += 8)
  760. ret += __popcount_tab[(x >> i) & 0xff];
  761. return ret;
  762. #endif
  763. }
  764. #endif
  765. #ifdef L_paritysi2
  766. #undef int
  767. int
  768. __paritySI2 (UWtype x)
  769. {
  770. #if W_TYPE_SIZE > 64
  771. # error "fill out the table"
  772. #endif
  773. #if W_TYPE_SIZE > 32
  774. x ^= x >> 32;
  775. #endif
  776. #if W_TYPE_SIZE > 16
  777. x ^= x >> 16;
  778. #endif
  779. x ^= x >> 8;
  780. x ^= x >> 4;
  781. x &= 0xf;
  782. return (0x6996 >> x) & 1;
  783. }
  784. #endif
  785. #ifdef L_paritydi2
  786. #undef int
  787. int
  788. __parityDI2 (UDWtype x)
  789. {
  790. const DWunion uu = {.ll = x};
  791. UWtype nx = uu.s.low ^ uu.s.high;
  792. #if W_TYPE_SIZE > 64
  793. # error "fill out the table"
  794. #endif
  795. #if W_TYPE_SIZE > 32
  796. nx ^= nx >> 32;
  797. #endif
  798. #if W_TYPE_SIZE > 16
  799. nx ^= nx >> 16;
  800. #endif
  801. nx ^= nx >> 8;
  802. nx ^= nx >> 4;
  803. nx &= 0xf;
  804. return (0x6996 >> nx) & 1;
  805. }
  806. #endif
  807. #ifdef L_udivmoddi4
  808. #ifdef TARGET_HAS_NO_HW_DIVIDE
  809. #if (defined (L_udivdi3) || defined (L_divdi3) || \
  810. defined (L_umoddi3) || defined (L_moddi3))
  811. static inline __attribute__ ((__always_inline__))
  812. #endif
  813. UDWtype
  814. __udivmoddi4 (UDWtype n, UDWtype d, UDWtype *rp)
  815. {
  816. UDWtype q = 0, r = n, y = d;
  817. UWtype lz1, lz2, i, k;
  818. /* Implements align divisor shift dividend method. This algorithm
  819. aligns the divisor under the dividend and then perform number of
  820. test-subtract iterations which shift the dividend left. Number of
  821. iterations is k + 1 where k is the number of bit positions the
  822. divisor must be shifted left to align it under the dividend.
  823. quotient bits can be saved in the rightmost positions of the dividend
  824. as it shifts left on each test-subtract iteration. */
  825. if (y <= r)
  826. {
  827. lz1 = __builtin_clzll (d);
  828. lz2 = __builtin_clzll (n);
  829. k = lz1 - lz2;
  830. y = (y << k);
  831. /* Dividend can exceed 2 ^ (width − 1) − 1 but still be less than the
  832. aligned divisor. Normal iteration can drops the high order bit
  833. of the dividend. Therefore, first test-subtract iteration is a
  834. special case, saving its quotient bit in a separate location and
  835. not shifting the dividend. */
  836. if (r >= y)
  837. {
  838. r = r - y;
  839. q = (1ULL << k);
  840. }
  841. if (k > 0)
  842. {
  843. y = y >> 1;
  844. /* k additional iterations where k regular test subtract shift
  845. dividend iterations are done. */
  846. i = k;
  847. do
  848. {
  849. if (r >= y)
  850. r = ((r - y) << 1) + 1;
  851. else
  852. r = (r << 1);
  853. i = i - 1;
  854. } while (i != 0);
  855. /* First quotient bit is combined with the quotient bits resulting
  856. from the k regular iterations. */
  857. q = q + r;
  858. r = r >> k;
  859. q = q - (r << k);
  860. }
  861. }
  862. if (rp)
  863. *rp = r;
  864. return q;
  865. }
  866. #else
  867. #if (defined (L_udivdi3) || defined (L_divdi3) || \
  868. defined (L_umoddi3) || defined (L_moddi3))
  869. static inline __attribute__ ((__always_inline__))
  870. #endif
  871. UDWtype
  872. __udivmoddi4 (UDWtype n, UDWtype d, UDWtype *rp)
  873. {
  874. const DWunion nn = {.ll = n};
  875. const DWunion dd = {.ll = d};
  876. DWunion rr;
  877. UWtype d0, d1, n0, n1, n2;
  878. UWtype q0, q1;
  879. UWtype b, bm;
  880. d0 = dd.s.low;
  881. d1 = dd.s.high;
  882. n0 = nn.s.low;
  883. n1 = nn.s.high;
  884. #if !UDIV_NEEDS_NORMALIZATION
  885. if (d1 == 0)
  886. {
  887. if (d0 > n1)
  888. {
  889. /* 0q = nn / 0D */
  890. udiv_qrnnd (q0, n0, n1, n0, d0);
  891. q1 = 0;
  892. /* Remainder in n0. */
  893. }
  894. else
  895. {
  896. /* qq = NN / 0d */
  897. if (d0 == 0)
  898. d0 = 1 / d0; /* Divide intentionally by zero. */
  899. udiv_qrnnd (q1, n1, 0, n1, d0);
  900. udiv_qrnnd (q0, n0, n1, n0, d0);
  901. /* Remainder in n0. */
  902. }
  903. if (rp != 0)
  904. {
  905. rr.s.low = n0;
  906. rr.s.high = 0;
  907. *rp = rr.ll;
  908. }
  909. }
  910. #else /* UDIV_NEEDS_NORMALIZATION */
  911. if (d1 == 0)
  912. {
  913. if (d0 > n1)
  914. {
  915. /* 0q = nn / 0D */
  916. count_leading_zeros (bm, d0);
  917. if (bm != 0)
  918. {
  919. /* Normalize, i.e. make the most significant bit of the
  920. denominator set. */
  921. d0 = d0 << bm;
  922. n1 = (n1 << bm) | (n0 >> (W_TYPE_SIZE - bm));
  923. n0 = n0 << bm;
  924. }
  925. udiv_qrnnd (q0, n0, n1, n0, d0);
  926. q1 = 0;
  927. /* Remainder in n0 >> bm. */
  928. }
  929. else
  930. {
  931. /* qq = NN / 0d */
  932. if (d0 == 0)
  933. d0 = 1 / d0; /* Divide intentionally by zero. */
  934. count_leading_zeros (bm, d0);
  935. if (bm == 0)
  936. {
  937. /* From (n1 >= d0) /\ (the most significant bit of d0 is set),
  938. conclude (the most significant bit of n1 is set) /\ (the
  939. leading quotient digit q1 = 1).
  940. This special case is necessary, not an optimization.
  941. (Shifts counts of W_TYPE_SIZE are undefined.) */
  942. n1 -= d0;
  943. q1 = 1;
  944. }
  945. else
  946. {
  947. /* Normalize. */
  948. b = W_TYPE_SIZE - bm;
  949. d0 = d0 << bm;
  950. n2 = n1 >> b;
  951. n1 = (n1 << bm) | (n0 >> b);
  952. n0 = n0 << bm;
  953. udiv_qrnnd (q1, n1, n2, n1, d0);
  954. }
  955. /* n1 != d0... */
  956. udiv_qrnnd (q0, n0, n1, n0, d0);
  957. /* Remainder in n0 >> bm. */
  958. }
  959. if (rp != 0)
  960. {
  961. rr.s.low = n0 >> bm;
  962. rr.s.high = 0;
  963. *rp = rr.ll;
  964. }
  965. }
  966. #endif /* UDIV_NEEDS_NORMALIZATION */
  967. else
  968. {
  969. if (d1 > n1)
  970. {
  971. /* 00 = nn / DD */
  972. q0 = 0;
  973. q1 = 0;
  974. /* Remainder in n1n0. */
  975. if (rp != 0)
  976. {
  977. rr.s.low = n0;
  978. rr.s.high = n1;
  979. *rp = rr.ll;
  980. }
  981. }
  982. else
  983. {
  984. /* 0q = NN / dd */
  985. count_leading_zeros (bm, d1);
  986. if (bm == 0)
  987. {
  988. /* From (n1 >= d1) /\ (the most significant bit of d1 is set),
  989. conclude (the most significant bit of n1 is set) /\ (the
  990. quotient digit q0 = 0 or 1).
  991. This special case is necessary, not an optimization. */
  992. /* The condition on the next line takes advantage of that
  993. n1 >= d1 (true due to program flow). */
  994. if (n1 > d1 || n0 >= d0)
  995. {
  996. q0 = 1;
  997. sub_ddmmss (n1, n0, n1, n0, d1, d0);
  998. }
  999. else
  1000. q0 = 0;
  1001. q1 = 0;
  1002. if (rp != 0)
  1003. {
  1004. rr.s.low = n0;
  1005. rr.s.high = n1;
  1006. *rp = rr.ll;
  1007. }
  1008. }
  1009. else
  1010. {
  1011. UWtype m1, m0;
  1012. /* Normalize. */
  1013. b = W_TYPE_SIZE - bm;
  1014. d1 = (d1 << bm) | (d0 >> b);
  1015. d0 = d0 << bm;
  1016. n2 = n1 >> b;
  1017. n1 = (n1 << bm) | (n0 >> b);
  1018. n0 = n0 << bm;
  1019. udiv_qrnnd (q0, n1, n2, n1, d1);
  1020. umul_ppmm (m1, m0, q0, d0);
  1021. if (m1 > n1 || (m1 == n1 && m0 > n0))
  1022. {
  1023. q0--;
  1024. sub_ddmmss (m1, m0, m1, m0, d1, d0);
  1025. }
  1026. q1 = 0;
  1027. /* Remainder in (n1n0 - m1m0) >> bm. */
  1028. if (rp != 0)
  1029. {
  1030. sub_ddmmss (n1, n0, n1, n0, m1, m0);
  1031. rr.s.low = (n1 << b) | (n0 >> bm);
  1032. rr.s.high = n1 >> bm;
  1033. *rp = rr.ll;
  1034. }
  1035. }
  1036. }
  1037. }
  1038. const DWunion ww = {{.low = q0, .high = q1}};
  1039. return ww.ll;
  1040. }
  1041. #endif
  1042. #endif
  1043. #ifdef L_divdi3
  1044. DWtype
  1045. __divdi3 (DWtype u, DWtype v)
  1046. {
  1047. Wtype c = 0;
  1048. DWunion uu = {.ll = u};
  1049. DWunion vv = {.ll = v};
  1050. DWtype w;
  1051. if (uu.s.high < 0)
  1052. c = ~c,
  1053. uu.ll = -uu.ll;
  1054. if (vv.s.high < 0)
  1055. c = ~c,
  1056. vv.ll = -vv.ll;
  1057. w = __udivmoddi4 (uu.ll, vv.ll, (UDWtype *) 0);
  1058. if (c)
  1059. w = -w;
  1060. return w;
  1061. }
  1062. #endif
  1063. #ifdef L_moddi3
  1064. DWtype
  1065. __moddi3 (DWtype u, DWtype v)
  1066. {
  1067. Wtype c = 0;
  1068. DWunion uu = {.ll = u};
  1069. DWunion vv = {.ll = v};
  1070. DWtype w;
  1071. if (uu.s.high < 0)
  1072. c = ~c,
  1073. uu.ll = -uu.ll;
  1074. if (vv.s.high < 0)
  1075. vv.ll = -vv.ll;
  1076. (void) __udivmoddi4 (uu.ll, vv.ll, (UDWtype*)&w);
  1077. if (c)
  1078. w = -w;
  1079. return w;
  1080. }
  1081. #endif
  1082. #ifdef L_umoddi3
  1083. UDWtype
  1084. __umoddi3 (UDWtype u, UDWtype v)
  1085. {
  1086. UDWtype w;
  1087. (void) __udivmoddi4 (u, v, &w);
  1088. return w;
  1089. }
  1090. #endif
  1091. #ifdef L_udivdi3
  1092. UDWtype
  1093. __udivdi3 (UDWtype n, UDWtype d)
  1094. {
  1095. return __udivmoddi4 (n, d, (UDWtype *) 0);
  1096. }
  1097. #endif
  1098. #ifdef L_cmpdi2
  1099. cmp_return_type
  1100. __cmpdi2 (DWtype a, DWtype b)
  1101. {
  1102. const DWunion au = {.ll = a};
  1103. const DWunion bu = {.ll = b};
  1104. if (au.s.high < bu.s.high)
  1105. return 0;
  1106. else if (au.s.high > bu.s.high)
  1107. return 2;
  1108. if ((UWtype) au.s.low < (UWtype) bu.s.low)
  1109. return 0;
  1110. else if ((UWtype) au.s.low > (UWtype) bu.s.low)
  1111. return 2;
  1112. return 1;
  1113. }
  1114. #endif
  1115. #ifdef L_ucmpdi2
  1116. cmp_return_type
  1117. __ucmpdi2 (DWtype a, DWtype b)
  1118. {
  1119. const DWunion au = {.ll = a};
  1120. const DWunion bu = {.ll = b};
  1121. if ((UWtype) au.s.high < (UWtype) bu.s.high)
  1122. return 0;
  1123. else if ((UWtype) au.s.high > (UWtype) bu.s.high)
  1124. return 2;
  1125. if ((UWtype) au.s.low < (UWtype) bu.s.low)
  1126. return 0;
  1127. else if ((UWtype) au.s.low > (UWtype) bu.s.low)
  1128. return 2;
  1129. return 1;
  1130. }
  1131. #endif
  1132. #if defined(L_fixunstfdi) && LIBGCC2_HAS_TF_MODE
  1133. UDWtype
  1134. __fixunstfDI (TFtype a)
  1135. {
  1136. if (a < 0)
  1137. return 0;
  1138. /* Compute high word of result, as a flonum. */
  1139. const TFtype b = (a / Wtype_MAXp1_F);
  1140. /* Convert that to fixed (but not to DWtype!),
  1141. and shift it into the high word. */
  1142. UDWtype v = (UWtype) b;
  1143. v <<= W_TYPE_SIZE;
  1144. /* Remove high part from the TFtype, leaving the low part as flonum. */
  1145. a -= (TFtype)v;
  1146. /* Convert that to fixed (but not to DWtype!) and add it in.
  1147. Sometimes A comes out negative. This is significant, since
  1148. A has more bits than a long int does. */
  1149. if (a < 0)
  1150. v -= (UWtype) (- a);
  1151. else
  1152. v += (UWtype) a;
  1153. return v;
  1154. }
  1155. #endif
  1156. #if defined(L_fixtfdi) && LIBGCC2_HAS_TF_MODE
  1157. DWtype
  1158. __fixtfdi (TFtype a)
  1159. {
  1160. if (a < 0)
  1161. return - __fixunstfDI (-a);
  1162. return __fixunstfDI (a);
  1163. }
  1164. #endif
  1165. #if defined(L_fixunsxfdi) && LIBGCC2_HAS_XF_MODE
  1166. UDWtype
  1167. __fixunsxfDI (XFtype a)
  1168. {
  1169. if (a < 0)
  1170. return 0;
  1171. /* Compute high word of result, as a flonum. */
  1172. const XFtype b = (a / Wtype_MAXp1_F);
  1173. /* Convert that to fixed (but not to DWtype!),
  1174. and shift it into the high word. */
  1175. UDWtype v = (UWtype) b;
  1176. v <<= W_TYPE_SIZE;
  1177. /* Remove high part from the XFtype, leaving the low part as flonum. */
  1178. a -= (XFtype)v;
  1179. /* Convert that to fixed (but not to DWtype!) and add it in.
  1180. Sometimes A comes out negative. This is significant, since
  1181. A has more bits than a long int does. */
  1182. if (a < 0)
  1183. v -= (UWtype) (- a);
  1184. else
  1185. v += (UWtype) a;
  1186. return v;
  1187. }
  1188. #endif
  1189. #if defined(L_fixxfdi) && LIBGCC2_HAS_XF_MODE
  1190. DWtype
  1191. __fixxfdi (XFtype a)
  1192. {
  1193. if (a < 0)
  1194. return - __fixunsxfDI (-a);
  1195. return __fixunsxfDI (a);
  1196. }
  1197. #endif
  1198. #if defined(L_fixunsdfdi) && LIBGCC2_HAS_DF_MODE
  1199. UDWtype
  1200. __fixunsdfDI (DFtype a)
  1201. {
  1202. /* Get high part of result. The division here will just moves the radix
  1203. point and will not cause any rounding. Then the conversion to integral
  1204. type chops result as desired. */
  1205. const UWtype hi = a / Wtype_MAXp1_F;
  1206. /* Get low part of result. Convert `hi' to floating type and scale it back,
  1207. then subtract this from the number being converted. This leaves the low
  1208. part. Convert that to integral type. */
  1209. const UWtype lo = a - (DFtype) hi * Wtype_MAXp1_F;
  1210. /* Assemble result from the two parts. */
  1211. return ((UDWtype) hi << W_TYPE_SIZE) | lo;
  1212. }
  1213. #endif
  1214. #if defined(L_fixdfdi) && LIBGCC2_HAS_DF_MODE
  1215. DWtype
  1216. __fixdfdi (DFtype a)
  1217. {
  1218. if (a < 0)
  1219. return - __fixunsdfDI (-a);
  1220. return __fixunsdfDI (a);
  1221. }
  1222. #endif
  1223. #if defined(L_fixunssfdi) && LIBGCC2_HAS_SF_MODE
  1224. UDWtype
  1225. __fixunssfDI (SFtype a)
  1226. {
  1227. #if LIBGCC2_HAS_DF_MODE
  1228. /* Convert the SFtype to a DFtype, because that is surely not going
  1229. to lose any bits. Some day someone else can write a faster version
  1230. that avoids converting to DFtype, and verify it really works right. */
  1231. const DFtype dfa = a;
  1232. /* Get high part of result. The division here will just moves the radix
  1233. point and will not cause any rounding. Then the conversion to integral
  1234. type chops result as desired. */
  1235. const UWtype hi = dfa / Wtype_MAXp1_F;
  1236. /* Get low part of result. Convert `hi' to floating type and scale it back,
  1237. then subtract this from the number being converted. This leaves the low
  1238. part. Convert that to integral type. */
  1239. const UWtype lo = dfa - (DFtype) hi * Wtype_MAXp1_F;
  1240. /* Assemble result from the two parts. */
  1241. return ((UDWtype) hi << W_TYPE_SIZE) | lo;
  1242. #elif FLT_MANT_DIG < W_TYPE_SIZE
  1243. if (a < 1)
  1244. return 0;
  1245. if (a < Wtype_MAXp1_F)
  1246. return (UWtype)a;
  1247. if (a < Wtype_MAXp1_F * Wtype_MAXp1_F)
  1248. {
  1249. /* Since we know that there are fewer significant bits in the SFmode
  1250. quantity than in a word, we know that we can convert out all the
  1251. significant bits in one step, and thus avoid losing bits. */
  1252. /* ??? This following loop essentially performs frexpf. If we could
  1253. use the real libm function, or poke at the actual bits of the fp
  1254. format, it would be significantly faster. */
  1255. UWtype shift = 0, counter;
  1256. SFtype msb;
  1257. a /= Wtype_MAXp1_F;
  1258. for (counter = W_TYPE_SIZE / 2; counter != 0; counter >>= 1)
  1259. {
  1260. SFtype counterf = (UWtype)1 << counter;
  1261. if (a >= counterf)
  1262. {
  1263. shift |= counter;
  1264. a /= counterf;
  1265. }
  1266. }
  1267. /* Rescale into the range of one word, extract the bits of that
  1268. one word, and shift the result into position. */
  1269. a *= Wtype_MAXp1_F;
  1270. counter = a;
  1271. return (DWtype)counter << shift;
  1272. }
  1273. return -1;
  1274. #else
  1275. # error
  1276. #endif
  1277. }
  1278. #endif
  1279. #if defined(L_fixsfdi) && LIBGCC2_HAS_SF_MODE
  1280. DWtype
  1281. __fixsfdi (SFtype a)
  1282. {
  1283. if (a < 0)
  1284. return - __fixunssfDI (-a);
  1285. return __fixunssfDI (a);
  1286. }
  1287. #endif
  1288. #if defined(L_floatdixf) && LIBGCC2_HAS_XF_MODE
  1289. XFtype
  1290. __floatdixf (DWtype u)
  1291. {
  1292. #if W_TYPE_SIZE > __LIBGCC_XF_MANT_DIG__
  1293. # error
  1294. #endif
  1295. XFtype d = (Wtype) (u >> W_TYPE_SIZE);
  1296. d *= Wtype_MAXp1_F;
  1297. d += (UWtype)u;
  1298. return d;
  1299. }
  1300. #endif
  1301. #if defined(L_floatundixf) && LIBGCC2_HAS_XF_MODE
  1302. XFtype
  1303. __floatundixf (UDWtype u)
  1304. {
  1305. #if W_TYPE_SIZE > __LIBGCC_XF_MANT_DIG__
  1306. # error
  1307. #endif
  1308. XFtype d = (UWtype) (u >> W_TYPE_SIZE);
  1309. d *= Wtype_MAXp1_F;
  1310. d += (UWtype)u;
  1311. return d;
  1312. }
  1313. #endif
  1314. #if defined(L_floatditf) && LIBGCC2_HAS_TF_MODE
  1315. TFtype
  1316. __floatditf (DWtype u)
  1317. {
  1318. #if W_TYPE_SIZE > __LIBGCC_TF_MANT_DIG__
  1319. # error
  1320. #endif
  1321. TFtype d = (Wtype) (u >> W_TYPE_SIZE);
  1322. d *= Wtype_MAXp1_F;
  1323. d += (UWtype)u;
  1324. return d;
  1325. }
  1326. #endif
  1327. #if defined(L_floatunditf) && LIBGCC2_HAS_TF_MODE
  1328. TFtype
  1329. __floatunditf (UDWtype u)
  1330. {
  1331. #if W_TYPE_SIZE > __LIBGCC_TF_MANT_DIG__
  1332. # error
  1333. #endif
  1334. TFtype d = (UWtype) (u >> W_TYPE_SIZE);
  1335. d *= Wtype_MAXp1_F;
  1336. d += (UWtype)u;
  1337. return d;
  1338. }
  1339. #endif
  1340. #if (defined(L_floatdisf) && LIBGCC2_HAS_SF_MODE) \
  1341. || (defined(L_floatdidf) && LIBGCC2_HAS_DF_MODE)
  1342. #define DI_SIZE (W_TYPE_SIZE * 2)
  1343. #define F_MODE_OK(SIZE) \
  1344. (SIZE < DI_SIZE \
  1345. && SIZE > (DI_SIZE - SIZE + FSSIZE) \
  1346. && !AVOID_FP_TYPE_CONVERSION(SIZE))
  1347. #if defined(L_floatdisf)
  1348. #define FUNC __floatdisf
  1349. #define FSTYPE SFtype
  1350. #define FSSIZE __LIBGCC_SF_MANT_DIG__
  1351. #else
  1352. #define FUNC __floatdidf
  1353. #define FSTYPE DFtype
  1354. #define FSSIZE __LIBGCC_DF_MANT_DIG__
  1355. #endif
  1356. FSTYPE
  1357. FUNC (DWtype u)
  1358. {
  1359. #if FSSIZE >= W_TYPE_SIZE
  1360. /* When the word size is small, we never get any rounding error. */
  1361. FSTYPE f = (Wtype) (u >> W_TYPE_SIZE);
  1362. f *= Wtype_MAXp1_F;
  1363. f += (UWtype)u;
  1364. return f;
  1365. #elif (LIBGCC2_HAS_DF_MODE && F_MODE_OK (__LIBGCC_DF_MANT_DIG__)) \
  1366. || (LIBGCC2_HAS_XF_MODE && F_MODE_OK (__LIBGCC_XF_MANT_DIG__)) \
  1367. || (LIBGCC2_HAS_TF_MODE && F_MODE_OK (__LIBGCC_TF_MANT_DIG__))
  1368. #if (LIBGCC2_HAS_DF_MODE && F_MODE_OK (__LIBGCC_DF_MANT_DIG__))
  1369. # define FSIZE __LIBGCC_DF_MANT_DIG__
  1370. # define FTYPE DFtype
  1371. #elif (LIBGCC2_HAS_XF_MODE && F_MODE_OK (__LIBGCC_XF_MANT_DIG__))
  1372. # define FSIZE __LIBGCC_XF_MANT_DIG__
  1373. # define FTYPE XFtype
  1374. #elif (LIBGCC2_HAS_TF_MODE && F_MODE_OK (__LIBGCC_TF_MANT_DIG__))
  1375. # define FSIZE __LIBGCC_TF_MANT_DIG__
  1376. # define FTYPE TFtype
  1377. #else
  1378. # error
  1379. #endif
  1380. #define REP_BIT ((UDWtype) 1 << (DI_SIZE - FSIZE))
  1381. /* Protect against double-rounding error.
  1382. Represent any low-order bits, that might be truncated by a bit that
  1383. won't be lost. The bit can go in anywhere below the rounding position
  1384. of the FSTYPE. A fixed mask and bit position handles all usual
  1385. configurations. */
  1386. if (! (- ((DWtype) 1 << FSIZE) < u
  1387. && u < ((DWtype) 1 << FSIZE)))
  1388. {
  1389. if ((UDWtype) u & (REP_BIT - 1))
  1390. {
  1391. u &= ~ (REP_BIT - 1);
  1392. u |= REP_BIT;
  1393. }
  1394. }
  1395. /* Do the calculation in a wider type so that we don't lose any of
  1396. the precision of the high word while multiplying it. */
  1397. FTYPE f = (Wtype) (u >> W_TYPE_SIZE);
  1398. f *= Wtype_MAXp1_F;
  1399. f += (UWtype)u;
  1400. return (FSTYPE) f;
  1401. #else
  1402. #if FSSIZE >= W_TYPE_SIZE - 2
  1403. # error
  1404. #endif
  1405. /* Finally, the word size is larger than the number of bits in the
  1406. required FSTYPE, and we've got no suitable wider type. The only
  1407. way to avoid double rounding is to special case the
  1408. extraction. */
  1409. /* If there are no high bits set, fall back to one conversion. */
  1410. if ((Wtype)u == u)
  1411. return (FSTYPE)(Wtype)u;
  1412. /* Otherwise, find the power of two. */
  1413. Wtype hi = u >> W_TYPE_SIZE;
  1414. if (hi < 0)
  1415. hi = -(UWtype) hi;
  1416. UWtype count, shift;
  1417. count_leading_zeros (count, hi);
  1418. /* No leading bits means u == minimum. */
  1419. if (count == 0)
  1420. return -(Wtype_MAXp1_F * (Wtype_MAXp1_F / 2));
  1421. shift = 1 + W_TYPE_SIZE - count;
  1422. /* Shift down the most significant bits. */
  1423. hi = u >> shift;
  1424. /* If we lost any nonzero bits, set the lsb to ensure correct rounding. */
  1425. if ((UWtype)u << (W_TYPE_SIZE - shift))
  1426. hi |= 1;
  1427. /* Convert the one word of data, and rescale. */
  1428. FSTYPE f = hi, e;
  1429. if (shift == W_TYPE_SIZE)
  1430. e = Wtype_MAXp1_F;
  1431. /* The following two cases could be merged if we knew that the target
  1432. supported a native unsigned->float conversion. More often, we only
  1433. have a signed conversion, and have to add extra fixup code. */
  1434. else if (shift == W_TYPE_SIZE - 1)
  1435. e = Wtype_MAXp1_F / 2;
  1436. else
  1437. e = (Wtype)1 << shift;
  1438. return f * e;
  1439. #endif
  1440. }
  1441. #endif
  1442. #if (defined(L_floatundisf) && LIBGCC2_HAS_SF_MODE) \
  1443. || (defined(L_floatundidf) && LIBGCC2_HAS_DF_MODE)
  1444. #define DI_SIZE (W_TYPE_SIZE * 2)
  1445. #define F_MODE_OK(SIZE) \
  1446. (SIZE < DI_SIZE \
  1447. && SIZE > (DI_SIZE - SIZE + FSSIZE) \
  1448. && !AVOID_FP_TYPE_CONVERSION(SIZE))
  1449. #if defined(L_floatundisf)
  1450. #define FUNC __floatundisf
  1451. #define FSTYPE SFtype
  1452. #define FSSIZE __LIBGCC_SF_MANT_DIG__
  1453. #else
  1454. #define FUNC __floatundidf
  1455. #define FSTYPE DFtype
  1456. #define FSSIZE __LIBGCC_DF_MANT_DIG__
  1457. #endif
  1458. FSTYPE
  1459. FUNC (UDWtype u)
  1460. {
  1461. #if FSSIZE >= W_TYPE_SIZE
  1462. /* When the word size is small, we never get any rounding error. */
  1463. FSTYPE f = (UWtype) (u >> W_TYPE_SIZE);
  1464. f *= Wtype_MAXp1_F;
  1465. f += (UWtype)u;
  1466. return f;
  1467. #elif (LIBGCC2_HAS_DF_MODE && F_MODE_OK (__LIBGCC_DF_MANT_DIG__)) \
  1468. || (LIBGCC2_HAS_XF_MODE && F_MODE_OK (__LIBGCC_XF_MANT_DIG__)) \
  1469. || (LIBGCC2_HAS_TF_MODE && F_MODE_OK (__LIBGCC_TF_MANT_DIG__))
  1470. #if (LIBGCC2_HAS_DF_MODE && F_MODE_OK (__LIBGCC_DF_MANT_DIG__))
  1471. # define FSIZE __LIBGCC_DF_MANT_DIG__
  1472. # define FTYPE DFtype
  1473. #elif (LIBGCC2_HAS_XF_MODE && F_MODE_OK (__LIBGCC_XF_MANT_DIG__))
  1474. # define FSIZE __LIBGCC_XF_MANT_DIG__
  1475. # define FTYPE XFtype
  1476. #elif (LIBGCC2_HAS_TF_MODE && F_MODE_OK (__LIBGCC_TF_MANT_DIG__))
  1477. # define FSIZE __LIBGCC_TF_MANT_DIG__
  1478. # define FTYPE TFtype
  1479. #else
  1480. # error
  1481. #endif
  1482. #define REP_BIT ((UDWtype) 1 << (DI_SIZE - FSIZE))
  1483. /* Protect against double-rounding error.
  1484. Represent any low-order bits, that might be truncated by a bit that
  1485. won't be lost. The bit can go in anywhere below the rounding position
  1486. of the FSTYPE. A fixed mask and bit position handles all usual
  1487. configurations. */
  1488. if (u >= ((UDWtype) 1 << FSIZE))
  1489. {
  1490. if ((UDWtype) u & (REP_BIT - 1))
  1491. {
  1492. u &= ~ (REP_BIT - 1);
  1493. u |= REP_BIT;
  1494. }
  1495. }
  1496. /* Do the calculation in a wider type so that we don't lose any of
  1497. the precision of the high word while multiplying it. */
  1498. FTYPE f = (UWtype) (u >> W_TYPE_SIZE);
  1499. f *= Wtype_MAXp1_F;
  1500. f += (UWtype)u;
  1501. return (FSTYPE) f;
  1502. #else
  1503. #if FSSIZE == W_TYPE_SIZE - 1
  1504. # error
  1505. #endif
  1506. /* Finally, the word size is larger than the number of bits in the
  1507. required FSTYPE, and we've got no suitable wider type. The only
  1508. way to avoid double rounding is to special case the
  1509. extraction. */
  1510. /* If there are no high bits set, fall back to one conversion. */
  1511. if ((UWtype)u == u)
  1512. return (FSTYPE)(UWtype)u;
  1513. /* Otherwise, find the power of two. */
  1514. UWtype hi = u >> W_TYPE_SIZE;
  1515. UWtype count, shift;
  1516. count_leading_zeros (count, hi);
  1517. shift = W_TYPE_SIZE - count;
  1518. /* Shift down the most significant bits. */
  1519. hi = u >> shift;
  1520. /* If we lost any nonzero bits, set the lsb to ensure correct rounding. */
  1521. if ((UWtype)u << (W_TYPE_SIZE - shift))
  1522. hi |= 1;
  1523. /* Convert the one word of data, and rescale. */
  1524. FSTYPE f = hi, e;
  1525. if (shift == W_TYPE_SIZE)
  1526. e = Wtype_MAXp1_F;
  1527. /* The following two cases could be merged if we knew that the target
  1528. supported a native unsigned->float conversion. More often, we only
  1529. have a signed conversion, and have to add extra fixup code. */
  1530. else if (shift == W_TYPE_SIZE - 1)
  1531. e = Wtype_MAXp1_F / 2;
  1532. else
  1533. e = (Wtype)1 << shift;
  1534. return f * e;
  1535. #endif
  1536. }
  1537. #endif
  1538. #if defined(L_fixunsxfsi) && LIBGCC2_HAS_XF_MODE
  1539. UWtype
  1540. __fixunsxfSI (XFtype a)
  1541. {
  1542. if (a >= - (DFtype) Wtype_MIN)
  1543. return (Wtype) (a + Wtype_MIN) - Wtype_MIN;
  1544. return (Wtype) a;
  1545. }
  1546. #endif
  1547. #if defined(L_fixunsdfsi) && LIBGCC2_HAS_DF_MODE
  1548. UWtype
  1549. __fixunsdfSI (DFtype a)
  1550. {
  1551. if (a >= - (DFtype) Wtype_MIN)
  1552. return (Wtype) (a + Wtype_MIN) - Wtype_MIN;
  1553. return (Wtype) a;
  1554. }
  1555. #endif
  1556. #if defined(L_fixunssfsi) && LIBGCC2_HAS_SF_MODE
  1557. UWtype
  1558. __fixunssfSI (SFtype a)
  1559. {
  1560. if (a >= - (SFtype) Wtype_MIN)
  1561. return (Wtype) (a + Wtype_MIN) - Wtype_MIN;
  1562. return (Wtype) a;
  1563. }
  1564. #endif
  1565. /* Integer power helper used from __builtin_powi for non-constant
  1566. exponents. */
  1567. #if (defined(L_powisf2) && LIBGCC2_HAS_SF_MODE) \
  1568. || (defined(L_powidf2) && LIBGCC2_HAS_DF_MODE) \
  1569. || (defined(L_powixf2) && LIBGCC2_HAS_XF_MODE) \
  1570. || (defined(L_powitf2) && LIBGCC2_HAS_TF_MODE)
  1571. # if defined(L_powisf2)
  1572. # define TYPE SFtype
  1573. # define NAME __powisf2
  1574. # elif defined(L_powidf2)
  1575. # define TYPE DFtype
  1576. # define NAME __powidf2
  1577. # elif defined(L_powixf2)
  1578. # define TYPE XFtype
  1579. # define NAME __powixf2
  1580. # elif defined(L_powitf2)
  1581. # define TYPE TFtype
  1582. # define NAME __powitf2
  1583. # endif
  1584. #undef int
  1585. #undef unsigned
  1586. TYPE
  1587. NAME (TYPE x, int m)
  1588. {
  1589. unsigned int n = m < 0 ? -m : m;
  1590. TYPE y = n % 2 ? x : 1;
  1591. while (n >>= 1)
  1592. {
  1593. x = x * x;
  1594. if (n % 2)
  1595. y = y * x;
  1596. }
  1597. return m < 0 ? 1/y : y;
  1598. }
  1599. #endif
  1600. #if ((defined(L_mulsc3) || defined(L_divsc3)) && LIBGCC2_HAS_SF_MODE) \
  1601. || ((defined(L_muldc3) || defined(L_divdc3)) && LIBGCC2_HAS_DF_MODE) \
  1602. || ((defined(L_mulxc3) || defined(L_divxc3)) && LIBGCC2_HAS_XF_MODE) \
  1603. || ((defined(L_multc3) || defined(L_divtc3)) && LIBGCC2_HAS_TF_MODE)
  1604. #undef float
  1605. #undef double
  1606. #undef long
  1607. #if defined(L_mulsc3) || defined(L_divsc3)
  1608. # define MTYPE SFtype
  1609. # define CTYPE SCtype
  1610. # define MODE sc
  1611. # define CEXT __LIBGCC_SF_FUNC_EXT__
  1612. # define NOTRUNC __LIBGCC_SF_EXCESS_PRECISION__
  1613. #elif defined(L_muldc3) || defined(L_divdc3)
  1614. # define MTYPE DFtype
  1615. # define CTYPE DCtype
  1616. # define MODE dc
  1617. # define CEXT __LIBGCC_DF_FUNC_EXT__
  1618. # define NOTRUNC __LIBGCC_DF_EXCESS_PRECISION__
  1619. #elif defined(L_mulxc3) || defined(L_divxc3)
  1620. # define MTYPE XFtype
  1621. # define CTYPE XCtype
  1622. # define MODE xc
  1623. # define CEXT __LIBGCC_XF_FUNC_EXT__
  1624. # define NOTRUNC __LIBGCC_XF_EXCESS_PRECISION__
  1625. #elif defined(L_multc3) || defined(L_divtc3)
  1626. # define MTYPE TFtype
  1627. # define CTYPE TCtype
  1628. # define MODE tc
  1629. # define CEXT __LIBGCC_TF_FUNC_EXT__
  1630. # define NOTRUNC __LIBGCC_TF_EXCESS_PRECISION__
  1631. #else
  1632. # error
  1633. #endif
  1634. #define CONCAT3(A,B,C) _CONCAT3(A,B,C)
  1635. #define _CONCAT3(A,B,C) A##B##C
  1636. #define CONCAT2(A,B) _CONCAT2(A,B)
  1637. #define _CONCAT2(A,B) A##B
  1638. /* All of these would be present in a full C99 implementation of <math.h>
  1639. and <complex.h>. Our problem is that only a few systems have such full
  1640. implementations. Further, libgcc_s.so isn't currently linked against
  1641. libm.so, and even for systems that do provide full C99, the extra overhead
  1642. of all programs using libgcc having to link against libm. So avoid it. */
  1643. #define isnan(x) __builtin_expect ((x) != (x), 0)
  1644. #define isfinite(x) __builtin_expect (!isnan((x) - (x)), 1)
  1645. #define isinf(x) __builtin_expect (!isnan(x) & !isfinite(x), 0)
  1646. #define INFINITY CONCAT2(__builtin_huge_val, CEXT) ()
  1647. #define I 1i
  1648. /* Helpers to make the following code slightly less gross. */
  1649. #define COPYSIGN CONCAT2(__builtin_copysign, CEXT)
  1650. #define FABS CONCAT2(__builtin_fabs, CEXT)
  1651. /* Verify that MTYPE matches up with CEXT. */
  1652. extern void *compile_type_assert[sizeof(INFINITY) == sizeof(MTYPE) ? 1 : -1];
  1653. /* Ensure that we've lost any extra precision. */
  1654. #if NOTRUNC
  1655. # define TRUNC(x)
  1656. #else
  1657. # define TRUNC(x) __asm__ ("" : "=m"(x) : "m"(x))
  1658. #endif
  1659. #if defined(L_mulsc3) || defined(L_muldc3) \
  1660. || defined(L_mulxc3) || defined(L_multc3)
  1661. CTYPE
  1662. CONCAT3(__mul,MODE,3) (MTYPE a, MTYPE b, MTYPE c, MTYPE d)
  1663. {
  1664. MTYPE ac, bd, ad, bc, x, y;
  1665. CTYPE res;
  1666. ac = a * c;
  1667. bd = b * d;
  1668. ad = a * d;
  1669. bc = b * c;
  1670. TRUNC (ac);
  1671. TRUNC (bd);
  1672. TRUNC (ad);
  1673. TRUNC (bc);
  1674. x = ac - bd;
  1675. y = ad + bc;
  1676. if (isnan (x) && isnan (y))
  1677. {
  1678. /* Recover infinities that computed as NaN + iNaN. */
  1679. _Bool recalc = 0;
  1680. if (isinf (a) || isinf (b))
  1681. {
  1682. /* z is infinite. "Box" the infinity and change NaNs in
  1683. the other factor to 0. */
  1684. a = COPYSIGN (isinf (a) ? 1 : 0, a);
  1685. b = COPYSIGN (isinf (b) ? 1 : 0, b);
  1686. if (isnan (c)) c = COPYSIGN (0, c);
  1687. if (isnan (d)) d = COPYSIGN (0, d);
  1688. recalc = 1;
  1689. }
  1690. if (isinf (c) || isinf (d))
  1691. {
  1692. /* w is infinite. "Box" the infinity and change NaNs in
  1693. the other factor to 0. */
  1694. c = COPYSIGN (isinf (c) ? 1 : 0, c);
  1695. d = COPYSIGN (isinf (d) ? 1 : 0, d);
  1696. if (isnan (a)) a = COPYSIGN (0, a);
  1697. if (isnan (b)) b = COPYSIGN (0, b);
  1698. recalc = 1;
  1699. }
  1700. if (!recalc
  1701. && (isinf (ac) || isinf (bd)
  1702. || isinf (ad) || isinf (bc)))
  1703. {
  1704. /* Recover infinities from overflow by changing NaNs to 0. */
  1705. if (isnan (a)) a = COPYSIGN (0, a);
  1706. if (isnan (b)) b = COPYSIGN (0, b);
  1707. if (isnan (c)) c = COPYSIGN (0, c);
  1708. if (isnan (d)) d = COPYSIGN (0, d);
  1709. recalc = 1;
  1710. }
  1711. if (recalc)
  1712. {
  1713. x = INFINITY * (a * c - b * d);
  1714. y = INFINITY * (a * d + b * c);
  1715. }
  1716. }
  1717. __real__ res = x;
  1718. __imag__ res = y;
  1719. return res;
  1720. }
  1721. #endif /* complex multiply */
  1722. #if defined(L_divsc3) || defined(L_divdc3) \
  1723. || defined(L_divxc3) || defined(L_divtc3)
  1724. CTYPE
  1725. CONCAT3(__div,MODE,3) (MTYPE a, MTYPE b, MTYPE c, MTYPE d)
  1726. {
  1727. MTYPE denom, ratio, x, y;
  1728. CTYPE res;
  1729. /* ??? We can get better behavior from logarithmic scaling instead of
  1730. the division. But that would mean starting to link libgcc against
  1731. libm. We could implement something akin to ldexp/frexp as gcc builtins
  1732. fairly easily... */
  1733. if (FABS (c) < FABS (d))
  1734. {
  1735. ratio = c / d;
  1736. denom = (c * ratio) + d;
  1737. x = ((a * ratio) + b) / denom;
  1738. y = ((b * ratio) - a) / denom;
  1739. }
  1740. else
  1741. {
  1742. ratio = d / c;
  1743. denom = (d * ratio) + c;
  1744. x = ((b * ratio) + a) / denom;
  1745. y = (b - (a * ratio)) / denom;
  1746. }
  1747. /* Recover infinities and zeros that computed as NaN+iNaN; the only cases
  1748. are nonzero/zero, infinite/finite, and finite/infinite. */
  1749. if (isnan (x) && isnan (y))
  1750. {
  1751. if (c == 0.0 && d == 0.0 && (!isnan (a) || !isnan (b)))
  1752. {
  1753. x = COPYSIGN (INFINITY, c) * a;
  1754. y = COPYSIGN (INFINITY, c) * b;
  1755. }
  1756. else if ((isinf (a) || isinf (b)) && isfinite (c) && isfinite (d))
  1757. {
  1758. a = COPYSIGN (isinf (a) ? 1 : 0, a);
  1759. b = COPYSIGN (isinf (b) ? 1 : 0, b);
  1760. x = INFINITY * (a * c + b * d);
  1761. y = INFINITY * (b * c - a * d);
  1762. }
  1763. else if ((isinf (c) || isinf (d)) && isfinite (a) && isfinite (b))
  1764. {
  1765. c = COPYSIGN (isinf (c) ? 1 : 0, c);
  1766. d = COPYSIGN (isinf (d) ? 1 : 0, d);
  1767. x = 0.0 * (a * c + b * d);
  1768. y = 0.0 * (b * c - a * d);
  1769. }
  1770. }
  1771. __real__ res = x;
  1772. __imag__ res = y;
  1773. return res;
  1774. }
  1775. #endif /* complex divide */
  1776. #endif /* all complex float routines */
  1777. /* From here on down, the routines use normal data types. */
  1778. #define SItype bogus_type
  1779. #define USItype bogus_type
  1780. #define DItype bogus_type
  1781. #define UDItype bogus_type
  1782. #define SFtype bogus_type
  1783. #define DFtype bogus_type
  1784. #undef Wtype
  1785. #undef UWtype
  1786. #undef HWtype
  1787. #undef UHWtype
  1788. #undef DWtype
  1789. #undef UDWtype
  1790. #undef char
  1791. #undef short
  1792. #undef int
  1793. #undef long
  1794. #undef unsigned
  1795. #undef float
  1796. #undef double
  1797. #ifdef L__gcc_bcmp
  1798. /* Like bcmp except the sign is meaningful.
  1799. Result is negative if S1 is less than S2,
  1800. positive if S1 is greater, 0 if S1 and S2 are equal. */
  1801. int
  1802. __gcc_bcmp (const unsigned char *s1, const unsigned char *s2, size_t size)
  1803. {
  1804. while (size > 0)
  1805. {
  1806. const unsigned char c1 = *s1++, c2 = *s2++;
  1807. if (c1 != c2)
  1808. return c1 - c2;
  1809. size--;
  1810. }
  1811. return 0;
  1812. }
  1813. #endif
  1814. /* __eprintf used to be used by GCC's private version of <assert.h>.
  1815. We no longer provide that header, but this routine remains in libgcc.a
  1816. for binary backward compatibility. Note that it is not included in
  1817. the shared version of libgcc. */
  1818. #ifdef L_eprintf
  1819. #ifndef inhibit_libc
  1820. #undef NULL /* Avoid errors if stdio.h and our stddef.h mismatch. */
  1821. #include <stdio.h>
  1822. void
  1823. __eprintf (const char *string, const char *expression,
  1824. unsigned int line, const char *filename)
  1825. {
  1826. fprintf (stderr, string, expression, line, filename);
  1827. fflush (stderr);
  1828. abort ();
  1829. }
  1830. #endif
  1831. #endif
  1832. #ifdef L_clear_cache
  1833. /* Clear part of an instruction cache. */
  1834. void
  1835. __clear_cache (char *beg __attribute__((__unused__)),
  1836. char *end __attribute__((__unused__)))
  1837. {
  1838. #ifdef CLEAR_INSN_CACHE
  1839. CLEAR_INSN_CACHE (beg, end);
  1840. #endif /* CLEAR_INSN_CACHE */
  1841. }
  1842. #endif /* L_clear_cache */
  1843. #ifdef L_trampoline
  1844. /* Jump to a trampoline, loading the static chain address. */
  1845. #if defined(WINNT) && ! defined(__CYGWIN__)
  1846. #include <windows.h>
  1847. int getpagesize (void);
  1848. int mprotect (char *,int, int);
  1849. int
  1850. getpagesize (void)
  1851. {
  1852. #ifdef _ALPHA_
  1853. return 8192;
  1854. #else
  1855. return 4096;
  1856. #endif
  1857. }
  1858. int
  1859. mprotect (char *addr, int len, int prot)
  1860. {
  1861. DWORD np, op;
  1862. if (prot == 7)
  1863. np = 0x40;
  1864. else if (prot == 5)
  1865. np = 0x20;
  1866. else if (prot == 4)
  1867. np = 0x10;
  1868. else if (prot == 3)
  1869. np = 0x04;
  1870. else if (prot == 1)
  1871. np = 0x02;
  1872. else if (prot == 0)
  1873. np = 0x01;
  1874. else
  1875. return -1;
  1876. if (VirtualProtect (addr, len, np, &op))
  1877. return 0;
  1878. else
  1879. return -1;
  1880. }
  1881. #endif /* WINNT && ! __CYGWIN__ */
  1882. #ifdef TRANSFER_FROM_TRAMPOLINE
  1883. TRANSFER_FROM_TRAMPOLINE
  1884. #endif
  1885. #endif /* L_trampoline */
  1886. #ifndef __CYGWIN__
  1887. #ifdef L__main
  1888. #include "gbl-ctors.h"
  1889. /* Some systems use __main in a way incompatible with its use in gcc, in these
  1890. cases use the macros NAME__MAIN to give a quoted symbol and SYMBOL__MAIN to
  1891. give the same symbol without quotes for an alternative entry point. You
  1892. must define both, or neither. */
  1893. #ifndef NAME__MAIN
  1894. #define NAME__MAIN "__main"
  1895. #define SYMBOL__MAIN __main
  1896. #endif
  1897. #if defined (__LIBGCC_INIT_SECTION_ASM_OP__) \
  1898. || defined (__LIBGCC_INIT_ARRAY_SECTION_ASM_OP__)
  1899. #undef HAS_INIT_SECTION
  1900. #define HAS_INIT_SECTION
  1901. #endif
  1902. #if !defined (HAS_INIT_SECTION) || !defined (OBJECT_FORMAT_ELF)
  1903. /* Some ELF crosses use crtstuff.c to provide __CTOR_LIST__, but use this
  1904. code to run constructors. In that case, we need to handle EH here, too. */
  1905. #ifdef __LIBGCC_EH_FRAME_SECTION_NAME__
  1906. #include "unwind-dw2-fde.h"
  1907. extern unsigned char __EH_FRAME_BEGIN__[];
  1908. #endif
  1909. /* Run all the global destructors on exit from the program. */
  1910. void
  1911. __do_global_dtors (void)
  1912. {
  1913. #ifdef DO_GLOBAL_DTORS_BODY
  1914. DO_GLOBAL_DTORS_BODY;
  1915. #else
  1916. static func_ptr *p = __DTOR_LIST__ + 1;
  1917. while (*p)
  1918. {
  1919. p++;
  1920. (*(p-1)) ();
  1921. }
  1922. #endif
  1923. #if defined (__LIBGCC_EH_FRAME_SECTION_NAME__) && !defined (HAS_INIT_SECTION)
  1924. {
  1925. static int completed = 0;
  1926. if (! completed)
  1927. {
  1928. completed = 1;
  1929. __deregister_frame_info (__EH_FRAME_BEGIN__);
  1930. }
  1931. }
  1932. #endif
  1933. }
  1934. #endif
  1935. #ifndef HAS_INIT_SECTION
  1936. /* Run all the global constructors on entry to the program. */
  1937. void
  1938. __do_global_ctors (void)
  1939. {
  1940. #ifdef __LIBGCC_EH_FRAME_SECTION_NAME__
  1941. {
  1942. static struct object object;
  1943. __register_frame_info (__EH_FRAME_BEGIN__, &object);
  1944. }
  1945. #endif
  1946. DO_GLOBAL_CTORS_BODY;
  1947. atexit (__do_global_dtors);
  1948. }
  1949. #endif /* no HAS_INIT_SECTION */
  1950. #if !defined (HAS_INIT_SECTION) || defined (INVOKE__main)
  1951. /* Subroutine called automatically by `main'.
  1952. Compiling a global function named `main'
  1953. produces an automatic call to this function at the beginning.
  1954. For many systems, this routine calls __do_global_ctors.
  1955. For systems which support a .init section we use the .init section
  1956. to run __do_global_ctors, so we need not do anything here. */
  1957. extern void SYMBOL__MAIN (void);
  1958. void
  1959. SYMBOL__MAIN (void)
  1960. {
  1961. /* Support recursive calls to `main': run initializers just once. */
  1962. static int initialized;
  1963. if (! initialized)
  1964. {
  1965. initialized = 1;
  1966. __do_global_ctors ();
  1967. }
  1968. }
  1969. #endif /* no HAS_INIT_SECTION or INVOKE__main */
  1970. #endif /* L__main */
  1971. #endif /* __CYGWIN__ */
  1972. #ifdef L_ctors
  1973. #include "gbl-ctors.h"
  1974. /* Provide default definitions for the lists of constructors and
  1975. destructors, so that we don't get linker errors. These symbols are
  1976. intentionally bss symbols, so that gld and/or collect will provide
  1977. the right values. */
  1978. /* We declare the lists here with two elements each,
  1979. so that they are valid empty lists if no other definition is loaded.
  1980. If we are using the old "set" extensions to have the gnu linker
  1981. collect ctors and dtors, then we __CTOR_LIST__ and __DTOR_LIST__
  1982. must be in the bss/common section.
  1983. Long term no port should use those extensions. But many still do. */
  1984. #if !defined(__LIBGCC_INIT_SECTION_ASM_OP__) \
  1985. && !defined(CTOR_LISTS_DEFINED_EXTERNALLY)
  1986. #if defined (TARGET_ASM_CONSTRUCTOR) || defined (USE_COLLECT2)
  1987. func_ptr __CTOR_LIST__[2] = {0, 0};
  1988. func_ptr __DTOR_LIST__[2] = {0, 0};
  1989. #else
  1990. func_ptr __CTOR_LIST__[2];
  1991. func_ptr __DTOR_LIST__[2];
  1992. #endif
  1993. #endif /* no __LIBGCC_INIT_SECTION_ASM_OP__ and not CTOR_LISTS_DEFINED_EXTERNALLY */
  1994. #endif /* L_ctors */
  1995. #endif /* LIBGCC2_UNITS_PER_WORD <= MIN_UNITS_PER_WORD */