Embedded Artistry libc
C Standard Library Support for Bare-metal Systems
misc.c File Reference
#include "gdtoaimp.h"
Include dependency graph for misc.c:

Go to the source code of this file.

Macros

#define PRIVATE_MEM   2304
 
#define PRIVATE_mem   ((PRIVATE_MEM + sizeof(double) - 1) / sizeof(double))
 
#define d0   word0(d)
 
#define d1   word1(d)
 
#define d0   word0(d)
 
#define d1   word1(d)
 

Functions

BigintBalloc (int k)
 
void Bfree (Bigint *v)
 
int lo0bits (ULong *y)
 
Bigintmultadd (Bigint *b, int m, int a)
 
int hi0bits_D2A (register ULong x)
 
Biginti2b (int i)
 
Bigintmult (Bigint *a, Bigint *b)
 
Bigintpow5mult (Bigint *b, int k)
 
Bigintlshift (Bigint *b, int k)
 
int cmp (Bigint *a, Bigint *b)
 
Bigintdiff (Bigint *a, Bigint *b)
 
double b2d (Bigint *a, int *e)
 
Bigintd2b (double d, int *e, int *bits)
 
char * strcp_D2A (char *a, CONST char *b)
 

Variables

static Bigintfreelist [Kmax+1]
 
static double private_mem [PRIVATE_mem]
 
static double * pmem_next = private_mem
 
static Bigintp5s
 
CONST double bigtens [] = {1e16, 1e32}
 
CONST double tinytens [] = {1e-16, 1e-32}
 
CONST double tens []
 

Macro Definition Documentation

◆ d0 [1/2]

#define d0   word0(d)

◆ d0 [2/2]

#define d0   word0(d)

◆ d1 [1/2]

#define d1   word1(d)

◆ d1 [2/2]

#define d1   word1(d)

◆ PRIVATE_MEM

#define PRIVATE_MEM   2304

Definition at line 37 of file misc.c.

◆ PRIVATE_mem

#define PRIVATE_mem   ((PRIVATE_MEM + sizeof(double) - 1) / sizeof(double))

Definition at line 39 of file misc.c.

Function Documentation

◆ b2d()

double b2d ( Bigint a,
int *  e 
)

Definition at line 724 of file misc.c.

726 {
727  ULong *xa, *xa0, w, y, z;
728  int k;
729  double d;
730 #ifdef VAX
731  ULong d0, d1;
732 #else
733 #define d0 word0(d)
734 #define d1 word1(d)
735 #endif
736 
737  xa0 = a->x;
738  xa = xa0 + a->wds;
739  y = *--xa;
740 #ifdef DEBUG
741  if(!y)
742  Bug("zero y in b2d");
743 #endif
744  k = hi0bits(y);
745  *e = 32 - k;
746 #ifdef Pack_32
747  if(k < Ebits)
748  {
749  d0 = Exp_1 | y >> (Ebits - k);
750  w = xa > xa0 ? *--xa : 0;
751  d1 = y << ((32 - Ebits) + k) | w >> (Ebits - k);
752  goto ret_d;
753  }
754  z = xa > xa0 ? *--xa : 0;
755  if(k -= Ebits)
756  {
757  d0 = Exp_1 | y << k | z >> (32 - k);
758  y = xa > xa0 ? *--xa : 0;
759  d1 = z << k | y >> (32 - k);
760  }
761  else
762  {
763  d0 = Exp_1 | y;
764  d1 = z;
765  }
766 #else
767  if(k < Ebits + 16)
768  {
769  z = xa > xa0 ? *--xa : 0;
770  d0 = Exp_1 | y << k - Ebits | z >> Ebits + 16 - k;
771  w = xa > xa0 ? *--xa : 0;
772  y = xa > xa0 ? *--xa : 0;
773  d1 = z << k + 16 - Ebits | w << k - Ebits | y >> 16 + Ebits - k;
774  goto ret_d;
775  }
776  z = xa > xa0 ? *--xa : 0;
777  w = xa > xa0 ? *--xa : 0;
778  k -= Ebits + 16;
779  d0 = Exp_1 | y << k + 16 | z << k | w >> 16 - k;
780  y = xa > xa0 ? *--xa : 0;
781  d1 = w << k + 16 | y << k;
782 #endif
783 ret_d:
784 #ifdef VAX
785  word0(d) = d0 >> 16 | d0 << 16;
786  word1(d) = d1 >> 16 | d1 << 16;
787 #endif
788  return dval(d);
789 }
ULong x[1]
Definition: gdtoaimp.h:488
int wds
Definition: gdtoaimp.h:487
#define word1(x)
Definition: gdtoaimp.h:305
#define dval(x)
Definition: gdtoaimp.h:307
#define word0(x)
Definition: gdtoaimp.h:304
#define hi0bits(x)
Definition: gdtoaimp.h:525
#define Exp_1
Definition: gdtoaimp.h:401
#define Ebits
Definition: gdtoaimp.h:403
unsigned Long ULong
Definition: gdtoa.h:41
#define d0
#define d1

