op-2.h 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617
  1. /* Software floating-point emulation.
  2. Basic two-word fraction declaration and manipulation.
  3. Copyright (C) 1997,1998,1999,2006,2007 Free Software Foundation, Inc.
  4. This file is part of the GNU C Library.
  5. Contributed by Richard Henderson (rth@cygnus.com),
  6. Jakub Jelinek (jj@ultra.linux.cz),
  7. David S. Miller (davem@redhat.com) and
  8. Peter Maydell (pmaydell@chiark.greenend.org.uk).
  9. The GNU C Library is free software; you can redistribute it and/or
  10. modify it under the terms of the GNU Lesser General Public
  11. License as published by the Free Software Foundation; either
  12. version 2.1 of the License, or (at your option) any later version.
  13. In addition to the permissions in the GNU Lesser General Public
  14. License, the Free Software Foundation gives you unlimited
  15. permission to link the compiled version of this file into
  16. combinations with other programs, and to distribute those
  17. combinations without any restriction coming from the use of this
  18. file. (The Lesser General Public License restrictions do apply in
  19. other respects; for example, they cover modification of the file,
  20. and distribution when not linked into a combine executable.)
  21. The GNU C Library is distributed in the hope that it will be useful,
  22. but WITHOUT ANY WARRANTY; without even the implied warranty of
  23. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  24. Lesser General Public License for more details.
  25. You should have received a copy of the GNU Lesser General Public
  26. License along with the GNU C Library; if not, write to the Free
  27. Software Foundation, 51 Franklin Street, Fifth Floor, Boston,
  28. MA 02110-1301, USA. */
  29. #define _FP_FRAC_DECL_2(X) _FP_W_TYPE X##_f0, X##_f1
  30. #define _FP_FRAC_COPY_2(D,S) (D##_f0 = S##_f0, D##_f1 = S##_f1)
  31. #define _FP_FRAC_SET_2(X,I) __FP_FRAC_SET_2(X, I)
  32. #define _FP_FRAC_HIGH_2(X) (X##_f1)
  33. #define _FP_FRAC_LOW_2(X) (X##_f0)
  34. #define _FP_FRAC_WORD_2(X,w) (X##_f##w)
  35. #define _FP_FRAC_SLL_2(X,N) \
  36. (void)(((N) < _FP_W_TYPE_SIZE) \
  37. ? ({ \
  38. if (__builtin_constant_p(N) && (N) == 1) \
  39. { \
  40. X##_f1 = X##_f1 + X##_f1 + (((_FP_WS_TYPE)(X##_f0)) < 0); \
  41. X##_f0 += X##_f0; \
  42. } \
  43. else \
  44. { \
  45. X##_f1 = X##_f1 << (N) | X##_f0 >> (_FP_W_TYPE_SIZE - (N)); \
  46. X##_f0 <<= (N); \
  47. } \
  48. 0; \
  49. }) \
  50. : ({ \
  51. X##_f1 = X##_f0 << ((N) - _FP_W_TYPE_SIZE); \
  52. X##_f0 = 0; \
  53. }))
  54. #define _FP_FRAC_SRL_2(X,N) \
  55. (void)(((N) < _FP_W_TYPE_SIZE) \
  56. ? ({ \
  57. X##_f0 = X##_f0 >> (N) | X##_f1 << (_FP_W_TYPE_SIZE - (N)); \
  58. X##_f1 >>= (N); \
  59. }) \
  60. : ({ \
  61. X##_f0 = X##_f1 >> ((N) - _FP_W_TYPE_SIZE); \
  62. X##_f1 = 0; \
  63. }))
  64. /* Right shift with sticky-lsb. */
  65. #define _FP_FRAC_SRST_2(X,S, N,sz) \
  66. (void)(((N) < _FP_W_TYPE_SIZE) \
  67. ? ({ \
  68. S = (__builtin_constant_p(N) && (N) == 1 \
  69. ? X##_f0 & 1 \
  70. : (X##_f0 << (_FP_W_TYPE_SIZE - (N))) != 0); \
  71. X##_f0 = (X##_f1 << (_FP_W_TYPE_SIZE - (N)) | X##_f0 >> (N)); \
  72. X##_f1 >>= (N); \
  73. }) \
  74. : ({ \
  75. S = ((((N) == _FP_W_TYPE_SIZE \
  76. ? 0 \
  77. : (X##_f1 << (2*_FP_W_TYPE_SIZE - (N)))) \
  78. | X##_f0) != 0); \
  79. X##_f0 = (X##_f1 >> ((N) - _FP_W_TYPE_SIZE)); \
  80. X##_f1 = 0; \
  81. }))
  82. #define _FP_FRAC_SRS_2(X,N,sz) \
  83. (void)(((N) < _FP_W_TYPE_SIZE) \
  84. ? ({ \
  85. X##_f0 = (X##_f1 << (_FP_W_TYPE_SIZE - (N)) | X##_f0 >> (N) | \
  86. (__builtin_constant_p(N) && (N) == 1 \
  87. ? X##_f0 & 1 \
  88. : (X##_f0 << (_FP_W_TYPE_SIZE - (N))) != 0)); \
  89. X##_f1 >>= (N); \
  90. }) \
  91. : ({ \
  92. X##_f0 = (X##_f1 >> ((N) - _FP_W_TYPE_SIZE) | \
  93. ((((N) == _FP_W_TYPE_SIZE \
  94. ? 0 \
  95. : (X##_f1 << (2*_FP_W_TYPE_SIZE - (N)))) \
  96. | X##_f0) != 0)); \
  97. X##_f1 = 0; \
  98. }))
  99. #define _FP_FRAC_ADDI_2(X,I) \
  100. __FP_FRAC_ADDI_2(X##_f1, X##_f0, I)
  101. #define _FP_FRAC_ADD_2(R,X,Y) \
  102. __FP_FRAC_ADD_2(R##_f1, R##_f0, X##_f1, X##_f0, Y##_f1, Y##_f0)
  103. #define _FP_FRAC_SUB_2(R,X,Y) \
  104. __FP_FRAC_SUB_2(R##_f1, R##_f0, X##_f1, X##_f0, Y##_f1, Y##_f0)
  105. #define _FP_FRAC_DEC_2(X,Y) \
  106. __FP_FRAC_DEC_2(X##_f1, X##_f0, Y##_f1, Y##_f0)
  107. #define _FP_FRAC_CLZ_2(R,X) \
  108. do { \
  109. if (X##_f1) \
  110. __FP_CLZ(R,X##_f1); \
  111. else \
  112. { \
  113. __FP_CLZ(R,X##_f0); \
  114. R += _FP_W_TYPE_SIZE; \
  115. } \
  116. } while(0)
  117. /* Predicates */
  118. #define _FP_FRAC_NEGP_2(X) ((_FP_WS_TYPE)X##_f1 < 0)
  119. #define _FP_FRAC_ZEROP_2(X) ((X##_f1 | X##_f0) == 0)
  120. #define _FP_FRAC_OVERP_2(fs,X) (_FP_FRAC_HIGH_##fs(X) & _FP_OVERFLOW_##fs)
  121. #define _FP_FRAC_CLEAR_OVERP_2(fs,X) (_FP_FRAC_HIGH_##fs(X) &= ~_FP_OVERFLOW_##fs)
  122. #define _FP_FRAC_EQ_2(X, Y) (X##_f1 == Y##_f1 && X##_f0 == Y##_f0)
  123. #define _FP_FRAC_GT_2(X, Y) \
  124. (X##_f1 > Y##_f1 || (X##_f1 == Y##_f1 && X##_f0 > Y##_f0))
  125. #define _FP_FRAC_GE_2(X, Y) \
  126. (X##_f1 > Y##_f1 || (X##_f1 == Y##_f1 && X##_f0 >= Y##_f0))
  127. #define _FP_ZEROFRAC_2 0, 0
  128. #define _FP_MINFRAC_2 0, 1
  129. #define _FP_MAXFRAC_2 (~(_FP_WS_TYPE)0), (~(_FP_WS_TYPE)0)
  130. /*
  131. * Internals
  132. */
  133. #define __FP_FRAC_SET_2(X,I1,I0) (X##_f0 = I0, X##_f1 = I1)
  134. #define __FP_CLZ_2(R, xh, xl) \
  135. do { \
  136. if (xh) \
  137. __FP_CLZ(R,xh); \
  138. else \
  139. { \
  140. __FP_CLZ(R,xl); \
  141. R += _FP_W_TYPE_SIZE; \
  142. } \
  143. } while(0)
  144. #if 0
  145. #ifndef __FP_FRAC_ADDI_2
  146. #define __FP_FRAC_ADDI_2(xh, xl, i) \
  147. (xh += ((xl += i) < i))
  148. #endif
  149. #ifndef __FP_FRAC_ADD_2
  150. #define __FP_FRAC_ADD_2(rh, rl, xh, xl, yh, yl) \
  151. (rh = xh + yh + ((rl = xl + yl) < xl))
  152. #endif
  153. #ifndef __FP_FRAC_SUB_2
  154. #define __FP_FRAC_SUB_2(rh, rl, xh, xl, yh, yl) \
  155. (rh = xh - yh - ((rl = xl - yl) > xl))
  156. #endif
  157. #ifndef __FP_FRAC_DEC_2
  158. #define __FP_FRAC_DEC_2(xh, xl, yh, yl) \
  159. do { \
  160. UWtype _t = xl; \
  161. xh -= yh + ((xl -= yl) > _t); \
  162. } while (0)
  163. #endif
  164. #else
  165. #undef __FP_FRAC_ADDI_2
  166. #define __FP_FRAC_ADDI_2(xh, xl, i) add_ssaaaa(xh, xl, xh, xl, 0, i)
  167. #undef __FP_FRAC_ADD_2
  168. #define __FP_FRAC_ADD_2 add_ssaaaa
  169. #undef __FP_FRAC_SUB_2
  170. #define __FP_FRAC_SUB_2 sub_ddmmss
  171. #undef __FP_FRAC_DEC_2
  172. #define __FP_FRAC_DEC_2(xh, xl, yh, yl) sub_ddmmss(xh, xl, xh, xl, yh, yl)
  173. #endif
  174. /*
  175. * Unpack the raw bits of a native fp value. Do not classify or
  176. * normalize the data.
  177. */
  178. #define _FP_UNPACK_RAW_2(fs, X, val) \
  179. do { \
  180. union _FP_UNION_##fs _flo; _flo.flt = (val); \
  181. \
  182. X##_f0 = _flo.bits.frac0; \
  183. X##_f1 = _flo.bits.frac1; \
  184. X##_e = _flo.bits.exp; \
  185. X##_s = _flo.bits.sign; \
  186. } while (0)
  187. #define _FP_UNPACK_RAW_2_P(fs, X, val) \
  188. do { \
  189. union _FP_UNION_##fs *_flo = \
  190. (union _FP_UNION_##fs *)(val); \
  191. \
  192. X##_f0 = _flo->bits.frac0; \
  193. X##_f1 = _flo->bits.frac1; \
  194. X##_e = _flo->bits.exp; \
  195. X##_s = _flo->bits.sign; \
  196. } while (0)
  197. /*
  198. * Repack the raw bits of a native fp value.
  199. */
  200. #define _FP_PACK_RAW_2(fs, val, X) \
  201. do { \
  202. union _FP_UNION_##fs _flo; \
  203. \
  204. _flo.bits.frac0 = X##_f0; \
  205. _flo.bits.frac1 = X##_f1; \
  206. _flo.bits.exp = X##_e; \
  207. _flo.bits.sign = X##_s; \
  208. \
  209. (val) = _flo.flt; \
  210. } while (0)
  211. #define _FP_PACK_RAW_2_P(fs, val, X) \
  212. do { \
  213. union _FP_UNION_##fs *_flo = \
  214. (union _FP_UNION_##fs *)(val); \
  215. \
  216. _flo->bits.frac0 = X##_f0; \
  217. _flo->bits.frac1 = X##_f1; \
  218. _flo->bits.exp = X##_e; \
  219. _flo->bits.sign = X##_s; \
  220. } while (0)
  221. /*
  222. * Multiplication algorithms:
  223. */
  224. /* Given a 1W * 1W => 2W primitive, do the extended multiplication. */
  225. #define _FP_MUL_MEAT_2_wide(wfracbits, R, X, Y, doit) \
  226. do { \
  227. _FP_FRAC_DECL_4(_z); _FP_FRAC_DECL_2(_b); _FP_FRAC_DECL_2(_c); \
  228. \
  229. doit(_FP_FRAC_WORD_4(_z,1), _FP_FRAC_WORD_4(_z,0), X##_f0, Y##_f0); \
  230. doit(_b_f1, _b_f0, X##_f0, Y##_f1); \
  231. doit(_c_f1, _c_f0, X##_f1, Y##_f0); \
  232. doit(_FP_FRAC_WORD_4(_z,3), _FP_FRAC_WORD_4(_z,2), X##_f1, Y##_f1); \
  233. \
  234. __FP_FRAC_ADD_3(_FP_FRAC_WORD_4(_z,3),_FP_FRAC_WORD_4(_z,2), \
  235. _FP_FRAC_WORD_4(_z,1), 0, _b_f1, _b_f0, \
  236. _FP_FRAC_WORD_4(_z,3),_FP_FRAC_WORD_4(_z,2), \
  237. _FP_FRAC_WORD_4(_z,1)); \
  238. __FP_FRAC_ADD_3(_FP_FRAC_WORD_4(_z,3),_FP_FRAC_WORD_4(_z,2), \
  239. _FP_FRAC_WORD_4(_z,1), 0, _c_f1, _c_f0, \
  240. _FP_FRAC_WORD_4(_z,3),_FP_FRAC_WORD_4(_z,2), \
  241. _FP_FRAC_WORD_4(_z,1)); \
  242. \
  243. /* Normalize since we know where the msb of the multiplicands \
  244. were (bit B), we know that the msb of the of the product is \
  245. at either 2B or 2B-1. */ \
  246. _FP_FRAC_SRS_4(_z, wfracbits-1, 2*wfracbits); \
  247. R##_f0 = _FP_FRAC_WORD_4(_z,0); \
  248. R##_f1 = _FP_FRAC_WORD_4(_z,1); \
  249. } while (0)
  250. /* Given a 1W * 1W => 2W primitive, do the extended multiplication.
  251. Do only 3 multiplications instead of four. This one is for machines
  252. where multiplication is much more expensive than subtraction. */
  253. #define _FP_MUL_MEAT_2_wide_3mul(wfracbits, R, X, Y, doit) \
  254. do { \
  255. _FP_FRAC_DECL_4(_z); _FP_FRAC_DECL_2(_b); _FP_FRAC_DECL_2(_c); \
  256. _FP_W_TYPE _d; \
  257. int _c1, _c2; \
  258. \
  259. _b_f0 = X##_f0 + X##_f1; \
  260. _c1 = _b_f0 < X##_f0; \
  261. _b_f1 = Y##_f0 + Y##_f1; \
  262. _c2 = _b_f1 < Y##_f0; \
  263. doit(_d, _FP_FRAC_WORD_4(_z,0), X##_f0, Y##_f0); \
  264. doit(_FP_FRAC_WORD_4(_z,2), _FP_FRAC_WORD_4(_z,1), _b_f0, _b_f1); \
  265. doit(_c_f1, _c_f0, X##_f1, Y##_f1); \
  266. \
  267. _b_f0 &= -_c2; \
  268. _b_f1 &= -_c1; \
  269. __FP_FRAC_ADD_3(_FP_FRAC_WORD_4(_z,3),_FP_FRAC_WORD_4(_z,2), \
  270. _FP_FRAC_WORD_4(_z,1), (_c1 & _c2), 0, _d, \
  271. 0, _FP_FRAC_WORD_4(_z,2), _FP_FRAC_WORD_4(_z,1)); \
  272. __FP_FRAC_ADDI_2(_FP_FRAC_WORD_4(_z,3),_FP_FRAC_WORD_4(_z,2), \
  273. _b_f0); \
  274. __FP_FRAC_ADDI_2(_FP_FRAC_WORD_4(_z,3),_FP_FRAC_WORD_4(_z,2), \
  275. _b_f1); \
  276. __FP_FRAC_DEC_3(_FP_FRAC_WORD_4(_z,3),_FP_FRAC_WORD_4(_z,2), \
  277. _FP_FRAC_WORD_4(_z,1), \
  278. 0, _d, _FP_FRAC_WORD_4(_z,0)); \
  279. __FP_FRAC_DEC_3(_FP_FRAC_WORD_4(_z,3),_FP_FRAC_WORD_4(_z,2), \
  280. _FP_FRAC_WORD_4(_z,1), 0, _c_f1, _c_f0); \
  281. __FP_FRAC_ADD_2(_FP_FRAC_WORD_4(_z,3), _FP_FRAC_WORD_4(_z,2), \
  282. _c_f1, _c_f0, \
  283. _FP_FRAC_WORD_4(_z,3), _FP_FRAC_WORD_4(_z,2)); \
  284. \
  285. /* Normalize since we know where the msb of the multiplicands \
  286. were (bit B), we know that the msb of the of the product is \
  287. at either 2B or 2B-1. */ \
  288. _FP_FRAC_SRS_4(_z, wfracbits-1, 2*wfracbits); \
  289. R##_f0 = _FP_FRAC_WORD_4(_z,0); \
  290. R##_f1 = _FP_FRAC_WORD_4(_z,1); \
  291. } while (0)
  292. #define _FP_MUL_MEAT_2_gmp(wfracbits, R, X, Y) \
  293. do { \
  294. _FP_FRAC_DECL_4(_z); \
  295. _FP_W_TYPE _x[2], _y[2]; \
  296. _x[0] = X##_f0; _x[1] = X##_f1; \
  297. _y[0] = Y##_f0; _y[1] = Y##_f1; \
  298. \
  299. mpn_mul_n(_z_f, _x, _y, 2); \
  300. \
  301. /* Normalize since we know where the msb of the multiplicands \
  302. were (bit B), we know that the msb of the of the product is \
  303. at either 2B or 2B-1. */ \
  304. _FP_FRAC_SRS_4(_z, wfracbits-1, 2*wfracbits); \
  305. R##_f0 = _z_f[0]; \
  306. R##_f1 = _z_f[1]; \
  307. } while (0)
  308. /* Do at most 120x120=240 bits multiplication using double floating
  309. point multiplication. This is useful if floating point
  310. multiplication has much bigger throughput than integer multiply.
  311. It is supposed to work for _FP_W_TYPE_SIZE 64 and wfracbits
  312. between 106 and 120 only.
  313. Caller guarantees that X and Y has (1LLL << (wfracbits - 1)) set.
  314. SETFETZ is a macro which will disable all FPU exceptions and set rounding
  315. towards zero, RESETFE should optionally reset it back. */
  316. #define _FP_MUL_MEAT_2_120_240_double(wfracbits, R, X, Y, setfetz, resetfe) \
  317. do { \
  318. static const double _const[] = { \
  319. /* 2^-24 */ 5.9604644775390625e-08, \
  320. /* 2^-48 */ 3.5527136788005009e-15, \
  321. /* 2^-72 */ 2.1175823681357508e-22, \
  322. /* 2^-96 */ 1.2621774483536189e-29, \
  323. /* 2^28 */ 2.68435456e+08, \
  324. /* 2^4 */ 1.600000e+01, \
  325. /* 2^-20 */ 9.5367431640625e-07, \
  326. /* 2^-44 */ 5.6843418860808015e-14, \
  327. /* 2^-68 */ 3.3881317890172014e-21, \
  328. /* 2^-92 */ 2.0194839173657902e-28, \
  329. /* 2^-116 */ 1.2037062152420224e-35}; \
  330. double _a240, _b240, _c240, _d240, _e240, _f240, \
  331. _g240, _h240, _i240, _j240, _k240; \
  332. union { double d; UDItype i; } _l240, _m240, _n240, _o240, \
  333. _p240, _q240, _r240, _s240; \
  334. UDItype _t240, _u240, _v240, _w240, _x240, _y240 = 0; \
  335. \
  336. if (wfracbits < 106 || wfracbits > 120) \
  337. abort(); \
  338. \
  339. setfetz; \
  340. \
  341. _e240 = (double)(long)(X##_f0 & 0xffffff); \
  342. _j240 = (double)(long)(Y##_f0 & 0xffffff); \
  343. _d240 = (double)(long)((X##_f0 >> 24) & 0xffffff); \
  344. _i240 = (double)(long)((Y##_f0 >> 24) & 0xffffff); \
  345. _c240 = (double)(long)(((X##_f1 << 16) & 0xffffff) | (X##_f0 >> 48)); \
  346. _h240 = (double)(long)(((Y##_f1 << 16) & 0xffffff) | (Y##_f0 >> 48)); \
  347. _b240 = (double)(long)((X##_f1 >> 8) & 0xffffff); \
  348. _g240 = (double)(long)((Y##_f1 >> 8) & 0xffffff); \
  349. _a240 = (double)(long)(X##_f1 >> 32); \
  350. _f240 = (double)(long)(Y##_f1 >> 32); \
  351. _e240 *= _const[3]; \
  352. _j240 *= _const[3]; \
  353. _d240 *= _const[2]; \
  354. _i240 *= _const[2]; \
  355. _c240 *= _const[1]; \
  356. _h240 *= _const[1]; \
  357. _b240 *= _const[0]; \
  358. _g240 *= _const[0]; \
  359. _s240.d = _e240*_j240;\
  360. _r240.d = _d240*_j240 + _e240*_i240;\
  361. _q240.d = _c240*_j240 + _d240*_i240 + _e240*_h240;\
  362. _p240.d = _b240*_j240 + _c240*_i240 + _d240*_h240 + _e240*_g240;\
  363. _o240.d = _a240*_j240 + _b240*_i240 + _c240*_h240 + _d240*_g240 + _e240*_f240;\
  364. _n240.d = _a240*_i240 + _b240*_h240 + _c240*_g240 + _d240*_f240; \
  365. _m240.d = _a240*_h240 + _b240*_g240 + _c240*_f240; \
  366. _l240.d = _a240*_g240 + _b240*_f240; \
  367. _k240 = _a240*_f240; \
  368. _r240.d += _s240.d; \
  369. _q240.d += _r240.d; \
  370. _p240.d += _q240.d; \
  371. _o240.d += _p240.d; \
  372. _n240.d += _o240.d; \
  373. _m240.d += _n240.d; \
  374. _l240.d += _m240.d; \
  375. _k240 += _l240.d; \
  376. _s240.d -= ((_const[10]+_s240.d)-_const[10]); \
  377. _r240.d -= ((_const[9]+_r240.d)-_const[9]); \
  378. _q240.d -= ((_const[8]+_q240.d)-_const[8]); \
  379. _p240.d -= ((_const[7]+_p240.d)-_const[7]); \
  380. _o240.d += _const[7]; \
  381. _n240.d += _const[6]; \
  382. _m240.d += _const[5]; \
  383. _l240.d += _const[4]; \
  384. if (_s240.d != 0.0) _y240 = 1; \
  385. if (_r240.d != 0.0) _y240 = 1; \
  386. if (_q240.d != 0.0) _y240 = 1; \
  387. if (_p240.d != 0.0) _y240 = 1; \
  388. _t240 = (DItype)_k240; \
  389. _u240 = _l240.i; \
  390. _v240 = _m240.i; \
  391. _w240 = _n240.i; \
  392. _x240 = _o240.i; \
  393. R##_f1 = (_t240 << (128 - (wfracbits - 1))) \
  394. | ((_u240 & 0xffffff) >> ((wfracbits - 1) - 104)); \
  395. R##_f0 = ((_u240 & 0xffffff) << (168 - (wfracbits - 1))) \
  396. | ((_v240 & 0xffffff) << (144 - (wfracbits - 1))) \
  397. | ((_w240 & 0xffffff) << (120 - (wfracbits - 1))) \
  398. | ((_x240 & 0xffffff) >> ((wfracbits - 1) - 96)) \
  399. | _y240; \
  400. resetfe; \
  401. } while (0)
  402. /*
  403. * Division algorithms:
  404. */
  405. #define _FP_DIV_MEAT_2_udiv(fs, R, X, Y) \
  406. do { \
  407. _FP_W_TYPE _n_f2, _n_f1, _n_f0, _r_f1, _r_f0, _m_f1, _m_f0; \
  408. if (_FP_FRAC_GT_2(X, Y)) \
  409. { \
  410. _n_f2 = X##_f1 >> 1; \
  411. _n_f1 = X##_f1 << (_FP_W_TYPE_SIZE - 1) | X##_f0 >> 1; \
  412. _n_f0 = X##_f0 << (_FP_W_TYPE_SIZE - 1); \
  413. } \
  414. else \
  415. { \
  416. R##_e--; \
  417. _n_f2 = X##_f1; \
  418. _n_f1 = X##_f0; \
  419. _n_f0 = 0; \
  420. } \
  421. \
  422. /* Normalize, i.e. make the most significant bit of the \
  423. denominator set. */ \
  424. _FP_FRAC_SLL_2(Y, _FP_WFRACXBITS_##fs); \
  425. \
  426. udiv_qrnnd(R##_f1, _r_f1, _n_f2, _n_f1, Y##_f1); \
  427. umul_ppmm(_m_f1, _m_f0, R##_f1, Y##_f0); \
  428. _r_f0 = _n_f0; \
  429. if (_FP_FRAC_GT_2(_m, _r)) \
  430. { \
  431. R##_f1--; \
  432. _FP_FRAC_ADD_2(_r, Y, _r); \
  433. if (_FP_FRAC_GE_2(_r, Y) && _FP_FRAC_GT_2(_m, _r)) \
  434. { \
  435. R##_f1--; \
  436. _FP_FRAC_ADD_2(_r, Y, _r); \
  437. } \
  438. } \
  439. _FP_FRAC_DEC_2(_r, _m); \
  440. \
  441. if (_r_f1 == Y##_f1) \
  442. { \
  443. /* This is a special case, not an optimization \
  444. (_r/Y##_f1 would not fit into UWtype). \
  445. As _r is guaranteed to be < Y, R##_f0 can be either \
  446. (UWtype)-1 or (UWtype)-2. But as we know what kind \
  447. of bits it is (sticky, guard, round), we don't care. \
  448. We also don't care what the reminder is, because the \
  449. guard bit will be set anyway. -jj */ \
  450. R##_f0 = -1; \
  451. } \
  452. else \
  453. { \
  454. udiv_qrnnd(R##_f0, _r_f1, _r_f1, _r_f0, Y##_f1); \
  455. umul_ppmm(_m_f1, _m_f0, R##_f0, Y##_f0); \
  456. _r_f0 = 0; \
  457. if (_FP_FRAC_GT_2(_m, _r)) \
  458. { \
  459. R##_f0--; \
  460. _FP_FRAC_ADD_2(_r, Y, _r); \
  461. if (_FP_FRAC_GE_2(_r, Y) && _FP_FRAC_GT_2(_m, _r)) \
  462. { \
  463. R##_f0--; \
  464. _FP_FRAC_ADD_2(_r, Y, _r); \
  465. } \
  466. } \
  467. if (!_FP_FRAC_EQ_2(_r, _m)) \
  468. R##_f0 |= _FP_WORK_STICKY; \
  469. } \
  470. } while (0)
  471. #define _FP_DIV_MEAT_2_gmp(fs, R, X, Y) \
  472. do { \
  473. _FP_W_TYPE _x[4], _y[2], _z[4]; \
  474. _y[0] = Y##_f0; _y[1] = Y##_f1; \
  475. _x[0] = _x[3] = 0; \
  476. if (_FP_FRAC_GT_2(X, Y)) \
  477. { \
  478. R##_e++; \
  479. _x[1] = (X##_f0 << (_FP_WFRACBITS_##fs-1 - _FP_W_TYPE_SIZE) | \
  480. X##_f1 >> (_FP_W_TYPE_SIZE - \
  481. (_FP_WFRACBITS_##fs-1 - _FP_W_TYPE_SIZE))); \
  482. _x[2] = X##_f1 << (_FP_WFRACBITS_##fs-1 - _FP_W_TYPE_SIZE); \
  483. } \
  484. else \
  485. { \
  486. _x[1] = (X##_f0 << (_FP_WFRACBITS_##fs - _FP_W_TYPE_SIZE) | \
  487. X##_f1 >> (_FP_W_TYPE_SIZE - \
  488. (_FP_WFRACBITS_##fs - _FP_W_TYPE_SIZE))); \
  489. _x[2] = X##_f1 << (_FP_WFRACBITS_##fs - _FP_W_TYPE_SIZE); \
  490. } \
  491. \
  492. (void) mpn_divrem (_z, 0, _x, 4, _y, 2); \
  493. R##_f1 = _z[1]; \
  494. R##_f0 = _z[0] | ((_x[0] | _x[1]) != 0); \
  495. } while (0)
  496. /*
  497. * Square root algorithms:
  498. * We have just one right now, maybe Newton approximation
  499. * should be added for those machines where division is fast.
  500. */
  501. #define _FP_SQRT_MEAT_2(R, S, T, X, q) \
  502. do { \
  503. while (q) \
  504. { \
  505. T##_f1 = S##_f1 + q; \
  506. if (T##_f1 <= X##_f1) \
  507. { \
  508. S##_f1 = T##_f1 + q; \
  509. X##_f1 -= T##_f1; \
  510. R##_f1 += q; \
  511. } \
  512. _FP_FRAC_SLL_2(X, 1); \
  513. q >>= 1; \
  514. } \
  515. q = (_FP_W_TYPE)1 << (_FP_W_TYPE_SIZE - 1); \
  516. while (q != _FP_WORK_ROUND) \
  517. { \
  518. T##_f0 = S##_f0 + q; \
  519. T##_f1 = S##_f1; \
  520. if (T##_f1 < X##_f1 || \
  521. (T##_f1 == X##_f1 && T##_f0 <= X##_f0)) \
  522. { \
  523. S##_f0 = T##_f0 + q; \
  524. S##_f1 += (T##_f0 > S##_f0); \
  525. _FP_FRAC_DEC_2(X, T); \
  526. R##_f0 += q; \
  527. } \
  528. _FP_FRAC_SLL_2(X, 1); \
  529. q >>= 1; \
  530. } \
  531. if (X##_f0 | X##_f1) \
  532. { \
  533. if (S##_f1 < X##_f1 || \
  534. (S##_f1 == X##_f1 && S##_f0 < X##_f0)) \
  535. R##_f0 |= _FP_WORK_ROUND; \
  536. R##_f0 |= _FP_WORK_STICKY; \
  537. } \
  538. } while (0)
  539. /*
  540. * Assembly/disassembly for converting to/from integral types.
  541. * No shifting or overflow handled here.
  542. */
  543. #define _FP_FRAC_ASSEMBLE_2(r, X, rsize) \
  544. (void)((rsize <= _FP_W_TYPE_SIZE) \
  545. ? ({ r = X##_f0; }) \
  546. : ({ \
  547. r = X##_f1; \
  548. r <<= _FP_W_TYPE_SIZE; \
  549. r += X##_f0; \
  550. }))
  551. #define _FP_FRAC_DISASSEMBLE_2(X, r, rsize) \
  552. do { \
  553. X##_f0 = r; \
  554. X##_f1 = (rsize <= _FP_W_TYPE_SIZE ? 0 : r >> _FP_W_TYPE_SIZE); \
  555. } while (0)
  556. /*
  557. * Convert FP values between word sizes
  558. */
  559. #define _FP_FRAC_COPY_1_2(D, S) (D##_f = S##_f0)
  560. #define _FP_FRAC_COPY_2_1(D, S) ((D##_f0 = S##_f), (D##_f1 = 0))
  561. #define _FP_FRAC_COPY_2_2(D,S) _FP_FRAC_COPY_2(D,S)