op-1.h 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. /* Software floating-point emulation.
  2. Basic one-word fraction declaration and manipulation.
  3. Copyright (C) 1997,1998,1999,2006 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_1(X) _FP_W_TYPE X##_f
  29. #define _FP_FRAC_COPY_1(D,S) (D##_f = S##_f)
  30. #define _FP_FRAC_SET_1(X,I) (X##_f = I)
  31. #define _FP_FRAC_HIGH_1(X) (X##_f)
  32. #define _FP_FRAC_LOW_1(X) (X##_f)
  33. #define _FP_FRAC_WORD_1(X,w) (X##_f)
  34. #define _FP_FRAC_ADDI_1(X,I) (X##_f += I)
  35. #define _FP_FRAC_SLL_1(X,N) \
  36. do { \
  37. if (__builtin_constant_p(N) && (N) == 1) \
  38. X##_f += X##_f; \
  39. else \
  40. X##_f <<= (N); \
  41. } while (0)
  42. #define _FP_FRAC_SRL_1(X,N) (X##_f >>= N)
  43. /* Right shift with sticky-lsb. */
  44. #define _FP_FRAC_SRST_1(X,S,N,sz) __FP_FRAC_SRST_1(X##_f, S, N, sz)
  45. #define _FP_FRAC_SRS_1(X,N,sz) __FP_FRAC_SRS_1(X##_f, N, sz)
  46. #define __FP_FRAC_SRST_1(X,S,N,sz) \
  47. do { \
  48. S = (__builtin_constant_p(N) && (N) == 1 \
  49. ? X & 1 : (X << (_FP_W_TYPE_SIZE - (N))) != 0); \
  50. X = X >> (N); \
  51. } while (0)
  52. #define __FP_FRAC_SRS_1(X,N,sz) \
  53. (X = (X >> (N) | (__builtin_constant_p(N) && (N) == 1 \
  54. ? X & 1 : (X << (_FP_W_TYPE_SIZE - (N))) != 0)))
  55. #define _FP_FRAC_ADD_1(R,X,Y) (R##_f = X##_f + Y##_f)
  56. #define _FP_FRAC_SUB_1(R,X,Y) (R##_f = X##_f - Y##_f)
  57. #define _FP_FRAC_DEC_1(X,Y) (X##_f -= Y##_f)
  58. #define _FP_FRAC_CLZ_1(z, X) __FP_CLZ(z, X##_f)
  59. /* Predicates */
  60. #define _FP_FRAC_NEGP_1(X) ((_FP_WS_TYPE)X##_f < 0)
  61. #define _FP_FRAC_ZEROP_1(X) (X##_f == 0)
  62. #define _FP_FRAC_OVERP_1(fs,X) (X##_f & _FP_OVERFLOW_##fs)
  63. #define _FP_FRAC_CLEAR_OVERP_1(fs,X) (X##_f &= ~_FP_OVERFLOW_##fs)
  64. #define _FP_FRAC_EQ_1(X, Y) (X##_f == Y##_f)
  65. #define _FP_FRAC_GE_1(X, Y) (X##_f >= Y##_f)
  66. #define _FP_FRAC_GT_1(X, Y) (X##_f > Y##_f)
  67. #define _FP_ZEROFRAC_1 0
  68. #define _FP_MINFRAC_1 1
  69. #define _FP_MAXFRAC_1 (~(_FP_WS_TYPE)0)
  70. /*
  71. * Unpack the raw bits of a native fp value. Do not classify or
  72. * normalize the data.
  73. */
  74. #define _FP_UNPACK_RAW_1(fs, X, val) \
  75. do { \
  76. union _FP_UNION_##fs _flo; _flo.flt = (val); \
  77. \
  78. X##_f = _flo.bits.frac; \
  79. X##_e = _flo.bits.exp; \
  80. X##_s = _flo.bits.sign; \
  81. } while (0)
  82. #define _FP_UNPACK_RAW_1_P(fs, X, val) \
  83. do { \
  84. union _FP_UNION_##fs *_flo = \
  85. (union _FP_UNION_##fs *)(val); \
  86. \
  87. X##_f = _flo->bits.frac; \
  88. X##_e = _flo->bits.exp; \
  89. X##_s = _flo->bits.sign; \
  90. } while (0)
  91. /*
  92. * Repack the raw bits of a native fp value.
  93. */
  94. #define _FP_PACK_RAW_1(fs, val, X) \
  95. do { \
  96. union _FP_UNION_##fs _flo; \
  97. \
  98. _flo.bits.frac = X##_f; \
  99. _flo.bits.exp = X##_e; \
  100. _flo.bits.sign = X##_s; \
  101. \
  102. (val) = _flo.flt; \
  103. } while (0)
  104. #define _FP_PACK_RAW_1_P(fs, val, X) \
  105. do { \
  106. union _FP_UNION_##fs *_flo = \
  107. (union _FP_UNION_##fs *)(val); \
  108. \
  109. _flo->bits.frac = X##_f; \
  110. _flo->bits.exp = X##_e; \
  111. _flo->bits.sign = X##_s; \
  112. } while (0)
  113. /*
  114. * Multiplication algorithms:
  115. */
  116. /* Basic. Assuming the host word size is >= 2*FRACBITS, we can do the
  117. multiplication immediately. */
  118. #define _FP_MUL_MEAT_1_imm(wfracbits, R, X, Y) \
  119. do { \
  120. R##_f = X##_f * Y##_f; \
  121. /* Normalize since we know where the msb of the multiplicands \
  122. were (bit B), we know that the msb of the of the product is \
  123. at either 2B or 2B-1. */ \
  124. _FP_FRAC_SRS_1(R, wfracbits-1, 2*wfracbits); \
  125. } while (0)
  126. /* Given a 1W * 1W => 2W primitive, do the extended multiplication. */
  127. #define _FP_MUL_MEAT_1_wide(wfracbits, R, X, Y, doit) \
  128. do { \
  129. _FP_W_TYPE _Z_f0, _Z_f1; \
  130. doit(_Z_f1, _Z_f0, X##_f, Y##_f); \
  131. /* Normalize since we know where the msb of the multiplicands \
  132. were (bit B), we know that the msb of the of the product is \
  133. at either 2B or 2B-1. */ \
  134. _FP_FRAC_SRS_2(_Z, wfracbits-1, 2*wfracbits); \
  135. R##_f = _Z_f0; \
  136. } while (0)
  137. /* Finally, a simple widening multiply algorithm. What fun! */
  138. #define _FP_MUL_MEAT_1_hard(wfracbits, R, X, Y) \
  139. do { \
  140. _FP_W_TYPE _xh, _xl, _yh, _yl, _z_f0, _z_f1, _a_f0, _a_f1; \
  141. \
  142. /* split the words in half */ \
  143. _xh = X##_f >> (_FP_W_TYPE_SIZE/2); \
  144. _xl = X##_f & (((_FP_W_TYPE)1 << (_FP_W_TYPE_SIZE/2)) - 1); \
  145. _yh = Y##_f >> (_FP_W_TYPE_SIZE/2); \
  146. _yl = Y##_f & (((_FP_W_TYPE)1 << (_FP_W_TYPE_SIZE/2)) - 1); \
  147. \
  148. /* multiply the pieces */ \
  149. _z_f0 = _xl * _yl; \
  150. _a_f0 = _xh * _yl; \
  151. _a_f1 = _xl * _yh; \
  152. _z_f1 = _xh * _yh; \
  153. \
  154. /* reassemble into two full words */ \
  155. if ((_a_f0 += _a_f1) < _a_f1) \
  156. _z_f1 += (_FP_W_TYPE)1 << (_FP_W_TYPE_SIZE/2); \
  157. _a_f1 = _a_f0 >> (_FP_W_TYPE_SIZE/2); \
  158. _a_f0 = _a_f0 << (_FP_W_TYPE_SIZE/2); \
  159. _FP_FRAC_ADD_2(_z, _z, _a); \
  160. \
  161. /* normalize */ \
  162. _FP_FRAC_SRS_2(_z, wfracbits - 1, 2*wfracbits); \
  163. R##_f = _z_f0; \
  164. } while (0)
  165. /*
  166. * Division algorithms:
  167. */
  168. /* Basic. Assuming the host word size is >= 2*FRACBITS, we can do the
  169. division immediately. Give this macro either _FP_DIV_HELP_imm for
  170. C primitives or _FP_DIV_HELP_ldiv for the ISO function. Which you
  171. choose will depend on what the compiler does with divrem4. */
  172. #define _FP_DIV_MEAT_1_imm(fs, R, X, Y, doit) \
  173. do { \
  174. _FP_W_TYPE _q, _r; \
  175. X##_f <<= (X##_f < Y##_f \
  176. ? R##_e--, _FP_WFRACBITS_##fs \
  177. : _FP_WFRACBITS_##fs - 1); \
  178. doit(_q, _r, X##_f, Y##_f); \
  179. R##_f = _q | (_r != 0); \
  180. } while (0)
  181. /* GCC's longlong.h defines a 2W / 1W => (1W,1W) primitive udiv_qrnnd
  182. that may be useful in this situation. This first is for a primitive
  183. that requires normalization, the second for one that does not. Look
  184. for UDIV_NEEDS_NORMALIZATION to tell which your machine needs. */
  185. #define _FP_DIV_MEAT_1_udiv_norm(fs, R, X, Y) \
  186. do { \
  187. _FP_W_TYPE _nh, _nl, _q, _r, _y; \
  188. \
  189. /* Normalize Y -- i.e. make the most significant bit set. */ \
  190. _y = Y##_f << _FP_WFRACXBITS_##fs; \
  191. \
  192. /* Shift X op correspondingly high, that is, up one full word. */ \
  193. if (X##_f < Y##_f) \
  194. { \
  195. R##_e--; \
  196. _nl = 0; \
  197. _nh = X##_f; \
  198. } \
  199. else \
  200. { \
  201. _nl = X##_f << (_FP_W_TYPE_SIZE - 1); \
  202. _nh = X##_f >> 1; \
  203. } \
  204. \
  205. udiv_qrnnd(_q, _r, _nh, _nl, _y); \
  206. R##_f = _q | (_r != 0); \
  207. } while (0)
  208. #define _FP_DIV_MEAT_1_udiv(fs, R, X, Y) \
  209. do { \
  210. _FP_W_TYPE _nh, _nl, _q, _r; \
  211. if (X##_f < Y##_f) \
  212. { \
  213. R##_e--; \
  214. _nl = X##_f << _FP_WFRACBITS_##fs; \
  215. _nh = X##_f >> _FP_WFRACXBITS_##fs; \
  216. } \
  217. else \
  218. { \
  219. _nl = X##_f << (_FP_WFRACBITS_##fs - 1); \
  220. _nh = X##_f >> (_FP_WFRACXBITS_##fs + 1); \
  221. } \
  222. udiv_qrnnd(_q, _r, _nh, _nl, Y##_f); \
  223. R##_f = _q | (_r != 0); \
  224. } while (0)
  225. /*
  226. * Square root algorithms:
  227. * We have just one right now, maybe Newton approximation
  228. * should be added for those machines where division is fast.
  229. */
  230. #define _FP_SQRT_MEAT_1(R, S, T, X, q) \
  231. do { \
  232. while (q != _FP_WORK_ROUND) \
  233. { \
  234. T##_f = S##_f + q; \
  235. if (T##_f <= X##_f) \
  236. { \
  237. S##_f = T##_f + q; \
  238. X##_f -= T##_f; \
  239. R##_f += q; \
  240. } \
  241. _FP_FRAC_SLL_1(X, 1); \
  242. q >>= 1; \
  243. } \
  244. if (X##_f) \
  245. { \
  246. if (S##_f < X##_f) \
  247. R##_f |= _FP_WORK_ROUND; \
  248. R##_f |= _FP_WORK_STICKY; \
  249. } \
  250. } while (0)
  251. /*
  252. * Assembly/disassembly for converting to/from integral types.
  253. * No shifting or overflow handled here.
  254. */
  255. #define _FP_FRAC_ASSEMBLE_1(r, X, rsize) (r = X##_f)
  256. #define _FP_FRAC_DISASSEMBLE_1(X, r, rsize) (X##_f = r)
  257. /*
  258. * Convert FP values between word sizes
  259. */
  260. #define _FP_FRAC_COPY_1_1(D, S) (D##_f = S##_f)