OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [gnu-old/] [gcc-4.2.2/] [gcc/] [real.c] - Blame information for rev 822

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 38 julius
/* real.c - software floating point emulation.
2
   Copyright (C) 1993, 1994, 1995, 1996, 1997, 1998, 1999,
3
   2000, 2002, 2003, 2004, 2005, 2007 Free Software Foundation, Inc.
4
   Contributed by Stephen L. Moshier (moshier@world.std.com).
5
   Re-written by Richard Henderson <rth@redhat.com>
6
 
7
   This file is part of GCC.
8
 
9
   GCC is free software; you can redistribute it and/or modify it under
10
   the terms of the GNU General Public License as published by the Free
11
   Software Foundation; either version 3, or (at your option) any later
12
   version.
13
 
14
   GCC is distributed in the hope that it will be useful, but WITHOUT ANY
15
   WARRANTY; without even the implied warranty of MERCHANTABILITY or
16
   FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
17
   for more details.
18
 
19
   You should have received a copy of the GNU General Public License
20
   along with GCC; see the file COPYING3.  If not see
21
   <http://www.gnu.org/licenses/>.  */
22
 
23
#include "config.h"
24
#include "system.h"
25
#include "coretypes.h"
26
#include "tm.h"
27
#include "tree.h"
28
#include "toplev.h"
29
#include "real.h"
30
#include "tm_p.h"
31
#include "dfp.h"
32
 
33
/* The floating point model used internally is not exactly IEEE 754
34
   compliant, and close to the description in the ISO C99 standard,
35
   section 5.2.4.2.2 Characteristics of floating types.
36
 
37
   Specifically
38
 
39
        x = s * b^e * \sum_{k=1}^p f_k * b^{-k}
40
 
41
        where
42
                s = sign (+- 1)
43
                b = base or radix, here always 2
44
                e = exponent
45
                p = precision (the number of base-b digits in the significand)
46
                f_k = the digits of the significand.
47
 
48
   We differ from typical IEEE 754 encodings in that the entire
49
   significand is fractional.  Normalized significands are in the
50
   range [0.5, 1.0).
51
 
52
   A requirement of the model is that P be larger than the largest
53
   supported target floating-point type by at least 2 bits.  This gives
54
   us proper rounding when we truncate to the target type.  In addition,
55
   E must be large enough to hold the smallest supported denormal number
56
   in a normalized form.
57
 
58
   Both of these requirements are easily satisfied.  The largest target
59
   significand is 113 bits; we store at least 160.  The smallest
60
   denormal number fits in 17 exponent bits; we store 27.
61
 
62
   Note that the decimal string conversion routines are sensitive to
63
   rounding errors.  Since the raw arithmetic routines do not themselves
64
   have guard digits or rounding, the computation of 10**exp can
65
   accumulate more than a few digits of error.  The previous incarnation
66
   of real.c successfully used a 144-bit fraction; given the current
67
   layout of REAL_VALUE_TYPE we're forced to expand to at least 160 bits.
68
 
69
   Target floating point models that use base 16 instead of base 2
70
   (i.e. IBM 370), are handled during round_for_format, in which we
71
   canonicalize the exponent to be a multiple of 4 (log2(16)), and
72
   adjust the significand to match.  */
73
 
74
 
75
/* Used to classify two numbers simultaneously.  */
76
#define CLASS2(A, B)  ((A) << 2 | (B))
77
 
78
#if HOST_BITS_PER_LONG != 64 && HOST_BITS_PER_LONG != 32
79
 #error "Some constant folding done by hand to avoid shift count warnings"
80
#endif
81
 
82
static void get_zero (REAL_VALUE_TYPE *, int);
83
static void get_canonical_qnan (REAL_VALUE_TYPE *, int);
84
static void get_canonical_snan (REAL_VALUE_TYPE *, int);
85
static void get_inf (REAL_VALUE_TYPE *, int);
86
static bool sticky_rshift_significand (REAL_VALUE_TYPE *,
87
                                       const REAL_VALUE_TYPE *, unsigned int);
88
static void rshift_significand (REAL_VALUE_TYPE *, const REAL_VALUE_TYPE *,
89
                                unsigned int);
90
static void lshift_significand (REAL_VALUE_TYPE *, const REAL_VALUE_TYPE *,
91
                                unsigned int);
92
static void lshift_significand_1 (REAL_VALUE_TYPE *, const REAL_VALUE_TYPE *);
93
static bool add_significands (REAL_VALUE_TYPE *r, const REAL_VALUE_TYPE *,
94
                              const REAL_VALUE_TYPE *);
95
static bool sub_significands (REAL_VALUE_TYPE *, const REAL_VALUE_TYPE *,
96
                              const REAL_VALUE_TYPE *, int);
97
static void neg_significand (REAL_VALUE_TYPE *, const REAL_VALUE_TYPE *);
98
static int cmp_significands (const REAL_VALUE_TYPE *, const REAL_VALUE_TYPE *);
99
static int cmp_significand_0 (const REAL_VALUE_TYPE *);
100
static void set_significand_bit (REAL_VALUE_TYPE *, unsigned int);
101
static void clear_significand_bit (REAL_VALUE_TYPE *, unsigned int);
102
static bool test_significand_bit (REAL_VALUE_TYPE *, unsigned int);
103
static void clear_significand_below (REAL_VALUE_TYPE *, unsigned int);
104
static bool div_significands (REAL_VALUE_TYPE *, const REAL_VALUE_TYPE *,
105
                              const REAL_VALUE_TYPE *);
106
static void normalize (REAL_VALUE_TYPE *);
107
 
108
static bool do_add (REAL_VALUE_TYPE *, const REAL_VALUE_TYPE *,
109
                    const REAL_VALUE_TYPE *, int);
110
static bool do_multiply (REAL_VALUE_TYPE *, const REAL_VALUE_TYPE *,
111
                         const REAL_VALUE_TYPE *);
112
static bool do_divide (REAL_VALUE_TYPE *, const REAL_VALUE_TYPE *,
113
                       const REAL_VALUE_TYPE *);
114
static int do_compare (const REAL_VALUE_TYPE *, const REAL_VALUE_TYPE *, int);
115
static void do_fix_trunc (REAL_VALUE_TYPE *, const REAL_VALUE_TYPE *);
116
 
117
static unsigned long rtd_divmod (REAL_VALUE_TYPE *, REAL_VALUE_TYPE *);
118
 
119
static const REAL_VALUE_TYPE * ten_to_ptwo (int);
120
static const REAL_VALUE_TYPE * ten_to_mptwo (int);
121
static const REAL_VALUE_TYPE * real_digit (int);
122
static void times_pten (REAL_VALUE_TYPE *, int);
123
 
124
static void round_for_format (const struct real_format *, REAL_VALUE_TYPE *);
125
 
126
/* Initialize R with a positive zero.  */
127
 
128
static inline void
129
get_zero (REAL_VALUE_TYPE *r, int sign)
130
{
131
  memset (r, 0, sizeof (*r));
132
  r->sign = sign;
133
}
134
 
135
/* Initialize R with the canonical quiet NaN.  */
136
 
137
static inline void
138
get_canonical_qnan (REAL_VALUE_TYPE *r, int sign)
139
{
140
  memset (r, 0, sizeof (*r));
141
  r->cl = rvc_nan;
142
  r->sign = sign;
143
  r->canonical = 1;
144
}
145
 
146
static inline void
147
get_canonical_snan (REAL_VALUE_TYPE *r, int sign)
148
{
149
  memset (r, 0, sizeof (*r));
150
  r->cl = rvc_nan;
151
  r->sign = sign;
152
  r->signalling = 1;
153
  r->canonical = 1;
154
}
155
 
156
static inline void
157
get_inf (REAL_VALUE_TYPE *r, int sign)
158
{
159
  memset (r, 0, sizeof (*r));
160
  r->cl = rvc_inf;
161
  r->sign = sign;
162
}
163
 
164
 
165
/* Right-shift the significand of A by N bits; put the result in the
166
   significand of R.  If any one bits are shifted out, return true.  */
167
 
168
static bool
169
sticky_rshift_significand (REAL_VALUE_TYPE *r, const REAL_VALUE_TYPE *a,
170
                           unsigned int n)
171
{
172
  unsigned long sticky = 0;
173
  unsigned int i, ofs = 0;
174
 
175
  if (n >= HOST_BITS_PER_LONG)
176
    {
177
      for (i = 0, ofs = n / HOST_BITS_PER_LONG; i < ofs; ++i)
178
        sticky |= a->sig[i];
179
      n &= HOST_BITS_PER_LONG - 1;
180
    }
181
 
182
  if (n != 0)
183
    {
184
      sticky |= a->sig[ofs] & (((unsigned long)1 << n) - 1);
185
      for (i = 0; i < SIGSZ; ++i)
186
        {
187
          r->sig[i]
188
            = (((ofs + i >= SIGSZ ? 0 : a->sig[ofs + i]) >> n)
189
               | ((ofs + i + 1 >= SIGSZ ? 0 : a->sig[ofs + i + 1])
190
                  << (HOST_BITS_PER_LONG - n)));
191
        }
192
    }
193
  else
194
    {
195
      for (i = 0; ofs + i < SIGSZ; ++i)
196
        r->sig[i] = a->sig[ofs + i];
197
      for (; i < SIGSZ; ++i)
198
        r->sig[i] = 0;
199
    }
200
 
201
  return sticky != 0;
202
}
203
 
204
/* Right-shift the significand of A by N bits; put the result in the
205
   significand of R.  */
206
 
207
static void
208
rshift_significand (REAL_VALUE_TYPE *r, const REAL_VALUE_TYPE *a,
209
                    unsigned int n)
210
{
211
  unsigned int i, ofs = n / HOST_BITS_PER_LONG;
212
 
213
  n &= HOST_BITS_PER_LONG - 1;
214
  if (n != 0)
215
    {
216
      for (i = 0; i < SIGSZ; ++i)
217
        {
218
          r->sig[i]
219
            = (((ofs + i >= SIGSZ ? 0 : a->sig[ofs + i]) >> n)
220
               | ((ofs + i + 1 >= SIGSZ ? 0 : a->sig[ofs + i + 1])
221
                  << (HOST_BITS_PER_LONG - n)));
222
        }
223
    }
224
  else
225
    {
226
      for (i = 0; ofs + i < SIGSZ; ++i)
227
        r->sig[i] = a->sig[ofs + i];
228
      for (; i < SIGSZ; ++i)
229
        r->sig[i] = 0;
230
    }
231
}
232
 
233
/* Left-shift the significand of A by N bits; put the result in the
234
   significand of R.  */
235
 
236
static void
237
lshift_significand (REAL_VALUE_TYPE *r, const REAL_VALUE_TYPE *a,
238
                    unsigned int n)
239
{
240
  unsigned int i, ofs = n / HOST_BITS_PER_LONG;
241
 
242
  n &= HOST_BITS_PER_LONG - 1;
243
  if (n == 0)
244
    {
245
      for (i = 0; ofs + i < SIGSZ; ++i)
246
        r->sig[SIGSZ-1-i] = a->sig[SIGSZ-1-i-ofs];
247
      for (; i < SIGSZ; ++i)
248
        r->sig[SIGSZ-1-i] = 0;
249
    }
250
  else
251
    for (i = 0; i < SIGSZ; ++i)
252
      {
253
        r->sig[SIGSZ-1-i]
254
          = (((ofs + i >= SIGSZ ? 0 : a->sig[SIGSZ-1-i-ofs]) << n)
255
             | ((ofs + i + 1 >= SIGSZ ? 0 : a->sig[SIGSZ-1-i-ofs-1])
256
                >> (HOST_BITS_PER_LONG - n)));
257
      }
258
}
259
 
260
/* Likewise, but N is specialized to 1.  */
261
 
262
static inline void
263
lshift_significand_1 (REAL_VALUE_TYPE *r, const REAL_VALUE_TYPE *a)
264
{
265
  unsigned int i;
266
 
267
  for (i = SIGSZ - 1; i > 0; --i)
268
    r->sig[i] = (a->sig[i] << 1) | (a->sig[i-1] >> (HOST_BITS_PER_LONG - 1));
269
  r->sig[0] = a->sig[0] << 1;
270
}
271
 
272
/* Add the significands of A and B, placing the result in R.  Return
273
   true if there was carry out of the most significant word.  */
274
 
275
static inline bool
276
add_significands (REAL_VALUE_TYPE *r, const REAL_VALUE_TYPE *a,
277
                  const REAL_VALUE_TYPE *b)
278
{
279
  bool carry = false;
280
  int i;
281
 
282
  for (i = 0; i < SIGSZ; ++i)
283
    {
284
      unsigned long ai = a->sig[i];
285
      unsigned long ri = ai + b->sig[i];
286
 
287
      if (carry)
288
        {
289
          carry = ri < ai;
290
          carry |= ++ri == 0;
291
        }
292
      else
293
        carry = ri < ai;
294
 
295
      r->sig[i] = ri;
296
    }
297
 
298
  return carry;
299
}
300
 
301
/* Subtract the significands of A and B, placing the result in R.  CARRY is
302
   true if there's a borrow incoming to the least significant word.
303
   Return true if there was borrow out of the most significant word.  */
304
 
305
static inline bool
306
sub_significands (REAL_VALUE_TYPE *r, const REAL_VALUE_TYPE *a,
307
                  const REAL_VALUE_TYPE *b, int carry)
308
{
309
  int i;
310
 
311
  for (i = 0; i < SIGSZ; ++i)
312
    {
313
      unsigned long ai = a->sig[i];
314
      unsigned long ri = ai - b->sig[i];
315
 
316
      if (carry)
317
        {
318
          carry = ri > ai;
319
          carry |= ~--ri == 0;
320
        }
321
      else
322
        carry = ri > ai;
323
 
324
      r->sig[i] = ri;
325
    }
326
 
327
  return carry;
328
}
329
 
330
/* Negate the significand A, placing the result in R.  */
331
 
332
static inline void
333
neg_significand (REAL_VALUE_TYPE *r, const REAL_VALUE_TYPE *a)
334
{
335
  bool carry = true;
336
  int i;
337
 
338
  for (i = 0; i < SIGSZ; ++i)
339
    {
340
      unsigned long ri, ai = a->sig[i];
341
 
342
      if (carry)
343
        {
344
          if (ai)
345
            {
346
              ri = -ai;
347
              carry = false;
348
            }
349
          else
350
            ri = ai;
351
        }
352
      else
353
        ri = ~ai;
354
 
355
      r->sig[i] = ri;
356
    }
357
}
358
 
359
/* Compare significands.  Return tri-state vs zero.  */
360
 
361
static inline int
362
cmp_significands (const REAL_VALUE_TYPE *a, const REAL_VALUE_TYPE *b)
363
{
364
  int i;
365
 
366
  for (i = SIGSZ - 1; i >= 0; --i)
367
    {
368
      unsigned long ai = a->sig[i];
369
      unsigned long bi = b->sig[i];
370
 
371
      if (ai > bi)
372
        return 1;
373
      if (ai < bi)
374
        return -1;
375
    }
376
 
377
  return 0;
378
}
379
 
380
/* Return true if A is nonzero.  */
381
 
382
static inline int
383
cmp_significand_0 (const REAL_VALUE_TYPE *a)
384
{
385
  int i;
386
 
387
  for (i = SIGSZ - 1; i >= 0; --i)
388
    if (a->sig[i])
389
      return 1;
390
 
391
  return 0;
392
}
393
 
394
/* Set bit N of the significand of R.  */
395
 
396
static inline void
397
set_significand_bit (REAL_VALUE_TYPE *r, unsigned int n)
398
{
399
  r->sig[n / HOST_BITS_PER_LONG]
400
    |= (unsigned long)1 << (n % HOST_BITS_PER_LONG);
401
}
402
 
403
/* Clear bit N of the significand of R.  */
404
 
405
static inline void
406
clear_significand_bit (REAL_VALUE_TYPE *r, unsigned int n)
407
{
408
  r->sig[n / HOST_BITS_PER_LONG]
409
    &= ~((unsigned long)1 << (n % HOST_BITS_PER_LONG));
410
}
411
 
412
/* Test bit N of the significand of R.  */
413
 
414
static inline bool
415
test_significand_bit (REAL_VALUE_TYPE *r, unsigned int n)
416
{
417
  /* ??? Compiler bug here if we return this expression directly.
418
     The conversion to bool strips the "&1" and we wind up testing
419
     e.g. 2 != 0 -> true.  Seen in gcc version 3.2 20020520.  */
420
  int t = (r->sig[n / HOST_BITS_PER_LONG] >> (n % HOST_BITS_PER_LONG)) & 1;
421
  return t;
422
}
423
 
424
/* Clear bits 0..N-1 of the significand of R.  */
425
 
426
static void
427
clear_significand_below (REAL_VALUE_TYPE *r, unsigned int n)
428
{
429
  int i, w = n / HOST_BITS_PER_LONG;
430
 
431
  for (i = 0; i < w; ++i)
432
    r->sig[i] = 0;
433
 
434
  r->sig[w] &= ~(((unsigned long)1 << (n % HOST_BITS_PER_LONG)) - 1);
435
}
436
 
437
/* Divide the significands of A and B, placing the result in R.  Return
438
   true if the division was inexact.  */
439
 
440
static inline bool
441
div_significands (REAL_VALUE_TYPE *r, const REAL_VALUE_TYPE *a,
442
                  const REAL_VALUE_TYPE *b)
443
{
444
  REAL_VALUE_TYPE u;
445
  int i, bit = SIGNIFICAND_BITS - 1;
446
  unsigned long msb, inexact;
447
 
448
  u = *a;
449
  memset (r->sig, 0, sizeof (r->sig));
450
 
451
  msb = 0;
452
  goto start;
453
  do
454
    {
455
      msb = u.sig[SIGSZ-1] & SIG_MSB;
456
      lshift_significand_1 (&u, &u);
457
    start:
458
      if (msb || cmp_significands (&u, b) >= 0)
459
        {
460
          sub_significands (&u, &u, b, 0);
461
          set_significand_bit (r, bit);
462
        }
463
    }
464
  while (--bit >= 0);
465
 
466
  for (i = 0, inexact = 0; i < SIGSZ; i++)
467
    inexact |= u.sig[i];
468
 
469
  return inexact != 0;
470
}
471
 
472
/* Adjust the exponent and significand of R such that the most
473
   significant bit is set.  We underflow to zero and overflow to
474
   infinity here, without denormals.  (The intermediate representation
475
   exponent is large enough to handle target denormals normalized.)  */
476
 
477
static void
478
normalize (REAL_VALUE_TYPE *r)
479
{
480
  int shift = 0, exp;
481
  int i, j;
482
 
483
  if (r->decimal)
484
    return;
485
 
486
  /* Find the first word that is nonzero.  */
487
  for (i = SIGSZ - 1; i >= 0; i--)
488
    if (r->sig[i] == 0)
489
      shift += HOST_BITS_PER_LONG;
490
    else
491
      break;
492
 
493
  /* Zero significand flushes to zero.  */
494
  if (i < 0)
495
    {
496
      r->cl = rvc_zero;
497
      SET_REAL_EXP (r, 0);
498
      return;
499
    }
500
 
501
  /* Find the first bit that is nonzero.  */
502
  for (j = 0; ; j++)
503
    if (r->sig[i] & ((unsigned long)1 << (HOST_BITS_PER_LONG - 1 - j)))
504
      break;
505
  shift += j;
506
 
507
  if (shift > 0)
508
    {
509
      exp = REAL_EXP (r) - shift;
510
      if (exp > MAX_EXP)
511
        get_inf (r, r->sign);
512
      else if (exp < -MAX_EXP)
513
        get_zero (r, r->sign);
514
      else
515
        {
516
          SET_REAL_EXP (r, exp);
517
          lshift_significand (r, r, shift);
518
        }
519
    }
520
}
521
 
522
/* Calculate R = A + (SUBTRACT_P ? -B : B).  Return true if the
523
   result may be inexact due to a loss of precision.  */
524
 
525
static bool
526
do_add (REAL_VALUE_TYPE *r, const REAL_VALUE_TYPE *a,
527
        const REAL_VALUE_TYPE *b, int subtract_p)
