1*61e56760Schristos /* $NetBSD: g_ddfmt.c,v 1.3 2011/03/20 23:15:35 christos Exp $ */
27684d5e0Skleink
37684d5e0Skleink /****************************************************************
47684d5e0Skleink
57684d5e0Skleink The author of this software is David M. Gay.
67684d5e0Skleink
77684d5e0Skleink Copyright (C) 1998 by Lucent Technologies
87684d5e0Skleink All Rights Reserved
97684d5e0Skleink
107684d5e0Skleink Permission to use, copy, modify, and distribute this software and
117684d5e0Skleink its documentation for any purpose and without fee is hereby
127684d5e0Skleink granted, provided that the above copyright notice appear in all
137684d5e0Skleink copies and that both that the copyright notice and this
147684d5e0Skleink permission notice and warranty disclaimer appear in supporting
157684d5e0Skleink documentation, and that the name of Lucent or any of its entities
167684d5e0Skleink not be used in advertising or publicity pertaining to
177684d5e0Skleink distribution of the software without specific, written prior
187684d5e0Skleink permission.
197684d5e0Skleink
207684d5e0Skleink LUCENT DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
217684d5e0Skleink INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS.
227684d5e0Skleink IN NO EVENT SHALL LUCENT OR ANY OF ITS ENTITIES BE LIABLE FOR ANY
237684d5e0Skleink SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
247684d5e0Skleink WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER
257684d5e0Skleink IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
267684d5e0Skleink ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF
277684d5e0Skleink THIS SOFTWARE.
287684d5e0Skleink
297684d5e0Skleink ****************************************************************/
307684d5e0Skleink
317684d5e0Skleink /* Please send bug reports to David M. Gay (dmg@acm.org). */
327684d5e0Skleink
337684d5e0Skleink #include "gdtoaimp.h"
347684d5e0Skleink #include <string.h>
357684d5e0Skleink
367684d5e0Skleink char *
377684d5e0Skleink #ifdef KR_headers
g_ddfmt(buf,dd0,ndig,bufsize)38*61e56760Schristos g_ddfmt(buf, dd0, ndig, bufsize) char *buf; double *dd0; int ndig; size_t bufsize;
397684d5e0Skleink #else
40*61e56760Schristos g_ddfmt(char *buf, double *dd0, int ndig, size_t bufsize)
417684d5e0Skleink #endif
427684d5e0Skleink {
437684d5e0Skleink FPI fpi;
447684d5e0Skleink char *b, *s, *se;
457684d5e0Skleink ULong *L, bits0[4], *bits, *zx;
467684d5e0Skleink int bx, by, decpt, ex, ey, i, j, mode;
477684d5e0Skleink Bigint *x, *y, *z;
48*61e56760Schristos U *dd, ddx[2];
49*61e56760Schristos #ifdef Honor_FLT_ROUNDS /*{{*/
50*61e56760Schristos int Rounding;
51*61e56760Schristos #ifdef Trust_FLT_ROUNDS /*{{ only define this if FLT_ROUNDS really works! */
52*61e56760Schristos Rounding = Flt_Rounds;
53*61e56760Schristos #else /*}{*/
54*61e56760Schristos Rounding = 1;
55*61e56760Schristos switch(fegetround()) {
56*61e56760Schristos case FE_TOWARDZERO: Rounding = 0; break;
57*61e56760Schristos case FE_UPWARD: Rounding = 2; break;
58*61e56760Schristos case FE_DOWNWARD: Rounding = 3;
59*61e56760Schristos }
60*61e56760Schristos #endif /*}}*/
61*61e56760Schristos #else /*}{*/
62*61e56760Schristos #define Rounding FPI_Round_near
63*61e56760Schristos #endif /*}}*/
647684d5e0Skleink
657684d5e0Skleink if (bufsize < 10 || bufsize < ndig + 8)
667684d5e0Skleink return 0;
677684d5e0Skleink
68*61e56760Schristos dd = (U*)dd0;
69*61e56760Schristos L = dd->L;
707684d5e0Skleink if ((L[_0] & 0x7ff00000L) == 0x7ff00000L) {
717684d5e0Skleink /* Infinity or NaN */
727684d5e0Skleink if (L[_0] & 0xfffff || L[_1]) {
737684d5e0Skleink nanret:
747684d5e0Skleink return strcp(buf, "NaN");
757684d5e0Skleink }
767684d5e0Skleink if ((L[2+_0] & 0x7ff00000) == 0x7ff00000) {
777684d5e0Skleink if (L[2+_0] & 0xfffff || L[2+_1])
787684d5e0Skleink goto nanret;
797684d5e0Skleink if ((L[_0] ^ L[2+_0]) & 0x80000000L)
807684d5e0Skleink goto nanret; /* Infinity - Infinity */
817684d5e0Skleink }
827684d5e0Skleink infret:
837684d5e0Skleink b = buf;
847684d5e0Skleink if (L[_0] & 0x80000000L)
857684d5e0Skleink *b++ = '-';
867684d5e0Skleink return strcp(b, "Infinity");
877684d5e0Skleink }
887684d5e0Skleink if ((L[2+_0] & 0x7ff00000) == 0x7ff00000) {
897684d5e0Skleink L += 2;
907684d5e0Skleink if (L[_0] & 0xfffff || L[_1])
917684d5e0Skleink goto nanret;
927684d5e0Skleink goto infret;
937684d5e0Skleink }
94*61e56760Schristos if (dval(&dd[0]) + dval(&dd[1]) == 0.) {
957684d5e0Skleink b = buf;
967684d5e0Skleink #ifndef IGNORE_ZERO_SIGN
977684d5e0Skleink if (L[_0] & L[2+_0] & 0x80000000L)
987684d5e0Skleink *b++ = '-';
997684d5e0Skleink #endif
1007684d5e0Skleink *b++ = '0';
1017684d5e0Skleink *b = 0;
1027684d5e0Skleink return b;
1037684d5e0Skleink }
1047684d5e0Skleink if ((L[_0] & 0x7ff00000L) < (L[2+_0] & 0x7ff00000L)) {
105*61e56760Schristos dval(&ddx[1]) = dval(&dd[0]);
106*61e56760Schristos dval(&ddx[0]) = dval(&dd[1]);
1077684d5e0Skleink dd = ddx;
108*61e56760Schristos L = dd->L;
1097684d5e0Skleink }
110*61e56760Schristos z = d2b(dval(&dd[0]), &ex, &bx);
111ab625449Schristos if (z == NULL)
112ab625449Schristos return NULL;
113*61e56760Schristos if (dval(&dd[1]) == 0.)
1147684d5e0Skleink goto no_y;
1157684d5e0Skleink x = z;
116*61e56760Schristos y = d2b(dval(&dd[1]), &ey, &by);
117ab625449Schristos if (y == NULL)
118ab625449Schristos return NULL;
1197684d5e0Skleink if ( (i = ex - ey) !=0) {
1207684d5e0Skleink if (i > 0) {
1217684d5e0Skleink x = lshift(x, i);
122ab625449Schristos if (x == NULL)
123ab625449Schristos return NULL;
1247684d5e0Skleink ex = ey;
1257684d5e0Skleink }
126ab625449Schristos else {
1277684d5e0Skleink y = lshift(y, -i);
128ab625449Schristos if (y == NULL)
129ab625449Schristos return NULL;
130ab625449Schristos }
1317684d5e0Skleink }
1327684d5e0Skleink if ((L[_0] ^ L[2+_0]) & 0x80000000L) {
1337684d5e0Skleink z = diff(x, y);
134ab625449Schristos if (z == NULL)
135ab625449Schristos return NULL;
1367684d5e0Skleink if (L[_0] & 0x80000000L)
1377684d5e0Skleink z->sign = 1 - z->sign;
1387684d5e0Skleink }
1397684d5e0Skleink else {
1407684d5e0Skleink z = sum(x, y);
141ab625449Schristos if (z == NULL)
142ab625449Schristos return NULL;
1437684d5e0Skleink if (L[_0] & 0x80000000L)
1447684d5e0Skleink z->sign = 1;
1457684d5e0Skleink }
1467684d5e0Skleink Bfree(x);
1477684d5e0Skleink Bfree(y);
1487684d5e0Skleink no_y:
1497684d5e0Skleink bits = zx = z->x;
1507684d5e0Skleink for(i = 0; !*zx; zx++)
1517684d5e0Skleink i += 32;
1527684d5e0Skleink i += lo0bits(zx);
1537684d5e0Skleink if (i) {
1547684d5e0Skleink rshift(z, i);
1557684d5e0Skleink ex += i;
1567684d5e0Skleink }
1577684d5e0Skleink fpi.nbits = z->wds * 32 - hi0bits(z->x[j = z->wds-1]);
1587684d5e0Skleink if (fpi.nbits < 106) {
1597684d5e0Skleink fpi.nbits = 106;
1607684d5e0Skleink if (j < 3) {
1617684d5e0Skleink for(i = 0; i <= j; i++)
1627684d5e0Skleink bits0[i] = bits[i];
1637684d5e0Skleink while(i < 4)
1647684d5e0Skleink bits0[i++] = 0;
1657684d5e0Skleink bits = bits0;
1667684d5e0Skleink }
1677684d5e0Skleink }
1687684d5e0Skleink mode = 2;
1697684d5e0Skleink if (ndig <= 0) {
1707684d5e0Skleink if (bufsize < (int)(fpi.nbits * .301029995664) + 10) {
1717684d5e0Skleink Bfree(z);
1727684d5e0Skleink return 0;
1737684d5e0Skleink }
1747684d5e0Skleink mode = 0;
1757684d5e0Skleink }
1767684d5e0Skleink fpi.emin = 1-1023-53+1;
1777684d5e0Skleink fpi.emax = 2046-1023-106+1;
178*61e56760Schristos fpi.rounding = Rounding;
1797684d5e0Skleink fpi.sudden_underflow = 0;
1807684d5e0Skleink i = STRTOG_Normal;
1817684d5e0Skleink s = gdtoa(&fpi, ex, bits, &i, mode, ndig, &decpt, &se);
182ab625449Schristos if (s == NULL)
183ab625449Schristos return NULL;
184*61e56760Schristos b = g__fmt(buf, s, se, decpt, z->sign, bufsize);
185ab625449Schristos if (b == NULL)
186ab625449Schristos return NULL;
1877684d5e0Skleink Bfree(z);
1887684d5e0Skleink return b;
1897684d5e0Skleink }
190