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