528
{
529
  int dexp, sign, exp;
530
  REAL_VALUE_TYPE t;
531
  bool inexact = false;
532
 
533
  /* Determine if we need to add or subtract.  */
534
  sign = a->sign;
535
  subtract_p = (sign ^ b->sign) ^ subtract_p;
536
 
537
  switch (CLASS2 (a->cl, b->cl))
538
    {
539
    case CLASS2 (rvc_zero, rvc_zero):
540
      /* -0 + -0 = -0, -0 - +0 = -0; all other cases yield +0.  */
541
      get_zero (r, sign & !subtract_p);
542
      return false;
543
 
544
    case CLASS2 (rvc_zero, rvc_normal):
545
    case CLASS2 (rvc_zero, rvc_inf):
546
    case CLASS2 (rvc_zero, rvc_nan):
547
      /* 0 + ANY = ANY.  */
548
    case CLASS2 (rvc_normal, rvc_nan):
549
    case CLASS2 (rvc_inf, rvc_nan):
550
    case CLASS2 (rvc_nan, rvc_nan):
551
      /* ANY + NaN = NaN.  */
552
    case CLASS2 (rvc_normal, rvc_inf):
553
      /* R + Inf = Inf.  */
554
      *r = *b;
555
      r->sign = sign ^ subtract_p;
556
      return false;
557
 
558
    case CLASS2 (rvc_normal, rvc_zero):
559
    case CLASS2 (rvc_inf, rvc_zero):
560
    case CLASS2 (rvc_nan, rvc_zero):
561
      /* ANY + 0 = ANY.  */
562
    case CLASS2 (rvc_nan, rvc_normal):
563
    case CLASS2 (rvc_nan, rvc_inf):
564
      /* NaN + ANY = NaN.  */
565
    case CLASS2 (rvc_inf, rvc_normal):
566
      /* Inf + R = Inf.  */
567
      *r = *a;
568
      return false;
569
 
570
    case CLASS2 (rvc_inf, rvc_inf):
571
      if (subtract_p)
572
        /* Inf - Inf = NaN.  */
573
        get_canonical_qnan (r, 0);
574
      else
575
        /* Inf + Inf = Inf.  */
576
        *r = *a;
577
      return false;
578
 
579
    case CLASS2 (rvc_normal, rvc_normal):
580
      break;
581
 
582
    default:
583
      gcc_unreachable ();
584
    }
585
 
586
  /* Swap the arguments such that A has the larger exponent.  */
587
  dexp = REAL_EXP (a) - REAL_EXP (b);
588
  if (dexp < 0)
589
    {
590
      const REAL_VALUE_TYPE *t;
591
      t = a, a = b, b = t;
592
      dexp = -dexp;
593
      sign ^= subtract_p;
594
    }
595
  exp = REAL_EXP (a);
596
 
597
  /* If the exponents are not identical, we need to shift the
598
     significand of B down.  */
599
  if (dexp > 0)
600
    {
601
      /* If the exponents are too far apart, the significands
602
         do not overlap, which makes the subtraction a noop.  */
603
      if (dexp >= SIGNIFICAND_BITS)
604
        {
605
          *r = *a;
606
          r->sign = sign;
607
          return true;
608
        }
609
 
610
      inexact |= sticky_rshift_significand (&t, b, dexp);
611
      b = &t;
612
    }
613
 
614
  if (subtract_p)
615
    {
616
      if (sub_significands (r, a, b, inexact))
617
        {
618
          /* We got a borrow out of the subtraction.  That means that
619
             A and B had the same exponent, and B had the larger
620
             significand.  We need to swap the sign and negate the
621
             significand.  */
622
          sign ^= 1;
623
          neg_significand (r, r);
624
        }
625
    }
626
  else
627
    {
628
      if (add_significands (r, a, b))
629
        {
630
          /* We got carry out of the addition.  This means we need to
631
             shift the significand back down one bit and increase the
632
             exponent.  */
633
          inexact |= sticky_rshift_significand (r, r, 1);
634
          r->sig[SIGSZ-1] |= SIG_MSB;
635
          if (++exp > MAX_EXP)
636
            {
637
              get_inf (r, sign);
638
              return true;
639
            }
640
        }
641
    }
642
 
643
  r->cl = rvc_normal;
644
  r->sign = sign;
645
  SET_REAL_EXP (r, exp);
646
  /* Zero out the remaining fields.  */
647
  r->signalling = 0;
648
  r->canonical = 0;
649
  r->decimal = 0;
650
 
651
  /* Re-normalize the result.  */
652
  normalize (r);
653
 
654
  /* Special case: if the subtraction results in zero, the result
655
     is positive.  */
656
  if (r->cl == rvc_zero)
657
    r->sign = 0;
658
  else
659
    r->sig[0] |= inexact;
660
 
661
  return inexact;
662
}
663
 
664
/* Calculate R = A * B.  Return true if the result may be inexact.  */
665
 
666
static bool
667
do_multiply (REAL_VALUE_TYPE *r, const REAL_VALUE_TYPE *a,
668
             const REAL_VALUE_TYPE *b)
669
{
670
  REAL_VALUE_TYPE u, t, *rr;
671
  unsigned int i, j, k;
672
  int sign = a->sign ^ b->sign;
673
  bool inexact = false;
674
 
675
  switch (CLASS2 (a->cl, b->cl))
676
    {
677
    case CLASS2 (rvc_zero, rvc_zero):
678
    case CLASS2 (rvc_zero, rvc_normal):
679
    case CLASS2 (rvc_normal, rvc_zero):
680
      /* +-0 * ANY = 0 with appropriate sign.  */
681
      get_zero (r, sign);
682
      return false;
683
 
684
    case CLASS2 (rvc_zero, rvc_nan):
685
    case CLASS2 (rvc_normal, rvc_nan):
686
    case CLASS2 (rvc_inf, rvc_nan):
687
    case CLASS2 (rvc_nan, rvc_nan):
688
      /* ANY * NaN = NaN.  */
689
      *r = *b;
690
      r->sign = sign;
691
      return false;
692
 
693
    case CLASS2 (rvc_nan, rvc_zero):
694
    case CLASS2 (rvc_nan, rvc_normal):
695
    case CLASS2 (rvc_nan, rvc_inf):
696
      /* NaN * ANY = NaN.  */
697
      *r = *a;
698
      r->sign = sign;
699
      return false;
700
 
701
    case CLASS2 (rvc_zero, rvc_inf):
702
    case CLASS2 (rvc_inf, rvc_zero):
703
      /* 0 * Inf = NaN */
704
      get_canonical_qnan (r, sign);
705
      return false;
706
 
707
    case CLASS2 (rvc_inf, rvc_inf):
708
    case CLASS2 (rvc_normal, rvc_inf):
709
    case CLASS2 (rvc_inf, rvc_normal):
710
      /* Inf * Inf = Inf, R * Inf = Inf */
711
      get_inf (r, sign);
712
      return false;
713
 
714
    case CLASS2 (rvc_normal, rvc_normal):
715
      break;
716
 
717
    default:
718
      gcc_unreachable ();
719
    }
720
 
721
  if (r == a || r == b)
722
    rr = &t;
723
  else
724
    rr = r;
725
  get_zero (rr, 0);
726
 
727
  /* Collect all the partial products.  Since we don't have sure access
728
     to a widening multiply, we split each long into two half-words.
729
 
730
     Consider the long-hand form of a four half-word multiplication:
731
 
732
                 A  B  C  D
733
              *  E  F  G  H
734
             --------------
735
                DE DF DG DH
736
             CE CF CG CH
737
          BE BF BG BH
738
       AE AF AG AH
739
 
740
     We construct partial products of the widened half-word products
741
     that are known to not overlap, e.g. DF+DH.  Each such partial
742
     product is given its proper exponent, which allows us to sum them
743
     and obtain the finished product.  */
744
 
745
  for (i = 0; i < SIGSZ * 2; ++i)
746
    {
747
      unsigned long ai = a->sig[i / 2];
748
      if (i & 1)
749
        ai >>= HOST_BITS_PER_LONG / 2;
750
      else
751
        ai &= ((unsigned long)1 << (HOST_BITS_PER_LONG / 2)) - 1;
752
 
753
      if (ai == 0)
754
        continue;
755
 
756
      for (j = 0; j < 2; ++j)
757
        {
758
          int exp = (REAL_EXP (a) - (2*SIGSZ-1-i)*(HOST_BITS_PER_LONG/2)
759
                     + (REAL_EXP (b) - (1-j)*(HOST_BITS_PER_LONG/2)));
760
 
761
          if (exp > MAX_EXP)
762
            {
763
              get_inf (r, sign);
764
              return true;
765
            }
766
          if (exp < -MAX_EXP)
767
            {
768
              /* Would underflow to zero, which we shouldn't bother adding.  */
769
              inexact = true;
770
              continue;
771
            }
772
 
773
          memset (&u, 0, sizeof (u));
774
          u.cl = rvc_normal;
775
          SET_REAL_EXP (&u, exp);
776
 
777
          for (k = j; k < SIGSZ * 2; k += 2)
778
            {
779
              unsigned long bi = b->sig[k / 2];
780
              if (k & 1)
781
                bi >>= HOST_BITS_PER_LONG / 2;
782
              else
783
                bi &= ((unsigned long)1 << (HOST_BITS_PER_LONG / 2)) - 1;
784
 
785
              u.sig[k / 2] = ai * bi;
786
            }
787
 
788
          normalize (&u);
789
          inexact |= do_add (rr, rr, &u, 0);
790
        }
791
    }
792
 
793
  rr->sign = sign;
794
  if (rr != r)
795
    *r = t;
796
 
797
  return inexact;
798
}
799
 
800
/* Calculate R = A / B.  Return true if the result may be inexact.  */
801
 
802
static bool
803
do_divide (REAL_VALUE_TYPE *r, const REAL_VALUE_TYPE *a,
804
           const REAL_VALUE_TYPE *b)
805
{
806
  int exp, sign = a->sign ^ b->sign;
807
  REAL_VALUE_TYPE t, *rr;
808
  bool inexact;
809
 
810
  switch (CLASS2 (a->cl, b->cl))
811
    {
812
    case CLASS2 (rvc_zero, rvc_zero):
813
      /* 0 / 0 = NaN.  */
814
    case CLASS2 (rvc_inf, rvc_inf):
815
      /* Inf / Inf = NaN.  */
816
      get_canonical_qnan (r, sign);
817
      return false;
818
 
819
    case CLASS2 (rvc_zero, rvc_normal):
820
    case CLASS2 (rvc_zero, rvc_inf):
821
      /* 0 / ANY = 0.  */
822
    case CLASS2 (rvc_normal, rvc_inf):
823
      /* R / Inf = 0.  */
824
      get_zero (r, sign);
825
      return false;
826
 
827
    case CLASS2 (rvc_normal, rvc_zero):
828
      /* R / 0 = Inf.  */
829
    case CLASS2 (rvc_inf, rvc_zero):
830
      /* Inf / 0 = Inf.  */
831
      get_inf (r, sign);
832
      return false;
833
 
834
    case CLASS2 (rvc_zero, rvc_nan):
835
    case CLASS2 (rvc_normal, rvc_nan):
836
    case CLASS2 (rvc_inf, rvc_nan):
837
    case CLASS2 (rvc_nan, rvc_nan):
838
      /* ANY / NaN = NaN.  */
839
      *r = *b;
840
      r->sign = sign;
841
      return false;
842
 
843
    case CLASS2 (rvc_nan, rvc_zero):
844
    case CLASS2 (rvc_nan, rvc_normal):
845
    case CLASS2 (rvc_nan, rvc_inf):
846
      /* NaN / ANY = NaN.  */
847
      *r = *a;
848
      r->sign = sign;
849
      return false;
850
 
851
    case CLASS2 (rvc_inf, rvc_normal):
852
      /* Inf / R = Inf.  */
853
      get_inf (r, sign);
854
      return false;
855
 
856
    case CLASS2 (rvc_normal, rvc_normal):
857
      break;
858
 
859
    default:
860
      gcc_unreachable ();
861
    }
862
 
863
  if (r == a || r == b)
864
    rr = &t;
865
  else
866
    rr = r;
867
 
868
  /* Make sure all fields in the result are initialized.  */
869
  get_zero (rr, 0);
870
  rr->cl = rvc_normal;
871
  rr->sign = sign;
872
 
873
  exp = REAL_EXP (a) - REAL_EXP (b) + 1;
874
  if (exp > MAX_EXP)
875
    {
876
      get_inf (r, sign);
877
      return true;
878
    }
879
  if (exp < -MAX_EXP)
880
    {
881
      get_zero (r, sign);
882
      return true;
883
    }
884
  SET_REAL_EXP (rr, exp);
885
 
886
  inexact = div_significands (rr, a, b);
887
 
888
  /* Re-normalize the result.  */
889
  normalize (rr);
890
  rr->sig[0] |= inexact;
891
 
892
  if (rr != r)
893
    *r = t;
894
 
895
  return inexact;
896
}
897
 
898
/* Return a tri-state comparison of A vs B.  Return NAN_RESULT if
899
   one of the two operands is a NaN.  */
900
 
901
static int
902
do_compare (const REAL_VALUE_TYPE *a, const REAL_VALUE_TYPE *b,
903
            int nan_result)
904
{
905
  int ret;
906
 
907
  switch (CLASS2 (a->cl, b->cl))
908
    {
909
    case CLASS2 (rvc_zero, rvc_zero):
910
      /* Sign of zero doesn't matter for compares.  */
911
      return 0;
912
 
913
    case CLASS2 (rvc_inf, rvc_zero):
914
    case CLASS2 (rvc_inf, rvc_normal):
915
    case CLASS2 (rvc_normal, rvc_zero):
916
      return (a->sign ? -1 : 1);
917
 
918
    case CLASS2 (rvc_inf, rvc_inf):
919
      return -a->sign - -b->sign;
920
 
921
    case CLASS2 (rvc_zero, rvc_normal):
922
    case CLASS2 (rvc_zero, rvc_inf):
923
    case CLASS2 (rvc_normal, rvc_inf):
924
      return (b->sign ? 1 : -1);
925
 
926
    case CLASS2 (rvc_zero, rvc_nan):
927
    case CLASS2 (rvc_normal, rvc_nan):
928
    case CLASS2 (rvc_inf, rvc_nan):
929
    case CLASS2 (rvc_nan, rvc_nan):
930
    case CLASS2 (rvc_nan, rvc_zero):
931
    case CLASS2 (rvc_nan, rvc_normal):
932
    case CLASS2 (rvc_nan, rvc_inf):
933
      return nan_result;
934
 
935
    case CLASS2 (rvc_normal, rvc_normal):
936
      break;
937
 
938
    default:
939
      gcc_unreachable ();
940
    }
941
 
942
  if (a->sign != b->sign)
943
    return -a->sign - -b->sign;
944
 
945
  if (a->decimal || b->decimal)
946
    return decimal_do_compare (a, b, nan_result);
947
 
948
  if (REAL_EXP (a) > REAL_EXP (b))
949
    ret = 1;
950
  else if (REAL_EXP (a) < REAL_EXP (b))
951
    ret = -1;
952
  else
953
    ret = cmp_significands (a, b);
954
 
955
  return (a->sign ? -ret : ret);
956
}
957
 
958
/* Return A truncated to an integral value toward zero.  */
959
 
960
static void
961
do_fix_trunc (REAL_VALUE_TYPE *r, const REAL_VALUE_TYPE *a)
962
{
963
  *r = *a;
964
 
965
  switch (r->cl)
966
    {
967
    case rvc_zero:
968
    case rvc_inf:
969
    case rvc_nan:
970
      break;
971
 
972
    case rvc_normal:
973
      if (r->decimal)
974
        {
975
          decimal_do_fix_trunc (r, a);
976
          return;
977
        }
978
      if (REAL_EXP (r) <= 0)
979
        get_zero (r, r->sign);
980
      else if (REAL_EXP (r) < SIGNIFICAND_BITS)
981
        clear_significand_below (r, SIGNIFICAND_BITS - REAL_EXP (r));
982
      break;
983
 
984
    default:
985
      gcc_unreachable ();
986
    }
987
}
988
 
989
/* Perform the binary or unary operation described by CODE.
990
   For a unary operation, leave OP1 NULL.  This function returns
991
   true if the result may be inexact due to loss of precision.  */
992
 
993
bool
994
real_arithmetic (REAL_VALUE_TYPE *r, int icode, const REAL_VALUE_TYPE *op0,
995
                 const REAL_VALUE_TYPE *op1)
996
{
997
  enum tree_code code = icode;
998
 
999
  if (op0->decimal || (op1 && op1->decimal))
1000
    return decimal_real_arithmetic (r, icode, op0, op1);
1001
 
1002
  switch (code)
1003
    {
1004
    case PLUS_EXPR:
1005
      return do_add (r, op0, op1, 0);
1006
 
1007
    case MINUS_EXPR:
1008
      return do_add (r, op0, op1, 1);
1009
 
1010
    case MULT_EXPR:
1011
      return do_multiply (r, op0, op1);
1012
 
1013
    case RDIV_EXPR:
1014
      return do_divide (r, op0, op1);
1015
 
1016
    case MIN_EXPR:
1017
      if (op1->cl == rvc_nan)
1018
        *r = *op1;
1019
      else if (do_compare (op0, op1, -1) < 0)
1020
        *r = *op0;
1021
      else
1022
        *r = *op1;
1023
      break;
1024
 
1025
    case MAX_EXPR:
1026
      if (op1->cl == rvc_nan)
1027
        *r = *op1;
1028
      else if (do_compare (op0, op1, 1) < 0)
1029
        *r = *op1;
1030
      else
1031
        *r = *op0;
1032
      break;
1033
 
1034
    case NEGATE_EXPR:
1035
      *r = *op0;
1036
      r->sign ^= 1;
1037
      break;
1038
 
1039
    case ABS_EXPR:
1040
      *r = *op0;
1041
      r->sign = 0;
1042
      break;
1043
 
1044
    case FIX_TRUNC_EXPR:
1045
      do_fix_trunc (r, op0);
1046
      break;
1047
 
1048
    default:
1049
      gcc_unreachable ();
1050
    }
1051
  return false;
1052
}
1053
 
1054
/* Legacy.  Similar, but return the result directly.  */
1055
 
1056
REAL_VALUE_TYPE
1057
real_arithmetic2 (int icode, const REAL_VALUE_TYPE *op0,
1058
                  const REAL_VALUE_TYPE *op1)
1059
{
1060
  REAL_VALUE_TYPE r;
1061
  real_arithmetic (&r, icode, op0, op1);
1062
  return r;
1063
}
1064
 
1065
bool
1066
real_compare (int icode, const REAL_VALUE_TYPE *op0,
1067
              const REAL_VALUE_TYPE *op1)
1068
{
1069
  enum tree_code code = icode;
1070
 
1071
  switch (code)
1072
    {
1073
    case LT_EXPR:
1074
      return do_compare (op0, op1, 1) < 0;
1075
    case LE_EXPR:
1076
      return do_compare (op0, op1, 1) <= 0;
1077
    case GT_EXPR:
1078
      return do_compare (op0, op1, -1) > 0;
1079
    case GE_EXPR:
1080
      return do_compare (op0, op1, -1) >= 0;
1081
    case EQ_EXPR:
1082
      return do_compare (op0, op1, -1) == 0;
1083
    case NE_EXPR:
1084
      return do_compare (op0, op1, -1) != 0;
1085
    case UNORDERED_EXPR:
1086
      return op0->cl == rvc_nan || op1->cl == rvc_nan;
1087
    case ORDERED_EXPR:
1088
      return op0->cl != rvc_nan && op1->cl != rvc_nan;
1089
    case UNLT_EXPR:
1090
      return do_compare (op0, op1, -1) < 0;
1091
    case UNLE_EXPR:
1092
      return do_compare (op0, op1, -1) <= 0;
1093
    case UNGT_EXPR:
1094
      return do_compare (op0, op1, 1) > 0;
1095
    case UNGE_EXPR:
1096
      return do_compare (op0, op1, 1) >= 0;
1097
    case UNEQ_EXPR:
1098
      return do_compare (op0, op1, 0) == 0;
1099
    case LTGT_EXPR:
1100
      return do_compare (op0, op1, 0) != 0;
1101
 
1102
    default:
1103
      gcc_unreachable ();
1104
    }
1105
}
1106
 
1107
/* Return floor log2(R).  */
1108
 
1109
int
1110
real_exponent (const REAL_VALUE_TYPE *r)
1111
{
1112
  switch (r->cl)
1113
    {
1114
    case rvc_zero:
1115
      return 0;
1116
    case rvc_inf:
1117
    case rvc_nan:
1118
      return (unsigned int)-1 >> 1;
1119
    case rvc_normal:
1120
      return REAL_EXP (r);
1121
    default:
1122
      gcc_unreachable ();
1123
    }
1124
}
1125
 
1126
/* R = OP0 * 2**EXP.  */
1127
 
1128
void
1129
real_ldexp (REAL_VALUE_TYPE *r, const REAL_VALUE_TYPE *op0, int exp)
1130
{
1131
  *r = *op0;
1132
  switch (r->cl)
1133
    {
1134
    case rvc_zero:
1135
    case rvc_inf:
1136
    case rvc_nan:
1137
      break;
1138
 
1139
    case rvc_normal:
1140
      exp += REAL_EXP (op0);
1141
      if (exp > MAX_EXP)
1142
        get_inf (r, r->sign);
1143
      else if (exp < -MAX_EXP)
1144
        get_zero (r, r->sign);
1145
      else
1146
        SET_REAL_EXP (r, exp);
1147
      break;
1148
 
1149
    default:
1150
      gcc_unreachable ();
1151
    }
1152
}
1153
 
1154
/* Determine whether a floating-point value X is infinite.  */
1155
 
1156
bool
1157
real_isinf (const REAL_VALUE_TYPE *r)
1158
{
1159
  return (r->cl == rvc_inf);
1160
}
1161
 
1162
/* Determine whether a floating-point value X is a NaN.  */
1163
 
1164
bool
1165
real_isnan (const REAL_VALUE_TYPE *r)
1166
{
1167
  return (r->cl == rvc_nan);
1168
}
1169
 
1170
/* Determine whether a floating-point value X is negative.  */
1171
 
1172
bool
1173
real_isneg (const REAL_VALUE_TYPE *r)
1174
{
1175
  return r->sign;
1176
}
1177
 
1178
/* Determine whether a floating-point value X is minus zero.  */
1179
 
1180
bool
1181
real_isnegzero (const REAL_VALUE_TYPE *r)
1182
{
1183
  return r->sign && r->cl == rvc_zero;
1184
}
1185
 
1186
/* Compare two floating-point objects for bitwise identity.  */
1187
 
