1 /* $NetBSD: dolfptoa.c,v 1.2 2003/12/04 16:23:36 drochner Exp $ */
4 * dolfptoa - do the grunge work of converting an l_fp number to decimal
9 #include "lib_strbuf.h"
10 #include "ntp_string.h"
11 #include "ntp_stdlib.h"
22 register u_char
*cp
, *cpend
;
23 register u_long lwork
;
31 * Get a string buffer before starting
36 * Zero the character buffer
38 memset((char *) cbuf
, 0, sizeof(cbuf
));
41 * safeguard against sign extensions and other mishaps on 64 bit platforms
42 * the code following is designed for and only for 32-bit inputs and
43 * only 32-bit worth of input are supplied.
49 * Work on the integral part. This is biased by what I know
50 * compiles fairly well for a 68000.
52 cp
= cpend
= &cbuf
[10];
54 if (lwork
& 0xffff0000) {
55 register u_long lten
= 10;
61 ltmp
-= (lwork
<< 3) + (lwork
<< 1);
62 if (cp
< cbuf
) abort(); /* rather die a horrible death than trash the memory */
64 } while (lwork
& 0xffff0000);
67 register u_short sten
= 10;
68 register u_short stmp
;
69 register u_short swork
= (u_short
)lwork
;
73 swork
= (u_short
) (swork
/sten
);
74 stmp
= (u_short
)(stmp
- ((swork
<<3) + (swork
<<1)));
75 if (cp
< cbuf
) abort(); /* rather die a horrible death than trash the memory */
81 * Done that, now deal with the problem of the fraction. First
82 * determine the number of decimal places.
99 * If there's a fraction to deal with, do so.
111 * The scheme here is to multiply the
112 * fraction (0.1234...) by ten. This moves
113 * a junk of BCD into the units part.
114 * record that and iterate.
122 *cpend
++ = (u_char
)work
.l_ui
;
125 if (cpend
> (cbuf
+ sizeof(cbuf
))) abort(); /* rather die a horrible death than trash the memory */
131 if (work
.l_uf
& 0x80000000) {
132 register u_char
*tp
= cpend
;
147 * We've now got the fraction in cbuf[], with cp pointing at
148 * the first character, cpend pointing past the last, and
149 * cpdec pointing at the first character past the decimal.
150 * Remove leading zeros, then format the number into the
167 *bp
++ = (char)(*cp
++ + '0'); /* ascii dependent? */