Changeset 3bacee1 in mainline for uspace/lib/c/generic/double_to_str.c
- Timestamp:
- 2018-04-12T16:27:17Z (7 years ago)
- Branches:
- lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
- Children:
- 3cf22f9
- Parents:
- 76d0981d
- git-author:
- Jiri Svoboda <jiri@…> (2018-04-11 19:25:33)
- git-committer:
- Jiri Svoboda <jiri@…> (2018-04-12 16:27:17)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/lib/c/generic/double_to_str.c
r76d0981d r3bacee1 56 56 static bool is_normalized(fp_num_t num) 57 57 { 58 assert(8 *sizeof(num.significand) == significand_width);58 assert(8 * sizeof(num.significand) == significand_width); 59 59 60 60 /* Normalized == most significant bit of the significand is set. */ … … 147 147 /** Returns the interval [low, high] of numbers that convert to binary val. */ 148 148 static void get_normalized_bounds(ieee_double_t val, fp_num_t *high, 149 149 fp_num_t *low, fp_num_t *val_dist) 150 150 { 151 151 /* … … 181 181 182 182 val_dist->significand = 183 183 val_dist->significand << (val_dist->exponent - high->exponent); 184 184 val_dist->exponent = high->exponent; 185 185 } … … 198 198 */ 199 199 static void calc_scaled_bounds(ieee_double_t val, fp_num_t *scaled_upper_bound, 200 200 fp_num_t *bounds_delta, fp_num_t *val_dist, int *scale) 201 201 { 202 202 fp_num_t upper_bound, lower_bound; … … 251 251 /** Rounds the last digit of buf so that it is closest to the converted number.*/ 252 252 static void round_last_digit(uint64_t rest, uint64_t w_dist, uint64_t delta, 253 253 uint64_t digit_val_diff, char *buf, int len) 254 254 { 255 255 /* … … 277 277 bool next_in_val_rng = cur_greater_w && (rest + digit_val_diff < delta); 278 278 /* Rounding down by one would bring buf closer to the processed number. */ 279 bool next_closer = next_in_val_rng 280 &&(rest + digit_val_diff < w_dist || rest - w_dist < w_dist - rest);279 bool next_closer = next_in_val_rng && 280 (rest + digit_val_diff < w_dist || rest - w_dist < w_dist - rest); 281 281 282 282 /* Of the shortest strings pick the one that is closest to the actual … … 291 291 cur_greater_w = rest < w_dist; 292 292 next_in_val_rng = cur_greater_w && (rest + digit_val_diff < delta); 293 next_closer = next_in_val_rng 294 &&(rest + digit_val_diff < w_dist || rest - w_dist < w_dist - rest);293 next_closer = next_in_val_rng && 294 (rest + digit_val_diff < w_dist || rest - w_dist < w_dist - rest); 295 295 } 296 296 } … … 326 326 */ 327 327 static int gen_dec_digits(fp_num_t scaled_upper, fp_num_t delta, 328 328 fp_num_t val_dist, int scale, char *buf, size_t buf_size, int *dec_exponent) 329 329 { 330 330 /* … … 411 411 /* Of the shortest representations choose the numerically closest. */ 412 412 round_last_digit(remainder, val_dist.significand, delta.significand, 413 413 (uint64_t)div << (-one.exponent), buf, len); 414 414 return len; 415 415 } … … 457 457 /* Of the shortest representations choose the numerically closest one. */ 458 458 round_last_digit(frac_part, val_dist.significand, delta.significand, 459 459 one.significand, buf, len); 460 460 461 461 return len; … … 504 504 */ 505 505 int double_to_short_str(ieee_double_t ieee_val, char *buf, size_t buf_size, 506 506 int *dec_exponent) 507 507 { 508 508 /* The whole computation assumes 64bit significand. */ … … 524 524 525 525 calc_scaled_bounds(ieee_val, &scaled_upper_bound, 526 526 &delta, &val_dist, &scale); 527 527 528 528 int len = gen_dec_digits(scaled_upper_bound, delta, val_dist, scale, 529 529 buf, buf_size, dec_exponent); 530 530 531 531 assert(len <= MAX_DOUBLE_STR_LEN); … … 554 554 */ 555 555 static int gen_fixed_dec_digits(fp_num_t w_scaled, int scale, int signif_d_cnt, 556 556 int frac_d_cnt, char *buf, size_t buf_size, int *dec_exponent) 557 557 { 558 558 /* We'll produce at least one digit and a null terminator. */ … … 608 608 int rem_signif_d_cnt = signif_d_cnt; 609 609 int rem_frac_d_cnt = 610 610 (frac_d_cnt >= 0) ? (kappa - scale + frac_d_cnt) : INT_MAX; 611 611 612 612 /* Produce decimal digits for the integral part of w_scaled. */ … … 752 752 */ 753 753 int double_to_fixed_str(ieee_double_t ieee_val, int signif_d_cnt, 754 754 int frac_d_cnt, char *buf, size_t buf_size, int *dec_exponent) 755 755 { 756 756 /* The whole computation assumes 64bit significand. */ … … 780 780 /* Produce decimal digits from the scaled number. */ 781 781 int len = gen_fixed_dec_digits(w_scaled, scale, signif_d_cnt, frac_d_cnt, 782 782 buf, buf_size, dec_exponent); 783 783 784 784 assert(len <= MAX_DOUBLE_STR_LEN);
Note:
See TracChangeset
for help on using the changeset viewer.