op-2.h 20 KB

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