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

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [gnu-old/] [newlib-1.17.0/] [newlib/] [libc/] [stdlib/] [mprec.c] - Blame information for rev 816

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 148 jeremybenn
/****************************************************************
2
 *
3
 * The author of this software is David M. Gay.
4
 *
5
 * Copyright (c) 1991 by AT&T.
6
 *
7
 * Permission to use, copy, modify, and distribute this software for any
8
 * purpose without fee is hereby granted, provided that this entire notice
9
 * is included in all copies of any software which is or includes a copy
10
 * or modification of this software and in all copies of the supporting
11
 * documentation for such software.
12
 *
13
 * THIS SOFTWARE IS BEING PROVIDED "AS IS", WITHOUT ANY EXPRESS OR IMPLIED
14
 * WARRANTY.  IN PARTICULAR, NEITHER THE AUTHOR NOR AT&T MAKES ANY
15
 * REPRESENTATION OR WARRANTY OF ANY KIND CONCERNING THE MERCHANTABILITY
16
 * OF THIS SOFTWARE OR ITS FITNESS FOR ANY PARTICULAR PURPOSE.
17
 *
18
 ***************************************************************/
19
 
20
/* Please send bug reports to
21
        David M. Gay
22
        AT&T Bell Laboratories, Room 2C-463
23
        600 Mountain Avenue
24
        Murray Hill, NJ 07974-2070
25
        U.S.A.
26
        dmg@research.att.com or research!dmg
27
 */
28
 
29
/* strtod for IEEE-, VAX-, and IBM-arithmetic machines.
30
 *
31
 * This strtod returns a nearest machine number to the input decimal
32
 * string (or sets errno to ERANGE).  With IEEE arithmetic, ties are
33
 * broken by the IEEE round-even rule.  Otherwise ties are broken by
34
 * biased rounding (add half and chop).
35
 *
36
 * Inspired loosely by William D. Clinger's paper "How to Read Floating
37
 * Point Numbers Accurately" [Proc. ACM SIGPLAN '90, pp. 92-101].
38
 *
39
 * Modifications:
40
 *
41
 *      1. We only require IEEE, IBM, or VAX double-precision
42
 *              arithmetic (not IEEE double-extended).
43
 *      2. We get by with floating-point arithmetic in a case that
44
 *              Clinger missed -- when we're computing d * 10^n
45
 *              for a small integer d and the integer n is not too
46
 *              much larger than 22 (the maximum integer k for which
47
 *              we can represent 10^k exactly), we may be able to
48
 *              compute (d*10^k) * 10^(e-k) with just one roundoff.
49
 *      3. Rather than a bit-at-a-time adjustment of the binary
50
 *              result in the hard case, we use floating-point
51
 *              arithmetic to determine the adjustment to within
52
 *              one bit; only in really hard cases do we need to
53
 *              compute a second residual.
54
 *      4. Because of 3., we don't need a large table of powers of 10
55
 *              for ten-to-e (just some small tables, e.g. of 10^k
56
 *              for 0 <= k <= 22).
57
 */
58
 
59
/*
60
 * #define IEEE_8087 for IEEE-arithmetic machines where the least
61
 *      significant byte has the lowest address.
62
 * #define IEEE_MC68k for IEEE-arithmetic machines where the most
63
 *      significant byte has the lowest address.
64
 * #define Sudden_Underflow for IEEE-format machines without gradual
65
 *      underflow (i.e., that flush to zero on underflow).
66
 * #define IBM for IBM mainframe-style floating-point arithmetic.
67
 * #define VAX for VAX-style floating-point arithmetic.
68
 * #define Unsigned_Shifts if >> does treats its left operand as unsigned.
69
 * #define No_leftright to omit left-right logic in fast floating-point
70
 *      computation of dtoa.
71
 * #define Check_FLT_ROUNDS if FLT_ROUNDS can assume the values 2 or 3.
72
 * #define RND_PRODQUOT to use rnd_prod and rnd_quot (assembly routines
73
 *      that use extended-precision instructions to compute rounded
74
 *      products and quotients) with IBM.
75
 * #define ROUND_BIASED for IEEE-format with biased rounding.
76
 * #define Inaccurate_Divide for IEEE-format with correctly rounded
77
 *      products but inaccurate quotients, e.g., for Intel i860.
78
 * #define Just_16 to store 16 bits per 32-bit long when doing high-precision
79
 *      integer arithmetic.  Whether this speeds things up or slows things
80
 *      down depends on the machine and the number being converted.
81
 */
