op-common.h 68 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134
  1. /* Software floating-point emulation. Common operations.
  2. Copyright (C) 1997-2017 Free Software Foundation, Inc.
  3. This file is part of the GNU C Library.
  4. Contributed by Richard Henderson (rth@cygnus.com),
  5. Jakub Jelinek (jj@ultra.linux.cz),
  6. David S. Miller (davem@redhat.com) and
  7. Peter Maydell (pmaydell@chiark.greenend.org.uk).
  8. The GNU C Library is free software; you can redistribute it and/or
  9. modify it under the terms of the GNU Lesser General Public
  10. License as published by the Free Software Foundation; either
  11. version 2.1 of the License, or (at your option) any later version.
  12. In addition to the permissions in the GNU Lesser General Public
  13. License, the Free Software Foundation gives you unlimited
  14. permission to link the compiled version of this file into
  15. combinations with other programs, and to distribute those
  16. combinations without any restriction coming from the use of this
  17. file. (The Lesser General Public License restrictions do apply in
  18. other respects; for example, they cover modification of the file,
  19. and distribution when not linked into a combine executable.)
  20. The GNU C Library is distributed in the hope that it will be useful,
  21. but WITHOUT ANY WARRANTY; without even the implied warranty of
  22. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  23. Lesser General Public License for more details.
  24. You should have received a copy of the GNU Lesser General Public
  25. License along with the GNU C Library; if not, see
  26. <http://www.gnu.org/licenses/>. */
  27. #ifndef SOFT_FP_OP_COMMON_H
  28. #define SOFT_FP_OP_COMMON_H 1
  29. #define _FP_DECL(wc, X) \
  30. _FP_I_TYPE X##_c __attribute__ ((unused)) _FP_ZERO_INIT; \
  31. _FP_I_TYPE X##_s __attribute__ ((unused)) _FP_ZERO_INIT; \
  32. _FP_I_TYPE X##_e __attribute__ ((unused)) _FP_ZERO_INIT; \
  33. _FP_FRAC_DECL_##wc (X)
  34. /* Test whether the qNaN bit denotes a signaling NaN. */
  35. #define _FP_FRAC_SNANP(fs, X) \
  36. ((_FP_QNANNEGATEDP) \
  37. ? (_FP_FRAC_HIGH_RAW_##fs (X) & _FP_QNANBIT_##fs) \
  38. : !(_FP_FRAC_HIGH_RAW_##fs (X) & _FP_QNANBIT_##fs))
  39. #define _FP_FRAC_SNANP_SEMIRAW(fs, X) \
  40. ((_FP_QNANNEGATEDP) \
  41. ? (_FP_FRAC_HIGH_##fs (X) & _FP_QNANBIT_SH_##fs) \
  42. : !(_FP_FRAC_HIGH_##fs (X) & _FP_QNANBIT_SH_##fs))
  43. /* Finish truly unpacking a native fp value by classifying the kind
  44. of fp value and normalizing both the exponent and the fraction. */
  45. #define _FP_UNPACK_CANONICAL(fs, wc, X) \
  46. do \
  47. { \
  48. switch (X##_e) \
  49. { \
  50. default: \
  51. _FP_FRAC_HIGH_RAW_##fs (X) |= _FP_IMPLBIT_##fs; \
  52. _FP_FRAC_SLL_##wc (X, _FP_WORKBITS); \
  53. X##_e -= _FP_EXPBIAS_##fs; \
  54. X##_c = FP_CLS_NORMAL; \
  55. break; \
  56. \
  57. case 0: \
  58. if (_FP_FRAC_ZEROP_##wc (X)) \
  59. X##_c = FP_CLS_ZERO; \
  60. else if (FP_DENORM_ZERO) \
  61. { \
  62. X##_c = FP_CLS_ZERO; \
  63. _FP_FRAC_SET_##wc (X, _FP_ZEROFRAC_##wc); \
  64. FP_SET_EXCEPTION (FP_EX_DENORM); \
  65. } \
  66. else \
  67. { \
  68. /* A denormalized number. */ \
  69. _FP_I_TYPE _FP_UNPACK_CANONICAL_shift; \
  70. _FP_FRAC_CLZ_##wc (_FP_UNPACK_CANONICAL_shift, \
  71. X); \
  72. _FP_UNPACK_CANONICAL_shift -= _FP_FRACXBITS_##fs; \
  73. _FP_FRAC_SLL_##wc (X, (_FP_UNPACK_CANONICAL_shift \
  74. + _FP_WORKBITS)); \
  75. X##_e -= (_FP_EXPBIAS_##fs - 1 \
  76. + _FP_UNPACK_CANONICAL_shift); \
  77. X##_c = FP_CLS_NORMAL; \
  78. FP_SET_EXCEPTION (FP_EX_DENORM); \
  79. } \
  80. break; \
  81. \
  82. case _FP_EXPMAX_##fs: \
  83. if (_FP_FRAC_ZEROP_##wc (X)) \
  84. X##_c = FP_CLS_INF; \
  85. else \
  86. { \
  87. X##_c = FP_CLS_NAN; \
  88. /* Check for signaling NaN. */ \
  89. if (_FP_FRAC_SNANP (fs, X)) \
  90. FP_SET_EXCEPTION (FP_EX_INVALID \
  91. | FP_EX_INVALID_SNAN); \
  92. } \
  93. break; \
  94. } \
  95. } \
  96. while (0)
  97. /* Finish unpacking an fp value in semi-raw mode: the mantissa is
  98. shifted by _FP_WORKBITS but the implicit MSB is not inserted and
  99. other classification is not done. */
  100. #define _FP_UNPACK_SEMIRAW(fs, wc, X) _FP_FRAC_SLL_##wc (X, _FP_WORKBITS)
  101. /* Check whether a raw or semi-raw input value should be flushed to
  102. zero, and flush it to zero if so. */
  103. #define _FP_CHECK_FLUSH_ZERO(fs, wc, X) \
  104. do \
  105. { \
  106. if (FP_DENORM_ZERO \
  107. && X##_e == 0 \
  108. && !_FP_FRAC_ZEROP_##wc (X)) \
  109. { \
  110. _FP_FRAC_SET_##wc (X, _FP_ZEROFRAC_##wc); \
  111. FP_SET_EXCEPTION (FP_EX_DENORM); \
  112. } \
  113. } \
  114. while (0)
  115. /* A semi-raw value has overflowed to infinity. Adjust the mantissa
  116. and exponent appropriately. */
  117. #define _FP_OVERFLOW_SEMIRAW(fs, wc, X) \
  118. do \
  119. { \
  120. if (FP_ROUNDMODE == FP_RND_NEAREST \
  121. || (FP_ROUNDMODE == FP_RND_PINF && !X##_s) \
  122. || (FP_ROUNDMODE == FP_RND_MINF && X##_s)) \
  123. { \
  124. X##_e = _FP_EXPMAX_##fs; \
  125. _FP_FRAC_SET_##wc (X, _FP_ZEROFRAC_##wc); \
  126. } \
  127. else \
  128. { \
  129. X##_e = _FP_EXPMAX_##fs - 1; \
  130. _FP_FRAC_SET_##wc (X, _FP_MAXFRAC_##wc); \
  131. } \
  132. FP_SET_EXCEPTION (FP_EX_INEXACT); \
  133. FP_SET_EXCEPTION (FP_EX_OVERFLOW); \
  134. } \
  135. while (0)
  136. /* Check for a semi-raw value being a signaling NaN and raise the
  137. invalid exception if so. */
  138. #define _FP_CHECK_SIGNAN_SEMIRAW(fs, wc, X) \
  139. do \
  140. { \
  141. if (X##_e == _FP_EXPMAX_##fs \
  142. && !_FP_FRAC_ZEROP_##wc (X) \
  143. && _FP_FRAC_SNANP_SEMIRAW (fs, X)) \
  144. FP_SET_EXCEPTION (FP_EX_INVALID | FP_EX_INVALID_SNAN); \
  145. } \
  146. while (0)
  147. /* Choose a NaN result from an operation on two semi-raw NaN
  148. values. */
  149. #define _FP_CHOOSENAN_SEMIRAW(fs, wc, R, X, Y, OP) \
  150. do \
  151. { \
  152. /* _FP_CHOOSENAN expects raw values, so shift as required. */ \
  153. _FP_FRAC_SRL_##wc (X, _FP_WORKBITS); \
  154. _FP_FRAC_SRL_##wc (Y, _FP_WORKBITS); \
  155. _FP_CHOOSENAN (fs, wc, R, X, Y, OP); \
  156. _FP_FRAC_SLL_##wc (R, _FP_WORKBITS); \
  157. } \
  158. while (0)
  159. /* Make the fractional part a quiet NaN, preserving the payload
  160. if possible, otherwise make it the canonical quiet NaN and set
  161. the sign bit accordingly. */
  162. #define _FP_SETQNAN(fs, wc, X) \
  163. do \
  164. { \
  165. if (_FP_QNANNEGATEDP) \
  166. { \
  167. _FP_FRAC_HIGH_RAW_##fs (X) &= _FP_QNANBIT_##fs - 1; \
  168. if (_FP_FRAC_ZEROP_##wc (X)) \
  169. { \
  170. X##_s = _FP_NANSIGN_##fs; \
  171. _FP_FRAC_SET_##wc (X, _FP_NANFRAC_##fs); \
  172. } \
  173. } \
  174. else \
  175. _FP_FRAC_HIGH_RAW_##fs (X) |= _FP_QNANBIT_##fs; \
  176. } \
  177. while (0)
  178. #define _FP_SETQNAN_SEMIRAW(fs, wc, X) \
  179. do \
  180. { \
  181. if (_FP_QNANNEGATEDP) \
  182. { \
  183. _FP_FRAC_HIGH_##fs (X) &= _FP_QNANBIT_SH_##fs - 1; \
  184. if (_FP_FRAC_ZEROP_##wc (X)) \
  185. { \
  186. X##_s = _FP_NANSIGN_##fs; \
  187. _FP_FRAC_SET_##wc (X, _FP_NANFRAC_##fs); \
  188. _FP_FRAC_SLL_##wc (X, _FP_WORKBITS); \
  189. } \
  190. } \
  191. else \
  192. _FP_FRAC_HIGH_##fs (X) |= _FP_QNANBIT_SH_##fs; \
  193. } \
  194. while (0)
  195. /* Test whether a biased exponent is normal (not zero or maximum). */
  196. #define _FP_EXP_NORMAL(fs, wc, X) (((X##_e + 1) & _FP_EXPMAX_##fs) > 1)
  197. /* Prepare to pack an fp value in semi-raw mode: the mantissa is
  198. rounded and shifted right, with the rounding possibly increasing
  199. the exponent (including changing a finite value to infinity). */
  200. #define _FP_PACK_SEMIRAW(fs, wc, X) \
  201. do \
  202. { \
  203. int _FP_PACK_SEMIRAW_is_tiny \
  204. = X##_e == 0 && !_FP_FRAC_ZEROP_##wc (X); \
  205. if (_FP_TININESS_AFTER_ROUNDING \
  206. && _FP_PACK_SEMIRAW_is_tiny) \
  207. { \
  208. FP_DECL_##fs (_FP_PACK_SEMIRAW_T); \
  209. _FP_FRAC_COPY_##wc (_FP_PACK_SEMIRAW_T, X); \
  210. _FP_PACK_SEMIRAW_T##_s = X##_s; \
  211. _FP_PACK_SEMIRAW_T##_e = X##_e; \
  212. _FP_FRAC_SLL_##wc (_FP_PACK_SEMIRAW_T, 1); \
  213. _FP_ROUND (wc, _FP_PACK_SEMIRAW_T); \
  214. if (_FP_FRAC_OVERP_##wc (fs, _FP_PACK_SEMIRAW_T)) \
  215. _FP_PACK_SEMIRAW_is_tiny = 0; \
  216. } \
  217. _FP_ROUND (wc, X); \
  218. if (_FP_PACK_SEMIRAW_is_tiny) \
  219. { \
  220. if ((FP_CUR_EXCEPTIONS & FP_EX_INEXACT) \
  221. || (FP_TRAPPING_EXCEPTIONS & FP_EX_UNDERFLOW)) \
  222. FP_SET_EXCEPTION (FP_EX_UNDERFLOW); \
  223. } \
  224. if (_FP_FRAC_HIGH_##fs (X) \
  225. & (_FP_OVERFLOW_##fs >> 1)) \
  226. { \
  227. _FP_FRAC_HIGH_##fs (X) &= ~(_FP_OVERFLOW_##fs >> 1); \
  228. X##_e++; \
  229. if (X##_e == _FP_EXPMAX_##fs) \
  230. _FP_OVERFLOW_SEMIRAW (fs, wc, X); \
  231. } \
  232. _FP_FRAC_SRL_##wc (X, _FP_WORKBITS); \
  233. if (X##_e == _FP_EXPMAX_##fs && !_FP_FRAC_ZEROP_##wc (X)) \
  234. { \
  235. if (!_FP_KEEPNANFRACP) \
  236. { \
  237. _FP_FRAC_SET_##wc (X, _FP_NANFRAC_##fs); \
  238. X##_s = _FP_NANSIGN_##fs; \
  239. } \
  240. else \
  241. _FP_SETQNAN (fs, wc, X); \
  242. } \
  243. } \
  244. while (0)
  245. /* Before packing the bits back into the native fp result, take care
  246. of such mundane things as rounding and overflow. Also, for some
  247. kinds of fp values, the original parts may not have been fully
  248. extracted -- but that is ok, we can regenerate them now. */
  249. #define _FP_PACK_CANONICAL(fs, wc, X) \
  250. do \
  251. { \
  252. switch (X##_c) \
  253. { \
  254. case FP_CLS_NORMAL: \
  255. X##_e += _FP_EXPBIAS_##fs; \
  256. if (X##_e > 0) \
  257. { \
  258. _FP_ROUND (wc, X); \
  259. if (_FP_FRAC_OVERP_##wc (fs, X)) \
  260. { \
  261. _FP_FRAC_CLEAR_OVERP_##wc (fs, X); \
  262. X##_e++; \
  263. } \
  264. _FP_FRAC_SRL_##wc (X, _FP_WORKBITS); \
  265. if (X##_e >= _FP_EXPMAX_##fs) \
  266. { \
  267. /* Overflow. */ \
  268. switch (FP_ROUNDMODE) \
  269. { \
  270. case FP_RND_NEAREST: \
  271. X##_c = FP_CLS_INF; \
  272. break; \
  273. case FP_RND_PINF: \
  274. if (!X##_s) \
  275. X##_c = FP_CLS_INF; \
  276. break; \
  277. case FP_RND_MINF: \
  278. if (X##_s) \
  279. X##_c = FP_CLS_INF; \
  280. break; \
  281. } \
  282. if (X##_c == FP_CLS_INF) \
  283. { \
  284. /* Overflow to infinity. */ \
  285. X##_e = _FP_EXPMAX_##fs; \
  286. _FP_FRAC_SET_##wc (X, _FP_ZEROFRAC_##wc); \
  287. } \
  288. else \
  289. { \
  290. /* Overflow to maximum normal. */ \
  291. X##_e = _FP_EXPMAX_##fs - 1; \
  292. _FP_FRAC_SET_##wc (X, _FP_MAXFRAC_##wc); \
  293. } \
  294. FP_SET_EXCEPTION (FP_EX_OVERFLOW); \
  295. FP_SET_EXCEPTION (FP_EX_INEXACT); \
  296. } \
  297. } \
  298. else \
  299. { \
  300. /* We've got a denormalized number. */ \
  301. int _FP_PACK_CANONICAL_is_tiny = 1; \
  302. if (_FP_TININESS_AFTER_ROUNDING && X##_e == 0) \
  303. { \
  304. FP_DECL_##fs (_FP_PACK_CANONICAL_T); \
  305. _FP_FRAC_COPY_##wc (_FP_PACK_CANONICAL_T, X); \
  306. _FP_PACK_CANONICAL_T##_s = X##_s; \
  307. _FP_PACK_CANONICAL_T##_e = X##_e; \
  308. _FP_ROUND (wc, _FP_PACK_CANONICAL_T); \
  309. if (_FP_FRAC_OVERP_##wc (fs, _FP_PACK_CANONICAL_T)) \
  310. _FP_PACK_CANONICAL_is_tiny = 0; \
  311. } \
  312. X##_e = -X##_e + 1; \
  313. if (X##_e <= _FP_WFRACBITS_##fs) \
  314. { \
  315. _FP_FRAC_SRS_##wc (X, X##_e, _FP_WFRACBITS_##fs); \
  316. _FP_ROUND (wc, X); \
  317. if (_FP_FRAC_HIGH_##fs (X) \
  318. & (_FP_OVERFLOW_##fs >> 1)) \
  319. { \
  320. X##_e = 1; \
  321. _FP_FRAC_SET_##wc (X, _FP_ZEROFRAC_##wc); \
  322. FP_SET_EXCEPTION (FP_EX_INEXACT); \
  323. } \
  324. else \
  325. { \
  326. X##_e = 0; \
  327. _FP_FRAC_SRL_##wc (X, _FP_WORKBITS); \
  328. } \
  329. if (_FP_PACK_CANONICAL_is_tiny \
  330. && ((FP_CUR_EXCEPTIONS & FP_EX_INEXACT) \
  331. || (FP_TRAPPING_EXCEPTIONS \
  332. & FP_EX_UNDERFLOW))) \
  333. FP_SET_EXCEPTION (FP_EX_UNDERFLOW); \
  334. } \
  335. else \
  336. { \
  337. /* Underflow to zero. */ \
  338. X##_e = 0; \
  339. if (!_FP_FRAC_ZEROP_##wc (X)) \
  340. { \
  341. _FP_FRAC_SET_##wc (X, _FP_MINFRAC_##wc); \
  342. _FP_ROUND (wc, X); \
  343. _FP_FRAC_LOW_##wc (X) >>= (_FP_WORKBITS); \
  344. } \
  345. FP_SET_EXCEPTION (FP_EX_UNDERFLOW); \
  346. } \
  347. } \
  348. break; \
  349. \
  350. case FP_CLS_ZERO: \
  351. X##_e = 0; \
  352. _FP_FRAC_SET_##wc (X, _FP_ZEROFRAC_##wc); \
  353. break; \
  354. \
  355. case FP_CLS_INF: \
  356. X##_e = _FP_EXPMAX_##fs; \
  357. _FP_FRAC_SET_##wc (X, _FP_ZEROFRAC_##wc); \
  358. break; \
  359. \
  360. case FP_CLS_NAN: \
  361. X##_e = _FP_EXPMAX_##fs; \
  362. if (!_FP_KEEPNANFRACP) \
  363. { \
  364. _FP_FRAC_SET_##wc (X, _FP_NANFRAC_##fs); \
  365. X##_s = _FP_NANSIGN_##fs; \
  366. } \
  367. else \
  368. _FP_SETQNAN (fs, wc, X); \
  369. break; \
  370. } \
  371. } \
  372. while (0)
  373. /* This one accepts raw argument and not cooked, returns
  374. 1 if X is a signaling NaN. */
  375. #define _FP_ISSIGNAN(fs, wc, X) \
  376. ({ \
  377. int _FP_ISSIGNAN_ret = 0; \
  378. if (X##_e == _FP_EXPMAX_##fs) \
  379. { \
  380. if (!_FP_FRAC_ZEROP_##wc (X) \
  381. && _FP_FRAC_SNANP (fs, X)) \
  382. _FP_ISSIGNAN_ret = 1; \
  383. } \
  384. _FP_ISSIGNAN_ret; \
  385. })
  386. /* Addition on semi-raw values. */
  387. #define _FP_ADD_INTERNAL(fs, wc, R, X, Y, OP) \
  388. do \
  389. { \
  390. _FP_CHECK_FLUSH_ZERO (fs, wc, X); \
  391. _FP_CHECK_FLUSH_ZERO (fs, wc, Y); \
  392. if (X##_s == Y##_s) \
  393. { \
  394. /* Addition. */ \
  395. __label__ add1, add2, add3, add_done; \
  396. R##_s = X##_s; \
  397. int _FP_ADD_INTERNAL_ediff = X##_e - Y##_e; \
  398. if (_FP_ADD_INTERNAL_ediff > 0) \
  399. { \
  400. R##_e = X##_e; \
  401. if (Y##_e == 0) \
  402. { \
  403. /* Y is zero or denormalized. */ \
  404. if (_FP_FRAC_ZEROP_##wc (Y)) \
  405. { \
  406. _FP_CHECK_SIGNAN_SEMIRAW (fs, wc, X); \
  407. _FP_FRAC_COPY_##wc (R, X); \
  408. goto add_done; \
  409. } \
  410. else \
  411. { \
  412. FP_SET_EXCEPTION (FP_EX_DENORM); \
  413. _FP_ADD_INTERNAL_ediff--; \
  414. if (_FP_ADD_INTERNAL_ediff == 0) \
  415. { \
  416. _FP_FRAC_ADD_##wc (R, X, Y); \
  417. goto add3; \
  418. } \
  419. if (X##_e == _FP_EXPMAX_##fs) \
  420. { \
  421. _FP_CHECK_SIGNAN_SEMIRAW (fs, wc, X); \
  422. _FP_FRAC_COPY_##wc (R, X); \
  423. goto add_done; \
  424. } \
  425. goto add1; \
  426. } \
  427. } \
  428. else if (X##_e == _FP_EXPMAX_##fs) \
  429. { \
  430. /* X is NaN or Inf, Y is normal. */ \
  431. _FP_CHECK_SIGNAN_SEMIRAW (fs, wc, X); \
  432. _FP_FRAC_COPY_##wc (R, X); \
  433. goto add_done; \
  434. } \
  435. \
  436. /* Insert implicit MSB of Y. */ \
  437. _FP_FRAC_HIGH_##fs (Y) |= _FP_IMPLBIT_SH_##fs; \
  438. \
  439. add1: \
  440. /* Shift the mantissa of Y to the right \
  441. _FP_ADD_INTERNAL_EDIFF steps; remember to account \
  442. later for the implicit MSB of X. */ \
  443. if (_FP_ADD_INTERNAL_ediff <= _FP_WFRACBITS_##fs) \
  444. _FP_FRAC_SRS_##wc (Y, _FP_ADD_INTERNAL_ediff, \
  445. _FP_WFRACBITS_##fs); \
  446. else if (!_FP_FRAC_ZEROP_##wc (Y)) \
  447. _FP_FRAC_SET_##wc (Y, _FP_MINFRAC_##wc); \
  448. _FP_FRAC_ADD_##wc (R, X, Y); \
  449. } \
  450. else if (_FP_ADD_INTERNAL_ediff < 0) \
  451. { \
  452. _FP_ADD_INTERNAL_ediff = -_FP_ADD_INTERNAL_ediff; \
  453. R##_e = Y##_e; \
  454. if (X##_e == 0) \
  455. { \
  456. /* X is zero or denormalized. */ \
  457. if (_FP_FRAC_ZEROP_##wc (X)) \
  458. { \
  459. _FP_CHECK_SIGNAN_SEMIRAW (fs, wc, Y); \
  460. _FP_FRAC_COPY_##wc (R, Y); \
  461. goto add_done; \
  462. } \
  463. else \
  464. { \
  465. FP_SET_EXCEPTION (FP_EX_DENORM); \
  466. _FP_ADD_INTERNAL_ediff--; \
  467. if (_FP_ADD_INTERNAL_ediff == 0) \
  468. { \
  469. _FP_FRAC_ADD_##wc (R, Y, X); \
  470. goto add3; \
  471. } \
  472. if (Y##_e == _FP_EXPMAX_##fs) \
  473. { \
  474. _FP_CHECK_SIGNAN_SEMIRAW (fs, wc, Y); \
  475. _FP_FRAC_COPY_##wc (R, Y); \
  476. goto add_done; \
  477. } \
  478. goto add2; \
  479. } \
  480. } \
  481. else if (Y##_e == _FP_EXPMAX_##fs) \
  482. { \
  483. /* Y is NaN or Inf, X is normal. */ \
  484. _FP_CHECK_SIGNAN_SEMIRAW (fs, wc, Y); \
  485. _FP_FRAC_COPY_##wc (R, Y); \
  486. goto add_done; \
  487. } \
  488. \
  489. /* Insert implicit MSB of X. */ \
  490. _FP_FRAC_HIGH_##fs (X) |= _FP_IMPLBIT_SH_##fs; \
  491. \
  492. add2: \
  493. /* Shift the mantissa of X to the right \
  494. _FP_ADD_INTERNAL_EDIFF steps; remember to account \
  495. later for the implicit MSB of Y. */ \
  496. if (_FP_ADD_INTERNAL_ediff <= _FP_WFRACBITS_##fs) \
  497. _FP_FRAC_SRS_##wc (X, _FP_ADD_INTERNAL_ediff, \
  498. _FP_WFRACBITS_##fs); \
  499. else if (!_FP_FRAC_ZEROP_##wc (X)) \
  500. _FP_FRAC_SET_##wc (X, _FP_MINFRAC_##wc); \
  501. _FP_FRAC_ADD_##wc (R, Y, X); \
  502. } \
  503. else \
  504. { \
  505. /* _FP_ADD_INTERNAL_ediff == 0. */ \
  506. if (!_FP_EXP_NORMAL (fs, wc, X)) \
  507. { \
  508. if (X##_e == 0) \
  509. { \
  510. /* X and Y are zero or denormalized. */ \
  511. R##_e = 0; \
  512. if (_FP_FRAC_ZEROP_##wc (X)) \
  513. { \
  514. if (!_FP_FRAC_ZEROP_##wc (Y)) \
  515. FP_SET_EXCEPTION (FP_EX_DENORM); \
  516. _FP_FRAC_COPY_##wc (R, Y); \
  517. goto add_done; \
  518. } \
  519. else if (_FP_FRAC_ZEROP_##wc (Y)) \
  520. { \
  521. FP_SET_EXCEPTION (FP_EX_DENORM); \
  522. _FP_FRAC_COPY_##wc (R, X); \
  523. goto add_done; \
  524. } \
  525. else \
  526. { \
  527. FP_SET_EXCEPTION (FP_EX_DENORM); \
  528. _FP_FRAC_ADD_##wc (R, X, Y); \
  529. if (_FP_FRAC_HIGH_##fs (R) & _FP_IMPLBIT_SH_##fs) \
  530. { \
  531. /* Normalized result. */ \
  532. _FP_FRAC_HIGH_##fs (R) \
  533. &= ~(_FP_W_TYPE) _FP_IMPLBIT_SH_##fs; \
  534. R##_e = 1; \
  535. } \
  536. goto add_done; \
  537. } \
  538. } \
  539. else \
  540. { \
  541. /* X and Y are NaN or Inf. */ \
  542. _FP_CHECK_SIGNAN_SEMIRAW (fs, wc, X); \
  543. _FP_CHECK_SIGNAN_SEMIRAW (fs, wc, Y); \
  544. R##_e = _FP_EXPMAX_##fs; \
  545. if (_FP_FRAC_ZEROP_##wc (X)) \
  546. _FP_FRAC_COPY_##wc (R, Y); \
  547. else if (_FP_FRAC_ZEROP_##wc (Y)) \
  548. _FP_FRAC_COPY_##wc (R, X); \
  549. else \
  550. _FP_CHOOSENAN_SEMIRAW (fs, wc, R, X, Y, OP); \
  551. goto add_done; \
  552. } \
  553. } \
  554. /* The exponents of X and Y, both normal, are equal. The \
  555. implicit MSBs will always add to increase the \
  556. exponent. */ \
  557. _FP_FRAC_ADD_##wc (R, X, Y); \
  558. R##_e = X##_e + 1; \
  559. _FP_FRAC_SRS_##wc (R, 1, _FP_WFRACBITS_##fs); \
  560. if (R##_e == _FP_EXPMAX_##fs) \
  561. /* Overflow to infinity (depending on rounding mode). */ \
  562. _FP_OVERFLOW_SEMIRAW (fs, wc, R); \
  563. goto add_done; \
  564. } \
  565. add3: \
  566. if (_FP_FRAC_HIGH_##fs (R) & _FP_IMPLBIT_SH_##fs) \
  567. { \
  568. /* Overflow. */ \
  569. _FP_FRAC_HIGH_##fs (R) &= ~(_FP_W_TYPE) _FP_IMPLBIT_SH_##fs; \
  570. R##_e++; \
  571. _FP_FRAC_SRS_##wc (R, 1, _FP_WFRACBITS_##fs); \
  572. if (R##_e == _FP_EXPMAX_##fs) \
  573. /* Overflow to infinity (depending on rounding mode). */ \
  574. _FP_OVERFLOW_SEMIRAW (fs, wc, R); \
  575. } \
  576. add_done: ; \
  577. } \
  578. else \
  579. { \
  580. /* Subtraction. */ \
  581. __label__ sub1, sub2, sub3, norm, sub_done; \
  582. int _FP_ADD_INTERNAL_ediff = X##_e - Y##_e; \
  583. if (_FP_ADD_INTERNAL_ediff > 0) \
  584. { \
  585. R##_e = X##_e; \
  586. R##_s = X##_s; \
  587. if (Y##_e == 0) \
  588. { \
  589. /* Y is zero or denormalized. */ \
  590. if (_FP_FRAC_ZEROP_##wc (Y)) \
  591. { \
  592. _FP_CHECK_SIGNAN_SEMIRAW (fs, wc, X); \
  593. _FP_FRAC_COPY_##wc (R, X); \
  594. goto sub_done; \
  595. } \
  596. else \
  597. { \
  598. FP_SET_EXCEPTION (FP_EX_DENORM); \
  599. _FP_ADD_INTERNAL_ediff--; \
  600. if (_FP_ADD_INTERNAL_ediff == 0) \
  601. { \
  602. _FP_FRAC_SUB_##wc (R, X, Y); \
  603. goto sub3; \
  604. } \
  605. if (X##_e == _FP_EXPMAX_##fs) \
  606. { \
  607. _FP_CHECK_SIGNAN_SEMIRAW (fs, wc, X); \
  608. _FP_FRAC_COPY_##wc (R, X); \
  609. goto sub_done; \
  610. } \
  611. goto sub1; \
  612. } \
  613. } \
  614. else if (X##_e == _FP_EXPMAX_##fs) \
  615. { \
  616. /* X is NaN or Inf, Y is normal. */ \
  617. _FP_CHECK_SIGNAN_SEMIRAW (fs, wc, X); \
  618. _FP_FRAC_COPY_##wc (R, X); \
  619. goto sub_done; \
  620. } \
  621. \
  622. /* Insert implicit MSB of Y. */ \
  623. _FP_FRAC_HIGH_##fs (Y) |= _FP_IMPLBIT_SH_##fs; \
  624. \
  625. sub1: \
  626. /* Shift the mantissa of Y to the right \
  627. _FP_ADD_INTERNAL_EDIFF steps; remember to account \
  628. later for the implicit MSB of X. */ \
  629. if (_FP_ADD_INTERNAL_ediff <= _FP_WFRACBITS_##fs) \
  630. _FP_FRAC_SRS_##wc (Y, _FP_ADD_INTERNAL_ediff, \
  631. _FP_WFRACBITS_##fs); \
  632. else if (!_FP_FRAC_ZEROP_##wc (Y)) \
  633. _FP_FRAC_SET_##wc (Y, _FP_MINFRAC_##wc); \
  634. _FP_FRAC_SUB_##wc (R, X, Y); \
  635. } \
  636. else if (_FP_ADD_INTERNAL_ediff < 0) \
  637. { \
  638. _FP_ADD_INTERNAL_ediff = -_FP_ADD_INTERNAL_ediff; \
  639. R##_e = Y##_e; \
  640. R##_s = Y##_s; \
  641. if (X##_e == 0) \
  642. { \
  643. /* X is zero or denormalized. */ \
  644. if (_FP_FRAC_ZEROP_##wc (X)) \
  645. { \
  646. _FP_CHECK_SIGNAN_SEMIRAW (fs, wc, Y); \
  647. _FP_FRAC_COPY_##wc (R, Y); \
  648. goto sub_done; \
  649. } \
  650. else \
  651. { \
  652. FP_SET_EXCEPTION (FP_EX_DENORM); \
  653. _FP_ADD_INTERNAL_ediff--; \
  654. if (_FP_ADD_INTERNAL_ediff == 0) \
  655. { \
  656. _FP_FRAC_SUB_##wc (R, Y, X); \
  657. goto sub3; \
  658. } \
  659. if (Y##_e == _FP_EXPMAX_##fs) \
  660. { \
  661. _FP_CHECK_SIGNAN_SEMIRAW (fs, wc, Y); \
  662. _FP_FRAC_COPY_##wc (R, Y); \
  663. goto sub_done; \
  664. } \
  665. goto sub2; \
  666. } \
  667. } \
  668. else if (Y##_e == _FP_EXPMAX_##fs) \
  669. { \
  670. /* Y is NaN or Inf, X is normal. */ \
  671. _FP_CHECK_SIGNAN_SEMIRAW (fs, wc, Y); \
  672. _FP_FRAC_COPY_##wc (R, Y); \
  673. goto sub_done; \
  674. } \
  675. \
  676. /* Insert implicit MSB of X. */ \
  677. _FP_FRAC_HIGH_##fs (X) |= _FP_IMPLBIT_SH_##fs; \
  678. \
  679. sub2: \
  680. /* Shift the mantissa of X to the right \
  681. _FP_ADD_INTERNAL_EDIFF steps; remember to account \
  682. later for the implicit MSB of Y. */ \
  683. if (_FP_ADD_INTERNAL_ediff <= _FP_WFRACBITS_##fs) \
  684. _FP_FRAC_SRS_##wc (X, _FP_ADD_INTERNAL_ediff, \
  685. _FP_WFRACBITS_##fs); \
  686. else if (!_FP_FRAC_ZEROP_##wc (X)) \
  687. _FP_FRAC_SET_##wc (X, _FP_MINFRAC_##wc); \
  688. _FP_FRAC_SUB_##wc (R, Y, X); \
  689. } \
  690. else \
  691. { \
  692. /* ediff == 0. */ \
  693. if (!_FP_EXP_NORMAL (fs, wc, X)) \
  694. { \
  695. if (X##_e == 0) \
  696. { \
  697. /* X and Y are zero or denormalized. */ \
  698. R##_e = 0; \
  699. if (_FP_FRAC_ZEROP_##wc (X)) \
  700. { \
  701. _FP_FRAC_COPY_##wc (R, Y); \
  702. if (_FP_FRAC_ZEROP_##wc (Y)) \
  703. R##_s = (FP_ROUNDMODE == FP_RND_MINF); \
  704. else \
  705. { \
  706. FP_SET_EXCEPTION (FP_EX_DENORM); \
  707. R##_s = Y##_s; \
  708. } \
  709. goto sub_done; \
  710. } \
  711. else if (_FP_FRAC_ZEROP_##wc (Y)) \
  712. { \
  713. FP_SET_EXCEPTION (FP_EX_DENORM); \
  714. _FP_FRAC_COPY_##wc (R, X); \
  715. R##_s = X##_s; \
  716. goto sub_done; \
  717. } \
  718. else \
  719. { \
  720. FP_SET_EXCEPTION (FP_EX_DENORM); \
  721. _FP_FRAC_SUB_##wc (R, X, Y); \
  722. R##_s = X##_s; \
  723. if (_FP_FRAC_HIGH_##fs (R) & _FP_IMPLBIT_SH_##fs) \
  724. { \
  725. /* |X| < |Y|, negate result. */ \
  726. _FP_FRAC_SUB_##wc (R, Y, X); \
  727. R##_s = Y##_s; \
  728. } \
  729. else if (_FP_FRAC_ZEROP_##wc (R)) \
  730. R##_s = (FP_ROUNDMODE == FP_RND_MINF); \
  731. goto sub_done; \
  732. } \
  733. } \
  734. else \
  735. { \
  736. /* X and Y are NaN or Inf, of opposite signs. */ \
  737. _FP_CHECK_SIGNAN_SEMIRAW (fs, wc, X); \
  738. _FP_CHECK_SIGNAN_SEMIRAW (fs, wc, Y); \
  739. R##_e = _FP_EXPMAX_##fs; \
  740. if (_FP_FRAC_ZEROP_##wc (X)) \
  741. { \
  742. if (_FP_FRAC_ZEROP_##wc (Y)) \
  743. { \
  744. /* Inf - Inf. */ \
  745. R##_s = _FP_NANSIGN_##fs; \
  746. _FP_FRAC_SET_##wc (R, _FP_NANFRAC_##fs); \
  747. _FP_FRAC_SLL_##wc (R, _FP_WORKBITS); \
  748. FP_SET_EXCEPTION (FP_EX_INVALID \
  749. | FP_EX_INVALID_ISI); \
  750. } \
  751. else \
  752. { \
  753. /* Inf - NaN. */ \
  754. R##_s = Y##_s; \
  755. _FP_FRAC_COPY_##wc (R, Y); \
  756. } \
  757. } \
  758. else \
  759. { \
  760. if (_FP_FRAC_ZEROP_##wc (Y)) \
  761. { \
  762. /* NaN - Inf. */ \
  763. R##_s = X##_s; \
  764. _FP_FRAC_COPY_##wc (R, X); \
  765. } \
  766. else \
  767. { \
  768. /* NaN - NaN. */ \
  769. _FP_CHOOSENAN_SEMIRAW (fs, wc, R, X, Y, OP); \
  770. } \
  771. } \
  772. goto sub_done; \
  773. } \
  774. } \
  775. /* The exponents of X and Y, both normal, are equal. The \
  776. implicit MSBs cancel. */ \
  777. R##_e = X##_e; \
  778. _FP_FRAC_SUB_##wc (R, X, Y); \
  779. R##_s = X##_s; \
  780. if (_FP_FRAC_HIGH_##fs (R) & _FP_IMPLBIT_SH_##fs) \
  781. { \
  782. /* |X| < |Y|, negate result. */ \
  783. _FP_FRAC_SUB_##wc (R, Y, X); \
  784. R##_s = Y##_s; \
  785. } \
  786. else if (_FP_FRAC_ZEROP_##wc (R)) \
  787. { \
  788. R##_e = 0; \
  789. R##_s = (FP_ROUNDMODE == FP_RND_MINF); \
  790. goto sub_done; \
  791. } \
  792. goto norm; \
  793. } \
  794. sub3: \
  795. if (_FP_FRAC_HIGH_##fs (R) & _FP_IMPLBIT_SH_##fs) \
  796. { \
  797. int _FP_ADD_INTERNAL_diff; \
  798. /* Carry into most significant bit of larger one of X and Y, \
  799. canceling it; renormalize. */ \
  800. _FP_FRAC_HIGH_##fs (R) &= _FP_IMPLBIT_SH_##fs - 1; \
  801. norm: \
  802. _FP_FRAC_CLZ_##wc (_FP_ADD_INTERNAL_diff, R); \
  803. _FP_ADD_INTERNAL_diff -= _FP_WFRACXBITS_##fs; \
  804. _FP_FRAC_SLL_##wc (R, _FP_ADD_INTERNAL_diff); \
  805. if (R##_e <= _FP_ADD_INTERNAL_diff) \
  806. { \
  807. /* R is denormalized. */ \
  808. _FP_ADD_INTERNAL_diff \
  809. = _FP_ADD_INTERNAL_diff - R##_e + 1; \
  810. _FP_FRAC_SRS_##wc (R, _FP_ADD_INTERNAL_diff, \
  811. _FP_WFRACBITS_##fs); \
  812. R##_e = 0; \
  813. } \
  814. else \
  815. { \
  816. R##_e -= _FP_ADD_INTERNAL_diff; \
  817. _FP_FRAC_HIGH_##fs (R) &= ~(_FP_W_TYPE) _FP_IMPLBIT_SH_##fs; \
  818. } \
  819. } \
  820. sub_done: ; \
  821. } \
  822. } \
  823. while (0)
  824. #define _FP_ADD(fs, wc, R, X, Y) _FP_ADD_INTERNAL (fs, wc, R, X, Y, '+')
  825. #define _FP_SUB(fs, wc, R, X, Y) \
  826. do \
  827. { \
  828. if (!(Y##_e == _FP_EXPMAX_##fs && !_FP_FRAC_ZEROP_##wc (Y))) \
  829. Y##_s ^= 1; \
  830. _FP_ADD_INTERNAL (fs, wc, R, X, Y, '-'); \
  831. } \
  832. while (0)
  833. /* Main negation routine. The input value is raw. */
  834. #define _FP_NEG(fs, wc, R, X) \
  835. do \
  836. { \
  837. _FP_FRAC_COPY_##wc (R, X); \
  838. R##_e = X##_e; \
  839. R##_s = 1 ^ X##_s; \
  840. } \
  841. while (0)
  842. /* Main multiplication routine. The input values should be cooked. */
  843. #define _FP_MUL(fs, wc, R, X, Y) \
  844. do \
  845. { \
  846. R##_s = X##_s ^ Y##_s; \
  847. R##_e = X##_e + Y##_e + 1; \
  848. switch (_FP_CLS_COMBINE (X##_c, Y##_c)) \
  849. { \
  850. case _FP_CLS_COMBINE (FP_CLS_NORMAL, FP_CLS_NORMAL): \
  851. R##_c = FP_CLS_NORMAL; \
  852. \
  853. _FP_MUL_MEAT_##fs (R, X, Y); \
  854. \
  855. if (_FP_FRAC_OVERP_##wc (fs, R)) \
  856. _FP_FRAC_SRS_##wc (R, 1, _FP_WFRACBITS_##fs); \
  857. else \
  858. R##_e--; \
  859. break; \
  860. \
  861. case _FP_CLS_COMBINE (FP_CLS_NAN, FP_CLS_NAN): \
  862. _FP_CHOOSENAN (fs, wc, R, X, Y, '*'); \
  863. break; \
  864. \
  865. case _FP_CLS_COMBINE (FP_CLS_NAN, FP_CLS_NORMAL): \
  866. case _FP_CLS_COMBINE (FP_CLS_NAN, FP_CLS_INF): \
  867. case _FP_CLS_COMBINE (FP_CLS_NAN, FP_CLS_ZERO): \
  868. R##_s = X##_s; \
  869. /* FALLTHRU */ \
  870. \
  871. case _FP_CLS_COMBINE (FP_CLS_INF, FP_CLS_INF): \
  872. case _FP_CLS_COMBINE (FP_CLS_INF, FP_CLS_NORMAL): \
  873. case _FP_CLS_COMBINE (FP_CLS_ZERO, FP_CLS_NORMAL): \
  874. case _FP_CLS_COMBINE (FP_CLS_ZERO, FP_CLS_ZERO): \
  875. _FP_FRAC_COPY_##wc (R, X); \
  876. R##_c = X##_c; \
  877. break; \
  878. \
  879. case _FP_CLS_COMBINE (FP_CLS_NORMAL, FP_CLS_NAN): \
  880. case _FP_CLS_COMBINE (FP_CLS_INF, FP_CLS_NAN): \
  881. case _FP_CLS_COMBINE (FP_CLS_ZERO, FP_CLS_NAN): \
  882. R##_s = Y##_s; \
  883. /* FALLTHRU */ \
  884. \
  885. case _FP_CLS_COMBINE (FP_CLS_NORMAL, FP_CLS_INF): \
  886. case _FP_CLS_COMBINE (FP_CLS_NORMAL, FP_CLS_ZERO): \
  887. _FP_FRAC_COPY_##wc (R, Y); \
  888. R##_c = Y##_c; \
  889. break; \
  890. \
  891. case _FP_CLS_COMBINE (FP_CLS_INF, FP_CLS_ZERO): \
  892. case _FP_CLS_COMBINE (FP_CLS_ZERO, FP_CLS_INF): \
  893. R##_s = _FP_NANSIGN_##fs; \
  894. R##_c = FP_CLS_NAN; \
  895. _FP_FRAC_SET_##wc (R, _FP_NANFRAC_##fs); \
  896. FP_SET_EXCEPTION (FP_EX_INVALID | FP_EX_INVALID_IMZ); \
  897. break; \
  898. \
  899. default: \
  900. _FP_UNREACHABLE; \
  901. } \
  902. } \
  903. while (0)
  904. /* Fused multiply-add. The input values should be cooked. */
  905. #define _FP_FMA(fs, wc, dwc, R, X, Y, Z) \
  906. do \
  907. { \
  908. __label__ done_fma; \
  909. FP_DECL_##fs (_FP_FMA_T); \
  910. _FP_FMA_T##_s = X##_s ^ Y##_s; \
  911. _FP_FMA_T##_e = X##_e + Y##_e + 1; \
  912. switch (_FP_CLS_COMBINE (X##_c, Y##_c)) \
  913. { \
  914. case _FP_CLS_COMBINE (FP_CLS_NORMAL, FP_CLS_NORMAL): \
  915. switch (Z##_c) \
  916. { \
  917. case FP_CLS_INF: \
  918. case FP_CLS_NAN: \
  919. R##_s = Z##_s; \
  920. _FP_FRAC_COPY_##wc (R, Z); \
  921. R##_c = Z##_c; \
  922. break; \
  923. \
  924. case FP_CLS_ZERO: \
  925. R##_c = FP_CLS_NORMAL; \
  926. R##_s = _FP_FMA_T##_s; \
  927. R##_e = _FP_FMA_T##_e; \
  928. \
  929. _FP_MUL_MEAT_##fs (R, X, Y); \
  930. \
  931. if (_FP_FRAC_OVERP_##wc (fs, R)) \
  932. _FP_FRAC_SRS_##wc (R, 1, _FP_WFRACBITS_##fs); \
  933. else \
  934. R##_e--; \
  935. break; \
  936. \
  937. case FP_CLS_NORMAL:; \
  938. _FP_FRAC_DECL_##dwc (_FP_FMA_TD); \
  939. _FP_FRAC_DECL_##dwc (_FP_FMA_ZD); \
  940. _FP_FRAC_DECL_##dwc (_FP_FMA_RD); \
  941. _FP_MUL_MEAT_DW_##fs (_FP_FMA_TD, X, Y); \
  942. R##_e = _FP_FMA_T##_e; \
  943. int _FP_FMA_tsh \
  944. = _FP_FRAC_HIGHBIT_DW_##dwc (fs, _FP_FMA_TD) == 0; \
  945. _FP_FMA_T##_e -= _FP_FMA_tsh; \
  946. int _FP_FMA_ediff = _FP_FMA_T##_e - Z##_e; \
  947. if (_FP_FMA_ediff >= 0) \
  948. { \
  949. int _FP_FMA_shift \
  950. = _FP_WFRACBITS_##fs - _FP_FMA_tsh - _FP_FMA_ediff; \
  951. if (_FP_FMA_shift <= -_FP_WFRACBITS_##fs) \
  952. _FP_FRAC_SET_##dwc (_FP_FMA_ZD, _FP_MINFRAC_##dwc); \
  953. else \
  954. { \
  955. _FP_FRAC_COPY_##dwc##_##wc (_FP_FMA_ZD, Z); \
  956. if (_FP_FMA_shift < 0) \
  957. _FP_FRAC_SRS_##dwc (_FP_FMA_ZD, -_FP_FMA_shift, \
  958. _FP_WFRACBITS_DW_##fs); \
  959. else if (_FP_FMA_shift > 0) \
  960. _FP_FRAC_SLL_##dwc (_FP_FMA_ZD, _FP_FMA_shift); \
  961. } \
  962. R##_s = _FP_FMA_T##_s; \
  963. if (_FP_FMA_T##_s == Z##_s) \
  964. _FP_FRAC_ADD_##dwc (_FP_FMA_RD, _FP_FMA_TD, \
  965. _FP_FMA_ZD); \
  966. else \
  967. { \
  968. _FP_FRAC_SUB_##dwc (_FP_FMA_RD, _FP_FMA_TD, \
  969. _FP_FMA_ZD); \
  970. if (_FP_FRAC_NEGP_##dwc (_FP_FMA_RD)) \
  971. { \
  972. R##_s = Z##_s; \
  973. _FP_FRAC_SUB_##dwc (_FP_FMA_RD, _FP_FMA_ZD, \
  974. _FP_FMA_TD); \
  975. } \
  976. } \
  977. } \
  978. else \
  979. { \
  980. R##_e = Z##_e; \
  981. R##_s = Z##_s; \
  982. _FP_FRAC_COPY_##dwc##_##wc (_FP_FMA_ZD, Z); \
  983. _FP_FRAC_SLL_##dwc (_FP_FMA_ZD, _FP_WFRACBITS_##fs); \
  984. int _FP_FMA_shift = -_FP_FMA_ediff - _FP_FMA_tsh; \
  985. if (_FP_FMA_shift >= _FP_WFRACBITS_DW_##fs) \
  986. _FP_FRAC_SET_##dwc (_FP_FMA_TD, _FP_MINFRAC_##dwc); \
  987. else if (_FP_FMA_shift > 0) \
  988. _FP_FRAC_SRS_##dwc (_FP_FMA_TD, _FP_FMA_shift, \
  989. _FP_WFRACBITS_DW_##fs); \
  990. if (Z##_s == _FP_FMA_T##_s) \
  991. _FP_FRAC_ADD_##dwc (_FP_FMA_RD, _FP_FMA_ZD, \
  992. _FP_FMA_TD); \
  993. else \
  994. _FP_FRAC_SUB_##dwc (_FP_FMA_RD, _FP_FMA_ZD, \
  995. _FP_FMA_TD); \
  996. } \
  997. if (_FP_FRAC_ZEROP_##dwc (_FP_FMA_RD)) \
  998. { \
  999. if (_FP_FMA_T##_s == Z##_s) \
  1000. R##_s = Z##_s; \
  1001. else \
  1002. R##_s = (FP_ROUNDMODE == FP_RND_MINF); \
  1003. _FP_FRAC_SET_##wc (R, _FP_ZEROFRAC_##wc); \
  1004. R##_c = FP_CLS_ZERO; \
  1005. } \
  1006. else \
  1007. { \
  1008. int _FP_FMA_rlz; \
  1009. _FP_FRAC_CLZ_##dwc (_FP_FMA_rlz, _FP_FMA_RD); \
  1010. _FP_FMA_rlz -= _FP_WFRACXBITS_DW_##fs; \
  1011. R##_e -= _FP_FMA_rlz; \
  1012. int _FP_FMA_shift = _FP_WFRACBITS_##fs - _FP_FMA_rlz; \
  1013. if (_FP_FMA_shift > 0) \
  1014. _FP_FRAC_SRS_##dwc (_FP_FMA_RD, _FP_FMA_shift, \
  1015. _FP_WFRACBITS_DW_##fs); \
  1016. else if (_FP_FMA_shift < 0) \
  1017. _FP_FRAC_SLL_##dwc (_FP_FMA_RD, -_FP_FMA_shift); \
  1018. _FP_FRAC_COPY_##wc##_##dwc (R, _FP_FMA_RD); \
  1019. R##_c = FP_CLS_NORMAL; \
  1020. } \
  1021. break; \
  1022. } \
  1023. goto done_fma; \
  1024. \
  1025. case _FP_CLS_COMBINE (FP_CLS_NAN, FP_CLS_NAN): \
  1026. _FP_CHOOSENAN (fs, wc, _FP_FMA_T, X, Y, '*'); \
  1027. break; \
  1028. \
  1029. case _FP_CLS_COMBINE (FP_CLS_NAN, FP_CLS_NORMAL): \
  1030. case _FP_CLS_COMBINE (FP_CLS_NAN, FP_CLS_INF): \
  1031. case _FP_CLS_COMBINE (FP_CLS_NAN, FP_CLS_ZERO): \
  1032. _FP_FMA_T##_s = X##_s; \
  1033. /* FALLTHRU */ \
  1034. \
  1035. case _FP_CLS_COMBINE (FP_CLS_INF, FP_CLS_INF): \
  1036. case _FP_CLS_COMBINE (FP_CLS_INF, FP_CLS_NORMAL): \
  1037. case _FP_CLS_COMBINE (FP_CLS_ZERO, FP_CLS_NORMAL): \
  1038. case _FP_CLS_COMBINE (FP_CLS_ZERO, FP_CLS_ZERO): \
  1039. _FP_FRAC_COPY_##wc (_FP_FMA_T, X); \
  1040. _FP_FMA_T##_c = X##_c; \
  1041. break; \
  1042. \
  1043. case _FP_CLS_COMBINE (FP_CLS_NORMAL, FP_CLS_NAN): \
  1044. case _FP_CLS_COMBINE (FP_CLS_INF, FP_CLS_NAN): \
  1045. case _FP_CLS_COMBINE (FP_CLS_ZERO, FP_CLS_NAN): \
  1046. _FP_FMA_T##_s = Y##_s; \
  1047. /* FALLTHRU */ \
  1048. \
  1049. case _FP_CLS_COMBINE (FP_CLS_NORMAL, FP_CLS_INF): \
  1050. case _FP_CLS_COMBINE (FP_CLS_NORMAL, FP_CLS_ZERO): \
  1051. _FP_FRAC_COPY_##wc (_FP_FMA_T, Y); \
  1052. _FP_FMA_T##_c = Y##_c; \
  1053. break; \
  1054. \
  1055. case _FP_CLS_COMBINE (FP_CLS_INF, FP_CLS_ZERO): \
  1056. case _FP_CLS_COMBINE (FP_CLS_ZERO, FP_CLS_INF): \
  1057. _FP_FMA_T##_s = _FP_NANSIGN_##fs; \
  1058. _FP_FMA_T##_c = FP_CLS_NAN; \
  1059. _FP_FRAC_SET_##wc (_FP_FMA_T, _FP_NANFRAC_##fs); \
  1060. FP_SET_EXCEPTION (FP_EX_INVALID | FP_EX_INVALID_IMZ_FMA); \
  1061. break; \
  1062. \
  1063. default: \
  1064. _FP_UNREACHABLE; \
  1065. } \
  1066. \
  1067. /* T = X * Y is zero, infinity or NaN. */ \
  1068. switch (_FP_CLS_COMBINE (_FP_FMA_T##_c, Z##_c)) \
  1069. { \
  1070. case _FP_CLS_COMBINE (FP_CLS_NAN, FP_CLS_NAN): \
  1071. _FP_CHOOSENAN (fs, wc, R, _FP_FMA_T, Z, '+'); \
  1072. break; \
  1073. \
  1074. case _FP_CLS_COMBINE (FP_CLS_NAN, FP_CLS_NORMAL): \
  1075. case _FP_CLS_COMBINE (FP_CLS_NAN, FP_CLS_INF): \
  1076. case _FP_CLS_COMBINE (FP_CLS_NAN, FP_CLS_ZERO): \
  1077. case _FP_CLS_COMBINE (FP_CLS_INF, FP_CLS_NORMAL): \
  1078. case _FP_CLS_COMBINE (FP_CLS_INF, FP_CLS_ZERO): \
  1079. R##_s = _FP_FMA_T##_s; \
  1080. _FP_FRAC_COPY_##wc (R, _FP_FMA_T); \
  1081. R##_c = _FP_FMA_T##_c; \
  1082. break; \
  1083. \
  1084. case _FP_CLS_COMBINE (FP_CLS_INF, FP_CLS_NAN): \
  1085. case _FP_CLS_COMBINE (FP_CLS_ZERO, FP_CLS_NAN): \
  1086. case _FP_CLS_COMBINE (FP_CLS_ZERO, FP_CLS_NORMAL): \
  1087. case _FP_CLS_COMBINE (FP_CLS_ZERO, FP_CLS_INF): \
  1088. R##_s = Z##_s; \
  1089. _FP_FRAC_COPY_##wc (R, Z); \
  1090. R##_c = Z##_c; \
  1091. R##_e = Z##_e; \
  1092. break; \
  1093. \
  1094. case _FP_CLS_COMBINE (FP_CLS_INF, FP_CLS_INF): \
  1095. if (_FP_FMA_T##_s == Z##_s) \
  1096. { \
  1097. R##_s = Z##_s; \
  1098. _FP_FRAC_COPY_##wc (R, Z); \
  1099. R##_c = Z##_c; \
  1100. } \
  1101. else \
  1102. { \
  1103. R##_s = _FP_NANSIGN_##fs; \
  1104. R##_c = FP_CLS_NAN; \
  1105. _FP_FRAC_SET_##wc (R, _FP_NANFRAC_##fs); \
  1106. FP_SET_EXCEPTION (FP_EX_INVALID | FP_EX_INVALID_ISI); \
  1107. } \
  1108. break; \
  1109. \
  1110. case _FP_CLS_COMBINE (FP_CLS_ZERO, FP_CLS_ZERO): \
  1111. if (_FP_FMA_T##_s == Z##_s) \
  1112. R##_s = Z##_s; \
  1113. else \
  1114. R##_s = (FP_ROUNDMODE == FP_RND_MINF); \
  1115. _FP_FRAC_COPY_##wc (R, Z); \
  1116. R##_c = Z##_c; \
  1117. break; \
  1118. \
  1119. default: \
  1120. _FP_UNREACHABLE; \
  1121. } \
  1122. done_fma: ; \
  1123. } \
  1124. while (0)
  1125. /* Main division routine. The input values should be cooked. */
  1126. #define _FP_DIV(fs, wc, R, X, Y) \
  1127. do \
  1128. { \
  1129. R##_s = X##_s ^ Y##_s; \
  1130. R##_e = X##_e - Y##_e; \
  1131. switch (_FP_CLS_COMBINE (X##_c, Y##_c)) \
  1132. { \
  1133. case _FP_CLS_COMBINE (FP_CLS_NORMAL, FP_CLS_NORMAL): \
  1134. R##_c = FP_CLS_NORMAL; \
  1135. \
  1136. _FP_DIV_MEAT_##fs (R, X, Y); \
  1137. break; \
  1138. \
  1139. case _FP_CLS_COMBINE (FP_CLS_NAN, FP_CLS_NAN): \
  1140. _FP_CHOOSENAN (fs, wc, R, X, Y, '/'); \
  1141. break; \
  1142. \
  1143. case _FP_CLS_COMBINE (FP_CLS_NAN, FP_CLS_NORMAL): \
  1144. case _FP_CLS_COMBINE (FP_CLS_NAN, FP_CLS_INF): \
  1145. case _FP_CLS_COMBINE (FP_CLS_NAN, FP_CLS_ZERO): \
  1146. R##_s = X##_s; \
  1147. _FP_FRAC_COPY_##wc (R, X); \
  1148. R##_c = X##_c; \
  1149. break; \
  1150. \
  1151. case _FP_CLS_COMBINE (FP_CLS_NORMAL, FP_CLS_NAN): \
  1152. case _FP_CLS_COMBINE (FP_CLS_INF, FP_CLS_NAN): \
  1153. case _FP_CLS_COMBINE (FP_CLS_ZERO, FP_CLS_NAN): \
  1154. R##_s = Y##_s; \
  1155. _FP_FRAC_COPY_##wc (R, Y); \
  1156. R##_c = Y##_c; \
  1157. break; \
  1158. \
  1159. case _FP_CLS_COMBINE (FP_CLS_NORMAL, FP_CLS_INF): \
  1160. case _FP_CLS_COMBINE (FP_CLS_ZERO, FP_CLS_INF): \
  1161. case _FP_CLS_COMBINE (FP_CLS_ZERO, FP_CLS_NORMAL): \
  1162. R##_c = FP_CLS_ZERO; \
  1163. break; \
  1164. \
  1165. case _FP_CLS_COMBINE (FP_CLS_NORMAL, FP_CLS_ZERO): \
  1166. FP_SET_EXCEPTION (FP_EX_DIVZERO); \
  1167. /* FALLTHRU */ \
  1168. case _FP_CLS_COMBINE (FP_CLS_INF, FP_CLS_ZERO): \
  1169. case _FP_CLS_COMBINE (FP_CLS_INF, FP_CLS_NORMAL): \
  1170. R##_c = FP_CLS_INF; \
  1171. break; \
  1172. \
  1173. case _FP_CLS_COMBINE (FP_CLS_INF, FP_CLS_INF): \
  1174. case _FP_CLS_COMBINE (FP_CLS_ZERO, FP_CLS_ZERO): \
  1175. R##_s = _FP_NANSIGN_##fs; \
  1176. R##_c = FP_CLS_NAN; \
  1177. _FP_FRAC_SET_##wc (R, _FP_NANFRAC_##fs); \
  1178. FP_SET_EXCEPTION (FP_EX_INVALID \
  1179. | (X##_c == FP_CLS_INF \
  1180. ? FP_EX_INVALID_IDI \
  1181. : FP_EX_INVALID_ZDZ)); \
  1182. break; \
  1183. \
  1184. default: \
  1185. _FP_UNREACHABLE; \
  1186. } \
  1187. } \
  1188. while (0)
  1189. /* Helper for comparisons. EX is 0 not to raise exceptions, 1 to
  1190. raise exceptions for signaling NaN operands, 2 to raise exceptions
  1191. for all NaN operands. Conditionals are organized to allow the
  1192. compiler to optimize away code based on the value of EX. */
  1193. #define _FP_CMP_CHECK_NAN(fs, wc, X, Y, ex) \
  1194. do \
  1195. { \
  1196. /* The arguments are unordered, which may or may not result in \
  1197. an exception. */ \
  1198. if (ex) \
  1199. { \
  1200. /* At least some cases of unordered arguments result in \
  1201. exceptions; check whether this is one. */ \
  1202. if (FP_EX_INVALID_SNAN || FP_EX_INVALID_VC) \
  1203. { \
  1204. /* Check separately for each case of "invalid" \
  1205. exceptions. */ \
  1206. if ((ex) == 2) \
  1207. FP_SET_EXCEPTION (FP_EX_INVALID | FP_EX_INVALID_VC); \
  1208. if (_FP_ISSIGNAN (fs, wc, X) \
  1209. || _FP_ISSIGNAN (fs, wc, Y)) \
  1210. FP_SET_EXCEPTION (FP_EX_INVALID | FP_EX_INVALID_SNAN); \
  1211. } \
  1212. /* Otherwise, we only need to check whether to raise an \
  1213. exception, not which case or cases it is. */ \
  1214. else if ((ex) == 2 \
  1215. || _FP_ISSIGNAN (fs, wc, X) \
  1216. || _FP_ISSIGNAN (fs, wc, Y)) \
  1217. FP_SET_EXCEPTION (FP_EX_INVALID); \
  1218. } \
  1219. } \
  1220. while (0)
  1221. /* Helper for comparisons. If denormal operands would raise an
  1222. exception, check for them, and flush to zero as appropriate
  1223. (otherwise, we need only check and flush to zero if it might affect
  1224. the result, which is done later with _FP_CMP_CHECK_FLUSH_ZERO). */
  1225. #define _FP_CMP_CHECK_DENORM(fs, wc, X, Y) \
  1226. do \
  1227. { \
  1228. if (FP_EX_DENORM != 0) \
  1229. { \
  1230. /* We must ensure the correct exceptions are raised for \
  1231. denormal operands, even though this may not affect the \
  1232. result of the comparison. */ \
  1233. if (FP_DENORM_ZERO) \
  1234. { \
  1235. _FP_CHECK_FLUSH_ZERO (fs, wc, X); \
  1236. _FP_CHECK_FLUSH_ZERO (fs, wc, Y); \
  1237. } \
  1238. else \
  1239. { \
  1240. if ((X##_e == 0 && !_FP_FRAC_ZEROP_##wc (X)) \
  1241. || (Y##_e == 0 && !_FP_FRAC_ZEROP_##wc (Y))) \
  1242. FP_SET_EXCEPTION (FP_EX_DENORM); \
  1243. } \
  1244. } \
  1245. } \
  1246. while (0)
  1247. /* Helper for comparisons. Check for flushing denormals for zero if
  1248. we didn't need to check earlier for any denormal operands. */
  1249. #define _FP_CMP_CHECK_FLUSH_ZERO(fs, wc, X, Y) \
  1250. do \
  1251. { \
  1252. if (FP_EX_DENORM == 0) \
  1253. { \
  1254. _FP_CHECK_FLUSH_ZERO (fs, wc, X); \
  1255. _FP_CHECK_FLUSH_ZERO (fs, wc, Y); \
  1256. } \
  1257. } \
  1258. while (0)
  1259. /* Main differential comparison routine. The inputs should be raw not
  1260. cooked. The return is -1, 0, 1 for normal values, UN
  1261. otherwise. */
  1262. #define _FP_CMP(fs, wc, ret, X, Y, un, ex) \
  1263. do \
  1264. { \
  1265. _FP_CMP_CHECK_DENORM (fs, wc, X, Y); \
  1266. /* NANs are unordered. */ \
  1267. if ((X##_e == _FP_EXPMAX_##fs && !_FP_FRAC_ZEROP_##wc (X)) \
  1268. || (Y##_e == _FP_EXPMAX_##fs && !_FP_FRAC_ZEROP_##wc (Y))) \
  1269. { \
  1270. (ret) = (un); \
  1271. _FP_CMP_CHECK_NAN (fs, wc, X, Y, (ex)); \
  1272. } \
  1273. else \
  1274. { \
  1275. int _FP_CMP_is_zero_x; \
  1276. int _FP_CMP_is_zero_y; \
  1277. \
  1278. _FP_CMP_CHECK_FLUSH_ZERO (fs, wc, X, Y); \
  1279. \
  1280. _FP_CMP_is_zero_x \
  1281. = (!X##_e && _FP_FRAC_ZEROP_##wc (X)) ? 1 : 0; \
  1282. _FP_CMP_is_zero_y \
  1283. = (!Y##_e && _FP_FRAC_ZEROP_##wc (Y)) ? 1 : 0; \
  1284. \
  1285. if (_FP_CMP_is_zero_x && _FP_CMP_is_zero_y) \
  1286. (ret) = 0; \
  1287. else if (_FP_CMP_is_zero_x) \
  1288. (ret) = Y##_s ? 1 : -1; \
  1289. else if (_FP_CMP_is_zero_y) \
  1290. (ret) = X##_s ? -1 : 1; \
  1291. else if (X##_s != Y##_s) \
  1292. (ret) = X##_s ? -1 : 1; \
  1293. else if (X##_e > Y##_e) \
  1294. (ret) = X##_s ? -1 : 1; \
  1295. else if (X##_e < Y##_e) \
  1296. (ret) = X##_s ? 1 : -1; \
  1297. else if (_FP_FRAC_GT_##wc (X, Y)) \
  1298. (ret) = X##_s ? -1 : 1; \
  1299. else if (_FP_FRAC_GT_##wc (Y, X)) \
  1300. (ret) = X##_s ? 1 : -1; \
  1301. else \
  1302. (ret) = 0; \
  1303. } \
  1304. } \
  1305. while (0)
  1306. /* Simplification for strict equality. */
  1307. #define _FP_CMP_EQ(fs, wc, ret, X, Y, ex) \
  1308. do \
  1309. { \
  1310. _FP_CMP_CHECK_DENORM (fs, wc, X, Y); \
  1311. /* NANs are unordered. */ \
  1312. if ((X##_e == _FP_EXPMAX_##fs && !_FP_FRAC_ZEROP_##wc (X)) \
  1313. || (Y##_e == _FP_EXPMAX_##fs && !_FP_FRAC_ZEROP_##wc (Y))) \
  1314. { \
  1315. (ret) = 1; \
  1316. _FP_CMP_CHECK_NAN (fs, wc, X, Y, (ex)); \
  1317. } \
  1318. else \
  1319. { \
  1320. _FP_CMP_CHECK_FLUSH_ZERO (fs, wc, X, Y); \
  1321. \
  1322. (ret) = !(X##_e == Y##_e \
  1323. && _FP_FRAC_EQ_##wc (X, Y) \
  1324. && (X##_s == Y##_s \
  1325. || (!X##_e && _FP_FRAC_ZEROP_##wc (X)))); \
  1326. } \
  1327. } \
  1328. while (0)
  1329. /* Version to test unordered. */
  1330. #define _FP_CMP_UNORD(fs, wc, ret, X, Y, ex) \
  1331. do \
  1332. { \
  1333. _FP_CMP_CHECK_DENORM (fs, wc, X, Y); \
  1334. (ret) = ((X##_e == _FP_EXPMAX_##fs && !_FP_FRAC_ZEROP_##wc (X)) \
  1335. || (Y##_e == _FP_EXPMAX_##fs && !_FP_FRAC_ZEROP_##wc (Y))); \
  1336. if (ret) \
  1337. _FP_CMP_CHECK_NAN (fs, wc, X, Y, (ex)); \
  1338. } \
  1339. while (0)
  1340. /* Main square root routine. The input value should be cooked. */
  1341. #define _FP_SQRT(fs, wc, R, X) \
  1342. do \
  1343. { \
  1344. _FP_FRAC_DECL_##wc (_FP_SQRT_T); \
  1345. _FP_FRAC_DECL_##wc (_FP_SQRT_S); \
  1346. _FP_W_TYPE _FP_SQRT_q; \
  1347. switch (X##_c) \
  1348. { \
  1349. case FP_CLS_NAN: \
  1350. _FP_FRAC_COPY_##wc (R, X); \
  1351. R##_s = X##_s; \
  1352. R##_c = FP_CLS_NAN; \
  1353. break; \
  1354. case FP_CLS_INF: \
  1355. if (X##_s) \
  1356. { \
  1357. R##_s = _FP_NANSIGN_##fs; \
  1358. R##_c = FP_CLS_NAN; /* NAN */ \
  1359. _FP_FRAC_SET_##wc (R, _FP_NANFRAC_##fs); \
  1360. FP_SET_EXCEPTION (FP_EX_INVALID | FP_EX_INVALID_SQRT); \
  1361. } \
  1362. else \
  1363. { \
  1364. R##_s = 0; \
  1365. R##_c = FP_CLS_INF; /* sqrt(+inf) = +inf */ \
  1366. } \
  1367. break; \
  1368. case FP_CLS_ZERO: \
  1369. R##_s = X##_s; \
  1370. R##_c = FP_CLS_ZERO; /* sqrt(+-0) = +-0 */ \
  1371. break; \
  1372. case FP_CLS_NORMAL: \
  1373. R##_s = 0; \
  1374. if (X##_s) \
  1375. { \
  1376. R##_c = FP_CLS_NAN; /* NAN */ \
  1377. R##_s = _FP_NANSIGN_##fs; \
  1378. _FP_FRAC_SET_##wc (R, _FP_NANFRAC_##fs); \
  1379. FP_SET_EXCEPTION (FP_EX_INVALID | FP_EX_INVALID_SQRT); \
  1380. break; \
  1381. } \
  1382. R##_c = FP_CLS_NORMAL; \
  1383. if (X##_e & 1) \
  1384. _FP_FRAC_SLL_##wc (X, 1); \
  1385. R##_e = X##_e >> 1; \
  1386. _FP_FRAC_SET_##wc (_FP_SQRT_S, _FP_ZEROFRAC_##wc); \
  1387. _FP_FRAC_SET_##wc (R, _FP_ZEROFRAC_##wc); \
  1388. _FP_SQRT_q = _FP_OVERFLOW_##fs >> 1; \
  1389. _FP_SQRT_MEAT_##wc (R, _FP_SQRT_S, _FP_SQRT_T, X, \
  1390. _FP_SQRT_q); \
  1391. } \
  1392. } \
  1393. while (0)
  1394. /* Convert from FP to integer. Input is raw. */
  1395. /* RSIGNED can have following values:
  1396. 0: the number is required to be 0..(2^rsize)-1, if not, NV is set plus
  1397. the result is either 0 or (2^rsize)-1 depending on the sign in such
  1398. case.
  1399. 1: the number is required to be -(2^(rsize-1))..(2^(rsize-1))-1, if not,
  1400. NV is set plus the result is either -(2^(rsize-1)) or (2^(rsize-1))-1
  1401. depending on the sign in such case.
  1402. 2: the number is required to be -(2^(rsize-1))..(2^(rsize-1))-1, if not,
  1403. NV is set plus the result is reduced modulo 2^rsize.
  1404. -1: the number is required to be -(2^(rsize-1))..(2^rsize)-1, if not, NV is
  1405. set plus the result is either -(2^(rsize-1)) or (2^(rsize-1))-1
  1406. depending on the sign in such case. */
  1407. #define _FP_TO_INT(fs, wc, r, X, rsize, rsigned) \
  1408. do \
  1409. { \
  1410. if (X##_e < _FP_EXPBIAS_##fs) \
  1411. { \
  1412. (r) = 0; \
  1413. if (X##_e == 0) \
  1414. { \
  1415. if (!_FP_FRAC_ZEROP_##wc (X)) \
  1416. { \
  1417. if (!FP_DENORM_ZERO) \
  1418. FP_SET_EXCEPTION (FP_EX_INEXACT); \
  1419. FP_SET_EXCEPTION (FP_EX_DENORM); \
  1420. } \
  1421. } \
  1422. else \
  1423. FP_SET_EXCEPTION (FP_EX_INEXACT); \
  1424. } \
  1425. else if ((rsigned) == 2 \
  1426. && (X##_e \
  1427. >= ((_FP_EXPMAX_##fs \
  1428. < _FP_EXPBIAS_##fs + _FP_FRACBITS_##fs + (rsize) - 1) \
  1429. ? _FP_EXPMAX_##fs \
  1430. : _FP_EXPBIAS_##fs + _FP_FRACBITS_##fs + (rsize) - 1))) \
  1431. { \
  1432. /* Overflow resulting in 0. */ \
  1433. (r) = 0; \
  1434. FP_SET_EXCEPTION (FP_EX_INVALID \
  1435. | FP_EX_INVALID_CVI \
  1436. | ((FP_EX_INVALID_SNAN \
  1437. && _FP_ISSIGNAN (fs, wc, X)) \
  1438. ? FP_EX_INVALID_SNAN \
  1439. : 0)); \
  1440. } \
  1441. else if ((rsigned) != 2 \
  1442. && (X##_e >= (_FP_EXPMAX_##fs < _FP_EXPBIAS_##fs + (rsize) \
  1443. ? _FP_EXPMAX_##fs \
  1444. : (_FP_EXPBIAS_##fs + (rsize) \
  1445. - ((rsigned) > 0 || X##_s))) \
  1446. || (!(rsigned) && X##_s))) \
  1447. { \
  1448. /* Overflow or converting to the most negative integer. */ \
  1449. if (rsigned) \
  1450. { \
  1451. (r) = 1; \
  1452. (r) <<= (rsize) - 1; \
  1453. (r) -= 1 - X##_s; \
  1454. } \
  1455. else \
  1456. { \
  1457. (r) = 0; \
  1458. if (!X##_s) \
  1459. (r) = ~(r); \
  1460. } \
  1461. \
  1462. if (_FP_EXPBIAS_##fs + (rsize) - 1 < _FP_EXPMAX_##fs \
  1463. && (rsigned) \
  1464. && X##_s \
  1465. && X##_e == _FP_EXPBIAS_##fs + (rsize) - 1) \
  1466. { \
  1467. /* Possibly converting to most negative integer; check the \
  1468. mantissa. */ \
  1469. int _FP_TO_INT_inexact = 0; \
  1470. (void) ((_FP_FRACBITS_##fs > (rsize)) \
  1471. ? ({ \
  1472. _FP_FRAC_SRST_##wc (X, _FP_TO_INT_inexact, \
  1473. _FP_FRACBITS_##fs - (rsize), \
  1474. _FP_FRACBITS_##fs); \
  1475. 0; \
  1476. }) \
  1477. : 0); \
  1478. if (!_FP_FRAC_ZEROP_##wc (X)) \
  1479. FP_SET_EXCEPTION (FP_EX_INVALID | FP_EX_INVALID_CVI); \
  1480. else if (_FP_TO_INT_inexact) \
  1481. FP_SET_EXCEPTION (FP_EX_INEXACT); \
  1482. } \
  1483. else \
  1484. FP_SET_EXCEPTION (FP_EX_INVALID \
  1485. | FP_EX_INVALID_CVI \
  1486. | ((FP_EX_INVALID_SNAN \
  1487. && _FP_ISSIGNAN (fs, wc, X)) \
  1488. ? FP_EX_INVALID_SNAN \
  1489. : 0)); \
  1490. } \
  1491. else \
  1492. { \
  1493. int _FP_TO_INT_inexact = 0; \
  1494. _FP_FRAC_HIGH_RAW_##fs (X) |= _FP_IMPLBIT_##fs; \
  1495. if (X##_e >= _FP_EXPBIAS_##fs + _FP_FRACBITS_##fs - 1) \
  1496. { \
  1497. _FP_FRAC_ASSEMBLE_##wc ((r), X, (rsize)); \
  1498. (r) <<= X##_e - _FP_EXPBIAS_##fs - _FP_FRACBITS_##fs + 1; \
  1499. } \
  1500. else \
  1501. { \
  1502. _FP_FRAC_SRST_##wc (X, _FP_TO_INT_inexact, \
  1503. (_FP_FRACBITS_##fs + _FP_EXPBIAS_##fs - 1 \
  1504. - X##_e), \
  1505. _FP_FRACBITS_##fs); \
  1506. _FP_FRAC_ASSEMBLE_##wc ((r), X, (rsize)); \
  1507. } \
  1508. if ((rsigned) && X##_s) \
  1509. (r) = -(r); \
  1510. if ((rsigned) == 2 && X##_e >= _FP_EXPBIAS_##fs + (rsize) - 1) \
  1511. { \
  1512. /* Overflow or converting to the most negative integer. */ \
  1513. if (X##_e > _FP_EXPBIAS_##fs + (rsize) - 1 \
  1514. || !X##_s \
  1515. || (r) != (((__typeof__ (r)) 1) << ((rsize) - 1))) \
  1516. { \
  1517. _FP_TO_INT_inexact = 0; \
  1518. FP_SET_EXCEPTION (FP_EX_INVALID | FP_EX_INVALID_CVI); \
  1519. } \
  1520. } \
  1521. if (_FP_TO_INT_inexact) \
  1522. FP_SET_EXCEPTION (FP_EX_INEXACT); \
  1523. } \
  1524. } \
  1525. while (0)
  1526. /* Convert from floating point to integer, rounding according to the
  1527. current rounding direction. Input is raw. RSIGNED is as for
  1528. _FP_TO_INT. */
  1529. #define _FP_TO_INT_ROUND(fs, wc, r, X, rsize, rsigned) \
  1530. do \
  1531. { \
  1532. __label__ _FP_TO_INT_ROUND_done; \
  1533. if (X##_e < _FP_EXPBIAS_##fs) \
  1534. { \
  1535. int _FP_TO_INT_ROUND_rounds_away = 0; \
  1536. if (X##_e == 0) \
  1537. { \
  1538. if (_FP_FRAC_ZEROP_##wc (X)) \
  1539. { \
  1540. (r) = 0; \
  1541. goto _FP_TO_INT_ROUND_done; \
  1542. } \
  1543. else \
  1544. { \
  1545. FP_SET_EXCEPTION (FP_EX_DENORM); \
  1546. if (FP_DENORM_ZERO) \
  1547. { \
  1548. (r) = 0; \
  1549. goto _FP_TO_INT_ROUND_done; \
  1550. } \
  1551. } \
  1552. } \
  1553. /* The result is 0, 1 or -1 depending on the rounding mode; \
  1554. -1 may cause overflow in the unsigned case. */ \
  1555. switch (FP_ROUNDMODE) \
  1556. { \
  1557. case FP_RND_NEAREST: \
  1558. _FP_TO_INT_ROUND_rounds_away \
  1559. = (X##_e == _FP_EXPBIAS_##fs - 1 \
  1560. && !_FP_FRAC_ZEROP_##wc (X)); \
  1561. break; \
  1562. case FP_RND_ZERO: \
  1563. /* _FP_TO_INT_ROUND_rounds_away is already 0. */ \
  1564. break; \
  1565. case FP_RND_PINF: \
  1566. _FP_TO_INT_ROUND_rounds_away = !X##_s; \
  1567. break; \
  1568. case FP_RND_MINF: \
  1569. _FP_TO_INT_ROUND_rounds_away = X##_s; \
  1570. break; \
  1571. } \
  1572. if ((rsigned) == 0 && _FP_TO_INT_ROUND_rounds_away && X##_s) \
  1573. { \
  1574. /* Result of -1 for an unsigned conversion. */ \
  1575. (r) = 0; \
  1576. FP_SET_EXCEPTION (FP_EX_INVALID | FP_EX_INVALID_CVI); \
  1577. } \
  1578. else if ((rsize) == 1 && (rsigned) > 0 \
  1579. && _FP_TO_INT_ROUND_rounds_away && !X##_s) \
  1580. { \
  1581. /* Converting to a 1-bit signed bit-field, which cannot \
  1582. represent +1. */ \
  1583. (r) = ((rsigned) == 2 ? -1 : 0); \
  1584. FP_SET_EXCEPTION (FP_EX_INVALID | FP_EX_INVALID_CVI); \
  1585. } \
  1586. else \
  1587. { \
  1588. (r) = (_FP_TO_INT_ROUND_rounds_away \
  1589. ? (X##_s ? -1 : 1) \
  1590. : 0); \
  1591. FP_SET_EXCEPTION (FP_EX_INEXACT); \
  1592. } \
  1593. } \
  1594. else if ((rsigned) == 2 \
  1595. && (X##_e \
  1596. >= ((_FP_EXPMAX_##fs \
  1597. < _FP_EXPBIAS_##fs + _FP_FRACBITS_##fs + (rsize) - 1) \
  1598. ? _FP_EXPMAX_##fs \
  1599. : _FP_EXPBIAS_##fs + _FP_FRACBITS_##fs + (rsize) - 1))) \
  1600. { \
  1601. /* Overflow resulting in 0. */ \
  1602. (r) = 0; \
  1603. FP_SET_EXCEPTION (FP_EX_INVALID \
  1604. | FP_EX_INVALID_CVI \
  1605. | ((FP_EX_INVALID_SNAN \
  1606. && _FP_ISSIGNAN (fs, wc, X)) \
  1607. ? FP_EX_INVALID_SNAN \
  1608. : 0)); \
  1609. } \
  1610. else if ((rsigned) != 2 \
  1611. && (X##_e >= (_FP_EXPMAX_##fs < _FP_EXPBIAS_##fs + (rsize) \
  1612. ? _FP_EXPMAX_##fs \
  1613. : (_FP_EXPBIAS_##fs + (rsize) \
  1614. - ((rsigned) > 0 && !X##_s))) \
  1615. || ((rsigned) == 0 && X##_s))) \
  1616. { \
  1617. /* Definite overflow (does not require rounding to tell). */ \
  1618. if ((rsigned) != 0) \
  1619. { \
  1620. (r) = 1; \
  1621. (r) <<= (rsize) - 1; \
  1622. (r) -= 1 - X##_s; \
  1623. } \
  1624. else \
  1625. { \
  1626. (r) = 0; \
  1627. if (!X##_s) \
  1628. (r) = ~(r); \
  1629. } \
  1630. \
  1631. FP_SET_EXCEPTION (FP_EX_INVALID \
  1632. | FP_EX_INVALID_CVI \
  1633. | ((FP_EX_INVALID_SNAN \
  1634. && _FP_ISSIGNAN (fs, wc, X)) \
  1635. ? FP_EX_INVALID_SNAN \
  1636. : 0)); \
  1637. } \
  1638. else \
  1639. { \
  1640. /* The value is finite, with magnitude at least 1. If \
  1641. the conversion is unsigned, the value is positive. \
  1642. If RSIGNED is not 2, the value does not definitely \
  1643. overflow by virtue of its exponent, but may still turn \
  1644. out to overflow after rounding; if RSIGNED is 2, the \
  1645. exponent may be such that the value definitely overflows, \
  1646. but at least one mantissa bit will not be shifted out. */ \
  1647. int _FP_TO_INT_ROUND_inexact = 0; \
  1648. _FP_FRAC_HIGH_RAW_##fs (X) |= _FP_IMPLBIT_##fs; \
  1649. if (X##_e >= _FP_EXPBIAS_##fs + _FP_FRACBITS_##fs - 1) \
  1650. { \
  1651. /* The value is an integer, no rounding needed. */ \
  1652. _FP_FRAC_ASSEMBLE_##wc ((r), X, (rsize)); \
  1653. (r) <<= X##_e - _FP_EXPBIAS_##fs - _FP_FRACBITS_##fs + 1; \
  1654. } \
  1655. else \
  1656. { \
  1657. /* May need to shift in order to round (unless there \
  1658. are exactly _FP_WORKBITS fractional bits already). */ \
  1659. int _FP_TO_INT_ROUND_rshift \
  1660. = (_FP_FRACBITS_##fs + _FP_EXPBIAS_##fs \
  1661. - 1 - _FP_WORKBITS - X##_e); \
  1662. if (_FP_TO_INT_ROUND_rshift > 0) \
  1663. _FP_FRAC_SRS_##wc (X, _FP_TO_INT_ROUND_rshift, \
  1664. _FP_WFRACBITS_##fs); \
  1665. else if (_FP_TO_INT_ROUND_rshift < 0) \
  1666. _FP_FRAC_SLL_##wc (X, -_FP_TO_INT_ROUND_rshift); \
  1667. /* Round like _FP_ROUND, but setting \
  1668. _FP_TO_INT_ROUND_inexact instead of directly setting \
  1669. the "inexact" exception, since it may turn out we \
  1670. should set "invalid" instead. */ \
  1671. if (_FP_FRAC_LOW_##wc (X) & 7) \
  1672. { \
  1673. _FP_TO_INT_ROUND_inexact = 1; \
  1674. switch (FP_ROUNDMODE) \
  1675. { \
  1676. case FP_RND_NEAREST: \
  1677. _FP_ROUND_NEAREST (wc, X); \
  1678. break; \
  1679. case FP_RND_ZERO: \
  1680. _FP_ROUND_ZERO (wc, X); \
  1681. break; \
  1682. case FP_RND_PINF: \
  1683. _FP_ROUND_PINF (wc, X); \
  1684. break; \
  1685. case FP_RND_MINF: \
  1686. _FP_ROUND_MINF (wc, X); \
  1687. break; \
  1688. } \
  1689. } \
  1690. _FP_FRAC_SRL_##wc (X, _FP_WORKBITS); \
  1691. _FP_FRAC_ASSEMBLE_##wc ((r), X, (rsize)); \
  1692. } \
  1693. if ((rsigned) != 0 && X##_s) \
  1694. (r) = -(r); \
  1695. /* An exponent of RSIZE - 1 always needs testing for \
  1696. overflow (either directly overflowing, or overflowing \
  1697. when rounding up results in 2^RSIZE). An exponent of \
  1698. RSIZE - 2 can overflow for positive values when rounding \
  1699. up to 2^(RSIZE-1), but cannot overflow for negative \
  1700. values. Smaller exponents cannot overflow. */ \
  1701. if (X##_e >= (_FP_EXPBIAS_##fs + (rsize) - 1 \
  1702. - ((rsigned) > 0 && !X##_s))) \
  1703. { \
  1704. if (X##_e > _FP_EXPBIAS_##fs + (rsize) - 1 \
  1705. || (X##_e == _FP_EXPBIAS_##fs + (rsize) - 1 \
  1706. && (X##_s \
  1707. ? (r) != (((__typeof__ (r)) 1) << ((rsize) - 1)) \
  1708. : ((rsigned) > 0 || (r) == 0))) \
  1709. || ((rsigned) > 0 \
  1710. && !X##_s \
  1711. && X##_e == _FP_EXPBIAS_##fs + (rsize) - 2 \
  1712. && (r) == (((__typeof__ (r)) 1) << ((rsize) - 1)))) \
  1713. { \
  1714. if ((rsigned) != 2) \
  1715. { \
  1716. if ((rsigned) != 0) \
  1717. { \
  1718. (r) = 1; \
  1719. (r) <<= (rsize) - 1; \
  1720. (r) -= 1 - X##_s; \
  1721. } \
  1722. else \
  1723. { \
  1724. (r) = 0; \
  1725. (r) = ~(r); \
  1726. } \
  1727. } \
  1728. _FP_TO_INT_ROUND_inexact = 0; \
  1729. FP_SET_EXCEPTION (FP_EX_INVALID | FP_EX_INVALID_CVI); \
  1730. } \
  1731. } \
  1732. if (_FP_TO_INT_ROUND_inexact) \
  1733. FP_SET_EXCEPTION (FP_EX_INEXACT); \
  1734. } \
  1735. _FP_TO_INT_ROUND_done: ; \
  1736. } \
  1737. while (0)
  1738. /* Convert integer to fp. Output is raw. RTYPE is unsigned even if
  1739. input is signed. */
  1740. #define _FP_FROM_INT(fs, wc, X, r, rsize, rtype) \
  1741. do \
  1742. { \
  1743. __label__ pack_semiraw; \
  1744. if (r) \
  1745. { \
  1746. rtype _FP_FROM_INT_ur = (r); \
  1747. \
  1748. if ((X##_s = ((r) < 0))) \
  1749. _FP_FROM_INT_ur = -_FP_FROM_INT_ur; \
  1750. \
  1751. _FP_STATIC_ASSERT ((rsize) <= 2 * _FP_W_TYPE_SIZE, \
  1752. "rsize too large"); \
  1753. (void) (((rsize) <= _FP_W_TYPE_SIZE) \
  1754. ? ({ \
  1755. int _FP_FROM_INT_lz; \
  1756. __FP_CLZ (_FP_FROM_INT_lz, \
  1757. (_FP_W_TYPE) _FP_FROM_INT_ur); \
  1758. X##_e = (_FP_EXPBIAS_##fs + _FP_W_TYPE_SIZE - 1 \
  1759. - _FP_FROM_INT_lz); \
  1760. }) \
  1761. : ({ \
  1762. int _FP_FROM_INT_lz; \
  1763. __FP_CLZ_2 (_FP_FROM_INT_lz, \
  1764. (_FP_W_TYPE) (_FP_FROM_INT_ur \
  1765. >> _FP_W_TYPE_SIZE), \
  1766. (_FP_W_TYPE) _FP_FROM_INT_ur); \
  1767. X##_e = (_FP_EXPBIAS_##fs + 2 * _FP_W_TYPE_SIZE - 1 \
  1768. - _FP_FROM_INT_lz); \
  1769. })); \
  1770. \
  1771. if ((rsize) - 1 + _FP_EXPBIAS_##fs >= _FP_EXPMAX_##fs \
  1772. && X##_e >= _FP_EXPMAX_##fs) \
  1773. { \
  1774. /* Exponent too big; overflow to infinity. (May also \
  1775. happen after rounding below.) */ \
  1776. _FP_OVERFLOW_SEMIRAW (fs, wc, X); \
  1777. goto pack_semiraw; \
  1778. } \
  1779. \
  1780. if ((rsize) <= _FP_FRACBITS_##fs \
  1781. || X##_e < _FP_EXPBIAS_##fs + _FP_FRACBITS_##fs) \
  1782. { \
  1783. /* Exactly representable; shift left. */ \
  1784. _FP_FRAC_DISASSEMBLE_##wc (X, _FP_FROM_INT_ur, (rsize)); \
  1785. if (_FP_EXPBIAS_##fs + _FP_FRACBITS_##fs - 1 - X##_e > 0) \
  1786. _FP_FRAC_SLL_##wc (X, (_FP_EXPBIAS_##fs \
  1787. + _FP_FRACBITS_##fs - 1 - X##_e)); \
  1788. } \
  1789. else \
  1790. { \
  1791. /* More bits in integer than in floating type; need to \
  1792. round. */ \
  1793. if (_FP_EXPBIAS_##fs + _FP_WFRACBITS_##fs - 1 < X##_e) \
  1794. _FP_FROM_INT_ur \
  1795. = ((_FP_FROM_INT_ur >> (X##_e - _FP_EXPBIAS_##fs \
  1796. - _FP_WFRACBITS_##fs + 1)) \
  1797. | ((_FP_FROM_INT_ur \
  1798. << ((rsize) - (X##_e - _FP_EXPBIAS_##fs \
  1799. - _FP_WFRACBITS_##fs + 1))) \
  1800. != 0)); \
  1801. _FP_FRAC_DISASSEMBLE_##wc (X, _FP_FROM_INT_ur, (rsize)); \
  1802. if ((_FP_EXPBIAS_##fs + _FP_WFRACBITS_##fs - 1 - X##_e) > 0) \
  1803. _FP_FRAC_SLL_##wc (X, (_FP_EXPBIAS_##fs \
  1804. + _FP_WFRACBITS_##fs - 1 - X##_e)); \
  1805. _FP_FRAC_HIGH_##fs (X) &= ~(_FP_W_TYPE) _FP_IMPLBIT_SH_##fs; \
  1806. pack_semiraw: \
  1807. _FP_PACK_SEMIRAW (fs, wc, X); \
  1808. } \
  1809. } \
  1810. else \
  1811. { \
  1812. X##_s = 0; \
  1813. X##_e = 0; \
  1814. _FP_FRAC_SET_##wc (X, _FP_ZEROFRAC_##wc); \
  1815. } \
  1816. } \
  1817. while (0)
  1818. /* Extend from a narrower floating-point format to a wider one. Input
  1819. and output are raw. If CHECK_NAN, then signaling NaNs are
  1820. converted to quiet with the "invalid" exception raised; otherwise
  1821. signaling NaNs remain signaling with no exception. */
  1822. #define _FP_EXTEND_CNAN(dfs, sfs, dwc, swc, D, S, check_nan) \
  1823. do \
  1824. { \
  1825. _FP_STATIC_ASSERT (_FP_FRACBITS_##dfs >= _FP_FRACBITS_##sfs, \
  1826. "destination mantissa narrower than source"); \
  1827. _FP_STATIC_ASSERT ((_FP_EXPMAX_##dfs - _FP_EXPBIAS_##dfs \
  1828. >= _FP_EXPMAX_##sfs - _FP_EXPBIAS_##sfs), \
  1829. "destination max exponent smaller" \
  1830. " than source"); \
  1831. _FP_STATIC_ASSERT (((_FP_EXPBIAS_##dfs \
  1832. >= (_FP_EXPBIAS_##sfs \
  1833. + _FP_FRACBITS_##sfs - 1)) \
  1834. || (_FP_EXPBIAS_##dfs == _FP_EXPBIAS_##sfs)), \
  1835. "source subnormals do not all become normal," \
  1836. " but bias not the same"); \
  1837. D##_s = S##_s; \
  1838. _FP_FRAC_COPY_##dwc##_##swc (D, S); \
  1839. if (_FP_EXP_NORMAL (sfs, swc, S)) \
  1840. { \
  1841. D##_e = S##_e + _FP_EXPBIAS_##dfs - _FP_EXPBIAS_##sfs; \
  1842. _FP_FRAC_SLL_##dwc (D, (_FP_FRACBITS_##dfs - _FP_FRACBITS_##sfs)); \
  1843. } \
  1844. else \
  1845. { \
  1846. if (S##_e == 0) \
  1847. { \
  1848. _FP_CHECK_FLUSH_ZERO (sfs, swc, S); \
  1849. if (_FP_FRAC_ZEROP_##swc (S)) \
  1850. D##_e = 0; \
  1851. else if (_FP_EXPBIAS_##dfs \
  1852. < _FP_EXPBIAS_##sfs + _FP_FRACBITS_##sfs - 1) \
  1853. { \
  1854. FP_SET_EXCEPTION (FP_EX_DENORM); \
  1855. _FP_FRAC_SLL_##dwc (D, (_FP_FRACBITS_##dfs \
  1856. - _FP_FRACBITS_##sfs)); \
  1857. D##_e = 0; \
  1858. if (FP_TRAPPING_EXCEPTIONS & FP_EX_UNDERFLOW) \
  1859. FP_SET_EXCEPTION (FP_EX_UNDERFLOW); \
  1860. } \
  1861. else \
  1862. { \
  1863. int FP_EXTEND_lz; \
  1864. FP_SET_EXCEPTION (FP_EX_DENORM); \
  1865. _FP_FRAC_CLZ_##swc (FP_EXTEND_lz, S); \
  1866. _FP_FRAC_SLL_##dwc (D, \
  1867. FP_EXTEND_lz + _FP_FRACBITS_##dfs \
  1868. - _FP_FRACTBITS_##sfs); \
  1869. D##_e = (_FP_EXPBIAS_##dfs - _FP_EXPBIAS_##sfs + 1 \
  1870. + _FP_FRACXBITS_##sfs - FP_EXTEND_lz); \
  1871. } \
  1872. } \
  1873. else \
  1874. { \
  1875. D##_e = _FP_EXPMAX_##dfs; \
  1876. if (!_FP_FRAC_ZEROP_##swc (S)) \
  1877. { \
  1878. if (check_nan && _FP_FRAC_SNANP (sfs, S)) \
  1879. FP_SET_EXCEPTION (FP_EX_INVALID \
  1880. | FP_EX_INVALID_SNAN); \
  1881. _FP_FRAC_SLL_##dwc (D, (_FP_FRACBITS_##dfs \
  1882. - _FP_FRACBITS_##sfs)); \
  1883. if (check_nan) \
  1884. _FP_SETQNAN (dfs, dwc, D); \
  1885. } \
  1886. } \
  1887. } \
  1888. } \
  1889. while (0)
  1890. #define FP_EXTEND(dfs, sfs, dwc, swc, D, S) \
  1891. _FP_EXTEND_CNAN (dfs, sfs, dwc, swc, D, S, 1)
  1892. /* Truncate from a wider floating-point format to a narrower one.
  1893. Input and output are semi-raw. */
  1894. #define FP_TRUNC(dfs, sfs, dwc, swc, D, S) \
  1895. do \
  1896. { \
  1897. _FP_STATIC_ASSERT (_FP_FRACBITS_##sfs >= _FP_FRACBITS_##dfs, \
  1898. "destination mantissa wider than source"); \
  1899. _FP_STATIC_ASSERT (((_FP_EXPBIAS_##sfs \
  1900. >= (_FP_EXPBIAS_##dfs \
  1901. + _FP_FRACBITS_##dfs - 1)) \
  1902. || _FP_EXPBIAS_##sfs == _FP_EXPBIAS_##dfs), \
  1903. "source subnormals do not all become same," \
  1904. " but bias not the same"); \
  1905. D##_s = S##_s; \
  1906. if (_FP_EXP_NORMAL (sfs, swc, S)) \
  1907. { \
  1908. D##_e = S##_e + _FP_EXPBIAS_##dfs - _FP_EXPBIAS_##sfs; \
  1909. if (D##_e >= _FP_EXPMAX_##dfs) \
  1910. _FP_OVERFLOW_SEMIRAW (dfs, dwc, D); \
  1911. else \
  1912. { \
  1913. if (D##_e <= 0) \
  1914. { \
  1915. if (D##_e < 1 - _FP_FRACBITS_##dfs) \
  1916. { \
  1917. _FP_FRAC_SET_##swc (S, _FP_ZEROFRAC_##swc); \
  1918. _FP_FRAC_LOW_##swc (S) |= 1; \
  1919. } \
  1920. else \
  1921. { \
  1922. _FP_FRAC_HIGH_##sfs (S) |= _FP_IMPLBIT_SH_##sfs; \
  1923. _FP_FRAC_SRS_##swc (S, (_FP_WFRACBITS_##sfs \
  1924. - _FP_WFRACBITS_##dfs \
  1925. + 1 - D##_e), \
  1926. _FP_WFRACBITS_##sfs); \
  1927. } \
  1928. D##_e = 0; \
  1929. } \
  1930. else \
  1931. _FP_FRAC_SRS_##swc (S, (_FP_WFRACBITS_##sfs \
  1932. - _FP_WFRACBITS_##dfs), \
  1933. _FP_WFRACBITS_##sfs); \
  1934. _FP_FRAC_COPY_##dwc##_##swc (D, S); \
  1935. } \
  1936. } \
  1937. else \
  1938. { \
  1939. if (S##_e == 0) \
  1940. { \
  1941. _FP_CHECK_FLUSH_ZERO (sfs, swc, S); \
  1942. D##_e = 0; \
  1943. if (_FP_FRAC_ZEROP_##swc (S)) \
  1944. _FP_FRAC_SET_##dwc (D, _FP_ZEROFRAC_##dwc); \
  1945. else \
  1946. { \
  1947. FP_SET_EXCEPTION (FP_EX_DENORM); \
  1948. if (_FP_EXPBIAS_##sfs \
  1949. < _FP_EXPBIAS_##dfs + _FP_FRACBITS_##dfs - 1) \
  1950. { \
  1951. _FP_FRAC_SRS_##swc (S, (_FP_WFRACBITS_##sfs \
  1952. - _FP_WFRACBITS_##dfs), \
  1953. _FP_WFRACBITS_##sfs); \
  1954. _FP_FRAC_COPY_##dwc##_##swc (D, S); \
  1955. } \
  1956. else \
  1957. { \
  1958. _FP_FRAC_SET_##dwc (D, _FP_ZEROFRAC_##dwc); \
  1959. _FP_FRAC_LOW_##dwc (D) |= 1; \
  1960. } \
  1961. } \
  1962. } \
  1963. else \
  1964. { \
  1965. D##_e = _FP_EXPMAX_##dfs; \
  1966. if (_FP_FRAC_ZEROP_##swc (S)) \
  1967. _FP_FRAC_SET_##dwc (D, _FP_ZEROFRAC_##dwc); \
  1968. else \
  1969. { \
  1970. _FP_CHECK_SIGNAN_SEMIRAW (sfs, swc, S); \
  1971. _FP_FRAC_SRL_##swc (S, (_FP_WFRACBITS_##sfs \
  1972. - _FP_WFRACBITS_##dfs)); \
  1973. _FP_FRAC_COPY_##dwc##_##swc (D, S); \
  1974. /* Semi-raw NaN must have all workbits cleared. */ \
  1975. _FP_FRAC_LOW_##dwc (D) \
  1976. &= ~(_FP_W_TYPE) ((1 << _FP_WORKBITS) - 1); \
  1977. _FP_SETQNAN_SEMIRAW (dfs, dwc, D); \
  1978. } \
  1979. } \
  1980. } \
  1981. } \
  1982. while (0)
  1983. /* Helper primitives. */
  1984. /* Count leading zeros in a word. */
  1985. #ifndef __FP_CLZ
  1986. /* GCC 3.4 and later provide the builtins for us. */
  1987. # define __FP_CLZ(r, x) \
  1988. do \
  1989. { \
  1990. _FP_STATIC_ASSERT ((sizeof (_FP_W_TYPE) == sizeof (unsigned int) \
  1991. || (sizeof (_FP_W_TYPE) \
  1992. == sizeof (unsigned long)) \
  1993. || (sizeof (_FP_W_TYPE) \
  1994. == sizeof (unsigned long long))), \
  1995. "_FP_W_TYPE size unsupported for clz"); \
  1996. if (sizeof (_FP_W_TYPE) == sizeof (unsigned int)) \
  1997. (r) = __builtin_clz (x); \
  1998. else if (sizeof (_FP_W_TYPE) == sizeof (unsigned long)) \
  1999. (r) = __builtin_clzl (x); \
  2000. else /* sizeof (_FP_W_TYPE) == sizeof (unsigned long long). */ \
  2001. (r) = __builtin_clzll (x); \
  2002. } \
  2003. while (0)
  2004. #endif /* ndef __FP_CLZ */
  2005. #define _FP_DIV_HELP_imm(q, r, n, d) \
  2006. do \
  2007. { \
  2008. (q) = (n) / (d), (r) = (n) % (d); \
  2009. } \
  2010. while (0)
  2011. /* A restoring bit-by-bit division primitive. */
  2012. #define _FP_DIV_MEAT_N_loop(fs, wc, R, X, Y) \
  2013. do \
  2014. { \
  2015. int _FP_DIV_MEAT_N_loop_count = _FP_WFRACBITS_##fs; \
  2016. _FP_FRAC_DECL_##wc (_FP_DIV_MEAT_N_loop_u); \
  2017. _FP_FRAC_DECL_##wc (_FP_DIV_MEAT_N_loop_v); \
  2018. _FP_FRAC_COPY_##wc (_FP_DIV_MEAT_N_loop_u, X); \
  2019. _FP_FRAC_COPY_##wc (_FP_DIV_MEAT_N_loop_v, Y); \
  2020. _FP_FRAC_SET_##wc (R, _FP_ZEROFRAC_##wc); \
  2021. /* Normalize _FP_DIV_MEAT_N_LOOP_U and _FP_DIV_MEAT_N_LOOP_V. */ \
  2022. _FP_FRAC_SLL_##wc (_FP_DIV_MEAT_N_loop_u, _FP_WFRACXBITS_##fs); \
  2023. _FP_FRAC_SLL_##wc (_FP_DIV_MEAT_N_loop_v, _FP_WFRACXBITS_##fs); \
  2024. /* First round. Since the operands are normalized, either the \
  2025. first or second bit will be set in the fraction. Produce a \
  2026. normalized result by checking which and adjusting the loop \
  2027. count and exponent accordingly. */ \
  2028. if (_FP_FRAC_GE_1 (_FP_DIV_MEAT_N_loop_u, _FP_DIV_MEAT_N_loop_v)) \
  2029. { \
  2030. _FP_FRAC_SUB_##wc (_FP_DIV_MEAT_N_loop_u, \
  2031. _FP_DIV_MEAT_N_loop_u, \
  2032. _FP_DIV_MEAT_N_loop_v); \
  2033. _FP_FRAC_LOW_##wc (R) |= 1; \
  2034. _FP_DIV_MEAT_N_loop_count--; \
  2035. } \
  2036. else \
  2037. R##_e--; \
  2038. /* Subsequent rounds. */ \
  2039. do \
  2040. { \
  2041. int _FP_DIV_MEAT_N_loop_msb \
  2042. = (_FP_WS_TYPE) _FP_FRAC_HIGH_##wc (_FP_DIV_MEAT_N_loop_u) < 0; \
  2043. _FP_FRAC_SLL_##wc (_FP_DIV_MEAT_N_loop_u, 1); \
  2044. _FP_FRAC_SLL_##wc (R, 1); \
  2045. if (_FP_DIV_MEAT_N_loop_msb \
  2046. || _FP_FRAC_GE_1 (_FP_DIV_MEAT_N_loop_u, \
  2047. _FP_DIV_MEAT_N_loop_v)) \
  2048. { \
  2049. _FP_FRAC_SUB_##wc (_FP_DIV_MEAT_N_loop_u, \
  2050. _FP_DIV_MEAT_N_loop_u, \
  2051. _FP_DIV_MEAT_N_loop_v); \
  2052. _FP_FRAC_LOW_##wc (R) |= 1; \
  2053. } \
  2054. } \
  2055. while (--_FP_DIV_MEAT_N_loop_count > 0); \
  2056. /* If there's anything left in _FP_DIV_MEAT_N_LOOP_U, the result \
  2057. is inexact. */ \
  2058. _FP_FRAC_LOW_##wc (R) \
  2059. |= !_FP_FRAC_ZEROP_##wc (_FP_DIV_MEAT_N_loop_u); \
  2060. } \
  2061. while (0)
  2062. #define _FP_DIV_MEAT_1_loop(fs, R, X, Y) _FP_DIV_MEAT_N_loop (fs, 1, R, X, Y)
  2063. #define _FP_DIV_MEAT_2_loop(fs, R, X, Y) _FP_DIV_MEAT_N_loop (fs, 2, R, X, Y)
  2064. #define _FP_DIV_MEAT_4_loop(fs, R, X, Y) _FP_DIV_MEAT_N_loop (fs, 4, R, X, Y)
  2065. #endif /* !SOFT_FP_OP_COMMON_H */