| 22 | } |
| 23 | |
| 24 | void |
| 25 | timer_ratio(timedelta_t *a, timedelta_t *b, char *buf, size_t buflen) { |
| 26 | uint64_t t0 = timer_usec(a); |
| 27 | uint64_t t1 = timer_usec(b); |
| 28 | uint64_t mult; |
| 29 | size_t i = 0; |
| 30 | size_t j, n; |
| 31 | |
| 32 | /* Whole. */ |
| 33 | n = malloc_snprintf(&buf[i], buflen-i, "%"FMTu64, t0 / t1); |
| 34 | i += n; |
| 35 | if (i >= buflen) { |
| 36 | return; |
| 37 | } |
| 38 | mult = 1; |
| 39 | for (j = 0; j < n; j++) { |
| 40 | mult *= 10; |
| 41 | } |
| 42 | |
| 43 | /* Decimal. */ |
| 44 | n = malloc_snprintf(&buf[i], buflen-i, "."); |
| 45 | i += n; |
| 46 | |
| 47 | /* Fraction. */ |
| 48 | while (i < buflen-1) { |
| 49 | uint64_t round = (i+1 == buflen-1 && ((t0 * mult * 10 / t1) % 10 |
| 50 | >= 5)) ? 1 : 0; |
| 51 | n = malloc_snprintf(&buf[i], buflen-i, |
| 52 | "%"FMTu64, (t0 * mult / t1) % 10 + round); |
| 53 | i += n; |
| 54 | mult *= 10; |
| 55 | } |
| 56 | } |
no test coverage detected