soft-fp.h 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  1. /* Software floating-point emulation.
  2. Copyright (C) 1997-2019 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 1
  29. #ifdef _LIBC
  30. # include <sfp-machine.h>
  31. #elif defined __KERNEL__
  32. /* The Linux kernel uses asm/ names for architecture-specific
  33. files. */
  34. # include <asm/sfp-machine.h>
  35. #else
  36. # include "sfp-machine.h"
  37. #endif
  38. /* Allow sfp-machine to have its own byte order definitions. */
  39. #ifndef __BYTE_ORDER
  40. # ifdef _LIBC
  41. # include <endian.h>
  42. # else
  43. # error "endianness not defined by sfp-machine.h"
  44. # endif
  45. #endif
  46. /* For unreachable default cases in switch statements over bitwise OR
  47. of FP_CLS_* values. */
  48. #if (defined __GNUC__ \
  49. && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 5)))
  50. # define _FP_UNREACHABLE __builtin_unreachable ()
  51. #else
  52. # define _FP_UNREACHABLE abort ()
  53. #endif
  54. #if ((defined __GNUC__ \
  55. && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6))) \
  56. || (defined __STDC_VERSION__ && __STDC_VERSION__ >= 201112L))
  57. # define _FP_STATIC_ASSERT(expr, msg) \
  58. _Static_assert ((expr), msg)
  59. #else
  60. # define _FP_STATIC_ASSERT(expr, msg) \
  61. extern int (*__Static_assert_function (void)) \
  62. [!!sizeof (struct { int __error_if_negative: (expr) ? 2 : -1; })]
  63. #endif
  64. /* In the Linux kernel, some architectures have a single function that
  65. uses different kinds of unpacking and packing depending on the
  66. instruction being emulated, meaning it is not readily visible to
  67. the compiler that variables from _FP_DECL and _FP_FRAC_DECL_*
  68. macros are only used in cases where they were initialized. */
  69. #ifdef __KERNEL__
  70. # define _FP_ZERO_INIT = 0
  71. #else
  72. # define _FP_ZERO_INIT
  73. #endif
  74. #define _FP_WORKBITS 3
  75. #define _FP_WORK_LSB ((_FP_W_TYPE) 1 << 3)
  76. #define _FP_WORK_ROUND ((_FP_W_TYPE) 1 << 2)
  77. #define _FP_WORK_GUARD ((_FP_W_TYPE) 1 << 1)
  78. #define _FP_WORK_STICKY ((_FP_W_TYPE) 1 << 0)
  79. #ifndef FP_RND_NEAREST
  80. # define FP_RND_NEAREST 0
  81. # define FP_RND_ZERO 1
  82. # define FP_RND_PINF 2
  83. # define FP_RND_MINF 3
  84. #endif
  85. #ifndef FP_ROUNDMODE
  86. # define FP_ROUNDMODE FP_RND_NEAREST
  87. #endif
  88. /* By default don't care about exceptions. */
  89. #ifndef FP_EX_INVALID
  90. # define FP_EX_INVALID 0
  91. #endif
  92. #ifndef FP_EX_OVERFLOW
  93. # define FP_EX_OVERFLOW 0
  94. #endif
  95. #ifndef FP_EX_UNDERFLOW
  96. # define FP_EX_UNDERFLOW 0
  97. #endif
  98. #ifndef FP_EX_DIVZERO
  99. # define FP_EX_DIVZERO 0
  100. #endif
  101. #ifndef FP_EX_INEXACT
  102. # define FP_EX_INEXACT 0
  103. #endif
  104. #ifndef FP_EX_DENORM
  105. # define FP_EX_DENORM 0
  106. #endif
  107. /* Sub-exceptions of "invalid". */
  108. /* Signaling NaN operand. */
  109. #ifndef FP_EX_INVALID_SNAN
  110. # define FP_EX_INVALID_SNAN 0
  111. #endif
  112. /* Inf * 0. */
  113. #ifndef FP_EX_INVALID_IMZ
  114. # define FP_EX_INVALID_IMZ 0
  115. #endif
  116. /* fma (Inf, 0, c). */
  117. #ifndef FP_EX_INVALID_IMZ_FMA
  118. # define FP_EX_INVALID_IMZ_FMA 0
  119. #endif
  120. /* Inf - Inf. */
  121. #ifndef FP_EX_INVALID_ISI
  122. # define FP_EX_INVALID_ISI 0
  123. #endif
  124. /* 0 / 0. */
  125. #ifndef FP_EX_INVALID_ZDZ
  126. # define FP_EX_INVALID_ZDZ 0
  127. #endif
  128. /* Inf / Inf. */
  129. #ifndef FP_EX_INVALID_IDI
  130. # define FP_EX_INVALID_IDI 0
  131. #endif
  132. /* sqrt (negative). */
  133. #ifndef FP_EX_INVALID_SQRT
  134. # define FP_EX_INVALID_SQRT 0
  135. #endif
  136. /* Invalid conversion to integer. */
  137. #ifndef FP_EX_INVALID_CVI
  138. # define FP_EX_INVALID_CVI 0
  139. #endif
  140. /* Invalid comparison. */
  141. #ifndef FP_EX_INVALID_VC
  142. # define FP_EX_INVALID_VC 0
  143. #endif
  144. /* _FP_STRUCT_LAYOUT may be defined as an attribute to determine the
  145. struct layout variant used for structures where bit-fields are used
  146. to access specific parts of binary floating-point numbers. This is
  147. required for systems where the default ABI uses struct layout with
  148. differences in how consecutive bit-fields are laid out from the
  149. default expected by soft-fp. */
  150. #ifndef _FP_STRUCT_LAYOUT
  151. # define _FP_STRUCT_LAYOUT
  152. #endif
  153. #ifdef _FP_DECL_EX
  154. # define FP_DECL_EX \
  155. int _fex = 0; \
  156. _FP_DECL_EX
  157. #else
  158. # define FP_DECL_EX int _fex = 0
  159. #endif
  160. /* Initialize any machine-specific state used in FP_ROUNDMODE,
  161. FP_TRAPPING_EXCEPTIONS or FP_HANDLE_EXCEPTIONS. */
  162. #ifndef FP_INIT_ROUNDMODE
  163. # define FP_INIT_ROUNDMODE do {} while (0)
  164. #endif
  165. /* Initialize any machine-specific state used in
  166. FP_TRAPPING_EXCEPTIONS or FP_HANDLE_EXCEPTIONS. */
  167. #ifndef FP_INIT_TRAPPING_EXCEPTIONS
  168. # define FP_INIT_TRAPPING_EXCEPTIONS FP_INIT_ROUNDMODE
  169. #endif
  170. /* Initialize any machine-specific state used in
  171. FP_HANDLE_EXCEPTIONS. */
  172. #ifndef FP_INIT_EXCEPTIONS
  173. # define FP_INIT_EXCEPTIONS FP_INIT_TRAPPING_EXCEPTIONS
  174. #endif
  175. #ifndef FP_HANDLE_EXCEPTIONS
  176. # define FP_HANDLE_EXCEPTIONS do {} while (0)
  177. #endif
  178. /* Whether to flush subnormal inputs to zero with the same sign. */
  179. #ifndef FP_DENORM_ZERO
  180. # define FP_DENORM_ZERO 0
  181. #endif
  182. #ifndef FP_INHIBIT_RESULTS
  183. /* By default we write the results always.
  184. sfp-machine may override this and e.g.
  185. check if some exceptions are unmasked
  186. and inhibit it in such a case. */
  187. # define FP_INHIBIT_RESULTS 0
  188. #endif
  189. #define FP_SET_EXCEPTION(ex) \
  190. _fex |= (ex)
  191. #define FP_CUR_EXCEPTIONS \
  192. (_fex)
  193. #ifndef FP_TRAPPING_EXCEPTIONS
  194. # define FP_TRAPPING_EXCEPTIONS 0
  195. #endif
  196. /* A file using soft-fp may define FP_NO_EXCEPTIONS before including
  197. soft-fp.h to indicate that, although a macro used there could raise
  198. exceptions, or do rounding and potentially thereby raise
  199. exceptions, for some arguments, for the particular arguments used
  200. in that file no exceptions or rounding can occur. Such a file
  201. should not itself use macros relating to handling exceptions and
  202. rounding modes; this is only for indirect uses (in particular, in
  203. _FP_FROM_INT and the macros it calls). */
  204. #ifdef FP_NO_EXCEPTIONS
  205. # undef FP_SET_EXCEPTION
  206. # define FP_SET_EXCEPTION(ex) do {} while (0)
  207. # undef FP_CUR_EXCEPTIONS
  208. # define FP_CUR_EXCEPTIONS 0
  209. # undef FP_TRAPPING_EXCEPTIONS
  210. # define FP_TRAPPING_EXCEPTIONS 0
  211. # undef FP_ROUNDMODE
  212. # define FP_ROUNDMODE FP_RND_ZERO
  213. # undef _FP_TININESS_AFTER_ROUNDING
  214. # define _FP_TININESS_AFTER_ROUNDING 0
  215. #endif
  216. /* A file using soft-fp may define FP_NO_EXACT_UNDERFLOW before
  217. including soft-fp.h to indicate that, although a macro used there
  218. could allow for the case of exact underflow requiring the underflow
  219. exception to be raised if traps are enabled, for the particular
  220. arguments used in that file no exact underflow can occur. */
  221. #ifdef FP_NO_EXACT_UNDERFLOW
  222. # undef FP_TRAPPING_EXCEPTIONS
  223. # define FP_TRAPPING_EXCEPTIONS 0
  224. #endif
  225. #define _FP_ROUND_NEAREST(wc, X) \
  226. do \
  227. { \
  228. if ((_FP_FRAC_LOW_##wc (X) & 15) != _FP_WORK_ROUND) \
  229. _FP_FRAC_ADDI_##wc (X, _FP_WORK_ROUND); \
  230. } \
  231. while (0)
  232. #define _FP_ROUND_ZERO(wc, X) (void) 0
  233. #define _FP_ROUND_PINF(wc, X) \
  234. do \
  235. { \
  236. if (!X##_s && (_FP_FRAC_LOW_##wc (X) & 7)) \
  237. _FP_FRAC_ADDI_##wc (X, _FP_WORK_LSB); \
  238. } \
  239. while (0)
  240. #define _FP_ROUND_MINF(wc, X) \
  241. do \
  242. { \
  243. if (X##_s && (_FP_FRAC_LOW_##wc (X) & 7)) \
  244. _FP_FRAC_ADDI_##wc (X, _FP_WORK_LSB); \
  245. } \
  246. while (0)
  247. #define _FP_ROUND(wc, X) \
  248. do \
  249. { \
  250. if (_FP_FRAC_LOW_##wc (X) & 7) \
  251. { \
  252. FP_SET_EXCEPTION (FP_EX_INEXACT); \
  253. switch (FP_ROUNDMODE) \
  254. { \
  255. case FP_RND_NEAREST: \
  256. _FP_ROUND_NEAREST (wc, X); \
  257. break; \
  258. case FP_RND_ZERO: \
  259. _FP_ROUND_ZERO (wc, X); \
  260. break; \
  261. case FP_RND_PINF: \
  262. _FP_ROUND_PINF (wc, X); \
  263. break; \
  264. case FP_RND_MINF: \
  265. _FP_ROUND_MINF (wc, X); \
  266. break; \
  267. } \
  268. } \
  269. } \
  270. while (0)
  271. #define FP_CLS_NORMAL 0
  272. #define FP_CLS_ZERO 1
  273. #define FP_CLS_INF 2
  274. #define FP_CLS_NAN 3
  275. #define _FP_CLS_COMBINE(x, y) (((x) << 2) | (y))
  276. #include "op-1.h"
  277. #include "op-2.h"
  278. #include "op-4.h"
  279. #include "op-8.h"
  280. #include "op-common.h"
  281. /* Sigh. Silly things longlong.h needs. */
  282. #define UWtype _FP_W_TYPE
  283. #define W_TYPE_SIZE _FP_W_TYPE_SIZE
  284. typedef int QItype __attribute__ ((mode (QI)));
  285. typedef int SItype __attribute__ ((mode (SI)));
  286. typedef int DItype __attribute__ ((mode (DI)));
  287. typedef unsigned int UQItype __attribute__ ((mode (QI)));
  288. typedef unsigned int USItype __attribute__ ((mode (SI)));
  289. typedef unsigned int UDItype __attribute__ ((mode (DI)));
  290. #if _FP_W_TYPE_SIZE == 32
  291. typedef unsigned int UHWtype __attribute__ ((mode (HI)));
  292. #elif _FP_W_TYPE_SIZE == 64
  293. typedef USItype UHWtype;
  294. #endif
  295. #ifndef CMPtype
  296. # define CMPtype int
  297. #endif
  298. #define SI_BITS (__CHAR_BIT__ * (int) sizeof (SItype))
  299. #define DI_BITS (__CHAR_BIT__ * (int) sizeof (DItype))
  300. #ifndef umul_ppmm
  301. # ifdef _LIBC
  302. # include <stdlib/longlong.h>
  303. # else
  304. # include "longlong.h"
  305. # endif
  306. #endif
  307. #endif /* !SOFT_FP_H */