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