op-4.h 23 KB

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