82
 
83
#include <_ansi.h>
84
#include <stdlib.h>
85
#include <string.h>
86
#include <reent.h>
87
#include "mprec.h"
88
 
89
/* reent.c knows this value */
90
#define _Kmax 15
91
 
92
_Bigint *
93
_DEFUN (Balloc, (ptr, k), struct _reent *ptr _AND int k)
94
{
95
  int x;
96
  _Bigint *rv ;
97
 
98
  _REENT_CHECK_MP(ptr);
99
  if (_REENT_MP_FREELIST(ptr) == NULL)
100
    {
101
      /* Allocate a list of pointers to the mprec objects */
102
      _REENT_MP_FREELIST(ptr) = (struct _Bigint **) _calloc_r (ptr,
103
                                                      sizeof (struct _Bigint *),
104
                                                      _Kmax + 1);
105
      if (_REENT_MP_FREELIST(ptr) == NULL)
106
        {
107
          return NULL;
108
        }
109
    }
110
 
111
  if ((rv = _REENT_MP_FREELIST(ptr)[k]) != 0)
112
    {
113
      _REENT_MP_FREELIST(ptr)[k] = rv->_next;
114
    }
115
  else
116
    {
117
      x = 1 << k;
118
      /* Allocate an mprec Bigint and stick in in the freelist */
119
      rv = (_Bigint *) _calloc_r (ptr,
120
                                  1,
121
                                  sizeof (_Bigint) +
122
                                  (x-1) * sizeof(rv->_x));
123
      if (rv == NULL) return NULL;
124
      rv->_k = k;
125
      rv->_maxwds = x;
126
    }
127
  rv->_sign = rv->_wds = 0;
128
  return rv;
129
}
130
 
131
void
132
_DEFUN (Bfree, (ptr, v), struct _reent *ptr _AND _Bigint * v)
133
{
134
  _REENT_CHECK_MP(ptr);
135
  if (v)
136
    {
137
      v->_next = _REENT_MP_FREELIST(ptr)[v->_k];
138
      _REENT_MP_FREELIST(ptr)[v->_k] = v;
139
    }
140
}
141
 
142
_Bigint *
143
_DEFUN (multadd, (ptr, b, m, a),
144
        struct _reent *ptr _AND
145
        _Bigint * b _AND
146
        int m _AND
147
        int a)
148
{
149
  int i, wds;
150
  __ULong *x, y;
151
#ifdef Pack_32
152
  __ULong xi, z;
153
#endif
154
  _Bigint *b1;
155
 
156
  wds = b->_wds;
157
  x = b->_x;
158
  i = 0;
159
  do
160
    {
161
#ifdef Pack_32
162
      xi = *x;
163
      y = (xi & 0xffff) * m + a;
164
      z = (xi >> 16) * m + (y >> 16);
165
      a = (int) (z >> 16);
166
      *x++ = (z << 16) + (y & 0xffff);
167
#else
168
      y = *x * m + a;
169
      a = (int) (y >> 16);
170
      *x++ = y & 0xffff;
171
#endif
172
    }
173
  while (++i < wds);
174
  if (a)
175
    {
176
      if (wds >= b->_maxwds)
177
        {
178
          b1 = Balloc (ptr, b->_k + 1);
179
          Bcopy (b1, b);
180
          Bfree (ptr, b);
181
          b = b1;
182
        }
183
      b->_x[wds++] = a;
184
      b->_wds = wds;
185
    }
186
  return b;
187
}
188
 
189
_Bigint *
190
_DEFUN (s2b, (ptr, s, nd0, nd, y9),
191
        struct _reent * ptr _AND
192
        _CONST char *s _AND
193
        int nd0 _AND
194
        int nd _AND
195
        __ULong y9)
