g_ddfmt.c revision 165744
1/**************************************************************** 2 3The author of this software is David M. Gay. 4 5Copyright (C) 1998 by Lucent Technologies 6All Rights Reserved 7 8Permission to use, copy, modify, and distribute this software and 9its documentation for any purpose and without fee is hereby 10granted, provided that the above copyright notice appear in all 11copies and that both that the copyright notice and this 12permission notice and warranty disclaimer appear in supporting 13documentation, and that the name of Lucent or any of its entities 14not be used in advertising or publicity pertaining to 15distribution of the software without specific, written prior 16permission. 17 18LUCENT DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, 19INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. 20IN NO EVENT SHALL LUCENT OR ANY OF ITS ENTITIES BE LIABLE FOR ANY 21SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 22WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER 23IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, 24ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF 25THIS SOFTWARE. 26 27****************************************************************/ 28 29/* Please send bug reports to David M. Gay (dmg@acm.org). */ 30 31#include "gdtoaimp.h" 32#include <string.h> 33 34 char * 35#ifdef KR_headers 36g_ddfmt(buf, dd, ndig, bufsize) char *buf; double *dd; int ndig; unsigned bufsize; 37#else 38g_ddfmt(char *buf, double *dd, int ndig, unsigned bufsize) 39#endif 40{ 41 FPI fpi; 42 char *b, *s, *se; 43 ULong *L, bits0[4], *bits, *zx; 44 int bx, by, decpt, ex, ey, i, j, mode; 45 Bigint *x, *y, *z; 46 double ddx[2]; 47 48 if (bufsize < 10 || bufsize < ndig + 8) 49 return 0; 50 51 L = (ULong*)dd; 52 if ((L[_0] & 0x7ff00000L) == 0x7ff00000L) { 53 /* Infinity or NaN */ 54 if (L[_0] & 0xfffff || L[_1]) { 55 nanret: 56 return strcp(buf, "NaN"); 57 } 58 if ((L[2+_0] & 0x7ff00000) == 0x7ff00000) { 59 if (L[2+_0] & 0xfffff || L[2+_1]) 60 goto nanret; 61 if ((L[_0] ^ L[2+_0]) & 0x80000000L) 62 goto nanret; /* Infinity - Infinity */ 63 } 64 infret: 65 b = buf; 66 if (L[_0] & 0x80000000L) 67 *b++ = '-'; 68 return strcp(b, "Infinity"); 69 } 70 if ((L[2+_0] & 0x7ff00000) == 0x7ff00000) { 71 L += 2; 72 if (L[_0] & 0xfffff || L[_1]) 73 goto nanret; 74 goto infret; 75 } 76 if (dd[0] + dd[1] == 0.) { 77 b = buf; 78#ifndef IGNORE_ZERO_SIGN 79 if (L[_0] & L[2+_0] & 0x80000000L) 80 *b++ = '-'; 81#endif 82 *b++ = '0'; 83 *b = 0; 84 return b; 85 } 86 if ((L[_0] & 0x7ff00000L) < (L[2+_0] & 0x7ff00000L)) { 87 ddx[1] = dd[0]; 88 ddx[0] = dd[1]; 89 dd = ddx; 90 L = (ULong*)dd; 91 } 92 z = d2b(dd[0], &ex, &bx); 93 if (dd[1] == 0.) 94 goto no_y; 95 x = z; 96 y = d2b(dd[1], &ey, &by); 97 if ( (i = ex - ey) !=0) { 98 if (i > 0) { 99 x = lshift(x, i); 100 ex = ey; 101 } 102 else 103 y = lshift(y, -i); 104 } 105 if ((L[_0] ^ L[2+_0]) & 0x80000000L) { 106 z = diff(x, y); 107 if (L[_0] & 0x80000000L) 108 z->sign = 1 - z->sign; 109 } 110 else { 111 z = sum(x, y); 112 if (L[_0] & 0x80000000L) 113 z->sign = 1; 114 } 115 Bfree(x); 116 Bfree(y); 117 no_y: 118 bits = zx = z->x; 119 for(i = 0; !*zx; zx++) 120 i += 32; 121 i += lo0bits(zx); 122 if (i) { 123 rshift(z, i); 124 ex += i; 125 } 126 fpi.nbits = z->wds * 32 - hi0bits(z->x[j = z->wds-1]); 127 if (fpi.nbits < 106) { 128 fpi.nbits = 106; 129 if (j < 3) { 130 for(i = 0; i <= j; i++) 131 bits0[i] = bits[i]; 132 while(i < 4) 133 bits0[i++] = 0; 134 bits = bits0; 135 } 136 } 137 mode = 2; 138 if (ndig <= 0) { 139 if (bufsize < (int)(fpi.nbits * .301029995664) + 10) { 140 Bfree(z); 141 return 0; 142 } 143 mode = 0; 144 } 145 fpi.emin = 1-1023-53+1; 146 fpi.emax = 2046-1023-106+1; 147 fpi.rounding = FPI_Round_near; 148 fpi.sudden_underflow = 0; 149 i = STRTOG_Normal; 150 s = gdtoa(&fpi, ex, bits, &i, mode, ndig, &decpt, &se); 151 b = g__fmt(buf, s, se, decpt, z->sign); 152 Bfree(z); 153 return b; 154 } 155