op-common.h 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359
  1. /* Software floating-point emulation. Common operations.
  2. Copyright (C) 1997,1998,1999,2006,2007 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, write to the Free
  26. Software Foundation, 51 Franklin Street, Fifth Floor, Boston,
  27. MA 02110-1301, USA. */
  28. #define _FP_DECL(wc, X) \
  29. _FP_I_TYPE X##_c __attribute__((unused)), X##_s, X##_e; \
  30. _FP_FRAC_DECL_##wc(X)
  31. /*
  32. * Finish truely unpacking a native fp value by classifying the kind
  33. * of fp value and normalizing both the exponent and the fraction.
  34. */
  35. #define _FP_UNPACK_CANONICAL(fs, wc, X) \
  36. do { \
  37. switch (X##_e) \
  38. { \
  39. default: \
  40. _FP_FRAC_HIGH_RAW_##fs(X) |= _FP_IMPLBIT_##fs; \
  41. _FP_FRAC_SLL_##wc(X, _FP_WORKBITS); \
  42. X##_e -= _FP_EXPBIAS_##fs; \
  43. X##_c = FP_CLS_NORMAL; \
  44. break; \
  45. \
  46. case 0: \
  47. if (_FP_FRAC_ZEROP_##wc(X)) \
  48. X##_c = FP_CLS_ZERO; \
  49. else \
  50. { \
  51. /* a denormalized number */ \
  52. _FP_I_TYPE _shift; \
  53. _FP_FRAC_CLZ_##wc(_shift, X); \
  54. _shift -= _FP_FRACXBITS_##fs; \
  55. _FP_FRAC_SLL_##wc(X, (_shift+_FP_WORKBITS)); \
  56. X##_e -= _FP_EXPBIAS_##fs - 1 + _shift; \
  57. X##_c = FP_CLS_NORMAL; \
  58. FP_SET_EXCEPTION(FP_EX_DENORM); \
  59. } \
  60. break; \
  61. \
  62. case _FP_EXPMAX_##fs: \
  63. if (_FP_FRAC_ZEROP_##wc(X)) \
  64. X##_c = FP_CLS_INF; \
  65. else \
  66. { \
  67. X##_c = FP_CLS_NAN; \
  68. /* Check for signaling NaN */ \
  69. if (!(_FP_FRAC_HIGH_RAW_##fs(X) & _FP_QNANBIT_##fs)) \
  70. FP_SET_EXCEPTION(FP_EX_INVALID); \
  71. } \
  72. break; \
  73. } \
  74. } while (0)
  75. /* Finish unpacking an fp value in semi-raw mode: the mantissa is
  76. shifted by _FP_WORKBITS but the implicit MSB is not inserted and
  77. other classification is not done. */
  78. #define _FP_UNPACK_SEMIRAW(fs, wc, X) _FP_FRAC_SLL_##wc(X, _FP_WORKBITS)
  79. /* A semi-raw value has overflowed to infinity. Adjust the mantissa
  80. and exponent appropriately. */
  81. #define _FP_OVERFLOW_SEMIRAW(fs, wc, X) \
  82. do { \
  83. if (FP_ROUNDMODE == FP_RND_NEAREST \
  84. || (FP_ROUNDMODE == FP_RND_PINF && !X##_s) \
  85. || (FP_ROUNDMODE == FP_RND_MINF && X##_s)) \
  86. { \
  87. X##_e = _FP_EXPMAX_##fs; \
  88. _FP_FRAC_SET_##wc(X, _FP_ZEROFRAC_##wc); \
  89. } \
  90. else \
  91. { \
  92. X##_e = _FP_EXPMAX_##fs - 1; \
  93. _FP_FRAC_SET_##wc(X, _FP_MAXFRAC_##wc); \
  94. } \
  95. FP_SET_EXCEPTION(FP_EX_INEXACT); \
  96. FP_SET_EXCEPTION(FP_EX_OVERFLOW); \
  97. } while (0)
  98. /* Check for a semi-raw value being a signaling NaN and raise the
  99. invalid exception if so. */
  100. #define _FP_CHECK_SIGNAN_SEMIRAW(fs, wc, X) \
  101. do { \
  102. if (X##_e == _FP_EXPMAX_##fs \
  103. && !_FP_FRAC_ZEROP_##wc(X) \
  104. && !(_FP_FRAC_HIGH_##fs(X) & _FP_QNANBIT_SH_##fs)) \
  105. FP_SET_EXCEPTION(FP_EX_INVALID); \
  106. } while (0)
  107. /* Choose a NaN result from an operation on two semi-raw NaN
  108. values. */
  109. #define _FP_CHOOSENAN_SEMIRAW(fs, wc, R, X, Y, OP) \
  110. do { \
  111. /* _FP_CHOOSENAN expects raw values, so shift as required. */ \
  112. _FP_FRAC_SRL_##wc(X, _FP_WORKBITS); \
  113. _FP_FRAC_SRL_##wc(Y, _FP_WORKBITS); \
  114. _FP_CHOOSENAN(fs, wc, R, X, Y, OP); \
  115. _FP_FRAC_SLL_##wc(R, _FP_WORKBITS); \
  116. } while (0)
  117. /* Test whether a biased exponent is normal (not zero or maximum). */
  118. #define _FP_EXP_NORMAL(fs, wc, X) (((X##_e + 1) & _FP_EXPMAX_##fs) > 1)
  119. /* Prepare to pack an fp value in semi-raw mode: the mantissa is
  120. rounded and shifted right, with the rounding possibly increasing
  121. the exponent (including changing a finite value to infinity). */
  122. #define _FP_PACK_SEMIRAW(fs, wc, X) \
  123. do { \
  124. _FP_ROUND(wc, X); \
  125. if (_FP_FRAC_HIGH_##fs(X) \
  126. & (_FP_OVERFLOW_##fs >> 1)) \
  127. { \
  128. _FP_FRAC_HIGH_##fs(X) &= ~(_FP_OVERFLOW_##fs >> 1); \
  129. X##_e++; \
  130. if (X##_e == _FP_EXPMAX_##fs) \
  131. _FP_OVERFLOW_SEMIRAW(fs, wc, X); \
  132. } \
  133. _FP_FRAC_SRL_##wc(X, _FP_WORKBITS); \
  134. if (!_FP_EXP_NORMAL(fs, wc, X) && !_FP_FRAC_ZEROP_##wc(X)) \
  135. { \
  136. if (X##_e == 0) \
  137. FP_SET_EXCEPTION(FP_EX_UNDERFLOW); \
  138. else \
  139. { \
  140. if (!_FP_KEEPNANFRACP) \
  141. { \
  142. _FP_FRAC_SET_##wc(X, _FP_NANFRAC_##fs); \
  143. X##_s = _FP_NANSIGN_##fs; \
  144. } \
  145. else \
  146. _FP_FRAC_HIGH_RAW_##fs(X) |= _FP_QNANBIT_##fs; \
  147. } \
  148. } \
  149. } while (0)
  150. /*
  151. * Before packing the bits back into the native fp result, take care
  152. * of such mundane things as rounding and overflow. Also, for some
  153. * kinds of fp values, the original parts may not have been fully
  154. * extracted -- but that is ok, we can regenerate them now.
  155. */
  156. #define _FP_PACK_CANONICAL(fs, wc, X) \
  157. do { \
  158. switch (X##_c) \
  159. { \
  160. case FP_CLS_NORMAL: \
  161. X##_e += _FP_EXPBIAS_##fs; \
  162. if (X##_e > 0) \
  163. { \
  164. _FP_ROUND(wc, X); \
  165. if (_FP_FRAC_OVERP_##wc(fs, X)) \
  166. { \
  167. _FP_FRAC_CLEAR_OVERP_##wc(fs, X); \
  168. X##_e++; \
  169. } \
  170. _FP_FRAC_SRL_##wc(X, _FP_WORKBITS); \
  171. if (X##_e >= _FP_EXPMAX_##fs) \
  172. { \
  173. /* overflow */ \
  174. switch (FP_ROUNDMODE) \
  175. { \
  176. case FP_RND_NEAREST: \
  177. X##_c = FP_CLS_INF; \
  178. break; \
  179. case FP_RND_PINF: \
  180. if (!X##_s) X##_c = FP_CLS_INF; \
  181. break; \
  182. case FP_RND_MINF: \
  183. if (X##_s) X##_c = FP_CLS_INF; \
  184. break; \
  185. } \
  186. if (X##_c == FP_CLS_INF) \
  187. { \
  188. /* Overflow to infinity */ \
  189. X##_e = _FP_EXPMAX_##fs; \
  190. _FP_FRAC_SET_##wc(X, _FP_ZEROFRAC_##wc); \
  191. } \
  192. else \
  193. { \
  194. /* Overflow to maximum normal */ \
  195. X##_e = _FP_EXPMAX_##fs - 1; \
  196. _FP_FRAC_SET_##wc(X, _FP_MAXFRAC_##wc); \
  197. } \
  198. FP_SET_EXCEPTION(FP_EX_OVERFLOW); \
  199. FP_SET_EXCEPTION(FP_EX_INEXACT); \
  200. } \
  201. } \
  202. else \
  203. { \
  204. /* we've got a denormalized number */ \
  205. X##_e = -X##_e + 1; \
  206. if (X##_e <= _FP_WFRACBITS_##fs) \
  207. { \
  208. _FP_FRAC_SRS_##wc(X, X##_e, _FP_WFRACBITS_##fs); \
  209. _FP_ROUND(wc, X); \
  210. if (_FP_FRAC_HIGH_##fs(X) \
  211. & (_FP_OVERFLOW_##fs >> 1)) \
  212. { \
  213. X##_e = 1; \
  214. _FP_FRAC_SET_##wc(X, _FP_ZEROFRAC_##wc); \
  215. } \
  216. else \
  217. { \
  218. X##_e = 0; \
  219. _FP_FRAC_SRL_##wc(X, _FP_WORKBITS); \
  220. FP_SET_EXCEPTION(FP_EX_UNDERFLOW); \
  221. } \
  222. } \
  223. else \
  224. { \
  225. /* underflow to zero */ \
  226. X##_e = 0; \
  227. if (!_FP_FRAC_ZEROP_##wc(X)) \
  228. { \
  229. _FP_FRAC_SET_##wc(X, _FP_MINFRAC_##wc); \
  230. _FP_ROUND(wc, X); \
  231. _FP_FRAC_LOW_##wc(X) >>= (_FP_WORKBITS); \
  232. } \
  233. FP_SET_EXCEPTION(FP_EX_UNDERFLOW); \
  234. } \
  235. } \
  236. break; \
  237. \
  238. case FP_CLS_ZERO: \
  239. X##_e = 0; \
  240. _FP_FRAC_SET_##wc(X, _FP_ZEROFRAC_##wc); \
  241. break; \
  242. \
  243. case FP_CLS_INF: \
  244. X##_e = _FP_EXPMAX_##fs; \
  245. _FP_FRAC_SET_##wc(X, _FP_ZEROFRAC_##wc); \
  246. break; \
  247. \
  248. case FP_CLS_NAN: \
  249. X##_e = _FP_EXPMAX_##fs; \
  250. if (!_FP_KEEPNANFRACP) \
  251. { \
  252. _FP_FRAC_SET_##wc(X, _FP_NANFRAC_##fs); \
  253. X##_s = _FP_NANSIGN_##fs; \
  254. } \
  255. else \
  256. _FP_FRAC_HIGH_RAW_##fs(X) |= _FP_QNANBIT_##fs; \
  257. break; \
  258. } \
  259. } while (0)
  260. /* This one accepts raw argument and not cooked, returns
  261. * 1 if X is a signaling NaN.
  262. */
  263. #define _FP_ISSIGNAN(fs, wc, X) \
  264. ({ \
  265. int __ret = 0; \
  266. if (X##_e == _FP_EXPMAX_##fs) \
  267. { \
  268. if (!_FP_FRAC_ZEROP_##wc(X) \
  269. && !(_FP_FRAC_HIGH_RAW_##fs(X) & _FP_QNANBIT_##fs)) \
  270. __ret = 1; \
  271. } \
  272. __ret; \
  273. })
  274. /* Addition on semi-raw values. */
  275. #define _FP_ADD_INTERNAL(fs, wc, R, X, Y, OP) \
  276. do { \
  277. if (X##_s == Y##_s) \
  278. { \
  279. /* Addition. */ \
  280. R##_s = X##_s; \
  281. int ediff = X##_e - Y##_e; \
  282. if (ediff > 0) \
  283. { \
  284. R##_e = X##_e; \
  285. if (Y##_e == 0) \
  286. { \
  287. /* Y is zero or denormalized. */ \
  288. if (_FP_FRAC_ZEROP_##wc(Y)) \
  289. { \
  290. _FP_CHECK_SIGNAN_SEMIRAW(fs, wc, X); \
  291. _FP_FRAC_COPY_##wc(R, X); \
  292. goto add_done; \
  293. } \
  294. else \
  295. { \
  296. FP_SET_EXCEPTION(FP_EX_DENORM); \
  297. ediff--; \
  298. if (ediff == 0) \
  299. { \
  300. _FP_FRAC_ADD_##wc(R, X, Y); \
  301. goto add3; \
  302. } \
  303. if (X##_e == _FP_EXPMAX_##fs) \
  304. { \
  305. _FP_CHECK_SIGNAN_SEMIRAW(fs, wc, X); \
  306. _FP_FRAC_COPY_##wc(R, X); \
  307. goto add_done; \
  308. } \
  309. goto add1; \
  310. } \
  311. } \
  312. else if (X##_e == _FP_EXPMAX_##fs) \
  313. { \
  314. /* X is NaN or Inf, Y is normal. */ \
  315. _FP_CHECK_SIGNAN_SEMIRAW(fs, wc, X); \
  316. _FP_FRAC_COPY_##wc(R, X); \
  317. goto add_done; \
  318. } \
  319. \
  320. /* Insert implicit MSB of Y. */ \
  321. _FP_FRAC_HIGH_##fs(Y) |= _FP_IMPLBIT_SH_##fs; \
  322. \
  323. add1: \
  324. /* Shift the mantissa of Y to the right EDIFF steps; \
  325. remember to account later for the implicit MSB of X. */ \
  326. if (ediff <= _FP_WFRACBITS_##fs) \
  327. _FP_FRAC_SRS_##wc(Y, ediff, _FP_WFRACBITS_##fs); \
  328. else if (!_FP_FRAC_ZEROP_##wc(Y)) \
  329. _FP_FRAC_SET_##wc(Y, _FP_MINFRAC_##wc); \
  330. _FP_FRAC_ADD_##wc(R, X, Y); \
  331. } \
  332. else if (ediff < 0) \
  333. { \
  334. ediff = -ediff; \
  335. R##_e = Y##_e; \
  336. if (X##_e == 0) \
  337. { \
  338. /* X is zero or denormalized. */ \
  339. if (_FP_FRAC_ZEROP_##wc(X)) \
  340. { \
  341. _FP_CHECK_SIGNAN_SEMIRAW(fs, wc, Y); \
  342. _FP_FRAC_COPY_##wc(R, Y); \
  343. goto add_done; \
  344. } \
  345. else \
  346. { \
  347. FP_SET_EXCEPTION(FP_EX_DENORM); \
  348. ediff--; \
  349. if (ediff == 0) \
  350. { \
  351. _FP_FRAC_ADD_##wc(R, Y, X); \
  352. goto add3; \
  353. } \
  354. if (Y##_e == _FP_EXPMAX_##fs) \
  355. { \
  356. _FP_CHECK_SIGNAN_SEMIRAW(fs, wc, Y); \
  357. _FP_FRAC_COPY_##wc(R, Y); \
  358. goto add_done; \
  359. } \
  360. goto add2; \
  361. } \
  362. } \
  363. else if (Y##_e == _FP_EXPMAX_##fs) \
  364. { \
  365. /* Y is NaN or Inf, X is normal. */ \
  366. _FP_CHECK_SIGNAN_SEMIRAW(fs, wc, Y); \
  367. _FP_FRAC_COPY_##wc(R, Y); \
  368. goto add_done; \
  369. } \
  370. \
  371. /* Insert implicit MSB of X. */ \
  372. _FP_FRAC_HIGH_##fs(X) |= _FP_IMPLBIT_SH_##fs; \
  373. \
  374. add2: \
  375. /* Shift the mantissa of X to the right EDIFF steps; \
  376. remember to account later for the implicit MSB of Y. */ \
  377. if (ediff <= _FP_WFRACBITS_##fs) \
  378. _FP_FRAC_SRS_##wc(X, ediff, _FP_WFRACBITS_##fs); \
  379. else if (!_FP_FRAC_ZEROP_##wc(X)) \
  380. _FP_FRAC_SET_##wc(X, _FP_MINFRAC_##wc); \
  381. _FP_FRAC_ADD_##wc(R, Y, X); \
  382. } \
  383. else \
  384. { \
  385. /* ediff == 0. */ \
  386. if (!_FP_EXP_NORMAL(fs, wc, X)) \
  387. { \
  388. if (X##_e == 0) \
  389. { \
  390. /* X and Y are zero or denormalized. */ \
  391. R##_e = 0; \
  392. if (_FP_FRAC_ZEROP_##wc(X)) \
  393. { \
  394. if (!_FP_FRAC_ZEROP_##wc(Y)) \
  395. FP_SET_EXCEPTION(FP_EX_DENORM); \
  396. _FP_FRAC_COPY_##wc(R, Y); \
  397. goto add_done; \
  398. } \
  399. else if (_FP_FRAC_ZEROP_##wc(Y)) \
  400. { \
  401. FP_SET_EXCEPTION(FP_EX_DENORM); \
  402. _FP_FRAC_COPY_##wc(R, X); \
  403. goto add_done; \
  404. } \
  405. else \
  406. { \
  407. FP_SET_EXCEPTION(FP_EX_DENORM); \
  408. _FP_FRAC_ADD_##wc(R, X, Y); \
  409. if (_FP_FRAC_HIGH_##fs(R) & _FP_IMPLBIT_SH_##fs) \
  410. { \
  411. /* Normalized result. */ \
  412. _FP_FRAC_HIGH_##fs(R) \
  413. &= ~(_FP_W_TYPE)_FP_IMPLBIT_SH_##fs; \
  414. R##_e = 1; \
  415. } \
  416. goto add_done; \
  417. } \
  418. } \
  419. else \
  420. { \
  421. /* X and Y are NaN or Inf. */ \
  422. _FP_CHECK_SIGNAN_SEMIRAW(fs, wc, X); \
  423. _FP_CHECK_SIGNAN_SEMIRAW(fs, wc, Y); \
  424. R##_e = _FP_EXPMAX_##fs; \
  425. if (_FP_FRAC_ZEROP_##wc(X)) \
  426. _FP_FRAC_COPY_##wc(R, Y); \
  427. else if (_FP_FRAC_ZEROP_##wc(Y)) \
  428. _FP_FRAC_COPY_##wc(R, X); \
  429. else \
  430. _FP_CHOOSENAN_SEMIRAW(fs, wc, R, X, Y, OP); \
  431. goto add_done; \
  432. } \
  433. } \
  434. /* The exponents of X and Y, both normal, are equal. The \
  435. implicit MSBs will always add to increase the \
  436. exponent. */ \
  437. _FP_FRAC_ADD_##wc(R, X, Y); \
  438. R##_e = X##_e + 1; \
  439. _FP_FRAC_SRS_##wc(R, 1, _FP_WFRACBITS_##fs); \
  440. if (R##_e == _FP_EXPMAX_##fs) \
  441. /* Overflow to infinity (depending on rounding mode). */ \
  442. _FP_OVERFLOW_SEMIRAW(fs, wc, R); \
  443. goto add_done; \
  444. } \
  445. add3: \
  446. if (_FP_FRAC_HIGH_##fs(R) & _FP_IMPLBIT_SH_##fs) \
  447. { \
  448. /* Overflow. */ \
  449. _FP_FRAC_HIGH_##fs(R) &= ~(_FP_W_TYPE)_FP_IMPLBIT_SH_##fs; \
  450. R##_e++; \
  451. _FP_FRAC_SRS_##wc(R, 1, _FP_WFRACBITS_##fs); \
  452. if (R##_e == _FP_EXPMAX_##fs) \
  453. /* Overflow to infinity (depending on rounding mode). */ \
  454. _FP_OVERFLOW_SEMIRAW(fs, wc, R); \
  455. } \
  456. add_done: ; \
  457. } \
  458. else \
  459. { \
  460. /* Subtraction. */ \
  461. int ediff = X##_e - Y##_e; \
  462. if (ediff > 0) \
  463. { \
  464. R##_e = X##_e; \
  465. R##_s = X##_s; \
  466. if (Y##_e == 0) \
  467. { \
  468. /* Y is zero or denormalized. */ \
  469. if (_FP_FRAC_ZEROP_##wc(Y)) \
  470. { \
  471. _FP_CHECK_SIGNAN_SEMIRAW(fs, wc, X); \
  472. _FP_FRAC_COPY_##wc(R, X); \
  473. goto sub_done; \
  474. } \
  475. else \
  476. { \
  477. FP_SET_EXCEPTION(FP_EX_DENORM); \
  478. ediff--; \
  479. if (ediff == 0) \
  480. { \
  481. _FP_FRAC_SUB_##wc(R, X, Y); \
  482. goto sub3; \
  483. } \
  484. if (X##_e == _FP_EXPMAX_##fs) \
  485. { \
  486. _FP_CHECK_SIGNAN_SEMIRAW(fs, wc, X); \
  487. _FP_FRAC_COPY_##wc(R, X); \
  488. goto sub_done; \
  489. } \
  490. goto sub1; \
  491. } \
  492. } \
  493. else if (X##_e == _FP_EXPMAX_##fs) \
  494. { \
  495. /* X is NaN or Inf, Y is normal. */ \
  496. _FP_CHECK_SIGNAN_SEMIRAW(fs, wc, X); \
  497. _FP_FRAC_COPY_##wc(R, X); \
  498. goto sub_done; \
  499. } \
  500. \
  501. /* Insert implicit MSB of Y. */ \
  502. _FP_FRAC_HIGH_##fs(Y) |= _FP_IMPLBIT_SH_##fs; \
  503. \
  504. sub1: \
  505. /* Shift the mantissa of Y to the right EDIFF steps; \
  506. remember to account later for the implicit MSB of X. */ \
  507. if (ediff <= _FP_WFRACBITS_##fs) \
  508. _FP_FRAC_SRS_##wc(Y, ediff, _FP_WFRACBITS_##fs); \
  509. else if (!_FP_FRAC_ZEROP_##wc(Y)) \
  510. _FP_FRAC_SET_##wc(Y, _FP_MINFRAC_##wc); \
  511. _FP_FRAC_SUB_##wc(R, X, Y); \
  512. } \
  513. else if (ediff < 0) \
  514. { \
  515. ediff = -ediff; \
  516. R##_e = Y##_e; \
  517. R##_s = Y##_s; \
  518. if (X##_e == 0) \
  519. { \
  520. /* X is zero or denormalized. */ \
  521. if (_FP_FRAC_ZEROP_##wc(X)) \
  522. { \
  523. _FP_CHECK_SIGNAN_SEMIRAW(fs, wc, Y); \
  524. _FP_FRAC_COPY_##wc(R, Y); \
  525. goto sub_done; \
  526. } \
  527. else \
  528. { \
  529. FP_SET_EXCEPTION(FP_EX_DENORM); \
  530. ediff--; \
  531. if (ediff == 0) \
  532. { \
  533. _FP_FRAC_SUB_##wc(R, Y, X); \
  534. goto sub3; \
  535. } \
  536. if (Y##_e == _FP_EXPMAX_##fs) \
  537. { \
  538. _FP_CHECK_SIGNAN_SEMIRAW(fs, wc, Y); \
  539. _FP_FRAC_COPY_##wc(R, Y); \
  540. goto sub_done; \
  541. } \
  542. goto sub2; \
  543. } \
  544. } \
  545. else if (Y##_e == _FP_EXPMAX_##fs) \
  546. { \
  547. /* Y is NaN or Inf, X is normal. */ \
  548. _FP_CHECK_SIGNAN_SEMIRAW(fs, wc, Y); \
  549. _FP_FRAC_COPY_##wc(R, Y); \
  550. goto sub_done; \
  551. } \
  552. \
  553. /* Insert implicit MSB of X. */ \
  554. _FP_FRAC_HIGH_##fs(X) |= _FP_IMPLBIT_SH_##fs; \
  555. \
  556. sub2: \
  557. /* Shift the mantissa of X to the right EDIFF steps; \
  558. remember to account later for the implicit MSB of Y. */ \
  559. if (ediff <= _FP_WFRACBITS_##fs) \
  560. _FP_FRAC_SRS_##wc(X, ediff, _FP_WFRACBITS_##fs); \
  561. else if (!_FP_FRAC_ZEROP_##wc(X)) \
  562. _FP_FRAC_SET_##wc(X, _FP_MINFRAC_##wc); \
  563. _FP_FRAC_SUB_##wc(R, Y, X); \
  564. } \
  565. else \
  566. { \
  567. /* ediff == 0. */ \
  568. if (!_FP_EXP_NORMAL(fs, wc, X)) \
  569. { \
  570. if (X##_e == 0) \
  571. { \
  572. /* X and Y are zero or denormalized. */ \
  573. R##_e = 0; \
  574. if (_FP_FRAC_ZEROP_##wc(X)) \
  575. { \
  576. _FP_FRAC_COPY_##wc(R, Y); \
  577. if (_FP_FRAC_ZEROP_##wc(Y)) \
  578. R##_s = (FP_ROUNDMODE == FP_RND_MINF); \
  579. else \
  580. { \
  581. FP_SET_EXCEPTION(FP_EX_DENORM); \
  582. R##_s = Y##_s; \
  583. } \
  584. goto sub_done; \
  585. } \
  586. else if (_FP_FRAC_ZEROP_##wc(Y)) \
  587. { \
  588. FP_SET_EXCEPTION(FP_EX_DENORM); \
  589. _FP_FRAC_COPY_##wc(R, X); \
  590. R##_s = X##_s; \
  591. goto sub_done; \
  592. } \
  593. else \
  594. { \
  595. FP_SET_EXCEPTION(FP_EX_DENORM); \
  596. _FP_FRAC_SUB_##wc(R, X, Y); \
  597. R##_s = X##_s; \
  598. if (_FP_FRAC_HIGH_##fs(R) & _FP_IMPLBIT_SH_##fs) \
  599. { \
  600. /* |X| < |Y|, negate result. */ \
  601. _FP_FRAC_SUB_##wc(R, Y, X); \
  602. R##_s = Y##_s; \
  603. } \
  604. else if (_FP_FRAC_ZEROP_##wc(R)) \
  605. R##_s = (FP_ROUNDMODE == FP_RND_MINF); \
  606. goto sub_done; \
  607. } \
  608. } \
  609. else \
  610. { \
  611. /* X and Y are NaN or Inf, of opposite signs. */ \
  612. _FP_CHECK_SIGNAN_SEMIRAW(fs, wc, X); \
  613. _FP_CHECK_SIGNAN_SEMIRAW(fs, wc, Y); \
  614. R##_e = _FP_EXPMAX_##fs; \
  615. if (_FP_FRAC_ZEROP_##wc(X)) \
  616. { \
  617. if (_FP_FRAC_ZEROP_##wc(Y)) \
  618. { \
  619. /* Inf - Inf. */ \
  620. R##_s = _FP_NANSIGN_##fs; \
  621. _FP_FRAC_SET_##wc(R, _FP_NANFRAC_##fs); \
  622. _FP_FRAC_SLL_##wc(R, _FP_WORKBITS); \
  623. FP_SET_EXCEPTION(FP_EX_INVALID); \
  624. } \
  625. else \
  626. { \
  627. /* Inf - NaN. */ \
  628. R##_s = Y##_s; \
  629. _FP_FRAC_COPY_##wc(R, Y); \
  630. } \
  631. } \
  632. else \
  633. { \
  634. if (_FP_FRAC_ZEROP_##wc(Y)) \
  635. { \
  636. /* NaN - Inf. */ \
  637. R##_s = X##_s; \
  638. _FP_FRAC_COPY_##wc(R, X); \
  639. } \
  640. else \
  641. { \
  642. /* NaN - NaN. */ \
  643. _FP_CHOOSENAN_SEMIRAW(fs, wc, R, X, Y, OP); \
  644. } \
  645. } \
  646. goto sub_done; \
  647. } \
  648. } \
  649. /* The exponents of X and Y, both normal, are equal. The \
  650. implicit MSBs cancel. */ \
  651. R##_e = X##_e; \
  652. _FP_FRAC_SUB_##wc(R, X, Y); \
  653. R##_s = X##_s; \
  654. if (_FP_FRAC_HIGH_##fs(R) & _FP_IMPLBIT_SH_##fs) \
  655. { \
  656. /* |X| < |Y|, negate result. */ \
  657. _FP_FRAC_SUB_##wc(R, Y, X); \
  658. R##_s = Y##_s; \
  659. } \
  660. else if (_FP_FRAC_ZEROP_##wc(R)) \
  661. { \
  662. R##_e = 0; \
  663. R##_s = (FP_ROUNDMODE == FP_RND_MINF); \
  664. goto sub_done; \
  665. } \
  666. goto norm; \
  667. } \
  668. sub3: \
  669. if (_FP_FRAC_HIGH_##fs(R) & _FP_IMPLBIT_SH_##fs) \
  670. { \
  671. int diff; \
  672. /* Carry into most significant bit of larger one of X and Y, \
  673. canceling it; renormalize. */ \
  674. _FP_FRAC_HIGH_##fs(R) &= _FP_IMPLBIT_SH_##fs - 1; \
  675. norm: \
  676. _FP_FRAC_CLZ_##wc(diff, R); \
  677. diff -= _FP_WFRACXBITS_##fs; \
  678. _FP_FRAC_SLL_##wc(R, diff); \
  679. if (R##_e <= diff) \
  680. { \
  681. /* R is denormalized. */ \
  682. diff = diff - R##_e + 1; \
  683. _FP_FRAC_SRS_##wc(R, diff, _FP_WFRACBITS_##fs); \
  684. R##_e = 0; \
  685. } \
  686. else \
  687. { \
  688. R##_e -= diff; \
  689. _FP_FRAC_HIGH_##fs(R) &= ~(_FP_W_TYPE)_FP_IMPLBIT_SH_##fs; \
  690. } \
  691. } \
  692. sub_done: ; \
  693. } \
  694. } while (0)
  695. #define _FP_ADD(fs, wc, R, X, Y) _FP_ADD_INTERNAL(fs, wc, R, X, Y, '+')
  696. #define _FP_SUB(fs, wc, R, X, Y) \
  697. do { \
  698. if (!(Y##_e == _FP_EXPMAX_##fs && !_FP_FRAC_ZEROP_##wc(Y))) Y##_s ^= 1; \
  699. _FP_ADD_INTERNAL(fs, wc, R, X, Y, '-'); \
  700. } while (0)
  701. /*
  702. * Main negation routine. FIXME -- when we care about setting exception
  703. * bits reliably, this will not do. We should examine all of the fp classes.
  704. */
  705. #define _FP_NEG(fs, wc, R, X) \
  706. do { \
  707. _FP_FRAC_COPY_##wc(R, X); \
  708. R##_c = X##_c; \
  709. R##_e = X##_e; \
  710. R##_s = 1 ^ X##_s; \
  711. } while (0)
  712. /*
  713. * Main multiplication routine. The input values should be cooked.
  714. */
  715. #define _FP_MUL(fs, wc, R, X, Y) \
  716. do { \
  717. R##_s = X##_s ^ Y##_s; \
  718. switch (_FP_CLS_COMBINE(X##_c, Y##_c)) \
  719. { \
  720. case _FP_CLS_COMBINE(FP_CLS_NORMAL,FP_CLS_NORMAL): \
  721. R##_c = FP_CLS_NORMAL; \
  722. R##_e = X##_e + Y##_e + 1; \
  723. \
  724. _FP_MUL_MEAT_##fs(R,X,Y); \
  725. \
  726. if (_FP_FRAC_OVERP_##wc(fs, R)) \
  727. _FP_FRAC_SRS_##wc(R, 1, _FP_WFRACBITS_##fs); \
  728. else \
  729. R##_e--; \
  730. break; \
  731. \
  732. case _FP_CLS_COMBINE(FP_CLS_NAN,FP_CLS_NAN): \
  733. _FP_CHOOSENAN(fs, wc, R, X, Y, '*'); \
  734. break; \
  735. \
  736. case _FP_CLS_COMBINE(FP_CLS_NAN,FP_CLS_NORMAL): \
  737. case _FP_CLS_COMBINE(FP_CLS_NAN,FP_CLS_INF): \
  738. case _FP_CLS_COMBINE(FP_CLS_NAN,FP_CLS_ZERO): \
  739. R##_s = X##_s; \
  740. \
  741. case _FP_CLS_COMBINE(FP_CLS_INF,FP_CLS_INF): \
  742. case _FP_CLS_COMBINE(FP_CLS_INF,FP_CLS_NORMAL): \
  743. case _FP_CLS_COMBINE(FP_CLS_ZERO,FP_CLS_NORMAL): \
  744. case _FP_CLS_COMBINE(FP_CLS_ZERO,FP_CLS_ZERO): \
  745. _FP_FRAC_COPY_##wc(R, X); \
  746. R##_c = X##_c; \
  747. break; \
  748. \
  749. case _FP_CLS_COMBINE(FP_CLS_NORMAL,FP_CLS_NAN): \
  750. case _FP_CLS_COMBINE(FP_CLS_INF,FP_CLS_NAN): \
  751. case _FP_CLS_COMBINE(FP_CLS_ZERO,FP_CLS_NAN): \
  752. R##_s = Y##_s; \
  753. \
  754. case _FP_CLS_COMBINE(FP_CLS_NORMAL,FP_CLS_INF): \
  755. case _FP_CLS_COMBINE(FP_CLS_NORMAL,FP_CLS_ZERO): \
  756. _FP_FRAC_COPY_##wc(R, Y); \
  757. R##_c = Y##_c; \
  758. break; \
  759. \
  760. case _FP_CLS_COMBINE(FP_CLS_INF,FP_CLS_ZERO): \
  761. case _FP_CLS_COMBINE(FP_CLS_ZERO,FP_CLS_INF): \
  762. R##_s = _FP_NANSIGN_##fs; \
  763. R##_c = FP_CLS_NAN; \
  764. _FP_FRAC_SET_##wc(R, _FP_NANFRAC_##fs); \
  765. FP_SET_EXCEPTION(FP_EX_INVALID); \
  766. break; \
  767. \
  768. default: \
  769. abort(); \
  770. } \
  771. } while (0)
  772. /*
  773. * Main division routine. The input values should be cooked.
  774. */
  775. #define _FP_DIV(fs, wc, R, X, Y) \
  776. do { \
  777. R##_s = X##_s ^ Y##_s; \
  778. switch (_FP_CLS_COMBINE(X##_c, Y##_c)) \
  779. { \
  780. case _FP_CLS_COMBINE(FP_CLS_NORMAL,FP_CLS_NORMAL): \
  781. R##_c = FP_CLS_NORMAL; \
  782. R##_e = X##_e - Y##_e; \
  783. \
  784. _FP_DIV_MEAT_##fs(R,X,Y); \
  785. break; \
  786. \
  787. case _FP_CLS_COMBINE(FP_CLS_NAN,FP_CLS_NAN): \
  788. _FP_CHOOSENAN(fs, wc, R, X, Y, '/'); \
  789. break; \
  790. \
  791. case _FP_CLS_COMBINE(FP_CLS_NAN,FP_CLS_NORMAL): \
  792. case _FP_CLS_COMBINE(FP_CLS_NAN,FP_CLS_INF): \
  793. case _FP_CLS_COMBINE(FP_CLS_NAN,FP_CLS_ZERO): \
  794. R##_s = X##_s; \
  795. _FP_FRAC_COPY_##wc(R, X); \
  796. R##_c = X##_c; \
  797. break; \
  798. \
  799. case _FP_CLS_COMBINE(FP_CLS_NORMAL,FP_CLS_NAN): \
  800. case _FP_CLS_COMBINE(FP_CLS_INF,FP_CLS_NAN): \
  801. case _FP_CLS_COMBINE(FP_CLS_ZERO,FP_CLS_NAN): \
  802. R##_s = Y##_s; \
  803. _FP_FRAC_COPY_##wc(R, Y); \
  804. R##_c = Y##_c; \
  805. break; \
  806. \
  807. case _FP_CLS_COMBINE(FP_CLS_NORMAL,FP_CLS_INF): \
  808. case _FP_CLS_COMBINE(FP_CLS_ZERO,FP_CLS_INF): \
  809. case _FP_CLS_COMBINE(FP_CLS_ZERO,FP_CLS_NORMAL): \
  810. R##_c = FP_CLS_ZERO; \
  811. break; \
  812. \
  813. case _FP_CLS_COMBINE(FP_CLS_NORMAL,FP_CLS_ZERO): \
  814. FP_SET_EXCEPTION(FP_EX_DIVZERO); \
  815. case _FP_CLS_COMBINE(FP_CLS_INF,FP_CLS_ZERO): \
  816. case _FP_CLS_COMBINE(FP_CLS_INF,FP_CLS_NORMAL): \
  817. R##_c = FP_CLS_INF; \
  818. break; \
  819. \
  820. case _FP_CLS_COMBINE(FP_CLS_INF,FP_CLS_INF): \
  821. case _FP_CLS_COMBINE(FP_CLS_ZERO,FP_CLS_ZERO): \
  822. R##_s = _FP_NANSIGN_##fs; \
  823. R##_c = FP_CLS_NAN; \
  824. _FP_FRAC_SET_##wc(R, _FP_NANFRAC_##fs); \
  825. FP_SET_EXCEPTION(FP_EX_INVALID); \
  826. break; \
  827. \
  828. default: \
  829. abort(); \
  830. } \
  831. } while (0)
  832. /*
  833. * Main differential comparison routine. The inputs should be raw not
  834. * cooked. The return is -1,0,1 for normal values, 2 otherwise.
  835. */
  836. #define _FP_CMP(fs, wc, ret, X, Y, un) \
  837. do { \
  838. /* NANs are unordered */ \
  839. if ((X##_e == _FP_EXPMAX_##fs && !_FP_FRAC_ZEROP_##wc(X)) \
  840. || (Y##_e == _FP_EXPMAX_##fs && !_FP_FRAC_ZEROP_##wc(Y))) \
  841. { \
  842. ret = un; \
  843. } \
  844. else \
  845. { \
  846. int __is_zero_x; \
  847. int __is_zero_y; \
  848. \
  849. __is_zero_x = (!X##_e && _FP_FRAC_ZEROP_##wc(X)) ? 1 : 0; \
  850. __is_zero_y = (!Y##_e && _FP_FRAC_ZEROP_##wc(Y)) ? 1 : 0; \
  851. \
  852. if (__is_zero_x && __is_zero_y) \
  853. ret = 0; \
  854. else if (__is_zero_x) \
  855. ret = Y##_s ? 1 : -1; \
  856. else if (__is_zero_y) \
  857. ret = X##_s ? -1 : 1; \
  858. else if (X##_s != Y##_s) \
  859. ret = X##_s ? -1 : 1; \
  860. else if (X##_e > Y##_e) \
  861. ret = X##_s ? -1 : 1; \
  862. else if (X##_e < Y##_e) \
  863. ret = X##_s ? 1 : -1; \
  864. else if (_FP_FRAC_GT_##wc(X, Y)) \
  865. ret = X##_s ? -1 : 1; \
  866. else if (_FP_FRAC_GT_##wc(Y, X)) \
  867. ret = X##_s ? 1 : -1; \
  868. else \
  869. ret = 0; \
  870. } \
  871. } while (0)
  872. /* Simplification for strict equality. */
  873. #define _FP_CMP_EQ(fs, wc, ret, X, Y) \
  874. do { \
  875. /* NANs are unordered */ \
  876. if ((X##_e == _FP_EXPMAX_##fs && !_FP_FRAC_ZEROP_##wc(X)) \
  877. || (Y##_e == _FP_EXPMAX_##fs && !_FP_FRAC_ZEROP_##wc(Y))) \
  878. { \
  879. ret = 1; \
  880. } \
  881. else \
  882. { \
  883. ret = !(X##_e == Y##_e \
  884. && _FP_FRAC_EQ_##wc(X, Y) \
  885. && (X##_s == Y##_s || (!X##_e && _FP_FRAC_ZEROP_##wc(X)))); \
  886. } \
  887. } while (0)
  888. /* Version to test unordered. */
  889. #define _FP_CMP_UNORD(fs, wc, ret, X, Y) \
  890. do { \
  891. ret = ((X##_e == _FP_EXPMAX_##fs && !_FP_FRAC_ZEROP_##wc(X)) \
  892. || (Y##_e == _FP_EXPMAX_##fs && !_FP_FRAC_ZEROP_##wc(Y))); \
  893. } while (0)
  894. /*
  895. * Main square root routine. The input value should be cooked.
  896. */
  897. #define _FP_SQRT(fs, wc, R, X) \
  898. do { \
  899. _FP_FRAC_DECL_##wc(T); _FP_FRAC_DECL_##wc(S); \
  900. _FP_W_TYPE q; \
  901. switch (X##_c) \
  902. { \
  903. case FP_CLS_NAN: \
  904. _FP_FRAC_COPY_##wc(R, X); \
  905. R##_s = X##_s; \
  906. R##_c = FP_CLS_NAN; \
  907. break; \
  908. case FP_CLS_INF: \
  909. if (X##_s) \
  910. { \
  911. R##_s = _FP_NANSIGN_##fs; \
  912. R##_c = FP_CLS_NAN; /* NAN */ \
  913. _FP_FRAC_SET_##wc(R, _FP_NANFRAC_##fs); \
  914. FP_SET_EXCEPTION(FP_EX_INVALID); \
  915. } \
  916. else \
  917. { \
  918. R##_s = 0; \
  919. R##_c = FP_CLS_INF; /* sqrt(+inf) = +inf */ \
  920. } \
  921. break; \
  922. case FP_CLS_ZERO: \
  923. R##_s = X##_s; \
  924. R##_c = FP_CLS_ZERO; /* sqrt(+-0) = +-0 */ \
  925. break; \
  926. case FP_CLS_NORMAL: \
  927. R##_s = 0; \
  928. if (X##_s) \
  929. { \
  930. R##_c = FP_CLS_NAN; /* sNAN */ \
  931. R##_s = _FP_NANSIGN_##fs; \
  932. _FP_FRAC_SET_##wc(R, _FP_NANFRAC_##fs); \
  933. FP_SET_EXCEPTION(FP_EX_INVALID); \
  934. break; \
  935. } \
  936. R##_c = FP_CLS_NORMAL; \
  937. if (X##_e & 1) \
  938. _FP_FRAC_SLL_##wc(X, 1); \
  939. R##_e = X##_e >> 1; \
  940. _FP_FRAC_SET_##wc(S, _FP_ZEROFRAC_##wc); \
  941. _FP_FRAC_SET_##wc(R, _FP_ZEROFRAC_##wc); \
  942. q = _FP_OVERFLOW_##fs >> 1; \
  943. _FP_SQRT_MEAT_##wc(R, S, T, X, q); \
  944. } \
  945. } while (0)
  946. /*
  947. * Convert from FP to integer. Input is raw.
  948. */
  949. /* RSIGNED can have following values:
  950. * 0: the number is required to be 0..(2^rsize)-1, if not, NV is set plus
  951. * the result is either 0 or (2^rsize)-1 depending on the sign in such
  952. * case.
  953. * 1: the number is required to be -(2^(rsize-1))..(2^(rsize-1))-1, if not,
  954. * NV is set plus the result is either -(2^(rsize-1)) or (2^(rsize-1))-1
  955. * depending on the sign in such case.
  956. * -1: the number is required to be -(2^(rsize-1))..(2^rsize)-1, if not, NV is
  957. * set plus the result is either -(2^(rsize-1)) or (2^(rsize-1))-1
  958. * depending on the sign in such case.
  959. */
  960. #define _FP_TO_INT(fs, wc, r, X, rsize, rsigned) \
  961. do { \
  962. if (X##_e < _FP_EXPBIAS_##fs) \
  963. { \
  964. r = 0; \
  965. if (X##_e == 0) \
  966. { \
  967. if (!_FP_FRAC_ZEROP_##wc(X)) \
  968. { \
  969. FP_SET_EXCEPTION(FP_EX_INEXACT); \
  970. FP_SET_EXCEPTION(FP_EX_DENORM); \
  971. } \
  972. } \
  973. else \
  974. FP_SET_EXCEPTION(FP_EX_INEXACT); \
  975. } \
  976. else if (X##_e >= _FP_EXPBIAS_##fs + rsize - (rsigned > 0 || X##_s) \
  977. || (!rsigned && X##_s)) \
  978. { \
  979. /* Overflow or converting to the most negative integer. */ \
  980. if (rsigned) \
  981. { \
  982. r = 1; \
  983. r <<= rsize - 1; \
  984. r -= 1 - X##_s; \
  985. } else { \
  986. r = 0; \
  987. if (X##_s) \
  988. r = ~r; \
  989. } \
  990. \
  991. if (rsigned && X##_s && X##_e == _FP_EXPBIAS_##fs + rsize - 1) \
  992. { \
  993. /* Possibly converting to most negative integer; check the \
  994. mantissa. */ \
  995. int inexact = 0; \
  996. (void)((_FP_FRACBITS_##fs > rsize) \
  997. ? ({ _FP_FRAC_SRST_##wc(X, inexact, \
  998. _FP_FRACBITS_##fs - rsize, \
  999. _FP_FRACBITS_##fs); 0; }) \
  1000. : 0); \
  1001. if (!_FP_FRAC_ZEROP_##wc(X)) \
  1002. FP_SET_EXCEPTION(FP_EX_INVALID); \
  1003. else if (inexact) \
  1004. FP_SET_EXCEPTION(FP_EX_INEXACT); \
  1005. } \
  1006. else \
  1007. FP_SET_EXCEPTION(FP_EX_INVALID); \
  1008. } \
  1009. else \
  1010. { \
  1011. _FP_FRAC_HIGH_RAW_##fs(X) |= _FP_IMPLBIT_##fs; \
  1012. if (X##_e >= _FP_EXPBIAS_##fs + _FP_FRACBITS_##fs - 1) \
  1013. { \
  1014. _FP_FRAC_ASSEMBLE_##wc(r, X, rsize); \
  1015. r <<= X##_e - _FP_EXPBIAS_##fs - _FP_FRACBITS_##fs + 1; \
  1016. } \
  1017. else \
  1018. { \
  1019. int inexact; \
  1020. _FP_FRAC_SRST_##wc(X, inexact, \
  1021. (_FP_FRACBITS_##fs + _FP_EXPBIAS_##fs - 1 \
  1022. - X##_e), \
  1023. _FP_FRACBITS_##fs); \
  1024. if (inexact) \
  1025. FP_SET_EXCEPTION(FP_EX_INEXACT); \
  1026. _FP_FRAC_ASSEMBLE_##wc(r, X, rsize); \
  1027. } \
  1028. if (rsigned && X##_s) \
  1029. r = -r; \
  1030. } \
  1031. } while (0)
  1032. /* Convert integer to fp. Output is raw. RTYPE is unsigned even if
  1033. input is signed. */
  1034. #define _FP_FROM_INT(fs, wc, X, r, rsize, rtype) \
  1035. do { \
  1036. if (r) \
  1037. { \
  1038. rtype ur_; \
  1039. \
  1040. if ((X##_s = (r < 0))) \
  1041. r = -(rtype)r; \
  1042. \
  1043. ur_ = (rtype) r; \
  1044. (void)((rsize <= _FP_W_TYPE_SIZE) \
  1045. ? ({ \
  1046. int lz_; \
  1047. __FP_CLZ(lz_, (_FP_W_TYPE)ur_); \
  1048. X##_e = _FP_EXPBIAS_##fs + _FP_W_TYPE_SIZE - 1 - lz_; \
  1049. }) \
  1050. : ((rsize <= 2 * _FP_W_TYPE_SIZE) \
  1051. ? ({ \
  1052. int lz_; \
  1053. __FP_CLZ_2(lz_, (_FP_W_TYPE)(ur_ >> _FP_W_TYPE_SIZE), \
  1054. (_FP_W_TYPE)ur_); \
  1055. X##_e = (_FP_EXPBIAS_##fs + 2 * _FP_W_TYPE_SIZE - 1 \
  1056. - lz_); \
  1057. }) \
  1058. : (abort(), 0))); \
  1059. \
  1060. if (rsize - 1 + _FP_EXPBIAS_##fs >= _FP_EXPMAX_##fs \
  1061. && X##_e >= _FP_EXPMAX_##fs) \
  1062. { \
  1063. /* Exponent too big; overflow to infinity. (May also \
  1064. happen after rounding below.) */ \
  1065. _FP_OVERFLOW_SEMIRAW(fs, wc, X); \
  1066. goto pack_semiraw; \
  1067. } \
  1068. \
  1069. if (rsize <= _FP_FRACBITS_##fs \
  1070. || X##_e < _FP_EXPBIAS_##fs + _FP_FRACBITS_##fs) \
  1071. { \
  1072. /* Exactly representable; shift left. */ \
  1073. _FP_FRAC_DISASSEMBLE_##wc(X, ur_, rsize); \
  1074. _FP_FRAC_SLL_##wc(X, (_FP_EXPBIAS_##fs \
  1075. + _FP_FRACBITS_##fs - 1 - X##_e)); \
  1076. } \
  1077. else \
  1078. { \
  1079. /* More bits in integer than in floating type; need to \
  1080. round. */ \
  1081. if (_FP_EXPBIAS_##fs + _FP_WFRACBITS_##fs - 1 < X##_e) \
  1082. ur_ = ((ur_ >> (X##_e - _FP_EXPBIAS_##fs \
  1083. - _FP_WFRACBITS_##fs + 1)) \
  1084. | ((ur_ << (rsize - (X##_e - _FP_EXPBIAS_##fs \
  1085. - _FP_WFRACBITS_##fs + 1))) \
  1086. != 0)); \
  1087. _FP_FRAC_DISASSEMBLE_##wc(X, ur_, rsize); \
  1088. if ((_FP_EXPBIAS_##fs + _FP_WFRACBITS_##fs - 1 - X##_e) > 0) \
  1089. _FP_FRAC_SLL_##wc(X, (_FP_EXPBIAS_##fs \
  1090. + _FP_WFRACBITS_##fs - 1 - X##_e)); \
  1091. _FP_FRAC_HIGH_##fs(X) &= ~(_FP_W_TYPE)_FP_IMPLBIT_SH_##fs; \
  1092. pack_semiraw: \
  1093. _FP_PACK_SEMIRAW(fs, wc, X); \
  1094. } \
  1095. } \
  1096. else \
  1097. { \
  1098. X##_s = 0; \
  1099. X##_e = 0; \
  1100. _FP_FRAC_SET_##wc(X, _FP_ZEROFRAC_##wc); \
  1101. } \
  1102. } while (0)
  1103. /* Extend from a narrower floating-point format to a wider one. Input
  1104. and output are raw. */
  1105. #define FP_EXTEND(dfs,sfs,dwc,swc,D,S) \
  1106. do { \
  1107. if (_FP_FRACBITS_##dfs < _FP_FRACBITS_##sfs \
  1108. || (_FP_EXPMAX_##dfs - _FP_EXPBIAS_##dfs \
  1109. < _FP_EXPMAX_##sfs - _FP_EXPBIAS_##sfs) \
  1110. || (_FP_EXPBIAS_##dfs < _FP_EXPBIAS_##sfs + _FP_FRACBITS_##sfs - 1 \
  1111. && _FP_EXPBIAS_##dfs != _FP_EXPBIAS_##sfs)) \
  1112. abort(); \
  1113. D##_s = S##_s; \
  1114. _FP_FRAC_COPY_##dwc##_##swc(D, S); \
  1115. if (_FP_EXP_NORMAL(sfs, swc, S)) \
  1116. { \
  1117. D##_e = S##_e + _FP_EXPBIAS_##dfs - _FP_EXPBIAS_##sfs; \
  1118. _FP_FRAC_SLL_##dwc(D, (_FP_FRACBITS_##dfs - _FP_FRACBITS_##sfs)); \
  1119. } \
  1120. else \
  1121. { \
  1122. if (S##_e == 0) \
  1123. { \
  1124. if (_FP_FRAC_ZEROP_##swc(S)) \
  1125. D##_e = 0; \
  1126. else if (_FP_EXPBIAS_##dfs \
  1127. < _FP_EXPBIAS_##sfs + _FP_FRACBITS_##sfs - 1) \
  1128. { \
  1129. FP_SET_EXCEPTION(FP_EX_DENORM); \
  1130. _FP_FRAC_SLL_##dwc(D, (_FP_FRACBITS_##dfs \
  1131. - _FP_FRACBITS_##sfs)); \
  1132. D##_e = 0; \
  1133. } \
  1134. else \
  1135. { \
  1136. int _lz; \
  1137. FP_SET_EXCEPTION(FP_EX_DENORM); \
  1138. _FP_FRAC_CLZ_##swc(_lz, S); \
  1139. _FP_FRAC_SLL_##dwc(D, \
  1140. _lz + _FP_FRACBITS_##dfs \
  1141. - _FP_FRACTBITS_##sfs); \
  1142. D##_e = (_FP_EXPBIAS_##dfs - _FP_EXPBIAS_##sfs + 1 \
  1143. + _FP_FRACXBITS_##sfs - _lz); \
  1144. } \
  1145. } \
  1146. else \
  1147. { \
  1148. D##_e = _FP_EXPMAX_##dfs; \
  1149. if (!_FP_FRAC_ZEROP_##swc(S)) \
  1150. { \
  1151. if (!(_FP_FRAC_HIGH_RAW_##sfs(S) & _FP_QNANBIT_##sfs)) \
  1152. FP_SET_EXCEPTION(FP_EX_INVALID); \
  1153. _FP_FRAC_SLL_##dwc(D, (_FP_FRACBITS_##dfs \
  1154. - _FP_FRACBITS_##sfs)); \
  1155. } \
  1156. } \
  1157. } \
  1158. } while (0)
  1159. /* Truncate from a wider floating-point format to a narrower one.
  1160. Input and output are semi-raw. */
  1161. #define FP_TRUNC(dfs,sfs,dwc,swc,D,S) \
  1162. do { \
  1163. if (_FP_FRACBITS_##sfs < _FP_FRACBITS_##dfs \
  1164. || (_FP_EXPBIAS_##sfs < _FP_EXPBIAS_##dfs + _FP_FRACBITS_##dfs - 1 \
  1165. && _FP_EXPBIAS_##sfs != _FP_EXPBIAS_##dfs)) \
  1166. abort(); \
  1167. D##_s = S##_s; \
  1168. if (_FP_EXP_NORMAL(sfs, swc, S)) \
  1169. { \
  1170. D##_e = S##_e + _FP_EXPBIAS_##dfs - _FP_EXPBIAS_##sfs; \
  1171. if (D##_e >= _FP_EXPMAX_##dfs) \
  1172. _FP_OVERFLOW_SEMIRAW(dfs, dwc, D); \
  1173. else \
  1174. { \
  1175. if (D##_e <= 0) \
  1176. { \
  1177. if (D##_e < 1 - _FP_FRACBITS_##dfs) \
  1178. { \
  1179. _FP_FRAC_SET_##swc(S, _FP_ZEROFRAC_##swc); \
  1180. _FP_FRAC_LOW_##swc(S) |= 1; \
  1181. } \
  1182. else \
  1183. { \
  1184. _FP_FRAC_HIGH_##sfs(S) |= _FP_IMPLBIT_SH_##sfs; \
  1185. _FP_FRAC_SRS_##swc(S, (_FP_WFRACBITS_##sfs \
  1186. - _FP_WFRACBITS_##dfs + 1 - D##_e), \
  1187. _FP_WFRACBITS_##sfs); \
  1188. } \
  1189. D##_e = 0; \
  1190. } \
  1191. else \
  1192. _FP_FRAC_SRS_##swc(S, (_FP_WFRACBITS_##sfs \
  1193. - _FP_WFRACBITS_##dfs), \
  1194. _FP_WFRACBITS_##sfs); \
  1195. _FP_FRAC_COPY_##dwc##_##swc(D, S); \
  1196. } \
  1197. } \
  1198. else \
  1199. { \
  1200. if (S##_e == 0) \
  1201. { \
  1202. D##_e = 0; \
  1203. if (_FP_FRAC_ZEROP_##swc(S)) \
  1204. _FP_FRAC_SET_##dwc(D, _FP_ZEROFRAC_##dwc); \
  1205. else \
  1206. { \
  1207. FP_SET_EXCEPTION(FP_EX_DENORM); \
  1208. if (_FP_EXPBIAS_##sfs \
  1209. < _FP_EXPBIAS_##dfs + _FP_FRACBITS_##dfs - 1) \
  1210. { \
  1211. _FP_FRAC_SRS_##swc(S, (_FP_WFRACBITS_##sfs \
  1212. - _FP_WFRACBITS_##dfs), \
  1213. _FP_WFRACBITS_##sfs); \
  1214. _FP_FRAC_COPY_##dwc##_##swc(D, S); \
  1215. } \
  1216. else \
  1217. { \
  1218. _FP_FRAC_SET_##dwc(D, _FP_ZEROFRAC_##dwc); \
  1219. _FP_FRAC_LOW_##dwc(D) |= 1; \
  1220. } \
  1221. } \
  1222. } \
  1223. else \
  1224. { \
  1225. D##_e = _FP_EXPMAX_##dfs; \
  1226. if (_FP_FRAC_ZEROP_##swc(S)) \
  1227. _FP_FRAC_SET_##dwc(D, _FP_ZEROFRAC_##dwc); \
  1228. else \
  1229. { \
  1230. _FP_CHECK_SIGNAN_SEMIRAW(sfs, swc, S); \
  1231. _FP_FRAC_SRL_##swc(S, (_FP_WFRACBITS_##sfs \
  1232. - _FP_WFRACBITS_##dfs)); \
  1233. _FP_FRAC_COPY_##dwc##_##swc(D, S); \
  1234. /* Semi-raw NaN must have all workbits cleared. */ \
  1235. _FP_FRAC_LOW_##dwc(D) \
  1236. &= ~(_FP_W_TYPE) ((1 << _FP_WORKBITS) - 1); \
  1237. _FP_FRAC_HIGH_##dfs(D) |= _FP_QNANBIT_SH_##dfs; \
  1238. } \
  1239. } \
  1240. } \
  1241. } while (0)
  1242. /*
  1243. * Helper primitives.
  1244. */
  1245. /* Count leading zeros in a word. */
  1246. #ifndef __FP_CLZ
  1247. /* GCC 3.4 and later provide the builtins for us. */
  1248. #define __FP_CLZ(r, x) \
  1249. do { \
  1250. if (sizeof (_FP_W_TYPE) == sizeof (unsigned int)) \
  1251. r = __builtin_clz (x); \
  1252. else if (sizeof (_FP_W_TYPE) == sizeof (unsigned long)) \
  1253. r = __builtin_clzl (x); \
  1254. else if (sizeof (_FP_W_TYPE) == sizeof (unsigned long long)) \
  1255. r = __builtin_clzll (x); \
  1256. else \
  1257. abort (); \
  1258. } while (0)
  1259. #endif /* ndef __FP_CLZ */
  1260. #define _FP_DIV_HELP_imm(q, r, n, d) \
  1261. do { \
  1262. q = n / d, r = n % d; \
  1263. } while (0)
  1264. /* A restoring bit-by-bit division primitive. */
  1265. #define _FP_DIV_MEAT_N_loop(fs, wc, R, X, Y) \
  1266. do { \
  1267. int count = _FP_WFRACBITS_##fs; \
  1268. _FP_FRAC_DECL_##wc (u); \
  1269. _FP_FRAC_DECL_##wc (v); \
  1270. _FP_FRAC_COPY_##wc (u, X); \
  1271. _FP_FRAC_COPY_##wc (v, Y); \
  1272. _FP_FRAC_SET_##wc (R, _FP_ZEROFRAC_##wc); \
  1273. /* Normalize U and V. */ \
  1274. _FP_FRAC_SLL_##wc (u, _FP_WFRACXBITS_##fs); \
  1275. _FP_FRAC_SLL_##wc (v, _FP_WFRACXBITS_##fs); \
  1276. /* First round. Since the operands are normalized, either the \
  1277. first or second bit will be set in the fraction. Produce a \
  1278. normalized result by checking which and adjusting the loop \
  1279. count and exponent accordingly. */ \
  1280. if (_FP_FRAC_GE_1 (u, v)) \
  1281. { \
  1282. _FP_FRAC_SUB_##wc (u, u, v); \
  1283. _FP_FRAC_LOW_##wc (R) |= 1; \
  1284. count--; \
  1285. } \
  1286. else \
  1287. R##_e--; \
  1288. /* Subsequent rounds. */ \
  1289. do { \
  1290. int msb = (_FP_WS_TYPE) _FP_FRAC_HIGH_##wc (u) < 0; \
  1291. _FP_FRAC_SLL_##wc (u, 1); \
  1292. _FP_FRAC_SLL_##wc (R, 1); \
  1293. if (msb || _FP_FRAC_GE_1 (u, v)) \
  1294. { \
  1295. _FP_FRAC_SUB_##wc (u, u, v); \
  1296. _FP_FRAC_LOW_##wc (R) |= 1; \
  1297. } \
  1298. } while (--count > 0); \
  1299. /* If there's anything left in U, the result is inexact. */ \
  1300. _FP_FRAC_LOW_##wc (R) |= !_FP_FRAC_ZEROP_##wc (u); \
  1301. } while (0)
  1302. #define _FP_DIV_MEAT_1_loop(fs, R, X, Y) _FP_DIV_MEAT_N_loop (fs, 1, R, X, Y)
  1303. #define _FP_DIV_MEAT_2_loop(fs, R, X, Y) _FP_DIV_MEAT_N_loop (fs, 2, R, X, Y)
  1304. #define _FP_DIV_MEAT_4_loop(fs, R, X, Y) _FP_DIV_MEAT_N_loop (fs, 4, R, X, Y)