1188
bool
1189
real_identical (const REAL_VALUE_TYPE *a, const REAL_VALUE_TYPE *b)
1190
{
1191
  int i;
1192
 
1193
  if (a->cl != b->cl)
1194
    return false;
1195
  if (a->sign != b->sign)
1196
    return false;
1197
 
1198
  switch (a->cl)
1199
    {
1200
    case rvc_zero:
1201
    case rvc_inf:
1202
      return true;
1203
 
1204
    case rvc_normal:
1205
      if (a->decimal != b->decimal)
1206
        return false;
1207
      if (REAL_EXP (a) != REAL_EXP (b))
1208
        return false;
1209
      break;
1210
 
1211
    case rvc_nan:
1212
      if (a->signalling != b->signalling)
1213
        return false;
1214
      /* The significand is ignored for canonical NaNs.  */
1215
      if (a->canonical || b->canonical)
1216
        return a->canonical == b->canonical;
1217
      break;
1218
 
1219
    default:
1220
      gcc_unreachable ();
1221
    }
1222
 
1223
  for (i = 0; i < SIGSZ; ++i)
1224
    if (a->sig[i] != b->sig[i])
1225
      return false;
1226
 
1227
  return true;
1228
}
1229
 
1230
/* Try to change R into its exact multiplicative inverse in machine
1231
   mode MODE.  Return true if successful.  */
1232
 
1233
bool
1234
exact_real_inverse (enum machine_mode mode, REAL_VALUE_TYPE *r)
1235
{
1236
  const REAL_VALUE_TYPE *one = real_digit (1);
1237
  REAL_VALUE_TYPE u;
1238
  int i;
1239
 
1240
  if (r->cl != rvc_normal)
1241
    return false;
1242
 
1243
  /* Check for a power of two: all significand bits zero except the MSB.  */
1244
  for (i = 0; i < SIGSZ-1; ++i)
1245
    if (r->sig[i] != 0)
1246
      return false;
1247
  if (r->sig[SIGSZ-1] != SIG_MSB)
1248
    return false;
1249
 
1250
  /* Find the inverse and truncate to the required mode.  */
1251
  do_divide (&u, one, r);
1252
  real_convert (&u, mode, &u);
1253
 
1254
  /* The rounding may have overflowed.  */
1255
  if (u.cl != rvc_normal)
1256
    return false;
1257
  for (i = 0; i < SIGSZ-1; ++i)
1258
    if (u.sig[i] != 0)
1259
      return false;
1260
  if (u.sig[SIGSZ-1] != SIG_MSB)
1261
    return false;
1262
 
1263
  *r = u;
1264
  return true;
1265
}
1266
 
1267
/* Render R as an integer.  */
1268
 
1269
HOST_WIDE_INT
1270
real_to_integer (const REAL_VALUE_TYPE *r)
1271
{
1272
  unsigned HOST_WIDE_INT i;
1273
 
1274
  switch (r->cl)
1275
    {
1276
    case rvc_zero:
1277
    underflow:
1278
      return 0;
1279
 
1280
    case rvc_inf:
1281
    case rvc_nan:
1282
    overflow:
1283
      i = (unsigned HOST_WIDE_INT) 1 << (HOST_BITS_PER_WIDE_INT - 1);
1284
      if (!r->sign)
1285
        i--;
1286
      return i;
1287
 
1288
    case rvc_normal:
1289
      if (r->decimal)
1290
        return decimal_real_to_integer (r);
1291
 
1292
      if (REAL_EXP (r) <= 0)
1293
        goto underflow;
1294
      /* Only force overflow for unsigned overflow.  Signed overflow is
1295
         undefined, so it doesn't matter what we return, and some callers
1296
         expect to be able to use this routine for both signed and
1297
         unsigned conversions.  */
1298
      if (REAL_EXP (r) > HOST_BITS_PER_WIDE_INT)
1299
        goto overflow;
1300
 
1301
      if (HOST_BITS_PER_WIDE_INT == HOST_BITS_PER_LONG)
1302
        i = r->sig[SIGSZ-1];
1303
      else
1304
        {
1305
          gcc_assert (HOST_BITS_PER_WIDE_INT == 2 * HOST_BITS_PER_LONG);
1306
          i = r->sig[SIGSZ-1];
1307
          i = i << (HOST_BITS_PER_LONG - 1) << 1;
1308
          i |= r->sig[SIGSZ-2];
1309
        }
1310
 
1311
      i >>= HOST_BITS_PER_WIDE_INT - REAL_EXP (r);
1312
 
1313
      if (r->sign)
1314
        i = -i;
1315
      return i;
1316
 
1317
    default:
1318
      gcc_unreachable ();
1319
    }
1320
}
1321
 
1322
/* Likewise, but to an integer pair, HI+LOW.  */
1323
 
1324
void
1325
real_to_integer2 (HOST_WIDE_INT *plow, HOST_WIDE_INT *phigh,
1326
                  const REAL_VALUE_TYPE *r)
1327
{
1328
  REAL_VALUE_TYPE t;
1329
  HOST_WIDE_INT low, high;
1330
  int exp;
1331
 
1332
  switch (r->cl)
1333
    {
1334
    case rvc_zero:
1335
    underflow:
1336
      low = high = 0;
1337
      break;
1338
 
1339
    case rvc_inf:
1340
    case rvc_nan:
1341
    overflow:
1342
      high = (unsigned HOST_WIDE_INT) 1 << (HOST_BITS_PER_WIDE_INT - 1);
1343
      if (r->sign)
1344
        low = 0;
1345
      else
1346
        {
1347
          high--;
1348
          low = -1;
1349
        }
1350
      break;
1351
 
1352
    case rvc_normal:
1353
      if (r->decimal)
1354
        {
1355
          decimal_real_to_integer2 (plow, phigh, r);
1356
          return;
1357
        }
1358
 
1359
      exp = REAL_EXP (r);
1360
      if (exp <= 0)
1361
        goto underflow;
1362
      /* Only force overflow for unsigned overflow.  Signed overflow is
1363
         undefined, so it doesn't matter what we return, and some callers
1364
         expect to be able to use this routine for both signed and
1365
         unsigned conversions.  */
1366
      if (exp > 2*HOST_BITS_PER_WIDE_INT)
1367
        goto overflow;
1368
 
1369
      rshift_significand (&t, r, 2*HOST_BITS_PER_WIDE_INT - exp);
1370
      if (HOST_BITS_PER_WIDE_INT == HOST_BITS_PER_LONG)
1371
        {
1372
          high = t.sig[SIGSZ-1];
1373
          low = t.sig[SIGSZ-2];
1374
        }
1375
      else
1376
        {
1377
          gcc_assert (HOST_BITS_PER_WIDE_INT == 2*HOST_BITS_PER_LONG);
1378
          high = t.sig[SIGSZ-1];
1379
          high = high << (HOST_BITS_PER_LONG - 1) << 1;
1380
          high |= t.sig[SIGSZ-2];
1381
 
1382
          low = t.sig[SIGSZ-3];
1383
          low = low << (HOST_BITS_PER_LONG - 1) << 1;
1384
          low |= t.sig[SIGSZ-4];
1385
        }
1386
 
1387
      if (r->sign)
1388
        {
1389
          if (low == 0)
1390
            high = -high;
1391
          else
1392
            low = -low, high = ~high;
1393
        }
1394
      break;
1395
 
1396
    default:
1397
      gcc_unreachable ();
1398
    }
1399
 
1400
  *plow = low;
1401
  *phigh = high;
1402
}
1403
 
1404
/* A subroutine of real_to_decimal.  Compute the quotient and remainder
1405
   of NUM / DEN.  Return the quotient and place the remainder in NUM.
1406
   It is expected that NUM / DEN are close enough that the quotient is
1407
   small.  */
1408
 
1409
static unsigned long
1410
rtd_divmod (REAL_VALUE_TYPE *num, REAL_VALUE_TYPE *den)
1411
{
1412
  unsigned long q, msb;
1413
  int expn = REAL_EXP (num), expd = REAL_EXP (den);
1414
 
1415
  if (expn < expd)
1416
    return 0;
1417
 
1418
  q = msb = 0;
1419
  goto start;
1420
  do
1421
    {
1422
      msb = num->sig[SIGSZ-1] & SIG_MSB;
1423
      q <<= 1;
1424
      lshift_significand_1 (num, num);
1425
    start:
1426
      if (msb || cmp_significands (num, den) >= 0)
1427
        {
1428
          sub_significands (num, num, den, 0);
1429
          q |= 1;
1430
        }
1431
    }
1432
  while (--expn >= expd);
1433
 
1434
  SET_REAL_EXP (num, expd);
1435
  normalize (num);
1436
 
1437
  return q;
1438
}
1439
 
1440
/* Render R as a decimal floating point constant.  Emit DIGITS significant
1441
   digits in the result, bounded by BUF_SIZE.  If DIGITS is 0, choose the
1442
   maximum for the representation.  If CROP_TRAILING_ZEROS, strip trailing
1443
   zeros.  */
1444
 
1445
#define M_LOG10_2       0.30102999566398119521
1446
 
1447
void
1448
real_to_decimal (char *str, const REAL_VALUE_TYPE *r_orig, size_t buf_size,
1449
                 size_t digits, int crop_trailing_zeros)
1450
{
1451
  const REAL_VALUE_TYPE *one, *ten;
1452
  REAL_VALUE_TYPE r, pten, u, v;
1453
  int dec_exp, cmp_one, digit;
1454
  size_t max_digits;
1455
  char *p, *first, *last;
1456
  bool sign;
1457
 
1458
  r = *r_orig;
1459
  switch (r.cl)
1460
    {
1461
    case rvc_zero:
1462
      strcpy (str, (r.sign ? "-0.0" : "0.0"));
1463
      return;
1464
    case rvc_normal:
1465
      break;
1466
    case rvc_inf:
1467
      strcpy (str, (r.sign ? "-Inf" : "+Inf"));
1468
      return;
1469
    case rvc_nan:
1470
      /* ??? Print the significand as well, if not canonical?  */
1471
      strcpy (str, (r.sign ? "-NaN" : "+NaN"));
1472
      return;
1473
    default:
1474
      gcc_unreachable ();
1475
    }
1476
 
1477
  if (r.decimal)
1478
    {
1479
      decimal_real_to_decimal (str, &r, buf_size, digits, crop_trailing_zeros);
1480
      return;
1481
    }
1482
 
1483
  /* Bound the number of digits printed by the size of the representation.  */
1484
  max_digits = SIGNIFICAND_BITS * M_LOG10_2;
1485
  if (digits == 0 || digits > max_digits)
1486
    digits = max_digits;
1487
 
1488
  /* Estimate the decimal exponent, and compute the length of the string it
1489
     will print as.  Be conservative and add one to account for possible
1490
     overflow or rounding error.  */
1491
  dec_exp = REAL_EXP (&r) * M_LOG10_2;
1492
  for (max_digits = 1; dec_exp ; max_digits++)
1493
    dec_exp /= 10;
1494
 
1495
  /* Bound the number of digits printed by the size of the output buffer.  */
1496
  max_digits = buf_size - 1 - 1 - 2 - max_digits - 1;
1497
  gcc_assert (max_digits <= buf_size);
1498
  if (digits > max_digits)
1499
    digits = max_digits;
1500
 
1501
  one = real_digit (1);
1502
  ten = ten_to_ptwo (0);
1503
 
1504
  sign = r.sign;
1505
  r.sign = 0;
1506
 
1507
  dec_exp = 0;
1508
  pten = *one;
1509
 
1510
  cmp_one = do_compare (&r, one, 0);
1511
  if (cmp_one > 0)
1512
    {
1513
      int m;
1514
 
1515
      /* Number is greater than one.  Convert significand to an integer
1516
         and strip trailing decimal zeros.  */
1517
 
1518
      u = r;
1519
      SET_REAL_EXP (&u, SIGNIFICAND_BITS - 1);
1520
 
1521
      /* Largest M, such that 10**2**M fits within SIGNIFICAND_BITS.  */
1522
      m = floor_log2 (max_digits);
1523
 
1524
      /* Iterate over the bits of the possible powers of 10 that might
1525
         be present in U and eliminate them.  That is, if we find that
1526
         10**2**M divides U evenly, keep the division and increase
1527
         DEC_EXP by 2**M.  */
1528
      do
1529
        {
1530
          REAL_VALUE_TYPE t;
1531
 
1532
          do_divide (&t, &u, ten_to_ptwo (m));
1533
          do_fix_trunc (&v, &t);
1534
          if (cmp_significands (&v, &t) == 0)
1535
            {
1536
              u = t;
1537
              dec_exp += 1 << m;
1538
            }
1539
        }
1540
      while (--m >= 0);
1541
 
1542
      /* Revert the scaling to integer that we performed earlier.  */
1543
      SET_REAL_EXP (&u, REAL_EXP (&u) + REAL_EXP (&r)
1544
                    - (SIGNIFICAND_BITS - 1));
1545
      r = u;
1546
 
1547
      /* Find power of 10.  Do this by dividing out 10**2**M when
1548
         this is larger than the current remainder.  Fill PTEN with
1549
         the power of 10 that we compute.  */
1550
      if (REAL_EXP (&r) > 0)
1551
        {
1552
          m = floor_log2 ((int)(REAL_EXP (&r) * M_LOG10_2)) + 1;
1553
          do
1554
            {
1555
              const REAL_VALUE_TYPE *ptentwo = ten_to_ptwo (m);
1556
              if (do_compare (&u, ptentwo, 0) >= 0)
1557
                {
1558
                  do_divide (&u, &u, ptentwo);
1559
                  do_multiply (&pten, &pten, ptentwo);
1560
                  dec_exp += 1 << m;
1561
                }
1562
            }
1563
          while (--m >= 0);
1564
        }
1565
      else
1566
        /* We managed to divide off enough tens in the above reduction
1567
           loop that we've now got a negative exponent.  Fall into the
1568
           less-than-one code to compute the proper value for PTEN.  */
1569
        cmp_one = -1;
1570
    }
1571
  if (cmp_one < 0)
1572
    {
1573
      int m;
1574
 
1575
      /* Number is less than one.  Pad significand with leading
1576
         decimal zeros.  */
1577
 
1578
      v = r;
1579
      while (1)
1580
        {
1581
          /* Stop if we'd shift bits off the bottom.  */
1582
          if (v.sig[0] & 7)
1583
            break;
1584
 
1585
          do_multiply (&u, &v, ten);
1586
 
1587
          /* Stop if we're now >= 1.  */
1588
          if (REAL_EXP (&u) > 0)
1589
            break;
1590
 
1591
          v = u;
1592
          dec_exp -= 1;
1593
        }
1594
      r = v;
1595
 
1596
      /* Find power of 10.  Do this by multiplying in P=10**2**M when
1597
         the current remainder is smaller than 1/P.  Fill PTEN with the
1598
         power of 10 that we compute.  */
1599
      m = floor_log2 ((int)(-REAL_EXP (&r) * M_LOG10_2)) + 1;
1600
      do
1601
        {
1602
          const REAL_VALUE_TYPE *ptentwo = ten_to_ptwo (m);
1603
          const REAL_VALUE_TYPE *ptenmtwo = ten_to_mptwo (m);
1604
 
1605
          if (do_compare (&v, ptenmtwo, 0) <= 0)
1606
            {
1607
              do_multiply (&v, &v, ptentwo);
1608
              do_multiply (&pten, &pten, ptentwo);
1609
              dec_exp -= 1 << m;
1610
            }
1611
        }
1612
      while (--m >= 0);
1613
 
1614
      /* Invert the positive power of 10 that we've collected so far.  */
1615
      do_divide (&pten, one, &pten);
1616
    }
1617
 
1618
  p = str;
1619
  if (sign)
1620
    *p++ = '-';
1621
  first = p++;
1622
 
1623
  /* At this point, PTEN should contain the nearest power of 10 smaller
1624
     than R, such that this division produces the first digit.
1625
 
1626
     Using a divide-step primitive that returns the complete integral
1627
     remainder avoids the rounding error that would be produced if
1628
     we were to use do_divide here and then simply multiply by 10 for
1629
     each subsequent digit.  */
1630
 
1631
  digit = rtd_divmod (&r, &pten);
1632
 
1633
  /* Be prepared for error in that division via underflow ...  */
1634
  if (digit == 0 && cmp_significand_0 (&r))
1635
    {
1636
      /* Multiply by 10 and try again.  */
1637
      do_multiply (&r, &r, ten);
1638
      digit = rtd_divmod (&r, &pten);
1639
      dec_exp -= 1;
1640
      gcc_assert (digit != 0);
1641
    }
1642
 
1643
  /* ... or overflow.  */
1644
  if (digit == 10)
1645
    {
1646
      *p++ = '1';
1647
      if (--digits > 0)
1648
        *p++ = '0';
1649
      dec_exp += 1;
1650
    }
1651
  else
1652
    {
1653
      gcc_assert (digit <= 10);
1654
      *p++ = digit + '0';
1655
    }
1656
 
1657
  /* Generate subsequent digits.  */
1658
  while (--digits > 0)
1659
    {
1660
      do_multiply (&r, &r, ten);
1661
      digit = rtd_divmod (&r, &pten);
1662
      *p++ = digit + '0';
1663
    }
1664
  last = p;
1665
 
1666
  /* Generate one more digit with which to do rounding.  */
1667
  do_multiply (&r, &r, ten);
1668
  digit = rtd_divmod (&r, &pten);
1669
 
1670
  /* Round the result.  */
1671
  if (digit == 5)
1672
    {
1673
      /* Round to nearest.  If R is nonzero there are additional
1674
         nonzero digits to be extracted.  */
1675
      if (cmp_significand_0 (&r))
1676
        digit++;
1677
      /* Round to even.  */
1678
      else if ((p[-1] - '0') & 1)
1679
        digit++;
1680
    }
1681
  if (digit > 5)
1682
    {
1683
      while (p > first)
1684
        {
1685
          digit = *--p;
1686
          if (digit == '9')
1687
            *p = '0';
1688
          else
1689
            {
1690
              *p = digit + 1;
1691
              break;
1692
            }
1693
        }
1694
 
1695
      /* Carry out of the first digit.  This means we had all 9's and
1696
         now have all 0's.  "Prepend" a 1 by overwriting the first 0.  */
1697
      if (p == first)
1698
        {
1699
          first[1] = '1';
1700
          dec_exp++;
1701
        }
1702
    }
1703
 
1704
  /* Insert the decimal point.  */
1705
  first[0] = first[1];
1706
  first[1] = '.';
1707
 
1708
  /* If requested, drop trailing zeros.  Never crop past "1.0".  */
1709
  if (crop_trailing_zeros)
1710
    while (last > first + 3 && last[-1] == '0')
1711
      last--;
1712
 
1713
  /* Append the exponent.  */
1714
  sprintf (last, "e%+d", dec_exp);
1715
}
1716
 
1717
/* Render R as a hexadecimal floating point constant.  Emit DIGITS
1718
   significant digits in the result, bounded by BUF_SIZE.  If DIGITS is 0,
1719
   choose the maximum for the representation.  If CROP_TRAILING_ZEROS,
1720
   strip trailing zeros.  */
1721
 
1722
void
1723
real_to_hexadecimal (char *str, const REAL_VALUE_TYPE *r, size_t buf_size,
1724
                     size_t digits, int crop_trailing_zeros)
1725
{
1726
  int i, j, exp = REAL_EXP (r);
1727
  char *p, *first;
1728
  char exp_buf[16];
1729
  size_t max_digits;
1730
 
1731
  switch (r->cl)
1732
    {
1733
    case rvc_zero:
1734
      exp = 0;
1735
      break;
1736
    case rvc_normal:
1737
      break;
1738
    case rvc_inf:
1739
      strcpy (str, (r->sign ? "-Inf" : "+Inf"));
1740
      return;
1741
    case rvc_nan:
1742
      /* ??? Print the significand as well, if not canonical?  */
1743
      strcpy (str, (r->sign ? "-NaN" : "+NaN"));
1744
      return;
1745
    default:
1746
      gcc_unreachable ();
1747
    }
1748
 
1749
  if (r->decimal)
1750
    {
1751
      /* Hexadecimal format for decimal floats is not interesting. */
1752
      strcpy (str, "N/A");
1753
      return;
1754
    }
1755
 
1756
  if (digits == 0)
1757
    digits = SIGNIFICAND_BITS / 4;
1758
 
1759
  /* Bound the number of digits printed by the size of the output buffer.  */
1760
 
1761
  sprintf (exp_buf, "p%+d", exp);
1762
  max_digits = buf_size - strlen (exp_buf) - r->sign - 4 - 1;
1763
  gcc_assert (max_digits <= buf_size);
1764
  if (digits > max_digits)
1765
    digits = max_digits;
1766
 
1767
  p = str;
1768
  if (r->sign)
1769
    *p++ = '-';
1770
  *p++ = '0';
1771
  *p++ = 'x';
1772
  *p++ = '0';
1773
  *p++ = '.';
1774
  first = p;
1775
 
1776
  for (i = SIGSZ - 1; i >= 0; --i)
1777
    for (j = HOST_BITS_PER_LONG - 4; j >= 0; j -= 4)
1778
      {
1779
        *p++ = "0123456789abcdef"[(r->sig[i] >> j) & 15];
1780
        if (--digits == 0)
1781
          goto out;
1782
      }
1783
 
1784
 out:
1785
  if (crop_trailing_zeros)
1786
    while (p > first + 1 && p[-1] == '0')
1787
      p--;
1788
 
1789
  sprintf (p, "p%+d", exp);
1790
}
1791
 
1792
/* Initialize R from a decimal or hexadecimal string.  The string is
1793
   assumed to have been syntax checked already.  */
1794
 
