kernel - TMPFS - Stabilization pass, fix accounting for rmdir
[dragonfly.git] / contrib / gdtoa / g_ddfmt.c
blobb65d39d793c424336a9884968e5909ba79aa25e0
1 /****************************************************************
3 The author of this software is David M. Gay.
5 Copyright (C) 1998 by Lucent Technologies
6 All Rights Reserved
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.
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.
27 ****************************************************************/
29 /* Please send bug reports to David M. Gay (dmg@acm.org). */
31 #include "gdtoaimp.h"
32 #include <string.h>
34 char *
35 #ifdef KR_headers
36 g_ddfmt(buf, dd, ndig, bufsize) char *buf; double *dd; int ndig; size_t bufsize;
37 #else
38 g_ddfmt(char *buf, double *dd, int ndig, size_t bufsize)
39 #endif
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 #ifdef Honor_FLT_ROUNDS /*{{*/
48 int Rounding;
49 #ifdef Trust_FLT_ROUNDS /*{{ only define this if FLT_ROUNDS really works! */
50 Rounding = Flt_Rounds;
51 #else /*}{*/
52 Rounding = 1;
53 switch(fegetround()) {
54 case FE_TOWARDZERO: Rounding = 0; break;
55 case FE_UPWARD: Rounding = 2; break;
56 case FE_DOWNWARD: Rounding = 3;
58 #endif /*}}*/
59 #else /*}{*/
60 #define Rounding FPI_Round_near
61 #endif /*}}*/
63 if (bufsize < 10 || bufsize < ndig + 8)
64 return 0;
66 L = (ULong*)dd;
67 if ((L[_0] & 0x7ff00000L) == 0x7ff00000L) {
68 /* Infinity or NaN */
69 if (L[_0] & 0xfffff || L[_1]) {
70 nanret:
71 return strcp(buf, "NaN");
73 if ((L[2+_0] & 0x7ff00000) == 0x7ff00000) {
74 if (L[2+_0] & 0xfffff || L[2+_1])
75 goto nanret;
76 if ((L[_0] ^ L[2+_0]) & 0x80000000L)
77 goto nanret; /* Infinity - Infinity */
79 infret:
80 b = buf;
81 if (L[_0] & 0x80000000L)
82 *b++ = '-';
83 return strcp(b, "Infinity");
85 if ((L[2+_0] & 0x7ff00000) == 0x7ff00000) {
86 L += 2;
87 if (L[_0] & 0xfffff || L[_1])
88 goto nanret;
89 goto infret;
91 if (dd[0] + dd[1] == 0.) {
92 b = buf;
93 #ifndef IGNORE_ZERO_SIGN
94 if (L[_0] & L[2+_0] & 0x80000000L)
95 *b++ = '-';
96 #endif
97 *b++ = '0';
98 *b = 0;
99 return b;
101 if ((L[_0] & 0x7ff00000L) < (L[2+_0] & 0x7ff00000L)) {
102 ddx[1] = dd[0];
103 ddx[0] = dd[1];
104 dd = ddx;
105 L = (ULong*)dd;
107 z = d2b(dd[0], &ex, &bx);
108 if (dd[1] == 0.)
109 goto no_y;
110 x = z;
111 y = d2b(dd[1], &ey, &by);
112 if ( (i = ex - ey) !=0) {
113 if (i > 0) {
114 x = lshift(x, i);
115 ex = ey;
117 else
118 y = lshift(y, -i);
120 if ((L[_0] ^ L[2+_0]) & 0x80000000L) {
121 z = diff(x, y);
122 if (L[_0] & 0x80000000L)
123 z->sign = 1 - z->sign;
125 else {
126 z = sum(x, y);
127 if (L[_0] & 0x80000000L)
128 z->sign = 1;
130 Bfree(x);
131 Bfree(y);
132 no_y:
133 bits = zx = z->x;
134 for(i = 0; !*zx; zx++)
135 i += 32;
136 i += lo0bits(zx);
137 if (i) {
138 rshift(z, i);
139 ex += i;
141 fpi.nbits = z->wds * 32 - hi0bits(z->x[j = z->wds-1]);
142 if (fpi.nbits < 106) {
143 fpi.nbits = 106;
144 if (j < 3) {
145 for(i = 0; i <= j; i++)
146 bits0[i] = bits[i];
147 while(i < 4)
148 bits0[i++] = 0;
149 bits = bits0;
152 mode = 2;
153 if (ndig <= 0) {
154 if (bufsize < (int)(fpi.nbits * .301029995664) + 10) {
155 Bfree(z);
156 return 0;
158 mode = 0;
160 fpi.emin = 1-1023-53+1;
161 fpi.emax = 2046-1023-106+1;
162 fpi.rounding = Rounding;
163 fpi.sudden_underflow = 0;
164 i = STRTOG_Normal;
165 s = gdtoa(&fpi, ex, bits, &i, mode, ndig, &decpt, &se);
166 b = g__fmt(buf, s, se, decpt, z->sign, bufsize);
167 Bfree(z);
168 return b;