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