op-4.h 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687
  1. /* Software floating-point emulation.
  2. Basic four-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_4(X) _FP_W_TYPE X##_f[4]
  29. #define _FP_FRAC_COPY_4(D,S) \
  30. (D##_f[0] = S##_f[0], D##_f[1] = S##_f[1], \
  31. D##_f[2] = S##_f[2], D##_f[3] = S##_f[3])
  32. #define _FP_FRAC_SET_4(X,I) __FP_FRAC_SET_4(X, I)
  33. #define _FP_FRAC_HIGH_4(X) (X##_f[3])
  34. #define _FP_FRAC_LOW_4(X) (X##_f[0])
  35. #define _FP_FRAC_WORD_4(X,w) (X##_f[w])
  36. #define _FP_FRAC_SLL_4(X,N) \
  37. do { \
  38. _FP_I_TYPE _up, _down, _skip, _i; \
  39. _skip = (N) / _FP_W_TYPE_SIZE; \
  40. _up = (N) % _FP_W_TYPE_SIZE; \
  41. _down = _FP_W_TYPE_SIZE - _up; \
  42. if (!_up) \
  43. for (_i = 3; _i >= _skip; --_i) \
  44. X##_f[_i] = X##_f[_i-_skip]; \
  45. else \
  46. { \
  47. for (_i = 3; _i > _skip; --_i) \
  48. X##_f[_i] = X##_f[_i-_skip] << _up \
  49. | X##_f[_i-_skip-1] >> _down; \
  50. X##_f[_i--] = X##_f[0] << _up; \
  51. } \
  52. for (; _i >= 0; --_i) \
  53. X##_f[_i] = 0; \
  54. } while (0)
  55. /* This one was broken too */
  56. #define _FP_FRAC_SRL_4(X,N) \
  57. do { \
  58. _FP_I_TYPE _up, _down, _skip, _i; \
  59. _skip = (N) / _FP_W_TYPE_SIZE; \
  60. _down = (N) % _FP_W_TYPE_SIZE; \
  61. _up = _FP_W_TYPE_SIZE - _down; \
  62. if (!_down) \
  63. for (_i = 0; _i <= 3-_skip; ++_i) \
  64. X##_f[_i] = X##_f[_i+_skip]; \
  65. else \
  66. { \
  67. for (_i = 0; _i < 3-_skip; ++_i) \
  68. X##_f[_i] = X##_f[_i+_skip] >> _down \
  69. | X##_f[_i+_skip+1] << _up; \
  70. X##_f[_i++] = X##_f[3] >> _down; \
  71. } \
  72. for (; _i < 4; ++_i) \
  73. X##_f[_i] = 0; \
  74. } while (0)
  75. /* Right shift with sticky-lsb.
  76. * What this actually means is that we do a standard right-shift,
  77. * but that if any of the bits that fall off the right hand side
  78. * were one then we always set the LSbit.
  79. */
  80. #define _FP_FRAC_SRST_4(X,S,N,size) \
  81. do { \
  82. _FP_I_TYPE _up, _down, _skip, _i; \
  83. _FP_W_TYPE _s; \
  84. _skip = (N) / _FP_W_TYPE_SIZE; \
  85. _down = (N) % _FP_W_TYPE_SIZE; \
  86. _up = _FP_W_TYPE_SIZE - _down; \
  87. for (_s = _i = 0; _i < _skip; ++_i) \
  88. _s |= X##_f[_i]; \
  89. if (!_down) \
  90. for (_i = 0; _i <= 3-_skip; ++_i) \
  91. X##_f[_i] = X##_f[_i+_skip]; \
  92. else \
  93. { \
  94. _s |= X##_f[_i] << _up; \
  95. for (_i = 0; _i < 3-_skip; ++_i) \
  96. X##_f[_i] = X##_f[_i+_skip] >> _down \
  97. | X##_f[_i+_skip+1] << _up; \
  98. X##_f[_i++] = X##_f[3] >> _down; \
  99. } \
  100. for (; _i < 4; ++_i) \
  101. X##_f[_i] = 0; \
  102. S = (_s != 0); \
  103. } while (0)
  104. #define _FP_FRAC_SRS_4(X,N,size) \
  105. do { \
  106. int _sticky; \
  107. _FP_FRAC_SRST_4(X, _sticky, N, size); \
  108. X##_f[0] |= _sticky; \
  109. } while (0)
  110. #define _FP_FRAC_ADD_4(R,X,Y) \
  111. __FP_FRAC_ADD_4(R##_f[3], R##_f[2], R##_f[1], R##_f[0], \
  112. X##_f[3], X##_f[2], X##_f[1], X##_f[0], \
  113. Y##_f[3], Y##_f[2], Y##_f[1], Y##_f[0])
  114. #define _FP_FRAC_SUB_4(R,X,Y) \
  115. __FP_FRAC_SUB_4(R##_f[3], R##_f[2], R##_f[1], R##_f[0], \
  116. X##_f[3], X##_f[2], X##_f[1], X##_f[0], \
  117. Y##_f[3], Y##_f[2], Y##_f[1], Y##_f[0])
  118. #define _FP_FRAC_DEC_4(X,Y) \
  119. __FP_FRAC_DEC_4(X##_f[3], X##_f[2], X##_f[1], X##_f[0], \
  120. Y##_f[3], Y##_f[2], Y##_f[1], Y##_f[0])
  121. #define _FP_FRAC_ADDI_4(X,I) \
  122. __FP_FRAC_ADDI_4(X##_f[3], X##_f[2], X##_f[1], X##_f[0], I)
  123. #define _FP_ZEROFRAC_4 0,0,0,0
  124. #define _FP_MINFRAC_4 0,0,0,1
  125. #define _FP_MAXFRAC_4 (~(_FP_WS_TYPE)0), (~(_FP_WS_TYPE)0), (~(_FP_WS_TYPE)0), (~(_FP_WS_TYPE)0)
  126. #define _FP_FRAC_ZEROP_4(X) ((X##_f[0] | X##_f[1] | X##_f[2] | X##_f[3]) == 0)
  127. #define _FP_FRAC_NEGP_4(X) ((_FP_WS_TYPE)X##_f[3] < 0)
  128. #define _FP_FRAC_OVERP_4(fs,X) (_FP_FRAC_HIGH_##fs(X) & _FP_OVERFLOW_##fs)
  129. #define _FP_FRAC_CLEAR_OVERP_4(fs,X) (_FP_FRAC_HIGH_##fs(X) &= ~_FP_OVERFLOW_##fs)
  130. #define _FP_FRAC_EQ_4(X,Y) \
  131. (X##_f[0] == Y##_f[0] && X##_f[1] == Y##_f[1] \
  132. && X##_f[2] == Y##_f[2] && X##_f[3] == Y##_f[3])
  133. #define _FP_FRAC_GT_4(X,Y) \
  134. (X##_f[3] > Y##_f[3] || \
  135. (X##_f[3] == Y##_f[3] && (X##_f[2] > Y##_f[2] || \
  136. (X##_f[2] == Y##_f[2] && (X##_f[1] > Y##_f[1] || \
  137. (X##_f[1] == Y##_f[1] && X##_f[0] > Y##_f[0]) \
  138. )) \
  139. )) \
  140. )
  141. #define _FP_FRAC_GE_4(X,Y) \
  142. (X##_f[3] > Y##_f[3] || \
  143. (X##_f[3] == Y##_f[3] && (X##_f[2] > Y##_f[2] || \
  144. (X##_f[2] == Y##_f[2] && (X##_f[1] > Y##_f[1] || \
  145. (X##_f[1] == Y##_f[1] && X##_f[0] >= Y##_f[0]) \
  146. )) \
  147. )) \
  148. )
  149. #define _FP_FRAC_CLZ_4(R,X) \
  150. do { \
  151. if (X##_f[3]) \
  152. { \
  153. __FP_CLZ(R,X##_f[3]); \
  154. } \
  155. else if (X##_f[2]) \
  156. { \
  157. __FP_CLZ(R,X##_f[2]); \
  158. R += _FP_W_TYPE_SIZE; \
  159. } \
  160. else if (X##_f[1]) \
  161. { \
  162. __FP_CLZ(R,X##_f[1]); \
  163. R += _FP_W_TYPE_SIZE*2; \
  164. } \
  165. else \
  166. { \
  167. __FP_CLZ(R,X##_f[0]); \
  168. R += _FP_W_TYPE_SIZE*3; \
  169. } \
  170. } while(0)
  171. #define _FP_UNPACK_RAW_4(fs, X, val) \
  172. do { \
  173. union _FP_UNION_##fs _flo; _flo.flt = (val); \
  174. X##_f[0] = _flo.bits.frac0; \
  175. X##_f[1] = _flo.bits.frac1; \
  176. X##_f[2] = _flo.bits.frac2; \
  177. X##_f[3] = _flo.bits.frac3; \
  178. X##_e = _flo.bits.exp; \
  179. X##_s = _flo.bits.sign; \
  180. } while (0)
  181. #define _FP_UNPACK_RAW_4_P(fs, X, val) \
  182. do { \
  183. union _FP_UNION_##fs *_flo = \
  184. (union _FP_UNION_##fs *)(val); \
  185. \
  186. X##_f[0] = _flo->bits.frac0; \
  187. X##_f[1] = _flo->bits.frac1; \
  188. X##_f[2] = _flo->bits.frac2; \
  189. X##_f[3] = _flo->bits.frac3; \
  190. X##_e = _flo->bits.exp; \
  191. X##_s = _flo->bits.sign; \
  192. } while (0)
  193. #define _FP_PACK_RAW_4(fs, val, X) \
  194. do { \
  195. union _FP_UNION_##fs _flo; \
  196. _flo.bits.frac0 = X##_f[0]; \
  197. _flo.bits.frac1 = X##_f[1]; \
  198. _flo.bits.frac2 = X##_f[2]; \
  199. _flo.bits.frac3 = X##_f[3]; \
  200. _flo.bits.exp = X##_e; \
  201. _flo.bits.sign = X##_s; \
  202. (val) = _flo.flt; \
  203. } while (0)
  204. #define _FP_PACK_RAW_4_P(fs, val, X) \
  205. do { \
  206. union _FP_UNION_##fs *_flo = \
  207. (union _FP_UNION_##fs *)(val); \
  208. \
  209. _flo->bits.frac0 = X##_f[0]; \
  210. _flo->bits.frac1 = X##_f[1]; \
  211. _flo->bits.frac2 = X##_f[2]; \
  212. _flo->bits.frac3 = X##_f[3]; \
  213. _flo->bits.exp = X##_e; \
  214. _flo->bits.sign = X##_s; \
  215. } while (0)
  216. /*
  217. * Multiplication algorithms:
  218. */
  219. /* Given a 1W * 1W => 2W primitive, do the extended multiplication. */
  220. #define _FP_MUL_MEAT_4_wide(wfracbits, R, X, Y, doit) \
  221. do { \
  222. _FP_FRAC_DECL_8(_z); _FP_FRAC_DECL_2(_b); _FP_FRAC_DECL_2(_c); \
  223. _FP_FRAC_DECL_2(_d); _FP_FRAC_DECL_2(_e); _FP_FRAC_DECL_2(_f); \
  224. \
  225. doit(_FP_FRAC_WORD_8(_z,1), _FP_FRAC_WORD_8(_z,0), X##_f[0], Y##_f[0]); \
  226. doit(_b_f1, _b_f0, X##_f[0], Y##_f[1]); \
  227. doit(_c_f1, _c_f0, X##_f[1], Y##_f[0]); \
  228. doit(_d_f1, _d_f0, X##_f[1], Y##_f[1]); \
  229. doit(_e_f1, _e_f0, X##_f[0], Y##_f[2]); \
  230. doit(_f_f1, _f_f0, X##_f[2], Y##_f[0]); \
  231. __FP_FRAC_ADD_3(_FP_FRAC_WORD_8(_z,3),_FP_FRAC_WORD_8(_z,2), \
  232. _FP_FRAC_WORD_8(_z,1), 0,_b_f1,_b_f0, \
  233. 0,0,_FP_FRAC_WORD_8(_z,1)); \
  234. __FP_FRAC_ADD_3(_FP_FRAC_WORD_8(_z,3),_FP_FRAC_WORD_8(_z,2), \
  235. _FP_FRAC_WORD_8(_z,1), 0,_c_f1,_c_f0, \
  236. _FP_FRAC_WORD_8(_z,3),_FP_FRAC_WORD_8(_z,2), \
  237. _FP_FRAC_WORD_8(_z,1)); \
  238. __FP_FRAC_ADD_3(_FP_FRAC_WORD_8(_z,4),_FP_FRAC_WORD_8(_z,3), \
  239. _FP_FRAC_WORD_8(_z,2), 0,_d_f1,_d_f0, \
  240. 0,_FP_FRAC_WORD_8(_z,3),_FP_FRAC_WORD_8(_z,2)); \
  241. __FP_FRAC_ADD_3(_FP_FRAC_WORD_8(_z,4),_FP_FRAC_WORD_8(_z,3), \
  242. _FP_FRAC_WORD_8(_z,2), 0,_e_f1,_e_f0, \
  243. _FP_FRAC_WORD_8(_z,4),_FP_FRAC_WORD_8(_z,3), \
  244. _FP_FRAC_WORD_8(_z,2)); \
  245. __FP_FRAC_ADD_3(_FP_FRAC_WORD_8(_z,4),_FP_FRAC_WORD_8(_z,3), \
  246. _FP_FRAC_WORD_8(_z,2), 0,_f_f1,_f_f0, \
  247. _FP_FRAC_WORD_8(_z,4),_FP_FRAC_WORD_8(_z,3), \
  248. _FP_FRAC_WORD_8(_z,2)); \
  249. doit(_b_f1, _b_f0, X##_f[0], Y##_f[3]); \
  250. doit(_c_f1, _c_f0, X##_f[3], Y##_f[0]); \
  251. doit(_d_f1, _d_f0, X##_f[1], Y##_f[2]); \
  252. doit(_e_f1, _e_f0, X##_f[2], Y##_f[1]); \
  253. __FP_FRAC_ADD_3(_FP_FRAC_WORD_8(_z,5),_FP_FRAC_WORD_8(_z,4), \
  254. _FP_FRAC_WORD_8(_z,3), 0,_b_f1,_b_f0, \
  255. 0,_FP_FRAC_WORD_8(_z,4),_FP_FRAC_WORD_8(_z,3)); \
  256. __FP_FRAC_ADD_3(_FP_FRAC_WORD_8(_z,5),_FP_FRAC_WORD_8(_z,4), \
  257. _FP_FRAC_WORD_8(_z,3), 0,_c_f1,_c_f0, \
  258. _FP_FRAC_WORD_8(_z,5),_FP_FRAC_WORD_8(_z,4), \
  259. _FP_FRAC_WORD_8(_z,3)); \
  260. __FP_FRAC_ADD_3(_FP_FRAC_WORD_8(_z,5),_FP_FRAC_WORD_8(_z,4), \
  261. _FP_FRAC_WORD_8(_z,3), 0,_d_f1,_d_f0, \
  262. _FP_FRAC_WORD_8(_z,5),_FP_FRAC_WORD_8(_z,4), \
  263. _FP_FRAC_WORD_8(_z,3)); \
  264. __FP_FRAC_ADD_3(_FP_FRAC_WORD_8(_z,5),_FP_FRAC_WORD_8(_z,4), \
  265. _FP_FRAC_WORD_8(_z,3), 0,_e_f1,_e_f0, \
  266. _FP_FRAC_WORD_8(_z,5),_FP_FRAC_WORD_8(_z,4), \
  267. _FP_FRAC_WORD_8(_z,3)); \
  268. doit(_b_f1, _b_f0, X##_f[2], Y##_f[2]); \
  269. doit(_c_f1, _c_f0, X##_f[1], Y##_f[3]); \
  270. doit(_d_f1, _d_f0, X##_f[3], Y##_f[1]); \
  271. doit(_e_f1, _e_f0, X##_f[2], Y##_f[3]); \
  272. doit(_f_f1, _f_f0, X##_f[3], Y##_f[2]); \
  273. __FP_FRAC_ADD_3(_FP_FRAC_WORD_8(_z,6),_FP_FRAC_WORD_8(_z,5), \
  274. _FP_FRAC_WORD_8(_z,4), 0,_b_f1,_b_f0, \
  275. 0,_FP_FRAC_WORD_8(_z,5),_FP_FRAC_WORD_8(_z,4)); \
  276. __FP_FRAC_ADD_3(_FP_FRAC_WORD_8(_z,6),_FP_FRAC_WORD_8(_z,5), \
  277. _FP_FRAC_WORD_8(_z,4), 0,_c_f1,_c_f0, \
  278. _FP_FRAC_WORD_8(_z,6),_FP_FRAC_WORD_8(_z,5), \
  279. _FP_FRAC_WORD_8(_z,4)); \
  280. __FP_FRAC_ADD_3(_FP_FRAC_WORD_8(_z,6),_FP_FRAC_WORD_8(_z,5), \
  281. _FP_FRAC_WORD_8(_z,4), 0,_d_f1,_d_f0, \
  282. _FP_FRAC_WORD_8(_z,6),_FP_FRAC_WORD_8(_z,5), \
  283. _FP_FRAC_WORD_8(_z,4)); \
  284. __FP_FRAC_ADD_3(_FP_FRAC_WORD_8(_z,7),_FP_FRAC_WORD_8(_z,6), \
  285. _FP_FRAC_WORD_8(_z,5), 0,_e_f1,_e_f0, \
  286. 0,_FP_FRAC_WORD_8(_z,6),_FP_FRAC_WORD_8(_z,5)); \
  287. __FP_FRAC_ADD_3(_FP_FRAC_WORD_8(_z,7),_FP_FRAC_WORD_8(_z,6), \
  288. _FP_FRAC_WORD_8(_z,5), 0,_f_f1,_f_f0, \
  289. _FP_FRAC_WORD_8(_z,7),_FP_FRAC_WORD_8(_z,6), \
  290. _FP_FRAC_WORD_8(_z,5)); \
  291. doit(_b_f1, _b_f0, X##_f[3], Y##_f[3]); \
  292. __FP_FRAC_ADD_2(_FP_FRAC_WORD_8(_z,7),_FP_FRAC_WORD_8(_z,6), \
  293. _b_f1,_b_f0, \
  294. _FP_FRAC_WORD_8(_z,7),_FP_FRAC_WORD_8(_z,6)); \
  295. \
  296. /* Normalize since we know where the msb of the multiplicands \
  297. were (bit B), we know that the msb of the of the product is \
  298. at either 2B or 2B-1. */ \
  299. _FP_FRAC_SRS_8(_z, wfracbits-1, 2*wfracbits); \
  300. __FP_FRAC_SET_4(R, _FP_FRAC_WORD_8(_z,3), _FP_FRAC_WORD_8(_z,2), \
  301. _FP_FRAC_WORD_8(_z,1), _FP_FRAC_WORD_8(_z,0)); \
  302. } while (0)
  303. #define _FP_MUL_MEAT_4_gmp(wfracbits, R, X, Y) \
  304. do { \
  305. _FP_FRAC_DECL_8(_z); \
  306. \
  307. mpn_mul_n(_z_f, _x_f, _y_f, 4); \
  308. \
  309. /* Normalize since we know where the msb of the multiplicands \
  310. were (bit B), we know that the msb of the of the product is \
  311. at either 2B or 2B-1. */ \
  312. _FP_FRAC_SRS_8(_z, wfracbits-1, 2*wfracbits); \
  313. __FP_FRAC_SET_4(R, _FP_FRAC_WORD_8(_z,3), _FP_FRAC_WORD_8(_z,2), \
  314. _FP_FRAC_WORD_8(_z,1), _FP_FRAC_WORD_8(_z,0)); \
  315. } while (0)
  316. /*
  317. * Helper utility for _FP_DIV_MEAT_4_udiv:
  318. * pppp = m * nnn
  319. */
  320. #define umul_ppppmnnn(p3,p2,p1,p0,m,n2,n1,n0) \
  321. do { \
  322. UWtype _t; \
  323. umul_ppmm(p1,p0,m,n0); \
  324. umul_ppmm(p2,_t,m,n1); \
  325. __FP_FRAC_ADDI_2(p2,p1,_t); \
  326. umul_ppmm(p3,_t,m,n2); \
  327. __FP_FRAC_ADDI_2(p3,p2,_t); \
  328. } while (0)
  329. /*
  330. * Division algorithms:
  331. */
  332. #define _FP_DIV_MEAT_4_udiv(fs, R, X, Y) \
  333. do { \
  334. int _i; \
  335. _FP_FRAC_DECL_4(_n); _FP_FRAC_DECL_4(_m); \
  336. _FP_FRAC_SET_4(_n, _FP_ZEROFRAC_4); \
  337. if (_FP_FRAC_GT_4(X, Y)) \
  338. { \
  339. _n_f[3] = X##_f[0] << (_FP_W_TYPE_SIZE - 1); \
  340. _FP_FRAC_SRL_4(X, 1); \
  341. } \
  342. else \
  343. R##_e--; \
  344. \
  345. /* Normalize, i.e. make the most significant bit of the \
  346. denominator set. */ \
  347. _FP_FRAC_SLL_4(Y, _FP_WFRACXBITS_##fs); \
  348. \
  349. for (_i = 3; ; _i--) \
  350. { \
  351. if (X##_f[3] == Y##_f[3]) \
  352. { \
  353. /* This is a special case, not an optimization \
  354. (X##_f[3]/Y##_f[3] would not fit into UWtype). \
  355. As X## is guaranteed to be < Y, R##_f[_i] can be either \
  356. (UWtype)-1 or (UWtype)-2. */ \
  357. R##_f[_i] = -1; \
  358. if (!_i) \
  359. break; \
  360. __FP_FRAC_SUB_4(X##_f[3], X##_f[2], X##_f[1], X##_f[0], \
  361. Y##_f[2], Y##_f[1], Y##_f[0], 0, \
  362. X##_f[2], X##_f[1], X##_f[0], _n_f[_i]); \
  363. _FP_FRAC_SUB_4(X, Y, X); \
  364. if (X##_f[3] > Y##_f[3]) \
  365. { \
  366. R##_f[_i] = -2; \
  367. _FP_FRAC_ADD_4(X, Y, X); \
  368. } \
  369. } \
  370. else \
  371. { \
  372. udiv_qrnnd(R##_f[_i], X##_f[3], X##_f[3], X##_f[2], Y##_f[3]); \
  373. umul_ppppmnnn(_m_f[3], _m_f[2], _m_f[1], _m_f[0], \
  374. R##_f[_i], Y##_f[2], Y##_f[1], Y##_f[0]); \
  375. X##_f[2] = X##_f[1]; \
  376. X##_f[1] = X##_f[0]; \
  377. X##_f[0] = _n_f[_i]; \
  378. if (_FP_FRAC_GT_4(_m, X)) \
  379. { \
  380. R##_f[_i]--; \
  381. _FP_FRAC_ADD_4(X, Y, X); \
  382. if (_FP_FRAC_GE_4(X, Y) && _FP_FRAC_GT_4(_m, X)) \
  383. { \
  384. R##_f[_i]--; \
  385. _FP_FRAC_ADD_4(X, Y, X); \
  386. } \
  387. } \
  388. _FP_FRAC_DEC_4(X, _m); \
  389. if (!_i) \
  390. { \
  391. if (!_FP_FRAC_EQ_4(X, _m)) \
  392. R##_f[0] |= _FP_WORK_STICKY; \
  393. break; \
  394. } \
  395. } \
  396. } \
  397. } while (0)
  398. /*
  399. * Square root algorithms:
  400. * We have just one right now, maybe Newton approximation
  401. * should be added for those machines where division is fast.
  402. */
  403. #define _FP_SQRT_MEAT_4(R, S, T, X, q) \
  404. do { \
  405. while (q) \
  406. { \
  407. T##_f[3] = S##_f[3] + q; \
  408. if (T##_f[3] <= X##_f[3]) \
  409. { \
  410. S##_f[3] = T##_f[3] + q; \
  411. X##_f[3] -= T##_f[3]; \
  412. R##_f[3] += q; \
  413. } \
  414. _FP_FRAC_SLL_4(X, 1); \
  415. q >>= 1; \
  416. } \
  417. q = (_FP_W_TYPE)1 << (_FP_W_TYPE_SIZE - 1); \
  418. while (q) \
  419. { \
  420. T##_f[2] = S##_f[2] + q; \
  421. T##_f[3] = S##_f[3]; \
  422. if (T##_f[3] < X##_f[3] || \
  423. (T##_f[3] == X##_f[3] && T##_f[2] <= X##_f[2])) \
  424. { \
  425. S##_f[2] = T##_f[2] + q; \
  426. S##_f[3] += (T##_f[2] > S##_f[2]); \
  427. __FP_FRAC_DEC_2(X##_f[3], X##_f[2], \
  428. T##_f[3], T##_f[2]); \
  429. R##_f[2] += q; \
  430. } \
  431. _FP_FRAC_SLL_4(X, 1); \
  432. q >>= 1; \
  433. } \
  434. q = (_FP_W_TYPE)1 << (_FP_W_TYPE_SIZE - 1); \
  435. while (q) \
  436. { \
  437. T##_f[1] = S##_f[1] + q; \
  438. T##_f[2] = S##_f[2]; \
  439. T##_f[3] = S##_f[3]; \
  440. if (T##_f[3] < X##_f[3] || \
  441. (T##_f[3] == X##_f[3] && (T##_f[2] < X##_f[2] || \
  442. (T##_f[2] == X##_f[2] && T##_f[1] <= X##_f[1])))) \
  443. { \
  444. S##_f[1] = T##_f[1] + q; \
  445. S##_f[2] += (T##_f[1] > S##_f[1]); \
  446. S##_f[3] += (T##_f[2] > S##_f[2]); \
  447. __FP_FRAC_DEC_3(X##_f[3], X##_f[2], X##_f[1], \
  448. T##_f[3], T##_f[2], T##_f[1]); \
  449. R##_f[1] += q; \
  450. } \
  451. _FP_FRAC_SLL_4(X, 1); \
  452. q >>= 1; \
  453. } \
  454. q = (_FP_W_TYPE)1 << (_FP_W_TYPE_SIZE - 1); \
  455. while (q != _FP_WORK_ROUND) \
  456. { \
  457. T##_f[0] = S##_f[0] + q; \
  458. T##_f[1] = S##_f[1]; \
  459. T##_f[2] = S##_f[2]; \
  460. T##_f[3] = S##_f[3]; \
  461. if (_FP_FRAC_GE_4(X,T)) \
  462. { \
  463. S##_f[0] = T##_f[0] + q; \
  464. S##_f[1] += (T##_f[0] > S##_f[0]); \
  465. S##_f[2] += (T##_f[1] > S##_f[1]); \
  466. S##_f[3] += (T##_f[2] > S##_f[2]); \
  467. _FP_FRAC_DEC_4(X, T); \
  468. R##_f[0] += q; \
  469. } \
  470. _FP_FRAC_SLL_4(X, 1); \
  471. q >>= 1; \
  472. } \
  473. if (!_FP_FRAC_ZEROP_4(X)) \
  474. { \
  475. if (_FP_FRAC_GT_4(X,S)) \
  476. R##_f[0] |= _FP_WORK_ROUND; \
  477. R##_f[0] |= _FP_WORK_STICKY; \
  478. } \
  479. } while (0)
  480. /*
  481. * Internals
  482. */
  483. #define __FP_FRAC_SET_4(X,I3,I2,I1,I0) \
  484. (X##_f[3] = I3, X##_f[2] = I2, X##_f[1] = I1, X##_f[0] = I0)
  485. #ifndef __FP_FRAC_ADD_3
  486. #define __FP_FRAC_ADD_3(r2,r1,r0,x2,x1,x0,y2,y1,y0) \
  487. do { \
  488. _FP_W_TYPE _c1, _c2; \
  489. r0 = x0 + y0; \
  490. _c1 = r0 < x0; \
  491. r1 = x1 + y1; \
  492. _c2 = r1 < x1; \
  493. r1 += _c1; \
  494. _c2 |= r1 < _c1; \
  495. r2 = x2 + y2 + _c2; \
  496. } while (0)
  497. #endif
  498. #ifndef __FP_FRAC_ADD_4
  499. #define __FP_FRAC_ADD_4(r3,r2,r1,r0,x3,x2,x1,x0,y3,y2,y1,y0) \
  500. do { \
  501. _FP_W_TYPE _c1, _c2, _c3; \
  502. r0 = x0 + y0; \
  503. _c1 = r0 < x0; \
  504. r1 = x1 + y1; \
  505. _c2 = r1 < x1; \
  506. r1 += _c1; \
  507. _c2 |= r1 < _c1; \
  508. r2 = x2 + y2; \
  509. _c3 = r2 < x2; \
  510. r2 += _c2; \
  511. _c3 |= r2 < _c2; \
  512. r3 = x3 + y3 + _c3; \
  513. } while (0)
  514. #endif
  515. #ifndef __FP_FRAC_SUB_3
  516. #define __FP_FRAC_SUB_3(r2,r1,r0,x2,x1,x0,y2,y1,y0) \
  517. do { \
  518. _FP_W_TYPE _c1, _c2; \
  519. r0 = x0 - y0; \
  520. _c1 = r0 > x0; \
  521. r1 = x1 - y1; \
  522. _c2 = r1 > x1; \
  523. r1 -= _c1; \
  524. _c2 |= _c1 && (y1 == x1); \
  525. r2 = x2 - y2 - _c2; \
  526. } while (0)
  527. #endif
  528. #ifndef __FP_FRAC_SUB_4
  529. #define __FP_FRAC_SUB_4(r3,r2,r1,r0,x3,x2,x1,x0,y3,y2,y1,y0) \
  530. do { \
  531. _FP_W_TYPE _c1, _c2, _c3; \
  532. r0 = x0 - y0; \
  533. _c1 = r0 > x0; \
  534. r1 = x1 - y1; \
  535. _c2 = r1 > x1; \
  536. r1 -= _c1; \
  537. _c2 |= _c1 && (y1 == x1); \
  538. r2 = x2 - y2; \
  539. _c3 = r2 > x2; \
  540. r2 -= _c2; \
  541. _c3 |= _c2 && (y2 == x2); \
  542. r3 = x3 - y3 - _c3; \
  543. } while (0)
  544. #endif
  545. #ifndef __FP_FRAC_DEC_3
  546. #define __FP_FRAC_DEC_3(x2,x1,x0,y2,y1,y0) \
  547. do { \
  548. UWtype _t0, _t1, _t2; \
  549. _t0 = x0, _t1 = x1, _t2 = x2; \
  550. __FP_FRAC_SUB_3 (x2, x1, x0, _t2, _t1, _t0, y2, y1, y0); \
  551. } while (0)
  552. #endif
  553. #ifndef __FP_FRAC_DEC_4
  554. #define __FP_FRAC_DEC_4(x3,x2,x1,x0,y3,y2,y1,y0) \
  555. do { \
  556. UWtype _t0, _t1, _t2, _t3; \
  557. _t0 = x0, _t1 = x1, _t2 = x2, _t3 = x3; \
  558. __FP_FRAC_SUB_4 (x3,x2,x1,x0,_t3,_t2,_t1,_t0, y3,y2,y1,y0); \
  559. } while (0)
  560. #endif
  561. #ifndef __FP_FRAC_ADDI_4
  562. #define __FP_FRAC_ADDI_4(x3,x2,x1,x0,i) \
  563. do { \
  564. UWtype _t; \
  565. _t = ((x0 += i) < i); \
  566. x1 += _t; _t = (x1 < _t); \
  567. x2 += _t; _t = (x2 < _t); \
  568. x3 += _t; \
  569. } while (0)
  570. #endif
  571. /* Convert FP values between word sizes. This appears to be more
  572. * complicated than I'd have expected it to be, so these might be
  573. * wrong... These macros are in any case somewhat bogus because they
  574. * use information about what various FRAC_n variables look like
  575. * internally [eg, that 2 word vars are X_f0 and x_f1]. But so do
  576. * the ones in op-2.h and op-1.h.
  577. */
  578. #define _FP_FRAC_COPY_1_4(D, S) (D##_f = S##_f[0])
  579. #define _FP_FRAC_COPY_2_4(D, S) \
  580. do { \
  581. D##_f0 = S##_f[0]; \
  582. D##_f1 = S##_f[1]; \
  583. } while (0)
  584. /* Assembly/disassembly for converting to/from integral types.
  585. * No shifting or overflow handled here.
  586. */
  587. /* Put the FP value X into r, which is an integer of size rsize. */
  588. #define _FP_FRAC_ASSEMBLE_4(r, X, rsize) \
  589. do { \
  590. if (rsize <= _FP_W_TYPE_SIZE) \
  591. r = X##_f[0]; \
  592. else if (rsize <= 2*_FP_W_TYPE_SIZE) \
  593. { \
  594. r = X##_f[1]; \
  595. r <<= _FP_W_TYPE_SIZE; \
  596. r += X##_f[0]; \
  597. } \
  598. else \
  599. { \
  600. /* I'm feeling lazy so we deal with int == 3words (implausible)*/ \
  601. /* and int == 4words as a single case. */ \
  602. r = X##_f[3]; \
  603. r <<= _FP_W_TYPE_SIZE; \
  604. r += X##_f[2]; \
  605. r <<= _FP_W_TYPE_SIZE; \
  606. r += X##_f[1]; \
  607. r <<= _FP_W_TYPE_SIZE; \
  608. r += X##_f[0]; \
  609. } \
  610. } while (0)
  611. /* "No disassemble Number Five!" */
  612. /* move an integer of size rsize into X's fractional part. We rely on
  613. * the _f[] array consisting of words of size _FP_W_TYPE_SIZE to avoid
  614. * having to mask the values we store into it.
  615. */
  616. #define _FP_FRAC_DISASSEMBLE_4(X, r, rsize) \
  617. do { \
  618. X##_f[0] = r; \
  619. X##_f[1] = (rsize <= _FP_W_TYPE_SIZE ? 0 : r >> _FP_W_TYPE_SIZE); \
  620. X##_f[2] = (rsize <= 2*_FP_W_TYPE_SIZE ? 0 : r >> 2*_FP_W_TYPE_SIZE); \
  621. X##_f[3] = (rsize <= 3*_FP_W_TYPE_SIZE ? 0 : r >> 3*_FP_W_TYPE_SIZE); \
  622. } while (0);
  623. #define _FP_FRAC_COPY_4_1(D, S) \
  624. do { \
  625. D##_f[0] = S##_f; \
  626. D##_f[1] = D##_f[2] = D##_f[3] = 0; \
  627. } while (0)
  628. #define _FP_FRAC_COPY_4_2(D, S) \
  629. do { \
  630. D##_f[0] = S##_f0; \
  631. D##_f[1] = S##_f1; \
  632. D##_f[2] = D##_f[3] = 0; \
  633. } while (0)
  634. #define _FP_FRAC_COPY_4_4(D,S) _FP_FRAC_COPY_4(D,S)