Home | History | Annotate | Line # | Download | only in gdtoa
dmisc.c revision 1.1.1.2
      1  1.1  kleink /****************************************************************
      2  1.1  kleink 
      3  1.1  kleink The author of this software is David M. Gay.
      4  1.1  kleink 
      5  1.1  kleink Copyright (C) 1998 by Lucent Technologies
      6  1.1  kleink All Rights Reserved
      7  1.1  kleink 
      8  1.1  kleink Permission to use, copy, modify, and distribute this software and
      9  1.1  kleink its documentation for any purpose and without fee is hereby
     10  1.1  kleink granted, provided that the above copyright notice appear in all
     11  1.1  kleink copies and that both that the copyright notice and this
     12  1.1  kleink permission notice and warranty disclaimer appear in supporting
     13  1.1  kleink documentation, and that the name of Lucent or any of its entities
     14  1.1  kleink not be used in advertising or publicity pertaining to
     15  1.1  kleink distribution of the software without specific, written prior
     16  1.1  kleink permission.
     17  1.1  kleink 
     18  1.1  kleink LUCENT DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
     19  1.1  kleink INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS.
     20  1.1  kleink IN NO EVENT SHALL LUCENT OR ANY OF ITS ENTITIES BE LIABLE FOR ANY
     21  1.1  kleink SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
     22  1.1  kleink WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER
     23  1.1  kleink IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
     24  1.1  kleink ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF
     25  1.1  kleink THIS SOFTWARE.
     26  1.1  kleink 
     27  1.1  kleink ****************************************************************/
     28  1.1  kleink 
     29  1.1  kleink /* Please send bug reports to David M. Gay (dmg at acm dot org,
     30  1.1  kleink  * with " at " changed at "@" and " dot " changed to ".").	*/
     31  1.1  kleink 
     32  1.1  kleink #include "gdtoaimp.h"
     33  1.1  kleink 
     34  1.1  kleink #ifndef MULTIPLE_THREADS
     35  1.1  kleink  char *dtoa_result;
     36  1.1  kleink #endif
     37  1.1  kleink 
     38  1.1  kleink  char *
     39  1.1  kleink #ifdef KR_headers
     40  1.1  kleink rv_alloc(i) int i;
     41  1.1  kleink #else
     42  1.1  kleink rv_alloc(int i)
     43  1.1  kleink #endif
     44  1.1  kleink {
     45  1.1  kleink 	int j, k, *r;
     46  1.1  kleink 
     47  1.1  kleink 	j = sizeof(ULong);
     48  1.1  kleink 	for(k = 0;
     49  1.1  kleink 		sizeof(Bigint) - sizeof(ULong) - sizeof(int) + j <= i;
     50  1.1  kleink 		j <<= 1)
     51  1.1  kleink 			k++;
     52  1.1  kleink 	r = (int*)Balloc(k);
     53  1.1  kleink 	*r = k;
     54  1.1  kleink 	return
     55  1.1  kleink #ifndef MULTIPLE_THREADS
     56  1.1  kleink 	dtoa_result =
     57  1.1  kleink #endif
     58  1.1  kleink 		(char *)(r+1);
     59  1.1  kleink 	}
     60  1.1  kleink 
     61  1.1  kleink  char *
     62  1.1  kleink #ifdef KR_headers
     63  1.1  kleink nrv_alloc(s, rve, n) char *s, **rve; int n;
     64  1.1  kleink #else
     65  1.1  kleink nrv_alloc(char *s, char **rve, int n)
     66  1.1  kleink #endif
     67  1.1  kleink {
     68  1.1  kleink 	char *rv, *t;
     69  1.1  kleink 
     70  1.1  kleink 	t = rv = rv_alloc(n);
     71  1.1  kleink 	while((*t = *s++) !=0)
     72  1.1  kleink 		t++;
     73  1.1  kleink 	if (rve)
     74  1.1  kleink 		*rve = t;
     75  1.1  kleink 	return rv;
     76  1.1  kleink 	}
     77  1.1  kleink 
     78  1.1  kleink /* freedtoa(s) must be used to free values s returned by dtoa
     79  1.1  kleink  * when MULTIPLE_THREADS is #defined.  It should be used in all cases,
     80  1.1  kleink  * but for consistency with earlier versions of dtoa, it is optional
     81  1.1  kleink  * when MULTIPLE_THREADS is not defined.
     82  1.1  kleink  */
     83  1.1  kleink 
     84  1.1  kleink  void
     85  1.1  kleink #ifdef KR_headers
     86  1.1  kleink freedtoa(s) char *s;
     87  1.1  kleink #else
     88  1.1  kleink freedtoa(char *s)
     89  1.1  kleink #endif
     90  1.1  kleink {
     91  1.1  kleink 	Bigint *b = (Bigint *)((int *)s - 1);
     92  1.1  kleink 	b->maxwds = 1 << (b->k = *(int*)b);
     93  1.1  kleink 	Bfree(b);
     94  1.1  kleink #ifndef MULTIPLE_THREADS
     95  1.1  kleink 	if (s == dtoa_result)
     96  1.1  kleink 		dtoa_result = 0;
     97  1.1  kleink #endif
     98  1.1  kleink 	}
     99  1.1  kleink 
    100  1.1  kleink  int
    101  1.1  kleink quorem
    102  1.1  kleink #ifdef KR_headers
    103  1.1  kleink 	(b, S) Bigint *b, *S;
    104  1.1  kleink #else
    105  1.1  kleink 	(Bigint *b, Bigint *S)
    106  1.1  kleink #endif
    107  1.1  kleink {
    108  1.1  kleink 	int n;
    109  1.1  kleink 	ULong *bx, *bxe, q, *sx, *sxe;
    110  1.1  kleink #ifdef ULLong
    111  1.1  kleink 	ULLong borrow, carry, y, ys;
    112  1.1  kleink #else
    113  1.1  kleink 	ULong borrow, carry, y, ys;
    114  1.1  kleink #ifdef Pack_32
    115  1.1  kleink 	ULong si, z, zs;
    116  1.1  kleink #endif
    117  1.1  kleink #endif
    118  1.1  kleink 
    119  1.1  kleink 	n = S->wds;
    120  1.1  kleink #ifdef DEBUG
    121  1.1  kleink 	/*debug*/ if (b->wds > n)
    122  1.1  kleink 	/*debug*/	Bug("oversize b in quorem");
    123  1.1  kleink #endif
    124  1.1  kleink 	if (b->wds < n)
    125  1.1  kleink 		return 0;
    126  1.1  kleink 	sx = S->x;
    127  1.1  kleink 	sxe = sx + --n;
    128  1.1  kleink 	bx = b->x;
    129  1.1  kleink 	bxe = bx + n;
    130  1.1  kleink 	q = *bxe / (*sxe + 1);	/* ensure q <= true quotient */
    131  1.1  kleink #ifdef DEBUG
    132  1.1  kleink 	/*debug*/ if (q > 9)
    133  1.1  kleink 	/*debug*/	Bug("oversized quotient in quorem");
    134  1.1  kleink #endif
    135  1.1  kleink 	if (q) {
    136  1.1  kleink 		borrow = 0;
    137  1.1  kleink 		carry = 0;
    138  1.1  kleink 		do {
    139  1.1  kleink #ifdef ULLong
    140  1.1  kleink 			ys = *sx++ * (ULLong)q + carry;
    141  1.1  kleink 			carry = ys >> 32;
    142  1.1  kleink 			y = *bx - (ys & 0xffffffffUL) - borrow;
    143  1.1  kleink 			borrow = y >> 32 & 1UL;
    144  1.1  kleink 			*bx++ = y & 0xffffffffUL;
    145  1.1  kleink #else
    146  1.1  kleink #ifdef Pack_32
    147  1.1  kleink 			si = *sx++;
    148  1.1  kleink 			ys = (si & 0xffff) * q + carry;
    149  1.1  kleink 			zs = (si >> 16) * q + (ys >> 16);
    150  1.1  kleink 			carry = zs >> 16;
    151  1.1  kleink 			y = (*bx & 0xffff) - (ys & 0xffff) - borrow;
    152  1.1  kleink 			borrow = (y & 0x10000) >> 16;
    153  1.1  kleink 			z = (*bx >> 16) - (zs & 0xffff) - borrow;
    154  1.1  kleink 			borrow = (z & 0x10000) >> 16;
    155  1.1  kleink 			Storeinc(bx, z, y);
    156  1.1  kleink #else
    157  1.1  kleink 			ys = *sx++ * q + carry;
    158  1.1  kleink 			carry = ys >> 16;
    159  1.1  kleink 			y = *bx - (ys & 0xffff) - borrow;
    160  1.1  kleink 			borrow = (y & 0x10000) >> 16;
    161  1.1  kleink 			*bx++ = y & 0xffff;
    162  1.1  kleink #endif
    163  1.1  kleink #endif
    164  1.1  kleink 			}
    165  1.1  kleink 			while(sx <= sxe);
    166  1.1  kleink 		if (!*bxe) {
    167  1.1  kleink 			bx = b->x;
    168  1.1  kleink 			while(--bxe > bx && !*bxe)
    169  1.1  kleink 				--n;
    170  1.1  kleink 			b->wds = n;
    171  1.1  kleink 			}
    172  1.1  kleink 		}
    173  1.1  kleink 	if (cmp(b, S) >= 0) {
    174  1.1  kleink 		q++;
    175  1.1  kleink 		borrow = 0;
    176  1.1  kleink 		carry = 0;
    177  1.1  kleink 		bx = b->x;
    178  1.1  kleink 		sx = S->x;
    179  1.1  kleink 		do {
    180  1.1  kleink #ifdef ULLong
    181  1.1  kleink 			ys = *sx++ + carry;
    182  1.1  kleink 			carry = ys >> 32;
    183  1.1  kleink 			y = *bx - (ys & 0xffffffffUL) - borrow;
    184  1.1  kleink 			borrow = y >> 32 & 1UL;
    185  1.1  kleink 			*bx++ = y & 0xffffffffUL;
    186  1.1  kleink #else
    187  1.1  kleink #ifdef Pack_32
    188  1.1  kleink 			si = *sx++;
    189  1.1  kleink 			ys = (si & 0xffff) + carry;
    190  1.1  kleink 			zs = (si >> 16) + (ys >> 16);
    191  1.1  kleink 			carry = zs >> 16;
    192  1.1  kleink 			y = (*bx & 0xffff) - (ys & 0xffff) - borrow;
    193  1.1  kleink 			borrow = (y & 0x10000) >> 16;
    194  1.1  kleink 			z = (*bx >> 16) - (zs & 0xffff) - borrow;
    195  1.1  kleink 			borrow = (z & 0x10000) >> 16;
    196  1.1  kleink 			Storeinc(bx, z, y);
    197  1.1  kleink #else
    198  1.1  kleink 			ys = *sx++ + carry;
    199  1.1  kleink 			carry = ys >> 16;
    200  1.1  kleink 			y = *bx - (ys & 0xffff) - borrow;
    201  1.1  kleink 			borrow = (y & 0x10000) >> 16;
    202  1.1  kleink 			*bx++ = y & 0xffff;
    203  1.1  kleink #endif
    204  1.1  kleink #endif
    205  1.1  kleink 			}
    206  1.1  kleink 			while(sx <= sxe);
    207  1.1  kleink 		bx = b->x;
    208  1.1  kleink 		bxe = bx + n;
    209  1.1  kleink 		if (!*bxe) {
    210  1.1  kleink 			while(--bxe > bx && !*bxe)
    211  1.1  kleink 				--n;
    212  1.1  kleink 			b->wds = n;
    213  1.1  kleink 			}
    214  1.1  kleink 		}
    215  1.1  kleink 	return q;
    216  1.1  kleink 	}
    217