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