op-2.h 20 KB

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