References d0, d1, dval, Ebits, Exp_1, hi0bits, Bigint::wds, word0, word1, and Bigint::x.

Referenced by gdtoa(), and ratio().

◆ Balloc()

Bigint* Balloc ( int  k)

Definition at line 47 of file misc.c.

49 {
50  Bigint* rv;
51 
53  if((rv = freelist[k]) != 0)
54  {
55  freelist[k] = rv->next;
56  }
57  else
58  {
59  int x = 1 << k;
60 #ifdef Omit_Private_Memory
61  rv = (Bigint*)MALLOC(sizeof(Bigint) + (x - 1) * sizeof(ULong));
62 #else
63 #ifndef Omit_Private_Memory
64  unsigned int len;
65 #endif
66  len = (sizeof(Bigint) + ((unsigned)x - 1) * sizeof(ULong) + sizeof(double) - 1) /
67  sizeof(double);
68  if((unsigned)(pmem_next - private_mem + len) <= PRIVATE_mem)
69  {
70  rv = (Bigint*)pmem_next;
71  pmem_next += len;
72  }
73  else
74  {
75  {
76  rv = (Bigint*)MALLOC(len * sizeof(double));
77  }
78  }
79 #endif
80  rv->k = k;
81  rv->maxwds = x;
82  }
83  FREE_DTOA_LOCK(0);
84  rv->sign = rv->wds = 0;
85  return rv;
86 }
#define FREE_DTOA_LOCK(n)
Definition: gdtoaimp.h:479
int wds
Definition: gdtoaimp.h:487
#define PRIVATE_mem
Definition: misc.c:39
struct Bigint Bigint
Definition: gdtoaimp.h:491
int sign
Definition: gdtoaimp.h:487
int maxwds
Definition: gdtoaimp.h:487
static double * pmem_next
Definition: misc.c:40
unsigned Long ULong
Definition: gdtoa.h:41
#define MALLOC
Definition: gdtoaimp.h:206
#define ACQUIRE_DTOA_LOCK(n)
Definition: gdtoaimp.h:478
static Bigint * freelist[Kmax+1]
Definition: misc.c:34
static double private_mem[PRIVATE_mem]
Definition: misc.c:40
int k
Definition: gdtoaimp.h:487
struct Bigint * next
Definition: gdtoaimp.h:486

References ACQUIRE_DTOA_LOCK, FREE_DTOA_LOCK, freelist, Bigint::k, MALLOC, Bigint::maxwds, Bigint::next, pmem_next, PRIVATE_mem, private_mem, Bigint::sign, and Bigint::wds.

Referenced by bitstob(), d2b(), diff(), dtoa(), gdtoa(), gethex(), i2b(), increment(), lshift(), mult(), multadd(), rv_alloc(), s2b(), set_ones(), strtod(), strtodg(), strtoId(), strtoIdd(), strtoIf(), strtoIg(), strtoIQ(), strtoIx(), strtoIxL(), and sum().

◆ Bfree()

void Bfree ( Bigint v)

Definition at line 92 of file misc.c.

94 {
95  if(v)
96  {
98  v->next = freelist[v->k];
99  freelist[v->k] = v;
100  FREE_DTOA_LOCK(0);
101  }
102 }
#define FREE_DTOA_LOCK(n)
Definition: gdtoaimp.h:479
#define ACQUIRE_DTOA_LOCK(n)
Definition: gdtoaimp.h:478
static Bigint * freelist[Kmax+1]
Definition: misc.c:34
int k
Definition: gdtoaimp.h:487
struct Bigint * next
Definition: gdtoaimp.h:486

