Import gdtoa-20081205.
[dragonfly.git] / contrib / gdtoa / g_dfmt.c
1 /****************************************************************
2
3 The author of this software is David M. Gay.
4
5 Copyright (C) 1998 by Lucent Technologies
6 All Rights Reserved
7
8 Permission to use, copy, modify, and distribute this software and
9 its documentation for any purpose and without fee is hereby
10 granted, provided that the above copyright notice appear in all
11 copies and that both that the copyright notice and this
12 permission notice and warranty disclaimer appear in supporting
13 documentation, and that the name of Lucent or any of its entities
14 not be used in advertising or publicity pertaining to
15 distribution of the software without specific, written prior
16 permission.
17
18 LUCENT DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
19 INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS.
20 IN NO EVENT SHALL LUCENT OR ANY OF ITS ENTITIES BE LIABLE FOR ANY
21 SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
22 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER
23 IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
24 ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF
25 THIS SOFTWARE.
26
27 ****************************************************************/
28
29 /* Please send bug reports to David M. Gay (dmg at acm dot org,
30  * with " at " changed at "@" and " dot " changed to ".").      */
31
32 #include "gdtoaimp.h"
33
34  char*
35 #ifdef KR_headers
36 g_dfmt(buf, d, ndig, bufsize) char *buf; double *d; int ndig; size_t bufsize;
37 #else
38 g_dfmt(char *buf, double *d, int ndig, size_t bufsize)
39 #endif
40 {
41         static FPI fpi0 = { 53, 1-1023-53+1, 2046-1023-53+1, 1, 0 };
42         char *b, *s, *se;
43         ULong bits[2], *L, sign;
44         int decpt, ex, i, mode;
45 #ifdef Honor_FLT_ROUNDS
46 #include "gdtoa_fltrnds.h"
47 #else
48 #define fpi &fpi0
49 #endif
50
51         if (ndig < 0)
52                 ndig = 0;
53         if (bufsize < ndig + 10)
54                 return 0;
55
56         L = (ULong*)d;
57         sign = L[_0] & 0x80000000L;
58         if ((L[_0] & 0x7ff00000) == 0x7ff00000) {
59                 /* Infinity or NaN */
60                 if (bufsize < 10)
61                         return 0;
62                 if (L[_0] & 0xfffff || L[_1]) {
63                         return strcp(buf, "NaN");
64                         }
65                 b = buf;
66                 if (sign)
67                         *b++ = '-';
68                 return strcp(b, "Infinity");
69                 }
70         if (L[_1] == 0 && (L[_0] ^ sign) == 0 /*d == 0.*/) {
71                 b = buf;
72 #ifndef IGNORE_ZERO_SIGN
73                 if (L[_0] & 0x80000000L)
74                         *b++ = '-';
75 #endif
76                 *b++ = '0';
77                 *b = 0;
78                 return b;
79                 }
80         bits[0] = L[_1];
81         bits[1] = L[_0] & 0xfffff;
82         if ( (ex = (L[_0] >> 20) & 0x7ff) !=0)
83                 bits[1] |= 0x100000;
84         else
85                 ex = 1;
86         ex -= 0x3ff + 52;
87         mode = 2;
88         if (ndig <= 0)
89                 mode = 0;
90         i = STRTOG_Normal;
91         s = gdtoa(fpi, ex, bits, &i, mode, ndig, &decpt, &se);
92         return g__fmt(buf, s, se, decpt, sign, bufsize);
93         }