196
{
197
  _Bigint *b;
198
  int i, k;
199
  __Long x, y;
200
 
201
  x = (nd + 8) / 9;
202
  for (k = 0, y = 1; x > y; y <<= 1, k++);
203
#ifdef Pack_32
204
  b = Balloc (ptr, k);
205
  b->_x[0] = y9;
206
  b->_wds = 1;
207
#else
208
  b = Balloc (ptr, k + 1);
209
  b->_x[0] = y9 & 0xffff;
210
  b->_wds = (b->_x[1] = y9 >> 16) ? 2 : 1;
211
#endif
212
 
213
  i = 9;
214
  if (9 < nd0)
215
    {
216
      s += 9;
217
      do
218
        b = multadd (ptr, b, 10, *s++ - '0');
219
      while (++i < nd0);
220
      s++;
221
    }
222
  else
223
    s += 10;
224
  for (; i < nd; i++)
225
    b = multadd (ptr, b, 10, *s++ - '0');
226
  return b;
227
}
228
 
229
int
230
_DEFUN (hi0bits,
231
        (x), register __ULong x)
232
{
233
  register int k = 0;
234
 
235
  if (!(x & 0xffff0000))
236
    {
237
      k = 16;
238
      x <<= 16;
239
    }
240
  if (!(x & 0xff000000))
241
    {
242
      k += 8;
243
      x <<= 8;
244
    }
245
  if (!(x & 0xf0000000))
246
    {
247
      k += 4;
248
      x <<= 4;
249
    }
250
  if (!(x & 0xc0000000))
251
    {
252
      k += 2;
253
      x <<= 2;
254
    }
255
  if (!(x & 0x80000000))
256
    {
257
      k++;
258
      if (!(x & 0x40000000))
259
        return 32;
260
    }
261
  return k;
262
}
263
 
264
int
265
_DEFUN (lo0bits, (y), __ULong *y)
266
{
267
  register int k;
268
  register __ULong x = *y;
269
 
270
  if (x & 7)
271
    {
272
      if (x & 1)
273
        return 0;
274
      if (x & 2)
275
        {
276
          *y = x >> 1;
277
          return 1;
278
        }
279
      *y = x >> 2;
280
      return 2;
281
    }
282
  k = 0;
283
  if (!(x & 0xffff))
284
    {
285
      k = 16;
286
      x >>= 16;
287
    }
288
  if (!(x & 0xff))
289
    {
290
      k += 8;
291
      x >>= 8;
292
    }
293
  if (!(x & 0xf))
294
    {
295
      k += 4;
296
      x >>= 4;
297
    }
298
  if (!(x & 0x3))
299
    {
300
      k += 2;
301
      x >>= 2;
302
    }
303
  if (!(x & 1))
304
    {
305
      k++;
306
      x >>= 1;
307
      if (!x & 1)
308
        return 32;
309
    }
310
  *y = x;
311
  return k;
312
}
313
 
314
_Bigint *
315
_DEFUN (i2b, (ptr, i), struct _reent * ptr _AND int i)
316
{
317
  _Bigint *b;
318
 
319
  b = Balloc (ptr, 1);
320
  b->_x[0] = i;
321
  b->_wds = 1;
322
  return b;
323
}
324
 