1795
void
1796
real_from_string (REAL_VALUE_TYPE *r, const char *str)
1797
{
1798
  int exp = 0;
1799
  bool sign = false;
1800
 
1801
  get_zero (r, 0);
1802
 
1803
  if (*str == '-')
1804
    {
1805
      sign = true;
1806
      str++;
1807
    }
1808
  else if (*str == '+')
1809
    str++;
1810
 
1811
  if (str[0] == '0' && (str[1] == 'x' || str[1] == 'X'))
1812
    {
1813
      /* Hexadecimal floating point.  */
1814
      int pos = SIGNIFICAND_BITS - 4, d;
1815
 
1816
      str += 2;
1817
 
1818
      while (*str == '0')
1819
        str++;
1820
      while (1)
1821
        {
1822
          d = hex_value (*str);
1823
          if (d == _hex_bad)
1824
            break;
1825
          if (pos >= 0)
1826
            {
1827
              r->sig[pos / HOST_BITS_PER_LONG]
1828
                |= (unsigned long) d << (pos % HOST_BITS_PER_LONG);
1829
              pos -= 4;
1830
            }
1831
          else if (d)
1832
            /* Ensure correct rounding by setting last bit if there is
1833
               a subsequent nonzero digit.  */
1834
            r->sig[0] |= 1;
1835
          exp += 4;
1836
          str++;
1837
        }
1838
      if (*str == '.')
1839
        {
1840
          str++;
1841
          if (pos == SIGNIFICAND_BITS - 4)
1842
            {
1843
              while (*str == '0')
1844
                str++, exp -= 4;
1845
            }
1846
          while (1)
1847
            {
1848
              d = hex_value (*str);
1849
              if (d == _hex_bad)
1850
                break;
1851
              if (pos >= 0)
1852
                {
1853
                  r->sig[pos / HOST_BITS_PER_LONG]
1854
                    |= (unsigned long) d << (pos % HOST_BITS_PER_LONG);
1855
                  pos -= 4;
1856
                }
1857
              else if (d)
1858
                /* Ensure correct rounding by setting last bit if there is
1859
                   a subsequent nonzero digit.  */
1860
                r->sig[0] |= 1;
1861
              str++;
1862
            }
1863
        }
1864
 
1865
      /* If the mantissa is zero, ignore the exponent.  */
1866
      if (!cmp_significand_0 (r))
1867
        goto underflow;
1868
 
1869
      if (*str == 'p' || *str == 'P')
1870
        {
1871
          bool exp_neg = false;
1872
 
1873
          str++;
1874
          if (*str == '-')
1875
            {
1876
              exp_neg = true;
1877
              str++;
1878
            }
1879
          else if (*str == '+')
1880
            str++;
1881
 
1882
          d = 0;
1883
          while (ISDIGIT (*str))
1884
            {
1885
              d *= 10;
1886
              d += *str - '0';
1887
              if (d > MAX_EXP)
1888
                {
1889
                  /* Overflowed the exponent.  */
1890
                  if (exp_neg)
1891
                    goto underflow;
1892
                  else
1893
                    goto overflow;
1894
                }
1895
              str++;
1896
            }
1897
          if (exp_neg)
1898
            d = -d;
1899
 
1900
          exp += d;
1901
        }
1902
 
1903
      r->cl = rvc_normal;
1904
      SET_REAL_EXP (r, exp);
1905
 
1906
      normalize (r);
1907
    }
1908
  else
1909
    {
1910
      /* Decimal floating point.  */
1911
      const REAL_VALUE_TYPE *ten = ten_to_ptwo (0);
1912
      int d;
1913
 
1914
      while (*str == '0')
1915
        str++;
1916
      while (ISDIGIT (*str))
1917
        {
1918
          d = *str++ - '0';
1919
          do_multiply (r, r, ten);
1920
          if (d)
1921
            do_add (r, r, real_digit (d), 0);
1922
        }
1923
      if (*str == '.')
1924
        {
1925
          str++;
1926
          if (r->cl == rvc_zero)
1927
            {
1928
              while (*str == '0')
1929
                str++, exp--;
1930
            }
1931
          while (ISDIGIT (*str))
1932
            {
1933
              d = *str++ - '0';
1934
              do_multiply (r, r, ten);
1935
              if (d)
1936
                do_add (r, r, real_digit (d), 0);
1937
              exp--;
1938
            }
1939
        }
1940
 
1941
      /* If the mantissa is zero, ignore the exponent.  */
1942
      if (r->cl == rvc_zero)
1943
        goto underflow;
1944
 
1945
      if (*str == 'e' || *str == 'E')
1946
        {
1947
          bool exp_neg = false;
1948
 
1949
          str++;
1950
          if (*str == '-')
1951
            {
1952
              exp_neg = true;
1953
              str++;
1954
            }
1955
          else if (*str == '+')
1956
            str++;
1957
 
1958
          d = 0;
1959
          while (ISDIGIT (*str))
1960
            {
1961
              d *= 10;
1962
              d += *str - '0';
1963
              if (d > MAX_EXP)
1964
                {
1965
                  /* Overflowed the exponent.  */
1966
                  if (exp_neg)
1967
                    goto underflow;
1968
                  else
1969
                    goto overflow;
1970
                }
1971
              str++;
1972
            }
1973
          if (exp_neg)
1974
            d = -d;
1975
          exp += d;
1976
        }
1977
 
1978
      if (exp)
1979
        times_pten (r, exp);
1980
    }
1981
 
1982
  r->sign = sign;
1983
  return;
1984
 
1985
 underflow:
1986
  get_zero (r, sign);
1987
  return;
1988
 
1989
 overflow:
1990
  get_inf (r, sign);
1991
  return;
1992
}
1993
 
1994
/* Legacy.  Similar, but return the result directly.  */
1995
 
1996
REAL_VALUE_TYPE
1997
real_from_string2 (const char *s, enum machine_mode mode)
1998
{
1999
  REAL_VALUE_TYPE r;
2000
 
2001
  real_from_string (&r, s);
2002
  if (mode != VOIDmode)
2003
    real_convert (&r, mode, &r);
2004
 
2005
  return r;
2006
}
2007
 
2008
/* Initialize R from string S and desired MODE. */
2009
 
2010
void
2011
real_from_string3 (REAL_VALUE_TYPE *r, const char *s, enum machine_mode mode)
2012
{
2013
  if (DECIMAL_FLOAT_MODE_P (mode))
2014
    decimal_real_from_string (r, s);
2015
  else
2016
    real_from_string (r, s);
2017
 
2018
  if (mode != VOIDmode)
2019
    real_convert (r, mode, r);
2020
}
2021
 
2022
/* Initialize R from the integer pair HIGH+LOW.  */
2023
 
2024
void
2025
real_from_integer (REAL_VALUE_TYPE *r, enum machine_mode mode,
2026
                   unsigned HOST_WIDE_INT low, HOST_WIDE_INT high,
2027
                   int unsigned_p)
2028
{
2029
  if (low == 0 && high == 0)
2030
    get_zero (r, 0);
2031
  else
2032
    {
2033
      memset (r, 0, sizeof (*r));
2034
      r->cl = rvc_normal;
2035
      r->sign = high < 0 && !unsigned_p;
2036
      SET_REAL_EXP (r, 2 * HOST_BITS_PER_WIDE_INT);
2037
 
2038
      if (r->sign)
2039
        {
2040
          high = ~high;
2041
          if (low == 0)
2042
            high += 1;
2043
          else
2044
            low = -low;
2045
        }
2046
 
2047
      if (HOST_BITS_PER_LONG == HOST_BITS_PER_WIDE_INT)
2048
        {
2049
          r->sig[SIGSZ-1] = high;
2050
          r->sig[SIGSZ-2] = low;
2051
        }
2052
      else
2053
        {
2054
          gcc_assert (HOST_BITS_PER_LONG*2 == HOST_BITS_PER_WIDE_INT);
2055
          r->sig[SIGSZ-1] = high >> (HOST_BITS_PER_LONG - 1) >> 1;
2056
          r->sig[SIGSZ-2] = high;
2057
          r->sig[SIGSZ-3] = low >> (HOST_BITS_PER_LONG - 1) >> 1;
2058
          r->sig[SIGSZ-4] = low;
2059
        }
2060
 
2061
      normalize (r);
2062
    }
2063
 
2064
  if (mode != VOIDmode)
2065
    real_convert (r, mode, r);
2066
}
2067
 
2068
/* Returns 10**2**N.  */
2069
 
2070
static const REAL_VALUE_TYPE *
2071
ten_to_ptwo (int n)
2072
{
2073
  static REAL_VALUE_TYPE tens[EXP_BITS];
2074
 
2075
  gcc_assert (n >= 0);
2076
  gcc_assert (n < EXP_BITS);
2077
 
2078
  if (tens[n].cl == rvc_zero)
2079
    {
2080
      if (n < (HOST_BITS_PER_WIDE_INT == 64 ? 5 : 4))
2081
        {
2082
          HOST_WIDE_INT t = 10;
2083
          int i;
2084
 
2085
          for (i = 0; i < n; ++i)
2086
            t *= t;
2087
 
2088
          real_from_integer (&tens[n], VOIDmode, t, 0, 1);
2089
        }
2090
      else
2091
        {
2092
          const REAL_VALUE_TYPE *t = ten_to_ptwo (n - 1);
2093
          do_multiply (&tens[n], t, t);
2094
        }
2095
    }
2096
 
2097
  return &tens[n];
2098
}
2099
 
2100
/* Returns 10**(-2**N).  */
2101
 
2102
static const REAL_VALUE_TYPE *
2103
ten_to_mptwo (int n)
2104
{
2105
  static REAL_VALUE_TYPE tens[EXP_BITS];
2106
 
2107
  gcc_assert (n >= 0);
2108
  gcc_assert (n < EXP_BITS);
2109
 
2110
  if (tens[n].cl == rvc_zero)
2111
    do_divide (&tens[n], real_digit (1), ten_to_ptwo (n));
2112
 
2113
  return &tens[n];
2114
}
2115
 
2116
/* Returns N.  */
2117
 
2118
static const REAL_VALUE_TYPE *
2119
real_digit (int n)
2120
{
2121
  static REAL_VALUE_TYPE num[10];
2122
 
2123
  gcc_assert (n >= 0);
2124
  gcc_assert (n <= 9);
2125
 
2126
  if (n > 0 && num[n].cl == rvc_zero)
2127
    real_from_integer (&num[n], VOIDmode, n, 0, 1);
2128
 
2129
  return &num[n];
2130
}
2131
 
2132
/* Multiply R by 10**EXP.  */
2133
 
2134
static void
2135
times_pten (REAL_VALUE_TYPE *r, int exp)
2136
{
2137
  REAL_VALUE_TYPE pten, *rr;
2138
  bool negative = (exp < 0);
2139
  int i;
2140
 
2141
  if (negative)
2142
    {
2143
      exp = -exp;
2144
      pten = *real_digit (1);
2145
      rr = &pten;
2146
    }
2147
  else
2148
    rr = r;
2149
 
2150
  for (i = 0; exp > 0; ++i, exp >>= 1)
2151
    if (exp & 1)
2152
      do_multiply (rr, rr, ten_to_ptwo (i));
2153
 
2154
  if (negative)
2155
    do_divide (r, r, &pten);
2156
}
2157
 
2158
/* Fills R with +Inf.  */
2159
 
2160
void
2161
real_inf (REAL_VALUE_TYPE *r)
2162
{
2163
  get_inf (r, 0);
2164
}
2165
 
2166
/* Fills R with a NaN whose significand is described by STR.  If QUIET,
2167
   we force a QNaN, else we force an SNaN.  The string, if not empty,
2168
   is parsed as a number and placed in the significand.  Return true
2169
   if the string was successfully parsed.  */
2170
 
2171
bool
2172
real_nan (REAL_VALUE_TYPE *r, const char *str, int quiet,
2173
          enum machine_mode mode)
2174
{
2175
  const struct real_format *fmt;
2176
 
2177
  fmt = REAL_MODE_FORMAT (mode);
2178
  gcc_assert (fmt);
2179
 
2180
  if (*str == 0)
2181
    {
2182
      if (quiet)
2183
        get_canonical_qnan (r, 0);
2184
      else
2185
        get_canonical_snan (r, 0);
2186
    }
2187
  else
2188
    {
2189
      int base = 10, d;
2190
 
2191
      memset (r, 0, sizeof (*r));
2192
      r->cl = rvc_nan;
2193
 
2194
      /* Parse akin to strtol into the significand of R.  */
2195
 
2196
      while (ISSPACE (*str))
2197
        str++;
2198
      if (*str == '-')
2199
        str++;
2200
      else if (*str == '+')
2201
        str++;
2202
      if (*str == '0')
2203
        {
2204
          str++;
2205
          if (*str == 'x' || *str == 'X')
2206
            {
2207
              base = 16;
2208
              str++;
2209
            }
2210
          else
2211
            base = 8;
2212
        }
2213
 
2214
      while ((d = hex_value (*str)) < base)
2215
        {
2216
          REAL_VALUE_TYPE u;
2217
 
2218
          switch (base)
2219
            {
2220
            case 8:
2221
              lshift_significand (r, r, 3);
2222
              break;
2223
            case 16:
2224
              lshift_significand (r, r, 4);
2225
              break;
2226
            case 10:
2227
              lshift_significand_1 (&u, r);
2228
              lshift_significand (r, r, 3);
2229
              add_significands (r, r, &u);
2230
              break;
2231
            default:
2232
              gcc_unreachable ();
2233
            }
2234
 
2235
          get_zero (&u, 0);
2236
          u.sig[0] = d;
2237
          add_significands (r, r, &u);
2238
 
2239
          str++;
2240
        }
2241
 
2242
      /* Must have consumed the entire string for success.  */
2243
      if (*str != 0)
2244
        return false;
2245
 
2246
      /* Shift the significand into place such that the bits
2247
         are in the most significant bits for the format.  */
2248
      lshift_significand (r, r, SIGNIFICAND_BITS - fmt->pnan);
2249
 
2250
      /* Our MSB is always unset for NaNs.  */
2251
      r->sig[SIGSZ-1] &= ~SIG_MSB;
2252
 
2253
      /* Force quiet or signalling NaN.  */
2254
      r->signalling = !quiet;
2255
    }
2256
 
2257
  return true;
2258
}
2259
 
2260
/* Fills R with the largest finite value representable in mode MODE.
2261
   If SIGN is nonzero, R is set to the most negative finite value.  */
2262
 
2263
void
2264
real_maxval (REAL_VALUE_TYPE *r, int sign, enum machine_mode mode)
2265
{
2266
  const struct real_format *fmt;
2267
  int np2;
2268
 
2269
  fmt = REAL_MODE_FORMAT (mode);
2270
  gcc_assert (fmt);
2271
  memset (r, 0, sizeof (*r));
2272
 
2273
  if (fmt->b == 10)
2274
    decimal_real_maxval (r, sign, mode);
2275
  else
2276
    {
2277
      r->cl = rvc_normal;
2278
      r->sign = sign;
2279
      SET_REAL_EXP (r, fmt->emax * fmt->log2_b);
2280
 
2281
      np2 = SIGNIFICAND_BITS - fmt->p * fmt->log2_b;
2282
      memset (r->sig, -1, SIGSZ * sizeof (unsigned long));
2283
      clear_significand_below (r, np2);
2284
 
2285
      if (fmt->pnan < fmt->p)
2286
        /* This is an IBM extended double format made up of two IEEE
2287
           doubles.  The value of the long double is the sum of the
2288
           values of the two parts.  The most significant part is
2289
           required to be the value of the long double rounded to the
2290
           nearest double.  Rounding means we need a slightly smaller
2291
           value for LDBL_MAX.  */
2292
        clear_significand_bit (r, SIGNIFICAND_BITS - fmt->pnan);
2293
    }
2294
}
2295
 
2296
/* Fills R with 2**N.  */
2297
 
2298
void
2299
real_2expN (REAL_VALUE_TYPE *r, int n)
2300
{
2301
  memset (r, 0, sizeof (*r));
2302
 
2303
  n++;
2304
  if (n > MAX_EXP)
2305
    r->cl = rvc_inf;
2306
  else if (n < -MAX_EXP)
2307
    ;
2308
  else
2309
    {
2310
      r->cl = rvc_normal;
2311
      SET_REAL_EXP (r, n);
2312
      r->sig[SIGSZ-1] = SIG_MSB;
2313
    }
2314
}
2315
 
2316
 
2317
static void
2318
round_for_format (const struct real_format *fmt, REAL_VALUE_TYPE *r)
2319
{
2320
  int p2, np2, i, w;
2321
  unsigned long sticky;
2322
  bool guard, lsb;
2323
  int emin2m1, emax2;
2324
 
2325
  if (r->decimal)
2326
    {
2327
      if (fmt->b == 10)
2328
        {
2329
          decimal_round_for_format (fmt, r);
2330
          return;
2331
        }
2332
      /* FIXME. We can come here via fp_easy_constant
2333
         (e.g. -O0 on '_Decimal32 x = 1.0 + 2.0dd'), but have not
2334
         investigated whether this convert needs to be here, or
2335
         something else is missing. */
2336
      decimal_real_convert (r, DFmode, r);
2337
    }
2338
 
2339
  p2 = fmt->p * fmt->log2_b;
2340
  emin2m1 = (fmt->emin - 1) * fmt->log2_b;
2341
  emax2 = fmt->emax * fmt->log2_b;
2342
 
2343
  np2 = SIGNIFICAND_BITS - p2;
2344
  switch (r->cl)
2345
    {
2346
    underflow:
2347
      get_zero (r, r->sign);
2348
    case rvc_zero:
2349
      if (!fmt->has_signed_zero)
2350
        r->sign = 0;
2351
      return;
2352
 
2353
    overflow:
2354
      get_inf (r, r->sign);
2355
    case rvc_inf:
2356
      return;
2357
 
2358
    case rvc_nan:
2359
      clear_significand_below (r, np2);
2360
      return;
2361
 
2362
    case rvc_normal:
2363
      break;
2364
 
2365
    default:
2366
      gcc_unreachable ();
2367
    }
2368
 
2369
  /* If we're not base2, normalize the exponent to a multiple of
2370
     the true base.  */
2371
  if (fmt->log2_b != 1)
2372
    {
2373
      int shift;
2374
 
2375
      gcc_assert (fmt->b != 10);
2376
      shift = REAL_EXP (r) & (fmt->log2_b - 1);
2377
      if (shift)
2378
        {
2379
          shift = fmt->log2_b - shift;
2380
          r->sig[0] |= sticky_rshift_significand (r, r, shift);
2381
          SET_REAL_EXP (r, REAL_EXP (r) + shift);
2382
        }
2383
    }
2384
 
2385
  /* Check the range of the exponent.  If we're out of range,
2386
     either underflow or overflow.  */
2387
  if (REAL_EXP (r) > emax2)
2388
    goto overflow;
2389
  else if (REAL_EXP (r) <= emin2m1)
2390
    {
2391
      int diff;
2392
 
2393
      if (!fmt->has_denorm)
2394
        {
2395
          /* Don't underflow completely until we've had a chance to round.  */
2396
          if (REAL_EXP (r) < emin2m1)
2397
            goto underflow;
2398
        }
2399
      else
2400
        {
2401
          diff = emin2m1 - REAL_EXP (r) + 1;
2402
          if (diff > p2)
2403
            goto underflow;
2404
 
2405
          /* De-normalize the significand.  */
2406
          r->sig[0] |= sticky_rshift_significand (r, r, diff);
2407
          SET_REAL_EXP (r, REAL_EXP (r) + diff);
2408
        }
2409
    }
2410
 
2411
  /* There are P2 true significand bits, followed by one guard bit,
2412
     followed by one sticky bit, followed by stuff.  Fold nonzero
2413
     stuff into the sticky bit.  */
2414
 
2415
  sticky = 0;
2416
  for (i = 0, w = (np2 - 1) / HOST_BITS_PER_LONG; i < w; ++i)
2417
    sticky |= r->sig[i];
2418
  sticky |=
2419
    r->sig[w] & (((unsigned long)1 << ((np2 - 1) % HOST_BITS_PER_LONG)) - 1);
2420
 
2421
  guard = test_significand_bit (r, np2 - 1);
2422
  lsb = test_significand_bit (r, np2);
2423
 
2424
  /* Round to even.  */
2425
  if (guard && (sticky || lsb))
2426
    {
2427
      REAL_VALUE_TYPE u;
2428
      get_zero (&u, 0);
2429
      set_significand_bit (&u, np2);
2430
 
2431
      if (add_significands (r, r, &u))
2432
        {
2433
          /* Overflow.  Means the significand had been all ones, and
2434
             is now all zeros.  Need to increase the exponent, and
2435
             possibly re-normalize it.  */
2436
          SET_REAL_EXP (r, REAL_EXP (r) + 1);
2437
          if (REAL_EXP (r) > emax2)
2438
            goto overflow;
2439
          r->sig[SIGSZ-1] = SIG_MSB;
2440
 
2441
          if (fmt->log2_b != 1)
2442
            {
2443
              int shift = REAL_EXP (r) & (fmt->log2_b - 1);
2444
              if (shift)
2445
                {
2446
                  shift = fmt->log2_b - shift;
2447
                  rshift_significand (r, r, shift);
2448
                  SET_REAL_EXP (r, REAL_EXP (r) + shift);
2449
                  if (REAL_EXP (r) > emax2)
2450
                    goto overflow;
2451
                }
2452
            }
2453
        }
2454
    }
2455
 
2456
  /* Catch underflow that we deferred until after rounding.  */
2457
  if (REAL_EXP (r) <= emin2m1)
2458
    goto underflow;
2459
 
2460
  /* Clear out trailing garbage.  */
2461
  clear_significand_below (r, np2);
2462
}
2463
 