References ACQUIRE_DTOA_LOCK, FREE_DTOA_LOCK, and freelist.

Referenced by dtoa(), freedtoa(), g_ddfmt(), gdtoa(), gethex(), increment(), lshift(), multadd(), pow5mult(), rvOK(), set_ones(), strtod(), strtodg(), strtoId(), strtoIdd(), strtoIf(), strtoIQ(), strtoIx(), strtoIxL(), and sum().

◆ cmp()

int cmp ( Bigint a,
Bigint b 
)

Definition at line 570 of file misc.c.

572 {
573  ULong *xa, *xa0, *xb, *xb0;
574  int i, j;
575 
576  i = a->wds;
577  j = b->wds;
578 #ifdef DEBUG
579  if(i > 1 && !a->x[i - 1])
580  Bug("cmp called with a->x[a->wds-1] == 0");
581  if(j > 1 && !b->x[j - 1])
582  Bug("cmp called with b->x[b->wds-1] == 0");
583 #endif
584  if(i -= j)
585  {
586  {
587  return i;
588  }
589  }
590  xa0 = a->x;
591  xa = xa0 + j;
592  xb0 = b->x;
593  xb = xb0 + j;
594  for(;;)
595  {
596  if(*--xa != *--xb)
597  {
598  {
599  return *xa < *xb ? -1 : 1;
600  }
601  }
602  if(xa <= xa0)
603  {
604  {
605  break;
606  }
607  }
608  }
609  return 0;
610 }
ULong x[1]
Definition: gdtoaimp.h:488
int wds
Definition: gdtoaimp.h:487
unsigned Long ULong
Definition: gdtoa.h:41

References Bigint::wds, and Bigint::x.

Referenced by _qsort(), bsearch(), diff(), dtoa(), gdtoa(), qsort(), quorem(), strtod(), and strtodg().

◆ d2b()

Bigint* d2b ( double  d,
int *  e,
int *  bits 
)

Definition at line 798 of file misc.c.

800 {
801  Bigint* b;
802 #ifndef Sudden_Underflow
803  int i;
804 #endif
805  int de, k;
806  ULong *x, y, z;
807 #ifdef VAX
808  ULong d0, d1;
809  d0 = word0(d) >> 16 | word0(d) << 16;
810  d1 = word1(d) >> 16 | word1(d) << 16;
811 #else
812 #define d0 word0(d)
813 #define d1 word1(d)
814 #endif
815 
816 #ifdef Pack_32
817  b = Balloc(1);
818 #else
819  b = Balloc(2);
820 #endif
821  x = b->x;
822 
823  z = d0 & Frac_mask;
824  d0 &= 0x7fffffff; /* clear sign bit, which we ignore */
825 #ifdef Sudden_Underflow
826  de = (int)(d0 >> Exp_shift);
827 #ifndef IBM
828  z |= Exp_msk11;
829 #endif
830 #else
831  if((de = (int)(d0 >> Exp_shift)) != 0)
832  {
833  {
834  z |= Exp_msk1;
835  }
836  }
837 #endif
838 #ifdef Pack_32
839  if((y = d1) != 0)
840  {
841  if((k = lo0bits(&y)) != 0)
842  {
843  x[0] = y | z << (32 - k);
844  z >>= k;
845  }
846  else
847  {
848  {
849  x[0] = y;
850  }
851  }
852 #ifndef Sudden_Underflow
853  i =
854 #endif
855  b->wds = (x[1] = z) != 0 ? 2 : 1;
856  }
857  else
858  {
859  k = lo0bits(&z);
860  x[0] = z;
861 #ifndef Sudden_Underflow
862  i =
863 #endif
864  b->wds = 1;
865  k += 32;
866  }
867 #else
868  if((y = d1) != 0)
869  {
870  if((k = lo0bits(&y)) != 0)
871  if(k >= 16)
872  {
873  x[0] = y | z << 32 - k & 0xffff;
874  x[1] = z >> k - 16 & 0xffff;
875  x[2] = z >> k;
876  i = 2;
877  }
878  else
879  {
880  x[0] = y & 0xffff;
881  x[1] = y >> 16 | z << 16 - k & 0xffff;
882  x[2] = z >> k & 0xffff;
883  x[3] = z >> k + 16;
884  i = 3;
885  }
886  else
887  {
888  x[0] = y & 0xffff;
889  x[1] = y >> 16;
890  x[2] = z & 0xffff;
891  x[3] = z >> 16;
892  i = 3;
893  }
894  }
895  else
896  {
897 #ifdef DEBUG
898  if(!z)
899  Bug("Zero passed to d2b");
900 #endif
901  k = lo0bits(&z);
902  if(k >= 16)
903  {
904  x[0] = z;
905  i = 0;
906  }
907  else
908  {
909  x[0] = z & 0xffff;
910  x[1] = z >> 16;
911  i = 1;
912  }
913  k += 32;
914  }
915  while(!x[i])
916  --i;
917  b->wds = i + 1;
918 #endif
919 #ifndef Sudden_Underflow
920  if(de)
921  {
922 #endif
923 #ifdef IBM
924  *e = (de - Bias - (P - 1) << 2) + k;
925  *bits = 4 * P + 8 - k - hi0bits(word0(d) & Frac_mask);
926 #else
927  *e = de - Bias - (P - 1) + k;
928  *bits = P - k;
929 #endif
930 #ifndef Sudden_Underflow
931  }
932  else
933  {
934  *e = de - Bias - (P - 1) + 1 + k;
935 #ifdef Pack_32
936  *bits = 32 * i - hi0bits(x[i - 1]);
937 #else
938  *bits = (i + 2) * 16 - hi0bits(x[i]);
939 #endif
940  }
941 #endif
942  return b;
943 }
#define P
Definition: gdtoaimp.h:399
ULong x[1]
Definition: gdtoaimp.h:488
int wds
Definition: gdtoaimp.h:487
#define Bias
Definition: gdtoaimp.h:400
#define Exp_msk1
Definition: gdtoaimp.h:396
#define word1(x)
Definition: gdtoaimp.h:305
int lo0bits(ULong *y)
Definition: misc.c:108
#define word0(x)
Definition: gdtoaimp.h:304
#define hi0bits(x)
Definition: gdtoaimp.h:525
unsigned Long ULong
Definition: gdtoa.h:41
#define Exp_msk11
Definition: gdtoaimp.h:397
Bigint * Balloc(int k)
Definition: misc.c:47
#define Frac_mask
Definition: gdtoaimp.h:404
#define Exp_shift
Definition: gdtoaimp.h:394
#define d0
#define d1

