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