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