References Balloc(), Bias, d0, d1, Exp_msk1, Exp_msk11, Exp_shift, Frac_mask, hi0bits, lo0bits(), P, Bigint::wds, word0, word1, and Bigint::x.

Referenced by dtoa(), g_ddfmt(), rvOK(), strtod(), and strtodg().

◆ diff()

Bigint* diff ( Bigint a,
Bigint b 
)

Definition at line 617 of file misc.c.

619 {
620  Bigint* c;
621  int i, wa, wb;
622  ULong *xa, *xae, *xb, *xbe, *xc;
623 #ifdef ULLong
624  ULLong borrow, y;
625 #else
626  ULong borrow, y;
627 #ifdef Pack_32
628  ULong z;
629 #endif
630 #endif
631 
632  i = cmp(a, b);
633  if(!i)
634  {
635  c = Balloc(0);
636  c->wds = 1;
637  c->x[0] = 0;
638  return c;
639  }
640  if(i < 0)
641  {
642  c = a;
643  a = b;
644  b = c;
645  i = 1;
646  }
647  else
648  {
649  {
650  i = 0;
651  }
652  }
653  c = Balloc(a->k);
654  c->sign = i;
655  wa = a->wds;
656  xa = a->x;
657  xae = xa + wa;
658  wb = b->wds;
659  xb = b->x;
660  xbe = xb + wb;
661  xc = c->x;
662  borrow = 0;
663 #ifdef ULLong
664  do
665  {
666  y = (ULLong)*xa++ - *xb++ - borrow;
667  borrow = y >> 32 & 1UL;
668  *xc++ = y & 0xffffffffUL;
669  } while(xb < xbe);
670  while(xa < xae)
671  {
672  y = *xa++ - borrow;
673  borrow = y >> 32 & 1UL;
674  *xc++ = y & 0xffffffffUL;
675  }
676 #else
677 #ifdef Pack_32
678  do
679  {
680  y = (*xa & 0xffff) - (*xb & 0xffff) - borrow;
681  borrow = (y & 0x10000) >> 16;
682  z = (*xa++ >> 16) - (*xb++ >> 16) - borrow;
683  borrow = (z & 0x10000) >> 16;
684  Storeinc(xc, z, y);
685  } while(xb < xbe);
686  while(xa < xae)
687  {
688  y = (*xa & 0xffff) - borrow;
689  borrow = (y & 0x10000) >> 16;
690  z = (*xa++ >> 16) - borrow;
691  borrow = (z & 0x10000) >> 16;
692  Storeinc(xc, z, y);
693  }
694 #else
695  do
696  {
697  y = *xa++ - *xb++ - borrow;
698  borrow = (y & 0x10000) >> 16;
699  *xc++ = y & 0xffff;
700  } while(xb < xbe);
701  while(xa < xae)
702  {
703  y = *xa++ - borrow;
704  borrow = (y & 0x10000) >> 16;
705  *xc++ = y & 0xffff;
706  }
707 #endif
708 #endif
709  while(!*--xc)
710  {
711  {
712  wa--;
713  }
714  }
715  c->wds = wa;
716  return c;
717 }
ULong x[1]
Definition: gdtoaimp.h:488
int wds
Definition: gdtoaimp.h:487
#define UL
Definition: qnan.c:66
#define ULLong
Definition: gdtoaimp.h:461
int sign
Definition: gdtoaimp.h:487
#define Storeinc(a, b, c)
Definition: gdtoaimp.h:318
unsigned Long ULong
Definition: gdtoa.h:41
int cmp(Bigint *a, Bigint *b)
Definition: misc.c:570
Bigint * Balloc(int k)
Definition: misc.c:47
int k
Definition: gdtoaimp.h:487

