Line data Source code
1 : /* real.cc - software floating point emulation.
2 : Copyright (C) 1993-2026 Free Software Foundation, Inc.
3 : Contributed by Stephen L. Moshier (moshier@world.std.com).
4 : Re-written by Richard Henderson <rth@redhat.com>
5 :
6 : This file is part of GCC.
7 :
8 : GCC is free software; you can redistribute it and/or modify it under
9 : the terms of the GNU General Public License as published by the Free
10 : Software Foundation; either version 3, or (at your option) any later
11 : version.
12 :
13 : GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 : WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 : FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 : for more details.
17 :
18 : You should have received a copy of the GNU General Public License
19 : along with GCC; see the file COPYING3. If not see
20 : <http://www.gnu.org/licenses/>. */
21 :
22 : #include "config.h"
23 : #include "system.h"
24 : #include "coretypes.h"
25 : #include "bitmap.h"
26 : #include "function.h"
27 : #include "tm.h"
28 : #include "rtl.h"
29 : #include "tree.h"
30 : #include "value-range.h"
31 : #include "vr-values.h"
32 : #include "realmpfr.h"
33 : #include "dfp.h"
34 :
35 : /* The floating point model used internally is not exactly IEEE 754
36 : compliant, and close to the description in the ISO C99 standard,
37 : section 5.2.4.2.2 Characteristics of floating types.
38 :
39 : Specifically
40 :
41 : x = s * b^e * \sum_{k=1}^p f_k * b^{-k}
42 :
43 : where
44 : s = sign (+- 1)
45 : b = base or radix, here always 2
46 : e = exponent
47 : p = precision (the number of base-b digits in the significand)
48 : f_k = the digits of the significand.
49 :
50 : We differ from typical IEEE 754 encodings in that the entire
51 : significand is fractional. Normalized significands are in the
52 : range [0.5, 1.0).
53 :
54 : A requirement of the model is that P be larger than the largest
55 : supported target floating-point type by at least 2 bits. This gives
56 : us proper rounding when we truncate to the target type. In addition,
57 : E must be large enough to hold the smallest supported denormal number
58 : in a normalized form.
59 :
60 : Both of these requirements are easily satisfied. The largest target
61 : significand is 113 bits; we store at least 160. The smallest
62 : denormal number fits in 17 exponent bits; we store 26. */
63 :
64 :
65 : /* Used to classify two numbers simultaneously. */
66 : #define CLASS2(A, B) ((A) << 2 | (B))
67 :
68 : #if HOST_BITS_PER_LONG != 64 && HOST_BITS_PER_LONG != 32
69 : #error "Some constant folding done by hand to avoid shift count warnings"
70 : #endif
71 :
72 : static void get_zero (REAL_VALUE_TYPE *, int);
73 : static void get_canonical_qnan (REAL_VALUE_TYPE *, int);
74 : static void get_canonical_snan (REAL_VALUE_TYPE *, int);
75 : static void get_inf (REAL_VALUE_TYPE *, int);
76 : static bool sticky_rshift_significand (REAL_VALUE_TYPE *,
77 : const REAL_VALUE_TYPE *, unsigned int);
78 : static void rshift_significand (REAL_VALUE_TYPE *, const REAL_VALUE_TYPE *,
79 : unsigned int);
80 : static void lshift_significand (REAL_VALUE_TYPE *, const REAL_VALUE_TYPE *,
81 : unsigned int);
82 : static void lshift_significand_1 (REAL_VALUE_TYPE *, const REAL_VALUE_TYPE *);
83 : static bool add_significands (REAL_VALUE_TYPE *r, const REAL_VALUE_TYPE *,
84 : const REAL_VALUE_TYPE *);
85 : static bool sub_significands (REAL_VALUE_TYPE *, const REAL_VALUE_TYPE *,
86 : const REAL_VALUE_TYPE *, int);
87 : static void neg_significand (REAL_VALUE_TYPE *, const REAL_VALUE_TYPE *);
88 : static int cmp_significands (const REAL_VALUE_TYPE *, const REAL_VALUE_TYPE *);
89 : static int cmp_significand_0 (const REAL_VALUE_TYPE *);
90 : static void set_significand_bit (REAL_VALUE_TYPE *, unsigned int);
91 : static void clear_significand_bit (REAL_VALUE_TYPE *, unsigned int);
92 : static bool test_significand_bit (REAL_VALUE_TYPE *, unsigned int);
93 : static void clear_significand_below (REAL_VALUE_TYPE *, unsigned int);
94 : static bool div_significands (REAL_VALUE_TYPE *, const REAL_VALUE_TYPE *,
95 : const REAL_VALUE_TYPE *);
96 : static void normalize (REAL_VALUE_TYPE *);
97 :
98 : static bool do_add (REAL_VALUE_TYPE *, const REAL_VALUE_TYPE *,
99 : const REAL_VALUE_TYPE *, int);
100 : static bool do_multiply (REAL_VALUE_TYPE *, const REAL_VALUE_TYPE *,
101 : const REAL_VALUE_TYPE *);
102 : static bool do_divide (REAL_VALUE_TYPE *, const REAL_VALUE_TYPE *,
103 : const REAL_VALUE_TYPE *);
104 : static int do_compare (const REAL_VALUE_TYPE *, const REAL_VALUE_TYPE *, int);
105 : static void do_fix_trunc (REAL_VALUE_TYPE *, const REAL_VALUE_TYPE *);
106 :
107 : static unsigned long rtd_divmod (REAL_VALUE_TYPE *, REAL_VALUE_TYPE *);
108 : static void decimal_from_integer (REAL_VALUE_TYPE *, int);
109 : static void decimal_integer_string (char *, const REAL_VALUE_TYPE *,
110 : size_t);
111 :
112 : static const REAL_VALUE_TYPE * ten_to_ptwo (int);
113 : static const REAL_VALUE_TYPE * ten_to_mptwo (int);
114 : static const REAL_VALUE_TYPE * real_digit (int);
115 : static void times_pten (REAL_VALUE_TYPE *, int);
116 :
117 : static void round_for_format (const struct real_format *, REAL_VALUE_TYPE *);
118 :
119 : /* Determine whether a floating-point value X is a denormal. R is
120 : expected to be in denormal form, so this function is only
121 : meaningful after a call to round_for_format. */
122 :
123 : static inline bool
124 2098281 : real_isdenormal (const REAL_VALUE_TYPE *r)
125 : {
126 2098281 : return r->cl == rvc_normal && (r->sig[SIGSZ-1] & SIG_MSB) == 0;
127 : }
128 :
129 : /* Initialize R with a positive zero. */
130 :
131 : static inline void
132 159775811 : get_zero (REAL_VALUE_TYPE *r, int sign)
133 : {
134 159775811 : memset (r, 0, sizeof (*r));
135 159775811 : r->sign = sign;
136 5651410 : }
137 :
138 : /* Initialize R with the canonical quiet NaN. */
139 :
140 : static inline void
141 268355 : get_canonical_qnan (REAL_VALUE_TYPE *r, int sign)
142 : {
143 268355 : memset (r, 0, sizeof (*r));
144 268355 : r->cl = rvc_nan;
145 268355 : r->sign = sign;
146 268355 : r->canonical = 1;
147 265642 : }
148 :
149 : static inline void
150 193122 : get_canonical_snan (REAL_VALUE_TYPE *r, int sign)
151 : {
152 193122 : memset (r, 0, sizeof (*r));
153 193122 : r->cl = rvc_nan;
154 193122 : r->sign = sign;
155 193122 : r->signalling = 1;
156 193122 : r->canonical = 1;
157 193106 : }
158 :
159 : static inline void
160 6582902 : get_inf (REAL_VALUE_TYPE *r, int sign)
161 : {
162 6582902 : memset (r, 0, sizeof (*r));
163 6582902 : r->cl = rvc_inf;
164 6582902 : r->sign = sign;
165 3477415 : }
166 :
167 :
168 : /* Right-shift the significand of A by N bits; put the result in the
169 : significand of R. If any one bits are shifted out, return true. */
170 :
171 : static bool
172 125075286 : sticky_rshift_significand (REAL_VALUE_TYPE *r, const REAL_VALUE_TYPE *a,
173 : unsigned int n)
174 : {
175 125075286 : unsigned long sticky = 0;
176 125075286 : unsigned int i, ofs = 0;
177 :
178 125075286 : if (n >= HOST_BITS_PER_LONG)
179 : {
180 227243 : for (i = 0, ofs = n / HOST_BITS_PER_LONG; i < ofs; ++i)
181 117540 : sticky |= a->sig[i];
182 109703 : n &= HOST_BITS_PER_LONG - 1;
183 : }
184 :
185 125075286 : if (n != 0)
186 : {
187 125065537 : sticky |= a->sig[ofs] & (((unsigned long)1 << n) - 1);
188 500262148 : for (i = 0; i < SIGSZ; ++i)
189 : {
190 375196611 : r->sig[i]
191 375196611 : = (((ofs + i >= SIGSZ ? 0 : a->sig[ofs + i]) >> n)
192 375196611 : | ((ofs + i + 1 >= SIGSZ ? 0 : a->sig[ofs + i + 1])
193 375196611 : << (HOST_BITS_PER_LONG - n)));
194 : }
195 : }
196 : else
197 : {
198 28703 : for (i = 0; ofs + i < SIGSZ; ++i)
199 18954 : r->sig[i] = a->sig[ofs + i];
200 20042 : for (; i < SIGSZ; ++i)
201 10293 : r->sig[i] = 0;
202 : }
203 :
204 125075286 : return sticky != 0;
205 : }
206 :
207 : /* Right-shift the significand of A by N bits; put the result in the
208 : significand of R. */
209 :
210 : static void
211 206322 : rshift_significand (REAL_VALUE_TYPE *r, const REAL_VALUE_TYPE *a,
212 : unsigned int n)
213 : {
214 206322 : unsigned int i, ofs = n / HOST_BITS_PER_LONG;
215 :
216 206322 : n &= HOST_BITS_PER_LONG - 1;
217 206322 : if (n != 0)
218 : {
219 825288 : for (i = 0; i < SIGSZ; ++i)
220 : {
221 618966 : r->sig[i]
222 618966 : = (((ofs + i >= SIGSZ ? 0 : a->sig[ofs + i]) >> n)
223 618966 : | ((ofs + i + 1 >= SIGSZ ? 0 : a->sig[ofs + i + 1])
224 618966 : << (HOST_BITS_PER_LONG - n)));
225 : }
226 : }
227 : else
228 : {
229 0 : for (i = 0; ofs + i < SIGSZ; ++i)
230 0 : r->sig[i] = a->sig[ofs + i];
231 0 : for (; i < SIGSZ; ++i)
232 0 : r->sig[i] = 0;
233 : }
234 206322 : }
235 :
236 : /* Left-shift the significand of A by N bits; put the result in the
237 : significand of R. */
238 :
239 : static void
240 171036835 : lshift_significand (REAL_VALUE_TYPE *r, const REAL_VALUE_TYPE *a,
241 : unsigned int n)
242 : {
243 171036835 : unsigned int i, ofs = n / HOST_BITS_PER_LONG;
244 :
245 171036835 : n &= HOST_BITS_PER_LONG - 1;
246 171036835 : if (n == 0)
247 : {
248 745670 : for (i = 0; ofs + i < SIGSZ; ++i)
249 496687 : r->sig[SIGSZ-1-i] = a->sig[SIGSZ-1-i-ofs];
250 499245 : for (; i < SIGSZ; ++i)
251 250262 : r->sig[SIGSZ-1-i] = 0;
252 : }
253 : else
254 683151408 : for (i = 0; i < SIGSZ; ++i)
255 : {
256 1024727112 : r->sig[SIGSZ-1-i]
257 512363556 : = (((ofs + i >= SIGSZ ? 0 : a->sig[SIGSZ-1-i-ofs]) << n)
258 512363556 : | ((ofs + i + 1 >= SIGSZ ? 0 : a->sig[SIGSZ-1-i-ofs-1])
259 512363556 : >> (HOST_BITS_PER_LONG - n)));
260 : }
261 171036835 : }
262 :
263 : /* Likewise, but N is specialized to 1. */
264 :
265 : static inline void
266 1662695182 : lshift_significand_1 (REAL_VALUE_TYPE *r, const REAL_VALUE_TYPE *a)
267 : {
268 1662695182 : unsigned int i;
269 :
270 4988085546 : for (i = SIGSZ - 1; i > 0; --i)
271 3325390364 : r->sig[i] = (a->sig[i] << 1) | (a->sig[i-1] >> (HOST_BITS_PER_LONG - 1));
272 1662695182 : r->sig[0] = a->sig[0] << 1;
273 1662695182 : }
274 :
275 : /* Add the significands of A and B, placing the result in R. Return
276 : true if there was carry out of the most significant word. */
277 :
278 : static inline bool
279 127867589 : add_significands (REAL_VALUE_TYPE *r, const REAL_VALUE_TYPE *a,
280 : const REAL_VALUE_TYPE *b)
281 : {
282 127867589 : bool carry = false;
283 127867589 : int i;
284 :
285 511470356 : for (i = 0; i < SIGSZ; ++i)
286 : {
287 383602767 : unsigned long ai = a->sig[i];
288 383602767 : unsigned long ri = ai + b->sig[i];
289 :
290 383602767 : if (carry)
291 : {
292 9478022 : carry = ri < ai;
293 9478022 : carry |= ++ri == 0;
294 : }
295 : else
296 374124745 : carry = ri < ai;
297 :
298 383602767 : r->sig[i] = ri;
299 : }
300 :
301 127867589 : return carry;
302 : }
303 :
304 : /* Subtract the significands of A and B, placing the result in R. CARRY is
305 : true if there's a borrow incoming to the least significant word.
306 : Return true if there was borrow out of the most significant word. */
307 :
308 : static inline bool
309 565316477 : sub_significands (REAL_VALUE_TYPE *r, const REAL_VALUE_TYPE *a,
310 : const REAL_VALUE_TYPE *b, int carry)
311 : {
312 565316477 : int i;
313 :
314 2261265908 : for (i = 0; i < SIGSZ; ++i)
315 : {
316 1695949431 : unsigned long ai = a->sig[i];
317 1695949431 : unsigned long ri = ai - b->sig[i];
318 :
319 1695949431 : if (carry)
320 : {
321 109890398 : carry = ri > ai;
322 109890398 : carry |= ~--ri == 0;
323 : }
324 : else
325 1586059033 : carry = ri > ai;
326 :
327 1695949431 : r->sig[i] = ri;
328 : }
329 :
330 565316477 : return carry;
331 : }
332 :
333 : /* Negate the significand A, placing the result in R. */
334 :
335 : static inline void
336 14545 : neg_significand (REAL_VALUE_TYPE *r, const REAL_VALUE_TYPE *a)
337 : {
338 14545 : bool carry = true;
339 14545 : int i;
340 :
341 58180 : for (i = 0; i < SIGSZ; ++i)
342 : {
343 43635 : unsigned long ri, ai = a->sig[i];
344 :
345 43635 : if (carry)
346 : {
347 42562 : if (ai)
348 : {
349 14545 : ri = -ai;
350 14545 : carry = false;
351 : }
352 : else
353 : ri = ai;
354 : }
355 : else
356 1073 : ri = ~ai;
357 :
358 43635 : r->sig[i] = ri;
359 : }
360 14545 : }
361 :
362 : /* Compare significands. Return tri-state vs zero. */
363 :
364 : static inline int
365 1406856 : cmp_significands (const REAL_VALUE_TYPE *a, const REAL_VALUE_TYPE *b)
366 : {
367 1406856 : int i;
368 :
369 1452004763 : for (i = SIGSZ - 1; i >= 0; --i)
370 : {
371 1427491803 : unsigned long ai = a->sig[i];
372 1427491803 : unsigned long bi = b->sig[i];
373 :
374 1427491803 : if (ai > bi)
375 : return 1;
376 1204715324 : if (ai < bi)
377 : return -1;
378 : }
379 :
380 : return 0;
381 : }
382 :
383 : /* Return true if A is nonzero. */
384 :
385 : static inline int
386 31741272 : cmp_significand_0 (const REAL_VALUE_TYPE *a)
387 : {
388 31741272 : int i;
389 :
390 32543561 : for (i = SIGSZ - 1; i >= 0; --i)
391 32433761 : if (a->sig[i])
392 : return 1;
393 :
394 : return 0;
395 : }
396 :
397 : /* Set bit N of the significand of R. */
398 :
399 : static inline void
400 542535981 : set_significand_bit (REAL_VALUE_TYPE *r, unsigned int n)
401 : {
402 542535981 : r->sig[n / HOST_BITS_PER_LONG]
403 542535981 : |= (unsigned long)1 << (n % HOST_BITS_PER_LONG);
404 535542686 : }
405 :
406 : /* Clear bit N of the significand of R. */
407 :
408 : static inline void
409 510293 : clear_significand_bit (REAL_VALUE_TYPE *r, unsigned int n)
410 : {
411 510293 : r->sig[n / HOST_BITS_PER_LONG]
412 510293 : &= ~((unsigned long)1 << (n % HOST_BITS_PER_LONG));
413 0 : }
414 :
415 : /* Test bit N of the significand of R. */
416 :
417 : static inline bool
418 42899404 : test_significand_bit (REAL_VALUE_TYPE *r, unsigned int n)
419 : {
420 : /* ??? Compiler bug here if we return this expression directly.
421 : The conversion to bool strips the "&1" and we wind up testing
422 : e.g. 2 != 0 -> true. Seen in gcc version 3.2 20020520. */
423 42899404 : int t = (r->sig[n / HOST_BITS_PER_LONG] >> (n % HOST_BITS_PER_LONG)) & 1;
424 42899404 : return t;
425 : }
426 :
427 : /* Clear bits 0..N-1 of the significand of R. */
428 :
429 : static void
430 47862875 : clear_significand_below (REAL_VALUE_TYPE *r, unsigned int n)
431 : {
432 47862875 : int i, w = n / HOST_BITS_PER_LONG;
433 :
434 139434504 : for (i = 0; i < w; ++i)
435 91571629 : r->sig[i] = 0;
436 :
437 : /* We are actually passing N == SIGNIFICAND_BITS which would result
438 : in an out-of-bound access below. */
439 47862847 : if (n % HOST_BITS_PER_LONG != 0)
440 33567458 : r->sig[w] &= ~(((unsigned long)1 << (n % HOST_BITS_PER_LONG)) - 1);
441 47862847 : }
442 :
443 : /* Divide the significands of A and B, placing the result in R. Return
444 : true if the division was inexact. */
445 :
446 : static inline bool
447 8519233 : div_significands (REAL_VALUE_TYPE *r, const REAL_VALUE_TYPE *a,
448 : const REAL_VALUE_TYPE *b)
449 : {
450 8519233 : REAL_VALUE_TYPE u;
451 8519233 : int i, bit = SIGNIFICAND_BITS - 1;
452 8519233 : unsigned long msb, inexact;
453 :
454 8519233 : u = *a;
455 8519233 : memset (r->sig, 0, sizeof (r->sig));
456 :
457 8519233 : msb = 0;
458 8519233 : goto start;
459 1627173503 : do
460 : {
461 1627173503 : msb = u.sig[SIGSZ-1] & SIG_MSB;
462 1627173503 : lshift_significand_1 (&u, &u);
463 1635692736 : start:
464 2942040738 : if (msb || cmp_significands (&u, b) >= 0)
465 : {
466 535032393 : sub_significands (&u, &u, b, 0);
467 535032393 : set_significand_bit (r, bit);
468 : }
469 : }
470 1635692736 : while (--bit >= 0);
471 :
472 34076932 : for (i = 0, inexact = 0; i < SIGSZ; i++)
473 25557699 : inexact |= u.sig[i];
474 :
475 8519233 : return inexact != 0;
476 : }
477 :
478 : /* Adjust the exponent and significand of R such that the most
479 : significant bit is set. We underflow to zero and overflow to
480 : infinity here, without denormals. (The intermediate representation
481 : exponent is large enough to handle target denormals normalized.) */
482 :
483 : static void
484 554727180 : normalize (REAL_VALUE_TYPE *r)
485 : {
486 554727180 : int shift = 0, exp;
487 554727180 : int i, j;
488 :
489 554727180 : if (r->decimal)
490 : return;
491 :
492 : /* Find the first word that is nonzero. */
493 983833913 : for (i = SIGSZ - 1; i >= 0; i--)
494 841180797 : if (r->sig[i] == 0)
495 429701069 : shift += HOST_BITS_PER_LONG;
496 : else
497 : break;
498 :
499 : /* Zero significand flushes to zero. */
500 554132844 : if (i < 0)
501 : {
502 142653116 : r->cl = rvc_zero;
503 142653116 : SET_REAL_EXP (r, 0);
504 142653116 : return;
505 : }
506 :
507 : /* Find the first bit that is nonzero. */
508 1587133170 : for (j = 0; ; j++)
509 1998612898 : if (r->sig[i] & ((unsigned long)1 << (HOST_BITS_PER_LONG - 1 - j)))
510 : break;
511 411479728 : shift += j;
512 :
513 411479728 : if (shift > 0)
514 : {
515 171032708 : exp = REAL_EXP (r) - shift;
516 171032708 : if (exp > MAX_EXP)
517 : get_inf (r, r->sign);
518 171032708 : else if (exp < -MAX_EXP)
519 0 : get_zero (r, r->sign);
520 : else
521 : {
522 171032708 : SET_REAL_EXP (r, exp);
523 171032708 : lshift_significand (r, r, shift);
524 : }
525 : }
526 : }
527 :
528 : /* Calculate R = A + (SUBTRACT_P ? -B : B). Return true if the
529 : result may be inexact due to a loss of precision. */
530 :
531 : static bool
532 315524327 : do_add (REAL_VALUE_TYPE *r, const REAL_VALUE_TYPE *a,
533 : const REAL_VALUE_TYPE *b, int subtract_p)
534 : {
535 315524327 : int dexp, sign, exp;
536 315524327 : REAL_VALUE_TYPE t;
537 315524327 : bool inexact = false;
538 :
539 : /* Determine if we need to add or subtract. */
540 315524327 : sign = a->sign;
541 315524327 : subtract_p = (sign ^ b->sign) ^ subtract_p;
542 :
543 315524327 : switch (CLASS2 (a->cl, b->cl))
544 : {
545 42477022 : case CLASS2 (rvc_zero, rvc_zero):
546 : /* -0 + -0 = -0, -0 - +0 = -0; all other cases yield +0. */
547 42477022 : get_zero (r, sign & !subtract_p);
548 42477022 : return false;
549 :
550 45838881 : case CLASS2 (rvc_zero, rvc_normal):
551 45838881 : case CLASS2 (rvc_zero, rvc_inf):
552 45838881 : case CLASS2 (rvc_zero, rvc_nan):
553 : /* 0 + ANY = ANY. */
554 45838881 : case CLASS2 (rvc_normal, rvc_nan):
555 45838881 : case CLASS2 (rvc_inf, rvc_nan):
556 45838881 : case CLASS2 (rvc_nan, rvc_nan):
557 : /* ANY + NaN = NaN. */
558 45838881 : case CLASS2 (rvc_normal, rvc_inf):
559 : /* R + Inf = Inf. */
560 45838881 : *r = *b;
561 : /* Make resulting NaN value to be qNaN. The caller has the
562 : responsibility to avoid the operation if flag_signaling_nans
563 : is on. */
564 45838881 : r->signalling = 0;
565 45838881 : r->sign = sign ^ subtract_p;
566 45838881 : return false;
567 :
568 101498630 : case CLASS2 (rvc_normal, rvc_zero):
569 101498630 : case CLASS2 (rvc_inf, rvc_zero):
570 101498630 : case CLASS2 (rvc_nan, rvc_zero):
571 : /* ANY + 0 = ANY. */
572 101498630 : case CLASS2 (rvc_nan, rvc_normal):
573 101498630 : case CLASS2 (rvc_nan, rvc_inf):
574 : /* NaN + ANY = NaN. */
575 101498630 : case CLASS2 (rvc_inf, rvc_normal):
576 : /* Inf + R = Inf. */
577 101498630 : *r = *a;
578 : /* Make resulting NaN value to be qNaN. The caller has the
579 : responsibility to avoid the operation if flag_signaling_nans
580 : is on. */
581 101498630 : r->signalling = 0;
582 101498630 : return false;
583 :
584 3206094 : case CLASS2 (rvc_inf, rvc_inf):
585 3206094 : if (subtract_p)
586 : /* Inf - Inf = NaN. */
587 1027 : get_canonical_qnan (r, 0);
588 : else
589 : /* Inf + Inf = Inf. */
590 3205067 : *r = *a;
591 : return false;
592 :
593 122503700 : case CLASS2 (rvc_normal, rvc_normal):
594 122503700 : break;
595 :
596 : default:
597 : gcc_unreachable ();
598 : }
599 :
600 : /* Swap the arguments such that A has the larger exponent. */
601 122503700 : dexp = REAL_EXP (a) - REAL_EXP (b);
602 122503700 : if (dexp < 0)
603 : {
604 114216907 : const REAL_VALUE_TYPE *t;
605 114216907 : t = a, a = b, b = t;
606 114216907 : dexp = -dexp;
607 114216907 : sign ^= subtract_p;
608 : }
609 122503700 : exp = REAL_EXP (a);
610 :
611 : /* If the exponents are not identical, we need to shift the
612 : significand of B down. */
613 122503700 : if (dexp > 0)
614 : {
615 : /* If the exponents are too far apart, the significands
616 : do not overlap, which makes the subtraction a noop. */
617 118977101 : if (dexp >= SIGNIFICAND_BITS)
618 : {
619 97088 : *r = *a;
620 97088 : r->sign = sign;
621 97088 : return true;
622 : }
623 :
624 118880013 : inexact |= sticky_rshift_significand (&t, b, dexp);
625 118880013 : b = &t;
626 : }
627 :
628 122406612 : if (subtract_p)
629 : {
630 337041 : if (sub_significands (r, a, b, inexact))
631 : {
632 : /* We got a borrow out of the subtraction. That means that
633 : A and B had the same exponent, and B had the larger
634 : significand. We need to swap the sign and negate the
635 : significand. */
636 14545 : sign ^= 1;
637 14545 : neg_significand (r, r);
638 : }
639 : }
640 : else
641 : {
642 122069571 : if (add_significands (r, a, b))
643 : {
644 : /* We got carry out of the addition. This means we need to
645 : shift the significand back down one bit and increase the
646 : exponent. */
647 5721347 : inexact |= sticky_rshift_significand (r, r, 1);
648 5721347 : r->sig[SIGSZ-1] |= SIG_MSB;
649 5721347 : if (++exp > MAX_EXP)
650 : {
651 0 : get_inf (r, sign);
652 0 : return true;
653 : }
654 : }
655 : }
656 :
657 122406612 : r->cl = rvc_normal;
658 122406612 : r->sign = sign;
659 122406612 : SET_REAL_EXP (r, exp);
660 : /* Zero out the remaining fields. */
661 122406612 : r->signalling = 0;
662 122406612 : r->canonical = 0;
663 122406612 : r->decimal = 0;
664 :
665 : /* Re-normalize the result. */
666 122406612 : normalize (r);
667 :
668 : /* Special case: if the subtraction results in zero, the result
669 : is positive. */
670 122406612 : if (r->cl == rvc_zero)
671 13464 : r->sign = 0;
672 : else
673 122393148 : r->sig[0] |= inexact;
674 :
675 : return inexact;
676 : }
677 :
678 : /* Calculate R = A * B. Return true if the result may be inexact. */
679 :
680 : static bool
681 55890287 : do_multiply (REAL_VALUE_TYPE *r, const REAL_VALUE_TYPE *a,
682 : const REAL_VALUE_TYPE *b)
683 : {
684 55890287 : REAL_VALUE_TYPE u, t, *rr;
685 55890287 : unsigned int i, j, k;
686 55890287 : int sign = a->sign ^ b->sign;
687 55890287 : bool inexact = false;
688 :
689 55890287 : switch (CLASS2 (a->cl, b->cl))
690 : {
691 9021215 : case CLASS2 (rvc_zero, rvc_zero):
692 9021215 : case CLASS2 (rvc_zero, rvc_normal):
693 9021215 : case CLASS2 (rvc_normal, rvc_zero):
694 : /* +-0 * ANY = 0 with appropriate sign. */
695 9021215 : get_zero (r, sign);
696 9021215 : return false;
697 :
698 0 : case CLASS2 (rvc_zero, rvc_nan):
699 0 : case CLASS2 (rvc_normal, rvc_nan):
700 0 : case CLASS2 (rvc_inf, rvc_nan):
701 0 : case CLASS2 (rvc_nan, rvc_nan):
702 : /* ANY * NaN = NaN. */
703 0 : *r = *b;
704 : /* Make resulting NaN value to be qNaN. The caller has the
705 : responsibility to avoid the operation if flag_signaling_nans
706 : is on. */
707 0 : r->signalling = 0;
708 0 : r->sign = sign;
709 0 : return false;
710 :
711 0 : case CLASS2 (rvc_nan, rvc_zero):
712 0 : case CLASS2 (rvc_nan, rvc_normal):
713 0 : case CLASS2 (rvc_nan, rvc_inf):
714 : /* NaN * ANY = NaN. */
715 0 : *r = *a;
716 : /* Make resulting NaN value to be qNaN. The caller has the
717 : responsibility to avoid the operation if flag_signaling_nans
718 : is on. */
719 0 : r->signalling = 0;
720 0 : r->sign = sign;
721 0 : return false;
722 :
723 2202 : case CLASS2 (rvc_zero, rvc_inf):
724 2202 : case CLASS2 (rvc_inf, rvc_zero):
725 : /* 0 * Inf = NaN */
726 2202 : get_canonical_qnan (r, sign);
727 2202 : return false;
728 :
729 1798808 : case CLASS2 (rvc_inf, rvc_inf):
730 1798808 : case CLASS2 (rvc_normal, rvc_inf):
731 1798808 : case CLASS2 (rvc_inf, rvc_normal):
732 : /* Inf * Inf = Inf, R * Inf = Inf */
733 1798808 : get_inf (r, sign);
734 1798808 : return false;
735 :
736 45068062 : case CLASS2 (rvc_normal, rvc_normal):
737 45068062 : break;
738 :
739 : default:
740 : gcc_unreachable ();
741 : }
742 :
743 45068062 : if (r == a || r == b)
744 : rr = &t;
745 : else
746 23422072 : rr = r;
747 45068062 : get_zero (rr, 0);
748 :
749 : /* Collect all the partial products. Since we don't have sure access
750 : to a widening multiply, we split each long into two half-words.
751 :
752 : Consider the long-hand form of a four half-word multiplication:
753 :
754 : A B C D
755 : * E F G H
756 : --------------
757 : DE DF DG DH
758 : CE CF CG CH
759 : BE BF BG BH
760 : AE AF AG AH
761 :
762 : We construct partial products of the widened half-word products
763 : that are known to not overlap, e.g. DF+DH. Each such partial
764 : product is given its proper exponent, which allows us to sum them
765 : and obtain the finished product. */
766 :
767 315476341 : for (i = 0; i < SIGSZ * 2; ++i)
768 : {
769 270408347 : unsigned long ai = a->sig[i / 2];
770 270408347 : if (i & 1)
771 135204171 : ai >>= HOST_BITS_PER_LONG / 2;
772 : else
773 135204176 : ai &= ((unsigned long)1 << (HOST_BITS_PER_LONG / 2)) - 1;
774 :
775 270408347 : if (ai == 0)
776 116668840 : continue;
777 :
778 461218448 : for (j = 0; j < 2; ++j)
779 : {
780 307479009 : int exp = (REAL_EXP (a) - (2*SIGSZ-1-i)*(HOST_BITS_PER_LONG/2)
781 307479009 : + (REAL_EXP (b) - (1-j)*(HOST_BITS_PER_LONG/2)));
782 :
783 307479009 : if (exp > MAX_EXP)
784 : {
785 68 : get_inf (r, sign);
786 68 : return true;
787 : }
788 307478941 : if (exp < -MAX_EXP)
789 : {
790 : /* Would underflow to zero, which we shouldn't bother adding. */
791 0 : inexact = true;
792 0 : continue;
793 : }
794 :
795 307478941 : memset (&u, 0, sizeof (u));
796 307478941 : u.cl = rvc_normal;
797 307478941 : SET_REAL_EXP (&u, exp);
798 :
799 1229915764 : for (k = j; k < SIGSZ * 2; k += 2)
800 : {
801 922436823 : unsigned long bi = b->sig[k / 2];
802 922436823 : if (k & 1)
803 461218317 : bi >>= HOST_BITS_PER_LONG / 2;
804 : else
805 461218506 : bi &= ((unsigned long)1 << (HOST_BITS_PER_LONG / 2)) - 1;
806 :
807 922436823 : u.sig[k / 2] = ai * bi;
808 : }
809 :
810 307478941 : normalize (&u);
811 307478941 : inexact |= do_add (rr, rr, &u, 0);
812 : }
813 : }
814 :
815 45067994 : rr->sign = sign;
816 45067994 : if (rr != r)
817 21645922 : *r = t;
818 :
819 : return inexact;
820 : }
821 :
822 : /* Calculate R = A / B. Return true if the result may be inexact. */
823 :
824 : static bool
825 9399454 : do_divide (REAL_VALUE_TYPE *r, const REAL_VALUE_TYPE *a,
826 : const REAL_VALUE_TYPE *b)
827 : {
828 9399454 : int exp, sign = a->sign ^ b->sign;
829 9399454 : REAL_VALUE_TYPE t, *rr;
830 9399454 : bool inexact;
831 :
832 9399454 : switch (CLASS2 (a->cl, b->cl))
833 : {
834 378 : case CLASS2 (rvc_zero, rvc_zero):
835 : /* 0 / 0 = NaN. */
836 378 : case CLASS2 (rvc_inf, rvc_inf):
837 : /* Inf / Inf = NaN. */
838 378 : get_canonical_qnan (r, sign);
839 378 : return false;
840 :
841 399947 : case CLASS2 (rvc_zero, rvc_normal):
842 399947 : case CLASS2 (rvc_zero, rvc_inf):
843 : /* 0 / ANY = 0. */
844 399947 : case CLASS2 (rvc_normal, rvc_inf):
845 : /* R / Inf = 0. */
846 399947 : get_zero (r, sign);
847 399947 : return false;
848 :
849 17061 : case CLASS2 (rvc_normal, rvc_zero):
850 : /* R / 0 = Inf. */
851 17061 : case CLASS2 (rvc_inf, rvc_zero):
852 : /* Inf / 0 = Inf. */
853 17061 : get_inf (r, sign);
854 17061 : return false;
855 :
856 0 : case CLASS2 (rvc_zero, rvc_nan):
857 0 : case CLASS2 (rvc_normal, rvc_nan):
858 0 : case CLASS2 (rvc_inf, rvc_nan):
859 0 : case CLASS2 (rvc_nan, rvc_nan):
860 : /* ANY / NaN = NaN. */
861 0 : *r = *b;
862 : /* Make resulting NaN value to be qNaN. The caller has the
863 : responsibility to avoid the operation if flag_signaling_nans
864 : is on. */
865 0 : r->signalling = 0;
866 0 : r->sign = sign;
867 0 : return false;
868 :
869 0 : case CLASS2 (rvc_nan, rvc_zero):
870 0 : case CLASS2 (rvc_nan, rvc_normal):
871 0 : case CLASS2 (rvc_nan, rvc_inf):
872 : /* NaN / ANY = NaN. */
873 0 : *r = *a;
874 : /* Make resulting NaN value to be qNaN. The caller has the
875 : responsibility to avoid the operation if flag_signaling_nans
876 : is on. */
877 0 : r->signalling = 0;
878 0 : r->sign = sign;
879 0 : return false;
880 :
881 462835 : case CLASS2 (rvc_inf, rvc_normal):
882 : /* Inf / R = Inf. */
883 462835 : get_inf (r, sign);
884 462835 : return false;
885 :
886 8519233 : case CLASS2 (rvc_normal, rvc_normal):
887 8519233 : break;
888 :
889 : default:
890 : gcc_unreachable ();
891 : }
892 :
893 8519233 : if (r == a || r == b)
894 : rr = &t;
895 : else
896 7401714 : rr = r;
897 :
898 : /* Make sure all fields in the result are initialized. */
899 8519233 : get_zero (rr, 0);
900 8519233 : rr->cl = rvc_normal;
901 8519233 : rr->sign = sign;
902 :
903 8519233 : exp = REAL_EXP (a) - REAL_EXP (b) + 1;
904 8519233 : if (exp > MAX_EXP)
905 : {
906 0 : get_inf (r, sign);
907 0 : return true;
908 : }
909 8519233 : if (exp < -MAX_EXP)
910 : {
911 0 : get_zero (r, sign);
912 0 : return true;
913 : }
914 8519233 : SET_REAL_EXP (rr, exp);
915 :
916 8519233 : inexact = div_significands (rr, a, b);
917 :
918 : /* Re-normalize the result. */
919 8519233 : normalize (rr);
920 8519233 : rr->sig[0] |= inexact;
921 :
922 8519233 : if (rr != r)
923 1117519 : *r = t;
924 :
925 : return inexact;
926 : }
927 :
928 : /* Return a tri-state comparison of A vs B. Return NAN_RESULT if
929 : one of the two operands is a NaN. */
930 :
931 : static int
932 213637277 : do_compare (const REAL_VALUE_TYPE *a, const REAL_VALUE_TYPE *b,
933 : int nan_result)
934 : {
935 213637277 : int ret;
936 :
937 213637277 : switch (CLASS2 (a->cl, b->cl))
938 : {
939 : case CLASS2 (rvc_zero, rvc_zero):
940 : /* Sign of zero doesn't matter for compares. */
941 : return 0;
942 :
943 29547733 : case CLASS2 (rvc_normal, rvc_zero):
944 : /* Decimal float zero is special and uses rvc_normal, not rvc_zero. */
945 29547733 : if (a->decimal)
946 52736 : return decimal_do_compare (a, b, nan_result);
947 : /* Fall through. */
948 42658559 : case CLASS2 (rvc_inf, rvc_zero):
949 42658559 : case CLASS2 (rvc_inf, rvc_normal):
950 42658559 : return (a->sign ? -1 : 1);
951 :
952 28620132 : case CLASS2 (rvc_inf, rvc_inf):
953 28620132 : return -a->sign - -b->sign;
954 :
955 14661259 : case CLASS2 (rvc_zero, rvc_normal):
956 : /* Decimal float zero is special and uses rvc_normal, not rvc_zero. */
957 14661259 : if (b->decimal)
958 1363 : return decimal_do_compare (a, b, nan_result);
959 : /* Fall through. */
960 38332329 : case CLASS2 (rvc_zero, rvc_inf):
961 38332329 : case CLASS2 (rvc_normal, rvc_inf):
962 38332329 : return (b->sign ? 1 : -1);
963 :
964 16515 : case CLASS2 (rvc_zero, rvc_nan):
965 16515 : case CLASS2 (rvc_normal, rvc_nan):
966 16515 : case CLASS2 (rvc_inf, rvc_nan):
967 16515 : case CLASS2 (rvc_nan, rvc_nan):
968 16515 : case CLASS2 (rvc_nan, rvc_zero):
969 16515 : case CLASS2 (rvc_nan, rvc_normal):
970 16515 : case CLASS2 (rvc_nan, rvc_inf):
971 16515 : return nan_result;
972 :
973 86749408 : case CLASS2 (rvc_normal, rvc_normal):
974 86749408 : break;
975 :
976 : default:
977 : gcc_unreachable ();
978 : }
979 :
980 86749408 : if (a->decimal || b->decimal)
981 305884 : return decimal_do_compare (a, b, nan_result);
982 :
983 86443524 : if (a->sign != b->sign)
984 35720373 : return -a->sign - -b->sign;
985 :
986 50723151 : if (REAL_EXP (a) > REAL_EXP (b))
987 : ret = 1;
988 40928765 : else if (REAL_EXP (a) < REAL_EXP (b))
989 : ret = -1;
990 : else
991 50723151 : ret = cmp_significands (a, b);
992 :
993 50723151 : return (a->sign ? -ret : ret);
994 : }
995 :
996 : /* Return A truncated to an integral value toward zero. */
997 :
998 : static void
999 1995388 : do_fix_trunc (REAL_VALUE_TYPE *r, const REAL_VALUE_TYPE *a)
1000 : {
1001 1995388 : *r = *a;
1002 :
1003 1995388 : switch (r->cl)
1004 : {
1005 37088 : case rvc_zero:
1006 37088 : case rvc_inf:
1007 37088 : case rvc_nan:
1008 : /* Make resulting NaN value to be qNaN. The caller has the
1009 : responsibility to avoid the operation if flag_signaling_nans
1010 : is on. */
1011 37088 : r->signalling = 0;
1012 37088 : break;
1013 :
1014 1958300 : case rvc_normal:
1015 1958300 : if (r->decimal)
1016 : {
1017 463 : decimal_do_fix_trunc (r, a);
1018 463 : return;
1019 : }
1020 1957837 : if (REAL_EXP (r) <= 0)
1021 204391 : get_zero (r, r->sign);
1022 1753446 : else if (REAL_EXP (r) < SIGNIFICAND_BITS)
1023 1753307 : clear_significand_below (r, SIGNIFICAND_BITS - REAL_EXP (r));
1024 : break;
1025 :
1026 0 : default:
1027 0 : gcc_unreachable ();
1028 : }
1029 : }
1030 :
1031 : /* Perform the binary or unary operation described by CODE.
1032 : For a unary operation, leave OP1 NULL. This function returns
1033 : true if the result may be inexact due to loss of precision. */
1034 :
1035 : bool
1036 39104445 : real_arithmetic (REAL_VALUE_TYPE *r, int icode, const REAL_VALUE_TYPE *op0,
1037 : const REAL_VALUE_TYPE *op1)
1038 : {
1039 39104445 : enum tree_code code = (enum tree_code) icode;
1040 :
1041 39104445 : if (op0->decimal || (op1 && op1->decimal))
1042 575179 : return decimal_real_arithmetic (r, code, op0, op1);
1043 :
1044 38529266 : switch (code)
1045 : {
1046 5875836 : case PLUS_EXPR:
1047 : /* Clear any padding areas in *r if it isn't equal to one of the
1048 : operands so that we can later do bitwise comparisons later on. */
1049 5875836 : if (r != op0 && r != op1)
1050 5875836 : memset (r, '\0', sizeof (*r));
1051 5875836 : return do_add (r, op0, op1, 0);
1052 :
1053 2167635 : case MINUS_EXPR:
1054 2167635 : if (r != op0 && r != op1)
1055 2165043 : memset (r, '\0', sizeof (*r));
1056 2167635 : return do_add (r, op0, op1, 1);
1057 :
1058 7529920 : case MULT_EXPR:
1059 7529920 : if (r != op0 && r != op1)
1060 7529856 : memset (r, '\0', sizeof (*r));
1061 7529920 : return do_multiply (r, op0, op1);
1062 :
1063 6296364 : case RDIV_EXPR:
1064 6296364 : if (r != op0 && r != op1)
1065 6294142 : memset (r, '\0', sizeof (*r));
1066 6296364 : return do_divide (r, op0, op1);
1067 :
1068 370 : case MIN_EXPR:
1069 370 : if (op1->cl == rvc_nan)
1070 : {
1071 0 : *r = *op1;
1072 : /* Make resulting NaN value to be qNaN. The caller has the
1073 : responsibility to avoid the operation if flag_signaling_nans
1074 : is on. */
1075 0 : r->signalling = 0;
1076 : }
1077 370 : else if (do_compare (op0, op1, -1) < 0)
1078 134 : *r = *op0;
1079 : else
1080 236 : *r = *op1;
1081 : break;
1082 :
1083 373 : case MAX_EXPR:
1084 373 : if (op1->cl == rvc_nan)
1085 : {
1086 0 : *r = *op1;
1087 : /* Make resulting NaN value to be qNaN. The caller has the
1088 : responsibility to avoid the operation if flag_signaling_nans
1089 : is on. */
1090 0 : r->signalling = 0;
1091 : }
1092 373 : else if (do_compare (op0, op1, 1) < 0)
1093 144 : *r = *op1;
1094 : else
1095 229 : *r = *op0;
1096 : break;
1097 :
1098 15815863 : case NEGATE_EXPR:
1099 15815863 : *r = *op0;
1100 15815863 : r->sign ^= 1;
1101 15815863 : break;
1102 :
1103 842905 : case ABS_EXPR:
1104 842905 : *r = *op0;
1105 842905 : r->sign = 0;
1106 842905 : break;
1107 :
1108 0 : case FIX_TRUNC_EXPR:
1109 0 : do_fix_trunc (r, op0);
1110 0 : break;
1111 :
1112 0 : default:
1113 0 : gcc_unreachable ();
1114 : }
1115 : return false;
1116 : }
1117 :
1118 : REAL_VALUE_TYPE
1119 15824792 : real_value_negate (const REAL_VALUE_TYPE *op0)
1120 : {
1121 15824792 : REAL_VALUE_TYPE r;
1122 15824792 : real_arithmetic (&r, NEGATE_EXPR, op0, NULL);
1123 15824792 : return r;
1124 : }
1125 :
1126 : REAL_VALUE_TYPE
1127 842905 : real_value_abs (const REAL_VALUE_TYPE *op0)
1128 : {
1129 842905 : REAL_VALUE_TYPE r;
1130 842905 : real_arithmetic (&r, ABS_EXPR, op0, NULL);
1131 842905 : return r;
1132 : }
1133 :
1134 : /* Return whether OP0 == OP1. */
1135 :
1136 : bool
1137 43420186 : real_equal (const REAL_VALUE_TYPE *op0, const REAL_VALUE_TYPE *op1)
1138 : {
1139 43420186 : return do_compare (op0, op1, -1) == 0;
1140 : }
1141 :
1142 : /* Return whether OP0 < OP1. */
1143 :
1144 : bool
1145 120387548 : real_less (const REAL_VALUE_TYPE *op0, const REAL_VALUE_TYPE *op1)
1146 : {
1147 120387548 : return do_compare (op0, op1, 1) < 0;
1148 : }
1149 :
1150 : bool
1151 41549416 : real_compare (int icode, const REAL_VALUE_TYPE *op0,
1152 : const REAL_VALUE_TYPE *op1)
1153 : {
1154 41549416 : enum tree_code code = (enum tree_code) icode;
1155 :
1156 41549416 : switch (code)
1157 : {
1158 129797 : case LT_EXPR:
1159 129797 : return real_less (op0, op1);
1160 35184705 : case LE_EXPR:
1161 35184705 : return do_compare (op0, op1, 1) <= 0;
1162 886029 : case GT_EXPR:
1163 886029 : return do_compare (op0, op1, -1) > 0;
1164 4232934 : case GE_EXPR:
1165 4232934 : return do_compare (op0, op1, -1) >= 0;
1166 116970 : case EQ_EXPR:
1167 116970 : return real_equal (op0, op1);
1168 945530 : case NE_EXPR:
1169 945530 : return do_compare (op0, op1, -1) != 0;
1170 36203 : case UNORDERED_EXPR:
1171 36203 : return op0->cl == rvc_nan || op1->cl == rvc_nan;
1172 1049 : case ORDERED_EXPR:
1173 1049 : return op0->cl != rvc_nan && op1->cl != rvc_nan;
1174 222 : case UNLT_EXPR:
1175 222 : return do_compare (op0, op1, -1) < 0;
1176 8545 : case UNLE_EXPR:
1177 8545 : return do_compare (op0, op1, -1) <= 0;
1178 1353 : case UNGT_EXPR:
1179 1353 : return do_compare (op0, op1, 1) > 0;
1180 5899 : case UNGE_EXPR:
1181 5899 : return do_compare (op0, op1, 1) >= 0;
1182 42 : case UNEQ_EXPR:
1183 42 : return do_compare (op0, op1, 0) == 0;
1184 138 : case LTGT_EXPR:
1185 138 : return do_compare (op0, op1, 0) != 0;
1186 :
1187 0 : default:
1188 0 : gcc_unreachable ();
1189 : }
1190 : }
1191 :
1192 : /* Return floor log2(R). */
1193 :
1194 : int
1195 0 : real_exponent (const REAL_VALUE_TYPE *r)
1196 : {
1197 0 : switch (r->cl)
1198 : {
1199 : case rvc_zero:
1200 : return 0;
1201 0 : case rvc_inf:
1202 0 : case rvc_nan:
1203 0 : return (unsigned int)-1 >> 1;
1204 0 : case rvc_normal:
1205 0 : return REAL_EXP (r);
1206 0 : default:
1207 0 : gcc_unreachable ();
1208 : }
1209 : }
1210 :
1211 : /* R = OP0 * 2**EXP. */
1212 :
1213 : void
1214 10218 : real_ldexp (REAL_VALUE_TYPE *r, const REAL_VALUE_TYPE *op0, int exp)
1215 : {
1216 10218 : *r = *op0;
1217 10218 : switch (r->cl)
1218 : {
1219 198 : case rvc_zero:
1220 198 : case rvc_inf:
1221 198 : case rvc_nan:
1222 : /* Make resulting NaN value to be qNaN. The caller has the
1223 : responsibility to avoid the operation if flag_signaling_nans
1224 : is on. */
1225 198 : r->signalling = 0;
1226 198 : break;
1227 :
1228 10020 : case rvc_normal:
1229 10020 : exp += REAL_EXP (op0);
1230 10020 : if (exp > MAX_EXP)
1231 0 : get_inf (r, r->sign);
1232 10020 : else if (exp < -MAX_EXP)
1233 0 : get_zero (r, r->sign);
1234 : else
1235 10020 : SET_REAL_EXP (r, exp);
1236 : break;
1237 :
1238 0 : default:
1239 0 : gcc_unreachable ();
1240 : }
1241 10218 : }
1242 :
1243 : /* Determine whether a floating-point value X is infinite. */
1244 :
1245 : bool
1246 58818772 : real_isinf (const REAL_VALUE_TYPE *r)
1247 : {
1248 58818772 : return (r->cl == rvc_inf);
1249 : }
1250 :
1251 : /* Determine whether a floating-point value X is infinite with SIGN. */
1252 :
1253 : bool
1254 23555999 : real_isinf (const REAL_VALUE_TYPE *r, bool sign)
1255 : {
1256 23555999 : return real_isinf (r) && r->sign == sign;
1257 : }
1258 :
1259 : /* Determine whether a floating-point value X is a NaN. */
1260 :
1261 : bool
1262 372225822 : real_isnan (const REAL_VALUE_TYPE *r)
1263 : {
1264 372225822 : return (r->cl == rvc_nan);
1265 : }
1266 :
1267 : /* Determine whether a floating-point value X is a signaling NaN. */
1268 86966 : bool real_issignaling_nan (const REAL_VALUE_TYPE *r)
1269 : {
1270 86966 : return real_isnan (r) && r->signalling;
1271 : }
1272 :
1273 : /* Determine whether a floating-point value X is finite. */
1274 :
1275 : bool
1276 3835969 : real_isfinite (const REAL_VALUE_TYPE *r)
1277 : {
1278 3835969 : return (r->cl != rvc_nan) && (r->cl != rvc_inf);
1279 : }
1280 :
1281 : /* Determine whether a floating-point value X is negative. */
1282 :
1283 : bool
1284 36207422 : real_isneg (const REAL_VALUE_TYPE *r)
1285 : {
1286 36207422 : return r->sign;
1287 : }
1288 :
1289 : /* Determine whether a floating-point value X is plus or minus zero. */
1290 :
1291 : bool
1292 32562644 : real_iszero (const REAL_VALUE_TYPE *r)
1293 : {
1294 32562644 : return r->cl == rvc_zero;
1295 : }
1296 :
1297 : /* Determine whether a floating-point value X is zero with SIGN. */
1298 :
1299 : bool
1300 4647822 : real_iszero (const REAL_VALUE_TYPE *r, bool sign)
1301 : {
1302 4647822 : return real_iszero (r) && r->sign == sign;
1303 : }
1304 :
1305 : /* Determine whether a floating-point value X is minus zero. */
1306 :
1307 : bool
1308 17459485 : real_isnegzero (const REAL_VALUE_TYPE *r)
1309 : {
1310 17459485 : return r->sign && r->cl == rvc_zero;
1311 : }
1312 :
1313 : /* Compare two floating-point objects for bitwise identity. */
1314 :
1315 : bool
1316 231785330 : real_identical (const REAL_VALUE_TYPE *a, const REAL_VALUE_TYPE *b)
1317 : {
1318 231785330 : int i;
1319 :
1320 231785330 : if (a->cl != b->cl)
1321 : return false;
1322 164399068 : if (a->sign != b->sign)
1323 : return false;
1324 :
1325 157281795 : switch (a->cl)
1326 : {
1327 : case rvc_zero:
1328 : case rvc_inf:
1329 : return true;
1330 :
1331 31779418 : case rvc_normal:
1332 31779418 : if (a->decimal != b->decimal)
1333 : return false;
1334 31775564 : if (REAL_EXP (a) != REAL_EXP (b))
1335 : return false;
1336 : break;
1337 :
1338 106556 : case rvc_nan:
1339 106556 : if (a->signalling != b->signalling)
1340 : return false;
1341 : /* The significand is ignored for canonical NaNs. */
1342 101597 : if (a->canonical || b->canonical)
1343 46422 : return a->canonical == b->canonical;
1344 : break;
1345 :
1346 0 : default:
1347 0 : gcc_unreachable ();
1348 : }
1349 :
1350 102625090 : for (i = 0; i < SIGSZ; ++i)
1351 78047271 : if (a->sig[i] != b->sig[i])
1352 : return false;
1353 :
1354 : return true;
1355 : }
1356 :
1357 : /* Try to change R into its exact multiplicative inverse in format FMT.
1358 : Return true if successful. */
1359 :
1360 : bool
1361 1153107 : exact_real_inverse (format_helper fmt, REAL_VALUE_TYPE *r)
1362 : {
1363 1153107 : const REAL_VALUE_TYPE *one = real_digit (1);
1364 1153107 : REAL_VALUE_TYPE u;
1365 1153107 : int i;
1366 :
1367 1153107 : if (r->cl != rvc_normal)
1368 : return false;
1369 :
1370 : /* Check for a power of two: all significand bits zero except the MSB. */
1371 3430786 : for (i = 0; i < SIGSZ-1; ++i)
1372 2287720 : if (r->sig[i] != 0)
1373 : return false;
1374 1143066 : if (r->sig[SIGSZ-1] != SIG_MSB)
1375 : return false;
1376 :
1377 : /* Find the inverse and truncate to the required format. */
1378 330695 : do_divide (&u, one, r);
1379 330695 : real_convert (&u, fmt, &u);
1380 :
1381 : /* The rounding may have overflowed. */
1382 330695 : if (u.cl != rvc_normal)
1383 : return false;
1384 992085 : for (i = 0; i < SIGSZ-1; ++i)
1385 661390 : if (u.sig[i] != 0)
1386 : return false;
1387 330695 : if (u.sig[SIGSZ-1] != SIG_MSB)
1388 : return false;
1389 :
1390 330695 : *r = u;
1391 330695 : return true;
1392 : }
1393 :
1394 : /* Return true if arithmetic on values in IMODE that were promoted
1395 : from values in TMODE is equivalent to direct arithmetic on values
1396 : in TMODE. */
1397 :
1398 : bool
1399 136517 : real_can_shorten_arithmetic (machine_mode imode, machine_mode tmode)
1400 : {
1401 136517 : const struct real_format *tfmt, *ifmt;
1402 136517 : tfmt = REAL_MODE_FORMAT (tmode);
1403 136517 : ifmt = REAL_MODE_FORMAT (imode);
1404 : /* These conditions are conservative rather than trying to catch the
1405 : exact boundary conditions; the main case to allow is IEEE float
1406 : and double. */
1407 136517 : return (ifmt->b == tfmt->b
1408 136517 : && ifmt->p > 2 * tfmt->p
1409 49933 : && ifmt->emin < 2 * tfmt->emin - tfmt->p - 2
1410 29211 : && ifmt->emin < tfmt->emin - tfmt->emax - tfmt->p - 2
1411 29211 : && ifmt->emax > 2 * tfmt->emax + 2
1412 29211 : && ifmt->emax > tfmt->emax - tfmt->emin + tfmt->p + 2
1413 29211 : && ifmt->round_towards_zero == tfmt->round_towards_zero
1414 29211 : && (ifmt->has_sign_dependent_rounding
1415 29211 : == tfmt->has_sign_dependent_rounding)
1416 29211 : && ifmt->has_nans >= tfmt->has_nans
1417 29211 : && ifmt->has_inf >= tfmt->has_inf
1418 29211 : && ifmt->has_signed_zero >= tfmt->has_signed_zero
1419 175266 : && !MODE_COMPOSITE_P (tmode)
1420 311783 : && !MODE_COMPOSITE_P (imode));
1421 : }
1422 :
1423 : /* Render R as an integer. */
1424 :
1425 : HOST_WIDE_INT
1426 2612 : real_to_integer (const REAL_VALUE_TYPE *r)
1427 : {
1428 2612 : unsigned HOST_WIDE_INT i;
1429 :
1430 2612 : switch (r->cl)
1431 : {
1432 : case rvc_zero:
1433 2612 : underflow:
1434 : return 0;
1435 :
1436 334 : case rvc_inf:
1437 334 : case rvc_nan:
1438 334 : overflow:
1439 334 : i = HOST_WIDE_INT_1U << (HOST_BITS_PER_WIDE_INT - 1);
1440 334 : if (!r->sign)
1441 232 : i--;
1442 334 : return i;
1443 :
1444 2297 : case rvc_normal:
1445 2297 : if (r->decimal)
1446 0 : return decimal_real_to_integer (r);
1447 :
1448 2297 : if (REAL_EXP (r) <= 0)
1449 398 : goto underflow;
1450 : /* Only force overflow for unsigned overflow. Signed overflow is
1451 : undefined, so it doesn't matter what we return, and some callers
1452 : expect to be able to use this routine for both signed and
1453 : unsigned conversions. */
1454 1899 : if (REAL_EXP (r) > HOST_BITS_PER_WIDE_INT)
1455 28 : goto overflow;
1456 :
1457 1871 : if (HOST_BITS_PER_WIDE_INT == HOST_BITS_PER_LONG)
1458 1871 : i = r->sig[SIGSZ-1];
1459 : else
1460 : {
1461 : gcc_assert (HOST_BITS_PER_WIDE_INT == 2 * HOST_BITS_PER_LONG);
1462 : i = r->sig[SIGSZ-1];
1463 : i = i << (HOST_BITS_PER_LONG - 1) << 1;
1464 : i |= r->sig[SIGSZ-2];
1465 : }
1466 :
1467 1871 : i >>= HOST_BITS_PER_WIDE_INT - REAL_EXP (r);
1468 :
1469 1871 : if (r->sign)
1470 619 : i = -i;
1471 1871 : return i;
1472 :
1473 0 : default:
1474 0 : gcc_unreachable ();
1475 : }
1476 : }
1477 :
1478 : /* Likewise, but producing a wide-int of PRECISION. If the value cannot
1479 : be represented in precision, *FAIL is set to TRUE. */
1480 :
1481 : wide_int
1482 472375 : real_to_integer (const REAL_VALUE_TYPE *r, bool *fail, int precision)
1483 : {
1484 472375 : HOST_WIDE_INT valb[WIDE_INT_MAX_INL_ELTS], *val;
1485 472375 : int exp;
1486 472375 : int words, w;
1487 472375 : wide_int result;
1488 :
1489 472375 : switch (r->cl)
1490 : {
1491 177369 : case rvc_zero:
1492 177369 : underflow:
1493 177369 : return wi::zero (precision);
1494 :
1495 302 : case rvc_inf:
1496 302 : case rvc_nan:
1497 302 : overflow:
1498 302 : *fail = true;
1499 :
1500 302 : if (r->sign)
1501 65 : return wi::set_bit_in_zero (precision - 1, precision);
1502 : else
1503 237 : return ~wi::set_bit_in_zero (precision - 1, precision);
1504 :
1505 294945 : case rvc_normal:
1506 294945 : if (r->decimal)
1507 395 : return decimal_real_to_integer (r, fail, precision);
1508 :
1509 294550 : exp = REAL_EXP (r);
1510 294550 : if (exp <= 0)
1511 0 : goto underflow;
1512 : /* Only force overflow for unsigned overflow. Signed overflow is
1513 : undefined, so it doesn't matter what we return, and some callers
1514 : expect to be able to use this routine for both signed and
1515 : unsigned conversions. */
1516 294550 : if (exp > precision)
1517 241 : goto overflow;
1518 :
1519 : /* Put the significand into a wide_int that has precision W, which
1520 : is the smallest HWI-multiple that has at least PRECISION bits.
1521 : This ensures that the top bit of the significand is in the
1522 : top bit of the wide_int. */
1523 294309 : words = ((precision + HOST_BITS_PER_WIDE_INT - 1)
1524 : / HOST_BITS_PER_WIDE_INT);
1525 294309 : val = valb;
1526 294309 : if (UNLIKELY (words > WIDE_INT_MAX_INL_ELTS))
1527 2 : val = XALLOCAVEC (HOST_WIDE_INT, words);
1528 294309 : w = words * HOST_BITS_PER_WIDE_INT;
1529 :
1530 : #if (HOST_BITS_PER_WIDE_INT == HOST_BITS_PER_LONG)
1531 589686 : for (int i = 0; i < words; i++)
1532 : {
1533 295377 : int j = SIGSZ - words + i;
1534 295377 : val[i] = (j < 0) ? 0 : r->sig[j];
1535 : }
1536 : #else
1537 : gcc_assert (HOST_BITS_PER_WIDE_INT == 2 * HOST_BITS_PER_LONG);
1538 : for (int i = 0; i < words; i++)
1539 : {
1540 : int j = SIGSZ - (words * 2) + (i * 2);
1541 : if (j < 0)
1542 : val[i] = 0;
1543 : else
1544 : val[i] = r->sig[j];
1545 : j += 1;
1546 : if (j >= 0)
1547 : val[i] |= (unsigned HOST_WIDE_INT) r->sig[j] << HOST_BITS_PER_LONG;
1548 : }
1549 : #endif
1550 : /* Shift the value into place and truncate to the desired precision. */
1551 294309 : result = wide_int::from_array (val, words, w);
1552 294309 : result = wi::lrshift (result, w - exp);
1553 294309 : result = wide_int::from (result, precision, UNSIGNED);
1554 :
1555 294309 : if (r->sign)
1556 42099 : return -result;
1557 : else
1558 252210 : return result;
1559 :
1560 0 : default:
1561 0 : gcc_unreachable ();
1562 : }
1563 472375 : }
1564 :
1565 : /* A subroutine of real_to_decimal. Compute the quotient and remainder
1566 : of NUM / DEN. Return the quotient and place the remainder in NUM.
1567 : It is expected that NUM / DEN are close enough that the quotient is
1568 : small. */
1569 :
1570 : static unsigned long
1571 27759087 : rtd_divmod (REAL_VALUE_TYPE *num, REAL_VALUE_TYPE *den)
1572 : {
1573 27759087 : unsigned long q, msb;
1574 27759087 : int expn = REAL_EXP (num), expd = REAL_EXP (den);
1575 :
1576 27759087 : if (expn < expd)
1577 : return 0;
1578 :
1579 17996288 : q = msb = 0;
1580 17996288 : goto start;
1581 35521679 : do
1582 : {
1583 35521679 : msb = num->sig[SIGSZ-1] & SIG_MSB;
1584 35521679 : q <<= 1;
1585 35521679 : lshift_significand_1 (num, num);
1586 53517967 : start:
1587 96912398 : if (msb || cmp_significands (num, den) >= 0)
1588 : {
1589 28751066 : sub_significands (num, num, den, 0);
1590 28751066 : q |= 1;
1591 : }
1592 : }
1593 53517967 : while (--expn >= expd);
1594 :
1595 17996288 : SET_REAL_EXP (num, expd);
1596 17996288 : normalize (num);
1597 :
1598 17996288 : return q;
1599 : }
1600 :
1601 : /* Render R as a decimal floating point constant. Emit DIGITS significant
1602 : digits in the result, bounded by BUF_SIZE. If DIGITS is 0, choose the
1603 : maximum for the representation. If CROP_TRAILING_ZEROS, strip trailing
1604 : zeros. If MODE is VOIDmode, round to nearest value. Otherwise, round
1605 : to a string that, when parsed back in mode MODE, yields the same value. */
1606 :
1607 : #define M_LOG10_2 0.30102999566398119521
1608 :
1609 : void
1610 784426 : real_to_decimal_for_mode (char *str, const REAL_VALUE_TYPE *r_orig,
1611 : size_t buf_size, size_t digits,
1612 : int crop_trailing_zeros, machine_mode mode)
1613 : {
1614 784426 : const struct real_format *fmt = NULL;
1615 784426 : const REAL_VALUE_TYPE *one, *ten;
1616 784426 : REAL_VALUE_TYPE r, pten, u, v;
1617 784426 : int dec_exp, cmp_one, digit;
1618 784426 : size_t max_digits;
1619 784426 : char *p, *first, *last;
1620 784426 : bool sign;
1621 784426 : bool round_up;
1622 :
1623 784426 : if (mode != VOIDmode)
1624 : {
1625 563563 : fmt = REAL_MODE_FORMAT (mode);
1626 563563 : gcc_assert (fmt);
1627 : }
1628 :
1629 784426 : r = *r_orig;
1630 784426 : switch (r.cl)
1631 : {
1632 107997 : case rvc_zero:
1633 107997 : strcpy (str, (r.sign ? "-0.0" : "0.0"));
1634 108727 : return;
1635 675744 : case rvc_normal:
1636 : /* When r_orig is a positive value that converts to all nines and is
1637 : rounded up to 1.0, str[0] is harmlessly accessed before being set to
1638 : '1'. That read access triggers a valgrind warning. Setting str[0]
1639 : to any value quiets the warning. */
1640 675744 : str[0] = ' ';
1641 675744 : break;
1642 374 : case rvc_inf:
1643 374 : strcpy (str, (r.sign ? "-Inf" : "+Inf"));
1644 374 : return;
1645 311 : case rvc_nan:
1646 : /* ??? Print the significand as well, if not canonical? */
1647 311 : sprintf (str, "%c%cNaN", (r_orig->sign ? '-' : '+'),
1648 311 : (r_orig->signalling ? 'S' : 'Q'));
1649 311 : return;
1650 0 : default:
1651 0 : gcc_unreachable ();
1652 : }
1653 :
1654 675744 : if (r.decimal)
1655 : {
1656 45 : decimal_real_to_decimal (str, &r, buf_size, digits, crop_trailing_zeros);
1657 45 : return;
1658 : }
1659 :
1660 : /* Bound the number of digits printed by the size of the representation. */
1661 675699 : max_digits = SIGNIFICAND_BITS * M_LOG10_2;
1662 675699 : if (digits == 0 || digits > max_digits)
1663 111242 : digits = max_digits;
1664 :
1665 : /* Estimate the decimal exponent, and compute the length of the string it
1666 : will print as. Be conservative and add one to account for possible
1667 : overflow or rounding error. */
1668 675699 : dec_exp = REAL_EXP (&r) * M_LOG10_2;
1669 2155496 : for (max_digits = 1; dec_exp ; max_digits++)
1670 1479797 : dec_exp /= 10;
1671 :
1672 : /* Bound the number of digits printed by the size of the output buffer. */
1673 675699 : max_digits = buf_size - 1 - 1 - 2 - max_digits - 1;
1674 675699 : gcc_assert (max_digits <= buf_size);
1675 675699 : if (digits > max_digits)
1676 : digits = max_digits;
1677 :
1678 675699 : one = real_digit (1);
1679 675699 : ten = ten_to_ptwo (0);
1680 :
1681 675699 : sign = r.sign;
1682 675699 : r.sign = 0;
1683 :
1684 675699 : dec_exp = 0;
1685 675699 : pten = *one;
1686 :
1687 675699 : cmp_one = do_compare (&r, one, 0);
1688 675699 : if (cmp_one > 0)
1689 : {
1690 225789 : int m;
1691 :
1692 : /* Number is greater than one. Convert significand to an integer
1693 : and strip trailing decimal zeros. */
1694 :
1695 225789 : u = r;
1696 225789 : SET_REAL_EXP (&u, SIGNIFICAND_BITS - 1);
1697 :
1698 : /* Largest M, such that 10**2**M fits within SIGNIFICAND_BITS. */
1699 451578 : m = floor_log2 (max_digits);
1700 :
1701 : /* Iterate over the bits of the possible powers of 10 that might
1702 : be present in U and eliminate them. That is, if we find that
1703 : 10**2**M divides U evenly, keep the division and increase
1704 : DEC_EXP by 2**M. */
1705 1406856 : do
1706 : {
1707 1406856 : REAL_VALUE_TYPE t;
1708 :
1709 1406856 : do_divide (&t, &u, ten_to_ptwo (m));
1710 1406856 : do_fix_trunc (&v, &t);
1711 2813712 : if (cmp_significands (&v, &t) == 0)
1712 : {
1713 98978 : u = t;
1714 98978 : dec_exp += 1 << m;
1715 : }
1716 : }
1717 1406856 : while (--m >= 0);
1718 :
1719 : /* Revert the scaling to integer that we performed earlier. */
1720 225789 : SET_REAL_EXP (&u, REAL_EXP (&u) + REAL_EXP (&r)
1721 : - (SIGNIFICAND_BITS - 1));
1722 225789 : r = u;
1723 :
1724 : /* Find power of 10. Do this by dividing out 10**2**M when
1725 : this is larger than the current remainder. Fill PTEN with
1726 : the power of 10 that we compute. */
1727 225789 : if (REAL_EXP (&r) > 0)
1728 : {
1729 410810 : m = floor_log2 ((int)(REAL_EXP (&r) * M_LOG10_2)) + 1;
1730 1808732 : do
1731 : {
1732 1808732 : const REAL_VALUE_TYPE *ptentwo = ten_to_ptwo (m);
1733 1808732 : if (do_compare (&u, ptentwo, 0) >= 0)
1734 : {
1735 714163 : do_divide (&u, &u, ptentwo);
1736 714163 : do_multiply (&pten, &pten, ptentwo);
1737 714163 : dec_exp += 1 << m;
1738 : }
1739 : }
1740 1808732 : while (--m >= 0);
1741 : }
1742 : else
1743 : /* We managed to divide off enough tens in the above reduction
1744 : loop that we've now got a negative exponent. Fall into the
1745 : less-than-one code to compute the proper value for PTEN. */
1746 : cmp_one = -1;
1747 : }
1748 670996 : if (cmp_one < 0)
1749 : {
1750 401025 : int m;
1751 :
1752 : /* Number is less than one. Pad significand with leading
1753 : decimal zeros. */
1754 :
1755 401025 : v = r;
1756 37354373 : while (1)
1757 : {
1758 : /* Stop if we'd shift bits off the bottom. */
1759 18877699 : if (v.sig[0] & 7)
1760 : break;
1761 :
1762 18717368 : do_multiply (&u, &v, ten);
1763 :
1764 : /* Stop if we're now >= 1 or zero. */
1765 18717368 : if (REAL_EXP (&u) > 0 || u.cl == rvc_zero)
1766 : break;
1767 :
1768 18476674 : v = u;
1769 18476674 : dec_exp -= 1;
1770 : }
1771 401025 : r = v;
1772 :
1773 : /* Find power of 10. Do this by multiplying in P=10**2**M when
1774 : the current remainder is smaller than 1/P. Fill PTEN with the
1775 : power of 10 that we compute. */
1776 561356 : m = floor_log2 ((int)(-REAL_EXP (&r) * M_LOG10_2)) + 1;
1777 2084729 : do
1778 : {
1779 2084729 : const REAL_VALUE_TYPE *ptentwo = ten_to_ptwo (m);
1780 2084729 : const REAL_VALUE_TYPE *ptenmtwo = ten_to_mptwo (m);
1781 :
1782 2084729 : if (do_compare (&v, ptenmtwo, 0) <= 0)
1783 : {
1784 837461 : do_multiply (&v, &v, ptentwo);
1785 837461 : do_multiply (&pten, &pten, ptentwo);
1786 837461 : dec_exp -= 1 << m;
1787 : }
1788 : }
1789 2084729 : while (--m >= 0);
1790 :
1791 : /* Invert the positive power of 10 that we've collected so far. */
1792 401025 : do_divide (&pten, one, &pten);
1793 : }
1794 :
1795 675699 : p = str;
1796 675699 : if (sign)
1797 20745 : *p++ = '-';
1798 675699 : first = p++;
1799 :
1800 : /* At this point, PTEN should contain the nearest power of 10 smaller
1801 : than R, such that this division produces the first digit.
1802 :
1803 : Using a divide-step primitive that returns the complete integral
1804 : remainder avoids the rounding error that would be produced if
1805 : we were to use do_divide here and then simply multiply by 10 for
1806 : each subsequent digit. */
1807 :
1808 675699 : digit = rtd_divmod (&r, &pten);
1809 :
1810 : /* Be prepared for error in that division via underflow ... */
1811 1076724 : if (digit == 0 && cmp_significand_0 (&r))
1812 : {
1813 : /* Multiply by 10 and try again. */
1814 401025 : do_multiply (&r, &r, ten);
1815 401025 : digit = rtd_divmod (&r, &pten);
1816 401025 : dec_exp -= 1;
1817 401025 : gcc_assert (digit != 0);
1818 : }
1819 :
1820 : /* ... or overflow. */
1821 675699 : if (digit == 10)
1822 : {
1823 0 : *p++ = '1';
1824 0 : if (--digits > 0)
1825 0 : *p++ = '0';
1826 0 : dec_exp += 1;
1827 : }
1828 : else
1829 : {
1830 675699 : gcc_assert (digit <= 10);
1831 675699 : *p++ = digit + '0';
1832 : }
1833 :
1834 : /* Generate subsequent digits. */
1835 26635060 : while (--digits > 0)
1836 : {
1837 25959361 : do_multiply (&r, &r, ten);
1838 25959361 : digit = rtd_divmod (&r, &pten);
1839 25959361 : *p++ = digit + '0';
1840 : }
1841 675699 : last = p;
1842 :
1843 : /* Generate one more digit with which to do rounding. */
1844 675699 : do_multiply (&r, &r, ten);
1845 675699 : digit = rtd_divmod (&r, &pten);
1846 :
1847 : /* Round the result. */
1848 675699 : if (fmt && fmt->round_towards_zero)
1849 : {
1850 : /* If the format uses round towards zero when parsing the string
1851 : back in, we need to always round away from zero here. */
1852 0 : if (cmp_significand_0 (&r))
1853 0 : digit++;
1854 0 : round_up = digit > 0;
1855 : }
1856 : else
1857 : {
1858 675699 : if (digit == 5)
1859 : {
1860 : /* Round to nearest. If R is nonzero there are additional
1861 : nonzero digits to be extracted. */
1862 71864 : if (cmp_significand_0 (&r))
1863 : digit++;
1864 : /* Round to even. */
1865 40119 : else if ((p[-1] - '0') & 1)
1866 31756 : digit++;
1867 : }
1868 :
1869 675699 : round_up = digit > 5;
1870 : }
1871 :
1872 675699 : if (round_up)
1873 : {
1874 210418 : while (p > first)
1875 : {
1876 210418 : digit = *--p;
1877 210418 : if (digit == '9')
1878 459 : *p = '0';
1879 : else
1880 : {
1881 209959 : *p = digit + 1;
1882 209959 : break;
1883 : }
1884 : }
1885 :
1886 : /* Carry out of the first digit. This means we had all 9's and
1887 : now have all 0's. "Prepend" a 1 by overwriting the first 0. */
1888 209959 : if (p == first)
1889 : {
1890 0 : first[1] = '1';
1891 0 : dec_exp++;
1892 : }
1893 : }
1894 :
1895 : /* Insert the decimal point. */
1896 675699 : first[0] = first[1];
1897 675699 : first[1] = '.';
1898 :
1899 : /* If requested, drop trailing zeros. Never crop past "1.0". */
1900 675699 : if (crop_trailing_zeros)
1901 5900982 : while (last > first + 3 && last[-1] == '0')
1902 5788578 : last--;
1903 :
1904 : /* Append the exponent. */
1905 675699 : sprintf (last, "e%+d", dec_exp);
1906 :
1907 : /* Verify that we can read the original value back in. */
1908 675699 : if (flag_checking && mode != VOIDmode)
1909 : {
1910 563425 : real_from_string (&r, str);
1911 563425 : real_convert (&r, mode, &r);
1912 563425 : gcc_assert (real_identical (&r, r_orig));
1913 : }
1914 : }
1915 :
1916 : /* Likewise, except always uses round-to-nearest. */
1917 :
1918 : void
1919 220863 : real_to_decimal (char *str, const REAL_VALUE_TYPE *r_orig, size_t buf_size,
1920 : size_t digits, int crop_trailing_zeros)
1921 : {
1922 220863 : real_to_decimal_for_mode (str, r_orig, buf_size,
1923 : digits, crop_trailing_zeros, VOIDmode);
1924 220863 : }
1925 :
1926 : DEBUG_FUNCTION void
1927 0 : debug (const REAL_VALUE_TYPE &r)
1928 : {
1929 0 : char s[60];
1930 0 : real_to_hexadecimal (s, &r, sizeof (s), 0, 1);
1931 0 : fprintf (stderr, "%s\n", s);
1932 0 : }
1933 :
1934 : /* Render R as a hexadecimal floating point constant. Emit DIGITS
1935 : significant digits in the result, bounded by BUF_SIZE. If DIGITS is 0,
1936 : choose the maximum for the representation. If CROP_TRAILING_ZEROS,
1937 : strip trailing zeros. */
1938 :
1939 : void
1940 564520 : real_to_hexadecimal (char *str, const REAL_VALUE_TYPE *r, size_t buf_size,
1941 : size_t digits, int crop_trailing_zeros)
1942 : {
1943 564520 : int i, j, exp = REAL_EXP (r);
1944 564520 : char *p, *first;
1945 564520 : char exp_buf[16];
1946 564520 : size_t max_digits;
1947 :
1948 564520 : switch (r->cl)
1949 : {
1950 96108 : case rvc_zero:
1951 96108 : exp = 0;
1952 96108 : break;
1953 : case rvc_normal:
1954 : break;
1955 0 : case rvc_inf:
1956 0 : strcpy (str, (r->sign ? "-Inf" : "+Inf"));
1957 4 : return;
1958 4 : case rvc_nan:
1959 : /* ??? Print the significand as well, if not canonical? */
1960 4 : sprintf (str, "%c%cNaN", (r->sign ? '-' : '+'),
1961 4 : (r->signalling ? 'S' : 'Q'));
1962 4 : return;
1963 0 : default:
1964 0 : gcc_unreachable ();
1965 : }
1966 :
1967 564516 : if (r->decimal)
1968 : {
1969 : /* Hexadecimal format for decimal floats is not interesting. */
1970 0 : strcpy (str, "N/A");
1971 0 : return;
1972 : }
1973 :
1974 564516 : if (digits == 0)
1975 564516 : digits = SIGNIFICAND_BITS / 4;
1976 :
1977 : /* Bound the number of digits printed by the size of the output buffer. */
1978 :
1979 564516 : sprintf (exp_buf, "p%+d", exp);
1980 564516 : max_digits = buf_size - strlen (exp_buf) - r->sign - 4 - 1;
1981 564516 : gcc_assert (max_digits <= buf_size);
1982 564516 : if (digits > max_digits)
1983 : digits = max_digits;
1984 :
1985 564516 : p = str;
1986 564516 : if (r->sign)
1987 93751 : *p++ = '-';
1988 564516 : *p++ = '0';
1989 564516 : *p++ = 'x';
1990 564516 : *p++ = '0';
1991 564516 : *p++ = '.';
1992 564516 : first = p;
1993 :
1994 1693548 : for (i = SIGSZ - 1; i >= 0; --i)
1995 28225800 : for (j = HOST_BITS_PER_LONG - 4; j >= 0; j -= 4)
1996 : {
1997 27096768 : *p++ = "0123456789abcdef"[(r->sig[i] >> j) & 15];
1998 27096768 : if (--digits == 0)
1999 564516 : goto out;
2000 : }
2001 :
2002 0 : out:
2003 564516 : if (crop_trailing_zeros)
2004 24696612 : while (p > first + 1 && p[-1] == '0')
2005 24132096 : p--;
2006 :
2007 564516 : sprintf (p, "p%+d", exp);
2008 : }
2009 :
2010 : /* Initialize R from a decimal or hexadecimal string. The string is
2011 : assumed to have been syntax checked already. Return -1 if the
2012 : value underflows, +1 if overflows, and 0 otherwise. */
2013 :
2014 : int
2015 38339911 : real_from_string (REAL_VALUE_TYPE *r, const char *str)
2016 : {
2017 38339911 : int exp = 0;
2018 38339911 : bool sign = false;
2019 :
2020 38339911 : get_zero (r, 0);
2021 :
2022 38339911 : if (*str == '-')
2023 : {
2024 83264 : sign = true;
2025 83264 : str++;
2026 : }
2027 38256647 : else if (*str == '+')
2028 48 : str++;
2029 :
2030 38339911 : if (startswith (str, "QNaN"))
2031 : {
2032 39 : get_canonical_qnan (r, sign);
2033 39 : return 0;
2034 : }
2035 38339872 : else if (startswith (str, "SNaN"))
2036 : {
2037 16 : get_canonical_snan (r, sign);
2038 16 : return 0;
2039 : }
2040 38339856 : else if (startswith (str, "Inf"))
2041 : {
2042 49 : get_inf (r, sign);
2043 49 : return 0;
2044 : }
2045 :
2046 38339807 : if (str[0] == '0' && (str[1] == 'x' || str[1] == 'X'))
2047 : {
2048 : /* Hexadecimal floating point. */
2049 31741272 : int pos = SIGNIFICAND_BITS - 4, d;
2050 :
2051 31741272 : str += 2;
2052 :
2053 57958040 : while (*str == '0')
2054 26216768 : str++;
2055 33350192 : while (1)
2056 : {
2057 32545732 : d = hex_value (*str);
2058 32545732 : if (d == _hex_bad)
2059 : break;
2060 804460 : if (pos >= 0)
2061 : {
2062 804424 : r->sig[pos / HOST_BITS_PER_LONG]
2063 804424 : |= (unsigned long) d << (pos % HOST_BITS_PER_LONG);
2064 804424 : pos -= 4;
2065 : }
2066 36 : else if (d)
2067 : /* Ensure correct rounding by setting last bit if there is
2068 : a subsequent nonzero digit. */
2069 8 : r->sig[0] |= 1;
2070 804460 : exp += 4;
2071 804460 : str++;
2072 : }
2073 31741272 : if (*str == '.')
2074 : {
2075 31006854 : str++;
2076 31006854 : if (pos == SIGNIFICAND_BITS - 4)
2077 : {
2078 32066507 : while (*str == '0')
2079 1109682 : str++, exp -= 4;
2080 : }
2081 1117517898 : while (1)
2082 : {
2083 574262376 : d = hex_value (*str);
2084 574262376 : if (d == _hex_bad)
2085 : break;
2086 543255522 : if (pos >= 0)
2087 : {
2088 538456718 : r->sig[pos / HOST_BITS_PER_LONG]
2089 538456718 : |= (unsigned long) d << (pos % HOST_BITS_PER_LONG);
2090 538456718 : pos -= 4;
2091 : }
2092 4798804 : else if (d)
2093 : /* Ensure correct rounding by setting last bit if there is
2094 : a subsequent nonzero digit. */
2095 1033130 : r->sig[0] |= 1;
2096 543255522 : str++;
2097 : }
2098 : }
2099 :
2100 : /* If the mantissa is zero, ignore the exponent. */
2101 63482544 : if (!cmp_significand_0 (r))
2102 69681 : goto is_a_zero;
2103 :
2104 31671591 : if (*str == 'p' || *str == 'P')
2105 : {
2106 31671591 : bool exp_neg = false;
2107 :
2108 31671591 : str++;
2109 31671591 : if (*str == '-')
2110 : {
2111 1658807 : exp_neg = true;
2112 1658807 : str++;
2113 : }
2114 30012784 : else if (*str == '+')
2115 21073 : str++;
2116 :
2117 31671591 : d = 0;
2118 142432533 : while (ISDIGIT (*str))
2119 : {
2120 110760942 : d *= 10;
2121 110760942 : d += *str - '0';
2122 110760942 : if (d > MAX_EXP)
2123 : {
2124 : /* Overflowed the exponent. */
2125 0 : if (exp_neg)
2126 0 : goto underflow;
2127 : else
2128 0 : goto overflow;
2129 : }
2130 110760942 : str++;
2131 : }
2132 31671591 : if (exp_neg)
2133 1658807 : d = -d;
2134 :
2135 31671591 : exp += d;
2136 : }
2137 :
2138 31671591 : r->cl = rvc_normal;
2139 31671591 : SET_REAL_EXP (r, exp);
2140 :
2141 31671591 : normalize (r);
2142 31671591 : }
2143 : else
2144 : {
2145 : /* Decimal floating point. */
2146 : const char *cstr = str;
2147 : bool inexact;
2148 :
2149 10162271 : while (*cstr == '0')
2150 3563736 : cstr++;
2151 6598535 : if (*cstr == '.')
2152 : {
2153 3564759 : cstr++;
2154 6389448 : while (*cstr == '0')
2155 2824689 : cstr++;
2156 : }
2157 :
2158 : /* If the mantissa is zero, ignore the exponent. */
2159 6598535 : if (!ISDIGIT (*cstr))
2160 2388347 : goto is_a_zero;
2161 :
2162 : /* Nonzero value, possibly overflowing or underflowing. */
2163 4210188 : auto_mpfr m (SIGNIFICAND_BITS);
2164 4210188 : inexact = mpfr_strtofr (m, str, NULL, 10, MPFR_RNDZ);
2165 : /* The result should never be a NaN, and because the rounding is
2166 : toward zero should never be an infinity. */
2167 4210188 : gcc_assert (!mpfr_nan_p (m) && !mpfr_inf_p (m));
2168 4210188 : if (mpfr_zero_p (m) || mpfr_get_exp (m) < -MAX_EXP + 4)
2169 42 : goto underflow;
2170 4210146 : else if (mpfr_get_exp (m) > MAX_EXP - 4)
2171 0 : goto overflow;
2172 : else
2173 : {
2174 4210146 : real_from_mpfr (r, m, NULL_TREE, MPFR_RNDZ);
2175 : /* 1 to 3 bits may have been shifted off (with a sticky bit)
2176 : because the hex digits used in real_from_mpfr did not
2177 : start with a digit 8 to f, but the exponent bounds above
2178 : should have avoided underflow or overflow. */
2179 4210146 : gcc_assert (r->cl == rvc_normal);
2180 : /* Set a sticky bit if mpfr_strtofr was inexact. */
2181 4210146 : r->sig[0] |= inexact;
2182 : }
2183 4210188 : }
2184 :
2185 35881737 : r->sign = sign;
2186 35881737 : return 0;
2187 :
2188 2458028 : is_a_zero:
2189 2458028 : get_zero (r, sign);
2190 2458028 : return 0;
2191 :
2192 42 : underflow:
2193 42 : get_zero (r, sign);
2194 42 : return -1;
2195 :
2196 0 : overflow:
2197 0 : get_inf (r, sign);
2198 0 : return 1;
2199 : }
2200 :
2201 : /* Legacy. Similar, but return the result directly. */
2202 :
2203 : REAL_VALUE_TYPE
2204 6681 : real_from_string2 (const char *s, format_helper fmt)
2205 : {
2206 6681 : REAL_VALUE_TYPE r;
2207 :
2208 6681 : real_from_string (&r, s);
2209 6681 : if (fmt)
2210 6681 : real_convert (&r, fmt, &r);
2211 :
2212 6681 : return r;
2213 : }
2214 :
2215 : /* Initialize R from string S and desired format FMT. */
2216 :
2217 : void
2218 6665978 : real_from_string3 (REAL_VALUE_TYPE *r, const char *s, format_helper fmt)
2219 : {
2220 6665978 : if (fmt.decimal_p ())
2221 16183 : decimal_real_from_string (r, s);
2222 : else
2223 6649795 : real_from_string (r, s);
2224 :
2225 6665978 : if (fmt)
2226 6665978 : real_convert (r, fmt, r);
2227 6665978 : }
2228 :
2229 : /* Initialize R from the wide_int VAL_IN. Round it to format FMT if
2230 : FMT is nonnull. */
2231 :
2232 : void
2233 27307842 : real_from_integer (REAL_VALUE_TYPE *r, format_helper fmt,
2234 : const wide_int_ref &val_in, signop sgn)
2235 : {
2236 27307842 : if (val_in == 0)
2237 5052767 : get_zero (r, 0);
2238 : else
2239 : {
2240 22255075 : unsigned int len = val_in.get_precision ();
2241 22255075 : int i, j, e = 0;
2242 22255075 : const unsigned int realmax = (SIGNIFICAND_BITS / HOST_BITS_PER_WIDE_INT
2243 : * HOST_BITS_PER_WIDE_INT);
2244 :
2245 22255075 : memset (r, 0, sizeof (*r));
2246 22255075 : r->cl = rvc_normal;
2247 22255075 : r->sign = wi::neg_p (val_in, sgn);
2248 :
2249 : /* Ensure a multiple of HOST_BITS_PER_WIDE_INT, ceiling, as elt
2250 : won't work with precisions that are not a multiple of
2251 : HOST_BITS_PER_WIDE_INT. */
2252 22255075 : len += HOST_BITS_PER_WIDE_INT - 1;
2253 :
2254 : /* Ensure we can represent the largest negative number. */
2255 22255075 : len += 1;
2256 :
2257 22255075 : len = len / HOST_BITS_PER_WIDE_INT * HOST_BITS_PER_WIDE_INT;
2258 :
2259 : /* We have to ensure we can negate the largest negative number. */
2260 22255075 : wide_int val = wide_int::from (val_in, len, sgn);
2261 :
2262 22255075 : if (r->sign)
2263 951871 : val = -val;
2264 :
2265 : /* Cap the size to the size allowed by real.h. */
2266 22255075 : if (len > realmax)
2267 : {
2268 226 : HOST_WIDE_INT cnt_l_z;
2269 226 : cnt_l_z = wi::clz (val);
2270 :
2271 226 : if (len - cnt_l_z > realmax)
2272 : {
2273 107 : e = len - cnt_l_z - realmax;
2274 :
2275 : /* This value is too large, we must shift it right to
2276 : preserve all the bits we can, and then bump the
2277 : exponent up by that amount, but or in 1 if any of
2278 : the shifted out bits are non-zero. */
2279 130 : if (wide_int::from (val, e, UNSIGNED) != 0)
2280 118 : val = wi::set_bit (wi::lrshift (val, e), 0);
2281 : else
2282 35 : val = wi::lrshift (val, e);
2283 : }
2284 : len = realmax;
2285 : }
2286 :
2287 : /* Clear out top bits so elt will work with precisions that aren't
2288 : a multiple of HOST_BITS_PER_WIDE_INT. */
2289 22255075 : val = wide_int::from (val, len, sgn);
2290 22255075 : len = len / HOST_BITS_PER_WIDE_INT;
2291 :
2292 22255075 : SET_REAL_EXP (r, len * HOST_BITS_PER_WIDE_INT + e);
2293 :
2294 22255075 : j = SIGSZ - 1;
2295 22255075 : if (HOST_BITS_PER_LONG == HOST_BITS_PER_WIDE_INT)
2296 46179294 : for (i = len - 1; i >= 0; i--)
2297 : {
2298 23956927 : r->sig[j--] = val.elt (i);
2299 23956927 : if (j < 0)
2300 : break;
2301 : }
2302 : else
2303 : {
2304 : gcc_assert (HOST_BITS_PER_LONG*2 == HOST_BITS_PER_WIDE_INT);
2305 : for (i = len - 1; i >= 0; i--)
2306 : {
2307 : HOST_WIDE_INT e = val.elt (i);
2308 : r->sig[j--] = e >> (HOST_BITS_PER_LONG - 1) >> 1;
2309 : if (j < 0)
2310 : break;
2311 : r->sig[j--] = e;
2312 : if (j < 0)
2313 : break;
2314 : }
2315 : }
2316 :
2317 22255075 : normalize (r);
2318 22255075 : }
2319 :
2320 27307842 : if (fmt.decimal_p ())
2321 : /* We need at most one decimal digits for each 3 bits of input
2322 : precision. */
2323 10047 : decimal_from_integer (r, val_in.get_precision () / 3);
2324 27307842 : if (fmt)
2325 26587532 : real_convert (r, fmt, r);
2326 27307842 : }
2327 :
2328 : /* Render R, an integral value, as a floating point constant with no
2329 : specified exponent. */
2330 :
2331 : static void
2332 10047 : decimal_integer_string (char *str, const REAL_VALUE_TYPE *r_orig,
2333 : size_t buf_size)
2334 : {
2335 10047 : int dec_exp, digit, digits;
2336 10047 : REAL_VALUE_TYPE r, pten;
2337 10047 : char *p;
2338 10047 : bool sign;
2339 :
2340 10047 : r = *r_orig;
2341 :
2342 10047 : if (r.cl == rvc_zero)
2343 : {
2344 1772 : strcpy (str, "0.");
2345 1772 : return;
2346 : }
2347 :
2348 8275 : sign = r.sign;
2349 8275 : r.sign = 0;
2350 :
2351 8275 : dec_exp = REAL_EXP (&r) * M_LOG10_2;
2352 8275 : digits = dec_exp + 1;
2353 8275 : gcc_assert ((digits + 2) < (int)buf_size);
2354 :
2355 8275 : pten = *real_digit (1);
2356 8275 : times_pten (&pten, dec_exp);
2357 :
2358 8275 : p = str;
2359 8275 : if (sign)
2360 1151 : *p++ = '-';
2361 :
2362 8275 : digit = rtd_divmod (&r, &pten);
2363 8275 : gcc_assert (digit >= 0 && digit <= 9);
2364 8275 : *p++ = digit + '0';
2365 47303 : while (--digits > 0)
2366 : {
2367 39028 : times_pten (&r, 1);
2368 39028 : digit = rtd_divmod (&r, &pten);
2369 39028 : *p++ = digit + '0';
2370 : }
2371 8275 : *p++ = '.';
2372 8275 : *p++ = '\0';
2373 : }
2374 :
2375 : /* Convert a real with an integral value to decimal float. */
2376 :
2377 : static void
2378 10047 : decimal_from_integer (REAL_VALUE_TYPE *r, int digits)
2379 : {
2380 10047 : char str[256];
2381 :
2382 10047 : if (digits <= 256)
2383 : {
2384 10045 : decimal_integer_string (str, r, sizeof (str) - 1);
2385 10045 : decimal_real_from_string (r, str);
2386 : }
2387 : else
2388 : {
2389 2 : char *s = XALLOCAVEC (char, digits);
2390 2 : decimal_integer_string (s, r, digits - 1);
2391 2 : decimal_real_from_string (r, s);
2392 : }
2393 10047 : }
2394 :
2395 : /* Returns 10**2**N. */
2396 :
2397 : static const REAL_VALUE_TYPE *
2398 6437605 : ten_to_ptwo (int n)
2399 : {
2400 6437605 : static REAL_VALUE_TYPE tens[EXP_BITS];
2401 :
2402 6437605 : gcc_assert (n >= 0);
2403 6437605 : gcc_assert (n < EXP_BITS);
2404 :
2405 6437605 : if (tens[n].cl == rvc_zero)
2406 : {
2407 260209 : if (n < (HOST_BITS_PER_WIDE_INT == 64 ? 5 : 4))
2408 : {
2409 : HOST_WIDE_INT t = 10;
2410 : int i;
2411 :
2412 288812 : for (i = 0; i < n; ++i)
2413 192156 : t *= t;
2414 :
2415 96656 : real_from_integer (&tens[n], VOIDmode, t, UNSIGNED);
2416 : }
2417 : else
2418 : {
2419 163553 : const REAL_VALUE_TYPE *t = ten_to_ptwo (n - 1);
2420 163553 : do_multiply (&tens[n], t, t);
2421 : }
2422 : }
2423 :
2424 6437605 : return &tens[n];
2425 : }
2426 :
2427 : /* Returns 10**(-2**N). */
2428 :
2429 : static const REAL_VALUE_TYPE *
2430 2084729 : ten_to_mptwo (int n)
2431 : {
2432 2084729 : static REAL_VALUE_TYPE tens[EXP_BITS];
2433 :
2434 2084729 : gcc_assert (n >= 0);
2435 2084729 : gcc_assert (n < EXP_BITS);
2436 :
2437 2084729 : if (tens[n].cl == rvc_zero)
2438 250210 : do_divide (&tens[n], real_digit (1), ten_to_ptwo (n));
2439 :
2440 2084729 : return &tens[n];
2441 : }
2442 :
2443 : /* Returns N. */
2444 :
2445 : static const REAL_VALUE_TYPE *
2446 2087363 : real_digit (int n)
2447 : {
2448 2087363 : static REAL_VALUE_TYPE num[10];
2449 :
2450 2087363 : gcc_assert (n >= 0);
2451 2087363 : gcc_assert (n <= 9);
2452 :
2453 2087363 : if (n > 0 && num[n].cl == rvc_zero)
2454 21242 : real_from_integer (&num[n], VOIDmode, n, UNSIGNED);
2455 :
2456 2087363 : return &num[n];
2457 : }
2458 :
2459 : /* Multiply R by 10**EXP. */
2460 :
2461 : static void
2462 47303 : times_pten (REAL_VALUE_TYPE *r, int exp)
2463 : {
2464 47303 : REAL_VALUE_TYPE pten, *rr;
2465 47303 : bool negative = (exp < 0);
2466 47303 : int i;
2467 :
2468 47303 : if (negative)
2469 : {
2470 0 : exp = -exp;
2471 0 : pten = *real_digit (1);
2472 0 : rr = &pten;
2473 : }
2474 : else
2475 : rr = r;
2476 :
2477 101274 : for (i = 0; exp > 0; ++i, exp >>= 1)
2478 53971 : if (exp & 1)
2479 47826 : do_multiply (rr, rr, ten_to_ptwo (i));
2480 :
2481 47303 : if (negative)
2482 0 : do_divide (r, r, &pten);
2483 47303 : }
2484 :
2485 : /* Returns the special REAL_VALUE_TYPE corresponding to 'e'. */
2486 :
2487 : const REAL_VALUE_TYPE *
2488 72 : dconst_e_ptr (void)
2489 : {
2490 72 : static REAL_VALUE_TYPE value;
2491 :
2492 : /* Initialize mathematical constants for constant folding builtins.
2493 : These constants need to be given to at least 160 bits precision. */
2494 72 : if (value.cl == rvc_zero)
2495 : {
2496 6 : auto_mpfr m (SIGNIFICAND_BITS);
2497 6 : mpfr_set_ui (m, 1, MPFR_RNDN);
2498 6 : mpfr_exp (m, m, MPFR_RNDN);
2499 6 : real_from_mpfr (&value, m, NULL_TREE, MPFR_RNDN);
2500 :
2501 6 : }
2502 72 : return &value;
2503 : }
2504 :
2505 : /* Returns the special REAL_VALUE_TYPE corresponding to 'pi'. */
2506 :
2507 : const REAL_VALUE_TYPE *
2508 1797 : dconst_pi_ptr (void)
2509 : {
2510 1797 : static REAL_VALUE_TYPE value;
2511 :
2512 : /* Initialize mathematical constants for constant folding builtins.
2513 : These constants need to be given to at least 160 bits precision. */
2514 1797 : if (value.cl == rvc_zero)
2515 : {
2516 140 : auto_mpfr m (SIGNIFICAND_BITS);
2517 140 : mpfr_set_si (m, -1, MPFR_RNDN);
2518 140 : mpfr_acos (m, m, MPFR_RNDN);
2519 140 : real_from_mpfr (&value, m, NULL_TREE, MPFR_RNDN);
2520 :
2521 140 : }
2522 1797 : return &value;
2523 : }
2524 :
2525 : /* Returns a cached REAL_VALUE_TYPE corresponding to 1/n, for various n. */
2526 :
2527 : #define CACHED_FRACTION(NAME, N) \
2528 : const REAL_VALUE_TYPE * \
2529 : NAME (void) \
2530 : { \
2531 : static REAL_VALUE_TYPE value; \
2532 : \
2533 : /* Initialize mathematical constants for constant folding builtins. \
2534 : These constants need to be given to at least 160 bits \
2535 : precision. */ \
2536 : if (value.cl == rvc_zero) \
2537 : real_arithmetic (&value, RDIV_EXPR, &dconst1, real_digit (N)); \
2538 : return &value; \
2539 : }
2540 :
2541 1659 : CACHED_FRACTION (dconst_third_ptr, 3)
2542 36 : CACHED_FRACTION (dconst_quarter_ptr, 4)
2543 72 : CACHED_FRACTION (dconst_sixth_ptr, 6)
2544 36 : CACHED_FRACTION (dconst_ninth_ptr, 9)
2545 :
2546 : /* Returns the special REAL_VALUE_TYPE corresponding to sqrt(2). */
2547 :
2548 : const REAL_VALUE_TYPE *
2549 23 : dconst_sqrt2_ptr (void)
2550 : {
2551 23 : static REAL_VALUE_TYPE value;
2552 :
2553 : /* Initialize mathematical constants for constant folding builtins.
2554 : These constants need to be given to at least 160 bits precision. */
2555 23 : if (value.cl == rvc_zero)
2556 : {
2557 4 : auto_mpfr m (SIGNIFICAND_BITS);
2558 4 : mpfr_sqrt_ui (m, 2, MPFR_RNDN);
2559 4 : real_from_mpfr (&value, m, NULL_TREE, MPFR_RNDN);
2560 4 : }
2561 23 : return &value;
2562 : }
2563 :
2564 : /* Fills R with Inf with SIGN. */
2565 :
2566 : void
2567 679774 : real_inf (REAL_VALUE_TYPE *r, bool sign)
2568 : {
2569 679774 : get_inf (r, sign);
2570 679774 : }
2571 :
2572 : /* Fills R with a NaN whose significand is described by STR. If QUIET,
2573 : we force a QNaN, else we force an SNaN. The string, if not empty,
2574 : is parsed as a number and placed in the significand. Return true
2575 : if the string was successfully parsed. */
2576 :
2577 : bool
2578 458244 : real_nan (REAL_VALUE_TYPE *r, const char *str, int quiet,
2579 : format_helper fmt)
2580 : {
2581 458244 : if (*str == 0)
2582 : {
2583 457721 : if (quiet)
2584 264615 : get_canonical_qnan (r, 0);
2585 : else
2586 193106 : get_canonical_snan (r, 0);
2587 : }
2588 : else
2589 : {
2590 523 : int base = 10, d;
2591 :
2592 523 : memset (r, 0, sizeof (*r));
2593 523 : r->cl = rvc_nan;
2594 :
2595 : /* Parse akin to strtol into the significand of R. */
2596 :
2597 523 : while (ISSPACE (*str))
2598 0 : str++;
2599 523 : if (*str == '-')
2600 0 : str++;
2601 523 : else if (*str == '+')
2602 0 : str++;
2603 523 : if (*str == '0')
2604 : {
2605 463 : str++;
2606 463 : if (*str == 'x' || *str == 'X')
2607 : {
2608 238 : base = 16;
2609 238 : str++;
2610 : }
2611 : else
2612 523 : base = 8;
2613 : }
2614 :
2615 1223 : while ((d = hex_value (*str)) < base)
2616 : {
2617 700 : REAL_VALUE_TYPE u;
2618 :
2619 700 : switch (base)
2620 : {
2621 0 : case 8:
2622 0 : lshift_significand (r, r, 3);
2623 0 : break;
2624 700 : case 16:
2625 700 : lshift_significand (r, r, 4);
2626 700 : break;
2627 0 : case 10:
2628 0 : lshift_significand_1 (&u, r);
2629 0 : lshift_significand (r, r, 3);
2630 0 : add_significands (r, r, &u);
2631 0 : break;
2632 0 : default:
2633 0 : gcc_unreachable ();
2634 : }
2635 :
2636 700 : get_zero (&u, 0);
2637 700 : u.sig[0] = d;
2638 700 : add_significands (r, r, &u);
2639 :
2640 700 : str++;
2641 : }
2642 :
2643 : /* Must have consumed the entire string for success. */
2644 523 : if (*str != 0)
2645 : return false;
2646 :
2647 : /* Shift the significand into place such that the bits
2648 : are in the most significant bits for the format. */
2649 463 : lshift_significand (r, r, SIGNIFICAND_BITS - fmt->pnan);
2650 :
2651 : /* Our MSB is always unset for NaNs. */
2652 463 : r->sig[SIGSZ-1] &= ~SIG_MSB;
2653 :
2654 : /* Force quiet or signaling NaN. */
2655 463 : r->signalling = !quiet;
2656 : }
2657 :
2658 : return true;
2659 : }
2660 :
2661 : /* Fills R with the largest finite value representable in mode MODE.
2662 : If SIGN is nonzero, R is set to the most negative finite value. */
2663 :
2664 : void
2665 78618 : real_maxval (REAL_VALUE_TYPE *r, int sign, machine_mode mode)
2666 : {
2667 78618 : const struct real_format *fmt;
2668 78618 : int np2;
2669 :
2670 78618 : fmt = REAL_MODE_FORMAT (mode);
2671 78618 : gcc_assert (fmt);
2672 78618 : memset (r, 0, sizeof (*r));
2673 :
2674 78618 : if (fmt->b == 10)
2675 81 : decimal_real_maxval (r, sign, mode);
2676 : else
2677 : {
2678 78537 : r->cl = rvc_normal;
2679 78537 : r->sign = sign;
2680 78537 : SET_REAL_EXP (r, fmt->emax);
2681 :
2682 78537 : np2 = SIGNIFICAND_BITS - fmt->p;
2683 78537 : memset (r->sig, -1, SIGSZ * sizeof (unsigned long));
2684 78537 : clear_significand_below (r, np2);
2685 :
2686 78537 : if (fmt->pnan < fmt->p)
2687 : /* This is an IBM extended double format made up of two IEEE
2688 : doubles. The value of the long double is the sum of the
2689 : values of the two parts. The most significant part is
2690 : required to be the value of the long double rounded to the
2691 : nearest double. Rounding means we need a slightly smaller
2692 : value for LDBL_MAX. */
2693 0 : clear_significand_bit (r, SIGNIFICAND_BITS - fmt->pnan - 1);
2694 : }
2695 78618 : }
2696 :
2697 : /* Fills R with 2**N. */
2698 :
2699 : void
2700 6315 : real_2expN (REAL_VALUE_TYPE *r, int n, format_helper fmt)
2701 : {
2702 6315 : memset (r, 0, sizeof (*r));
2703 :
2704 6315 : n++;
2705 6315 : if (n > MAX_EXP)
2706 0 : r->cl = rvc_inf;
2707 6315 : else if (n < -MAX_EXP)
2708 : ;
2709 : else
2710 : {
2711 6315 : r->cl = rvc_normal;
2712 6315 : SET_REAL_EXP (r, n);
2713 6315 : r->sig[SIGSZ-1] = SIG_MSB;
2714 : }
2715 6315 : if (fmt.decimal_p ())
2716 0 : decimal_real_convert (r, fmt, r);
2717 6315 : }
2718 :
2719 :
2720 : static void
2721 65480680 : round_for_format (const struct real_format *fmt, REAL_VALUE_TYPE *r)
2722 : {
2723 65480680 : int p2, np2, i, w;
2724 65480680 : int emin2m1, emax2;
2725 65480680 : bool round_up = false;
2726 :
2727 65480680 : if (r->decimal)
2728 : {
2729 634427 : if (fmt->b == 10)
2730 : {
2731 634427 : decimal_round_for_format (fmt, r);
2732 634427 : return;
2733 : }
2734 : /* FIXME. We can come here via fp_easy_constant
2735 : (e.g. -O0 on '_Decimal32 x = 1.0 + 2.0dd'), but have not
2736 : investigated whether this convert needs to be here, or
2737 : something else is missing. */
2738 0 : decimal_real_convert (r, REAL_MODE_FORMAT (DFmode), r);
2739 : }
2740 :
2741 64846253 : p2 = fmt->p;
2742 64846253 : emin2m1 = fmt->emin - 1;
2743 64846253 : emax2 = fmt->emax;
2744 :
2745 64846253 : np2 = SIGNIFICAND_BITS - p2;
2746 64846253 : switch (r->cl)
2747 : {
2748 394252 : underflow:
2749 394252 : get_zero (r, r->sign);
2750 : /* FALLTHRU */
2751 9806453 : case rvc_zero:
2752 9806453 : if (!fmt->has_signed_zero)
2753 0 : r->sign = 0;
2754 : return;
2755 :
2756 3477415 : overflow:
2757 3477415 : get_inf (r, r->sign);
2758 : case rvc_inf:
2759 : return;
2760 :
2761 138253 : case rvc_nan:
2762 138253 : clear_significand_below (r, np2);
2763 138253 : return;
2764 :
2765 46764534 : case rvc_normal:
2766 46764534 : break;
2767 :
2768 0 : default:
2769 0 : gcc_unreachable ();
2770 : }
2771 :
2772 : /* Check the range of the exponent. If we're out of range,
2773 : either underflow or overflow. */
2774 46764534 : if (REAL_EXP (r) > emax2)
2775 3470878 : goto overflow;
2776 43293656 : else if (REAL_EXP (r) <= emin2m1)
2777 : {
2778 868178 : int diff;
2779 :
2780 868178 : if (!fmt->has_denorm)
2781 : {
2782 : /* Don't underflow completely until we've had a chance to round. */
2783 0 : if (REAL_EXP (r) < emin2m1)
2784 0 : goto underflow;
2785 : }
2786 : else
2787 : {
2788 868178 : diff = emin2m1 - REAL_EXP (r) + 1;
2789 868178 : if (diff > p2)
2790 394252 : goto underflow;
2791 :
2792 : /* De-normalize the significand. */
2793 473926 : r->sig[0] |= sticky_rshift_significand (r, r, diff);
2794 473926 : SET_REAL_EXP (r, REAL_EXP (r) + diff);
2795 : }
2796 : }
2797 :
2798 42899404 : if (!fmt->round_towards_zero)
2799 : {
2800 : /* There are P2 true significand bits, followed by one guard bit,
2801 : followed by one sticky bit, followed by stuff. Fold nonzero
2802 : stuff into the sticky bit. */
2803 42899404 : unsigned long sticky;
2804 42899404 : bool guard, lsb;
2805 :
2806 42899404 : sticky = 0;
2807 113310770 : for (i = 0, w = (np2 - 1) / HOST_BITS_PER_LONG; i < w; ++i)
2808 70411366 : sticky |= r->sig[i];
2809 42899404 : sticky |= r->sig[w]
2810 42899404 : & (((unsigned long)1 << ((np2 - 1) % HOST_BITS_PER_LONG)) - 1);
2811 :
2812 42899404 : guard = test_significand_bit (r, np2 - 1);
2813 42899404 : lsb = test_significand_bit (r, np2);
2814 :
2815 : /* Round to even. */
2816 42899404 : round_up = guard && (sticky || lsb);
2817 : }
2818 :
2819 3846520 : if (round_up)
2820 : {
2821 3846520 : REAL_VALUE_TYPE u;
2822 3846520 : get_zero (&u, 0);
2823 3846520 : set_significand_bit (&u, np2);
2824 :
2825 3846520 : if (add_significands (r, r, &u))
2826 : {
2827 : /* Overflow. Means the significand had been all ones, and
2828 : is now all zeros. Need to increase the exponent, and
2829 : possibly re-normalize it. */
2830 567940 : SET_REAL_EXP (r, REAL_EXP (r) + 1);
2831 567940 : if (REAL_EXP (r) > emax2)
2832 6537 : goto overflow;
2833 561403 : r->sig[SIGSZ-1] = SIG_MSB;
2834 : }
2835 : }
2836 :
2837 : /* Catch underflow that we deferred until after rounding. */
2838 42892867 : if (REAL_EXP (r) <= emin2m1)
2839 0 : goto underflow;
2840 :
2841 : /* Clear out trailing garbage. */
2842 42892867 : clear_significand_below (r, np2);
2843 : }
2844 :
2845 : /* Extend or truncate to a new format. */
2846 :
2847 : void
2848 62664580 : real_convert (REAL_VALUE_TYPE *r, format_helper fmt,
2849 : const REAL_VALUE_TYPE *a)
2850 : {
2851 62664580 : *r = *a;
2852 :
2853 62664580 : if (a->decimal || fmt->b == 10)
2854 595985 : decimal_real_convert (r, fmt, a);
2855 :
2856 62664580 : round_for_format (fmt, r);
2857 :
2858 : /* Make resulting NaN value to be qNaN. The caller has the
2859 : responsibility to avoid the operation if flag_signaling_nans
2860 : is on. */
2861 62664580 : if (r->cl == rvc_nan)
2862 51977 : r->signalling = 0;
2863 :
2864 : /* round_for_format de-normalizes denormals. Undo just that part. */
2865 62664580 : if (r->cl == rvc_normal)
2866 41388922 : normalize (r);
2867 62664580 : }
2868 :
2869 : /* Legacy. Likewise, except return the struct directly. */
2870 :
2871 : REAL_VALUE_TYPE
2872 198989 : real_value_truncate (format_helper fmt, REAL_VALUE_TYPE a)
2873 : {
2874 198989 : REAL_VALUE_TYPE r;
2875 198989 : real_convert (&r, fmt, &a);
2876 198989 : return r;
2877 : }
2878 :
2879 : /* Return true if truncating to FMT is exact. */
2880 :
2881 : bool
2882 3677188 : exact_real_truncate (format_helper fmt, const REAL_VALUE_TYPE *a)
2883 : {
2884 3677188 : REAL_VALUE_TYPE t;
2885 3677188 : int emin2m1;
2886 :
2887 : /* Don't allow conversion to denormals. */
2888 3677188 : emin2m1 = fmt->emin - 1;
2889 3677188 : if (REAL_EXP (a) <= emin2m1)
2890 : return false;
2891 :
2892 : /* After conversion to the new format, the value must be identical. */
2893 3318028 : real_convert (&t, fmt, a);
2894 3318028 : return real_identical (&t, a);
2895 : }
2896 :
2897 : /* Write R to the given target format. Place the words of the result
2898 : in target word order in BUF. There are always 32 bits in each
2899 : long, no matter the size of the host long.
2900 :
2901 : Legacy: return word 0 for implementing REAL_VALUE_TO_TARGET_SINGLE. */
2902 :
2903 : long
2904 2816100 : real_to_target (long *buf, const REAL_VALUE_TYPE *r_orig,
2905 : format_helper fmt)
2906 : {
2907 2816100 : REAL_VALUE_TYPE r;
2908 2816100 : long buf1;
2909 :
2910 2816100 : r = *r_orig;
2911 2816100 : round_for_format (fmt, &r);
2912 :
2913 2816100 : if (!buf)
2914 277455 : buf = &buf1;
2915 2816100 : (*fmt->encode) (fmt, buf, &r);
2916 :
2917 2816100 : return *buf;
2918 : }
2919 :
2920 : /* Read R from the given target format. Read the words of the result
2921 : in target word order in BUF. There are always 32 bits in each
2922 : long, no matter the size of the host long. */
2923 :
2924 : void
2925 290483 : real_from_target (REAL_VALUE_TYPE *r, const long *buf, format_helper fmt)
2926 : {
2927 290483 : (*fmt->decode) (fmt, r, buf);
2928 290483 : }
2929 :
2930 : /* Return the number of bits of the largest binary value that the
2931 : significand of FMT will hold. */
2932 : /* ??? Legacy. Should get access to real_format directly. */
2933 :
2934 : int
2935 157116 : significand_size (format_helper fmt)
2936 : {
2937 157116 : if (fmt == NULL)
2938 : return 0;
2939 :
2940 157116 : if (fmt->b == 10)
2941 : {
2942 : /* Return the size in bits of the largest binary value that can be
2943 : held by the decimal coefficient for this format. This is one more
2944 : than the number of bits required to hold the largest coefficient
2945 : of this format. */
2946 8887 : double log2_10 = 3.3219281;
2947 8887 : return fmt->p * log2_10;
2948 : }
2949 148229 : return fmt->p;
2950 : }
2951 :
2952 : /* Return a hash value for the given real value. */
2953 : /* ??? The "unsigned int" return value is intended to be hashval_t,
2954 : but I didn't want to pull hashtab.h into real.h. */
2955 :
2956 : unsigned int
2957 45087615 : real_hash (const REAL_VALUE_TYPE *r)
2958 : {
2959 45087615 : unsigned int h;
2960 45087615 : size_t i;
2961 :
2962 45087615 : h = r->cl | (r->sign << 2);
2963 45087615 : switch (r->cl)
2964 : {
2965 : case rvc_zero:
2966 : case rvc_inf:
2967 : return h;
2968 :
2969 30564206 : case rvc_normal:
2970 30564206 : h |= (unsigned int)REAL_EXP (r) << 3;
2971 30564206 : break;
2972 :
2973 550383 : case rvc_nan:
2974 550383 : if (r->signalling)
2975 14295 : h ^= (unsigned int)-1;
2976 550383 : if (r->canonical)
2977 : return h;
2978 : break;
2979 :
2980 0 : default:
2981 0 : gcc_unreachable ();
2982 : }
2983 :
2984 : if (sizeof (unsigned long) > sizeof (unsigned int))
2985 123698968 : for (i = 0; i < SIGSZ; ++i)
2986 : {
2987 92774226 : unsigned long s = r->sig[i];
2988 92774226 : h ^= s ^ (s >> (HOST_BITS_PER_LONG / 2));
2989 : }
2990 : else
2991 : for (i = 0; i < SIGSZ; ++i)
2992 : h ^= r->sig[i];
2993 :
2994 : return h;
2995 : }
2996 :
2997 : /* IEEE single-precision format. */
2998 :
2999 : static void encode_ieee_single (const struct real_format *fmt,
3000 : long *, const REAL_VALUE_TYPE *);
3001 : static void decode_ieee_single (const struct real_format *,
3002 : REAL_VALUE_TYPE *, const long *);
3003 :
3004 : static void
3005 1284464 : encode_ieee_single (const struct real_format *fmt, long *buf,
3006 : const REAL_VALUE_TYPE *r)
3007 : {
3008 1284464 : unsigned long image, sig, exp;
3009 1284464 : unsigned long sign = r->sign;
3010 :
3011 1284464 : image = sign << 31;
3012 1284464 : sig = (r->sig[SIGSZ-1] >> (HOST_BITS_PER_LONG - 24)) & 0x7fffff;
3013 :
3014 1284464 : switch (r->cl)
3015 : {
3016 : case rvc_zero:
3017 : break;
3018 :
3019 7646 : case rvc_inf:
3020 7646 : if (fmt->has_inf)
3021 7646 : image |= 255 << 23;
3022 : else
3023 0 : image |= 0x7fffffff;
3024 : break;
3025 :
3026 38666 : case rvc_nan:
3027 38666 : if (fmt->has_nans)
3028 : {
3029 38666 : if (r->canonical)
3030 5243 : sig = (fmt->canonical_nan_lsbs_set ? (1 << 22) - 1 : 0);
3031 38666 : if (r->signalling == fmt->qnan_msb_set)
3032 272 : sig &= ~(1 << 22);
3033 : else
3034 38394 : sig |= 1 << 22;
3035 38666 : if (sig == 0)
3036 225 : sig = 1 << 21;
3037 :
3038 38666 : image |= 255 << 23;
3039 38666 : image |= sig;
3040 : }
3041 : else
3042 0 : image |= 0x7fffffff;
3043 : break;
3044 :
3045 998499 : case rvc_normal:
3046 : /* Recall that IEEE numbers are interpreted as 1.F x 2**exp,
3047 : whereas the intermediate representation is 0.F x 2**exp.
3048 : Which means we're off by one. */
3049 998499 : if (real_isdenormal (r))
3050 : exp = 0;
3051 : else
3052 991262 : exp = REAL_EXP (r) + 127 - 1;
3053 998499 : image |= exp << 23;
3054 998499 : image |= sig;
3055 998499 : break;
3056 :
3057 0 : default:
3058 0 : gcc_unreachable ();
3059 : }
3060 :
3061 1284464 : buf[0] = image;
3062 1284464 : }
3063 :
3064 : static void
3065 150006 : decode_ieee_single (const struct real_format *fmt, REAL_VALUE_TYPE *r,
3066 : const long *buf)
3067 : {
3068 150006 : unsigned long image = buf[0] & 0xffffffff;
3069 150006 : bool sign = (image >> 31) & 1;
3070 150006 : int exp = (image >> 23) & 0xff;
3071 :
3072 150006 : memset (r, 0, sizeof (*r));
3073 150006 : image <<= HOST_BITS_PER_LONG - 24;
3074 150006 : image &= ~SIG_MSB;
3075 :
3076 150006 : if (exp == 0)
3077 : {
3078 88847 : if (image && fmt->has_denorm)
3079 : {
3080 5502 : r->cl = rvc_normal;
3081 5502 : r->sign = sign;
3082 5502 : SET_REAL_EXP (r, -126);
3083 5502 : r->sig[SIGSZ-1] = image << 1;
3084 5502 : normalize (r);
3085 : }
3086 83345 : else if (fmt->has_signed_zero)
3087 83345 : r->sign = sign;
3088 : }
3089 61159 : else if (exp == 255 && (fmt->has_nans || fmt->has_inf))
3090 : {
3091 23394 : if (image)
3092 : {
3093 22801 : r->cl = rvc_nan;
3094 22801 : r->sign = sign;
3095 22801 : r->signalling = (((image >> (HOST_BITS_PER_LONG - 2)) & 1)
3096 22801 : ^ fmt->qnan_msb_set);
3097 22801 : r->sig[SIGSZ-1] = image;
3098 : }
3099 : else
3100 : {
3101 593 : r->cl = rvc_inf;
3102 593 : r->sign = sign;
3103 : }
3104 : }
3105 : else
3106 : {
3107 37765 : r->cl = rvc_normal;
3108 37765 : r->sign = sign;
3109 37765 : SET_REAL_EXP (r, exp - 127 + 1);
3110 37765 : r->sig[SIGSZ-1] = image | SIG_MSB;
3111 : }
3112 150006 : }
3113 :
3114 : const struct real_format ieee_single_format =
3115 : {
3116 : encode_ieee_single,
3117 : decode_ieee_single,
3118 : 2,
3119 : 24,
3120 : 24,
3121 : -125,
3122 : 128,
3123 : 31,
3124 : 31,
3125 : 32,
3126 : false,
3127 : true,
3128 : true,
3129 : true,
3130 : true,
3131 : true,
3132 : true,
3133 : false,
3134 : "ieee_single"
3135 : };
3136 :
3137 : const struct real_format mips_single_format =
3138 : {
3139 : encode_ieee_single,
3140 : decode_ieee_single,
3141 : 2,
3142 : 24,
3143 : 24,
3144 : -125,
3145 : 128,
3146 : 31,
3147 : 31,
3148 : 32,
3149 : false,
3150 : true,
3151 : true,
3152 : true,
3153 : true,
3154 : true,
3155 : false,
3156 : true,
3157 : "mips_single"
3158 : };
3159 :
3160 : const struct real_format motorola_single_format =
3161 : {
3162 : encode_ieee_single,
3163 : decode_ieee_single,
3164 : 2,
3165 : 24,
3166 : 24,
3167 : -125,
3168 : 128,
3169 : 31,
3170 : 31,
3171 : 32,
3172 : false,
3173 : true,
3174 : true,
3175 : true,
3176 : true,
3177 : true,
3178 : true,
3179 : true,
3180 : "motorola_single"
3181 : };
3182 :
3183 : /* SPU Single Precision (Extended-Range Mode) format is the same as IEEE
3184 : single precision with the following differences:
3185 : - Infinities are not supported. Instead MAX_FLOAT or MIN_FLOAT
3186 : are generated.
3187 : - NaNs are not supported.
3188 : - The range of non-zero numbers in binary is
3189 : (001)[1.]000...000 to (255)[1.]111...111.
3190 : - Denormals can be represented, but are treated as +0.0 when
3191 : used as an operand and are never generated as a result.
3192 : - -0.0 can be represented, but a zero result is always +0.0.
3193 : - the only supported rounding mode is truncation (towards zero). */
3194 : const struct real_format spu_single_format =
3195 : {
3196 : encode_ieee_single,
3197 : decode_ieee_single,
3198 : 2,
3199 : 24,
3200 : 24,
3201 : -125,
3202 : 129,
3203 : 31,
3204 : 31,
3205 : 0,
3206 : true,
3207 : false,
3208 : false,
3209 : false,
3210 : true,
3211 : true,
3212 : false,
3213 : false,
3214 : "spu_single"
3215 : };
3216 :
3217 : /* IEEE double-precision format. */
3218 :
3219 : static void encode_ieee_double (const struct real_format *fmt,
3220 : long *, const REAL_VALUE_TYPE *);
3221 : static void decode_ieee_double (const struct real_format *,
3222 : REAL_VALUE_TYPE *, const long *);
3223 :
3224 : static void
3225 1145973 : encode_ieee_double (const struct real_format *fmt, long *buf,
3226 : const REAL_VALUE_TYPE *r)
3227 : {
3228 1145973 : unsigned long image_lo, image_hi, sig_lo, sig_hi, exp;
3229 1145973 : unsigned long sign = r->sign;
3230 :
3231 1145973 : image_hi = sign << 31;
3232 1145973 : image_lo = 0;
3233 :
3234 1145973 : if (HOST_BITS_PER_LONG == 64)
3235 : {
3236 1145973 : sig_hi = r->sig[SIGSZ-1];
3237 1145973 : sig_lo = (sig_hi >> (64 - 53)) & 0xffffffff;
3238 1145973 : sig_hi = (sig_hi >> (64 - 53 + 1) >> 31) & 0xfffff;
3239 : }
3240 : else
3241 : {
3242 : sig_hi = r->sig[SIGSZ-1];
3243 : sig_lo = r->sig[SIGSZ-2];
3244 : sig_lo = (sig_hi << 21) | (sig_lo >> 11);
3245 : sig_hi = (sig_hi >> 11) & 0xfffff;
3246 : }
3247 :
3248 1145973 : switch (r->cl)
3249 : {
3250 : case rvc_zero:
3251 : break;
3252 :
3253 10229 : case rvc_inf:
3254 10229 : if (fmt->has_inf)
3255 10229 : image_hi |= 2047 << 20;
3256 : else
3257 : {
3258 0 : image_hi |= 0x7fffffff;
3259 0 : image_lo = 0xffffffff;
3260 : }
3261 : break;
3262 :
3263 40377 : case rvc_nan:
3264 40377 : if (fmt->has_nans)
3265 : {
3266 40377 : if (r->canonical)
3267 : {
3268 2402 : if (fmt->canonical_nan_lsbs_set)
3269 : {
3270 : sig_hi = (1 << 19) - 1;
3271 : sig_lo = 0xffffffff;
3272 : }
3273 : else
3274 : {
3275 2402 : sig_hi = 0;
3276 2402 : sig_lo = 0;
3277 : }
3278 : }
3279 40377 : if (r->signalling == fmt->qnan_msb_set)
3280 337 : sig_hi &= ~(1 << 19);
3281 : else
3282 40040 : sig_hi |= 1 << 19;
3283 40377 : if (sig_hi == 0 && sig_lo == 0)
3284 295 : sig_hi = 1 << 18;
3285 :
3286 40377 : image_hi |= 2047 << 20;
3287 40377 : image_hi |= sig_hi;
3288 40377 : image_lo = sig_lo;
3289 : }
3290 : else
3291 : {
3292 0 : image_hi |= 0x7fffffff;
3293 0 : image_lo = 0xffffffff;
3294 : }
3295 : break;
3296 :
3297 800260 : case rvc_normal:
3298 : /* Recall that IEEE numbers are interpreted as 1.F x 2**exp,
3299 : whereas the intermediate representation is 0.F x 2**exp.
3300 : Which means we're off by one. */
3301 800260 : if (real_isdenormal (r))
3302 : exp = 0;
3303 : else
3304 795217 : exp = REAL_EXP (r) + 1023 - 1;
3305 800260 : image_hi |= exp << 20;
3306 800260 : image_hi |= sig_hi;
3307 800260 : image_lo = sig_lo;
3308 800260 : break;
3309 :
3310 0 : default:
3311 0 : gcc_unreachable ();
3312 : }
3313 :
3314 1145973 : if (FLOAT_WORDS_BIG_ENDIAN)
3315 : buf[0] = image_hi, buf[1] = image_lo;
3316 : else
3317 1145973 : buf[0] = image_lo, buf[1] = image_hi;
3318 1145973 : }
3319 :
3320 : static void
3321 109311 : decode_ieee_double (const struct real_format *fmt, REAL_VALUE_TYPE *r,
3322 : const long *buf)
3323 : {
3324 109311 : unsigned long image_hi, image_lo;
3325 109311 : bool sign;
3326 109311 : int exp;
3327 :
3328 109311 : if (FLOAT_WORDS_BIG_ENDIAN)
3329 : image_hi = buf[0], image_lo = buf[1];
3330 : else
3331 109311 : image_lo = buf[0], image_hi = buf[1];
3332 109311 : image_lo &= 0xffffffff;
3333 109311 : image_hi &= 0xffffffff;
3334 :
3335 109311 : sign = (image_hi >> 31) & 1;
3336 109311 : exp = (image_hi >> 20) & 0x7ff;
3337 :
3338 109311 : memset (r, 0, sizeof (*r));
3339 :
3340 109311 : image_hi <<= 32 - 21;
3341 109311 : image_hi |= image_lo >> 21;
3342 109311 : image_hi &= 0x7fffffff;
3343 109311 : image_lo <<= 32 - 21;
3344 :
3345 109311 : if (exp == 0)
3346 : {
3347 52801 : if ((image_hi || image_lo) && fmt->has_denorm)
3348 : {
3349 2995 : r->cl = rvc_normal;
3350 2995 : r->sign = sign;
3351 2995 : SET_REAL_EXP (r, -1022);
3352 2995 : if (HOST_BITS_PER_LONG == 32)
3353 : {
3354 : image_hi = (image_hi << 1) | (image_lo >> 31);
3355 : image_lo <<= 1;
3356 : r->sig[SIGSZ-1] = image_hi;
3357 : r->sig[SIGSZ-2] = image_lo;
3358 : }
3359 : else
3360 : {
3361 2995 : image_hi = (image_hi << 31 << 2) | (image_lo << 1);
3362 2995 : r->sig[SIGSZ-1] = image_hi;
3363 : }
3364 2995 : normalize (r);
3365 : }
3366 49806 : else if (fmt->has_signed_zero)
3367 49806 : r->sign = sign;
3368 : }
3369 56510 : else if (exp == 2047 && (fmt->has_nans || fmt->has_inf))
3370 : {
3371 29898 : if (image_hi || image_lo)
3372 : {
3373 28492 : r->cl = rvc_nan;
3374 28492 : r->sign = sign;
3375 28492 : r->signalling = ((image_hi >> 30) & 1) ^ fmt->qnan_msb_set;
3376 28492 : if (HOST_BITS_PER_LONG == 32)
3377 : {
3378 : r->sig[SIGSZ-1] = image_hi;
3379 : r->sig[SIGSZ-2] = image_lo;
3380 : }
3381 : else
3382 28492 : r->sig[SIGSZ-1] = (image_hi << 31 << 1) | image_lo;
3383 : }
3384 : else
3385 : {
3386 1406 : r->cl = rvc_inf;
3387 1406 : r->sign = sign;
3388 : }
3389 : }
3390 : else
3391 : {
3392 26612 : r->cl = rvc_normal;
3393 26612 : r->sign = sign;
3394 26612 : SET_REAL_EXP (r, exp - 1023 + 1);
3395 26612 : if (HOST_BITS_PER_LONG == 32)
3396 : {
3397 : r->sig[SIGSZ-1] = image_hi | SIG_MSB;
3398 : r->sig[SIGSZ-2] = image_lo;
3399 : }
3400 : else
3401 26612 : r->sig[SIGSZ-1] = (image_hi << 31 << 1) | image_lo | SIG_MSB;
3402 : }
3403 109311 : }
3404 :
3405 : const struct real_format ieee_double_format =
3406 : {
3407 : encode_ieee_double,
3408 : decode_ieee_double,
3409 : 2,
3410 : 53,
3411 : 53,
3412 : -1021,
3413 : 1024,
3414 : 63,
3415 : 63,
3416 : 64,
3417 : false,
3418 : true,
3419 : true,
3420 : true,
3421 : true,
3422 : true,
3423 : true,
3424 : false,
3425 : "ieee_double"
3426 : };
3427 :
3428 : const struct real_format mips_double_format =
3429 : {
3430 : encode_ieee_double,
3431 : decode_ieee_double,
3432 : 2,
3433 : 53,
3434 : 53,
3435 : -1021,
3436 : 1024,
3437 : 63,
3438 : 63,
3439 : 64,
3440 : false,
3441 : true,
3442 : true,
3443 : true,
3444 : true,
3445 : true,
3446 : false,
3447 : true,
3448 : "mips_double"
3449 : };
3450 :
3451 : const struct real_format motorola_double_format =
3452 : {
3453 : encode_ieee_double,
3454 : decode_ieee_double,
3455 : 2,
3456 : 53,
3457 : 53,
3458 : -1021,
3459 : 1024,
3460 : 63,
3461 : 63,
3462 : 64,
3463 : false,
3464 : true,
3465 : true,
3466 : true,
3467 : true,
3468 : true,
3469 : true,
3470 : true,
3471 : "motorola_double"
3472 : };
3473 :
3474 : /* IEEE extended real format. This comes in three flavors: Intel's as
3475 : a 12 byte image, Intel's as a 16 byte image, and Motorola's. Intel
3476 : 12- and 16-byte images may be big- or little endian; Motorola's is
3477 : always big endian. */
3478 :
3479 : /* Helper subroutine which converts from the internal format to the
3480 : 12-byte little-endian Intel format. Functions below adjust this
3481 : for the other possible formats. */
3482 : static void
3483 53744 : encode_ieee_extended (const struct real_format *fmt, long *buf,
3484 : const REAL_VALUE_TYPE *r)
3485 : {
3486 53744 : unsigned long image_hi, sig_hi, sig_lo;
3487 :
3488 53744 : image_hi = r->sign << 15;
3489 53744 : sig_hi = sig_lo = 0;
3490 :
3491 53744 : switch (r->cl)
3492 : {
3493 : case rvc_zero:
3494 : break;
3495 :
3496 1053 : case rvc_inf:
3497 1053 : if (fmt->has_inf)
3498 : {
3499 1053 : image_hi |= 32767;
3500 :
3501 : /* Intel requires the explicit integer bit to be set, otherwise
3502 : it considers the value a "pseudo-infinity". Motorola docs
3503 : say it doesn't care. */
3504 1053 : sig_hi = 0x80000000;
3505 : }
3506 : else
3507 : {
3508 0 : image_hi |= 32767;
3509 0 : sig_lo = sig_hi = 0xffffffff;
3510 : }
3511 : break;
3512 :
3513 2546 : case rvc_nan:
3514 2546 : if (fmt->has_nans)
3515 : {
3516 2546 : image_hi |= 32767;
3517 2546 : if (r->canonical)
3518 : {
3519 885 : if (fmt->canonical_nan_lsbs_set)
3520 : {
3521 0 : sig_hi = (1 << 30) - 1;
3522 0 : sig_lo = 0xffffffff;
3523 : }
3524 : }
3525 1661 : else if (HOST_BITS_PER_LONG == 32)
3526 : {
3527 : sig_hi = r->sig[SIGSZ-1];
3528 : sig_lo = r->sig[SIGSZ-2];
3529 : }
3530 : else
3531 : {
3532 1661 : sig_lo = r->sig[SIGSZ-1];
3533 1661 : sig_hi = sig_lo >> 31 >> 1;
3534 1661 : sig_lo &= 0xffffffff;
3535 : }
3536 2546 : if (r->signalling == fmt->qnan_msb_set)
3537 156 : sig_hi &= ~(1 << 30);
3538 : else
3539 2390 : sig_hi |= 1 << 30;
3540 2546 : if ((sig_hi & 0x7fffffff) == 0 && sig_lo == 0)
3541 147 : sig_hi = 1 << 29;
3542 :
3543 : /* Intel requires the explicit integer bit to be set, otherwise
3544 : it considers the value a "pseudo-nan". Motorola docs say it
3545 : doesn't care. */
3546 2546 : sig_hi |= 0x80000000;
3547 : }
3548 : else
3549 : {
3550 0 : image_hi |= 32767;
3551 0 : sig_lo = sig_hi = 0xffffffff;
3552 : }
3553 : break;
3554 :
3555 39292 : case rvc_normal:
3556 39292 : {
3557 39292 : int exp = REAL_EXP (r);
3558 :
3559 : /* Recall that IEEE numbers are interpreted as 1.F x 2**exp,
3560 : whereas the intermediate representation is 0.F x 2**exp.
3561 : Which means we're off by one.
3562 :
3563 : Except for Motorola, which consider exp=0 and explicit
3564 : integer bit set to continue to be normalized. In theory
3565 : this discrepancy has been taken care of by the difference
3566 : in fmt->emin in round_for_format. */
3567 :
3568 39292 : if (real_isdenormal (r))
3569 : exp = 0;
3570 : else
3571 : {
3572 39001 : exp += 16383 - 1;
3573 39001 : gcc_assert (exp >= 0);
3574 : }
3575 39292 : image_hi |= exp;
3576 :
3577 39292 : if (HOST_BITS_PER_LONG == 32)
3578 : {
3579 : sig_hi = r->sig[SIGSZ-1];
3580 : sig_lo = r->sig[SIGSZ-2];
3581 : }
3582 : else
3583 : {
3584 39292 : sig_lo = r->sig[SIGSZ-1];
3585 39292 : sig_hi = sig_lo >> 31 >> 1;
3586 39292 : sig_lo &= 0xffffffff;
3587 : }
3588 : }
3589 39292 : break;
3590 :
3591 0 : default:
3592 0 : gcc_unreachable ();
3593 : }
3594 :
3595 53744 : buf[0] = sig_lo, buf[1] = sig_hi, buf[2] = image_hi;
3596 53744 : }
3597 :
3598 : /* Convert from the internal format to the 12-byte Motorola format
3599 : for an IEEE extended real. */
3600 : static void
3601 0 : encode_ieee_extended_motorola (const struct real_format *fmt, long *buf,
3602 : const REAL_VALUE_TYPE *r)
3603 : {
3604 0 : long intermed[3];
3605 0 : encode_ieee_extended (fmt, intermed, r);
3606 :
3607 0 : if (r->cl == rvc_inf)
3608 : /* For infinity clear the explicit integer bit again, so that the
3609 : format matches the canonical infinity generated by the FPU. */
3610 0 : intermed[1] = 0;
3611 :
3612 : /* Motorola chips are assumed always to be big-endian. Also, the
3613 : padding in a Motorola extended real goes between the exponent and
3614 : the mantissa. At this point the mantissa is entirely within
3615 : elements 0 and 1 of intermed, and the exponent entirely within
3616 : element 2, so all we have to do is swap the order around, and
3617 : shift element 2 left 16 bits. */
3618 0 : buf[0] = intermed[2] << 16;
3619 0 : buf[1] = intermed[1];
3620 0 : buf[2] = intermed[0];
3621 0 : }
3622 :
3623 : /* Convert from the internal format to the 12-byte Intel format for
3624 : an IEEE extended real. */
3625 : static void
3626 53744 : encode_ieee_extended_intel_96 (const struct real_format *fmt, long *buf,
3627 : const REAL_VALUE_TYPE *r)
3628 : {
3629 53744 : if (FLOAT_WORDS_BIG_ENDIAN)
3630 : {
3631 : /* All the padding in an Intel-format extended real goes at the high
3632 : end, which in this case is after the mantissa, not the exponent.
3633 : Therefore we must shift everything down 16 bits. */
3634 : long intermed[3];
3635 : encode_ieee_extended (fmt, intermed, r);
3636 : buf[0] = ((intermed[2] << 16) | ((unsigned long)(intermed[1] & 0xFFFF0000) >> 16));
3637 : buf[1] = ((intermed[1] << 16) | ((unsigned long)(intermed[0] & 0xFFFF0000) >> 16));
3638 : buf[2] = (intermed[0] << 16);
3639 : }
3640 : else
3641 : /* encode_ieee_extended produces what we want directly. */
3642 3395 : encode_ieee_extended (fmt, buf, r);
3643 3395 : }
3644 :
3645 : /* Convert from the internal format to the 16-byte Intel format for
3646 : an IEEE extended real. */
3647 : static void
3648 50349 : encode_ieee_extended_intel_128 (const struct real_format *fmt, long *buf,
3649 : const REAL_VALUE_TYPE *r)
3650 : {
3651 : /* All the padding in an Intel-format extended real goes at the high end. */
3652 50349 : encode_ieee_extended_intel_96 (fmt, buf, r);
3653 50349 : buf[3] = 0;
3654 50349 : }
3655 :
3656 : /* As above, we have a helper function which converts from 12-byte
3657 : little-endian Intel format to internal format. Functions below
3658 : adjust for the other possible formats. */
3659 : static void
3660 3061 : decode_ieee_extended (const struct real_format *fmt, REAL_VALUE_TYPE *r,
3661 : const long *buf)
3662 : {
3663 3061 : unsigned long image_hi, sig_hi, sig_lo;
3664 3061 : bool sign;
3665 3061 : int exp;
3666 :
3667 3061 : sig_lo = buf[0], sig_hi = buf[1], image_hi = buf[2];
3668 3061 : sig_lo &= 0xffffffff;
3669 3061 : sig_hi &= 0xffffffff;
3670 3061 : image_hi &= 0xffffffff;
3671 :
3672 3061 : sign = (image_hi >> 15) & 1;
3673 3061 : exp = image_hi & 0x7fff;
3674 :
3675 3061 : memset (r, 0, sizeof (*r));
3676 :
3677 3061 : if (exp == 0)
3678 : {
3679 305 : if ((sig_hi || sig_lo) && fmt->has_denorm)
3680 : {
3681 189 : r->cl = rvc_normal;
3682 189 : r->sign = sign;
3683 :
3684 : /* When the IEEE format contains a hidden bit, we know that
3685 : it's zero at this point, and so shift up the significand
3686 : and decrease the exponent to match. In this case, Motorola
3687 : defines the explicit integer bit to be valid, so we don't
3688 : know whether the msb is set or not. */
3689 189 : SET_REAL_EXP (r, fmt->emin);
3690 189 : if (HOST_BITS_PER_LONG == 32)
3691 : {
3692 : r->sig[SIGSZ-1] = sig_hi;
3693 : r->sig[SIGSZ-2] = sig_lo;
3694 : }
3695 : else
3696 189 : r->sig[SIGSZ-1] = (sig_hi << 31 << 1) | sig_lo;
3697 :
3698 189 : normalize (r);
3699 : }
3700 116 : else if (fmt->has_signed_zero)
3701 116 : r->sign = sign;
3702 : }
3703 2756 : else if (exp == 32767 && (fmt->has_nans || fmt->has_inf))
3704 : {
3705 : /* See above re "pseudo-infinities" and "pseudo-nans".
3706 : Short summary is that the MSB will likely always be
3707 : set, and that we don't care about it. */
3708 1691 : sig_hi &= 0x7fffffff;
3709 :
3710 1691 : if (sig_hi || sig_lo)
3711 : {
3712 1619 : r->cl = rvc_nan;
3713 1619 : r->sign = sign;
3714 1619 : r->signalling = ((sig_hi >> 30) & 1) ^ fmt->qnan_msb_set;
3715 1619 : if (HOST_BITS_PER_LONG == 32)
3716 : {
3717 : r->sig[SIGSZ-1] = sig_hi;
3718 : r->sig[SIGSZ-2] = sig_lo;
3719 : }
3720 : else
3721 1619 : r->sig[SIGSZ-1] = (sig_hi << 31 << 1) | sig_lo;
3722 : }
3723 : else
3724 : {
3725 72 : r->cl = rvc_inf;
3726 72 : r->sign = sign;
3727 : }
3728 : }
3729 : else
3730 : {
3731 1065 : r->cl = rvc_normal;
3732 1065 : r->sign = sign;
3733 1065 : SET_REAL_EXP (r, exp - 16383 + 1);
3734 1065 : if (HOST_BITS_PER_LONG == 32)
3735 : {
3736 : r->sig[SIGSZ-1] = sig_hi;
3737 : r->sig[SIGSZ-2] = sig_lo;
3738 : }
3739 : else
3740 1065 : r->sig[SIGSZ-1] = (sig_hi << 31 << 1) | sig_lo;
3741 : }
3742 3061 : }
3743 :
3744 : /* Convert from the internal format to the 12-byte Motorola format
3745 : for an IEEE extended real. */
3746 : static void
3747 0 : decode_ieee_extended_motorola (const struct real_format *fmt, REAL_VALUE_TYPE *r,
3748 : const long *buf)
3749 : {
3750 0 : long intermed[3];
3751 :
3752 : /* Motorola chips are assumed always to be big-endian. Also, the
3753 : padding in a Motorola extended real goes between the exponent and
3754 : the mantissa; remove it. */
3755 0 : intermed[0] = buf[2];
3756 0 : intermed[1] = buf[1];
3757 0 : intermed[2] = (unsigned long)buf[0] >> 16;
3758 :
3759 0 : decode_ieee_extended (fmt, r, intermed);
3760 0 : }
3761 :
3762 : /* Convert from the internal format to the 12-byte Intel format for
3763 : an IEEE extended real. */
3764 : static void
3765 3061 : decode_ieee_extended_intel_96 (const struct real_format *fmt, REAL_VALUE_TYPE *r,
3766 : const long *buf)
3767 : {
3768 3061 : if (FLOAT_WORDS_BIG_ENDIAN)
3769 : {
3770 : /* All the padding in an Intel-format extended real goes at the high
3771 : end, which in this case is after the mantissa, not the exponent.
3772 : Therefore we must shift everything up 16 bits. */
3773 : long intermed[3];
3774 :
3775 : intermed[0] = (((unsigned long)buf[2] >> 16) | (buf[1] << 16));
3776 : intermed[1] = (((unsigned long)buf[1] >> 16) | (buf[0] << 16));
3777 : intermed[2] = ((unsigned long)buf[0] >> 16);
3778 :
3779 : decode_ieee_extended (fmt, r, intermed);
3780 : }
3781 : else
3782 : /* decode_ieee_extended produces what we want directly. */
3783 0 : decode_ieee_extended (fmt, r, buf);
3784 0 : }
3785 :
3786 : /* Convert from the internal format to the 16-byte Intel format for
3787 : an IEEE extended real. */
3788 : static void
3789 3061 : decode_ieee_extended_intel_128 (const struct real_format *fmt, REAL_VALUE_TYPE *r,
3790 : const long *buf)
3791 : {
3792 : /* All the padding in an Intel-format extended real goes at the high end. */
3793 3061 : decode_ieee_extended_intel_96 (fmt, r, buf);
3794 3061 : }
3795 :
3796 : const struct real_format ieee_extended_motorola_format =
3797 : {
3798 : encode_ieee_extended_motorola,
3799 : decode_ieee_extended_motorola,
3800 : 2,
3801 : 64,
3802 : 64,
3803 : -16382,
3804 : 16384,
3805 : 95,
3806 : 95,
3807 : 0,
3808 : false,
3809 : true,
3810 : true,
3811 : true,
3812 : true,
3813 : true,
3814 : true,
3815 : true,
3816 : "ieee_extended_motorola"
3817 : };
3818 :
3819 : const struct real_format ieee_extended_intel_96_format =
3820 : {
3821 : encode_ieee_extended_intel_96,
3822 : decode_ieee_extended_intel_96,
3823 : 2,
3824 : 64,
3825 : 64,
3826 : -16381,
3827 : 16384,
3828 : 79,
3829 : 79,
3830 : 65,
3831 : false,
3832 : true,
3833 : true,
3834 : true,
3835 : true,
3836 : true,
3837 : true,
3838 : false,
3839 : "ieee_extended_intel_96"
3840 : };
3841 :
3842 : const struct real_format ieee_extended_intel_128_format =
3843 : {
3844 : encode_ieee_extended_intel_128,
3845 : decode_ieee_extended_intel_128,
3846 : 2,
3847 : 64,
3848 : 64,
3849 : -16381,
3850 : 16384,
3851 : 79,
3852 : 79,
3853 : 65,
3854 : false,
3855 : true,
3856 : true,
3857 : true,
3858 : true,
3859 : true,
3860 : true,
3861 : false,
3862 : "ieee_extended_intel_128"
3863 : };
3864 :
3865 : /* The following caters to i386 systems that set the rounding precision
3866 : to 53 bits instead of 64, e.g. FreeBSD. */
3867 : const struct real_format ieee_extended_intel_96_round_53_format =
3868 : {
3869 : encode_ieee_extended_intel_96,
3870 : decode_ieee_extended_intel_96,
3871 : 2,
3872 : 53,
3873 : 53,
3874 : -16381,
3875 : 16384,
3876 : 79,
3877 : 79,
3878 : 33,
3879 : false,
3880 : true,
3881 : true,
3882 : true,
3883 : true,
3884 : true,
3885 : true,
3886 : false,
3887 : "ieee_extended_intel_96_round_53"
3888 : };
3889 :
3890 : /* IBM 128-bit extended precision format: a pair of IEEE double precision
3891 : numbers whose sum is equal to the extended precision value. The number
3892 : with greater magnitude is first. This format has the same magnitude
3893 : range as an IEEE double precision value, but effectively 106 bits of
3894 : significand precision. Infinity and NaN are represented by their IEEE
3895 : double precision value stored in the first number, the second number is
3896 : +0.0 or -0.0 for Infinity and don't-care for NaN. */
3897 :
3898 : static void encode_ibm_extended (const struct real_format *fmt,
3899 : long *, const REAL_VALUE_TYPE *);
3900 : static void decode_ibm_extended (const struct real_format *,
3901 : REAL_VALUE_TYPE *, const long *);
3902 :
3903 : static void
3904 0 : encode_ibm_extended (const struct real_format *fmt, long *buf,
3905 : const REAL_VALUE_TYPE *r)
3906 : {
3907 0 : REAL_VALUE_TYPE u, normr, v;
3908 0 : const struct real_format *base_fmt;
3909 :
3910 0 : base_fmt = fmt->qnan_msb_set ? &ieee_double_format : &mips_double_format;
3911 :
3912 : /* Renormalize R before doing any arithmetic on it. */
3913 0 : normr = *r;
3914 0 : if (normr.cl == rvc_normal)
3915 0 : normalize (&normr);
3916 :
3917 : /* u = IEEE double precision portion of significand. */
3918 0 : u = normr;
3919 0 : round_for_format (base_fmt, &u);
3920 0 : encode_ieee_double (base_fmt, &buf[0], &u);
3921 :
3922 0 : if (u.cl == rvc_normal)
3923 : {
3924 0 : do_add (&v, &normr, &u, 1);
3925 : /* Call round_for_format since we might need to denormalize. */
3926 0 : round_for_format (base_fmt, &v);
3927 0 : encode_ieee_double (base_fmt, &buf[2], &v);
3928 : }
3929 : else
3930 : {
3931 : /* Inf, NaN, 0 are all representable as doubles, so the
3932 : least-significant part can be 0.0. */
3933 0 : buf[2] = 0;
3934 0 : buf[3] = 0;
3935 : }
3936 0 : }
3937 :
3938 : static void
3939 0 : decode_ibm_extended (const struct real_format *fmt ATTRIBUTE_UNUSED, REAL_VALUE_TYPE *r,
3940 : const long *buf)
3941 : {
3942 0 : REAL_VALUE_TYPE u, v;
3943 0 : const struct real_format *base_fmt;
3944 :
3945 0 : base_fmt = fmt->qnan_msb_set ? &ieee_double_format : &mips_double_format;
3946 0 : decode_ieee_double (base_fmt, &u, &buf[0]);
3947 :
3948 0 : if (u.cl != rvc_zero && u.cl != rvc_inf && u.cl != rvc_nan)
3949 : {
3950 0 : decode_ieee_double (base_fmt, &v, &buf[2]);
3951 0 : do_add (r, &u, &v, 0);
3952 : }
3953 : else
3954 0 : *r = u;
3955 0 : }
3956 :
3957 : const struct real_format ibm_extended_format =
3958 : {
3959 : encode_ibm_extended,
3960 : decode_ibm_extended,
3961 : 2,
3962 : 53 + 53,
3963 : 53,
3964 : -1021 + 53,
3965 : 1024,
3966 : 127,
3967 : -1,
3968 : 0,
3969 : false,
3970 : true,
3971 : true,
3972 : true,
3973 : true,
3974 : true,
3975 : true,
3976 : false,
3977 : "ibm_extended"
3978 : };
3979 :
3980 : const struct real_format mips_extended_format =
3981 : {
3982 : encode_ibm_extended,
3983 : decode_ibm_extended,
3984 : 2,
3985 : 53 + 53,
3986 : 53,
3987 : -1021 + 53,
3988 : 1024,
3989 : 127,
3990 : -1,
3991 : 0,
3992 : false,
3993 : true,
3994 : true,
3995 : true,
3996 : true,
3997 : true,
3998 : false,
3999 : true,
4000 : "mips_extended"
4001 : };
4002 :
4003 :
4004 : /* IEEE quad precision format. */
4005 :
4006 : static void encode_ieee_quad (const struct real_format *fmt,
4007 : long *, const REAL_VALUE_TYPE *);
4008 : static void decode_ieee_quad (const struct real_format *,
4009 : REAL_VALUE_TYPE *, const long *);
4010 :
4011 : static void
4012 206322 : encode_ieee_quad (const struct real_format *fmt, long *buf,
4013 : const REAL_VALUE_TYPE *r)
4014 : {
4015 206322 : unsigned long image3, image2, image1, image0, exp;
4016 206322 : unsigned long sign = r->sign;
4017 206322 : REAL_VALUE_TYPE u;
4018 :
4019 206322 : image3 = sign << 31;
4020 206322 : image2 = 0;
4021 206322 : image1 = 0;
4022 206322 : image0 = 0;
4023 :
4024 206322 : rshift_significand (&u, r, SIGNIFICAND_BITS - 113);
4025 :
4026 206322 : switch (r->cl)
4027 : {
4028 : case rvc_zero:
4029 : break;
4030 :
4031 1011 : case rvc_inf:
4032 1011 : if (fmt->has_inf)
4033 1011 : image3 |= 32767 << 16;
4034 : else
4035 : {
4036 0 : image3 |= 0x7fffffff;
4037 0 : image2 = 0xffffffff;
4038 0 : image1 = 0xffffffff;
4039 0 : image0 = 0xffffffff;
4040 : }
4041 : break;
4042 :
4043 3278 : case rvc_nan:
4044 3278 : if (fmt->has_nans)
4045 : {
4046 3278 : image3 |= 32767 << 16;
4047 :
4048 3278 : if (r->canonical)
4049 : {
4050 560 : if (fmt->canonical_nan_lsbs_set)
4051 : {
4052 0 : image3 |= 0x7fff;
4053 0 : image2 = image1 = image0 = 0xffffffff;
4054 : }
4055 : }
4056 2718 : else if (HOST_BITS_PER_LONG == 32)
4057 : {
4058 : image0 = u.sig[0];
4059 : image1 = u.sig[1];
4060 : image2 = u.sig[2];
4061 : image3 |= u.sig[3] & 0xffff;
4062 : }
4063 : else
4064 : {
4065 2718 : image0 = u.sig[0];
4066 2718 : image1 = image0 >> 31 >> 1;
4067 2718 : image2 = u.sig[1];
4068 2718 : image3 |= (image2 >> 31 >> 1) & 0xffff;
4069 2718 : image0 &= 0xffffffff;
4070 2718 : image2 &= 0xffffffff;
4071 : }
4072 3278 : if (r->signalling == fmt->qnan_msb_set)
4073 133 : image3 &= ~0x8000;
4074 : else
4075 3145 : image3 |= 0x8000;
4076 3278 : if (((image3 & 0xffff) | image2 | image1 | image0) == 0)
4077 108 : image3 |= 0x4000;
4078 : }
4079 : else
4080 : {
4081 0 : image3 |= 0x7fffffff;
4082 0 : image2 = 0xffffffff;
4083 0 : image1 = 0xffffffff;
4084 0 : image0 = 0xffffffff;
4085 : }
4086 : break;
4087 :
4088 197269 : case rvc_normal:
4089 : /* Recall that IEEE numbers are interpreted as 1.F x 2**exp,
4090 : whereas the intermediate representation is 0.F x 2**exp.
4091 : Which means we're off by one. */
4092 197269 : if (real_isdenormal (r))
4093 : exp = 0;
4094 : else
4095 196491 : exp = REAL_EXP (r) + 16383 - 1;
4096 197269 : image3 |= exp << 16;
4097 :
4098 197269 : if (HOST_BITS_PER_LONG == 32)
4099 : {
4100 : image0 = u.sig[0];
4101 : image1 = u.sig[1];
4102 : image2 = u.sig[2];
4103 : image3 |= u.sig[3] & 0xffff;
4104 : }
4105 : else
4106 : {
4107 197269 : image0 = u.sig[0];
4108 197269 : image1 = image0 >> 31 >> 1;
4109 197269 : image2 = u.sig[1];
4110 197269 : image3 |= (image2 >> 31 >> 1) & 0xffff;
4111 197269 : image0 &= 0xffffffff;
4112 197269 : image2 &= 0xffffffff;
4113 : }
4114 197269 : break;
4115 :
4116 0 : default:
4117 0 : gcc_unreachable ();
4118 : }
4119 :
4120 206322 : if (FLOAT_WORDS_BIG_ENDIAN)
4121 : {
4122 : buf[0] = image3;
4123 : buf[1] = image2;
4124 : buf[2] = image1;
4125 : buf[3] = image0;
4126 : }
4127 : else
4128 : {
4129 206322 : buf[0] = image0;
4130 206322 : buf[1] = image1;
4131 206322 : buf[2] = image2;
4132 206322 : buf[3] = image3;
4133 : }
4134 206322 : }
4135 :
4136 : static void
4137 4283 : decode_ieee_quad (const struct real_format *fmt, REAL_VALUE_TYPE *r,
4138 : const long *buf)
4139 : {
4140 4283 : unsigned long image3, image2, image1, image0;
4141 4283 : bool sign;
4142 4283 : int exp;
4143 :
4144 4283 : if (FLOAT_WORDS_BIG_ENDIAN)
4145 : {
4146 : image3 = buf[0];
4147 : image2 = buf[1];
4148 : image1 = buf[2];
4149 : image0 = buf[3];
4150 : }
4151 : else
4152 : {
4153 4283 : image0 = buf[0];
4154 4283 : image1 = buf[1];
4155 4283 : image2 = buf[2];
4156 4283 : image3 = buf[3];
4157 : }
4158 4283 : image0 &= 0xffffffff;
4159 4283 : image1 &= 0xffffffff;
4160 4283 : image2 &= 0xffffffff;
4161 :
4162 4283 : sign = (image3 >> 31) & 1;
4163 4283 : exp = (image3 >> 16) & 0x7fff;
4164 4283 : image3 &= 0xffff;
4165 :
4166 4283 : memset (r, 0, sizeof (*r));
4167 :
4168 4283 : if (exp == 0)
4169 : {
4170 1315 : if ((image3 | image2 | image1 | image0) && fmt->has_denorm)
4171 : {
4172 324 : r->cl = rvc_normal;
4173 324 : r->sign = sign;
4174 :
4175 324 : SET_REAL_EXP (r, -16382 + (SIGNIFICAND_BITS - 112));
4176 324 : if (HOST_BITS_PER_LONG == 32)
4177 : {
4178 : r->sig[0] = image0;
4179 : r->sig[1] = image1;
4180 : r->sig[2] = image2;
4181 : r->sig[3] = image3;
4182 : }
4183 : else
4184 : {
4185 324 : r->sig[0] = (image1 << 31 << 1) | image0;
4186 324 : r->sig[1] = (image3 << 31 << 1) | image2;
4187 : }
4188 :
4189 324 : normalize (r);
4190 : }
4191 991 : else if (fmt->has_signed_zero)
4192 991 : r->sign = sign;
4193 : }
4194 2968 : else if (exp == 32767 && (fmt->has_nans || fmt->has_inf))
4195 : {
4196 1915 : if (image3 | image2 | image1 | image0)
4197 : {
4198 1911 : r->cl = rvc_nan;
4199 1911 : r->sign = sign;
4200 1911 : r->signalling = ((image3 >> 15) & 1) ^ fmt->qnan_msb_set;
4201 :
4202 1911 : if (HOST_BITS_PER_LONG == 32)
4203 : {
4204 : r->sig[0] = image0;
4205 : r->sig[1] = image1;
4206 : r->sig[2] = image2;
4207 : r->sig[3] = image3;
4208 : }
4209 : else
4210 : {
4211 1911 : r->sig[0] = (image1 << 31 << 1) | image0;
4212 1911 : r->sig[1] = (image3 << 31 << 1) | image2;
4213 : }
4214 1911 : lshift_significand (r, r, SIGNIFICAND_BITS - 113);
4215 : }
4216 : else
4217 : {
4218 4 : r->cl = rvc_inf;
4219 4 : r->sign = sign;
4220 : }
4221 : }
4222 : else
4223 : {
4224 1053 : r->cl = rvc_normal;
4225 1053 : r->sign = sign;
4226 1053 : SET_REAL_EXP (r, exp - 16383 + 1);
4227 :
4228 1053 : if (HOST_BITS_PER_LONG == 32)
4229 : {
4230 : r->sig[0] = image0;
4231 : r->sig[1] = image1;
4232 : r->sig[2] = image2;
4233 : r->sig[3] = image3;
4234 : }
4235 : else
4236 : {
4237 1053 : r->sig[0] = (image1 << 31 << 1) | image0;
4238 1053 : r->sig[1] = (image3 << 31 << 1) | image2;
4239 : }
4240 1053 : lshift_significand (r, r, SIGNIFICAND_BITS - 113);
4241 1053 : r->sig[SIGSZ-1] |= SIG_MSB;
4242 : }
4243 4283 : }
4244 :
4245 : const struct real_format ieee_quad_format =
4246 : {
4247 : encode_ieee_quad,
4248 : decode_ieee_quad,
4249 : 2,
4250 : 113,
4251 : 113,
4252 : -16381,
4253 : 16384,
4254 : 127,
4255 : 127,
4256 : 128,
4257 : false,
4258 : true,
4259 : true,
4260 : true,
4261 : true,
4262 : true,
4263 : true,
4264 : false,
4265 : "ieee_quad"
4266 : };
4267 :
4268 : const struct real_format mips_quad_format =
4269 : {
4270 : encode_ieee_quad,
4271 : decode_ieee_quad,
4272 : 2,
4273 : 113,
4274 : 113,
4275 : -16381,
4276 : 16384,
4277 : 127,
4278 : 127,
4279 : 128,
4280 : false,
4281 : true,
4282 : true,
4283 : true,
4284 : true,
4285 : true,
4286 : false,
4287 : true,
4288 : "mips_quad"
4289 : };
4290 :
4291 : /* Descriptions of VAX floating point formats can be found beginning at
4292 :
4293 : http://h71000.www7.hp.com/doc/73FINAL/4515/4515pro_013.html#f_floating_point_format
4294 :
4295 : The thing to remember is that they're almost IEEE, except for word
4296 : order, exponent bias, and the lack of infinities, nans, and denormals.
4297 :
4298 : We don't implement the H_floating format here, simply because neither
4299 : the VAX or Alpha ports use it. */
4300 :
4301 : static void encode_vax_f (const struct real_format *fmt,
4302 : long *, const REAL_VALUE_TYPE *);
4303 : static void decode_vax_f (const struct real_format *,
4304 : REAL_VALUE_TYPE *, const long *);
4305 : static void encode_vax_d (const struct real_format *fmt,
4306 : long *, const REAL_VALUE_TYPE *);
4307 : static void decode_vax_d (const struct real_format *,
4308 : REAL_VALUE_TYPE *, const long *);
4309 : static void encode_vax_g (const struct real_format *fmt,
4310 : long *, const REAL_VALUE_TYPE *);
4311 : static void decode_vax_g (const struct real_format *,
4312 : REAL_VALUE_TYPE *, const long *);
4313 :
4314 : static void
4315 0 : encode_vax_f (const struct real_format *fmt ATTRIBUTE_UNUSED, long *buf,
4316 : const REAL_VALUE_TYPE *r)
4317 : {
4318 0 : unsigned long sign, exp, sig, image;
4319 :
4320 0 : sign = r->sign << 15;
4321 :
4322 0 : switch (r->cl)
4323 : {
4324 : case rvc_zero:
4325 : image = 0;
4326 : break;
4327 :
4328 0 : case rvc_inf:
4329 0 : case rvc_nan:
4330 0 : image = 0xffff7fff | sign;
4331 0 : break;
4332 :
4333 0 : case rvc_normal:
4334 0 : sig = (r->sig[SIGSZ-1] >> (HOST_BITS_PER_LONG - 24)) & 0x7fffff;
4335 0 : exp = REAL_EXP (r) + 128;
4336 :
4337 0 : image = (sig << 16) & 0xffff0000;
4338 0 : image |= sign;
4339 0 : image |= exp << 7;
4340 0 : image |= sig >> 16;
4341 0 : break;
4342 :
4343 0 : default:
4344 0 : gcc_unreachable ();
4345 : }
4346 :
4347 0 : buf[0] = image;
4348 0 : }
4349 :
4350 : static void
4351 0 : decode_vax_f (const struct real_format *fmt ATTRIBUTE_UNUSED,
4352 : REAL_VALUE_TYPE *r, const long *buf)
4353 : {
4354 0 : unsigned long image = buf[0] & 0xffffffff;
4355 0 : int exp = (image >> 7) & 0xff;
4356 :
4357 0 : memset (r, 0, sizeof (*r));
4358 :
4359 0 : if (exp != 0)
4360 : {
4361 0 : r->cl = rvc_normal;
4362 0 : r->sign = (image >> 15) & 1;
4363 0 : SET_REAL_EXP (r, exp - 128);
4364 :
4365 0 : image = ((image & 0x7f) << 16) | ((image >> 16) & 0xffff);
4366 0 : r->sig[SIGSZ-1] = (image << (HOST_BITS_PER_LONG - 24)) | SIG_MSB;
4367 : }
4368 0 : }
4369 :
4370 : static void
4371 0 : encode_vax_d (const struct real_format *fmt ATTRIBUTE_UNUSED, long *buf,
4372 : const REAL_VALUE_TYPE *r)
4373 : {
4374 0 : unsigned long image0, image1, sign = r->sign << 15;
4375 :
4376 0 : switch (r->cl)
4377 : {
4378 : case rvc_zero:
4379 : image0 = image1 = 0;
4380 : break;
4381 :
4382 0 : case rvc_inf:
4383 0 : case rvc_nan:
4384 0 : image0 = 0xffff7fff | sign;
4385 0 : image1 = 0xffffffff;
4386 0 : break;
4387 :
4388 0 : case rvc_normal:
4389 : /* Extract the significand into straight hi:lo. */
4390 0 : if (HOST_BITS_PER_LONG == 64)
4391 : {
4392 0 : image0 = r->sig[SIGSZ-1];
4393 0 : image1 = (image0 >> (64 - 56)) & 0xffffffff;
4394 0 : image0 = (image0 >> (64 - 56 + 1) >> 31) & 0x7fffff;
4395 : }
4396 : else
4397 : {
4398 : image0 = r->sig[SIGSZ-1];
4399 : image1 = r->sig[SIGSZ-2];
4400 : image1 = (image0 << 24) | (image1 >> 8);
4401 : image0 = (image0 >> 8) & 0xffffff;
4402 : }
4403 :
4404 : /* Rearrange the half-words of the significand to match the
4405 : external format. */
4406 0 : image0 = ((image0 << 16) | (image0 >> 16)) & 0xffff007f;
4407 0 : image1 = ((image1 << 16) | (image1 >> 16)) & 0xffffffff;
4408 :
4409 : /* Add the sign and exponent. */
4410 0 : image0 |= sign;
4411 0 : image0 |= (REAL_EXP (r) + 128) << 7;
4412 0 : break;
4413 :
4414 0 : default:
4415 0 : gcc_unreachable ();
4416 : }
4417 :
4418 0 : if (FLOAT_WORDS_BIG_ENDIAN)
4419 : buf[0] = image1, buf[1] = image0;
4420 : else
4421 0 : buf[0] = image0, buf[1] = image1;
4422 0 : }
4423 :
4424 : static void
4425 0 : decode_vax_d (const struct real_format *fmt ATTRIBUTE_UNUSED,
4426 : REAL_VALUE_TYPE *r, const long *buf)
4427 : {
4428 0 : unsigned long image0, image1;
4429 0 : int exp;
4430 :
4431 0 : if (FLOAT_WORDS_BIG_ENDIAN)
4432 : image1 = buf[0], image0 = buf[1];
4433 : else
4434 0 : image0 = buf[0], image1 = buf[1];
4435 0 : image0 &= 0xffffffff;
4436 0 : image1 &= 0xffffffff;
4437 :
4438 0 : exp = (image0 >> 7) & 0xff;
4439 :
4440 0 : memset (r, 0, sizeof (*r));
4441 :
4442 0 : if (exp != 0)
4443 : {
4444 0 : r->cl = rvc_normal;
4445 0 : r->sign = (image0 >> 15) & 1;
4446 0 : SET_REAL_EXP (r, exp - 128);
4447 :
4448 : /* Rearrange the half-words of the external format into
4449 : proper ascending order. */
4450 0 : image0 = ((image0 & 0x7f) << 16) | ((image0 >> 16) & 0xffff);
4451 0 : image1 = ((image1 & 0xffff) << 16) | ((image1 >> 16) & 0xffff);
4452 :
4453 0 : if (HOST_BITS_PER_LONG == 64)
4454 : {
4455 0 : image0 = (image0 << 31 << 1) | image1;
4456 0 : image0 <<= 64 - 56;
4457 0 : image0 |= SIG_MSB;
4458 0 : r->sig[SIGSZ-1] = image0;
4459 : }
4460 : else
4461 : {
4462 : r->sig[SIGSZ-1] = image0;
4463 : r->sig[SIGSZ-2] = image1;
4464 : lshift_significand (r, r, 2*HOST_BITS_PER_LONG - 56);
4465 : r->sig[SIGSZ-1] |= SIG_MSB;
4466 : }
4467 : }
4468 0 : }
4469 :
4470 : static void
4471 0 : encode_vax_g (const struct real_format *fmt ATTRIBUTE_UNUSED, long *buf,
4472 : const REAL_VALUE_TYPE *r)
4473 : {
4474 0 : unsigned long image0, image1, sign = r->sign << 15;
4475 :
4476 0 : switch (r->cl)
4477 : {
4478 : case rvc_zero:
4479 : image0 = image1 = 0;
4480 : break;
4481 :
4482 0 : case rvc_inf:
4483 0 : case rvc_nan:
4484 0 : image0 = 0xffff7fff | sign;
4485 0 : image1 = 0xffffffff;
4486 0 : break;
4487 :
4488 0 : case rvc_normal:
4489 : /* Extract the significand into straight hi:lo. */
4490 0 : if (HOST_BITS_PER_LONG == 64)
4491 : {
4492 0 : image0 = r->sig[SIGSZ-1];
4493 0 : image1 = (image0 >> (64 - 53)) & 0xffffffff;
4494 0 : image0 = (image0 >> (64 - 53 + 1) >> 31) & 0xfffff;
4495 : }
4496 : else
4497 : {
4498 : image0 = r->sig[SIGSZ-1];
4499 : image1 = r->sig[SIGSZ-2];
4500 : image1 = (image0 << 21) | (image1 >> 11);
4501 : image0 = (image0 >> 11) & 0xfffff;
4502 : }
4503 :
4504 : /* Rearrange the half-words of the significand to match the
4505 : external format. */
4506 0 : image0 = ((image0 << 16) | (image0 >> 16)) & 0xffff000f;
4507 0 : image1 = ((image1 << 16) | (image1 >> 16)) & 0xffffffff;
4508 :
4509 : /* Add the sign and exponent. */
4510 0 : image0 |= sign;
4511 0 : image0 |= (REAL_EXP (r) + 1024) << 4;
4512 0 : break;
4513 :
4514 0 : default:
4515 0 : gcc_unreachable ();
4516 : }
4517 :
4518 0 : if (FLOAT_WORDS_BIG_ENDIAN)
4519 : buf[0] = image1, buf[1] = image0;
4520 : else
4521 0 : buf[0] = image0, buf[1] = image1;
4522 0 : }
4523 :
4524 : static void
4525 0 : decode_vax_g (const struct real_format *fmt ATTRIBUTE_UNUSED,
4526 : REAL_VALUE_TYPE *r, const long *buf)
4527 : {
4528 0 : unsigned long image0, image1;
4529 0 : int exp;
4530 :
4531 0 : if (FLOAT_WORDS_BIG_ENDIAN)
4532 : image1 = buf[0], image0 = buf[1];
4533 : else
4534 0 : image0 = buf[0], image1 = buf[1];
4535 0 : image0 &= 0xffffffff;
4536 0 : image1 &= 0xffffffff;
4537 :
4538 0 : exp = (image0 >> 4) & 0x7ff;
4539 :
4540 0 : memset (r, 0, sizeof (*r));
4541 :
4542 0 : if (exp != 0)
4543 : {
4544 0 : r->cl = rvc_normal;
4545 0 : r->sign = (image0 >> 15) & 1;
4546 0 : SET_REAL_EXP (r, exp - 1024);
4547 :
4548 : /* Rearrange the half-words of the external format into
4549 : proper ascending order. */
4550 0 : image0 = ((image0 & 0xf) << 16) | ((image0 >> 16) & 0xffff);
4551 0 : image1 = ((image1 & 0xffff) << 16) | ((image1 >> 16) & 0xffff);
4552 :
4553 0 : if (HOST_BITS_PER_LONG == 64)
4554 : {
4555 0 : image0 = (image0 << 31 << 1) | image1;
4556 0 : image0 <<= 64 - 53;
4557 0 : image0 |= SIG_MSB;
4558 0 : r->sig[SIGSZ-1] = image0;
4559 : }
4560 : else
4561 : {
4562 : r->sig[SIGSZ-1] = image0;
4563 : r->sig[SIGSZ-2] = image1;
4564 : lshift_significand (r, r, 64 - 53);
4565 : r->sig[SIGSZ-1] |= SIG_MSB;
4566 : }
4567 : }
4568 0 : }
4569 :
4570 : const struct real_format vax_f_format =
4571 : {
4572 : encode_vax_f,
4573 : decode_vax_f,
4574 : 2,
4575 : 24,
4576 : 24,
4577 : -127,
4578 : 127,
4579 : 15,
4580 : 15,
4581 : 0,
4582 : false,
4583 : false,
4584 : false,
4585 : false,
4586 : false,
4587 : false,
4588 : false,
4589 : false,
4590 : "vax_f"
4591 : };
4592 :
4593 : const struct real_format vax_d_format =
4594 : {
4595 : encode_vax_d,
4596 : decode_vax_d,
4597 : 2,
4598 : 56,
4599 : 56,
4600 : -127,
4601 : 127,
4602 : 15,
4603 : 15,
4604 : 0,
4605 : false,
4606 : false,
4607 : false,
4608 : false,
4609 : false,
4610 : false,
4611 : false,
4612 : false,
4613 : "vax_d"
4614 : };
4615 :
4616 : const struct real_format vax_g_format =
4617 : {
4618 : encode_vax_g,
4619 : decode_vax_g,
4620 : 2,
4621 : 53,
4622 : 53,
4623 : -1023,
4624 : 1023,
4625 : 15,
4626 : 15,
4627 : 0,
4628 : false,
4629 : false,
4630 : false,
4631 : false,
4632 : false,
4633 : false,
4634 : false,
4635 : false,
4636 : "vax_g"
4637 : };
4638 :
4639 : /* Encode real R into a single precision DFP value in BUF. */
4640 : static void
4641 10590 : encode_decimal_single (const struct real_format *fmt ATTRIBUTE_UNUSED,
4642 : long *buf ATTRIBUTE_UNUSED,
4643 : const REAL_VALUE_TYPE *r ATTRIBUTE_UNUSED)
4644 : {
4645 10590 : encode_decimal32 (fmt, buf, r);
4646 10590 : }
4647 :
4648 : /* Decode a single precision DFP value in BUF into a real R. */
4649 : static void
4650 850 : decode_decimal_single (const struct real_format *fmt ATTRIBUTE_UNUSED,
4651 : REAL_VALUE_TYPE *r ATTRIBUTE_UNUSED,
4652 : const long *buf ATTRIBUTE_UNUSED)
4653 : {
4654 850 : decode_decimal32 (fmt, r, buf);
4655 850 : }
4656 :
4657 : /* Encode real R into a double precision DFP value in BUF. */
4658 : static void
4659 12424 : encode_decimal_double (const struct real_format *fmt ATTRIBUTE_UNUSED,
4660 : long *buf ATTRIBUTE_UNUSED,
4661 : const REAL_VALUE_TYPE *r ATTRIBUTE_UNUSED)
4662 : {
4663 12424 : encode_decimal64 (fmt, buf, r);
4664 12424 : }
4665 :
4666 : /* Decode a double precision DFP value in BUF into a real R. */
4667 : static void
4668 3223 : decode_decimal_double (const struct real_format *fmt ATTRIBUTE_UNUSED,
4669 : REAL_VALUE_TYPE *r ATTRIBUTE_UNUSED,
4670 : const long *buf ATTRIBUTE_UNUSED)
4671 : {
4672 3223 : decode_decimal64 (fmt, r, buf);
4673 3223 : }
4674 :
4675 : /* Encode real R into a quad precision DFP value in BUF. */
4676 : static void
4677 16053 : encode_decimal_quad (const struct real_format *fmt ATTRIBUTE_UNUSED,
4678 : long *buf ATTRIBUTE_UNUSED,
4679 : const REAL_VALUE_TYPE *r ATTRIBUTE_UNUSED)
4680 : {
4681 16053 : encode_decimal128 (fmt, buf, r);
4682 16053 : }
4683 :
4684 : /* Decode a quad precision DFP value in BUF into a real R. */
4685 : static void
4686 5637 : decode_decimal_quad (const struct real_format *fmt ATTRIBUTE_UNUSED,
4687 : REAL_VALUE_TYPE *r ATTRIBUTE_UNUSED,
4688 : const long *buf ATTRIBUTE_UNUSED)
4689 : {
4690 5637 : decode_decimal128 (fmt, r, buf);
4691 5637 : }
4692 :
4693 : /* Single precision decimal floating point (IEEE 754). */
4694 : const struct real_format decimal_single_format =
4695 : {
4696 : encode_decimal_single,
4697 : decode_decimal_single,
4698 : 10,
4699 : 7,
4700 : 7,
4701 : -94,
4702 : 97,
4703 : 31,
4704 : 31,
4705 : 32,
4706 : false,
4707 : true,
4708 : true,
4709 : true,
4710 : true,
4711 : true,
4712 : true,
4713 : false,
4714 : "decimal_single"
4715 : };
4716 :
4717 : /* Double precision decimal floating point (IEEE 754). */
4718 : const struct real_format decimal_double_format =
4719 : {
4720 : encode_decimal_double,
4721 : decode_decimal_double,
4722 : 10,
4723 : 16,
4724 : 16,
4725 : -382,
4726 : 385,
4727 : 63,
4728 : 63,
4729 : 64,
4730 : false,
4731 : true,
4732 : true,
4733 : true,
4734 : true,
4735 : true,
4736 : true,
4737 : false,
4738 : "decimal_double"
4739 : };
4740 :
4741 : /* Quad precision decimal floating point (IEEE 754). */
4742 : const struct real_format decimal_quad_format =
4743 : {
4744 : encode_decimal_quad,
4745 : decode_decimal_quad,
4746 : 10,
4747 : 34,
4748 : 34,
4749 : -6142,
4750 : 6145,
4751 : 127,
4752 : 127,
4753 : 128,
4754 : false,
4755 : true,
4756 : true,
4757 : true,
4758 : true,
4759 : true,
4760 : true,
4761 : false,
4762 : "decimal_quad"
4763 : };
4764 :
4765 : /* Encode half-precision floats. This routine is used both for the IEEE
4766 : ARM alternative encodings. */
4767 : static void
4768 81303 : encode_ieee_half (const struct real_format *fmt, long *buf,
4769 : const REAL_VALUE_TYPE *r)
4770 : {
4771 81303 : unsigned long image, sig, exp;
4772 81303 : unsigned long sign = r->sign;
4773 :
4774 81303 : image = sign << 15;
4775 81303 : sig = (r->sig[SIGSZ-1] >> (HOST_BITS_PER_LONG - 11)) & 0x3ff;
4776 :
4777 81303 : switch (r->cl)
4778 : {
4779 : case rvc_zero:
4780 : break;
4781 :
4782 605 : case rvc_inf:
4783 605 : if (fmt->has_inf)
4784 605 : image |= 31 << 10;
4785 : else
4786 0 : image |= 0x7fff;
4787 : break;
4788 :
4789 1513 : case rvc_nan:
4790 1513 : if (fmt->has_nans)
4791 : {
4792 1513 : if (r->canonical)
4793 199 : sig = (fmt->canonical_nan_lsbs_set ? (1 << 9) - 1 : 0);
4794 1513 : if (r->signalling == fmt->qnan_msb_set)
4795 78 : sig &= ~(1 << 9);
4796 : else
4797 1435 : sig |= 1 << 9;
4798 1513 : if (sig == 0)
4799 45 : sig = 1 << 8;
4800 :
4801 1513 : image |= 31 << 10;
4802 1513 : image |= sig;
4803 : }
4804 : else
4805 0 : image |= 0x3ff;
4806 : break;
4807 :
4808 61327 : case rvc_normal:
4809 : /* Recall that IEEE numbers are interpreted as 1.F x 2**exp,
4810 : whereas the intermediate representation is 0.F x 2**exp.
4811 : Which means we're off by one. */
4812 61327 : if (real_isdenormal (r))
4813 : exp = 0;
4814 : else
4815 58377 : exp = REAL_EXP (r) + 15 - 1;
4816 61327 : image |= exp << 10;
4817 61327 : image |= sig;
4818 61327 : break;
4819 :
4820 0 : default:
4821 0 : gcc_unreachable ();
4822 : }
4823 :
4824 81303 : buf[0] = image;
4825 81303 : }
4826 :
4827 : /* Decode half-precision floats. This routine is used both for the IEEE
4828 : ARM alternative encodings. */
4829 : static void
4830 11303 : decode_ieee_half (const struct real_format *fmt, REAL_VALUE_TYPE *r,
4831 : const long *buf)
4832 : {
4833 11303 : unsigned long image = buf[0] & 0xffff;
4834 11303 : bool sign = (image >> 15) & 1;
4835 11303 : int exp = (image >> 10) & 0x1f;
4836 :
4837 11303 : memset (r, 0, sizeof (*r));
4838 11303 : image <<= HOST_BITS_PER_LONG - 11;
4839 11303 : image &= ~SIG_MSB;
4840 :
4841 11303 : if (exp == 0)
4842 : {
4843 8769 : if (image && fmt->has_denorm)
4844 : {
4845 1595 : r->cl = rvc_normal;
4846 1595 : r->sign = sign;
4847 1595 : SET_REAL_EXP (r, -14);
4848 1595 : r->sig[SIGSZ-1] = image << 1;
4849 1595 : normalize (r);
4850 : }
4851 7174 : else if (fmt->has_signed_zero)
4852 7174 : r->sign = sign;
4853 : }
4854 2534 : else if (exp == 31 && (fmt->has_nans || fmt->has_inf))
4855 : {
4856 1567 : if (image)
4857 : {
4858 1427 : r->cl = rvc_nan;
4859 1427 : r->sign = sign;
4860 1427 : r->signalling = (((image >> (HOST_BITS_PER_LONG - 2)) & 1)
4861 1427 : ^ fmt->qnan_msb_set);
4862 1427 : r->sig[SIGSZ-1] = image;
4863 : }
4864 : else
4865 : {
4866 140 : r->cl = rvc_inf;
4867 140 : r->sign = sign;
4868 : }
4869 : }
4870 : else
4871 : {
4872 967 : r->cl = rvc_normal;
4873 967 : r->sign = sign;
4874 967 : SET_REAL_EXP (r, exp - 15 + 1);
4875 967 : r->sig[SIGSZ-1] = image | SIG_MSB;
4876 : }
4877 11303 : }
4878 :
4879 : /* Encode arm_bfloat types. */
4880 : static void
4881 5227 : encode_arm_bfloat_half (const struct real_format *fmt, long *buf,
4882 : const REAL_VALUE_TYPE *r)
4883 : {
4884 5227 : unsigned long image, sig, exp;
4885 5227 : unsigned long sign = r->sign;
4886 :
4887 5227 : image = sign << 15;
4888 5227 : sig = (r->sig[SIGSZ-1] >> (HOST_BITS_PER_LONG - 8)) & 0x7f;
4889 :
4890 5227 : switch (r->cl)
4891 : {
4892 : case rvc_zero:
4893 : break;
4894 :
4895 226 : case rvc_inf:
4896 226 : if (fmt->has_inf)
4897 226 : image |= 255 << 7;
4898 : else
4899 0 : image |= 0x7fff;
4900 : break;
4901 :
4902 180 : case rvc_nan:
4903 180 : if (fmt->has_nans)
4904 : {
4905 180 : if (r->canonical)
4906 135 : sig = (fmt->canonical_nan_lsbs_set ? (1 << 6) - 1 : 0);
4907 180 : if (r->signalling == fmt->qnan_msb_set)
4908 45 : sig &= ~(1 << 6);
4909 : else
4910 135 : sig |= 1 << 6;
4911 180 : if (sig == 0)
4912 40 : sig = 1 << 5;
4913 :
4914 180 : image |= 255 << 7;
4915 180 : image |= sig;
4916 : }
4917 : else
4918 0 : image |= 0x7fff;
4919 : break;
4920 :
4921 1634 : case rvc_normal:
4922 1634 : if (real_isdenormal (r))
4923 : exp = 0;
4924 : else
4925 1559 : exp = REAL_EXP (r) + 127 - 1;
4926 1634 : image |= exp << 7;
4927 1634 : image |= sig;
4928 1634 : break;
4929 :
4930 0 : default:
4931 0 : gcc_unreachable ();
4932 : }
4933 :
4934 5227 : buf[0] = image;
4935 5227 : }
4936 :
4937 : /* Decode arm_bfloat types. */
4938 : static void
4939 2809 : decode_arm_bfloat_half (const struct real_format *fmt, REAL_VALUE_TYPE *r,
4940 : const long *buf)
4941 : {
4942 2809 : unsigned long image = buf[0] & 0xffff;
4943 2809 : bool sign = (image >> 15) & 1;
4944 2809 : int exp = (image >> 7) & 0xff;
4945 :
4946 2809 : memset (r, 0, sizeof (*r));
4947 2809 : image <<= HOST_BITS_PER_LONG - 8;
4948 2809 : image &= ~SIG_MSB;
4949 :
4950 2809 : if (exp == 0)
4951 : {
4952 2470 : if (image && fmt->has_denorm)
4953 : {
4954 30 : r->cl = rvc_normal;
4955 30 : r->sign = sign;
4956 30 : SET_REAL_EXP (r, -126);
4957 30 : r->sig[SIGSZ-1] = image << 1;
4958 30 : normalize (r);
4959 : }
4960 2440 : else if (fmt->has_signed_zero)
4961 2440 : r->sign = sign;
4962 : }
4963 339 : else if (exp == 255 && (fmt->has_nans || fmt->has_inf))
4964 : {
4965 102 : if (image)
4966 : {
4967 20 : r->cl = rvc_nan;
4968 20 : r->sign = sign;
4969 20 : r->signalling = (((image >> (HOST_BITS_PER_LONG - 2)) & 1)
4970 20 : ^ fmt->qnan_msb_set);
4971 20 : r->sig[SIGSZ-1] = image;
4972 : }
4973 : else
4974 : {
4975 82 : r->cl = rvc_inf;
4976 82 : r->sign = sign;
4977 : }
4978 : }
4979 : else
4980 : {
4981 237 : r->cl = rvc_normal;
4982 237 : r->sign = sign;
4983 237 : SET_REAL_EXP (r, exp - 127 + 1);
4984 237 : r->sig[SIGSZ-1] = image | SIG_MSB;
4985 : }
4986 2809 : }
4987 :
4988 : /* Half-precision format, as specified in IEEE 754R. */
4989 : const struct real_format ieee_half_format =
4990 : {
4991 : encode_ieee_half,
4992 : decode_ieee_half,
4993 : 2,
4994 : 11,
4995 : 11,
4996 : -13,
4997 : 16,
4998 : 15,
4999 : 15,
5000 : 16,
5001 : false,
5002 : true,
5003 : true,
5004 : true,
5005 : true,
5006 : true,
5007 : true,
5008 : false,
5009 : "ieee_half"
5010 : };
5011 :
5012 : /* ARM's alternative half-precision format, similar to IEEE but with
5013 : no reserved exponent value for NaNs and infinities; rather, it just
5014 : extends the range of exponents by one. */
5015 : const struct real_format arm_half_format =
5016 : {
5017 : encode_ieee_half,
5018 : decode_ieee_half,
5019 : 2,
5020 : 11,
5021 : 11,
5022 : -13,
5023 : 17,
5024 : 15,
5025 : 15,
5026 : 0,
5027 : false,
5028 : true,
5029 : false,
5030 : false,
5031 : true,
5032 : true,
5033 : false,
5034 : false,
5035 : "arm_half"
5036 : };
5037 :
5038 : /* ARM Bfloat half-precision format. This format resembles a truncated
5039 : (16-bit) version of the 32-bit IEEE 754 single-precision floating-point
5040 : format. */
5041 : const struct real_format arm_bfloat_half_format =
5042 : {
5043 : encode_arm_bfloat_half,
5044 : decode_arm_bfloat_half,
5045 : 2,
5046 : 8,
5047 : 8,
5048 : -125,
5049 : 128,
5050 : 15,
5051 : 15,
5052 : 0,
5053 : false,
5054 : true,
5055 : true,
5056 : true,
5057 : true,
5058 : true,
5059 : true,
5060 : false,
5061 : "arm_bfloat_half"
5062 : };
5063 :
5064 :
5065 : /* A synthetic "format" for internal arithmetic. It's the size of the
5066 : internal significand minus the two bits needed for proper rounding.
5067 : The encode and decode routines exist only to satisfy our paranoia
5068 : harness. */
5069 :
5070 : static void encode_internal (const struct real_format *fmt,
5071 : long *, const REAL_VALUE_TYPE *);
5072 : static void decode_internal (const struct real_format *,
5073 : REAL_VALUE_TYPE *, const long *);
5074 :
5075 : static void
5076 0 : encode_internal (const struct real_format *fmt ATTRIBUTE_UNUSED, long *buf,
5077 : const REAL_VALUE_TYPE *r)
5078 : {
5079 0 : memcpy (buf, r, sizeof (*r));
5080 0 : }
5081 :
5082 : static void
5083 0 : decode_internal (const struct real_format *fmt ATTRIBUTE_UNUSED,
5084 : REAL_VALUE_TYPE *r, const long *buf)
5085 : {
5086 0 : memcpy (r, buf, sizeof (*r));
5087 0 : }
5088 :
5089 : const struct real_format real_internal_format =
5090 : {
5091 : encode_internal,
5092 : decode_internal,
5093 : 2,
5094 : SIGNIFICAND_BITS - 2,
5095 : SIGNIFICAND_BITS - 2,
5096 : -MAX_EXP,
5097 : MAX_EXP,
5098 : -1,
5099 : -1,
5100 : 0,
5101 : false,
5102 : false,
5103 : true,
5104 : true,
5105 : false,
5106 : true,
5107 : true,
5108 : false,
5109 : "real_internal"
5110 : };
5111 :
5112 : /* Calculate X raised to the integer exponent N in format FMT and store
5113 : the result in R. Return true if the result may be inexact due to
5114 : loss of precision. The algorithm is the classic "left-to-right binary
5115 : method" described in section 4.6.3 of Donald Knuth's "Seminumerical
5116 : Algorithms", "The Art of Computer Programming", Volume 2. */
5117 :
5118 : bool
5119 380 : real_powi (REAL_VALUE_TYPE *r, format_helper fmt,
5120 : const REAL_VALUE_TYPE *x, HOST_WIDE_INT n)
5121 : {
5122 380 : unsigned HOST_WIDE_INT bit;
5123 380 : REAL_VALUE_TYPE t;
5124 380 : bool inexact = false;
5125 380 : bool init = false;
5126 380 : bool neg;
5127 380 : int i;
5128 :
5129 380 : if (n == 0)
5130 : {
5131 29 : *r = dconst1;
5132 29 : return false;
5133 : }
5134 351 : else if (n < 0)
5135 : {
5136 : /* Don't worry about overflow, from now on n is unsigned. */
5137 141 : neg = true;
5138 141 : n = -n;
5139 : }
5140 : else
5141 : neg = false;
5142 :
5143 351 : t = *x;
5144 351 : bit = HOST_WIDE_INT_1U << (HOST_BITS_PER_WIDE_INT - 1);
5145 22815 : for (i = 0; i < HOST_BITS_PER_WIDE_INT; i++)
5146 : {
5147 22464 : if (init)
5148 : {
5149 3678 : inexact |= do_multiply (&t, &t, &t);
5150 3678 : if (n & bit)
5151 2772 : inexact |= do_multiply (&t, &t, x);
5152 : }
5153 18786 : else if (n & bit)
5154 351 : init = true;
5155 22464 : bit >>= 1;
5156 : }
5157 :
5158 351 : if (neg)
5159 141 : inexact |= do_divide (&t, &dconst1, &t);
5160 :
5161 351 : real_convert (r, fmt, &t);
5162 351 : return inexact;
5163 : }
5164 :
5165 : /* Round X to the nearest integer not larger in absolute value, i.e.
5166 : towards zero, placing the result in R in format FMT. */
5167 :
5168 : void
5169 546074 : real_trunc (REAL_VALUE_TYPE *r, format_helper fmt,
5170 : const REAL_VALUE_TYPE *x)
5171 : {
5172 546074 : do_fix_trunc (r, x);
5173 546074 : if (fmt)
5174 434792 : real_convert (r, fmt, r);
5175 546074 : }
5176 :
5177 : /* Round X to the largest integer not greater in value, i.e. round
5178 : down, placing the result in R in format FMT. */
5179 :
5180 : void
5181 1240 : real_floor (REAL_VALUE_TYPE *r, format_helper fmt,
5182 : const REAL_VALUE_TYPE *x)
5183 : {
5184 1240 : REAL_VALUE_TYPE t;
5185 :
5186 1240 : do_fix_trunc (&t, x);
5187 1240 : if (! real_identical (&t, x) && x->sign)
5188 288 : do_add (&t, &t, &dconstm1, 0);
5189 1240 : if (fmt)
5190 1240 : real_convert (r, fmt, &t);
5191 : else
5192 0 : *r = t;
5193 1240 : }
5194 :
5195 : /* Round X to the smallest integer not less then argument, i.e. round
5196 : up, placing the result in R in format FMT. */
5197 :
5198 : void
5199 39931 : real_ceil (REAL_VALUE_TYPE *r, format_helper fmt,
5200 : const REAL_VALUE_TYPE *x)
5201 : {
5202 39931 : REAL_VALUE_TYPE t;
5203 :
5204 39931 : do_fix_trunc (&t, x);
5205 39931 : if (! real_identical (&t, x) && ! x->sign)
5206 270 : do_add (&t, &t, &dconst1, 0);
5207 39931 : if (fmt)
5208 39931 : real_convert (r, fmt, &t);
5209 : else
5210 0 : *r = t;
5211 39931 : }
5212 :
5213 : /* Round X to the nearest integer, but round halfway cases away from
5214 : zero. */
5215 :
5216 : void
5217 1287 : real_round (REAL_VALUE_TYPE *r, format_helper fmt,
5218 : const REAL_VALUE_TYPE *x)
5219 : {
5220 1287 : do_add (r, x, &dconsthalf, x->sign);
5221 1287 : do_fix_trunc (r, r);
5222 1287 : if (fmt)
5223 1287 : real_convert (r, fmt, r);
5224 1287 : }
5225 :
5226 : /* Return true (including 0) if integer part of R is even, else return
5227 : false. The function is not valid for rvc_inf and rvc_nan classes. */
5228 :
5229 : static bool
5230 49 : is_even (REAL_VALUE_TYPE *r)
5231 : {
5232 49 : gcc_assert (r->cl != rvc_inf);
5233 49 : gcc_assert (r->cl != rvc_nan);
5234 :
5235 49 : if (r->cl == rvc_zero)
5236 : return true;
5237 :
5238 : /* For (-1,1), number is even. */
5239 49 : if (REAL_EXP (r) <= 0)
5240 : return true;
5241 :
5242 : /* Check lowest bit, if not set, return true. */
5243 49 : else if (REAL_EXP (r) <= SIGNIFICAND_BITS)
5244 : {
5245 49 : unsigned int n = SIGNIFICAND_BITS - REAL_EXP (r);
5246 49 : int w = n / HOST_BITS_PER_LONG;
5247 :
5248 49 : unsigned long num = ((unsigned long)1 << (n % HOST_BITS_PER_LONG));
5249 :
5250 49 : if ((r->sig[w] & num) == 0)
5251 28 : return true;
5252 : }
5253 : else
5254 : return true;
5255 :
5256 : return false;
5257 : }
5258 :
5259 : /* Return true if R is halfway between two integers, else return
5260 : false. */
5261 :
5262 : static bool
5263 189 : is_halfway_below (const REAL_VALUE_TYPE *r)
5264 : {
5265 189 : if (r->cl != rvc_normal)
5266 : return false;
5267 :
5268 : /* For numbers (-0.5,0) and (0,0.5). */
5269 147 : if (REAL_EXP (r) < 0)
5270 : return false;
5271 :
5272 133 : else if (REAL_EXP (r) < SIGNIFICAND_BITS)
5273 : {
5274 119 : unsigned int n = SIGNIFICAND_BITS - REAL_EXP (r) - 1;
5275 119 : int w = n / HOST_BITS_PER_LONG;
5276 :
5277 322 : for (int i = 0; i < w; ++i)
5278 210 : if (r->sig[i] != 0)
5279 : return false;
5280 :
5281 112 : unsigned long num = 1UL << (n % HOST_BITS_PER_LONG);
5282 :
5283 112 : if ((r->sig[w] & num) != 0 && (r->sig[w] & (num - 1)) == 0)
5284 77 : return true;
5285 : }
5286 : return false;
5287 : }
5288 :
5289 : /* Round X to nearest integer, rounding halfway cases towards even. */
5290 :
5291 : void
5292 189 : real_roundeven (REAL_VALUE_TYPE *r, format_helper fmt,
5293 : const REAL_VALUE_TYPE *x)
5294 : {
5295 189 : if (is_halfway_below (x))
5296 : {
5297 : /* Special case as -0.5 rounds to -0.0 and
5298 : similarly +0.5 rounds to +0.0. */
5299 77 : if (REAL_EXP (x) == 0)
5300 : {
5301 28 : *r = *x;
5302 28 : clear_significand_below (r, SIGNIFICAND_BITS);
5303 : }
5304 : else
5305 : {
5306 49 : do_add (r, x, &dconsthalf, x->sign);
5307 49 : if (!is_even (r))
5308 21 : do_add (r, r, &dconstm1, x->sign);
5309 : }
5310 77 : if (fmt)
5311 77 : real_convert (r, fmt, r);
5312 : }
5313 : else
5314 112 : real_round (r, fmt, x);
5315 189 : }
5316 :
5317 : /* Set the sign of R to the sign of X. */
5318 :
5319 : void
5320 70628 : real_copysign (REAL_VALUE_TYPE *r, const REAL_VALUE_TYPE *x)
5321 : {
5322 70628 : r->sign = x->sign;
5323 70628 : }
5324 :
5325 : /* Check whether the real constant value given is an integer.
5326 : Returns false for signaling NaN. */
5327 :
5328 : bool
5329 6678 : real_isinteger (const REAL_VALUE_TYPE *c, format_helper fmt)
5330 : {
5331 6678 : REAL_VALUE_TYPE cint;
5332 :
5333 6678 : real_trunc (&cint, fmt, c);
5334 6678 : return real_identical (c, &cint);
5335 : }
5336 :
5337 : /* Check whether C is an integer that fits in a HOST_WIDE_INT,
5338 : storing it in *INT_OUT if so. */
5339 :
5340 : bool
5341 221 : real_isinteger (const REAL_VALUE_TYPE *c, HOST_WIDE_INT *int_out)
5342 : {
5343 221 : REAL_VALUE_TYPE cint;
5344 :
5345 221 : HOST_WIDE_INT n = real_to_integer (c);
5346 221 : real_from_integer (&cint, VOIDmode, n, SIGNED);
5347 221 : if (real_identical (c, &cint))
5348 : {
5349 126 : *int_out = n;
5350 126 : return true;
5351 : }
5352 : return false;
5353 : }
5354 :
5355 : /* Calculate nextafter (X, Y) or nexttoward (X, Y). Return true if
5356 : underflow or overflow needs to be raised. */
5357 :
5358 : bool
5359 3994243 : real_nextafter (REAL_VALUE_TYPE *r, format_helper fmt,
5360 : const REAL_VALUE_TYPE *x, const REAL_VALUE_TYPE *y)
5361 : {
5362 3994243 : int cmp = do_compare (x, y, 2);
5363 : /* If either operand is NaN, return qNaN. */
5364 3994243 : if (cmp == 2)
5365 : {
5366 94 : get_canonical_qnan (r, 0);
5367 94 : return false;
5368 : }
5369 : /* If x == y, return y cast to target type. */
5370 3994149 : if (cmp == 0)
5371 : {
5372 428 : real_convert (r, fmt, y);
5373 428 : return false;
5374 : }
5375 :
5376 3993721 : if (x->cl == rvc_zero)
5377 : {
5378 846946 : get_zero (r, y->sign);
5379 846946 : r->cl = rvc_normal;
5380 846946 : SET_REAL_EXP (r, fmt->emin - fmt->p + 1);
5381 846946 : r->sig[SIGSZ - 1] = SIG_MSB;
5382 846946 : return false;
5383 : }
5384 :
5385 3146775 : int np2 = SIGNIFICAND_BITS - fmt->p;
5386 : /* For denormals adjust np2 correspondingly. */
5387 3146775 : if (x->cl == rvc_normal && REAL_EXP (x) < fmt->emin)
5388 69251 : np2 += fmt->emin - REAL_EXP (x);
5389 :
5390 3146775 : REAL_VALUE_TYPE u;
5391 3146775 : get_zero (r, x->sign);
5392 3146775 : get_zero (&u, 0);
5393 3146775 : set_significand_bit (&u, np2);
5394 3146775 : r->cl = rvc_normal;
5395 3146775 : SET_REAL_EXP (r, REAL_EXP (x));
5396 :
5397 3146775 : if (x->cl == rvc_inf)
5398 : {
5399 175409 : bool borrow = sub_significands (r, r, &u, 0);
5400 175409 : gcc_assert (borrow);
5401 175409 : SET_REAL_EXP (r, fmt->emax);
5402 : }
5403 4904056 : else if (cmp == (x->sign ? 1 : -1))
5404 : {
5405 1950798 : if (add_significands (r, x, &u))
5406 : {
5407 : /* Overflow. Means the significand had been all ones, and
5408 : is now all zeros. Need to increase the exponent, and
5409 : possibly re-normalize it. */
5410 260311 : SET_REAL_EXP (r, REAL_EXP (r) + 1);
5411 260311 : if (REAL_EXP (r) > fmt->emax)
5412 : {
5413 146892 : get_inf (r, x->sign);
5414 146892 : return true;
5415 : }
5416 113419 : r->sig[SIGSZ - 1] = SIG_MSB;
5417 : }
5418 : }
5419 : else
5420 : {
5421 1020568 : if (REAL_EXP (x) > fmt->emin && x->sig[SIGSZ - 1] == SIG_MSB)
5422 : {
5423 : int i;
5424 1532928 : for (i = SIGSZ - 2; i >= 0; i--)
5425 1022635 : if (x->sig[i])
5426 : break;
5427 512342 : if (i < 0)
5428 : {
5429 : /* When mantissa is 1.0, we need to subtract only
5430 : half of u: nextafter (1.0, 0.0) is 1.0 - __DBL_EPSILON__ / 2
5431 : rather than 1.0 - __DBL_EPSILON__. */
5432 510293 : clear_significand_bit (&u, np2);
5433 510293 : np2--;
5434 510293 : set_significand_bit (&u, np2);
5435 : }
5436 : }
5437 1020568 : sub_significands (r, x, &u, 0);
5438 : }
5439 :
5440 : /* Clear out trailing garbage. */
5441 2999883 : clear_significand_below (r, np2);
5442 2999883 : normalize (r);
5443 2999883 : if (REAL_EXP (r) <= fmt->emin - fmt->p)
5444 : {
5445 0 : get_zero (r, x->sign);
5446 0 : return true;
5447 : }
5448 2999883 : return r->cl == rvc_zero || REAL_EXP (r) < fmt->emin;
5449 : }
5450 :
5451 : /* Write into BUF the maximum representable finite floating-point
5452 : number, (1 - b**-p) * b**emax for a given FP format FMT as a hex
5453 : float string. LEN is the size of BUF, and the buffer must be large
5454 : enough to contain the resulting string. If NORM_MAX, instead write
5455 : the maximum representable finite normalized floating-point number,
5456 : defined to be such that all choices of digits for that exponent are
5457 : representable in the format (this only makes a difference for IBM
5458 : long double). */
5459 :
5460 : void
5461 31158727 : get_max_float (const struct real_format *fmt, char *buf, size_t len,
5462 : bool norm_max)
5463 : {
5464 31158727 : if (fmt->b == 10)
5465 : {
5466 852955 : char *p = buf;
5467 20257670 : for (int i = fmt->p; i; i--)
5468 : {
5469 19404715 : *p++ = '9';
5470 19404715 : if (i == fmt->p)
5471 852955 : *p++ = '.';
5472 : }
5473 : /* fmt->p plus 1, to account for the decimal point and fmt->emax
5474 : minus 1 because the digits are nines, not 1.0. */
5475 852955 : sprintf (buf + fmt->p + 1, "E%d", fmt->emax - 1);
5476 852955 : gcc_assert (strlen (buf) < len);
5477 : return;
5478 : }
5479 :
5480 30305772 : int i, n;
5481 30305772 : char *p;
5482 30305772 : bool is_ibm_extended = fmt->pnan < fmt->p;
5483 :
5484 30305772 : strcpy (buf, "0x0.");
5485 30305772 : n = fmt->p;
5486 395022435 : for (i = 0, p = buf + 4; i + 3 < n; i += 4)
5487 364716663 : *p++ = 'f';
5488 30305772 : if (i < n)
5489 13543488 : *p++ = "08ce"[n - i];
5490 30305772 : sprintf (p, "p%d",
5491 30305772 : (is_ibm_extended && norm_max) ? fmt->emax - 1 : fmt->emax);
5492 30305772 : if (is_ibm_extended && !norm_max)
5493 : {
5494 : /* This is an IBM extended double format made up of two IEEE
5495 : doubles. The value of the long double is the sum of the
5496 : values of the two parts. The most significant part is
5497 : required to be the value of the long double rounded to the
5498 : nearest double. Rounding means we need a slightly smaller
5499 : value for LDBL_MAX. */
5500 0 : buf[4 + fmt->pnan / 4] = "7bde"[fmt->pnan % 4];
5501 : }
5502 :
5503 30305772 : gcc_assert (strlen (buf) < len);
5504 : }
5505 :
5506 : /* True if all values of integral type can be represented
5507 : by this floating-point type exactly. */
5508 :
5509 101117 : bool format_helper::can_represent_integral_type_p (tree type) const
5510 : {
5511 202234 : gcc_assert (! decimal_p () && INTEGRAL_TYPE_P (type));
5512 :
5513 : /* INT?_MIN is power-of-two so it takes
5514 : only one mantissa bit. */
5515 101117 : bool signed_p = TYPE_SIGN (type) == SIGNED;
5516 101117 : return TYPE_PRECISION (type) - signed_p <= significand_size (*this);
5517 : }
5518 :
5519 : /* True if all values in integer range *VR can be represented by this
5520 : floating-point type exactly. */
5521 :
5522 : bool
5523 15640 : format_helper::can_represent_range_value_p (const irange *vr) const
5524 : {
5525 15640 : gcc_assert (!decimal_p ());
5526 :
5527 15640 : if (vr->undefined_p () || vr->varying_p ())
5528 : return false;
5529 :
5530 1681 : tree type = vr->type ();
5531 1681 : unsigned precision = significand_size (*this);
5532 :
5533 1681 : if (TYPE_SIGN (type) == SIGNED)
5534 599 : precision++;
5535 :
5536 1681 : return range_fits_type_p (vr, precision, TYPE_SIGN (type));
5537 : }
5538 :
5539 : /* True if mode M has a NaN representation and
5540 : the treatment of NaN operands is important. */
5541 :
5542 : bool
5543 1356688602 : HONOR_NANS (machine_mode m)
5544 : {
5545 2475547325 : return MODE_HAS_NANS (m) && !flag_finite_math_only;
5546 : }
5547 :
5548 : bool
5549 418722540 : HONOR_NANS (const_tree t)
5550 : {
5551 418722540 : return HONOR_NANS (element_mode (t));
5552 : }
5553 :
5554 : bool
5555 650024492 : HONOR_NANS (const_rtx x)
5556 : {
5557 650024492 : return HONOR_NANS (GET_MODE (x));
5558 : }
5559 :
5560 : /* Like HONOR_NANs, but true if we honor signaling NaNs (or sNaNs). */
5561 :
5562 : bool
5563 451079265 : HONOR_SNANS (machine_mode m)
5564 : {
5565 451079265 : return flag_signaling_nans && HONOR_NANS (m);
5566 : }
5567 :
5568 : bool
5569 46821745 : HONOR_SNANS (const_tree t)
5570 : {
5571 46821745 : return HONOR_SNANS (element_mode (t));
5572 : }
5573 :
5574 : bool
5575 87626246 : HONOR_SNANS (const_rtx x)
5576 : {
5577 87626246 : return HONOR_SNANS (GET_MODE (x));
5578 : }
5579 :
5580 : /* As for HONOR_NANS, but true if the mode can represent infinity and
5581 : the treatment of infinite values is important. */
5582 :
5583 : bool
5584 366165737 : HONOR_INFINITIES (machine_mode m)
5585 : {
5586 1464654567 : return MODE_HAS_INFINITIES (m) && !flag_finite_math_only;
5587 : }
5588 :
5589 : bool
5590 366155338 : HONOR_INFINITIES (const_tree t)
5591 : {
5592 366155338 : return HONOR_INFINITIES (element_mode (t));
5593 : }
5594 :
5595 : bool
5596 0 : HONOR_INFINITIES (const_rtx x)
5597 : {
5598 0 : return HONOR_INFINITIES (GET_MODE (x));
5599 : }
5600 :
5601 : /* Like HONOR_NANS, but true if the given mode distinguishes between
5602 : positive and negative zero, and the sign of zero is important. */
5603 :
5604 : bool
5605 614551373 : HONOR_SIGNED_ZEROS (machine_mode m)
5606 : {
5607 1053904552 : return MODE_HAS_SIGNED_ZEROS (m) && flag_signed_zeros;
5608 : }
5609 :
5610 : bool
5611 191143197 : HONOR_SIGNED_ZEROS (const_tree t)
5612 : {
5613 191143197 : return HONOR_SIGNED_ZEROS (element_mode (t));
5614 : }
5615 :
5616 : bool
5617 530624 : HONOR_SIGNED_ZEROS (const_rtx x)
5618 : {
5619 530624 : return HONOR_SIGNED_ZEROS (GET_MODE (x));
5620 : }
5621 :
5622 : /* Like HONOR_NANS, but true if given mode supports sign-dependent rounding,
5623 : and the rounding mode is important. */
5624 :
5625 : bool
5626 363217047 : HONOR_SIGN_DEPENDENT_ROUNDING (machine_mode m)
5627 : {
5628 479576914 : return MODE_HAS_SIGN_DEPENDENT_ROUNDING (m) && flag_rounding_math;
5629 : }
5630 :
5631 : bool
5632 38827515 : HONOR_SIGN_DEPENDENT_ROUNDING (const_tree t)
5633 : {
5634 38827515 : return HONOR_SIGN_DEPENDENT_ROUNDING (element_mode (t));
5635 : }
5636 :
5637 : bool
5638 0 : HONOR_SIGN_DEPENDENT_ROUNDING (const_rtx x)
5639 : {
5640 0 : return HONOR_SIGN_DEPENDENT_ROUNDING (GET_MODE (x));
5641 : }
5642 :
5643 : /* Fills r with the largest value such that 1 + r*r won't overflow.
5644 : This is used in both sin (atan (x)) and cos (atan(x)) optimizations. */
5645 :
5646 : void
5647 77 : build_sinatan_real (REAL_VALUE_TYPE * r, tree type)
5648 : {
5649 77 : REAL_VALUE_TYPE maxval;
5650 77 : mpfr_t mpfr_const1, mpfr_c, mpfr_maxval;
5651 77 : machine_mode mode = TYPE_MODE (type);
5652 77 : const struct real_format * fmt = REAL_MODE_FORMAT (mode);
5653 :
5654 77 : real_maxval (&maxval, 0, mode);
5655 :
5656 77 : mpfr_inits (mpfr_const1, mpfr_c, mpfr_maxval, NULL);
5657 :
5658 77 : mpfr_from_real (mpfr_const1, &dconst1, MPFR_RNDN);
5659 77 : mpfr_from_real (mpfr_maxval, &maxval, MPFR_RNDN);
5660 :
5661 77 : mpfr_sub (mpfr_c, mpfr_maxval, mpfr_const1, MPFR_RNDN);
5662 77 : mpfr_sqrt (mpfr_c, mpfr_c, MPFR_RNDZ);
5663 :
5664 77 : real_from_mpfr (r, mpfr_c, fmt, MPFR_RNDZ);
5665 :
5666 77 : mpfr_clears (mpfr_const1, mpfr_c, mpfr_maxval, NULL);
5667 77 : }
|