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