325
_Bigint *
326
_DEFUN (mult, (ptr, a, b), struct _reent * ptr _AND _Bigint * a _AND _Bigint * b)
327
{
328
  _Bigint *c;
329
  int k, wa, wb, wc;
330
  __ULong carry, y, z;
331
  __ULong *x, *xa, *xae, *xb, *xbe, *xc, *xc0;
332
#ifdef Pack_32
333
  __ULong z2;
334
#endif
335
 
336
  if (a->_wds < b->_wds)
337
    {
338
      c = a;
339
      a = b;
340
      b = c;
341
    }
342
  k = a->_k;
343
  wa = a->_wds;
344
  wb = b->_wds;
345
  wc = wa + wb;
346
  if (wc > a->_maxwds)
347
    k++;
348
  c = Balloc (ptr, k);
349
  for (x = c->_x, xa = x + wc; x < xa; x++)
350
    *x = 0;
351
  xa = a->_x;
352
  xae = xa + wa;
353
  xb = b->_x;
354
  xbe = xb + wb;
355
  xc0 = c->_x;
356
#ifdef Pack_32
357
  for (; xb < xbe; xb++, xc0++)
358
    {
359
      if ((y = *xb & 0xffff) != 0)
360
        {
361
          x = xa;
362
          xc = xc0;
363
          carry = 0;
364
          do
365
            {
366
              z = (*x & 0xffff) * y + (*xc & 0xffff) + carry;
367
              carry = z >> 16;
368
              z2 = (*x++ >> 16) * y + (*xc >> 16) + carry;
369
              carry = z2 >> 16;
370
              Storeinc (xc, z2, z);
371
            }
372
          while (x < xae);
373
          *xc = carry;
374
        }
375
      if ((y = *xb >> 16) != 0)
376
        {
377
          x = xa;
378
          xc = xc0;
379
          carry = 0;
380
          z2 = *xc;
381
          do
382
            {
383
              z = (*x & 0xffff) * y + (*xc >> 16) + carry;
384
              carry = z >> 16;
385
              Storeinc (xc, z, z2);
386
              z2 = (*x++ >> 16) * y + (*xc & 0xffff) + carry;
387
              carry = z2 >> 16;
388
            }
389
          while (x < xae);
390
          *xc = z2;
391
        }
392
    }
393
#else
394
  for (; xb < xbe; xc0++)
395
    {
396
      if (y = *xb++)
397
        {
398
          x = xa;
399
          xc = xc0;
400
          carry = 0;
401
          do
402
            {
403
              z = *x++ * y + *xc + carry;
404
              carry = z >> 16;
405
              *xc++ = z & 0xffff;
406
            }
407
          while (x < xae);
408
          *xc = carry;
409
        }
410
    }
411
#endif
412
  for (xc0 = c->_x, xc = xc0 + wc; wc > 0 && !*--xc; --wc);
413
  c->_wds = wc;
414
  return c;
415
}
416
 
417
_Bigint *
418
_DEFUN (pow5mult,
419
        (ptr, b, k), struct _reent * ptr _AND _Bigint * b _AND int k)
420
{
421
  _Bigint *b1, *p5, *p51;
422
  int i;
423
  static _CONST int p05[3] = {5, 25, 125};
424
 
425
  if ((i = k & 3) != 0)
426
    b = multadd (ptr, b, p05[i - 1], 0);
427
 
428
  if (!(k >>= 2))
429
    return b;
430
  _REENT_CHECK_MP(ptr);
431
  if (!(p5 = _REENT_MP_P5S(ptr)))
432
    {
433
      /* first time */
434
      p5 = _REENT_MP_P5S(ptr) = i2b (ptr, 625);
435
      p5->_next = 0;
436
    }
437
  for (;;)
438
    {
439
      if (k & 1)
440
        {
441
          b1 = mult (ptr, b, p5);
442
          Bfree (ptr, b);
443
          b = b1;
444
        }
445
      if (!(k >>= 1))
446
        break;
447
      if (!(p51 = p5->_next))
448
        {
449
          p51 = p5->_next = mult (ptr, p5, p5);
450
          p51->_next = 0;
451
        }
452
      p5 = p51;
453
    }
454
  return b;
455
}
456
 
457
_Bigint *
458
_DEFUN (lshift, (ptr, b, k), struct _reent * ptr _AND _Bigint * b _AND int k)
459
{
460
  int i, k1, n, n1;
461
  _Bigint *b1;
462
  __ULong *x, *x1, *xe, z;
463
 
464
#ifdef Pack_32
465
  n = k >> 5;
466
#else
467
  n = k >> 4;
468
#endif
469
  k1 = b->_k;
470
  n1 = n + b->_wds + 1;
471
  for (i = b->_maxwds; n1 > i; i <<= 1)
472
    k1++;
473
  b1 = Balloc (ptr, k1);
474
  x1 = b1->_x;
475
  for (i = 0; i < n; i++)
476
    *x1++ = 0;
477
  x = b->_x;
478
  xe = x + b->_wds;
479
#ifdef Pack_32
480
  if (k &= 0x1f)
481
    {
482
      k1 = 32 - k;
483
      z = 0;
484
      do
485
        {
486
          *x1++ = *x << k | z;
487
          z = *x++ >> k1;
488
        }
489
      while (x < xe);
490
      if ((*x1 = z) != 0)
491
        ++n1;
492
    }
493
#else
494
  if (k &= 0xf)
495
    {
496
      k1 = 16 - k;
497
      z = 0;
498
      do
499
        {
500
          *x1++ = *x << k & 0xffff | z;
501
          z = *x++ >> k1;
502
        }
503
      while (x < xe);
504
      if (*x1 = z)
505
        ++n1;
506
    }
507
#endif
508
  else
509
    do
510
      *x1++ = *x++;
511
    while (x < xe);
512
  b1->_wds = n1 - 1;
513
  Bfree (ptr, b);
514
  return b1;
515
}
516
 
