op-1.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  1. /* Software floating-point emulation.
  2. Basic one-word fraction declaration and manipulation.
  3. Copyright (C) 1997-2017 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. #ifndef SOFT_FP_OP_1_H
  29. #define SOFT_FP_OP_1_H 1
  30. #define _FP_FRAC_DECL_1(X) _FP_W_TYPE X##_f _FP_ZERO_INIT
  31. #define _FP_FRAC_COPY_1(D, S) (D##_f = S##_f)
  32. #define _FP_FRAC_SET_1(X, I) (X##_f = I)
  33. #define _FP_FRAC_HIGH_1(X) (X##_f)
  34. #define _FP_FRAC_LOW_1(X) (X##_f)
  35. #define _FP_FRAC_WORD_1(X, w) (X##_f)
  36. #define _FP_FRAC_ADDI_1(X, I) (X##_f += I)
  37. #define _FP_FRAC_SLL_1(X, N) \
  38. do \
  39. { \
  40. if (__builtin_constant_p (N) && (N) == 1) \
  41. X##_f += X##_f; \
  42. else \
  43. X##_f <<= (N); \
  44. } \
  45. while (0)
  46. #define _FP_FRAC_SRL_1(X, N) (X##_f >>= N)
  47. /* Right shift with sticky-lsb. */
  48. #define _FP_FRAC_SRST_1(X, S, N, sz) __FP_FRAC_SRST_1 (X##_f, S, (N), (sz))
  49. #define _FP_FRAC_SRS_1(X, N, sz) __FP_FRAC_SRS_1 (X##_f, (N), (sz))
  50. #define __FP_FRAC_SRST_1(X, S, N, sz) \
  51. do \
  52. { \
  53. S = (__builtin_constant_p (N) && (N) == 1 \
  54. ? X & 1 \
  55. : (X << (_FP_W_TYPE_SIZE - (N))) != 0); \
  56. X = X >> (N); \
  57. } \
  58. while (0)
  59. #define __FP_FRAC_SRS_1(X, N, sz) \
  60. (X = (X >> (N) | (__builtin_constant_p (N) && (N) == 1 \
  61. ? X & 1 \
  62. : (X << (_FP_W_TYPE_SIZE - (N))) != 0)))
  63. #define _FP_FRAC_ADD_1(R, X, Y) (R##_f = X##_f + Y##_f)
  64. #define _FP_FRAC_SUB_1(R, X, Y) (R##_f = X##_f - Y##_f)
  65. #define _FP_FRAC_DEC_1(X, Y) (X##_f -= Y##_f)
  66. #define _FP_FRAC_CLZ_1(z, X) __FP_CLZ ((z), X##_f)
  67. /* Predicates. */
  68. #define _FP_FRAC_NEGP_1(X) ((_FP_WS_TYPE) X##_f < 0)
  69. #define _FP_FRAC_ZEROP_1(X) (X##_f == 0)
  70. #define _FP_FRAC_OVERP_1(fs, X) (X##_f & _FP_OVERFLOW_##fs)
  71. #define _FP_FRAC_CLEAR_OVERP_1(fs, X) (X##_f &= ~_FP_OVERFLOW_##fs)
  72. #define _FP_FRAC_HIGHBIT_DW_1(fs, X) (X##_f & _FP_HIGHBIT_DW_##fs)
  73. #define _FP_FRAC_EQ_1(X, Y) (X##_f == Y##_f)
  74. #define _FP_FRAC_GE_1(X, Y) (X##_f >= Y##_f)
  75. #define _FP_FRAC_GT_1(X, Y) (X##_f > Y##_f)
  76. #define _FP_ZEROFRAC_1 0
  77. #define _FP_MINFRAC_1 1
  78. #define _FP_MAXFRAC_1 (~(_FP_WS_TYPE) 0)
  79. /* Unpack the raw bits of a native fp value. Do not classify or
  80. normalize the data. */
  81. #define _FP_UNPACK_RAW_1(fs, X, val) \
  82. do \
  83. { \
  84. union _FP_UNION_##fs _FP_UNPACK_RAW_1_flo; \
  85. _FP_UNPACK_RAW_1_flo.flt = (val); \
  86. \
  87. X##_f = _FP_UNPACK_RAW_1_flo.bits.frac; \
  88. X##_e = _FP_UNPACK_RAW_1_flo.bits.exp; \
  89. X##_s = _FP_UNPACK_RAW_1_flo.bits.sign; \
  90. } \
  91. while (0)
  92. #define _FP_UNPACK_RAW_1_P(fs, X, val) \
  93. do \
  94. { \
  95. union _FP_UNION_##fs *_FP_UNPACK_RAW_1_P_flo \
  96. = (union _FP_UNION_##fs *) (val); \
  97. \
  98. X##_f = _FP_UNPACK_RAW_1_P_flo->bits.frac; \
  99. X##_e = _FP_UNPACK_RAW_1_P_flo->bits.exp; \
  100. X##_s = _FP_UNPACK_RAW_1_P_flo->bits.sign; \
  101. } \
  102. while (0)
  103. /* Repack the raw bits of a native fp value. */
  104. #define _FP_PACK_RAW_1(fs, val, X) \
  105. do \
  106. { \
  107. union _FP_UNION_##fs _FP_PACK_RAW_1_flo; \
  108. \
  109. _FP_PACK_RAW_1_flo.bits.frac = X##_f; \
  110. _FP_PACK_RAW_1_flo.bits.exp = X##_e; \
  111. _FP_PACK_RAW_1_flo.bits.sign = X##_s; \
  112. \
  113. (val) = _FP_PACK_RAW_1_flo.flt; \
  114. } \
  115. while (0)
  116. #define _FP_PACK_RAW_1_P(fs, val, X) \
  117. do \
  118. { \
  119. union _FP_UNION_##fs *_FP_PACK_RAW_1_P_flo \
  120. = (union _FP_UNION_##fs *) (val); \
  121. \
  122. _FP_PACK_RAW_1_P_flo->bits.frac = X##_f; \
  123. _FP_PACK_RAW_1_P_flo->bits.exp = X##_e; \
  124. _FP_PACK_RAW_1_P_flo->bits.sign = X##_s; \
  125. } \
  126. while (0)
  127. /* Multiplication algorithms: */
  128. /* Basic. Assuming the host word size is >= 2*FRACBITS, we can do the
  129. multiplication immediately. */
  130. #define _FP_MUL_MEAT_DW_1_imm(wfracbits, R, X, Y) \
  131. do \
  132. { \
  133. R##_f = X##_f * Y##_f; \
  134. } \
  135. while (0)
  136. #define _FP_MUL_MEAT_1_imm(wfracbits, R, X, Y) \
  137. do \
  138. { \
  139. _FP_MUL_MEAT_DW_1_imm ((wfracbits), R, X, Y); \
  140. /* Normalize since we know where the msb of the multiplicands \
  141. were (bit B), we know that the msb of the of the product is \
  142. at either 2B or 2B-1. */ \
  143. _FP_FRAC_SRS_1 (R, (wfracbits)-1, 2*(wfracbits)); \
  144. } \
  145. while (0)
  146. /* Given a 1W * 1W => 2W primitive, do the extended multiplication. */
  147. #define _FP_MUL_MEAT_DW_1_wide(wfracbits, R, X, Y, doit) \
  148. do \
  149. { \
  150. doit (R##_f1, R##_f0, X##_f, Y##_f); \
  151. } \
  152. while (0)
  153. #define _FP_MUL_MEAT_1_wide(wfracbits, R, X, Y, doit) \
  154. do \
  155. { \
  156. _FP_FRAC_DECL_2 (_FP_MUL_MEAT_1_wide_Z); \
  157. _FP_MUL_MEAT_DW_1_wide ((wfracbits), _FP_MUL_MEAT_1_wide_Z, \
  158. X, Y, doit); \
  159. /* Normalize since we know where the msb of the multiplicands \
  160. were (bit B), we know that the msb of the of the product is \
  161. at either 2B or 2B-1. */ \
  162. _FP_FRAC_SRS_2 (_FP_MUL_MEAT_1_wide_Z, (wfracbits)-1, \
  163. 2*(wfracbits)); \
  164. R##_f = _FP_MUL_MEAT_1_wide_Z_f0; \
  165. } \
  166. while (0)
  167. /* Finally, a simple widening multiply algorithm. What fun! */
  168. #define _FP_MUL_MEAT_DW_1_hard(wfracbits, R, X, Y) \
  169. do \
  170. { \
  171. _FP_W_TYPE _FP_MUL_MEAT_DW_1_hard_xh, _FP_MUL_MEAT_DW_1_hard_xl; \
  172. _FP_W_TYPE _FP_MUL_MEAT_DW_1_hard_yh, _FP_MUL_MEAT_DW_1_hard_yl; \
  173. _FP_FRAC_DECL_2 (_FP_MUL_MEAT_DW_1_hard_a); \
  174. \
  175. /* Split the words in half. */ \
  176. _FP_MUL_MEAT_DW_1_hard_xh = X##_f >> (_FP_W_TYPE_SIZE/2); \
  177. _FP_MUL_MEAT_DW_1_hard_xl \
  178. = X##_f & (((_FP_W_TYPE) 1 << (_FP_W_TYPE_SIZE/2)) - 1); \
  179. _FP_MUL_MEAT_DW_1_hard_yh = Y##_f >> (_FP_W_TYPE_SIZE/2); \
  180. _FP_MUL_MEAT_DW_1_hard_yl \
  181. = Y##_f & (((_FP_W_TYPE) 1 << (_FP_W_TYPE_SIZE/2)) - 1); \
  182. \
  183. /* Multiply the pieces. */ \
  184. R##_f0 = _FP_MUL_MEAT_DW_1_hard_xl * _FP_MUL_MEAT_DW_1_hard_yl; \
  185. _FP_MUL_MEAT_DW_1_hard_a_f0 \
  186. = _FP_MUL_MEAT_DW_1_hard_xh * _FP_MUL_MEAT_DW_1_hard_yl; \
  187. _FP_MUL_MEAT_DW_1_hard_a_f1 \
  188. = _FP_MUL_MEAT_DW_1_hard_xl * _FP_MUL_MEAT_DW_1_hard_yh; \
  189. R##_f1 = _FP_MUL_MEAT_DW_1_hard_xh * _FP_MUL_MEAT_DW_1_hard_yh; \
  190. \
  191. /* Reassemble into two full words. */ \
  192. if ((_FP_MUL_MEAT_DW_1_hard_a_f0 += _FP_MUL_MEAT_DW_1_hard_a_f1) \
  193. < _FP_MUL_MEAT_DW_1_hard_a_f1) \
  194. R##_f1 += (_FP_W_TYPE) 1 << (_FP_W_TYPE_SIZE/2); \
  195. _FP_MUL_MEAT_DW_1_hard_a_f1 \
  196. = _FP_MUL_MEAT_DW_1_hard_a_f0 >> (_FP_W_TYPE_SIZE/2); \
  197. _FP_MUL_MEAT_DW_1_hard_a_f0 \
  198. = _FP_MUL_MEAT_DW_1_hard_a_f0 << (_FP_W_TYPE_SIZE/2); \
  199. _FP_FRAC_ADD_2 (R, R, _FP_MUL_MEAT_DW_1_hard_a); \
  200. } \
  201. while (0)
  202. #define _FP_MUL_MEAT_1_hard(wfracbits, R, X, Y) \
  203. do \
  204. { \
  205. _FP_FRAC_DECL_2 (_FP_MUL_MEAT_1_hard_z); \
  206. _FP_MUL_MEAT_DW_1_hard ((wfracbits), \
  207. _FP_MUL_MEAT_1_hard_z, X, Y); \
  208. \
  209. /* Normalize. */ \
  210. _FP_FRAC_SRS_2 (_FP_MUL_MEAT_1_hard_z, \
  211. (wfracbits) - 1, 2*(wfracbits)); \
  212. R##_f = _FP_MUL_MEAT_1_hard_z_f0; \
  213. } \
  214. while (0)
  215. /* Division algorithms: */
  216. /* Basic. Assuming the host word size is >= 2*FRACBITS, we can do the
  217. division immediately. Give this macro either _FP_DIV_HELP_imm for
  218. C primitives or _FP_DIV_HELP_ldiv for the ISO function. Which you
  219. choose will depend on what the compiler does with divrem4. */
  220. #define _FP_DIV_MEAT_1_imm(fs, R, X, Y, doit) \
  221. do \
  222. { \
  223. _FP_W_TYPE _FP_DIV_MEAT_1_imm_q, _FP_DIV_MEAT_1_imm_r; \
  224. X##_f <<= (X##_f < Y##_f \
  225. ? R##_e--, _FP_WFRACBITS_##fs \
  226. : _FP_WFRACBITS_##fs - 1); \
  227. doit (_FP_DIV_MEAT_1_imm_q, _FP_DIV_MEAT_1_imm_r, X##_f, Y##_f); \
  228. R##_f = _FP_DIV_MEAT_1_imm_q | (_FP_DIV_MEAT_1_imm_r != 0); \
  229. } \
  230. while (0)
  231. /* GCC's longlong.h defines a 2W / 1W => (1W,1W) primitive udiv_qrnnd
  232. that may be useful in this situation. This first is for a primitive
  233. that requires normalization, the second for one that does not. Look
  234. for UDIV_NEEDS_NORMALIZATION to tell which your machine needs. */
  235. #define _FP_DIV_MEAT_1_udiv_norm(fs, R, X, Y) \
  236. do \
  237. { \
  238. _FP_W_TYPE _FP_DIV_MEAT_1_udiv_norm_nh; \
  239. _FP_W_TYPE _FP_DIV_MEAT_1_udiv_norm_nl; \
  240. _FP_W_TYPE _FP_DIV_MEAT_1_udiv_norm_q; \
  241. _FP_W_TYPE _FP_DIV_MEAT_1_udiv_norm_r; \
  242. _FP_W_TYPE _FP_DIV_MEAT_1_udiv_norm_y; \
  243. \
  244. /* Normalize Y -- i.e. make the most significant bit set. */ \
  245. _FP_DIV_MEAT_1_udiv_norm_y = Y##_f << _FP_WFRACXBITS_##fs; \
  246. \
  247. /* Shift X op correspondingly high, that is, up one full word. */ \
  248. if (X##_f < Y##_f) \
  249. { \
  250. R##_e--; \
  251. _FP_DIV_MEAT_1_udiv_norm_nl = 0; \
  252. _FP_DIV_MEAT_1_udiv_norm_nh = X##_f; \
  253. } \
  254. else \
  255. { \
  256. _FP_DIV_MEAT_1_udiv_norm_nl = X##_f << (_FP_W_TYPE_SIZE - 1); \
  257. _FP_DIV_MEAT_1_udiv_norm_nh = X##_f >> 1; \
  258. } \
  259. \
  260. udiv_qrnnd (_FP_DIV_MEAT_1_udiv_norm_q, \
  261. _FP_DIV_MEAT_1_udiv_norm_r, \
  262. _FP_DIV_MEAT_1_udiv_norm_nh, \
  263. _FP_DIV_MEAT_1_udiv_norm_nl, \
  264. _FP_DIV_MEAT_1_udiv_norm_y); \
  265. R##_f = (_FP_DIV_MEAT_1_udiv_norm_q \
  266. | (_FP_DIV_MEAT_1_udiv_norm_r != 0)); \
  267. } \
  268. while (0)
  269. #define _FP_DIV_MEAT_1_udiv(fs, R, X, Y) \
  270. do \
  271. { \
  272. _FP_W_TYPE _FP_DIV_MEAT_1_udiv_nh, _FP_DIV_MEAT_1_udiv_nl; \
  273. _FP_W_TYPE _FP_DIV_MEAT_1_udiv_q, _FP_DIV_MEAT_1_udiv_r; \
  274. if (X##_f < Y##_f) \
  275. { \
  276. R##_e--; \
  277. _FP_DIV_MEAT_1_udiv_nl = X##_f << _FP_WFRACBITS_##fs; \
  278. _FP_DIV_MEAT_1_udiv_nh = X##_f >> _FP_WFRACXBITS_##fs; \
  279. } \
  280. else \
  281. { \
  282. _FP_DIV_MEAT_1_udiv_nl = X##_f << (_FP_WFRACBITS_##fs - 1); \
  283. _FP_DIV_MEAT_1_udiv_nh = X##_f >> (_FP_WFRACXBITS_##fs + 1); \
  284. } \
  285. udiv_qrnnd (_FP_DIV_MEAT_1_udiv_q, _FP_DIV_MEAT_1_udiv_r, \
  286. _FP_DIV_MEAT_1_udiv_nh, _FP_DIV_MEAT_1_udiv_nl, \
  287. Y##_f); \
  288. R##_f = _FP_DIV_MEAT_1_udiv_q | (_FP_DIV_MEAT_1_udiv_r != 0); \
  289. } \
  290. while (0)
  291. /* Square root algorithms:
  292. We have just one right now, maybe Newton approximation
  293. should be added for those machines where division is fast. */
  294. #define _FP_SQRT_MEAT_1(R, S, T, X, q) \
  295. do \
  296. { \
  297. while ((q) != _FP_WORK_ROUND) \
  298. { \
  299. T##_f = S##_f + (q); \
  300. if (T##_f <= X##_f) \
  301. { \
  302. S##_f = T##_f + (q); \
  303. X##_f -= T##_f; \
  304. R##_f += (q); \
  305. } \
  306. _FP_FRAC_SLL_1 (X, 1); \
  307. (q) >>= 1; \
  308. } \
  309. if (X##_f) \
  310. { \
  311. if (S##_f < X##_f) \
  312. R##_f |= _FP_WORK_ROUND; \
  313. R##_f |= _FP_WORK_STICKY; \
  314. } \
  315. } \
  316. while (0)
  317. /* Assembly/disassembly for converting to/from integral types.
  318. No shifting or overflow handled here. */
  319. #define _FP_FRAC_ASSEMBLE_1(r, X, rsize) ((r) = X##_f)
  320. #define _FP_FRAC_DISASSEMBLE_1(X, r, rsize) (X##_f = (r))
  321. /* Convert FP values between word sizes. */
  322. #define _FP_FRAC_COPY_1_1(D, S) (D##_f = S##_f)
  323. #endif /* !SOFT_FP_OP_1_H */