2464
/* Extend or truncate to a new mode.  */
2465
 
2466
void
2467
real_convert (REAL_VALUE_TYPE *r, enum machine_mode mode,
2468
              const REAL_VALUE_TYPE *a)
2469
{
2470
  const struct real_format *fmt;
2471
 
2472
  fmt = REAL_MODE_FORMAT (mode);
2473
  gcc_assert (fmt);
2474
 
2475
  *r = *a;
2476
 
2477
  if (a->decimal || fmt->b == 10)
2478
    decimal_real_convert (r, mode, a);
2479
 
2480
  round_for_format (fmt, r);
2481
 
2482
  /* round_for_format de-normalizes denormals.  Undo just that part.  */
2483
  if (r->cl == rvc_normal)
2484
    normalize (r);
2485
}
2486
 
2487
/* Legacy.  Likewise, except return the struct directly.  */
2488
 
2489
REAL_VALUE_TYPE
2490
real_value_truncate (enum machine_mode mode, REAL_VALUE_TYPE a)
2491
{
2492
  REAL_VALUE_TYPE r;
2493
  real_convert (&r, mode, &a);
2494
  return r;
2495
}
2496
 
2497
/* Return true if truncating to MODE is exact.  */
2498
 
2499
bool
2500
exact_real_truncate (enum machine_mode mode, const REAL_VALUE_TYPE *a)
2501
{
2502
  const struct real_format *fmt;
2503
  REAL_VALUE_TYPE t;
2504
  int emin2m1;
2505
 
2506
  fmt = REAL_MODE_FORMAT (mode);
2507
  gcc_assert (fmt);
2508
 
2509
  /* Don't allow conversion to denormals.  */
2510
  emin2m1 = (fmt->emin - 1) * fmt->log2_b;
2511
  if (REAL_EXP (a) <= emin2m1)
2512
    return false;
2513
 
2514
  /* After conversion to the new mode, the value must be identical.  */
2515
  real_convert (&t, mode, a);
2516
  return real_identical (&t, a);
2517
}
2518
 
2519
/* Write R to the given target format.  Place the words of the result
2520
   in target word order in BUF.  There are always 32 bits in each
2521
   long, no matter the size of the host long.
2522
 
2523
   Legacy: return word 0 for implementing REAL_VALUE_TO_TARGET_SINGLE.  */
2524
 
2525
long
2526
real_to_target_fmt (long *buf, const REAL_VALUE_TYPE *r_orig,
2527
                    const struct real_format *fmt)
2528
{
2529
  REAL_VALUE_TYPE r;
2530
  long buf1;
2531
 
2532
  r = *r_orig;
2533
  round_for_format (fmt, &r);
2534
 
2535
  if (!buf)
2536
    buf = &buf1;
2537
  (*fmt->encode) (fmt, buf, &r);
2538
 
2539
  return *buf;
2540
}
2541
 
2542
/* Similar, but look up the format from MODE.  */
2543
 
2544
long
2545
real_to_target (long *buf, const REAL_VALUE_TYPE *r, enum machine_mode mode)
2546
{
2547
  const struct real_format *fmt;
2548
 
2549
  fmt = REAL_MODE_FORMAT (mode);
2550
  gcc_assert (fmt);
2551
 
2552
  return real_to_target_fmt (buf, r, fmt);
2553
}
2554
 
2555
/* Read R from the given target format.  Read the words of the result
2556
   in target word order in BUF.  There are always 32 bits in each
2557
   long, no matter the size of the host long.  */
2558
 
2559
void
2560
real_from_target_fmt (REAL_VALUE_TYPE *r, const long *buf,
2561
                      const struct real_format *fmt)
2562
{
2563
  (*fmt->decode) (fmt, r, buf);
2564
}
2565
 
2566
/* Similar, but look up the format from MODE.  */
2567
 
2568
void
2569
real_from_target (REAL_VALUE_TYPE *r, const long *buf, enum machine_mode mode)
2570
{
2571
  const struct real_format *fmt;
2572
 
2573
  fmt = REAL_MODE_FORMAT (mode);
2574
  gcc_assert (fmt);
2575
 
2576
  (*fmt->decode) (fmt, r, buf);
2577
}
2578
 
2579
/* Return the number of bits of the largest binary value that the
2580
   significand of MODE will hold.  */
2581
/* ??? Legacy.  Should get access to real_format directly.  */
2582
 
2583
int
2584
significand_size (enum machine_mode mode)
2585
{
2586
  const struct real_format *fmt;
2587
 
2588
  fmt = REAL_MODE_FORMAT (mode);
2589
  if (fmt == NULL)
2590
    return 0;
2591
 
2592
  if (fmt->b == 10)
2593
    {
2594
      /* Return the size in bits of the largest binary value that can be
2595
         held by the decimal coefficient for this mode.  This is one more
2596
         than the number of bits required to hold the largest coefficient
2597
         of this mode.  */
2598
      double log2_10 = 3.3219281;
2599
      return fmt->p * log2_10;
2600
    }
2601
  return fmt->p * fmt->log2_b;
2602
}
2603
 
2604
/* Return a hash value for the given real value.  */
2605
/* ??? The "unsigned int" return value is intended to be hashval_t,
2606
   but I didn't want to pull hashtab.h into real.h.  */
2607
 
2608
unsigned int
2609
real_hash (const REAL_VALUE_TYPE *r)
2610
{
2611
  unsigned int h;
2612
  size_t i;
2613
 
2614
  h = r->cl | (r->sign << 2);
2615
  switch (r->cl)
2616
    {
2617
    case rvc_zero:
2618
    case rvc_inf:
2619
      return h;
2620
 
2621
    case rvc_normal:
2622
      h |= REAL_EXP (r) << 3;
2623
      break;
2624
 
2625
    case rvc_nan:
2626
      if (r->signalling)
2627
        h ^= (unsigned int)-1;
2628
      if (r->canonical)
2629
        return h;
2630
      break;
2631
 
2632
    default:
2633
      gcc_unreachable ();
2634
    }
2635
 
2636
  if (sizeof(unsigned long) > sizeof(unsigned int))
2637
    for (i = 0; i < SIGSZ; ++i)
2638
      {
2639
        unsigned long s = r->sig[i];
2640
        h ^= s ^ (s >> (HOST_BITS_PER_LONG / 2));
2641
      }
2642
  else
2643
    for (i = 0; i < SIGSZ; ++i)
2644
      h ^= r->sig[i];
2645
 
2646
  return h;
2647
}
2648
 
2649
/* IEEE single-precision format.  */
2650
 
2651
static void encode_ieee_single (const struct real_format *fmt,
2652
                                long *, const REAL_VALUE_TYPE *);
2653
static void decode_ieee_single (const struct real_format *,
2654
                                REAL_VALUE_TYPE *, const long *);
2655
 
2656
static void
2657
encode_ieee_single (const struct real_format *fmt, long *buf,
2658
                    const REAL_VALUE_TYPE *r)
2659
{
2660
  unsigned long image, sig, exp;
2661
  unsigned long sign = r->sign;
2662
  bool denormal = (r->sig[SIGSZ-1] & SIG_MSB) == 0;
2663
 
2664
  image = sign << 31;
2665
  sig = (r->sig[SIGSZ-1] >> (HOST_BITS_PER_LONG - 24)) & 0x7fffff;
2666
 
2667
  switch (r->cl)
2668
    {
2669
    case rvc_zero:
2670
      break;
2671
 
2672
    case rvc_inf:
2673
      if (fmt->has_inf)
2674
        image |= 255 << 23;
2675
      else
2676
        image |= 0x7fffffff;
2677
      break;
2678
 
2679
    case rvc_nan:
2680
      if (fmt->has_nans)
2681
        {
2682
          if (r->canonical)
2683
            sig = 0;
2684
          if (r->signalling == fmt->qnan_msb_set)
2685
            sig &= ~(1 << 22);
2686
          else
2687
            sig |= 1 << 22;
2688
          /* We overload qnan_msb_set here: it's only clear for
2689
             mips_ieee_single, which wants all mantissa bits but the
2690
             quiet/signalling one set in canonical NaNs (at least
2691
             Quiet ones).  */
2692
          if (r->canonical && !fmt->qnan_msb_set)
2693
            sig |= (1 << 22) - 1;
2694
          else if (sig == 0)
2695
            sig = 1 << 21;
2696
 
2697
          image |= 255 << 23;
2698
          image |= sig;
2699
        }
2700
      else
2701
        image |= 0x7fffffff;
2702
      break;
2703
 
2704
    case rvc_normal:
2705
      /* Recall that IEEE numbers are interpreted as 1.F x 2**exp,
2706
         whereas the intermediate representation is 0.F x 2**exp.
2707
         Which means we're off by one.  */
2708
      if (denormal)
2709
        exp = 0;
2710
      else
2711
      exp = REAL_EXP (r) + 127 - 1;
2712
      image |= exp << 23;
2713
      image |= sig;
2714
      break;
2715
 
2716
    default:
2717
      gcc_unreachable ();
2718
    }
2719
 
2720
  buf[0] = image;
2721
}
2722
 
2723
static void
2724
decode_ieee_single (const struct real_format *fmt, REAL_VALUE_TYPE *r,
2725
                    const long *buf)
2726
{
2727
  unsigned long image = buf[0] & 0xffffffff;
2728
  bool sign = (image >> 31) & 1;
2729
  int exp = (image >> 23) & 0xff;
2730
 
2731
  memset (r, 0, sizeof (*r));
2732
  image <<= HOST_BITS_PER_LONG - 24;
2733
  image &= ~SIG_MSB;
2734
 
2735
  if (exp == 0)
2736
    {
2737
      if (image && fmt->has_denorm)
2738
        {
2739
          r->cl = rvc_normal;
2740
          r->sign = sign;
2741
          SET_REAL_EXP (r, -126);
2742
          r->sig[SIGSZ-1] = image << 1;
2743
          normalize (r);
2744
        }
2745
      else if (fmt->has_signed_zero)
2746
        r->sign = sign;
2747
    }
2748
  else if (exp == 255 && (fmt->has_nans || fmt->has_inf))
2749
    {
2750
      if (image)
2751
        {
2752
          r->cl = rvc_nan;
2753
          r->sign = sign;
2754
          r->signalling = (((image >> (HOST_BITS_PER_LONG - 2)) & 1)
2755
                           ^ fmt->qnan_msb_set);
2756
          r->sig[SIGSZ-1] = image;
2757
        }
2758
      else
2759
        {
2760
          r->cl = rvc_inf;
2761
          r->sign = sign;
2762
        }
2763
    }
2764
  else
2765
    {
2766
      r->cl = rvc_normal;
2767
      r->sign = sign;
2768
      SET_REAL_EXP (r, exp - 127 + 1);
2769
      r->sig[SIGSZ-1] = image | SIG_MSB;
2770
    }
2771
}
2772
 
2773
const struct real_format ieee_single_format =
2774
  {
2775
    encode_ieee_single,
2776
    decode_ieee_single,
2777
    2,
2778
    1,
2779
    24,
2780
    24,
2781
    -125,
2782
    128,
2783
    31,
2784
    31,
2785
    true,
2786
    true,
2787
    true,
2788
    true,
2789
    true
2790
  };
2791
 
2792
const struct real_format mips_single_format =
2793
  {
2794
    encode_ieee_single,
2795
    decode_ieee_single,
2796
    2,
2797
    1,
2798
    24,
2799
    24,
2800
    -125,
2801
    128,
2802
    31,
2803
    31,
2804
    true,
2805
    true,
2806
    true,
2807
    true,
2808
    false
2809
  };
2810
 
2811
 
2812
/* IEEE double-precision format.  */
2813
 
2814
static void encode_ieee_double (const struct real_format *fmt,
2815
                                long *, const REAL_VALUE_TYPE *);
2816
static void decode_ieee_double (const struct real_format *,
2817
                                REAL_VALUE_TYPE *, const long *);
2818
 
2819
static void
2820
encode_ieee_double (const struct real_format *fmt, long *buf,
2821
                    const REAL_VALUE_TYPE *r)
2822
{
2823
  unsigned long image_lo, image_hi, sig_lo, sig_hi, exp;
2824
  bool denormal = (r->sig[SIGSZ-1] & SIG_MSB) == 0;
2825
 
2826
  image_hi = r->sign << 31;
2827
  image_lo = 0;
2828
 
2829
  if (HOST_BITS_PER_LONG == 64)
2830
    {
2831
      sig_hi = r->sig[SIGSZ-1];
2832
      sig_lo = (sig_hi >> (64 - 53)) & 0xffffffff;
2833
      sig_hi = (sig_hi >> (64 - 53 + 1) >> 31) & 0xfffff;
2834
    }
2835
  else
2836
    {
2837
      sig_hi = r->sig[SIGSZ-1];
2838
      sig_lo = r->sig[SIGSZ-2];
2839
      sig_lo = (sig_hi << 21) | (sig_lo >> 11);
2840
      sig_hi = (sig_hi >> 11) & 0xfffff;
2841
    }
2842
 
2843
  switch (r->cl)
2844
    {
2845
    case rvc_zero:
2846
      break;
2847
 
2848
    case rvc_inf:
2849
      if (fmt->has_inf)
2850
        image_hi |= 2047 << 20;
2851
      else
2852
        {
2853
          image_hi |= 0x7fffffff;
2854
          image_lo = 0xffffffff;
2855
        }
2856
      break;
2857
 
2858
    case rvc_nan:
2859
      if (fmt->has_nans)
2860
        {
2861
          if (r->canonical)
2862
            sig_hi = sig_lo = 0;
2863
          if (r->signalling == fmt->qnan_msb_set)
2864
            sig_hi &= ~(1 << 19);
2865
          else
2866
            sig_hi |= 1 << 19;
2867
          /* We overload qnan_msb_set here: it's only clear for
2868
             mips_ieee_single, which wants all mantissa bits but the
2869
             quiet/signalling one set in canonical NaNs (at least
2870
             Quiet ones).  */
2871
          if (r->canonical && !fmt->qnan_msb_set)
2872
            {
2873
              sig_hi |= (1 << 19) - 1;
2874
              sig_lo = 0xffffffff;
2875
            }
2876
          else if (sig_hi == 0 && sig_lo == 0)
2877
            sig_hi = 1 << 18;
2878
 
2879
          image_hi |= 2047 << 20;
2880
          image_hi |= sig_hi;
2881
          image_lo = sig_lo;
2882
        }
2883
      else
2884
        {
2885
          image_hi |= 0x7fffffff;
2886
          image_lo = 0xffffffff;
2887
        }
2888
      break;
2889
 
2890
    case rvc_normal:
2891
      /* Recall that IEEE numbers are interpreted as 1.F x 2**exp,
2892
         whereas the intermediate representation is 0.F x 2**exp.
2893
         Which means we're off by one.  */
2894
      if (denormal)
2895
        exp = 0;
2896
      else
2897
        exp = REAL_EXP (r) + 1023 - 1;
2898
      image_hi |= exp << 20;
2899
      image_hi |= sig_hi;
2900
      image_lo = sig_lo;
2901
      break;
2902
 
2903
    default:
2904
      gcc_unreachable ();
2905
    }
2906
 
2907
  if (FLOAT_WORDS_BIG_ENDIAN)
2908
    buf[0] = image_hi, buf[1] = image_lo;
2909
  else
2910
    buf[0] = image_lo, buf[1] = image_hi;
2911
}
2912
 
2913
static void
2914
decode_ieee_double (const struct real_format *fmt, REAL_VALUE_TYPE *r,
2915
                    const long *buf)
2916
{
2917
  unsigned long image_hi, image_lo;
2918
  bool sign;
2919
  int exp;
2920
 
2921
  if (FLOAT_WORDS_BIG_ENDIAN)
2922
    image_hi = buf[0], image_lo = buf[1];
2923
  else
2924
    image_lo = buf[0], image_hi = buf[1];
2925
  image_lo &= 0xffffffff;
2926
  image_hi &= 0xffffffff;
2927
 
2928
  sign = (image_hi >> 31) & 1;
2929
  exp = (image_hi >> 20) & 0x7ff;
2930
 
2931
  memset (r, 0, sizeof (*r));
2932
 
2933
  image_hi <<= 32 - 21;
2934
  image_hi |= image_lo >> 21;
2935
  image_hi &= 0x7fffffff;
2936
  image_lo <<= 32 - 21;
2937
 
2938
  if (exp == 0)
2939
    {
2940
      if ((image_hi || image_lo) && fmt->has_denorm)
2941
        {
2942
          r->cl = rvc_normal;
2943
          r->sign = sign;
2944
          SET_REAL_EXP (r, -1022);
2945
          if (HOST_BITS_PER_LONG == 32)
2946
            {
2947
              image_hi = (image_hi << 1) | (image_lo >> 31);
2948
              image_lo <<= 1;
2949
              r->sig[SIGSZ-1] = image_hi;
2950
              r->sig[SIGSZ-2] = image_lo;
2951
            }
2952
          else
2953
            {
2954
              image_hi = (image_hi << 31 << 2) | (image_lo << 1);
2955
              r->sig[SIGSZ-1] = image_hi;
2956
            }
2957
          normalize (r);
2958
        }
2959
      else if (fmt->has_signed_zero)
2960
        r->sign = sign;
2961
    }
2962
  else if (exp == 2047 && (fmt->has_nans || fmt->has_inf))
2963
    {
2964
      if (image_hi || image_lo)
2965
        {
2966
          r->cl = rvc_nan;
2967
          r->sign = sign;
2968
          r->signalling = ((image_hi >> 30) & 1) ^ fmt->qnan_msb_set;
2969
          if (HOST_BITS_PER_LONG == 32)
2970
            {
2971
              r->sig[SIGSZ-1] = image_hi;
2972
              r->sig[SIGSZ-2] = image_lo;
2973
            }
2974
          else
2975
            r->sig[SIGSZ-1] = (image_hi << 31 << 1) | image_lo;
2976
        }
2977
      else
2978
        {
2979
          r->cl = rvc_inf;
2980
          r->sign = sign;
2981
        }
2982
    }
2983
  else
2984
    {
2985
      r->cl = rvc_normal;
2986
      r->sign = sign;
2987
      SET_REAL_EXP (r, exp - 1023 + 1);
2988
      if (HOST_BITS_PER_LONG == 32)
2989
        {
2990
          r->sig[SIGSZ-1] = image_hi | SIG_MSB;
2991
          r->sig[SIGSZ-2] = image_lo;
2992
        }
2993
      else
2994
        r->sig[SIGSZ-1] = (image_hi << 31 << 1) | image_lo | SIG_MSB;
2995
    }
2996
}
2997
 
2998
const struct real_format ieee_double_format =
2999
  {
3000
    encode_ieee_double,
3001
    decode_ieee_double,
3002
    2,
3003
    1,
3004
    53,
3005
    53,
3006
    -1021,
3007
    1024,
3008
    63,
3009
    63,
3010
    true,
3011
    true,
3012
    true,
3013
    true,
3014
    true
3015
  };
3016
 
3017
const struct real_format mips_double_format =
3018
  {
3019
    encode_ieee_double,
3020
    decode_ieee_double,
3021
    2,
3022
    1,
3023
    53,
3024
    53,
3025
    -1021,
3026
    1024,
3027
    63,
3028
    63,
3029
    true,
3030
    true,
3031
    true,
3032
    true,
3033
    false
3034
  };
3035
 
3036
 
3037
/* IEEE extended real format.  This comes in three flavors: Intel's as
3038
   a 12 byte image, Intel's as a 16 byte image, and Motorola's.  Intel
3039
   12- and 16-byte images may be big- or little endian; Motorola's is
3040
   always big endian.  */
3041
 
3042
/* Helper subroutine which converts from the internal format to the
3043
   12-byte little-endian Intel format.  Functions below adjust this
3044
   for the other possible formats.  */
3045
static void
3046
encode_ieee_extended (const struct real_format *fmt, long *buf,
3047
                      const REAL_VALUE_TYPE *r)
