1 /* $NetBSD: g_ddfmt.c,v 1.3 2011/03/20 23:15:35 christos Exp $ */
3 /****************************************************************
5 The author of this software is David M. Gay.
7 Copyright (C) 1998 by Lucent Technologies
10 Permission to use, copy, modify, and distribute this software and
11 its documentation for any purpose and without fee is hereby
12 granted, provided that the above copyright notice appear in all
13 copies and that both that the copyright notice and this
14 permission notice and warranty disclaimer appear in supporting
15 documentation, and that the name of Lucent or any of its entities
16 not be used in advertising or publicity pertaining to
17 distribution of the software without specific, written prior
20 LUCENT DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
21 INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS.
22 IN NO EVENT SHALL LUCENT OR ANY OF ITS ENTITIES BE LIABLE FOR ANY
23 SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
24 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER
25 IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
26 ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF
29 ****************************************************************/
31 /* Please send bug reports to David M. Gay (dmg@acm.org). */
38 g_ddfmt(buf
, dd0
, ndig
, bufsize
) char *buf
; double *dd0
; int ndig
; size_t bufsize
;
40 g_ddfmt(char *buf
, double *dd0
, int ndig
, size_t bufsize
)
45 ULong
*L
, bits0
[4], *bits
, *zx
;
46 int bx
, by
, decpt
, ex
, ey
, i
, j
, mode
;
49 #ifdef Honor_FLT_ROUNDS /*{{*/
51 #ifdef Trust_FLT_ROUNDS /*{{ only define this if FLT_ROUNDS really works! */
52 Rounding
= Flt_Rounds
;
55 switch(fegetround()) {
56 case FE_TOWARDZERO
: Rounding
= 0; break;
57 case FE_UPWARD
: Rounding
= 2; break;
58 case FE_DOWNWARD
: Rounding
= 3;
62 #define Rounding FPI_Round_near
65 if (bufsize
< 10 || bufsize
< ndig
+ 8)
70 if ((L
[_0
] & 0x7ff00000L
) == 0x7ff00000L
) {
72 if (L
[_0
] & 0xfffff || L
[_1
]) {
74 return strcp(buf
, "NaN");
76 if ((L
[2+_0
] & 0x7ff00000) == 0x7ff00000) {
77 if (L
[2+_0
] & 0xfffff || L
[2+_1
])
79 if ((L
[_0
] ^ L
[2+_0
]) & 0x80000000L
)
80 goto nanret
; /* Infinity - Infinity */
84 if (L
[_0
] & 0x80000000L
)
86 return strcp(b
, "Infinity");
88 if ((L
[2+_0
] & 0x7ff00000) == 0x7ff00000) {
90 if (L
[_0
] & 0xfffff || L
[_1
])
94 if (dval(&dd
[0]) + dval(&dd
[1]) == 0.) {
96 #ifndef IGNORE_ZERO_SIGN
97 if (L
[_0
] & L
[2+_0
] & 0x80000000L
)
104 if ((L
[_0
] & 0x7ff00000L
) < (L
[2+_0
] & 0x7ff00000L
)) {
105 dval(&ddx
[1]) = dval(&dd
[0]);
106 dval(&ddx
[0]) = dval(&dd
[1]);
110 z
= d2b(dval(&dd
[0]), &ex
, &bx
);
113 if (dval(&dd
[1]) == 0.)
116 y
= d2b(dval(&dd
[1]), &ey
, &by
);
119 if ( (i
= ex
- ey
) !=0) {
132 if ((L
[_0
] ^ L
[2+_0
]) & 0x80000000L
) {
136 if (L
[_0
] & 0x80000000L
)
137 z
->sign
= 1 - z
->sign
;
143 if (L
[_0
] & 0x80000000L
)
150 for(i
= 0; !*zx
; zx
++)
157 fpi
.nbits
= z
->wds
* 32 - hi0bits(z
->x
[j
= z
->wds
-1]);
158 if (fpi
.nbits
< 106) {
161 for(i
= 0; i
<= j
; i
++)
170 if (bufsize
< (int)(fpi
.nbits
* .301029995664) + 10) {
176 fpi
.emin
= 1-1023-53+1;
177 fpi
.emax
= 2046-1023-106+1;
178 fpi
.rounding
= Rounding
;
179 fpi
.sudden_underflow
= 0;
181 s
= gdtoa(&fpi
, ex
, bits
, &i
, mode
, ndig
, &decpt
, &se
);
184 b
= g__fmt(buf
, s
, se
, decpt
, z
->sign
, bufsize
);