libgcc2.c 54 KB

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