3048
{
3049
  unsigned long image_hi, sig_hi, sig_lo;
3050
  bool denormal = (r->sig[SIGSZ-1] & SIG_MSB) == 0;
3051
 
3052
  image_hi = r->sign << 15;
3053
  sig_hi = sig_lo = 0;
3054
 
3055
  switch (r->cl)
3056
    {
3057
    case rvc_zero:
3058
      break;
3059
 
3060
    case rvc_inf:
3061
      if (fmt->has_inf)
3062
        {
3063
          image_hi |= 32767;
3064
 
3065
          /* Intel requires the explicit integer bit to be set, otherwise
3066
             it considers the value a "pseudo-infinity".  Motorola docs
3067
             say it doesn't care.  */
3068
          sig_hi = 0x80000000;
3069
        }
3070
      else
3071
        {
3072
          image_hi |= 32767;
3073
          sig_lo = sig_hi = 0xffffffff;
3074
        }
3075
      break;
3076
 
3077
    case rvc_nan:
3078
      if (fmt->has_nans)
3079
        {
3080
          image_hi |= 32767;
3081
          if (HOST_BITS_PER_LONG == 32)
3082
            {
3083
              sig_hi = r->sig[SIGSZ-1];
3084
              sig_lo = r->sig[SIGSZ-2];
3085
            }
3086
          else
3087
            {
3088
              sig_lo = r->sig[SIGSZ-1];
3089
              sig_hi = sig_lo >> 31 >> 1;
3090
              sig_lo &= 0xffffffff;
3091
            }
3092
          if (r->signalling == fmt->qnan_msb_set)
3093
            sig_hi &= ~(1 << 30);
3094
          else
3095
            sig_hi |= 1 << 30;
3096
          if ((sig_hi & 0x7fffffff) == 0 && sig_lo == 0)
3097
            sig_hi = 1 << 29;
3098
 
3099
          /* Intel requires the explicit integer bit to be set, otherwise
3100
             it considers the value a "pseudo-nan".  Motorola docs say it
3101
             doesn't care.  */
3102
          sig_hi |= 0x80000000;
3103
        }
3104
      else
3105
        {
3106
          image_hi |= 32767;
3107
          sig_lo = sig_hi = 0xffffffff;
3108
        }
3109
      break;
3110
 
3111
    case rvc_normal:
3112
      {
3113
        int exp = REAL_EXP (r);
3114
 
3115
        /* Recall that IEEE numbers are interpreted as 1.F x 2**exp,
3116
           whereas the intermediate representation is 0.F x 2**exp.
3117
           Which means we're off by one.
3118
 
3119
           Except for Motorola, which consider exp=0 and explicit
3120
           integer bit set to continue to be normalized.  In theory
3121
           this discrepancy has been taken care of by the difference
3122
           in fmt->emin in round_for_format.  */
3123
 
3124
        if (denormal)
3125
          exp = 0;
3126
        else
3127
          {
3128
            exp += 16383 - 1;
3129
            gcc_assert (exp >= 0);
3130
          }
3131
        image_hi |= exp;
3132
 
3133
        if (HOST_BITS_PER_LONG == 32)
3134
          {
3135
            sig_hi = r->sig[SIGSZ-1];
3136
            sig_lo = r->sig[SIGSZ-2];
3137
          }
3138
        else
3139
          {
3140
            sig_lo = r->sig[SIGSZ-1];
3141
            sig_hi = sig_lo >> 31 >> 1;
3142
            sig_lo &= 0xffffffff;
3143
          }
3144
      }
3145
      break;
3146
 
3147
    default:
3148
      gcc_unreachable ();
3149
    }
3150
 
3151
  buf[0] = sig_lo, buf[1] = sig_hi, buf[2] = image_hi;
3152
}
3153
 
3154
/* Convert from the internal format to the 12-byte Motorola format
3155
   for an IEEE extended real.  */
3156
static void
3157
encode_ieee_extended_motorola (const struct real_format *fmt, long *buf,
3158
                               const REAL_VALUE_TYPE *r)
3159
{
3160
  long intermed[3];
3161
  encode_ieee_extended (fmt, intermed, r);
3162
 
3163
  /* Motorola chips are assumed always to be big-endian.  Also, the
3164
     padding in a Motorola extended real goes between the exponent and
3165
     the mantissa.  At this point the mantissa is entirely within
3166
     elements 0 and 1 of intermed, and the exponent entirely within
3167
     element 2, so all we have to do is swap the order around, and
3168
     shift element 2 left 16 bits.  */
3169
  buf[0] = intermed[2] << 16;
3170
  buf[1] = intermed[1];
3171
  buf[2] = intermed[0];
3172
}
3173
 
3174
/* Convert from the internal format to the 12-byte Intel format for
3175
   an IEEE extended real.  */
3176
static void
3177
encode_ieee_extended_intel_96 (const struct real_format *fmt, long *buf,
3178
                               const REAL_VALUE_TYPE *r)
3179
{
3180
  if (FLOAT_WORDS_BIG_ENDIAN)
3181
    {
3182
      /* All the padding in an Intel-format extended real goes at the high
3183
         end, which in this case is after the mantissa, not the exponent.
3184
         Therefore we must shift everything down 16 bits.  */
3185
      long intermed[3];
3186
      encode_ieee_extended (fmt, intermed, r);
3187
      buf[0] = ((intermed[2] << 16) | ((unsigned long)(intermed[1] & 0xFFFF0000) >> 16));
3188
      buf[1] = ((intermed[1] << 16) | ((unsigned long)(intermed[0] & 0xFFFF0000) >> 16));
3189
      buf[2] =  (intermed[0] << 16);
3190
    }
3191
  else
3192
    /* encode_ieee_extended produces what we want directly.  */
3193
    encode_ieee_extended (fmt, buf, r);
3194
}
3195
 
3196
/* Convert from the internal format to the 16-byte Intel format for
3197
   an IEEE extended real.  */
3198
static void
3199
encode_ieee_extended_intel_128 (const struct real_format *fmt, long *buf,
3200
                                const REAL_VALUE_TYPE *r)
3201
{
3202
  /* All the padding in an Intel-format extended real goes at the high end.  */
3203
  encode_ieee_extended_intel_96 (fmt, buf, r);
3204
  buf[3] = 0;
3205
}
3206
 
3207
/* As above, we have a helper function which converts from 12-byte
3208
   little-endian Intel format to internal format.  Functions below
3209
   adjust for the other possible formats.  */
3210
static void
3211
decode_ieee_extended (const struct real_format *fmt, REAL_VALUE_TYPE *r,
3212
                      const long *buf)
3213
{
3214
  unsigned long image_hi, sig_hi, sig_lo;
3215
  bool sign;
3216
  int exp;
3217
 
3218
  sig_lo = buf[0], sig_hi = buf[1], image_hi = buf[2];
3219
  sig_lo &= 0xffffffff;
3220
  sig_hi &= 0xffffffff;
3221
  image_hi &= 0xffffffff;
3222
 
3223
  sign = (image_hi >> 15) & 1;
3224
  exp = image_hi & 0x7fff;
3225
 
3226
  memset (r, 0, sizeof (*r));
3227
 
3228
  if (exp == 0)
3229
    {
3230
      if ((sig_hi || sig_lo) && fmt->has_denorm)
3231
        {
3232
          r->cl = rvc_normal;
3233
          r->sign = sign;
3234
 
3235
          /* When the IEEE format contains a hidden bit, we know that
3236
             it's zero at this point, and so shift up the significand
3237
             and decrease the exponent to match.  In this case, Motorola
3238
             defines the explicit integer bit to be valid, so we don't
3239
             know whether the msb is set or not.  */
3240
          SET_REAL_EXP (r, fmt->emin);
3241
          if (HOST_BITS_PER_LONG == 32)
3242
            {
3243
              r->sig[SIGSZ-1] = sig_hi;
3244
              r->sig[SIGSZ-2] = sig_lo;
3245
            }
3246
          else
3247
            r->sig[SIGSZ-1] = (sig_hi << 31 << 1) | sig_lo;
3248
 
3249
          normalize (r);
3250
        }
3251
      else if (fmt->has_signed_zero)
3252
        r->sign = sign;
3253
    }
3254
  else if (exp == 32767 && (fmt->has_nans || fmt->has_inf))
3255
    {
3256
      /* See above re "pseudo-infinities" and "pseudo-nans".
3257
         Short summary is that the MSB will likely always be
3258
         set, and that we don't care about it.  */
3259
      sig_hi &= 0x7fffffff;
3260
 
3261
      if (sig_hi || sig_lo)
3262
        {
3263
          r->cl = rvc_nan;
3264
          r->sign = sign;
3265
          r->signalling = ((sig_hi >> 30) & 1) ^ fmt->qnan_msb_set;
3266
          if (HOST_BITS_PER_LONG == 32)
3267
            {
3268
              r->sig[SIGSZ-1] = sig_hi;
3269
              r->sig[SIGSZ-2] = sig_lo;
3270
            }
3271
          else
3272
            r->sig[SIGSZ-1] = (sig_hi << 31 << 1) | sig_lo;
3273
        }
3274
      else
3275
        {
3276
          r->cl = rvc_inf;
3277
          r->sign = sign;
3278
        }
3279
    }
3280
  else
3281
    {
3282
      r->cl = rvc_normal;
3283
      r->sign = sign;
3284
      SET_REAL_EXP (r, exp - 16383 + 1);
3285
      if (HOST_BITS_PER_LONG == 32)
3286
        {
3287
          r->sig[SIGSZ-1] = sig_hi;
3288
          r->sig[SIGSZ-2] = sig_lo;
3289
        }
3290
      else
3291
        r->sig[SIGSZ-1] = (sig_hi << 31 << 1) | sig_lo;
3292
    }
3293
}
3294
 
3295
/* Convert from the internal format to the 12-byte Motorola format
3296
   for an IEEE extended real.  */
3297
static void
3298
decode_ieee_extended_motorola (const struct real_format *fmt, REAL_VALUE_TYPE *r,
3299
                               const long *buf)
3300
{
3301
  long intermed[3];
3302
 
3303
  /* Motorola chips are assumed always to be big-endian.  Also, the
3304
     padding in a Motorola extended real goes between the exponent and
3305
     the mantissa; remove it.  */
3306
  intermed[0] = buf[2];
3307
  intermed[1] = buf[1];
3308
  intermed[2] = (unsigned long)buf[0] >> 16;
3309
 
3310
  decode_ieee_extended (fmt, r, intermed);
3311
}
3312
 
3313
/* Convert from the internal format to the 12-byte Intel format for
3314
   an IEEE extended real.  */
3315
static void
3316
decode_ieee_extended_intel_96 (const struct real_format *fmt, REAL_VALUE_TYPE *r,
3317
                               const long *buf)
3318
{
3319
  if (FLOAT_WORDS_BIG_ENDIAN)
3320
    {
3321
      /* All the padding in an Intel-format extended real goes at the high
3322
         end, which in this case is after the mantissa, not the exponent.
3323
         Therefore we must shift everything up 16 bits.  */
3324
      long intermed[3];
3325
 
3326
      intermed[0] = (((unsigned long)buf[2] >> 16) | (buf[1] << 16));
3327
      intermed[1] = (((unsigned long)buf[1] >> 16) | (buf[0] << 16));
3328
      intermed[2] =  ((unsigned long)buf[0] >> 16);
3329
 
3330
      decode_ieee_extended (fmt, r, intermed);
3331
    }
3332
  else
3333
    /* decode_ieee_extended produces what we want directly.  */
3334
    decode_ieee_extended (fmt, r, buf);
3335
}
3336
 
3337
/* Convert from the internal format to the 16-byte Intel format for
3338
   an IEEE extended real.  */
3339
static void
3340
decode_ieee_extended_intel_128 (const struct real_format *fmt, REAL_VALUE_TYPE *r,
3341
                                const long *buf)
3342
{
3343
  /* All the padding in an Intel-format extended real goes at the high end.  */
3344
  decode_ieee_extended_intel_96 (fmt, r, buf);
3345
}
3346
 
3347
const struct real_format ieee_extended_motorola_format =
3348
  {
3349
    encode_ieee_extended_motorola,
3350
    decode_ieee_extended_motorola,
3351
    2,
3352
    1,
3353
    64,
3354
    64,
3355
    -16382,
3356
    16384,
3357
    95,
3358
    95,
3359
    true,
3360
    true,
3361
    true,
3362
    true,
3363
    true
3364
  };
3365
 
3366
const struct real_format ieee_extended_intel_96_format =
3367
  {
3368
    encode_ieee_extended_intel_96,
3369
    decode_ieee_extended_intel_96,
3370
    2,
3371
    1,
3372
    64,
3373
    64,
3374
    -16381,
3375
    16384,
3376
    79,
3377
    79,
3378
    true,
3379
    true,
3380
    true,
3381
    true,
3382
    true
3383
  };
3384
 
3385
const struct real_format ieee_extended_intel_128_format =
3386
  {
3387
    encode_ieee_extended_intel_128,
3388
    decode_ieee_extended_intel_128,
3389
    2,
3390
    1,
3391
    64,
3392
    64,
3393
    -16381,
3394
    16384,
3395
    79,
3396
    79,
3397
    true,
3398
    true,
3399
    true,
3400
    true,
3401
    true
3402
  };
3403
 
3404
/* The following caters to i386 systems that set the rounding precision
3405
   to 53 bits instead of 64, e.g. FreeBSD.  */
3406
const struct real_format ieee_extended_intel_96_round_53_format =
3407
  {
3408
    encode_ieee_extended_intel_96,
3409
    decode_ieee_extended_intel_96,
3410
    2,
3411
    1,
3412
    53,
3413
    53,
3414
    -16381,
3415
    16384,
3416
    79,
3417
    79,
3418
    true,
3419
    true,
3420
    true,
3421
    true,
3422
    true
3423
  };
3424
 
3425
/* IBM 128-bit extended precision format: a pair of IEEE double precision
3426
   numbers whose sum is equal to the extended precision value.  The number
3427
   with greater magnitude is first.  This format has the same magnitude
3428
   range as an IEEE double precision value, but effectively 106 bits of
3429
   significand precision.  Infinity and NaN are represented by their IEEE
3430
   double precision value stored in the first number, the second number is
3431
   +0.0 or -0.0 for Infinity and don't-care for NaN.  */
3432
 
3433
static void encode_ibm_extended (const struct real_format *fmt,
3434
                                 long *, const REAL_VALUE_TYPE *);
3435
static void decode_ibm_extended (const struct real_format *,
3436
                                 REAL_VALUE_TYPE *, const long *);
3437
 
3438
static void
3439
encode_ibm_extended (const struct real_format *fmt, long *buf,
3440
                     const REAL_VALUE_TYPE *r)
3441
{
3442
  REAL_VALUE_TYPE u, normr, v;
3443
  const struct real_format *base_fmt;
3444
 
3445
  base_fmt = fmt->qnan_msb_set ? &ieee_double_format : &mips_double_format;
3446
 
3447
  /* Renormlize R before doing any arithmetic on it.  */
3448
  normr = *r;
3449
  if (normr.cl == rvc_normal)
3450
    normalize (&normr);
3451
 
3452
  /* u = IEEE double precision portion of significand.  */
3453
  u = normr;
3454
  round_for_format (base_fmt, &u);
3455
  encode_ieee_double (base_fmt, &buf[0], &u);
3456
 
3457
  if (u.cl == rvc_normal)
3458
    {
3459
      do_add (&v, &normr, &u, 1);
3460
      /* Call round_for_format since we might need to denormalize.  */
3461
      round_for_format (base_fmt, &v);
3462
      encode_ieee_double (base_fmt, &buf[2], &v);
3463
    }
3464
  else
3465
    {
3466
      /* Inf, NaN, 0 are all representable as doubles, so the
3467
         least-significant part can be 0.0.  */
3468
      buf[2] = 0;
3469
      buf[3] = 0;
3470
    }
3471
}
3472
 
3473
static void
3474
decode_ibm_extended (const struct real_format *fmt ATTRIBUTE_UNUSED, REAL_VALUE_TYPE *r,
3475
                     const long *buf)
3476
{
3477
  REAL_VALUE_TYPE u, v;
3478
  const struct real_format *base_fmt;
3479
 
3480
  base_fmt = fmt->qnan_msb_set ? &ieee_double_format : &mips_double_format;
3481
  decode_ieee_double (base_fmt, &u, &buf[0]);
3482
 
3483
  if (u.cl != rvc_zero && u.cl != rvc_inf && u.cl != rvc_nan)
3484
    {
3485
      decode_ieee_double (base_fmt, &v, &buf[2]);
3486
      do_add (r, &u, &v, 0);
3487
    }
3488
  else
3489
    *r = u;
3490
}
3491
 
3492
const struct real_format ibm_extended_format =
3493
  {
3494
    encode_ibm_extended,
3495
    decode_ibm_extended,
3496
    2,
3497
    1,
3498
    53 + 53,
3499
    53,
3500
    -1021 + 53,
3501
    1024,
3502
    127,
3503
    -1,
3504
    true,
3505
    true,
3506
    true,
3507
    true,
3508
    true
3509
  };
3510
 
3511
const struct real_format mips_extended_format =
3512
  {
3513
    encode_ibm_extended,
3514
    decode_ibm_extended,
3515
    2,
3516
    1,
3517
    53 + 53,
3518
    53,
3519
    -1021 + 53,
3520
    1024,
3521
    127,
3522
    -1,
3523
    true,
3524
    true,
3525
    true,
3526
    true,
3527
    false
3528
  };
3529
 
3530
 
3531
/* IEEE quad precision format.  */
3532
 
3533
static void encode_ieee_quad (const struct real_format *fmt,
3534
                              long *, const REAL_VALUE_TYPE *);
3535
static void decode_ieee_quad (const struct real_format *,
3536
                              REAL_VALUE_TYPE *, const long *);
3537
 
3538
static void
3539
encode_ieee_quad (const struct real_format *fmt, long *buf,
3540
                  const REAL_VALUE_TYPE *r)
3541
{
3542
  unsigned long image3, image2, image1, image0, exp;
3543
  bool denormal = (r->sig[SIGSZ-1] & SIG_MSB) == 0;
3544
  REAL_VALUE_TYPE u;
3545
 
3546
  image3 = r->sign << 31;
3547
  image2 = 0;
3548
  image1 = 0;
3549
  image0 = 0;
3550
 
3551
  rshift_significand (&u, r, SIGNIFICAND_BITS - 113);
3552
 
3553
  switch (r->cl)
3554
    {
3555
    case rvc_zero:
3556
      break;
3557
 
3558
    case rvc_inf:
3559
      if (fmt->has_inf)
3560
        image3 |= 32767 << 16;
3561
      else
3562
        {
3563
          image3 |= 0x7fffffff;
3564
          image2 = 0xffffffff;
3565
          image1 = 0xffffffff;
3566
          image0 = 0xffffffff;
3567
        }
3568
      break;
3569
 
3570
    case rvc_nan:
3571
      if (fmt->has_nans)
3572
        {
3573
          image3 |= 32767 << 16;
3574
 
3575
          if (r->canonical)
3576
            {
3577
              /* Don't use bits from the significand.  The
3578
                 initialization above is right.  */
3579
            }
3580
          else if (HOST_BITS_PER_LONG == 32)
3581
            {
3582
              image0 = u.sig[0];
3583
              image1 = u.sig[1];
3584
              image2 = u.sig[2];
3585
              image3 |= u.sig[3] & 0xffff;
3586
            }
3587
          else
3588
            {
3589
              image0 = u.sig[0];
3590
              image1 = image0 >> 31 >> 1;
3591
              image2 = u.sig[1];
3592
              image3 |= (image2 >> 31 >> 1) & 0xffff;
3593
              image0 &= 0xffffffff;
3594
              image2 &= 0xffffffff;
3595
            }
3596
          if (r->signalling == fmt->qnan_msb_set)
3597
            image3 &= ~0x8000;
3598
          else
3599
            image3 |= 0x8000;
3600
          /* We overload qnan_msb_set here: it's only clear for
3601
             mips_ieee_single, which wants all mantissa bits but the
3602
             quiet/signalling one set in canonical NaNs (at least
3603
             Quiet ones).  */
3604
          if (r->canonical && !fmt->qnan_msb_set)
3605
            {
3606
              image3 |= 0x7fff;
3607
              image2 = image1 = image0 = 0xffffffff;
3608
            }
3609
          else if (((image3 & 0xffff) | image2 | image1 | image0) == 0)
3610
            image3 |= 0x4000;
3611
        }
3612
      else
3613
        {
3614
          image3 |= 0x7fffffff;
3615
          image2 = 0xffffffff;
3616
          image1 = 0xffffffff;
3617
          image0 = 0xffffffff;
3618
        }
3619
      break;
3620
 
3621
    case rvc_normal:
3622
      /* Recall that IEEE numbers are interpreted as 1.F x 2**exp,
3623
         whereas the intermediate representation is 0.F x 2**exp.
3624
         Which means we're off by one.  */
3625
      if (denormal)
3626
        exp = 0;
3627
      else
3628
        exp = REAL_EXP (r) + 16383 - 1;
3629
      image3 |= exp << 16;
3630
 
3631
      if (HOST_BITS_PER_LONG == 32)
3632
        {
3633
          image0 = u.sig[0];
3634
          image1 = u.sig[1];
3635
          image2 = u.sig[2];
3636
          image3 |= u.sig[3] & 0xffff;
3637
        }
3638
      else
3639
        {
3640
          image0 = u.sig[0];
3641
          image1 = image0 >> 31 >> 1;
3642
          image2 = u.sig[1];
3643
          image3 |= (image2 >> 31 >> 1) & 0xffff;
3644
          image0 &= 0xffffffff;
3645
          image2 &= 0xffffffff;
3646
        }
3647
      break;
3648
 
3649
    default:
3650
      gcc_unreachable ();
3651
    }
3652
 
3653
  if (FLOAT_WORDS_BIG_ENDIAN)
3654
    {
3655
      buf[0] = image3;
3656
      buf[1] = image2;
3657
      buf[2] = image1;
3658
      buf[3] = image0;
3659
    }
3660
  else
3661
    {
3662
      buf[0] = image0;
3663
      buf[1] = image1;
3664
      buf[2] = image2;
3665
      buf[3] = image3;
3666
    }
3667
}
3668
 
3669
static void
3670
decode_ieee_quad (const struct real_format *fmt, REAL_VALUE_TYPE *r,
3671
                  const long *buf)