517
int
518
_DEFUN (cmp, (a, b), _Bigint * a _AND _Bigint * b)
519
{
520
  __ULong *xa, *xa0, *xb, *xb0;
521
  int i, j;
522
 
523
  i = a->_wds;
524
  j = b->_wds;
525
#ifdef DEBUG
526
  if (i > 1 && !a->_x[i - 1])
527
    Bug ("cmp called with a->_x[a->_wds-1] == 0");
528
  if (j > 1 && !b->_x[j - 1])
529
    Bug ("cmp called with b->_x[b->_wds-1] == 0");
530
#endif
531
  if (i -= j)
532
    return i;
533
  xa0 = a->_x;
534
  xa = xa0 + j;
535
  xb0 = b->_x;
536
  xb = xb0 + j;
537
  for (;;)
538
    {
539
      if (*--xa != *--xb)
540
        return *xa < *xb ? -1 : 1;
541
      if (xa <= xa0)
542
        break;
543
    }
544
  return 0;
545
}
546
 
547
_Bigint *
548
_DEFUN (diff, (ptr, a, b), struct _reent * ptr _AND
549
        _Bigint * a _AND _Bigint * b)
550
{
551
  _Bigint *c;
552
  int i, wa, wb;
553
  __Long borrow, y;             /* We need signed shifts here. */
554
  __ULong *xa, *xae, *xb, *xbe, *xc;
555
#ifdef Pack_32
556
  __Long z;
557
#endif
558
 
559
  i = cmp (a, b);
560
  if (!i)
561
    {
562
      c = Balloc (ptr, 0);
563
      c->_wds = 1;
564
      c->_x[0] = 0;
565
      return c;
566
    }
567
  if (i < 0)
568
    {
569
      c = a;
570
      a = b;
571
      b = c;
572
      i = 1;
573
    }
574
  else
575
    i = 0;
576
  c = Balloc (ptr, a->_k);
577
  c->_sign = i;
578
  wa = a->_wds;
579
  xa = a->_x;
580
  xae = xa + wa;
581
  wb = b->_wds;
582
  xb = b->_x;
583
  xbe = xb + wb;
584
  xc = c->_x;
585
  borrow = 0;
586
#ifdef Pack_32
587
  do
588
    {
589
      y = (*xa & 0xffff) - (*xb & 0xffff) + borrow;
590
      borrow = y >> 16;
591
      Sign_Extend (borrow, y);
592
      z = (*xa++ >> 16) - (*xb++ >> 16) + borrow;
593
      borrow = z >> 16;
594
      Sign_Extend (borrow, z);
595
      Storeinc (xc, z, y);
596
    }
597
  while (xb < xbe);
598
  while (xa < xae)
599
    {
600
      y = (*xa & 0xffff) + borrow;
601
      borrow = y >> 16;
602
      Sign_Extend (borrow, y);
603
      z = (*xa++ >> 16) + borrow;
604
      borrow = z >> 16;
605
      Sign_Extend (borrow, z);
606
      Storeinc (xc, z, y);
607
    }
608
#else
609
  do
610
    {
611
      y = *xa++ - *xb++ + borrow;
612
      borrow = y >> 16;
613
      Sign_Extend (borrow, y);
614
      *xc++ = y & 0xffff;
615
    }
616
  while (xb < xbe);
617
  while (xa < xae)
618
    {
619
      y = *xa++ + borrow;
620
      borrow = y >> 16;
621
      Sign_Extend (borrow, y);
622
      *xc++ = y & 0xffff;
623
    }
624
#endif
625
  while (!*--xc)
626
    wa--;
627
  c->_wds = wa;
628
  return c;
629
}
630
 