References Balloc(), cmp(), Bigint::k, Bigint::sign, Storeinc, UL, ULLong, Bigint::wds, and Bigint::x.

Referenced by dtoa(), g_ddfmt(), gdtoa(), strtod(), and strtodg().

◆ hi0bits_D2A()

int hi0bits_D2A ( register ULong  x)

Definition at line 229 of file misc.c.

231 {
232  register int k = 0;
233 
234  if(!(x & 0xffff0000))
235  {
236  k = 16;
237  x <<= 16;
238  }
239  if(!(x & 0xff000000))
240  {
241  k += 8;
242  x <<= 8;
243  }
244  if(!(x & 0xf0000000))
245  {
246  k += 4;
247  x <<= 4;
248  }
249  if(!(x & 0xc0000000))
250  {
251  k += 2;
252  x <<= 2;
253  }
254  if(!(x & 0x80000000))
255  {
256  k++;
257  if(!(x & 0x40000000))
258  {
259  {
260  return 32;
261  }
262  }
263  }
264  return k;
265 }

◆ i2b()

Bigint* i2b ( int  i)

Definition at line 271 of file misc.c.

273 {
274  Bigint* b;
275 
276  b = Balloc(1);
277  b->x[0] = (ULong)i;
278  b->wds = 1;
279  return b;
280 }
ULong x[1]
Definition: gdtoaimp.h:488
int wds
Definition: gdtoaimp.h:487
unsigned Long ULong
Definition: gdtoa.h:41
Bigint * Balloc(int k)
Definition: misc.c:47

References Balloc(), Bigint::wds, and Bigint::x.

Referenced by dtoa(), gdtoa(), pow5mult(), strtod(), and strtodg().

◆ lo0bits()

int lo0bits ( ULong y)

Definition at line 108 of file misc.c.

110 {
111  register int k;
112  register ULong x = *y;
113 
114  if(x & 7)
115  {
116  if(x & 1)
117  {
118  {
119  return 0;
120  }
121  }
122  if(x & 2)
123  {
124  *y = x >> 1;
125  return 1;
126  }
127  *y = x >> 2;
128  return 2;
129  }
130  k = 0;
131  if(!(x & 0xffff))
132  {
133  k = 16;
134  x >>= 16;
135  }
136  if(!(x & 0xff))
137  {
138  k += 8;
139  x >>= 8;
140  }
141  if(!(x & 0xf))
142  {
143  k += 4;
144  x >>= 4;
145  }
146  if(!(x & 0x3))
147  {
148  k += 2;
149  x >>= 2;
150  }
151  if(!(x & 1))
152  {
153  k++;
154  x >>= 1;
155  if(!x)
156  {
157  {
158  return 32;
159  }
160  }
161  }
162  *y = x;
163  return k;
164 }
unsigned Long ULong
Definition: gdtoa.h:41

