Changeset View
Changeset View
Standalone View
Standalone View
head/contrib/ntp/libntp/dolfptoa.c
Show All 34 Lines | dolfptoa( | ||||
/* | /* | ||||
* Work on the integral part. This should work reasonable on | * Work on the integral part. This should work reasonable on | ||||
* all machines with 32 bit arithmetic. Please note that 32 bits | * all machines with 32 bit arithmetic. Please note that 32 bits | ||||
* can *always* be represented with at most 10 decimal digits, | * can *always* be represented with at most 10 decimal digits, | ||||
* including a possible rounding from the fractional part. | * including a possible rounding from the fractional part. | ||||
*/ | */ | ||||
cp = cpend = cpdec = &cbuf[10]; | cp = cpend = cpdec = &cbuf[10]; | ||||
for (dec = cp - cbuf; dec > 0 && fpi != 0; dec--) { | for (dec = (int)(cp - cbuf); dec > 0 && fpi != 0; dec--) { | ||||
/* can add another digit */ | /* can add another digit */ | ||||
u_int32 digit; | u_int32 digit; | ||||
digit = fpi; | digit = fpi; | ||||
fpi /= 10U; | fpi /= 10U; | ||||
digit -= (fpi << 3) + (fpi << 1); /* i*10 */ | digit -= (fpi << 3) + (fpi << 1); /* i*10 */ | ||||
*--cp = (u_char)digit; | *--cp = (u_char)digit; | ||||
} | } | ||||
/* | /* | ||||
* Done that, now deal with the problem of the fraction. First | * Done that, now deal with the problem of the fraction. First | ||||
* determine the number of decimal places. | * determine the number of decimal places. | ||||
*/ | */ | ||||
dec = ndec; | dec = ndec; | ||||
if (dec < 0) | if (dec < 0) | ||||
dec = 0; | dec = 0; | ||||
if (msec) { | if (msec) { | ||||
dec += 3; | dec += 3; | ||||
cpdec += 3; | cpdec += 3; | ||||
} | } | ||||
if ((size_t)dec > sizeof(cbuf) - (cpend - cbuf)) | if ((size_t)dec > sizeof(cbuf) - (cpend - cbuf)) | ||||
dec = sizeof(cbuf) - (cpend - cbuf); | dec = (int)(sizeof(cbuf) - (cpend - cbuf)); | ||||
/* | /* | ||||
* If there's a fraction to deal with, do so. | * If there's a fraction to deal with, do so. | ||||
*/ | */ | ||||
for (/*NOP*/; dec > 0 && fpv != 0; dec--) { | for (/*NOP*/; dec > 0 && fpv != 0; dec--) { | ||||
u_int32 digit, tmph, tmpl; | u_int32 digit, tmph, tmpl; | ||||
/* | /* | ||||
Show All 16 Lines | dolfptoa( | ||||
if (dec > 0) { | if (dec > 0) { | ||||
/* only '0' digits left -- just reposition end */ | /* only '0' digits left -- just reposition end */ | ||||
cpend += dec; | cpend += dec; | ||||
} else { | } else { | ||||
/* some bits remain in 'fpv'; do round */ | /* some bits remain in 'fpv'; do round */ | ||||
u_char *tp = cpend; | u_char *tp = cpend; | ||||
int carry = ((fpv & 0x80000000) != 0); | int carry = ((fpv & 0x80000000) != 0); | ||||
for (dec = tp - cbuf; carry && dec > 0; dec--) { | for (dec = (int)(tp - cbuf); carry && dec > 0; dec--) { | ||||
*--tp += 1; | *--tp += 1; | ||||
if (*tp == 10) | if (*tp == 10) | ||||
*tp = 0; | *tp = 0; | ||||
else | else | ||||
carry = FALSE; | carry = FALSE; | ||||
} | } | ||||
if (tp < cp) /* rounding from 999 to 1000 or similiar? */ | if (tp < cp) /* rounding from 999 to 1000 or similiar? */ | ||||
▲ Show 20 Lines • Show All 68 Lines • Show Last 20 Lines |