3672
{
3673
  unsigned long image3, image2, image1, image0;
3674
  bool sign;
3675
  int exp;
3676
 
3677
  if (FLOAT_WORDS_BIG_ENDIAN)
3678
    {
3679
      image3 = buf[0];
3680
      image2 = buf[1];
3681
      image1 = buf[2];
3682
      image0 = buf[3];
3683
    }
3684
  else
3685
    {
3686
      image0 = buf[0];
3687
      image1 = buf[1];
3688
      image2 = buf[2];
3689
      image3 = buf[3];
3690
    }
3691
  image0 &= 0xffffffff;
3692
  image1 &= 0xffffffff;
3693
  image2 &= 0xffffffff;
3694
 
3695
  sign = (image3 >> 31) & 1;
3696
  exp = (image3 >> 16) & 0x7fff;
3697
  image3 &= 0xffff;
3698
 
3699
  memset (r, 0, sizeof (*r));
3700
 
3701
  if (exp == 0)
3702
    {
3703
      if ((image3 | image2 | image1 | image0) && fmt->has_denorm)
3704
        {
3705
          r->cl = rvc_normal;
3706
          r->sign = sign;
3707
 
3708
          SET_REAL_EXP (r, -16382 + (SIGNIFICAND_BITS - 112));
3709
          if (HOST_BITS_PER_LONG == 32)
3710
            {
3711
              r->sig[0] = image0;
3712
              r->sig[1] = image1;
3713
              r->sig[2] = image2;
3714
              r->sig[3] = image3;
3715
            }
3716
          else
3717
            {
3718
              r->sig[0] = (image1 << 31 << 1) | image0;
3719
              r->sig[1] = (image3 << 31 << 1) | image2;
3720
            }
3721
 
3722
          normalize (r);
3723
        }
3724
      else if (fmt->has_signed_zero)
3725
        r->sign = sign;
3726
    }
3727
  else if (exp == 32767 && (fmt->has_nans || fmt->has_inf))
3728
    {
3729
      if (image3 | image2 | image1 | image0)
3730
        {
3731
          r->cl = rvc_nan;
3732
          r->sign = sign;
3733
          r->signalling = ((image3 >> 15) & 1) ^ fmt->qnan_msb_set;
3734
 
3735
          if (HOST_BITS_PER_LONG == 32)
3736
            {
3737
              r->sig[0] = image0;
3738
              r->sig[1] = image1;
3739
              r->sig[2] = image2;
3740
              r->sig[3] = image3;
3741
            }
3742
          else
3743
            {
3744
              r->sig[0] = (image1 << 31 << 1) | image0;
3745
              r->sig[1] = (image3 << 31 << 1) | image2;
3746
            }
3747
          lshift_significand (r, r, SIGNIFICAND_BITS - 113);
3748
        }
3749
      else
3750
        {
3751
          r->cl = rvc_inf;
3752
          r->sign = sign;
3753
        }
3754
    }
3755
  else
3756
    {
3757
      r->cl = rvc_normal;
3758
      r->sign = sign;
3759
      SET_REAL_EXP (r, exp - 16383 + 1);
3760
 
3761
      if (HOST_BITS_PER_LONG == 32)
3762
        {
3763
          r->sig[0] = image0;
3764
          r->sig[1] = image1;
3765
          r->sig[2] = image2;
3766
          r->sig[3] = image3;
3767
        }
3768
      else
3769
        {
3770
          r->sig[0] = (image1 << 31 << 1) | image0;
3771
          r->sig[1] = (image3 << 31 << 1) | image2;
3772
        }
3773
      lshift_significand (r, r, SIGNIFICAND_BITS - 113);
3774
      r->sig[SIGSZ-1] |= SIG_MSB;
3775
    }
3776
}
3777
 
3778
const struct real_format ieee_quad_format =
3779
  {
3780
    encode_ieee_quad,
3781
    decode_ieee_quad,
3782
    2,
3783
    1,
3784
    113,
3785
    113,
3786
    -16381,
3787
    16384,
3788
    127,
3789
    127,
3790
    true,
3791
    true,
3792
    true,
3793
    true,
3794
    true
3795
  };
3796
 
3797
const struct real_format mips_quad_format =
3798
  {
3799
    encode_ieee_quad,
3800
    decode_ieee_quad,
3801
    2,
3802
    1,
3803
    113,
3804
    113,
3805
    -16381,
3806
    16384,
3807
    127,
3808
    127,
3809
    true,
3810
    true,
3811
    true,
3812
    true,
3813
    false
3814
  };
3815
 
3816
/* Descriptions of VAX floating point formats can be found beginning at
3817
 
3818
   http://h71000.www7.hp.com/doc/73FINAL/4515/4515pro_013.html#f_floating_point_format
3819
 
3820
   The thing to remember is that they're almost IEEE, except for word
3821
   order, exponent bias, and the lack of infinities, nans, and denormals.
3822
 
3823
   We don't implement the H_floating format here, simply because neither
3824
   the VAX or Alpha ports use it.  */
3825
 
3826
static void encode_vax_f (const struct real_format *fmt,
3827
                          long *, const REAL_VALUE_TYPE *);
3828
static void decode_vax_f (const struct real_format *,
3829
                          REAL_VALUE_TYPE *, const long *);
3830
static void encode_vax_d (const struct real_format *fmt,
3831
                          long *, const REAL_VALUE_TYPE *);
3832
static void decode_vax_d (const struct real_format *,
3833
                          REAL_VALUE_TYPE *, const long *);
3834
static void encode_vax_g (const struct real_format *fmt,
3835
                          long *, const REAL_VALUE_TYPE *);
3836
static void decode_vax_g (const struct real_format *,
3837
                          REAL_VALUE_TYPE *, const long *);
3838
 
3839
static void
3840
encode_vax_f (const struct real_format *fmt ATTRIBUTE_UNUSED, long *buf,
3841
              const REAL_VALUE_TYPE *r)
3842
{
3843
  unsigned long sign, exp, sig, image;
3844
 
3845
  sign = r->sign << 15;
3846
 
3847
  switch (r->cl)
3848
    {
3849
    case rvc_zero:
3850
      image = 0;
3851
      break;
3852
 
3853
    case rvc_inf:
3854
    case rvc_nan:
3855
      image = 0xffff7fff | sign;
3856
      break;
3857
 
3858
    case rvc_normal:
3859
      sig = (r->sig[SIGSZ-1] >> (HOST_BITS_PER_LONG - 24)) & 0x7fffff;
3860
      exp = REAL_EXP (r) + 128;
3861
 
3862
      image = (sig << 16) & 0xffff0000;
3863
      image |= sign;
3864
      image |= exp << 7;
3865
      image |= sig >> 16;
3866
      break;
3867
 
3868
    default:
3869
      gcc_unreachable ();
3870
    }
3871
 
3872
  buf[0] = image;
3873
}
3874
 
3875
static void
3876
decode_vax_f (const struct real_format *fmt ATTRIBUTE_UNUSED,
3877
              REAL_VALUE_TYPE *r, const long *buf)
3878
{
3879
  unsigned long image = buf[0] & 0xffffffff;
3880
  int exp = (image >> 7) & 0xff;
3881
 
3882
  memset (r, 0, sizeof (*r));
3883
 
3884
  if (exp != 0)
3885
    {
3886
      r->cl = rvc_normal;
3887
      r->sign = (image >> 15) & 1;
3888
      SET_REAL_EXP (r, exp - 128);
3889
 
3890
      image = ((image & 0x7f) << 16) | ((image >> 16) & 0xffff);
3891
      r->sig[SIGSZ-1] = (image << (HOST_BITS_PER_LONG - 24)) | SIG_MSB;
3892
    }
3893
}
3894
 
3895
static void
3896
encode_vax_d (const struct real_format *fmt ATTRIBUTE_UNUSED, long *buf,
3897
              const REAL_VALUE_TYPE *r)
3898
{
3899
  unsigned long image0, image1, sign = r->sign << 15;
3900
 
3901
  switch (r->cl)
3902
    {
3903
    case rvc_zero:
3904
      image0 = image1 = 0;
3905
      break;
3906
 
3907
    case rvc_inf:
3908
    case rvc_nan:
3909
      image0 = 0xffff7fff | sign;
3910
      image1 = 0xffffffff;
3911
      break;
3912
 
3913
    case rvc_normal:
3914
      /* Extract the significand into straight hi:lo.  */
3915
      if (HOST_BITS_PER_LONG == 64)
3916
        {
3917
          image0 = r->sig[SIGSZ-1];
3918
          image1 = (image0 >> (64 - 56)) & 0xffffffff;
3919
          image0 = (image0 >> (64 - 56 + 1) >> 31) & 0x7fffff;
3920
        }
3921
      else
3922
        {
3923
          image0 = r->sig[SIGSZ-1];
3924
          image1 = r->sig[SIGSZ-2];
3925
          image1 = (image0 << 24) | (image1 >> 8);
3926
          image0 = (image0 >> 8) & 0xffffff;
3927
        }
3928
 
3929
      /* Rearrange the half-words of the significand to match the
3930
         external format.  */
3931
      image0 = ((image0 << 16) | (image0 >> 16)) & 0xffff007f;
3932
      image1 = ((image1 << 16) | (image1 >> 16)) & 0xffffffff;
3933
 
3934
      /* Add the sign and exponent.  */
3935
      image0 |= sign;
3936
      image0 |= (REAL_EXP (r) + 128) << 7;
3937
      break;
3938
 
3939
    default:
3940
      gcc_unreachable ();
3941
    }
3942
 
3943
  if (FLOAT_WORDS_BIG_ENDIAN)
3944
    buf[0] = image1, buf[1] = image0;
3945
  else
3946
    buf[0] = image0, buf[1] = image1;
3947
}
3948
 
3949
static void
3950
decode_vax_d (const struct real_format *fmt ATTRIBUTE_UNUSED,
3951
              REAL_VALUE_TYPE *r, const long *buf)
3952
{
3953
  unsigned long image0, image1;
3954
  int exp;
3955
 
3956
  if (FLOAT_WORDS_BIG_ENDIAN)
3957
    image1 = buf[0], image0 = buf[1];
3958
  else
3959
    image0 = buf[0], image1 = buf[1];
3960
  image0 &= 0xffffffff;
3961
  image1 &= 0xffffffff;
3962
 
3963
  exp = (image0 >> 7) & 0xff;
3964
 
3965
  memset (r, 0, sizeof (*r));
3966
 
3967
  if (exp != 0)
3968
    {
3969
      r->cl = rvc_normal;
3970
      r->sign = (image0 >> 15) & 1;
3971
      SET_REAL_EXP (r, exp - 128);
3972
 
3973
      /* Rearrange the half-words of the external format into
3974
         proper ascending order.  */
3975
      image0 = ((image0 & 0x7f) << 16) | ((image0 >> 16) & 0xffff);
3976
      image1 = ((image1 & 0xffff) << 16) | ((image1 >> 16) & 0xffff);
3977
 
3978
      if (HOST_BITS_PER_LONG == 64)
3979
        {
3980
          image0 = (image0 << 31 << 1) | image1;
3981
          image0 <<= 64 - 56;
3982
          image0 |= SIG_MSB;
3983
          r->sig[SIGSZ-1] = image0;
3984
        }
3985
      else
3986
        {
3987
          r->sig[SIGSZ-1] = image0;
3988
          r->sig[SIGSZ-2] = image1;
3989
          lshift_significand (r, r, 2*HOST_BITS_PER_LONG - 56);
3990
          r->sig[SIGSZ-1] |= SIG_MSB;
3991
        }
3992
    }
3993
}
3994
 
3995
static void
3996
encode_vax_g (const struct real_format *fmt ATTRIBUTE_UNUSED, long *buf,
3997
              const REAL_VALUE_TYPE *r)
3998
{
3999
  unsigned long image0, image1, sign = r->sign << 15;
4000
 
4001
  switch (r->cl)
4002
    {
4003
    case rvc_zero:
4004
      image0 = image1 = 0;
4005
      break;
4006
 
4007
    case rvc_inf:
4008
    case rvc_nan:
4009
      image0 = 0xffff7fff | sign;
4010
      image1 = 0xffffffff;
4011
      break;
4012
 
4013
    case rvc_normal:
4014
      /* Extract the significand into straight hi:lo.  */
4015
      if (HOST_BITS_PER_LONG == 64)
4016
        {
4017
          image0 = r->sig[SIGSZ-1];
4018
          image1 = (image0 >> (64 - 53)) & 0xffffffff;
4019
          image0 = (image0 >> (64 - 53 + 1) >> 31) & 0xfffff;
4020
        }
4021
      else
4022
        {
4023
          image0 = r->sig[SIGSZ-1];
4024
          image1 = r->sig[SIGSZ-2];
4025
          image1 = (image0 << 21) | (image1 >> 11);
4026
          image0 = (image0 >> 11) & 0xfffff;
4027
        }
4028
 
4029
      /* Rearrange the half-words of the significand to match the
4030
         external format.  */
4031
      image0 = ((image0 << 16) | (image0 >> 16)) & 0xffff000f;
4032
      image1 = ((image1 << 16) | (image1 >> 16)) & 0xffffffff;
4033
 
4034
      /* Add the sign and exponent.  */
4035
      image0 |= sign;
4036
      image0 |= (REAL_EXP (r) + 1024) << 4;
4037
      break;
4038
 
4039
    default:
4040
      gcc_unreachable ();
4041
    }
4042
 
4043
  if (FLOAT_WORDS_BIG_ENDIAN)
4044
    buf[0] = image1, buf[1] = image0;
4045
  else
4046
    buf[0] = image0, buf[1] = image1;
4047
}
4048
 
4049
static void
4050
decode_vax_g (const struct real_format *fmt ATTRIBUTE_UNUSED,
4051
              REAL_VALUE_TYPE *r, const long *buf)
4052
{
4053
  unsigned long image0, image1;
4054
  int exp;
4055
 
4056
  if (FLOAT_WORDS_BIG_ENDIAN)
4057
    image1 = buf[0], image0 = buf[1];
4058
  else
4059
    image0 = buf[0], image1 = buf[1];
4060
  image0 &= 0xffffffff;
4061
  image1 &= 0xffffffff;
4062
 
4063
  exp = (image0 >> 4) & 0x7ff;
4064
 
4065
  memset (r, 0, sizeof (*r));
4066
 
4067
  if (exp != 0)
4068
    {
4069
      r->cl = rvc_normal;
4070
      r->sign = (image0 >> 15) & 1;
4071
      SET_REAL_EXP (r, exp - 1024);
4072
 
4073
      /* Rearrange the half-words of the external format into
4074
         proper ascending order.  */
4075
      image0 = ((image0 & 0xf) << 16) | ((image0 >> 16) & 0xffff);
4076
      image1 = ((image1 & 0xffff) << 16) | ((image1 >> 16) & 0xffff);
4077
 
4078
      if (HOST_BITS_PER_LONG == 64)
4079
        {
4080
          image0 = (image0 << 31 << 1) | image1;
4081
          image0 <<= 64 - 53;
4082
          image0 |= SIG_MSB;
4083
          r->sig[SIGSZ-1] = image0;
4084
        }
4085
      else
4086
        {
4087
          r->sig[SIGSZ-1] = image0;
4088
          r->sig[SIGSZ-2] = image1;
4089
          lshift_significand (r, r, 64 - 53);
4090
          r->sig[SIGSZ-1] |= SIG_MSB;
4091
        }
4092
    }
4093
}
4094
 
4095
const struct real_format vax_f_format =
4096
  {
4097
    encode_vax_f,
4098
    decode_vax_f,
4099
    2,
4100
    1,
4101
    24,
4102
    24,
4103
    -127,
4104
    127,
4105
    15,
4106
    15,
4107
    false,
4108
    false,
4109
    false,
4110
    false,
4111
    false
4112
  };
4113
 
4114
const struct real_format vax_d_format =
4115
  {
4116
    encode_vax_d,
4117
    decode_vax_d,
4118
    2,
4119
    1,
4120
    56,
4121
    56,
4122
    -127,
4123
    127,
4124
    15,
4125
    15,
4126
    false,
4127
    false,
4128
    false,
4129
    false,
4130
    false
4131
  };
4132
 
4133
const struct real_format vax_g_format =
4134
  {
4135
    encode_vax_g,
4136
    decode_vax_g,
4137
    2,
4138
    1,
4139
    53,
4140
    53,
4141
    -1023,
4142
    1023,
4143
    15,
4144
    15,
4145
    false,
4146
    false,
4147
    false,
4148
    false,
4149
    false
4150
  };
4151
 
4152
/* A good reference for these can be found in chapter 9 of
4153
   "ESA/390 Principles of Operation", IBM document number SA22-7201-01.
4154
   An on-line version can be found here:
4155
 
4156
   http://publibz.boulder.ibm.com/cgi-bin/bookmgr_OS390/BOOKS/DZ9AR001/9.1?DT=19930923083613
4157
*/
4158
 
4159
static void encode_i370_single (const struct real_format *fmt,
4160
                                long *, const REAL_VALUE_TYPE *);
4161
static void decode_i370_single (const struct real_format *,
4162
                                REAL_VALUE_TYPE *, const long *);
4163
static void encode_i370_double (const struct real_format *fmt,
4164
                                long *, const REAL_VALUE_TYPE *);
4165
static void decode_i370_double (const struct real_format *,
4166
                                REAL_VALUE_TYPE *, const long *);
4167
 
4168
static void
4169
encode_i370_single (const struct real_format *fmt ATTRIBUTE_UNUSED,
4170
                    long *buf, const REAL_VALUE_TYPE *r)
4171
{
4172
  unsigned long sign, exp, sig, image;
4173
 
4174
  sign = r->sign << 31;
4175
 
4176
  switch (r->cl)
4177
    {
4178
    case rvc_zero:
4179
      image = 0;
4180
      break;
4181
 
4182
    case rvc_inf:
4183
    case rvc_nan:
4184
      image = 0x7fffffff | sign;
4185
      break;
4186
 
4187
    case rvc_normal:
4188
      sig = (r->sig[SIGSZ-1] >> (HOST_BITS_PER_LONG - 24)) & 0xffffff;
4189
      exp = ((REAL_EXP (r) / 4) + 64) << 24;
4190
      image = sign | exp | sig;
4191
      break;
4192
 
4193
    default:
4194
      gcc_unreachable ();
4195
    }
4196
 
4197
  buf[0] = image;
4198
}
4199
 
4200
static void
4201
decode_i370_single (const struct real_format *fmt ATTRIBUTE_UNUSED,
4202
                    REAL_VALUE_TYPE *r, const long *buf)
4203
{
4204
  unsigned long sign, sig, image = buf[0];
4205
  int exp;
4206
 
4207
  sign = (image >> 31) & 1;
4208
  exp = (image >> 24) & 0x7f;
4209
  sig = image & 0xffffff;
4210
 
4211
  memset (r, 0, sizeof (*r));
4212
 
4213
  if (exp || sig)
4214
    {
4215
      r->cl = rvc_normal;
4216
      r->sign = sign;
4217
      SET_REAL_EXP (r, (exp - 64) * 4);
4218
      r->sig[SIGSZ-1] = sig << (HOST_BITS_PER_LONG - 24);
4219
      normalize (r);
4220
    }
4221
}
4222
 
4223
static void
4224
encode_i370_double (const struct real_format *fmt ATTRIBUTE_UNUSED,
4225
                    long *buf, const REAL_VALUE_TYPE *r)
4226
{
4227
  unsigned long sign, exp, image_hi, image_lo;
4228
 
4229
  sign = r->sign << 31;
4230
 
4231
  switch (r->cl)
4232
    {
4233
    case rvc_zero:
4234
      image_hi = image_lo = 0;
4235
      break;
4236
 
4237
    case rvc_inf:
4238
    case rvc_nan:
4239
      image_hi = 0x7fffffff | sign;
4240
      image_lo = 0xffffffff;
4241
      break;
4242
 
4243
    case rvc_normal:
4244
      if (HOST_BITS_PER_LONG == 64)
4245
        {
4246
          image_hi = r->sig[SIGSZ-1];
4247
          image_lo = (image_hi >> (64 - 56)) & 0xffffffff;
4248
          image_hi = (image_hi >> (64 - 56 + 1) >> 31) & 0xffffff;
4249
        }
4250
      else
4251
        {
4252
          image_hi = r->sig[SIGSZ-1];
4253
          image_lo = r->sig[SIGSZ-2];
4254
          image_lo = (image_lo >> 8) | (image_hi << 24);
4255
          image_hi >>= 8;
4256
        }
4257
 
4258
      exp = ((REAL_EXP (r) / 4) + 64) << 24;
4259
      image_hi |= sign | exp;
4260
      break;
4261
 
4262
    default:
4263
      gcc_unreachable ();
4264
    }
4265
 
4266
  if (FLOAT_WORDS_BIG_ENDIAN)
4267
    buf[0] = image_hi, buf[1] = image_lo;
4268
  else
4269
    buf[0] = image_lo, buf[1] = image_hi;
4270
}
4271
 
4272
static void
4273
decode_i370_double (const struct real_format *fmt ATTRIBUTE_UNUSED,
4274
                    REAL_VALUE_TYPE *r, const long *buf)
4275
{
4276
  unsigned long sign, image_hi, image_lo;
4277
  int exp;
4278
 
4279
  if (FLOAT_WORDS_BIG_ENDIAN)
4280
    image_hi = buf[0], image_lo = buf[1];
4281
  else
4282
    image_lo = buf[0], image_hi = buf[1];
4283
 
4284
  sign = (image_hi >> 31) & 1;
4285
  exp = (image_hi >> 24) & 0x7f;
4286
  image_hi &= 0xffffff;
4287
  image_lo &= 0xffffffff;
4288
 
4289
  memset (r, 0, sizeof (*r));
4290
 
4291
  if (exp || image_hi || image_lo)
4292
    {
4293
      r->cl = rvc_normal;
4294
      r->sign = sign;
4295
      SET_REAL_EXP (r, (exp - 64) * 4 + (SIGNIFICAND_BITS - 56));
4296
 
4297
      if (HOST_BITS_PER_LONG == 32)
4298
        {
4299
          r->sig[0] = image_lo;
4300
          r->sig[1] = image_hi;
4301
        }
4302
      else
4303
        r->sig[0] = image_lo | (image_hi << 31 << 1);
4304
 
4305
      normalize (r);
4306
    }
4307
}
4308
 