Referenced by d2b(), g_ddfmt(), mantbits(), strtodg(), and trailz().

◆ lshift()

Bigint* lshift ( Bigint b,
int  k 
)

Definition at line 495 of file misc.c.

497 {
498  int i, k1, n, n1;
499  Bigint* b1;
500  ULong *x, *x1, *xe, z;
501 
502  n = k >> kshift;
503  k1 = b->k;
504  n1 = n + b->wds + 1;
505  for(i = b->maxwds; n1 > i; i <<= 1)
506  {
507  {
508  k1++;
509  }
510  }
511  b1 = Balloc(k1);
512  x1 = b1->x;
513  for(i = 0; i < n; i++)
514  {
515  {
516  *x1++ = 0;
517  }
518  }
519  x = b->x;
520  xe = x + b->wds;
521  if(k &= kmask)
522  {
523 #ifdef Pack_32
524  k1 = 32 - k;
525  z = 0;
526  do
527  {
528  *x1++ = *x << k | z;
529  z = *x++ >> k1;
530  } while(x < xe);
531  if((*x1 = z) != 0)
532  {
533  {
534  ++n1;
535  }
536  }
537 #else
538  k1 = 16 - k;
539  z = 0;
540  do
541  {
542  *x1++ = *x << k & 0xffff | z;
543  z = *x++ >> k1;
544  } while(x < xe);
545  if(*x1 = z)
546  ++n1;
547 #endif
548  }
549  else
550  {
551  {
552  do
553  {
554  {
555  *x1++ = *x++;
556  }
557  } while(x < xe);
558  }
559  }
560  b1->wds = n1 - 1;
561  Bfree(b);
562  return b1;
563 }
ULong x[1]
Definition: gdtoaimp.h:488
int wds
Definition: gdtoaimp.h:487
int maxwds
Definition: gdtoaimp.h:487
#define kshift
Definition: gdtoaimp.h:467
unsigned Long ULong
Definition: gdtoa.h:41
void Bfree(Bigint *v)
Definition: misc.c:92
#define kmask
Definition: gdtoaimp.h:468
Bigint * Balloc(int k)
Definition: misc.c:47
int k
Definition: gdtoaimp.h:487

References Balloc(), Bfree(), Bigint::k, kmask, kshift, Bigint::maxwds, Bigint::wds, and Bigint::x.

Referenced by dtoa(), g_ddfmt(), gdtoa(), gethex(), rvOK(), strtod(), strtodg(), and strtoIg().

◆ mult()

Bigint* mult ( Bigint a,
Bigint b 
)

Definition at line 287 of file misc.c.

