op-4.h 23 KB

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