4309
const struct real_format i370_single_format =
4310
  {
4311
    encode_i370_single,
4312
    decode_i370_single,
4313
    16,
4314
    4,
4315
    6,
4316
    6,
4317
    -64,
4318
    63,
4319
    31,
4320
    31,
4321
    false,
4322
    false,
4323
    false, /* ??? The encoding does allow for "unnormals".  */
4324
    false, /* ??? The encoding does allow for "unnormals".  */
4325
    false
4326
  };
4327
 
4328
const struct real_format i370_double_format =
4329
  {
4330
    encode_i370_double,
4331
    decode_i370_double,
4332
    16,
4333
    4,
4334
    14,
4335
    14,
4336
    -64,
4337
    63,
4338
    63,
4339
    63,
4340
    false,
4341
    false,
4342
    false, /* ??? The encoding does allow for "unnormals".  */
4343
    false, /* ??? The encoding does allow for "unnormals".  */
4344
    false
4345
  };
4346
 
4347
/* Encode real R into a single precision DFP value in BUF.  */
4348
static void
4349
encode_decimal_single (const struct real_format *fmt ATTRIBUTE_UNUSED,
4350
                       long *buf ATTRIBUTE_UNUSED,
4351
                       const REAL_VALUE_TYPE *r ATTRIBUTE_UNUSED)
4352
{
4353
  encode_decimal32 (fmt, buf, r);
4354
}
4355
 
4356
/* Decode a single precision DFP value in BUF into a real R.  */
4357
static void
4358
decode_decimal_single (const struct real_format *fmt ATTRIBUTE_UNUSED,
4359
                       REAL_VALUE_TYPE *r ATTRIBUTE_UNUSED,
4360
                       const long *buf ATTRIBUTE_UNUSED)
4361
{
4362
  decode_decimal32 (fmt, r, buf);
4363
}
4364
 
4365
/* Encode real R into a double precision DFP value in BUF.  */
4366
static void
4367
encode_decimal_double (const struct real_format *fmt ATTRIBUTE_UNUSED,
4368
                       long *buf ATTRIBUTE_UNUSED,
4369
                       const REAL_VALUE_TYPE *r ATTRIBUTE_UNUSED)
4370
{
4371
  encode_decimal64 (fmt, buf, r);
4372
}
4373
 
4374
/* Decode a double precision DFP value in BUF into a real R.  */
4375
static void
4376
decode_decimal_double (const struct real_format *fmt ATTRIBUTE_UNUSED,
4377
                       REAL_VALUE_TYPE *r ATTRIBUTE_UNUSED,
4378
                       const long *buf ATTRIBUTE_UNUSED)
4379
{
4380
  decode_decimal64 (fmt, r, buf);
4381
}
4382
 
4383
/* Encode real R into a quad precision DFP value in BUF.  */
4384
static void
4385
encode_decimal_quad (const struct real_format *fmt ATTRIBUTE_UNUSED,
4386
                     long *buf ATTRIBUTE_UNUSED,
4387
                     const REAL_VALUE_TYPE *r ATTRIBUTE_UNUSED)
4388
{
4389
  encode_decimal128 (fmt, buf, r);
4390
}
4391
 
4392
/* Decode a quad precision DFP value in BUF into a real R.  */
4393
static void
4394
decode_decimal_quad (const struct real_format *fmt ATTRIBUTE_UNUSED,
4395
                     REAL_VALUE_TYPE *r ATTRIBUTE_UNUSED,
4396
                     const long *buf ATTRIBUTE_UNUSED)
4397
{
4398
  decode_decimal128 (fmt, r, buf);
4399
}
4400
 
4401
/* Single precision decimal floating point (IEEE 754R). */
4402
const struct real_format decimal_single_format =
4403
  {
4404
    encode_decimal_single,
4405
    decode_decimal_single,
4406
    10,
4407
    1,  /* log10 */
4408
    7,
4409
    7,
4410
    -95,
4411
    96,
4412
    31,
4413
    31,
4414
    true,
4415
    true,
4416
    true,
4417
    true,
4418
    true
4419
  };
4420
 
4421
/* Double precision decimal floating point (IEEE 754R). */
4422
const struct real_format decimal_double_format =
4423
  {
4424
    encode_decimal_double,
4425
    decode_decimal_double,
4426
    10,
4427
    1,  /* log10 */
4428
    16,
4429
    16,
4430
    -383,
4431
    384,
4432
    63,
4433
    63,
4434
    true,
4435
    true,
4436
    true,
4437
    true,
4438
    true
4439
  };
4440
 
4441
/* Quad precision decimal floating point (IEEE 754R). */
4442
const struct real_format decimal_quad_format =
4443
  {
4444
    encode_decimal_quad,
4445
    decode_decimal_quad,
4446
    10,
4447
    1,  /* log10 */
4448
    34,
4449
    34,
4450
    -6143,
4451
    6144,
4452
    127,
4453
    127,
4454
    true,
4455
    true,
4456
    true,
4457
    true,
4458
    true
4459
  };
4460
 
4461
/* The "twos-complement" c4x format is officially defined as
4462
 
4463
        x = s(~s).f * 2**e
4464
 
4465
   This is rather misleading.  One must remember that F is signed.
4466
   A better description would be
4467
 
4468
        x = -1**s * ((s + 1 + .f) * 2**e
4469
 
4470
   So if we have a (4 bit) fraction of .1000 with a sign bit of 1,
4471
   that's -1 * (1+1+(-.5)) == -1.5.  I think.
4472
 
4473
   The constructions here are taken from Tables 5-1 and 5-2 of the
4474
   TMS320C4x User's Guide wherein step-by-step instructions for
4475
   conversion from IEEE are presented.  That's close enough to our
4476
   internal representation so as to make things easy.
4477
 
4478
   See http://www-s.ti.com/sc/psheets/spru063c/spru063c.pdf  */
4479
 
4480
static void encode_c4x_single (const struct real_format *fmt,
4481
                               long *, const REAL_VALUE_TYPE *);
4482
static void decode_c4x_single (const struct real_format *,
4483
                               REAL_VALUE_TYPE *, const long *);
4484
static void encode_c4x_extended (const struct real_format *fmt,
4485
                                 long *, const REAL_VALUE_TYPE *);
4486
static void decode_c4x_extended (const struct real_format *,
4487
                                 REAL_VALUE_TYPE *, const long *);
4488
 
4489
static void
4490
encode_c4x_single (const struct real_format *fmt ATTRIBUTE_UNUSED,
4491
                   long *buf, const REAL_VALUE_TYPE *r)
4492
{
4493
  unsigned long image, exp, sig;
4494
 
4495
  switch (r->cl)
4496
    {
4497
    case rvc_zero:
4498
      exp = -128;
4499
      sig = 0;
4500
      break;
4501
 
4502
    case rvc_inf:
4503
    case rvc_nan:
4504
      exp = 127;
4505
      sig = 0x800000 - r->sign;
4506
      break;
4507
 
4508
    case rvc_normal:
4509
      exp = REAL_EXP (r) - 1;
4510
      sig = (r->sig[SIGSZ-1] >> (HOST_BITS_PER_LONG - 24)) & 0x7fffff;
4511
      if (r->sign)
4512
        {
4513
          if (sig)
4514
            sig = -sig;
4515
          else
4516
            exp--;
4517
          sig |= 0x800000;
4518
        }
4519
      break;
4520
 
4521
    default:
4522
      gcc_unreachable ();
4523
    }
4524
 
4525
  image = ((exp & 0xff) << 24) | (sig & 0xffffff);
4526
  buf[0] = image;
4527
}
4528
 
4529
static void
4530
decode_c4x_single (const struct real_format *fmt ATTRIBUTE_UNUSED,
4531
                   REAL_VALUE_TYPE *r, const long *buf)
4532
{
4533
  unsigned long image = buf[0];
4534
  unsigned long sig;
4535
  int exp, sf;
4536
 
4537
  exp = (((image >> 24) & 0xff) ^ 0x80) - 0x80;
4538
  sf = ((image & 0xffffff) ^ 0x800000) - 0x800000;
4539
 
4540
  memset (r, 0, sizeof (*r));
4541
 
4542
  if (exp != -128)
4543
    {
4544
      r->cl = rvc_normal;
4545
 
4546
      sig = sf & 0x7fffff;
4547
      if (sf < 0)
4548
        {
4549
          r->sign = 1;
4550
          if (sig)
4551
            sig = -sig;
4552
          else
4553
            exp++;
4554
        }
4555
      sig = (sig << (HOST_BITS_PER_LONG - 24)) | SIG_MSB;
4556
 
4557
      SET_REAL_EXP (r, exp + 1);
4558
      r->sig[SIGSZ-1] = sig;
4559
    }
4560
}
4561
 
4562
static void
4563
encode_c4x_extended (const struct real_format *fmt ATTRIBUTE_UNUSED,
4564
                     long *buf, const REAL_VALUE_TYPE *r)
4565
{
4566
  unsigned long exp, sig;
4567
 
4568
  switch (r->cl)
4569
    {
4570
    case rvc_zero:
4571
      exp = -128;
4572
      sig = 0;
4573
      break;
4574
 
4575
    case rvc_inf:
4576
    case rvc_nan:
4577
      exp = 127;
4578
      sig = 0x80000000 - r->sign;
4579
      break;
4580
 
4581
    case rvc_normal:
4582
      exp = REAL_EXP (r) - 1;
4583
 
4584
      sig = r->sig[SIGSZ-1];
4585
      if (HOST_BITS_PER_LONG == 64)
4586
        sig = sig >> 1 >> 31;
4587
      sig &= 0x7fffffff;
4588
 
4589
      if (r->sign)
4590
        {
4591
          if (sig)
4592
            sig = -sig;
4593
          else
4594
            exp--;
4595
          sig |= 0x80000000;
4596
        }
4597
      break;
4598
 
4599
    default:
4600
      gcc_unreachable ();
4601
    }
4602
 
4603
  exp = (exp & 0xff) << 24;
4604
  sig &= 0xffffffff;
4605
 
4606
  if (FLOAT_WORDS_BIG_ENDIAN)
4607
    buf[0] = exp, buf[1] = sig;
4608
  else
4609
    buf[0] = sig, buf[0] = exp;
4610
}
4611
 
4612
static void
4613
decode_c4x_extended (const struct real_format *fmt ATTRIBUTE_UNUSED,
4614
                     REAL_VALUE_TYPE *r, const long *buf)
4615
{
4616
  unsigned long sig;
4617
  int exp, sf;
4618
 
4619
  if (FLOAT_WORDS_BIG_ENDIAN)
4620
    exp = buf[0], sf = buf[1];
4621
  else
4622
    sf = buf[0], exp = buf[1];
4623
 
4624
  exp = (((exp >> 24) & 0xff) & 0x80) - 0x80;
4625
  sf = ((sf & 0xffffffff) ^ 0x80000000) - 0x80000000;
4626
 
4627
  memset (r, 0, sizeof (*r));
4628
 
4629
  if (exp != -128)
4630
    {
4631
      r->cl = rvc_normal;
4632
 
4633
      sig = sf & 0x7fffffff;
4634
      if (sf < 0)
4635
        {
4636
          r->sign = 1;
4637
          if (sig)
4638
            sig = -sig;
4639
          else
4640
            exp++;
4641
        }
4642
      if (HOST_BITS_PER_LONG == 64)
4643
        sig = sig << 1 << 31;
4644
      sig |= SIG_MSB;
4645
 
4646
      SET_REAL_EXP (r, exp + 1);
4647
      r->sig[SIGSZ-1] = sig;
4648
    }
4649
}
4650
 
4651
const struct real_format c4x_single_format =
4652
  {
4653
    encode_c4x_single,
4654
    decode_c4x_single,
4655
    2,
4656
    1,
4657
    24,
4658
    24,
4659
    -126,
4660
    128,
4661
    23,
4662
    -1,
4663
    false,
4664
    false,
4665
    false,
4666
    false,
4667
    false
4668
  };
4669
 
4670
const struct real_format c4x_extended_format =
4671
  {
4672
    encode_c4x_extended,
4673
    decode_c4x_extended,
4674
    2,
4675
    1,
4676
    32,
4677
    32,
4678
    -126,
4679
    128,
4680
    31,
4681
    -1,
4682
    false,
4683
    false,
4684
    false,
4685
    false,
4686
    false
4687
  };
4688
 
4689
 
4690
/* A synthetic "format" for internal arithmetic.  It's the size of the
4691
   internal significand minus the two bits needed for proper rounding.
4692
   The encode and decode routines exist only to satisfy our paranoia
4693
   harness.  */
4694
 
4695
static void encode_internal (const struct real_format *fmt,
4696
                             long *, const REAL_VALUE_TYPE *);
4697
static void decode_internal (const struct real_format *,
4698
                             REAL_VALUE_TYPE *, const long *);
4699
 
4700
static void
4701
encode_internal (const struct real_format *fmt ATTRIBUTE_UNUSED, long *buf,
4702
                 const REAL_VALUE_TYPE *r)
4703
{
4704
  memcpy (buf, r, sizeof (*r));
4705
}
4706
 
4707
static void
4708
decode_internal (const struct real_format *fmt ATTRIBUTE_UNUSED,
4709
                 REAL_VALUE_TYPE *r, const long *buf)
4710
{
4711
  memcpy (r, buf, sizeof (*r));
4712
}
4713
 
4714
const struct real_format real_internal_format =
4715
  {
4716
    encode_internal,
4717
    decode_internal,
4718
    2,
4719
    1,
4720
    SIGNIFICAND_BITS - 2,
4721
    SIGNIFICAND_BITS - 2,
4722
    -MAX_EXP,
4723
    MAX_EXP,
4724
    -1,
4725
    -1,
4726
    true,
4727
    true,
4728
    false,
4729
    true,
4730
    true
4731
  };
4732
 
4733
/* Calculate the square root of X in mode MODE, and store the result
4734
   in R.  Return TRUE if the operation does not raise an exception.
4735
   For details see "High Precision Division and Square Root",
4736
   Alan H. Karp and Peter Markstein, HP Lab Report 93-93-42, June
4737
   1993.  http://www.hpl.hp.com/techreports/93/HPL-93-42.pdf.  */
4738
 
4739
bool
4740
real_sqrt (REAL_VALUE_TYPE *r, enum machine_mode mode,
4741
           const REAL_VALUE_TYPE *x)
4742
{
4743
  static REAL_VALUE_TYPE halfthree;
4744
  static bool init = false;
4745
  REAL_VALUE_TYPE h, t, i;
4746
  int iter, exp;
4747
 
4748
  /* sqrt(-0.0) is -0.0.  */
4749
  if (real_isnegzero (x))
4750
    {
4751
      *r = *x;
4752
      return false;
4753
    }
4754
 
4755
  /* Negative arguments return NaN.  */
4756
  if (real_isneg (x))
4757
    {
4758
      get_canonical_qnan (r, 0);
4759
      return false;
4760
    }
4761
 
4762
  /* Infinity and NaN return themselves.  */
4763
  if (real_isinf (x) || real_isnan (x))
4764
    {
4765
      *r = *x;
4766
      return false;
4767
    }
4768
 
4769
  if (!init)
4770
    {
4771
      do_add (&halfthree, &dconst1, &dconsthalf, 0);
4772
      init = true;
4773
    }
4774
 
4775
  /* Initial guess for reciprocal sqrt, i.  */
4776
  exp = real_exponent (x);
4777
  real_ldexp (&i, &dconst1, -exp/2);
4778
 
4779
  /* Newton's iteration for reciprocal sqrt, i.  */
4780
  for (iter = 0; iter < 16; iter++)
4781
    {
4782
      /* i(n+1) = i(n) * (1.5 - 0.5*i(n)*i(n)*x).  */
4783
      do_multiply (&t, x, &i);
4784
      do_multiply (&h, &t, &i);
4785
      do_multiply (&t, &h, &dconsthalf);
4786
      do_add (&h, &halfthree, &t, 1);
4787
      do_multiply (&t, &i, &h);
4788
 
4789
      /* Check for early convergence.  */
4790
      if (iter >= 6 && real_identical (&i, &t))
4791
        break;
4792
 
4793
      /* ??? Unroll loop to avoid copying.  */
4794
      i = t;
4795
    }
4796
 
4797
  /* Final iteration: r = i*x + 0.5*i*x*(1.0 - i*(i*x)).  */
4798
  do_multiply (&t, x, &i);
4799
  do_multiply (&h, &t, &i);
4800
  do_add (&i, &dconst1, &h, 1);
4801
  do_multiply (&h, &t, &i);
4802
  do_multiply (&i, &dconsthalf, &h);
4803
  do_add (&h, &t, &i, 0);
4804
 
4805
  /* ??? We need a Tuckerman test to get the last bit.  */
4806
 
4807
  real_convert (r, mode, &h);
4808
  return true;
4809
}
4810
 
4811
/* Calculate X raised to the integer exponent N in mode MODE and store
4812
   the result in R.  Return true if the result may be inexact due to
4813
   loss of precision.  The algorithm is the classic "left-to-right binary
4814
   method" described in section 4.6.3 of Donald Knuth's "Seminumerical
4815
   Algorithms", "The Art of Computer Programming", Volume 2.  */
4816
 
4817
bool
4818
real_powi (REAL_VALUE_TYPE *r, enum machine_mode mode,
4819
           const REAL_VALUE_TYPE *x, HOST_WIDE_INT n)
4820
{
4821
  unsigned HOST_WIDE_INT bit;
4822
  REAL_VALUE_TYPE t;
4823
  bool inexact = false;
4824
  bool init = false;
4825
  bool neg;
4826
  int i;
4827
 
4828
  if (n == 0)
4829
    {
4830
      *r = dconst1;
4831
      return false;
4832
    }
4833
  else if (n < 0)
4834
    {
4835
      /* Don't worry about overflow, from now on n is unsigned.  */
4836
      neg = true;
4837
      n = -n;
4838
    }
4839
  else
4840
    neg = false;
4841
 
4842
  t = *x;
4843
  bit = (unsigned HOST_WIDE_INT) 1 << (HOST_BITS_PER_WIDE_INT - 1);
4844
  for (i = 0; i < HOST_BITS_PER_WIDE_INT; i++)
4845
    {
4846
      if (init)
4847
        {
4848
          inexact |= do_multiply (&t, &t, &t);
4849
          if (n & bit)
4850
            inexact |= do_multiply (&t, &t, x);
4851
        }
4852
      else if (n & bit)
4853
        init = true;
4854
      bit >>= 1;
4855
    }
4856
 
4857
  if (neg)
4858
    inexact |= do_divide (&t, &dconst1, &t);
4859
 
4860
  real_convert (r, mode, &t);
4861
  return inexact;
4862
}
4863
 
4864
/* Round X to the nearest integer not larger in absolute value, i.e.
4865
   towards zero, placing the result in R in mode MODE.  */
4866
 
4867
void
4868
real_trunc (REAL_VALUE_TYPE *r, enum machine_mode mode,
4869
            const REAL_VALUE_TYPE *x)
4870
{
4871
  do_fix_trunc (r, x);
4872
  if (mode != VOIDmode)
4873
    real_convert (r, mode, r);
4874
}
4875
 
4876
/* Round X to the largest integer not greater in value, i.e. round
4877
   down, placing the result in R in mode MODE.  */
4878
 
4879
void
4880
real_floor (REAL_VALUE_TYPE *r, enum machine_mode mode,
4881
            const REAL_VALUE_TYPE *x)
4882
{
4883
  REAL_VALUE_TYPE t;
4884
 
4885
  do_fix_trunc (&t, x);
4886
  if (! real_identical (&t, x) && x->sign)
4887
    do_add (&t, &t, &dconstm1, 0);
4888
  if (mode != VOIDmode)
4889
    real_convert (r, mode, &t);
4890
  else
4891
    *r = t;
4892
}
4893
 
4894
/* Round X to the smallest integer not less then argument, i.e. round
4895
   up, placing the result in R in mode MODE.  */
4896
 
4897
void
4898
real_ceil (REAL_VALUE_TYPE *r, enum machine_mode mode,
4899
           const REAL_VALUE_TYPE *x)
4900
{
4901
  REAL_VALUE_TYPE t;
4902
 
4903
  do_fix_trunc (&t, x);
4904
  if (! real_identical (&t, x) && ! x->sign)
4905
    do_add (&t, &t, &dconst1, 0);
4906
  if (mode != VOIDmode)
4907
    real_convert (r, mode, &t);
4908
  else
4909
    *r = t;
4910
}
4911
 
4912
/* Round X to the nearest integer, but round halfway cases away from
4913
   zero.  */
4914
 
4915
void
4916
real_round (REAL_VALUE_TYPE *r, enum machine_mode mode,
4917
            const REAL_VALUE_TYPE *x)
4918
{
4919
  do_add (r, x, &dconsthalf, x->sign);
4920
  do_fix_trunc (r, r);
4921
  if (mode != VOIDmode)
4922
    real_convert (r, mode, r);
4923
}
4924
 
4925
/* Set the sign of R to the sign of X.  */
4926
 
4927
void
4928
real_copysign (REAL_VALUE_TYPE *r, const REAL_VALUE_TYPE *x)
4929
{
4930
  r->sign = x->sign;
4931
}
4932
 

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.