631
double
632
_DEFUN (ulp, (_x), double _x)
633
{
634
  union double_union x, a;
635
  register __Long L;
636
 
637
  x.d = _x;
638
 
639
  L = (word0 (x) & Exp_mask) - (P - 1) * Exp_msk1;
640
#ifndef Sudden_Underflow
641
  if (L > 0)
642
    {
643
#endif
644
#ifdef IBM
645
      L |= Exp_msk1 >> 4;
646
#endif
647
      word0 (a) = L;
648
#ifndef _DOUBLE_IS_32BITS
649
      word1 (a) = 0;
650
#endif
651
 
652
#ifndef Sudden_Underflow
653
    }
654
  else
655
    {
656
      L = -L >> Exp_shift;
657
      if (L < Exp_shift)
658
        {
659
          word0 (a) = 0x80000 >> L;
660
#ifndef _DOUBLE_IS_32BITS
661
          word1 (a) = 0;
662
#endif
663
        }
664
      else
665
        {
666
          word0 (a) = 0;
667
          L -= Exp_shift;
668
#ifndef _DOUBLE_IS_32BITS
669
         word1 (a) = L >= 31 ? 1 : 1 << (31 - L);
670
#endif
671
        }
672
    }
673
#endif
674
  return a.d;
675
}
676
 
677
double
678
_DEFUN (b2d, (a, e),
679
        _Bigint * a _AND int *e)
680
{
681
  __ULong *xa, *xa0, w, y, z;
682
  int k;
683
  union double_union d;
684
#ifdef VAX
685
  __ULong d0, d1;
686
#else
687
#define d0 word0(d)
688
#define d1 word1(d)
689
#endif
690
 
691
  xa0 = a->_x;
692
  xa = xa0 + a->_wds;
693
  y = *--xa;
694
#ifdef DEBUG
695
  if (!y)
696
    Bug ("zero y in b2d");
697
#endif
698
  k = hi0bits (y);
699
  *e = 32 - k;
700
#ifdef Pack_32
701
  if (k < Ebits)
702
    {
703
      d0 = Exp_1 | y >> (Ebits - k);
704
      w = xa > xa0 ? *--xa : 0;
705
#ifndef _DOUBLE_IS_32BITS
706
      d1 = y << ((32 - Ebits) + k) | w >> (Ebits - k);
707
#endif
708
      goto ret_d;
709
    }
710
  z = xa > xa0 ? *--xa : 0;
711
  if (k -= Ebits)
712
    {
713
      d0 = Exp_1 | y << k | z >> (32 - k);
714
      y = xa > xa0 ? *--xa : 0;
715
#ifndef _DOUBLE_IS_32BITS
716
      d1 = z << k | y >> (32 - k);
717
#endif
718
    }
719
  else
720
    {
721
      d0 = Exp_1 | y;
722
#ifndef _DOUBLE_IS_32BITS
723
      d1 = z;
724
#endif
725
    }
726
#else
727
  if (k < Ebits + 16)
728
    {
729
      z = xa > xa0 ? *--xa : 0;
730
      d0 = Exp_1 | y << k - Ebits | z >> Ebits + 16 - k;
731
      w = xa > xa0 ? *--xa : 0;
732
      y = xa > xa0 ? *--xa : 0;
733
      d1 = z << k + 16 - Ebits | w << k - Ebits | y >> 16 + Ebits - k;
734
      goto ret_d;
735
    }
736
  z = xa > xa0 ? *--xa : 0;
737
  w = xa > xa0 ? *--xa : 0;
738
  k -= Ebits + 16;
739
  d0 = Exp_1 | y << k + 16 | z << k | w >> 16 - k;
740
  y = xa > xa0 ? *--xa : 0;
741
  d1 = w << k + 16 | y << k;
742
#endif
743
ret_d:
744
#ifdef VAX
745
  word0 (d) = d0 >> 16 | d0 << 16;
746
  word1 (d) = d1 >> 16 | d1 << 16;
747
#else
748
#undef d0
749
#undef d1
750
#endif
751
  return d.d;
752
}
753
 
754
_Bigint *
755
_DEFUN (d2b,
756
        (ptr, _d, e, bits),
757
        struct _reent * ptr _AND
758
        double _d _AND
759
        int *e _AND
760
        int *bits)
761
 
