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