freebsd-skq/usr.sbin/xntpd/lib/dolfptoa.c

162 lines
2.7 KiB
C
Raw Normal View History

1994-09-29 23:04:24 +00:00
/*
1993-12-21 18:36:48 +00:00
* dolfptoa - do the grunge work of converting an l_fp number to decimal
*/
#include <stdio.h>
#include "ntp_fp.h"
#include "lib_strbuf.h"
#include "ntp_string.h"
#include "ntp_stdlib.h"
char *
dolfptoa(fpi, fpv, neg, ndec, msec)
1994-09-29 23:04:24 +00:00
u_long fpi;
u_long fpv;
1993-12-21 18:36:48 +00:00
int neg;
int ndec;
int msec;
{
register u_char *cp, *cpend;
1994-09-29 23:04:24 +00:00
register u_long lwork;
1993-12-21 18:36:48 +00:00
register int dec;
u_char cbuf[24];
u_char *cpdec;
char *buf;
char *bp;
/*
* Get a string buffer before starting
*/
LIB_GETBUF(buf);
/*
* Zero the character buffer
*/
1994-02-03 22:09:07 +00:00
memset((char *) cbuf, 0, sizeof(cbuf));
1993-12-21 18:36:48 +00:00
/*
* Work on the integral part. This is biased by what I know
* compiles fairly well for a 68000.
*/
cp = cpend = &cbuf[10];
1994-09-29 23:04:24 +00:00
lwork = fpi;
if (lwork & 0xffff0000) {
register u_long lten = 10;
register u_long ltmp;
1993-12-21 18:36:48 +00:00
do {
1994-09-29 23:04:24 +00:00
ltmp = lwork;
lwork /= lten;
ltmp -= (lwork << 3) + (lwork << 1);
1993-12-21 18:36:48 +00:00
*--cp = (u_char)ltmp;
1994-09-29 23:04:24 +00:00
} while (lwork & 0xffff0000);
1993-12-21 18:36:48 +00:00
}
1994-09-29 23:04:24 +00:00
if (lwork != 0) {
1993-12-21 18:36:48 +00:00
register u_short sten = 10;
register u_short stmp;
1994-09-29 23:04:24 +00:00
register u_short swork = (u_short)lwork;
1993-12-21 18:36:48 +00:00
do {
stmp = swork;
swork /= sten;
stmp -= (swork<<3) + (swork<<1);
*--cp = (u_char)stmp;
} while (swork != 0);
}
/*
* Done that, now deal with the problem of the fraction. First
* determine the number of decimal places.
*/
if (msec) {
dec = ndec + 3;
if (dec < 3)
dec = 3;
cpdec = &cbuf[13];
} else {
dec = ndec;
if (dec < 0)
dec = 0;
cpdec = &cbuf[10];
}
if (dec > 12)
dec = 12;
/*
* If there's a fraction to deal with, do so.
*/
if (fpv != 0) {
1994-09-29 23:04:24 +00:00
l_fp work;
1993-12-21 18:36:48 +00:00
1994-09-29 23:04:24 +00:00
work.l_ui = 0;
work.l_uf = fpv;
1993-12-21 18:36:48 +00:00
while (dec > 0) {
1994-09-29 23:04:24 +00:00
l_fp ftmp;
1993-12-21 18:36:48 +00:00
dec--;
/*
* The scheme here is to multiply the
* fraction (0.1234...) by ten. This moves
* a junk of BCD into the units part.
* record that and iterate.
*/
1994-09-29 23:04:24 +00:00
work.l_ui = 0;
L_LSHIFT(&work);
ftmp = work;
L_LSHIFT(&work);
L_LSHIFT(&work);
L_ADD(&work, &ftmp);
*cpend++ = (u_char)work.l_ui;
if (work.l_uf == 0)
1993-12-21 18:36:48 +00:00
break;
}
/*
* Rounding is rotten
*/
1994-09-29 23:04:24 +00:00
if (work.l_uf & 0x80000000) {
1993-12-21 18:36:48 +00:00
register u_char *tp = cpend;
*(--tp) += 1;
while (*tp >= 10) {
*tp = 0;
*(--tp) += 1;
};
if (tp < cp)
cp = tp;
}
}
cpend += dec;
/*
* We've now got the fraction in cbuf[], with cp pointing at
* the first character, cpend pointing past the last, and
* cpdec pointing at the first character past the decimal.
* Remove leading zeros, then format the number into the
* buffer.
*/
while (cp < cpdec) {
if (*cp != 0)
break;
cp++;
}
if (cp == cpdec)
--cp;
bp = buf;
if (neg)
*bp++ = '-';
while (cp < cpend) {
if (cp == cpdec)
*bp++ = '.';
*bp++ = (char)(*cp++ + '0'); /* ascii dependent? */
}
*bp = '\0';
/*
* Done!
*/
return buf;
}