289 {
290  Bigint* c;
291  int k, wa, wb, wc;
292  ULong *x, *xa, *xae, *xb, *xbe, *xc, *xc0;
293  ULong y;
294 #ifdef ULLong
295  ULLong carry, z;
296 #else
297  ULong carry, z;
298 #ifdef Pack_32
299  ULong z2;
300 #endif
301 #endif
302 
303  if(a->wds < b->wds)
304  {
305  c = a;
306  a = b;
307  b = c;
308  }
309  k = a->k;
310  wa = a->wds;
311  wb = b->wds;
312  wc = wa + wb;
313  if(wc > a->maxwds)
314  {
315  {
316  k++;
317  }
318  }
319  c = Balloc(k);
320  for(x = c->x, xa = x + wc; x < xa; x++)
321  {
322  {
323  *x = 0;
324  }
325  }
326  xa = a->x;
327  xae = xa + wa;
328  xb = b->x;
329  xbe = xb + wb;
330  xc0 = c->x;
331 #ifdef ULLong
332  for(; xb < xbe; xc0++)
333  {
334  if((y = *xb++) != 0)
335  {
336  x = xa;
337  xc = xc0;
338  carry = 0;
339  do
340  {
341  z = *x++ * (ULLong)y + *xc + carry;
342  carry = z >> 32;
343  *xc++ = z & 0xffffffffUL;
344  } while(x < xae);
345  *xc = (ULong)carry;
346  }
347  }
348 #else
349 #ifdef Pack_32
350  for(; xb < xbe; xb++, xc0++)
351  {
352  if((y = *xb & 0xffff) != 0)
353  {
354  x = xa;
355  xc = xc0;
356  carry = 0;
357  do
358  {
359  z = (*x & 0xffff) * y + (*xc & 0xffff) + carry;
360  carry = z >> 16;
361  z2 = (*x++ >> 16) * y + (*xc >> 16) + carry;
362  carry = z2 >> 16;
363  Storeinc(xc, z2, z);
364  } while(x < xae);
365  *xc = carry;
366  }
367  if((y = *xb >> 16) != 0)
368  {
369  x = xa;
370  xc = xc0;
371  carry = 0;
372  z2 = *xc;
373  do
374  {
375  z = (*x & 0xffff) * y + (*xc >> 16) + carry;
376  carry = z >> 16;
377  Storeinc(xc, z, z2);
378  z2 = (*x++ >> 16) * y + (*xc & 0xffff) + carry;
379  carry = z2 >> 16;
380  } while(x < xae);
381  *xc = z2;
382  }
383  }
384 #else
385  for(; xb < xbe; xc0++)
386  {
387  if((y = *xb++) != 0)
388  {
389  x = xa;
390  xc = xc0;
391  carry = 0;
392  do
393  {
394  z = *x++ * y + *xc + carry;
395  carry = z >> 16;
396  *xc++ = z & 0xffff;
397  } while(x < xae);
398  *xc = carry;
399  }
400  }
401 #endif
402 #endif
403  for(xc0 = c->x, xc = xc0 + wc; wc > 0 && !*--xc; --wc)
404  {
405  {
406  ;
407  }
408  }
409  c->wds = wc;
410  return c;
411 }
ULong x[1]
Definition: gdtoaimp.h:488
int wds
Definition: gdtoaimp.h:487
#define UL
Definition: qnan.c:66
#define ULLong
Definition: gdtoaimp.h:461
int maxwds
Definition: gdtoaimp.h:487
#define Storeinc(a, b, c)
Definition: gdtoaimp.h:318
unsigned Long ULong
Definition: gdtoa.h:41
Bigint * Balloc(int k)
Definition: misc.c:47
int k
Definition: gdtoaimp.h:487

References Balloc(), Bigint::k, Bigint::maxwds, Storeinc, UL, ULLong, Bigint::wds, and Bigint::x.

Referenced by dtoa(), gdtoa(), pow5mult(), strtod(), and strtodg().

◆ multadd()

Bigint* multadd ( Bigint b,
int  m,
int  a 
)

Definition at line 171 of file misc.c.

173 {
174  int i, wds;
175 #ifdef ULLong
176  ULong* x;
177  ULLong carry, y;
178 #else
179  ULong carry, *x, y;
180 #ifdef Pack_32
181  ULong xi, z;
182 #endif
183 #endif
184  Bigint* b1;
185 
186  wds = b->wds;
187  x = b->x;
188  i = 0;
189  carry = (unsigned long long)a;
190  do
191  {
192 #ifdef ULLong
193  y = *x * (ULLong)m + carry;
194  carry = y >> 32;
195  *x++ = y & 0xffffffffUL;
196 #else
197 #ifdef Pack_32
198  xi = *x;
199  y = (xi & 0xffff) * m + carry;
200  z = (xi >> 16) * m + (y >> 16);
201  carry = z >> 16;
202  *x++ = (z << 16) + (y & 0xffff);
203 #else
204  y = *x * m + carry;
205  carry = y >> 16;
206  *x++ = y & 0xffff;
207 #endif
208 #endif
209  } while(++i < wds);
210  if(carry)
211  {
212  if(wds >= b->maxwds)
213  {
214  b1 = Balloc(b->k + 1);
215  Bcopy(b1, b);
216  Bfree(b);
217  b = b1;
218  }
219  b->x[wds++] = (ULong)carry;
220  b->wds = wds;
221  }
222  return b;
223 }
ULong x[1]
Definition: gdtoaimp.h:488
int wds
Definition: gdtoaimp.h:487
#define UL
Definition: qnan.c:66
#define ULLong
Definition: gdtoaimp.h:461
#define Bcopy(x, y)
Definition: gdtoaimp.h:501
int maxwds
Definition: gdtoaimp.h:487
unsigned Long ULong
Definition: gdtoa.h:41
void Bfree(Bigint *v)
Definition: misc.c:92
Bigint * Balloc(int k)
Definition: misc.c:47
int k
Definition: gdtoaimp.h:487

