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