7 * These routines assume that if the number is representable
8 * in IEEE floating point, it will be representable in the native
9 * double format. Naive but workable, probably.
12 ieeeftoa64(char *buf, uint n, u32int h, u32int l)
28 return snprint(buf, n, "0.");
29 exp = (h>>20) & ((1L<<11)-1L);
31 return snprint(buf, n, "DeN(%.8lux%.8lux)", h, l);
32 if(exp == ((1L<<11)-1L)){
33 if(l==0 && (h&((1L<<20)-1L)) == 0)
34 return snprint(buf, n, "Inf");
36 return snprint(buf, n, "NaN(%.8lux%.8lux)", h&((1L<<20)-1L), l);
39 fr = l & ((1L<<16)-1L);
41 fr += (l>>16) & ((1L<<16)-1L);
43 fr += (h & (1L<<20)-1L) | (1L<<20);
46 return snprint(buf, n, "%.18g", fr);
50 ieeeftoa32(char *buf, uint n, u32int h)
65 return snprint(buf, n, "0.");
66 exp = (h>>23) & ((1L<<8)-1L);
68 return snprint(buf, n, "DeN(%.8lux)", h);
69 if(exp == ((1L<<8)-1L)){
70 if((h&((1L<<23)-1L)) == 0)
71 return snprint(buf, n, "Inf");
73 return snprint(buf, n, "NaN(%.8lux)", h&((1L<<23)-1L));
76 fr = (h & ((1L<<23)-1L)) | (1L<<23);
79 return snprint(buf, n, "%.9g", fr);
83 beieeeftoa32(char *buf, uint n, void *s)
85 return ieeeftoa32(buf, n, beswap4(*(u32int*)s));
89 beieeeftoa64(char *buf, uint n, void *s)
91 return ieeeftoa64(buf, n, beswap4(*(u32int*)s), beswap4(((u32int*)(s))[1]));
95 leieeeftoa32(char *buf, uint n, void *s)
97 return ieeeftoa32(buf, n, leswap4(*(u32int*)s));
101 leieeeftoa64(char *buf, uint n, void *s)
103 return ieeeftoa64(buf, n, leswap4(((u32int*)(s))[1]), leswap4(*(u32int*)s));
106 /* packed in 12 bytes, with s[2]==s[3]==0; mantissa starts at s[4]*/
108 beieeeftoa80(char *buf, uint n, void *s)
110 uchar *reg = (uchar*)s;
113 uchar ieee[8+8]; /* room for slop */
116 memset(ieee, 0, sizeof(ieee));
122 x = ((reg[0]&0x7F)<<8) | reg[1];
123 if(x == 0) /* number is ±0 */
126 if(memcmp(reg+4, ieee+1, 8) == 0){ /* infinity */
130 ieee[7] = 0x1; /* make sure */
133 ieee[1] |= (x&0xF)<<4;
136 x -= 0x3FFF; /* exponent bias */
138 if(x >= (1<<11) || ((reg[4]&0x80)==0 && x!=0))
139 return snprint(buf, n, "not in range");
141 ieee[1] |= (x&0xF)<<4;
146 for(i=0; i<56; i+=8, p++, q++){ /* move one byte */
147 x = (p[0]&0x7F) << 1;
154 return beieeeftoa64(buf, n, (void*)ieee);
159 leieeeftoa80(char *buf, uint n, void *s)
168 return beieeeftoa80(buf, n, b);