References Balloc(), Bcopy, Bfree(), Bigint::k, Bigint::maxwds, UL, ULLong, Bigint::wds, and Bigint::x.

Referenced by dtoa(), gdtoa(), pow5mult(), and s2b().

◆ pow5mult()

Bigint* pow5mult ( Bigint b,
int  k 
)

Definition at line 420 of file misc.c.

422 {
423  Bigint *b1, *p5, *p51;
424  int i;
425 
426  if((i = k & 3) != 0)
427  {
428  {
429  static int p05[3] = {5, 25, 125};
430  b = multadd(b, p05[i - 1], 0);
431  }
432  }
433 
434  if(!(k >>= 2))
435  {
436  {
437  return b;
438  }
439  }
440  if((p5 = p5s) == 0)
441  {
442  /* first time */
443 #ifdef MULTIPLE_THREADS
445  if(!(p5 = p5s))
446  {
447  p5 = p5s = i2b(625);
448  p5->next = 0;
449  }
450  FREE_DTOA_LOCK(1);
451 #else
452  p5 = p5s = i2b(625);
453  p5->next = 0;
454 #endif
455  }
456  for(;;)
457  {
458  if(k & 1)
459  {
460  b1 = mult(b, p5);
461  Bfree(b);
462  b = b1;
463  }
464  if(!(k >>= 1))
465  {
466  {
467  break;
468  }
469  }
470  if((p51 = p5->next) == 0)
471  {
472 #ifdef MULTIPLE_THREADS
474  if(!(p51 = p5->next))
475  {
476  p51 = p5->next = mult(p5, p5);
477  p51->next = 0;
478  }
479  FREE_DTOA_LOCK(1);
480 #else
481  p51 = p5->next = mult(p5, p5);
482  p51->next = 0;
483 #endif
484  }
485  p5 = p51;
486  }
487  return b;
488 }
#define FREE_DTOA_LOCK(n)
Definition: gdtoaimp.h:479
Bigint * i2b(int i)
Definition: misc.c:271
static Bigint * p5s
Definition: misc.c:413
void Bfree(Bigint *v)
Definition: misc.c:92
Bigint * multadd(Bigint *b, int m, int a)
Definition: misc.c:171
#define ACQUIRE_DTOA_LOCK(n)
Definition: gdtoaimp.h:478
Bigint * mult(Bigint *a, Bigint *b)
Definition: misc.c:287
struct Bigint * next
Definition: gdtoaimp.h:486

References ACQUIRE_DTOA_LOCK, Bfree(), FREE_DTOA_LOCK, i2b(), mult(), multadd(), Bigint::next, and p5s.

Referenced by dtoa(), gdtoa(), strtod(), and strtodg().

◆ strcp_D2A()

char* strcp_D2A ( char *  a,
CONST char *  b 
)

Definition at line 996 of file misc.c.

998 {
999  while((*a = *b++))
1000  {
1001  {
1002  a++;
1003  }
1004  }
1005  return a;
1006 }

Variable Documentation

◆ bigtens

CONST double bigtens[] = {1e16, 1e32}

Definition at line 956 of file misc.c.

◆ freelist

Bigint* freelist[Kmax+1]
static

Definition at line 34 of file misc.c.

Referenced by Balloc(), and Bfree().

◆ p5s

Bigint* p5s
static

Definition at line 413 of file misc.c.

Referenced by pow5mult().

◆ pmem_next

double * pmem_next = private_mem
static

Definition at line 40 of file misc.c.

Referenced by Balloc().

◆ private_mem

double private_mem[PRIVATE_mem]
static

Definition at line 40 of file misc.c.

Referenced by Balloc().

◆ tens

CONST double tens[]
Initial value:
= {1e0,
1e1,
1e2,
1e3,
1e4,
1e5,
1e6,
1e7,
1e8,
1e9,
1e10,
1e11,
1e12,
1e13,
1e14,
1e15,
1e16,
1e17,
1e18,
1e19,
1e20,
1e21,
1e22
}

Definition at line 961 of file misc.c.

◆ tinytens

CONST double tinytens[] = {1e-16, 1e-32}

Definition at line 957 of file misc.c.