1*9a9a4122Sriastradh /* $NetBSD: g_dfmt.c,v 1.4 2019/08/01 02:27:43 riastradh 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 at acm dot org,
327684d5e0Skleink * with " at " changed at "@" and " dot " changed to "."). */
337684d5e0Skleink
347684d5e0Skleink #include "gdtoaimp.h"
357684d5e0Skleink
367684d5e0Skleink char*
377684d5e0Skleink #ifdef KR_headers
g_dfmt(buf,d,ndig,bufsize)3861e56760Schristos g_dfmt(buf, d, ndig, bufsize) char *buf; double *d; int ndig; size_t bufsize;
397684d5e0Skleink #else
4061e56760Schristos g_dfmt(char *buf, double *d, int ndig, size_t bufsize)
417684d5e0Skleink #endif
427684d5e0Skleink {
43*9a9a4122Sriastradh static CONST FPI fpi0 = { 53, 1-1023-53+1, 2046-1023-53+1, 1, 0 };
447684d5e0Skleink char *b, *s, *se;
457684d5e0Skleink ULong bits[2], *L, sign;
467684d5e0Skleink int decpt, ex, i, mode;
4761e56760Schristos #ifdef Honor_FLT_ROUNDS
4861e56760Schristos #include "gdtoa_fltrnds.h"
4961e56760Schristos #else
5061e56760Schristos #define fpi &fpi0
5161e56760Schristos #endif
527684d5e0Skleink
537684d5e0Skleink if (ndig < 0)
547684d5e0Skleink ndig = 0;
557684d5e0Skleink if (bufsize < ndig + 10)
567684d5e0Skleink return 0;
577684d5e0Skleink
587684d5e0Skleink L = (ULong*)d;
597684d5e0Skleink sign = L[_0] & 0x80000000L;
607684d5e0Skleink if ((L[_0] & 0x7ff00000) == 0x7ff00000) {
617684d5e0Skleink /* Infinity or NaN */
6261e56760Schristos if (bufsize < 10)
6361e56760Schristos return 0;
647684d5e0Skleink if (L[_0] & 0xfffff || L[_1]) {
657684d5e0Skleink return strcp(buf, "NaN");
667684d5e0Skleink }
677684d5e0Skleink b = buf;
687684d5e0Skleink if (sign)
697684d5e0Skleink *b++ = '-';
707684d5e0Skleink return strcp(b, "Infinity");
717684d5e0Skleink }
727684d5e0Skleink if (L[_1] == 0 && (L[_0] ^ sign) == 0 /*d == 0.*/) {
737684d5e0Skleink b = buf;
747684d5e0Skleink #ifndef IGNORE_ZERO_SIGN
757684d5e0Skleink if (L[_0] & 0x80000000L)
767684d5e0Skleink *b++ = '-';
777684d5e0Skleink #endif
787684d5e0Skleink *b++ = '0';
797684d5e0Skleink *b = 0;
807684d5e0Skleink return b;
817684d5e0Skleink }
827684d5e0Skleink bits[0] = L[_1];
837684d5e0Skleink bits[1] = L[_0] & 0xfffff;
847684d5e0Skleink if ( (ex = (L[_0] >> 20) & 0x7ff) !=0)
857684d5e0Skleink bits[1] |= 0x100000;
867684d5e0Skleink else
877684d5e0Skleink ex = 1;
887684d5e0Skleink ex -= 0x3ff + 52;
897684d5e0Skleink mode = 2;
9061e56760Schristos if (ndig <= 0)
917684d5e0Skleink mode = 0;
927684d5e0Skleink i = STRTOG_Normal;
9361e56760Schristos if (sign)
9461e56760Schristos i = STRTOG_Normal | STRTOG_Neg;
9561e56760Schristos s = gdtoa(fpi, ex, bits, &i, mode, ndig, &decpt, &se);
96ab625449Schristos if (s == NULL)
97ab625449Schristos return NULL;
9861e56760Schristos return g__fmt(buf, s, se, decpt, sign, bufsize);
997684d5e0Skleink }
100