real.c (etoe113, toe113): Ifndef INTEL_EXTENDED_IEEE_FORMAT.
[gcc.git] / gcc / real.h
1 /* Definitions of floating-point access for GNU compiler.
2 Copyright (C) 1989, 1991, 1994, 1996, 1997, 1998,
3 1999, 2000 Free Software Foundation, Inc.
4
5 This file is part of GCC.
6
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 2, or (at your option) any later
10 version.
11
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING. If not, write to the Free
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
20 02111-1307, USA. */
21
22 #ifndef GCC_REAL_H
23 #define GCC_REAL_H
24
25 /* Define codes for all the float formats that we know of. */
26 #define UNKNOWN_FLOAT_FORMAT 0
27 #define IEEE_FLOAT_FORMAT 1
28 #define VAX_FLOAT_FORMAT 2
29 #define IBM_FLOAT_FORMAT 3
30 #define C4X_FLOAT_FORMAT 4
31
32 /* Default to IEEE float if not specified. Nearly all machines use it. */
33
34 #ifndef TARGET_FLOAT_FORMAT
35 #define TARGET_FLOAT_FORMAT IEEE_FLOAT_FORMAT
36 #endif
37
38 #ifndef HOST_FLOAT_FORMAT
39 #define HOST_FLOAT_FORMAT IEEE_FLOAT_FORMAT
40 #endif
41
42 #ifndef INTEL_EXTENDED_IEEE_FORMAT
43 #define INTEL_EXTENDED_IEEE_FORMAT 0
44 #endif
45
46 #if TARGET_FLOAT_FORMAT == IEEE_FLOAT_FORMAT
47 #define REAL_INFINITY
48 #endif
49
50 /* If FLOAT_WORDS_BIG_ENDIAN and HOST_FLOAT_WORDS_BIG_ENDIAN are not defined
51 in the header files, then this implies the word-endianness is the same as
52 for integers. */
53
54 /* This is defined 0 or 1, like WORDS_BIG_ENDIAN. */
55 #ifndef FLOAT_WORDS_BIG_ENDIAN
56 #define FLOAT_WORDS_BIG_ENDIAN WORDS_BIG_ENDIAN
57 #endif
58
59 /* This is defined 0 or 1, unlike HOST_WORDS_BIG_ENDIAN. */
60 #ifndef HOST_FLOAT_WORDS_BIG_ENDIAN
61 #ifdef HOST_WORDS_BIG_ENDIAN
62 #define HOST_FLOAT_WORDS_BIG_ENDIAN 1
63 #else
64 #define HOST_FLOAT_WORDS_BIG_ENDIAN 0
65 #endif
66 #endif
67
68 /* Defining REAL_ARITHMETIC invokes a floating point emulator
69 that can produce a target machine format differing by more
70 than just endian-ness from the host's format. The emulator
71 is also used to support extended real XFmode. */
72 #ifndef LONG_DOUBLE_TYPE_SIZE
73 #define LONG_DOUBLE_TYPE_SIZE 64
74 #endif
75 /* MAX_LONG_DOUBLE_TYPE_SIZE is a constant tested by #if.
76 LONG_DOUBLE_TYPE_SIZE can vary at compiler run time.
77 So long as macros like REAL_VALUE_TO_TARGET_LONG_DOUBLE cannot
78 vary too, however, then XFmode and TFmode long double
79 cannot both be supported at the same time. */
80 #ifndef MAX_LONG_DOUBLE_TYPE_SIZE
81 #define MAX_LONG_DOUBLE_TYPE_SIZE LONG_DOUBLE_TYPE_SIZE
82 #endif
83 #if (MAX_LONG_DOUBLE_TYPE_SIZE == 96) || (MAX_LONG_DOUBLE_TYPE_SIZE == 128)
84 #ifndef REAL_ARITHMETIC
85 #define REAL_ARITHMETIC
86 #endif
87 #endif
88 #ifdef REAL_ARITHMETIC
89 /* **** Start of software floating point emulator interface macros **** */
90
91 /* Support 80-bit extended real XFmode if LONG_DOUBLE_TYPE_SIZE
92 has been defined to be 96 in the tm.h machine file. */
93 #if (MAX_LONG_DOUBLE_TYPE_SIZE == 96)
94 #define REAL_IS_NOT_DOUBLE
95 #define REAL_ARITHMETIC
96 typedef struct {
97 HOST_WIDE_INT r[(11 + sizeof (HOST_WIDE_INT))/(sizeof (HOST_WIDE_INT))];
98 } realvaluetype;
99 #define REAL_VALUE_TYPE realvaluetype
100
101 #else /* no XFmode support */
102
103 #if (MAX_LONG_DOUBLE_TYPE_SIZE == 128)
104
105 #define REAL_IS_NOT_DOUBLE
106 #define REAL_ARITHMETIC
107 typedef struct {
108 HOST_WIDE_INT r[(19 + sizeof (HOST_WIDE_INT))/(sizeof (HOST_WIDE_INT))];
109 } realvaluetype;
110 #define REAL_VALUE_TYPE realvaluetype
111
112 #else /* not TFmode */
113
114 #if HOST_FLOAT_FORMAT != TARGET_FLOAT_FORMAT
115 /* If no XFmode support, then a REAL_VALUE_TYPE is 64 bits wide
116 but it is not necessarily a host machine double. */
117 #define REAL_IS_NOT_DOUBLE
118 typedef struct {
119 HOST_WIDE_INT r[(7 + sizeof (HOST_WIDE_INT))/(sizeof (HOST_WIDE_INT))];
120 } realvaluetype;
121 #define REAL_VALUE_TYPE realvaluetype
122 #else
123 /* If host and target formats are compatible, then a REAL_VALUE_TYPE
124 is actually a host machine double. */
125 #define REAL_VALUE_TYPE double
126 #endif
127
128 #endif /* no TFmode support */
129 #endif /* no XFmode support */
130
131 extern unsigned int significand_size PARAMS ((enum machine_mode));
132
133 /* If emulation has been enabled by defining REAL_ARITHMETIC or by
134 setting LONG_DOUBLE_TYPE_SIZE to 96 or 128, then define macros so that
135 they invoke emulator functions. This will succeed only if the machine
136 files have been updated to use these macros in place of any
137 references to host machine `double' or `float' types. */
138 #ifdef REAL_ARITHMETIC
139 #undef REAL_ARITHMETIC
140 #define REAL_ARITHMETIC(value, code, d1, d2) \
141 earith (&(value), (code), &(d1), &(d2))
142
143 /* Declare functions in real.c. */
144 extern void earith PARAMS ((REAL_VALUE_TYPE *, int,
145 REAL_VALUE_TYPE *, REAL_VALUE_TYPE *));
146 extern REAL_VALUE_TYPE etrunci PARAMS ((REAL_VALUE_TYPE));
147 extern REAL_VALUE_TYPE etruncui PARAMS ((REAL_VALUE_TYPE));
148 extern REAL_VALUE_TYPE ereal_negate PARAMS ((REAL_VALUE_TYPE));
149 extern HOST_WIDE_INT efixi PARAMS ((REAL_VALUE_TYPE));
150 extern unsigned HOST_WIDE_INT efixui PARAMS ((REAL_VALUE_TYPE));
151 extern void ereal_from_int PARAMS ((REAL_VALUE_TYPE *,
152 HOST_WIDE_INT, HOST_WIDE_INT,
153 enum machine_mode));
154 extern void ereal_from_uint PARAMS ((REAL_VALUE_TYPE *,
155 unsigned HOST_WIDE_INT,
156 unsigned HOST_WIDE_INT,
157 enum machine_mode));
158 extern void ereal_to_int PARAMS ((HOST_WIDE_INT *, HOST_WIDE_INT *,
159 REAL_VALUE_TYPE));
160 extern REAL_VALUE_TYPE ereal_ldexp PARAMS ((REAL_VALUE_TYPE, int));
161
162 extern void etartdouble PARAMS ((REAL_VALUE_TYPE, long *));
163 extern void etarldouble PARAMS ((REAL_VALUE_TYPE, long *));
164 extern void etardouble PARAMS ((REAL_VALUE_TYPE, long *));
165 extern long etarsingle PARAMS ((REAL_VALUE_TYPE));
166 extern void ereal_to_decimal PARAMS ((REAL_VALUE_TYPE, char *));
167 extern int ereal_cmp PARAMS ((REAL_VALUE_TYPE, REAL_VALUE_TYPE));
168 extern int ereal_isneg PARAMS ((REAL_VALUE_TYPE));
169 extern REAL_VALUE_TYPE ereal_unto_float PARAMS ((long));
170 extern REAL_VALUE_TYPE ereal_unto_double PARAMS ((long *));
171 extern REAL_VALUE_TYPE ereal_from_float PARAMS ((HOST_WIDE_INT));
172 extern REAL_VALUE_TYPE ereal_from_double PARAMS ((HOST_WIDE_INT *));
173
174 #define REAL_VALUES_EQUAL(x, y) (ereal_cmp ((x), (y)) == 0)
175 /* true if x < y : */
176 #define REAL_VALUES_LESS(x, y) (ereal_cmp ((x), (y)) == -1)
177 #define REAL_VALUE_LDEXP(x, n) ereal_ldexp (x, n)
178
179 /* These return REAL_VALUE_TYPE: */
180 #define REAL_VALUE_RNDZINT(x) (etrunci (x))
181 #define REAL_VALUE_UNSIGNED_RNDZINT(x) (etruncui (x))
182 #define REAL_VALUE_TRUNCATE(mode, x) real_value_truncate (mode, x)
183
184 /* These return HOST_WIDE_INT: */
185 /* Convert a floating-point value to integer, rounding toward zero. */
186 #define REAL_VALUE_FIX(x) (efixi (x))
187 /* Convert a floating-point value to unsigned integer, rounding
188 toward zero. */
189 #define REAL_VALUE_UNSIGNED_FIX(x) (efixui (x))
190
191 /* Convert ASCII string S to floating point in mode M.
192 Decimal input uses ATOF. Hexadecimal uses HTOF. */
193 #define REAL_VALUE_ATOF(s,m) ereal_atof(s,m)
194 #define REAL_VALUE_HTOF(s,m) ereal_atof(s,m)
195
196 #define REAL_VALUE_NEGATE ereal_negate
197
198 #define REAL_VALUE_MINUS_ZERO(x) \
199 ((ereal_cmp (x, dconst0) == 0) && (ereal_isneg (x) != 0 ))
200
201 #define REAL_VALUE_TO_INT ereal_to_int
202
203 /* Here the cast to HOST_WIDE_INT sign-extends arguments such as ~0. */
204 #define REAL_VALUE_FROM_INT(d, lo, hi, mode) \
205 ereal_from_int (&d, (HOST_WIDE_INT) (lo), (HOST_WIDE_INT) (hi), mode)
206
207 #define REAL_VALUE_FROM_UNSIGNED_INT(d, lo, hi, mode) \
208 ereal_from_uint (&d, lo, hi, mode)
209
210 /* IN is a REAL_VALUE_TYPE. OUT is an array of longs. */
211 #define REAL_VALUE_TO_TARGET_LONG_DOUBLE(IN, OUT) \
212 (LONG_DOUBLE_TYPE_SIZE == 64 ? etardouble ((IN), (OUT)) \
213 : LONG_DOUBLE_TYPE_SIZE == 96 ? etarldouble ((IN), (OUT)) \
214 : LONG_DOUBLE_TYPE_SIZE == 128 ? etartdouble ((IN), (OUT)) \
215 : abort())
216 #define REAL_VALUE_TO_TARGET_DOUBLE(IN, OUT) (etardouble ((IN), (OUT)))
217
218 /* IN is a REAL_VALUE_TYPE. OUT is a long. */
219 #define REAL_VALUE_TO_TARGET_SINGLE(IN, OUT) ((OUT) = etarsingle ((IN)))
220
221 /* Inverse of REAL_VALUE_TO_TARGET_DOUBLE. */
222 #define REAL_VALUE_UNTO_TARGET_DOUBLE(d) (ereal_unto_double (d))
223
224 /* Inverse of REAL_VALUE_TO_TARGET_SINGLE. */
225 #define REAL_VALUE_UNTO_TARGET_SINGLE(f) (ereal_unto_float (f))
226
227 /* d is an array of HOST_WIDE_INT that holds a double precision
228 value in the target computer's floating point format. */
229 #define REAL_VALUE_FROM_TARGET_DOUBLE(d) (ereal_from_double (d))
230
231 /* f is a HOST_WIDE_INT containing a single precision target float value. */
232 #define REAL_VALUE_FROM_TARGET_SINGLE(f) (ereal_from_float (f))
233
234 /* Conversions to decimal ASCII string. */
235 #define REAL_VALUE_TO_DECIMAL(r, fmt, s) (ereal_to_decimal (r, s))
236
237 #endif /* REAL_ARITHMETIC defined */
238
239 /* **** End of software floating point emulator interface macros **** */
240 #else /* No XFmode or TFmode and REAL_ARITHMETIC not defined */
241
242 /* old interface */
243 #ifdef REAL_ARITHMETIC
244 /* Defining REAL_IS_NOT_DOUBLE breaks certain initializations
245 when REAL_ARITHMETIC etc. are not defined. */
246
247 /* Now see if the host and target machines use the same format.
248 If not, define REAL_IS_NOT_DOUBLE (even if we end up representing
249 reals as doubles because we have no better way in this cross compiler.)
250 This turns off various optimizations that can happen when we know the
251 compiler's float format matches the target's float format.
252 */
253 #if HOST_FLOAT_FORMAT != TARGET_FLOAT_FORMAT
254 #define REAL_IS_NOT_DOUBLE
255 #ifndef REAL_VALUE_TYPE
256 typedef struct {
257 HOST_WIDE_INT r[sizeof (double)/sizeof (HOST_WIDE_INT)];
258 } realvaluetype;
259 #define REAL_VALUE_TYPE realvaluetype
260 #endif /* no REAL_VALUE_TYPE */
261 #endif /* formats differ */
262 #endif /* 0 */
263
264 #endif /* emulator not used */
265
266 /* If we are not cross-compiling, use a `double' to represent the
267 floating-point value. Otherwise, use some other type
268 (probably a struct containing an array of longs). */
269 #ifndef REAL_VALUE_TYPE
270 #define REAL_VALUE_TYPE double
271 #else
272 #define REAL_IS_NOT_DOUBLE
273 #endif
274
275 #if HOST_FLOAT_FORMAT == TARGET_FLOAT_FORMAT
276
277 /* Convert a type `double' value in host format first to a type `float'
278 value in host format and then to a single type `long' value which
279 is the bitwise equivalent of the `float' value. */
280 #ifndef REAL_VALUE_TO_TARGET_SINGLE
281 #define REAL_VALUE_TO_TARGET_SINGLE(IN, OUT) \
282 do { \
283 union { \
284 float f; \
285 HOST_WIDE_INT l; \
286 } u; \
287 if (sizeof(HOST_WIDE_INT) < sizeof(float)) \
288 abort(); \
289 u.l = 0; \
290 u.f = (IN); \
291 (OUT) = u.l; \
292 } while (0)
293 #endif
294
295 /* Convert a type `double' value in host format to a pair of type `long'
296 values which is its bitwise equivalent, but put the two words into
297 proper word order for the target. */
298 #ifndef REAL_VALUE_TO_TARGET_DOUBLE
299 #define REAL_VALUE_TO_TARGET_DOUBLE(IN, OUT) \
300 do { \
301 union { \
302 REAL_VALUE_TYPE f; \
303 HOST_WIDE_INT l[2]; \
304 } u; \
305 if (sizeof(HOST_WIDE_INT) * 2 < sizeof(REAL_VALUE_TYPE)) \
306 abort(); \
307 u.l[0] = u.l[1] = 0; \
308 u.f = (IN); \
309 if (HOST_FLOAT_WORDS_BIG_ENDIAN == FLOAT_WORDS_BIG_ENDIAN) \
310 (OUT)[0] = u.l[0], (OUT)[1] = u.l[1]; \
311 else \
312 (OUT)[1] = u.l[0], (OUT)[0] = u.l[1]; \
313 } while (0)
314 #endif
315 #endif /* HOST_FLOAT_FORMAT == TARGET_FLOAT_FORMAT */
316
317 /* In this configuration, double and long double are the same. */
318 #ifndef REAL_VALUE_TO_TARGET_LONG_DOUBLE
319 #define REAL_VALUE_TO_TARGET_LONG_DOUBLE(a, b) REAL_VALUE_TO_TARGET_DOUBLE (a, b)
320 #endif
321
322 /* Compare two floating-point objects for bitwise identity.
323 This is not the same as comparing for equality on IEEE hosts:
324 -0.0 equals 0.0 but they are not identical, and conversely
325 two NaNs might be identical but they cannot be equal. */
326 #define REAL_VALUES_IDENTICAL(x, y) \
327 (!memcmp ((char *) &(x), (char *) &(y), sizeof (REAL_VALUE_TYPE)))
328
329 /* Compare two floating-point values for equality. */
330 #ifndef REAL_VALUES_EQUAL
331 #define REAL_VALUES_EQUAL(x, y) ((x) == (y))
332 #endif
333
334 /* Compare two floating-point values for less than. */
335 #ifndef REAL_VALUES_LESS
336 #define REAL_VALUES_LESS(x, y) ((x) < (y))
337 #endif
338
339 /* Truncate toward zero to an integer floating-point value. */
340 #ifndef REAL_VALUE_RNDZINT
341 #define REAL_VALUE_RNDZINT(x) ((double) ((int) (x)))
342 #endif
343
344 /* Truncate toward zero to an unsigned integer floating-point value. */
345 #ifndef REAL_VALUE_UNSIGNED_RNDZINT
346 #define REAL_VALUE_UNSIGNED_RNDZINT(x) ((double) ((unsigned int) (x)))
347 #endif
348
349 /* Convert a floating-point value to integer, rounding toward zero. */
350 #ifndef REAL_VALUE_FIX
351 #define REAL_VALUE_FIX(x) ((int) (x))
352 #endif
353
354 /* Convert a floating-point value to unsigned integer, rounding
355 toward zero. */
356 #ifndef REAL_VALUE_UNSIGNED_FIX
357 #define REAL_VALUE_UNSIGNED_FIX(x) ((unsigned int) (x))
358 #endif
359
360 /* Scale X by Y powers of 2. */
361 #ifndef REAL_VALUE_LDEXP
362 #define REAL_VALUE_LDEXP(x, y) ldexp (x, y)
363 extern double ldexp PARAMS ((double, int));
364 #endif
365
366 /* Convert the string X to a floating-point value. */
367 #ifndef REAL_VALUE_ATOF
368 #if 1
369 /* Use real.c to convert decimal numbers to binary, ... */
370 #define REAL_VALUE_ATOF(x, s) ereal_atof (x, s)
371 /* Could use ereal_atof here for hexadecimal floats too, but real_hex_to_f
372 is OK and it uses faster native fp arithmetic. */
373 /* #define REAL_VALUE_HTOF(x, s) ereal_atof (x, s) */
374 #else
375 /* ... or, if you like the host computer's atof, go ahead and use it: */
376 #define REAL_VALUE_ATOF(x, s) atof (x)
377 #if defined (MIPSEL) || defined (MIPSEB)
378 /* MIPS compiler can't handle parens around the function name.
379 This problem *does not* appear to be connected with any
380 macro definition for atof. It does not seem there is one. */
381 extern double atof ();
382 #else
383 extern double (atof) ();
384 #endif
385 #endif
386 #endif
387
388 /* Hexadecimal floating constant input for use with host computer's
389 fp arithmetic. */
390 #ifndef REAL_VALUE_HTOF
391 extern REAL_VALUE_TYPE real_hex_to_f PARAMS ((const char *,
392 enum machine_mode));
393 #define REAL_VALUE_HTOF(s,m) real_hex_to_f(s,m)
394 #endif
395
396 /* Negate the floating-point value X. */
397 #ifndef REAL_VALUE_NEGATE
398 #define REAL_VALUE_NEGATE(x) (- (x))
399 #endif
400
401 /* Truncate the floating-point value X to mode MODE. This is correct only
402 for the most common case where the host and target have objects of the same
403 size and where `float' is SFmode. */
404
405 /* Don't use REAL_VALUE_TRUNCATE directly--always call real_value_truncate. */
406 extern REAL_VALUE_TYPE real_value_truncate PARAMS ((enum machine_mode,
407 REAL_VALUE_TYPE));
408
409 #ifndef REAL_VALUE_TRUNCATE
410 #define REAL_VALUE_TRUNCATE(mode, x) \
411 (GET_MODE_BITSIZE (mode) == sizeof (float) * HOST_BITS_PER_CHAR \
412 ? (float) (x) : (x))
413 #endif
414
415 /* Determine whether a floating-point value X is infinite. */
416 #ifndef REAL_VALUE_ISINF
417 #define REAL_VALUE_ISINF(x) (target_isinf (x))
418 #endif
419
420 /* Determine whether a floating-point value X is a NaN. */
421 #ifndef REAL_VALUE_ISNAN
422 #define REAL_VALUE_ISNAN(x) (target_isnan (x))
423 #endif
424
425 /* Determine whether a floating-point value X is negative. */
426 #ifndef REAL_VALUE_NEGATIVE
427 #define REAL_VALUE_NEGATIVE(x) (target_negative (x))
428 #endif
429
430 /* Determine whether a floating-point value X is minus 0. */
431 #ifndef REAL_VALUE_MINUS_ZERO
432 #define REAL_VALUE_MINUS_ZERO(x) ((x) == 0 && REAL_VALUE_NEGATIVE (x))
433 #endif
434 \f
435 /* Constant real values 0, 1, 2, and -1. */
436
437 extern REAL_VALUE_TYPE dconst0;
438 extern REAL_VALUE_TYPE dconst1;
439 extern REAL_VALUE_TYPE dconst2;
440 extern REAL_VALUE_TYPE dconstm1;
441
442 /* Union type used for extracting real values from CONST_DOUBLEs
443 or putting them in. */
444
445 union real_extract
446 {
447 REAL_VALUE_TYPE d;
448 HOST_WIDE_INT i[sizeof (REAL_VALUE_TYPE) / sizeof (HOST_WIDE_INT)];
449 };
450
451 /* Given a CONST_DOUBLE in FROM, store into TO the value it represents. */
452 /* Function to return a real value (not a tree node)
453 from a given integer constant. */
454 union tree_node;
455 REAL_VALUE_TYPE real_value_from_int_cst PARAMS ((union tree_node *,
456 union tree_node *));
457
458 #define REAL_VALUE_FROM_CONST_DOUBLE(to, from) \
459 do { union real_extract u; \
460 memcpy (&u, &CONST_DOUBLE_LOW ((from)), sizeof u); \
461 to = u.d; } while (0)
462
463 /* Return a CONST_DOUBLE with value R and mode M. */
464
465 #define CONST_DOUBLE_FROM_REAL_VALUE(r, m) immed_real_const_1 (r, m)
466 extern struct rtx_def *immed_real_const_1 PARAMS ((REAL_VALUE_TYPE,
467 enum machine_mode));
468
469
470 /* Convert a floating point value `r', that can be interpreted
471 as a host machine float or double, to a decimal ASCII string `s'
472 using printf format string `fmt'. */
473 #ifndef REAL_VALUE_TO_DECIMAL
474 #define REAL_VALUE_TO_DECIMAL(r, fmt, s) (sprintf (s, fmt, r))
475 #endif
476
477 /* Replace R by 1/R in the given machine mode, if the result is exact. */
478 extern int exact_real_inverse PARAMS ((enum machine_mode, REAL_VALUE_TYPE *));
479 extern int target_isnan PARAMS ((REAL_VALUE_TYPE));
480 extern int target_isinf PARAMS ((REAL_VALUE_TYPE));
481 extern int target_negative PARAMS ((REAL_VALUE_TYPE));
482 extern void debug_real PARAMS ((REAL_VALUE_TYPE));
483 extern REAL_VALUE_TYPE ereal_atof PARAMS ((const char *, enum machine_mode));
484
485 #endif /* ! GCC_REAL_H */