soft-fp.h 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. /* Software floating-point emulation.
  2. Copyright (C) 1997-2014 Free Software Foundation, Inc.
  3. This file is part of the GNU C Library.
  4. Contributed by Richard Henderson (rth@cygnus.com),
  5. Jakub Jelinek (jj@ultra.linux.cz),
  6. David S. Miller (davem@redhat.com) and
  7. Peter Maydell (pmaydell@chiark.greenend.org.uk).
  8. The GNU C Library is free software; you can redistribute it and/or
  9. modify it under the terms of the GNU Lesser General Public
  10. License as published by the Free Software Foundation; either
  11. version 2.1 of the License, or (at your option) any later version.
  12. In addition to the permissions in the GNU Lesser General Public
  13. License, the Free Software Foundation gives you unlimited
  14. permission to link the compiled version of this file into
  15. combinations with other programs, and to distribute those
  16. combinations without any restriction coming from the use of this
  17. file. (The Lesser General Public License restrictions do apply in
  18. other respects; for example, they cover modification of the file,
  19. and distribution when not linked into a combine executable.)
  20. The GNU C Library is distributed in the hope that it will be useful,
  21. but WITHOUT ANY WARRANTY; without even the implied warranty of
  22. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  23. Lesser General Public License for more details.
  24. You should have received a copy of the GNU Lesser General Public
  25. License along with the GNU C Library; if not, see
  26. <http://www.gnu.org/licenses/>. */
  27. #ifndef SOFT_FP_H
  28. #define SOFT_FP_H
  29. #ifdef _LIBC
  30. # include <sfp-machine.h>
  31. #else
  32. # include "sfp-machine.h"
  33. #endif
  34. /* Allow sfp-machine to have its own byte order definitions. */
  35. #ifndef __BYTE_ORDER
  36. # ifdef _LIBC
  37. # include <endian.h>
  38. # else
  39. # error "endianness not defined by sfp-machine.h"
  40. # endif
  41. #endif
  42. #define _FP_WORKBITS 3
  43. #define _FP_WORK_LSB ((_FP_W_TYPE) 1 << 3)
  44. #define _FP_WORK_ROUND ((_FP_W_TYPE) 1 << 2)
  45. #define _FP_WORK_GUARD ((_FP_W_TYPE) 1 << 1)
  46. #define _FP_WORK_STICKY ((_FP_W_TYPE) 1 << 0)
  47. #ifndef FP_RND_NEAREST
  48. # define FP_RND_NEAREST 0
  49. # define FP_RND_ZERO 1
  50. # define FP_RND_PINF 2
  51. # define FP_RND_MINF 3
  52. #endif
  53. #ifndef FP_ROUNDMODE
  54. # define FP_ROUNDMODE FP_RND_NEAREST
  55. #endif
  56. /* By default don't care about exceptions. */
  57. #ifndef FP_EX_INVALID
  58. # define FP_EX_INVALID 0
  59. #endif
  60. #ifndef FP_EX_OVERFLOW
  61. # define FP_EX_OVERFLOW 0
  62. #endif
  63. #ifndef FP_EX_UNDERFLOW
  64. # define FP_EX_UNDERFLOW 0
  65. #endif
  66. #ifndef FP_EX_DIVZERO
  67. # define FP_EX_DIVZERO 0
  68. #endif
  69. #ifndef FP_EX_INEXACT
  70. # define FP_EX_INEXACT 0
  71. #endif
  72. #ifndef FP_EX_DENORM
  73. # define FP_EX_DENORM 0
  74. #endif
  75. /* Sub-exceptions of "invalid". */
  76. /* Signaling NaN operand. */
  77. #ifndef FP_EX_INVALID_SNAN
  78. # define FP_EX_INVALID_SNAN 0
  79. #endif
  80. /* Inf * 0. */
  81. #ifndef FP_EX_INVALID_IMZ
  82. # define FP_EX_INVALID_IMZ 0
  83. #endif
  84. /* fma (Inf, 0, c). */
  85. #ifndef FP_EX_INVALID_IMZ_FMA
  86. # define FP_EX_INVALID_IMZ_FMA 0
  87. #endif
  88. /* Inf - Inf. */
  89. #ifndef FP_EX_INVALID_ISI
  90. # define FP_EX_INVALID_ISI 0
  91. #endif
  92. /* 0 / 0. */
  93. #ifndef FP_EX_INVALID_ZDZ
  94. # define FP_EX_INVALID_ZDZ 0
  95. #endif
  96. /* Inf / Inf. */
  97. #ifndef FP_EX_INVALID_IDI
  98. # define FP_EX_INVALID_IDI 0
  99. #endif
  100. /* sqrt (negative). */
  101. #ifndef FP_EX_INVALID_SQRT
  102. # define FP_EX_INVALID_SQRT 0
  103. #endif
  104. /* Invalid conversion to integer. */
  105. #ifndef FP_EX_INVALID_CVI
  106. # define FP_EX_INVALID_CVI 0
  107. #endif
  108. /* Invalid comparison. */
  109. #ifndef FP_EX_INVALID_VC
  110. # define FP_EX_INVALID_VC 0
  111. #endif
  112. /* _FP_STRUCT_LAYOUT may be defined as an attribute to determine the
  113. struct layout variant used for structures where bit-fields are used
  114. to access specific parts of binary floating-point numbers. This is
  115. required for systems where the default ABI uses struct layout with
  116. differences in how consecutive bit-fields are laid out from the
  117. default expected by soft-fp. */
  118. #ifndef _FP_STRUCT_LAYOUT
  119. # define _FP_STRUCT_LAYOUT
  120. #endif
  121. #ifdef _FP_DECL_EX
  122. # define FP_DECL_EX \
  123. int _fex = 0; \
  124. _FP_DECL_EX
  125. #else
  126. # define FP_DECL_EX int _fex = 0
  127. #endif
  128. /* Initialize any machine-specific state used in FP_ROUNDMODE,
  129. FP_TRAPPING_EXCEPTIONS or FP_HANDLE_EXCEPTIONS. */
  130. #ifndef FP_INIT_ROUNDMODE
  131. # define FP_INIT_ROUNDMODE do {} while (0)
  132. #endif
  133. /* Initialize any machine-specific state used in
  134. FP_TRAPPING_EXCEPTIONS or FP_HANDLE_EXCEPTIONS. */
  135. #ifndef FP_INIT_TRAPPING_EXCEPTIONS
  136. # define FP_INIT_TRAPPING_EXCEPTIONS FP_INIT_ROUNDMODE
  137. #endif
  138. /* Initialize any machine-specific state used in
  139. FP_HANDLE_EXCEPTIONS. */
  140. #ifndef FP_INIT_EXCEPTIONS
  141. # define FP_INIT_EXCEPTIONS FP_INIT_TRAPPING_EXCEPTIONS
  142. #endif
  143. #ifndef FP_HANDLE_EXCEPTIONS
  144. # define FP_HANDLE_EXCEPTIONS do {} while (0)
  145. #endif
  146. /* Whether to flush subnormal inputs to zero with the same sign. */
  147. #ifndef FP_DENORM_ZERO
  148. # define FP_DENORM_ZERO 0
  149. #endif
  150. #ifndef FP_INHIBIT_RESULTS
  151. /* By default we write the results always.
  152. sfp-machine may override this and e.g.
  153. check if some exceptions are unmasked
  154. and inhibit it in such a case. */
  155. # define FP_INHIBIT_RESULTS 0
  156. #endif
  157. #define FP_SET_EXCEPTION(ex) \
  158. _fex |= (ex)
  159. #define FP_CUR_EXCEPTIONS \
  160. (_fex)
  161. #ifndef FP_TRAPPING_EXCEPTIONS
  162. # define FP_TRAPPING_EXCEPTIONS 0
  163. #endif
  164. /* A file using soft-fp may define FP_NO_EXCEPTIONS before including
  165. soft-fp.h to indicate that, although a macro used there could raise
  166. exceptions, or do rounding and potentially thereby raise
  167. exceptions, for some arguments, for the particular arguments used
  168. in that file no exceptions or rounding can occur. Such a file
  169. should not itself use macros relating to handling exceptions and
  170. rounding modes; this is only for indirect uses (in particular, in
  171. _FP_FROM_INT and the macros it calls). */
  172. #ifdef FP_NO_EXCEPTIONS
  173. # undef FP_SET_EXCEPTION
  174. # define FP_SET_EXCEPTION(ex) do {} while (0)
  175. # undef FP_CUR_EXCEPTIONS
  176. # define FP_CUR_EXCEPTIONS 0
  177. # undef FP_TRAPPING_EXCEPTIONS
  178. # define FP_TRAPPING_EXCEPTIONS 0
  179. # undef FP_ROUNDMODE
  180. # define FP_ROUNDMODE FP_RND_ZERO
  181. # undef _FP_TININESS_AFTER_ROUNDING
  182. # define _FP_TININESS_AFTER_ROUNDING 0
  183. #endif
  184. /* A file using soft-fp may define FP_NO_EXACT_UNDERFLOW before
  185. including soft-fp.h to indicate that, although a macro used there
  186. could allow for the case of exact underflow requiring the underflow
  187. exception to be raised if traps are enabled, for the particular
  188. arguments used in that file no exact underflow can occur. */
  189. #ifdef FP_NO_EXACT_UNDERFLOW
  190. # undef FP_TRAPPING_EXCEPTIONS
  191. # define FP_TRAPPING_EXCEPTIONS 0
  192. #endif
  193. #define _FP_ROUND_NEAREST(wc, X) \
  194. do \
  195. { \
  196. if ((_FP_FRAC_LOW_##wc (X) & 15) != _FP_WORK_ROUND) \
  197. _FP_FRAC_ADDI_##wc (X, _FP_WORK_ROUND); \
  198. } \
  199. while (0)
  200. #define _FP_ROUND_ZERO(wc, X) (void) 0
  201. #define _FP_ROUND_PINF(wc, X) \
  202. do \
  203. { \
  204. if (!X##_s && (_FP_FRAC_LOW_##wc (X) & 7)) \
  205. _FP_FRAC_ADDI_##wc (X, _FP_WORK_LSB); \
  206. } \
  207. while (0)
  208. #define _FP_ROUND_MINF(wc, X) \
  209. do \
  210. { \
  211. if (X##_s && (_FP_FRAC_LOW_##wc (X) & 7)) \
  212. _FP_FRAC_ADDI_##wc (X, _FP_WORK_LSB); \
  213. } \
  214. while (0)
  215. #define _FP_ROUND(wc, X) \
  216. do \
  217. { \
  218. if (_FP_FRAC_LOW_##wc (X) & 7) \
  219. { \
  220. FP_SET_EXCEPTION (FP_EX_INEXACT); \
  221. switch (FP_ROUNDMODE) \
  222. { \
  223. case FP_RND_NEAREST: \
  224. _FP_ROUND_NEAREST (wc, X); \
  225. break; \
  226. case FP_RND_ZERO: \
  227. _FP_ROUND_ZERO (wc, X); \
  228. break; \
  229. case FP_RND_PINF: \
  230. _FP_ROUND_PINF (wc, X); \
  231. break; \
  232. case FP_RND_MINF: \
  233. _FP_ROUND_MINF (wc, X); \
  234. break; \
  235. } \
  236. } \
  237. } \
  238. while (0)
  239. #define FP_CLS_NORMAL 0
  240. #define FP_CLS_ZERO 1
  241. #define FP_CLS_INF 2
  242. #define FP_CLS_NAN 3
  243. #define _FP_CLS_COMBINE(x, y) (((x) << 2) | (y))
  244. #include "op-1.h"
  245. #include "op-2.h"
  246. #include "op-4.h"
  247. #include "op-8.h"
  248. #include "op-common.h"
  249. /* Sigh. Silly things longlong.h needs. */
  250. #define UWtype _FP_W_TYPE
  251. #define W_TYPE_SIZE _FP_W_TYPE_SIZE
  252. typedef int QItype __attribute__ ((mode (QI)));
  253. typedef int SItype __attribute__ ((mode (SI)));
  254. typedef int DItype __attribute__ ((mode (DI)));
  255. typedef unsigned int UQItype __attribute__ ((mode (QI)));
  256. typedef unsigned int USItype __attribute__ ((mode (SI)));
  257. typedef unsigned int UDItype __attribute__ ((mode (DI)));
  258. #if _FP_W_TYPE_SIZE == 32
  259. typedef unsigned int UHWtype __attribute__ ((mode (HI)));
  260. #elif _FP_W_TYPE_SIZE == 64
  261. typedef USItype UHWtype;
  262. #endif
  263. #ifndef CMPtype
  264. # define CMPtype int
  265. #endif
  266. #define SI_BITS (__CHAR_BIT__ * (int) sizeof (SItype))
  267. #define DI_BITS (__CHAR_BIT__ * (int) sizeof (DItype))
  268. #ifndef umul_ppmm
  269. # ifdef _LIBC
  270. # include <stdlib/longlong.h>
  271. # else
  272. # include "longlong.h"
  273. # endif
  274. #endif
  275. #ifdef _LIBC
  276. # include <stdlib.h>
  277. #else
  278. extern void abort (void);
  279. #endif
  280. #endif