762
{
763
  union double_union d;
764
  _Bigint *b;
765
  int de, i, k;
766
  __ULong *x, y, z;
767
#ifdef VAX
768
  __ULong d0, d1;
769
#endif
770
  d.d = _d;
771
#ifdef VAX
772
  d0 = word0 (d) >> 16 | word0 (d) << 16;
773
  d1 = word1 (d) >> 16 | word1 (d) << 16;
774
#else
775
#define d0 word0(d)
776
#define d1 word1(d)
777
  d.d = _d;
778
#endif
779
 
780
#ifdef Pack_32
781
  b = Balloc (ptr, 1);
782
#else
783
  b = Balloc (ptr, 2);
784
#endif
785
  x = b->_x;
786
 
787
  z = d0 & Frac_mask;
788
  d0 &= 0x7fffffff;             /* clear sign bit, which we ignore */
789
#ifdef Sudden_Underflow
790
  de = (int) (d0 >> Exp_shift);
791
#ifndef IBM
792
  z |= Exp_msk11;
793
#endif
794
#else
795
  if ((de = (int) (d0 >> Exp_shift)) != 0)
796
    z |= Exp_msk1;
797
#endif
798
#ifdef Pack_32
799
#ifndef _DOUBLE_IS_32BITS
800
  if (d1)
801
    {
802
      y = d1;
803
      k = lo0bits (&y);
804
      if (k)
805
        {
806
         x[0] = y | z << (32 - k);
807
          z >>= k;
808
        }
809
      else
810
        x[0] = y;
811
      i = b->_wds = (x[1] = z) ? 2 : 1;
812
    }
813
  else
814
#endif
815
    {
816
#ifdef DEBUG
817
      if (!z)
818
        Bug ("Zero passed to d2b");
819
#endif
820
      k = lo0bits (&z);
821
      x[0] = z;
822
      i = b->_wds = 1;
823
#ifndef _DOUBLE_IS_32BITS
824
      k += 32;
825
#endif
826
    }
827
#else
828
  if (d1)
829
    {
830
      y = d1;
831
      k = lo0bits (&y);
832
      if (k)
833
        if (k >= 16)
834
          {
835
            x[0] = y | z << 32 - k & 0xffff;
836
            x[1] = z >> k - 16 & 0xffff;
837
            x[2] = z >> k;
838
            i = 2;
839
          }
840
        else
841
          {
842
            x[0] = y & 0xffff;
843
            x[1] = y >> 16 | z << 16 - k & 0xffff;
844
            x[2] = z >> k & 0xffff;
845
            x[3] = z >> k + 16;
846
            i = 3;
847
          }
848
      else
849
        {
850
          x[0] = y & 0xffff;
851
          x[1] = y >> 16;
852
          x[2] = z & 0xffff;
853
          x[3] = z >> 16;
854
          i = 3;
855
        }
856
    }
857
  else
858
    {
859
#ifdef DEBUG
860
      if (!z)
861
        Bug ("Zero passed to d2b");
862
#endif
863
      k = lo0bits (&z);
864
      if (k >= 16)
865
        {
866
          x[0] = z;
867
          i = 0;
868
        }
869
      else
870
        {
871
          x[0] = z & 0xffff;
872
          x[1] = z >> 16;
873
          i = 1;
874
        }
875
      k += 32;
876
    }
877
  while (!x[i])
878
    --i;
879
  b->_wds = i + 1;
880
#endif
881
#ifndef Sudden_Underflow
882
  if (de)
883
    {
884
#endif
885
#ifdef IBM
886
      *e = (de - Bias - (P - 1) << 2) + k;
887
      *bits = 4 * P + 8 - k - hi0bits (word0 (d) & Frac_mask);
888
#else
889
      *e = de - Bias - (P - 1) + k;
890
      *bits = P - k;
891
#endif
892
#ifndef Sudden_Underflow
893
    }
894
  else
895
    {
896
      *e = de - Bias - (P - 1) + 1 + k;
897
#ifdef Pack_32
898
      *bits = 32 * i - hi0bits (x[i - 1]);
899
#else
900
      *bits = (i + 2) * 16 - hi0bits (x[i]);
901
#endif
902
    }
903
#endif
904
  return b;
905
}
906
#undef d0
907
#undef d1
908
 
