Skip to content

Commit

Permalink
Using the general-case logic for precision==0 in `get_normalized_comp…
Browse files Browse the repository at this point in the history
…onents`. Code size win :-)
  • Loading branch information
eyalroz committed Feb 21, 2022
1 parent 99aeb57 commit 5d0c680
Showing 1 changed file with 15 additions and 22 deletions.
37 changes: 15 additions & 22 deletions src/printf/printf.c
Original file line number Diff line number Diff line change
Expand Up @@ -661,29 +661,22 @@ static struct double_components get_normalized_components(bool negative, printf_
double scaled_remainder = apply_scaling(remainder, account_for_precision);
double rounding_threshold = 0.5;

if (precision == 0U) {
components.fractional = 0;
components.integral += (scaled_remainder >= rounding_threshold);
if (scaled_remainder == rounding_threshold) {
// banker's rounding: Round towards the even number (making the mean error 0)
components.integral &= ~((int_fast64_t) 0x1);
}
}
else {
components.fractional = (int_fast64_t) scaled_remainder;
scaled_remainder -= (double) components.fractional;
components.fractional = (int_fast64_t) scaled_remainder; // when precision == 0, the assigned value should be 0
scaled_remainder -= (double) components.fractional; //when precision == 0, this will not change scaled_remainder

components.fractional += (scaled_remainder >= rounding_threshold);
if (scaled_remainder == rounding_threshold) {
// banker's rounding: Round towards the even number (making the mean error 0)
components.fractional &= ~((int_fast64_t) 0x1);
}
// handle rollover, e.g. the case of 0.99 with precision 1 becoming (0,100),
// and must then be corrected into (1, 0).
if ((double) components.fractional >= prec_power_of_10) {
components.fractional = 0;
++components.integral;
}
components.fractional += (scaled_remainder >= rounding_threshold);
if (scaled_remainder == rounding_threshold) {
// banker's rounding: Round towards the even number (making the mean error 0)
components.fractional &= ~((int_fast64_t) 0x1);
}
// handle rollover, e.g. the case of 0.99 with precision 1 becoming (0,100),
// and must then be corrected into (1, 0).
// Note: for precision = 0, this will "translate" the rounding effect from
// the fractional part to the integral part where it should actually be
// felt (as prec_power_of_10 is 1)
if ((double) components.fractional >= prec_power_of_10) {
components.fractional = 0;
++components.integral;
}
return components;
}
Expand Down

0 comments on commit 5d0c680

Please sign in to comment.