real.h 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491
  1. /* Definitions of floating-point access for GNU compiler.
  2. Copyright (C) 1989-2015 Free Software Foundation, Inc.
  3. This file is part of GCC.
  4. GCC is free software; you can redistribute it and/or modify it under
  5. the terms of the GNU General Public License as published by the Free
  6. Software Foundation; either version 3, or (at your option) any later
  7. version.
  8. GCC is distributed in the hope that it will be useful, but WITHOUT ANY
  9. WARRANTY; without even the implied warranty of MERCHANTABILITY or
  10. FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  11. for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GCC; see the file COPYING3. If not see
  14. <http://www.gnu.org/licenses/>. */
  15. #ifndef GCC_REAL_H
  16. #define GCC_REAL_H
  17. #include "machmode.h"
  18. #include "signop.h"
  19. #include "wide-int.h"
  20. #include "insn-modes.h"
  21. /* An expanded form of the represented number. */
  22. /* Enumerate the special cases of numbers that we encounter. */
  23. enum real_value_class {
  24. rvc_zero,
  25. rvc_normal,
  26. rvc_inf,
  27. rvc_nan
  28. };
  29. #define SIGNIFICAND_BITS (128 + HOST_BITS_PER_LONG)
  30. #define EXP_BITS (32 - 6)
  31. #define MAX_EXP ((1 << (EXP_BITS - 1)) - 1)
  32. #define SIGSZ (SIGNIFICAND_BITS / HOST_BITS_PER_LONG)
  33. #define SIG_MSB ((unsigned long)1 << (HOST_BITS_PER_LONG - 1))
  34. struct GTY(()) real_value {
  35. /* Use the same underlying type for all bit-fields, so as to make
  36. sure they're packed together, otherwise REAL_VALUE_TYPE_SIZE will
  37. be miscomputed. */
  38. unsigned int /* ENUM_BITFIELD (real_value_class) */ cl : 2;
  39. unsigned int decimal : 1;
  40. unsigned int sign : 1;
  41. unsigned int signalling : 1;
  42. unsigned int canonical : 1;
  43. unsigned int uexp : EXP_BITS;
  44. unsigned long sig[SIGSZ];
  45. };
  46. #define REAL_EXP(REAL) \
  47. ((int)((REAL)->uexp ^ (unsigned int)(1 << (EXP_BITS - 1))) \
  48. - (1 << (EXP_BITS - 1)))
  49. #define SET_REAL_EXP(REAL, EXP) \
  50. ((REAL)->uexp = ((unsigned int)(EXP) & (unsigned int)((1 << EXP_BITS) - 1)))
  51. /* Various headers condition prototypes on #ifdef REAL_VALUE_TYPE, so it
  52. needs to be a macro. We do need to continue to have a structure tag
  53. so that other headers can forward declare it. */
  54. #define REAL_VALUE_TYPE struct real_value
  55. /* We store a REAL_VALUE_TYPE into an rtx, and we do this by putting it in
  56. consecutive "w" slots. Moreover, we've got to compute the number of "w"
  57. slots at preprocessor time, which means we can't use sizeof. Guess. */
  58. #define REAL_VALUE_TYPE_SIZE (SIGNIFICAND_BITS + 32)
  59. #define REAL_WIDTH \
  60. (REAL_VALUE_TYPE_SIZE/HOST_BITS_PER_WIDE_INT \
  61. + (REAL_VALUE_TYPE_SIZE%HOST_BITS_PER_WIDE_INT ? 1 : 0)) /* round up */
  62. /* Verify the guess. */
  63. extern char test_real_width
  64. [sizeof (REAL_VALUE_TYPE) <= REAL_WIDTH * sizeof (HOST_WIDE_INT) ? 1 : -1];
  65. /* Calculate the format for CONST_DOUBLE. We need as many slots as
  66. are necessary to overlay a REAL_VALUE_TYPE on them. This could be
  67. as many as four (32-bit HOST_WIDE_INT, 128-bit REAL_VALUE_TYPE).
  68. A number of places assume that there are always at least two 'w'
  69. slots in a CONST_DOUBLE, so we provide them even if one would suffice. */
  70. #if REAL_WIDTH == 1
  71. # define CONST_DOUBLE_FORMAT "ww"
  72. #else
  73. # if REAL_WIDTH == 2
  74. # define CONST_DOUBLE_FORMAT "ww"
  75. # else
  76. # if REAL_WIDTH == 3
  77. # define CONST_DOUBLE_FORMAT "www"
  78. # else
  79. # if REAL_WIDTH == 4
  80. # define CONST_DOUBLE_FORMAT "wwww"
  81. # else
  82. # if REAL_WIDTH == 5
  83. # define CONST_DOUBLE_FORMAT "wwwww"
  84. # else
  85. # if REAL_WIDTH == 6
  86. # define CONST_DOUBLE_FORMAT "wwwwww"
  87. # else
  88. #error "REAL_WIDTH > 6 not supported"
  89. # endif
  90. # endif
  91. # endif
  92. # endif
  93. # endif
  94. #endif
  95. /* Describes the properties of the specific target format in use. */
  96. struct real_format
  97. {
  98. /* Move to and from the target bytes. */
  99. void (*encode) (const struct real_format *, long *,
  100. const REAL_VALUE_TYPE *);
  101. void (*decode) (const struct real_format *, REAL_VALUE_TYPE *,
  102. const long *);
  103. /* The radix of the exponent and digits of the significand. */
  104. int b;
  105. /* Size of the significand in digits of radix B. */
  106. int p;
  107. /* Size of the significant of a NaN, in digits of radix B. */
  108. int pnan;
  109. /* The minimum negative integer, x, such that b**(x-1) is normalized. */
  110. int emin;
  111. /* The maximum integer, x, such that b**(x-1) is representable. */
  112. int emax;
  113. /* The bit position of the sign bit, for determining whether a value
  114. is positive/negative, or -1 for a complex encoding. */
  115. int signbit_ro;
  116. /* The bit position of the sign bit, for changing the sign of a number,
  117. or -1 for a complex encoding. */
  118. int signbit_rw;
  119. /* Default rounding mode for operations on this format. */
  120. bool round_towards_zero;
  121. bool has_sign_dependent_rounding;
  122. /* Properties of the format. */
  123. bool has_nans;
  124. bool has_inf;
  125. bool has_denorm;
  126. bool has_signed_zero;
  127. bool qnan_msb_set;
  128. bool canonical_nan_lsbs_set;
  129. const char *name;
  130. };
  131. /* The target format used for each floating point mode.
  132. Float modes are followed by decimal float modes, with entries for
  133. float modes indexed by (MODE - first float mode), and entries for
  134. decimal float modes indexed by (MODE - first decimal float mode) +
  135. the number of float modes. */
  136. extern const struct real_format *
  137. real_format_for_mode[MAX_MODE_FLOAT - MIN_MODE_FLOAT + 1
  138. + MAX_MODE_DECIMAL_FLOAT - MIN_MODE_DECIMAL_FLOAT + 1];
  139. #define REAL_MODE_FORMAT(MODE) \
  140. (real_format_for_mode[DECIMAL_FLOAT_MODE_P (MODE) \
  141. ? (((MODE) - MIN_MODE_DECIMAL_FLOAT) \
  142. + (MAX_MODE_FLOAT - MIN_MODE_FLOAT + 1)) \
  143. : ((MODE) - MIN_MODE_FLOAT)])
  144. #define FLOAT_MODE_FORMAT(MODE) \
  145. (REAL_MODE_FORMAT (SCALAR_FLOAT_MODE_P (MODE)? (MODE) \
  146. : GET_MODE_INNER (MODE)))
  147. /* The following macro determines whether the floating point format is
  148. composite, i.e. may contain non-consecutive mantissa bits, in which
  149. case compile-time FP overflow may not model run-time overflow. */
  150. #define MODE_COMPOSITE_P(MODE) \
  151. (FLOAT_MODE_P (MODE) \
  152. && FLOAT_MODE_FORMAT (MODE)->pnan < FLOAT_MODE_FORMAT (MODE)->p)
  153. /* Accessor macros for format properties. */
  154. #define MODE_HAS_NANS(MODE) \
  155. (FLOAT_MODE_P (MODE) && FLOAT_MODE_FORMAT (MODE)->has_nans)
  156. #define MODE_HAS_INFINITIES(MODE) \
  157. (FLOAT_MODE_P (MODE) && FLOAT_MODE_FORMAT (MODE)->has_inf)
  158. #define MODE_HAS_SIGNED_ZEROS(MODE) \
  159. (FLOAT_MODE_P (MODE) && FLOAT_MODE_FORMAT (MODE)->has_signed_zero)
  160. #define MODE_HAS_SIGN_DEPENDENT_ROUNDING(MODE) \
  161. (FLOAT_MODE_P (MODE) \
  162. && FLOAT_MODE_FORMAT (MODE)->has_sign_dependent_rounding)
  163. /* Declare functions in real.c. */
  164. /* True if the given mode has a NaN representation and the treatment of
  165. NaN operands is important. Certain optimizations, such as folding
  166. x * 0 into 0, are not correct for NaN operands, and are normally
  167. disabled for modes with NaNs. The user can ask for them to be
  168. done anyway using the -funsafe-math-optimizations switch. */
  169. extern bool HONOR_NANS (machine_mode);
  170. extern bool HONOR_NANS (const_tree);
  171. extern bool HONOR_NANS (const_rtx);
  172. /* Like HONOR_NANs, but true if we honor signaling NaNs (or sNaNs). */
  173. extern bool HONOR_SNANS (machine_mode);
  174. extern bool HONOR_SNANS (const_tree);
  175. extern bool HONOR_SNANS (const_rtx);
  176. /* As for HONOR_NANS, but true if the mode can represent infinity and
  177. the treatment of infinite values is important. */
  178. extern bool HONOR_INFINITIES (machine_mode);
  179. extern bool HONOR_INFINITIES (const_tree);
  180. extern bool HONOR_INFINITIES (const_rtx);
  181. /* Like HONOR_NANS, but true if the given mode distinguishes between
  182. positive and negative zero, and the sign of zero is important. */
  183. extern bool HONOR_SIGNED_ZEROS (machine_mode);
  184. extern bool HONOR_SIGNED_ZEROS (const_tree);
  185. extern bool HONOR_SIGNED_ZEROS (const_rtx);
  186. /* Like HONOR_NANS, but true if given mode supports sign-dependent rounding,
  187. and the rounding mode is important. */
  188. extern bool HONOR_SIGN_DEPENDENT_ROUNDING (machine_mode);
  189. extern bool HONOR_SIGN_DEPENDENT_ROUNDING (const_tree);
  190. extern bool HONOR_SIGN_DEPENDENT_ROUNDING (const_rtx);
  191. /* Binary or unary arithmetic on tree_code. */
  192. extern bool real_arithmetic (REAL_VALUE_TYPE *, int, const REAL_VALUE_TYPE *,
  193. const REAL_VALUE_TYPE *);
  194. /* Compare reals by tree_code. */
  195. extern bool real_compare (int, const REAL_VALUE_TYPE *, const REAL_VALUE_TYPE *);
  196. /* Determine whether a floating-point value X is infinite. */
  197. extern bool real_isinf (const REAL_VALUE_TYPE *);
  198. /* Determine whether a floating-point value X is a NaN. */
  199. extern bool real_isnan (const REAL_VALUE_TYPE *);
  200. /* Determine whether a floating-point value X is finite. */
  201. extern bool real_isfinite (const REAL_VALUE_TYPE *);
  202. /* Determine whether a floating-point value X is negative. */
  203. extern bool real_isneg (const REAL_VALUE_TYPE *);
  204. /* Determine whether a floating-point value X is minus zero. */
  205. extern bool real_isnegzero (const REAL_VALUE_TYPE *);
  206. /* Compare two floating-point objects for bitwise identity. */
  207. extern bool real_identical (const REAL_VALUE_TYPE *, const REAL_VALUE_TYPE *);
  208. /* Extend or truncate to a new mode. */
  209. extern void real_convert (REAL_VALUE_TYPE *, machine_mode,
  210. const REAL_VALUE_TYPE *);
  211. /* Return true if truncating to NEW is exact. */
  212. extern bool exact_real_truncate (machine_mode, const REAL_VALUE_TYPE *);
  213. /* Render R as a decimal floating point constant. */
  214. extern void real_to_decimal (char *, const REAL_VALUE_TYPE *, size_t,
  215. size_t, int);
  216. /* Render R as a decimal floating point constant, rounded so as to be
  217. parsed back to the same value when interpreted in mode MODE. */
  218. extern void real_to_decimal_for_mode (char *, const REAL_VALUE_TYPE *, size_t,
  219. size_t, int, machine_mode);
  220. /* Render R as a hexadecimal floating point constant. */
  221. extern void real_to_hexadecimal (char *, const REAL_VALUE_TYPE *,
  222. size_t, size_t, int);
  223. /* Render R as an integer. */
  224. extern HOST_WIDE_INT real_to_integer (const REAL_VALUE_TYPE *);
  225. /* Initialize R from a decimal or hexadecimal string. Return -1 if
  226. the value underflows, +1 if overflows, and 0 otherwise. */
  227. extern int real_from_string (REAL_VALUE_TYPE *, const char *);
  228. /* Wrapper to allow different internal representation for decimal floats. */
  229. extern void real_from_string3 (REAL_VALUE_TYPE *, const char *, machine_mode);
  230. extern long real_to_target_fmt (long *, const REAL_VALUE_TYPE *,
  231. const struct real_format *);
  232. extern long real_to_target (long *, const REAL_VALUE_TYPE *, machine_mode);
  233. extern void real_from_target_fmt (REAL_VALUE_TYPE *, const long *,
  234. const struct real_format *);
  235. extern void real_from_target (REAL_VALUE_TYPE *, const long *,
  236. machine_mode);
  237. extern void real_inf (REAL_VALUE_TYPE *);
  238. extern bool real_nan (REAL_VALUE_TYPE *, const char *, int, machine_mode);
  239. extern void real_maxval (REAL_VALUE_TYPE *, int, machine_mode);
  240. extern void real_2expN (REAL_VALUE_TYPE *, int, machine_mode);
  241. extern unsigned int real_hash (const REAL_VALUE_TYPE *);
  242. /* Target formats defined in real.c. */
  243. extern const struct real_format ieee_single_format;
  244. extern const struct real_format mips_single_format;
  245. extern const struct real_format motorola_single_format;
  246. extern const struct real_format spu_single_format;
  247. extern const struct real_format ieee_double_format;
  248. extern const struct real_format mips_double_format;
  249. extern const struct real_format motorola_double_format;
  250. extern const struct real_format ieee_extended_motorola_format;
  251. extern const struct real_format ieee_extended_intel_96_format;
  252. extern const struct real_format ieee_extended_intel_96_round_53_format;
  253. extern const struct real_format ieee_extended_intel_128_format;
  254. extern const struct real_format ibm_extended_format;
  255. extern const struct real_format mips_extended_format;
  256. extern const struct real_format ieee_quad_format;
  257. extern const struct real_format mips_quad_format;
  258. extern const struct real_format vax_f_format;
  259. extern const struct real_format vax_d_format;
  260. extern const struct real_format vax_g_format;
  261. extern const struct real_format real_internal_format;
  262. extern const struct real_format decimal_single_format;
  263. extern const struct real_format decimal_double_format;
  264. extern const struct real_format decimal_quad_format;
  265. extern const struct real_format ieee_half_format;
  266. extern const struct real_format arm_half_format;
  267. /* ====================================================================== */
  268. /* Crap. */
  269. #define REAL_ARITHMETIC(value, code, d1, d2) \
  270. real_arithmetic (&(value), code, &(d1), &(d2))
  271. #define REAL_VALUES_IDENTICAL(x, y) real_identical (&(x), &(y))
  272. #define REAL_VALUES_EQUAL(x, y) real_compare (EQ_EXPR, &(x), &(y))
  273. #define REAL_VALUES_LESS(x, y) real_compare (LT_EXPR, &(x), &(y))
  274. /* Determine whether a floating-point value X is infinite. */
  275. #define REAL_VALUE_ISINF(x) real_isinf (&(x))
  276. /* Determine whether a floating-point value X is a NaN. */
  277. #define REAL_VALUE_ISNAN(x) real_isnan (&(x))
  278. /* Determine whether a floating-point value X is negative. */
  279. #define REAL_VALUE_NEGATIVE(x) real_isneg (&(x))
  280. /* Determine whether a floating-point value X is minus zero. */
  281. #define REAL_VALUE_MINUS_ZERO(x) real_isnegzero (&(x))
  282. /* IN is a REAL_VALUE_TYPE. OUT is an array of longs. */
  283. #define REAL_VALUE_TO_TARGET_LONG_DOUBLE(IN, OUT) \
  284. real_to_target (OUT, &(IN), \
  285. mode_for_size (LONG_DOUBLE_TYPE_SIZE, MODE_FLOAT, 0))
  286. #define REAL_VALUE_TO_TARGET_DOUBLE(IN, OUT) \
  287. real_to_target (OUT, &(IN), mode_for_size (64, MODE_FLOAT, 0))
  288. /* IN is a REAL_VALUE_TYPE. OUT is a long. */
  289. #define REAL_VALUE_TO_TARGET_SINGLE(IN, OUT) \
  290. ((OUT) = real_to_target (NULL, &(IN), mode_for_size (32, MODE_FLOAT, 0)))
  291. /* Real values to IEEE 754 decimal floats. */
  292. /* IN is a REAL_VALUE_TYPE. OUT is an array of longs. */
  293. #define REAL_VALUE_TO_TARGET_DECIMAL128(IN, OUT) \
  294. real_to_target (OUT, &(IN), mode_for_size (128, MODE_DECIMAL_FLOAT, 0))
  295. #define REAL_VALUE_TO_TARGET_DECIMAL64(IN, OUT) \
  296. real_to_target (OUT, &(IN), mode_for_size (64, MODE_DECIMAL_FLOAT, 0))
  297. /* IN is a REAL_VALUE_TYPE. OUT is a long. */
  298. #define REAL_VALUE_TO_TARGET_DECIMAL32(IN, OUT) \
  299. ((OUT) = real_to_target (NULL, &(IN), mode_for_size (32, MODE_DECIMAL_FLOAT, 0)))
  300. extern REAL_VALUE_TYPE real_value_truncate (machine_mode,
  301. REAL_VALUE_TYPE);
  302. extern REAL_VALUE_TYPE real_value_negate (const REAL_VALUE_TYPE *);
  303. extern REAL_VALUE_TYPE real_value_abs (const REAL_VALUE_TYPE *);
  304. extern int significand_size (machine_mode);
  305. extern REAL_VALUE_TYPE real_from_string2 (const char *, machine_mode);
  306. #define REAL_VALUE_ATOF(s, m) \
  307. real_from_string2 (s, m)
  308. #define CONST_DOUBLE_ATOF(s, m) \
  309. CONST_DOUBLE_FROM_REAL_VALUE (real_from_string2 (s, m), m)
  310. #define REAL_VALUE_FIX(r) \
  311. real_to_integer (&(r))
  312. /* ??? Not quite right. */
  313. #define REAL_VALUE_UNSIGNED_FIX(r) \
  314. real_to_integer (&(r))
  315. /* ??? These were added for Paranoia support. */
  316. /* Return floor log2(R). */
  317. extern int real_exponent (const REAL_VALUE_TYPE *);
  318. /* R = A * 2**EXP. */
  319. extern void real_ldexp (REAL_VALUE_TYPE *, const REAL_VALUE_TYPE *, int);
  320. /* **** End of software floating point emulator interface macros **** */
  321. /* Constant real values 0, 1, 2, -1 and 0.5. */
  322. extern REAL_VALUE_TYPE dconst0;
  323. extern REAL_VALUE_TYPE dconst1;
  324. extern REAL_VALUE_TYPE dconst2;
  325. extern REAL_VALUE_TYPE dconstm1;
  326. extern REAL_VALUE_TYPE dconsthalf;
  327. #define dconst_e() (*dconst_e_ptr ())
  328. #define dconst_third() (*dconst_third_ptr ())
  329. #define dconst_sqrt2() (*dconst_sqrt2_ptr ())
  330. /* Function to return the real value special constant 'e'. */
  331. extern const REAL_VALUE_TYPE * dconst_e_ptr (void);
  332. /* Returns the special REAL_VALUE_TYPE corresponding to 1/3. */
  333. extern const REAL_VALUE_TYPE * dconst_third_ptr (void);
  334. /* Returns the special REAL_VALUE_TYPE corresponding to sqrt(2). */
  335. extern const REAL_VALUE_TYPE * dconst_sqrt2_ptr (void);
  336. /* Function to return a real value (not a tree node)
  337. from a given integer constant. */
  338. REAL_VALUE_TYPE real_value_from_int_cst (const_tree, const_tree);
  339. /* Given a CONST_DOUBLE in FROM, store into TO the value it represents. */
  340. #define REAL_VALUE_FROM_CONST_DOUBLE(to, from) \
  341. ((to) = *CONST_DOUBLE_REAL_VALUE (from))
  342. /* Return a CONST_DOUBLE with value R and mode M. */
  343. #define CONST_DOUBLE_FROM_REAL_VALUE(r, m) \
  344. const_double_from_real_value (r, m)
  345. extern rtx const_double_from_real_value (REAL_VALUE_TYPE, machine_mode);
  346. /* Replace R by 1/R in the given machine mode, if the result is exact. */
  347. extern bool exact_real_inverse (machine_mode, REAL_VALUE_TYPE *);
  348. /* Return true if arithmetic on values in IMODE that were promoted
  349. from values in TMODE is equivalent to direct arithmetic on values
  350. in TMODE. */
  351. bool real_can_shorten_arithmetic (machine_mode, machine_mode);
  352. /* In tree.c: wrap up a REAL_VALUE_TYPE in a tree node. */
  353. extern tree build_real (tree, REAL_VALUE_TYPE);
  354. /* Calculate R as X raised to the integer exponent N in mode MODE. */
  355. extern bool real_powi (REAL_VALUE_TYPE *, machine_mode,
  356. const REAL_VALUE_TYPE *, HOST_WIDE_INT);
  357. /* Standard round to integer value functions. */
  358. extern void real_trunc (REAL_VALUE_TYPE *, machine_mode,
  359. const REAL_VALUE_TYPE *);
  360. extern void real_floor (REAL_VALUE_TYPE *, machine_mode,
  361. const REAL_VALUE_TYPE *);
  362. extern void real_ceil (REAL_VALUE_TYPE *, machine_mode,
  363. const REAL_VALUE_TYPE *);
  364. extern void real_round (REAL_VALUE_TYPE *, machine_mode,
  365. const REAL_VALUE_TYPE *);
  366. /* Set the sign of R to the sign of X. */
  367. extern void real_copysign (REAL_VALUE_TYPE *, const REAL_VALUE_TYPE *);
  368. /* Check whether the real constant value given is an integer. */
  369. extern bool real_isinteger (const REAL_VALUE_TYPE *c, machine_mode mode);
  370. /* Write into BUF the maximum representable finite floating-point
  371. number, (1 - b**-p) * b**emax for a given FP format FMT as a hex
  372. float string. BUF must be large enough to contain the result. */
  373. extern void get_max_float (const struct real_format *, char *, size_t);
  374. #ifndef GENERATOR_FILE
  375. /* real related routines. */
  376. extern wide_int real_to_integer (const REAL_VALUE_TYPE *, bool *, int);
  377. extern void real_from_integer (REAL_VALUE_TYPE *, machine_mode,
  378. const wide_int_ref &, signop);
  379. #endif
  380. #endif /* ! GCC_REAL_H */