909
double
910
_DEFUN (ratio, (a, b), _Bigint * a _AND _Bigint * b)
911
 
912
{
913
  union double_union da, db;
914
  int k, ka, kb;
915
 
916
  da.d = b2d (a, &ka);
917
  db.d = b2d (b, &kb);
918
#ifdef Pack_32
919
  k = ka - kb + 32 * (a->_wds - b->_wds);
920
#else
921
  k = ka - kb + 16 * (a->_wds - b->_wds);
922
#endif
923
#ifdef IBM
924
  if (k > 0)
925
    {
926
      word0 (da) += (k >> 2) * Exp_msk1;
927
      if (k &= 3)
928
        da.d *= 1 << k;
929
    }
930
  else
931
    {
932
      k = -k;
933
      word0 (db) += (k >> 2) * Exp_msk1;
934
      if (k &= 3)
935
        db.d *= 1 << k;
936
    }
937
#else
938
  if (k > 0)
939
    word0 (da) += k * Exp_msk1;
940
  else
941
    {
942
      k = -k;
943
      word0 (db) += k * Exp_msk1;
944
    }
945
#endif
946
  return da.d / db.d;
947
}
948
 
949
 
950
_CONST double
951
  tens[] =
952
{
953
  1e0, 1e1, 1e2, 1e3, 1e4, 1e5, 1e6, 1e7, 1e8, 1e9,
954
  1e10, 1e11, 1e12, 1e13, 1e14, 1e15, 1e16, 1e17, 1e18, 1e19,
955
  1e20, 1e21, 1e22, 1e23, 1e24
956
 
957
};
958
 
959
#if !defined(_DOUBLE_IS_32BITS) && !defined(__v800)
960
_CONST double bigtens[] =
961
{1e16, 1e32, 1e64, 1e128, 1e256};
962
 
963
_CONST double tinytens[] =
964
{1e-16, 1e-32, 1e-64, 1e-128, 1e-256};
965
#else
966
_CONST double bigtens[] =
967
{1e16, 1e32};
968
 
969
_CONST double tinytens[] =
970
{1e-16, 1e-32};
971
#endif
972
 
973
 
974
double
975
_DEFUN (_mprec_log10, (dig),
976
        int dig)
977
{
978
  double v = 1.0;
979
  if (dig < 24)
980
    return tens[dig];
981
  while (dig > 0)
982
    {
983
      v *= 10;
984
      dig--;
985
    }
986
  return v;
987
}
988
 
989
void
990
_DEFUN (copybits, (c, n, b),
991
        __ULong *c _AND
992
        int n _AND
993
        _Bigint *b)
994
{
995
        __ULong *ce, *x, *xe;
996
#ifdef Pack_16
997
        int nw, nw1;
998
#endif
999
 
1000
        ce = c + ((n-1) >> kshift) + 1;
1001
        x = b->_x;
1002
#ifdef Pack_32
1003
        xe = x + b->_wds;
1004
        while(x < xe)
1005
                *c++ = *x++;
1006
#else
1007
        nw = b->_wds;
1008
        nw1 = nw & 1;
1009
        for(xe = x + (nw - nw1); x < xe; x += 2)
1010
                Storeinc(c, x[1], x[0]);
1011
        if (nw1)
1012
                *c++ = *x;
1013
#endif
1014
        while(c < ce)
1015
                *c++ = 0;
1016
}
1017
 
1018
__ULong
1019
_DEFUN (any_on, (b, k),
1020
        _Bigint *b _AND
1021
        int k)
1022
{
1023
        int n, nwds;
1024
        __ULong *x, *x0, x1, x2;
1025
 
1026
        x = b->_x;
1027
        nwds = b->_wds;
1028
        n = k >> kshift;
1029
        if (n > nwds)
1030
                n = nwds;
1031
        else if (n < nwds && (k &= kmask)) {
1032
                x1 = x2 = x[n];
1033
                x1 >>= k;
1034
                x1 <<= k;
1035
                if (x1 != x2)
1036
                        return 1;
1037
                }
1038
        x0 = x;
1039
        x += n;
1040
        while(x > x0)
1041
                if (*--x)
1042
                        return 1;
1043
        return 0;
1044
}
1045
 

powered by: WebSVN 2.1.0

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