Home | History | Annotate | Line # | Download | only in gdtoa
g_ddfmt.c revision 1.1
      1  1.1  kleink /* $NetBSD: g_ddfmt.c,v 1.1 2006/01/25 15:18:44 kleink 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.org). */
     32  1.1  kleink 
     33  1.1  kleink #include "gdtoaimp.h"
     34  1.1  kleink #include <string.h>
     35  1.1  kleink 
     36  1.1  kleink  char *
     37  1.1  kleink #ifdef KR_headers
     38  1.1  kleink g_ddfmt(buf, dd, ndig, bufsize) char *buf; double *dd; int ndig; unsigned bufsize;
     39  1.1  kleink #else
     40  1.1  kleink g_ddfmt(char *buf, double *dd, int ndig, unsigned bufsize)
     41  1.1  kleink #endif
     42  1.1  kleink {
     43  1.1  kleink 	FPI fpi;
     44  1.1  kleink 	char *b, *s, *se;
     45  1.1  kleink 	ULong *L, bits0[4], *bits, *zx;
     46  1.1  kleink 	int bx, by, decpt, ex, ey, i, j, mode;
     47  1.1  kleink 	Bigint *x, *y, *z;
     48  1.1  kleink 	double ddx[2];
     49  1.1  kleink 
     50  1.1  kleink 	if (bufsize < 10 || bufsize < ndig + 8)
     51  1.1  kleink 		return 0;
     52  1.1  kleink 
     53  1.1  kleink 	L = (ULong*)dd;
     54  1.1  kleink 	if ((L[_0] & 0x7ff00000L) == 0x7ff00000L) {
     55  1.1  kleink 		/* Infinity or NaN */
     56  1.1  kleink 		if (L[_0] & 0xfffff || L[_1]) {
     57  1.1  kleink  nanret:
     58  1.1  kleink 			return strcp(buf, "NaN");
     59  1.1  kleink 			}
     60  1.1  kleink 		if ((L[2+_0] & 0x7ff00000) == 0x7ff00000) {
     61  1.1  kleink 			if (L[2+_0] & 0xfffff || L[2+_1])
     62  1.1  kleink 				goto nanret;
     63  1.1  kleink 			if ((L[_0] ^ L[2+_0]) & 0x80000000L)
     64  1.1  kleink 				goto nanret;	/* Infinity - Infinity */
     65  1.1  kleink 			}
     66  1.1  kleink  infret:
     67  1.1  kleink 		b = buf;
     68  1.1  kleink 		if (L[_0] & 0x80000000L)
     69  1.1  kleink 			*b++ = '-';
     70  1.1  kleink 		return strcp(b, "Infinity");
     71  1.1  kleink 		}
     72  1.1  kleink 	if ((L[2+_0] & 0x7ff00000) == 0x7ff00000) {
     73  1.1  kleink 		L += 2;
     74  1.1  kleink 		if (L[_0] & 0xfffff || L[_1])
     75  1.1  kleink 			goto nanret;
     76  1.1  kleink 		goto infret;
     77  1.1  kleink 		}
     78  1.1  kleink 	if (dd[0] + dd[1] == 0.) {
     79  1.1  kleink 		b = buf;
     80  1.1  kleink #ifndef IGNORE_ZERO_SIGN
     81  1.1  kleink 		if (L[_0] & L[2+_0] & 0x80000000L)
     82  1.1  kleink 			*b++ = '-';
     83  1.1  kleink #endif
     84  1.1  kleink 		*b++ = '0';
     85  1.1  kleink 		*b = 0;
     86  1.1  kleink 		return b;
     87  1.1  kleink 		}
     88  1.1  kleink 	if ((L[_0] & 0x7ff00000L) < (L[2+_0] & 0x7ff00000L)) {
     89  1.1  kleink 		ddx[1] = dd[0];
     90  1.1  kleink 		ddx[0] = dd[1];
     91  1.1  kleink 		dd = ddx;
     92  1.1  kleink 		L = (ULong*)dd;
     93  1.1  kleink 		}
     94  1.1  kleink 	z = d2b(dd[0], &ex, &bx);
     95  1.1  kleink 	if (dd[1] == 0.)
     96  1.1  kleink 		goto no_y;
     97  1.1  kleink 	x = z;
     98  1.1  kleink 	y = d2b(dd[1], &ey, &by);
     99  1.1  kleink 	if ( (i = ex - ey) !=0) {
    100  1.1  kleink 		if (i > 0) {
    101  1.1  kleink 			x = lshift(x, i);
    102  1.1  kleink 			ex = ey;
    103  1.1  kleink 			}
    104  1.1  kleink 		else
    105  1.1  kleink 			y = lshift(y, -i);
    106  1.1  kleink 		}
    107  1.1  kleink 	if ((L[_0] ^ L[2+_0]) & 0x80000000L) {
    108  1.1  kleink 		z = diff(x, y);
    109  1.1  kleink 		if (L[_0] & 0x80000000L)
    110  1.1  kleink 			z->sign = 1 - z->sign;
    111  1.1  kleink 		}
    112  1.1  kleink 	else {
    113  1.1  kleink 		z = sum(x, y);
    114  1.1  kleink 		if (L[_0] & 0x80000000L)
    115  1.1  kleink 			z->sign = 1;
    116  1.1  kleink 		}
    117  1.1  kleink 	Bfree(x);
    118  1.1  kleink 	Bfree(y);
    119  1.1  kleink  no_y:
    120  1.1  kleink 	bits = zx = z->x;
    121  1.1  kleink 	for(i = 0; !*zx; zx++)
    122  1.1  kleink 		i += 32;
    123  1.1  kleink 	i += lo0bits(zx);
    124  1.1  kleink 	if (i) {
    125  1.1  kleink 		rshift(z, i);
    126  1.1  kleink 		ex += i;
    127  1.1  kleink 		}
    128  1.1  kleink 	fpi.nbits = z->wds * 32 - hi0bits(z->x[j = z->wds-1]);
    129  1.1  kleink 	if (fpi.nbits < 106) {
    130  1.1  kleink 		fpi.nbits = 106;
    131  1.1  kleink 		if (j < 3) {
    132  1.1  kleink 			for(i = 0; i <= j; i++)
    133  1.1  kleink 				bits0[i] = bits[i];
    134  1.1  kleink 			while(i < 4)
    135  1.1  kleink 				bits0[i++] = 0;
    136  1.1  kleink 			bits = bits0;
    137  1.1  kleink 			}
    138  1.1  kleink 		}
    139  1.1  kleink 	mode = 2;
    140  1.1  kleink 	if (ndig <= 0) {
    141  1.1  kleink 		if (bufsize < (int)(fpi.nbits * .301029995664) + 10) {
    142  1.1  kleink 			Bfree(z);
    143  1.1  kleink 			return 0;
    144  1.1  kleink 			}
    145  1.1  kleink 		mode = 0;
    146  1.1  kleink 		}
    147  1.1  kleink 	fpi.emin = 1-1023-53+1;
    148  1.1  kleink 	fpi.emax = 2046-1023-106+1;
    149  1.1  kleink 	fpi.rounding = FPI_Round_near;
    150  1.1  kleink 	fpi.sudden_underflow = 0;
    151  1.1  kleink 	i = STRTOG_Normal;
    152  1.1  kleink 	s = gdtoa(&fpi, ex, bits, &i, mode, ndig, &decpt, &se);
    153  1.1  kleink 	b = g__fmt(buf, s, se, decpt, z->sign);
    154  1.1  kleink 	Bfree(z);
    155  1.1  kleink 	return b;
    156  1.1  kleink 	}
    157