Jens Axboe | 1ec3d69 | 2010-09-16 09:35:34 +0200 | [diff] [blame] | 1 | #include <stdlib.h> |
| 2 | #include <stdio.h> |
| 3 | #include <string.h> |
| 4 | |
Jens Axboe | 10aa136 | 2014-04-01 21:10:36 -0600 | [diff] [blame] | 5 | #include "../fio.h" |
| 6 | |
Jens Axboe | b7e147d | 2014-04-14 10:21:11 -0600 | [diff] [blame] | 7 | #define ARRAY_LENGTH(arr) sizeof(arr) / sizeof((arr)[0]) |
| 8 | |
Jens Axboe | 1ec3d69 | 2010-09-16 09:35:34 +0200 | [diff] [blame] | 9 | /* |
| 10 | * Cheesy number->string conversion, complete with carry rounding error. |
| 11 | */ |
Steven Noonan | 73798eb | 2013-04-05 16:57:38 -0700 | [diff] [blame] | 12 | char *num2str(unsigned long num, int maxlen, int base, int pow2, int unit_base) |
Jens Axboe | 1ec3d69 | 2010-09-16 09:35:34 +0200 | [diff] [blame] | 13 | { |
Steven Noonan | 73798eb | 2013-04-05 16:57:38 -0700 | [diff] [blame] | 14 | const char *postfix[] = { "", "K", "M", "G", "P", "E" }; |
| 15 | const char *byte_postfix[] = { "", "B", "bit" }; |
| 16 | const unsigned int thousand[] = { 1000, 1024 }; |
Jens Axboe | 1ec3d69 | 2010-09-16 09:35:34 +0200 | [diff] [blame] | 17 | unsigned int modulo, decimals; |
Steven Noonan | 73798eb | 2013-04-05 16:57:38 -0700 | [diff] [blame] | 18 | int byte_post_index = 0, post_index, carry = 0; |
Jens Axboe | 0546381 | 2010-09-16 14:56:23 +0200 | [diff] [blame] | 19 | char tmp[32]; |
Jens Axboe | 1ec3d69 | 2010-09-16 09:35:34 +0200 | [diff] [blame] | 20 | char *buf; |
| 21 | |
| 22 | buf = malloc(128); |
| 23 | |
| 24 | for (post_index = 0; base > 1; post_index++) |
| 25 | base /= thousand[!!pow2]; |
| 26 | |
Steven Noonan | 73798eb | 2013-04-05 16:57:38 -0700 | [diff] [blame] | 27 | switch (unit_base) { |
| 28 | case 1: |
| 29 | byte_post_index = 2; |
| 30 | num *= 8; |
| 31 | break; |
| 32 | case 8: |
| 33 | byte_post_index = 1; |
| 34 | break; |
| 35 | } |
| 36 | |
Jens Axboe | 1ec3d69 | 2010-09-16 09:35:34 +0200 | [diff] [blame] | 37 | modulo = -1U; |
| 38 | while (post_index < sizeof(postfix)) { |
| 39 | sprintf(tmp, "%lu", num); |
| 40 | if (strlen(tmp) <= maxlen) |
| 41 | break; |
| 42 | |
| 43 | modulo = num % thousand[!!pow2]; |
| 44 | num /= thousand[!!pow2]; |
Jens Axboe | 0546381 | 2010-09-16 14:56:23 +0200 | [diff] [blame] | 45 | carry = modulo >= thousand[!!pow2] / 2; |
Jens Axboe | 1ec3d69 | 2010-09-16 09:35:34 +0200 | [diff] [blame] | 46 | post_index++; |
| 47 | } |
| 48 | |
| 49 | if (modulo == -1U) { |
| 50 | done: |
Jens Axboe | b7e147d | 2014-04-14 10:21:11 -0600 | [diff] [blame] | 51 | if (post_index >= ARRAY_LENGTH(postfix)) |
| 52 | post_index = 0; |
| 53 | |
Steven Noonan | 73798eb | 2013-04-05 16:57:38 -0700 | [diff] [blame] | 54 | sprintf(buf, "%lu%s%s", num, postfix[post_index], |
| 55 | byte_postfix[byte_post_index]); |
Jens Axboe | 1ec3d69 | 2010-09-16 09:35:34 +0200 | [diff] [blame] | 56 | return buf; |
| 57 | } |
| 58 | |
| 59 | sprintf(tmp, "%lu", num); |
| 60 | decimals = maxlen - strlen(tmp); |
Jens Axboe | 0546381 | 2010-09-16 14:56:23 +0200 | [diff] [blame] | 61 | if (decimals <= 1) { |
| 62 | if (carry) |
| 63 | num++; |
Jens Axboe | 1ec3d69 | 2010-09-16 09:35:34 +0200 | [diff] [blame] | 64 | goto done; |
Jens Axboe | 0546381 | 2010-09-16 14:56:23 +0200 | [diff] [blame] | 65 | } |
Jens Axboe | 1ec3d69 | 2010-09-16 09:35:34 +0200 | [diff] [blame] | 66 | |
Jens Axboe | 0546381 | 2010-09-16 14:56:23 +0200 | [diff] [blame] | 67 | do { |
| 68 | sprintf(tmp, "%u", modulo); |
| 69 | if (strlen(tmp) <= decimals - 1) |
| 70 | break; |
| 71 | |
| 72 | modulo = (modulo + 9) / 10; |
| 73 | } while (1); |
| 74 | |
Steven Noonan | 73798eb | 2013-04-05 16:57:38 -0700 | [diff] [blame] | 75 | sprintf(buf, "%lu.%u%s%s", num, modulo, postfix[post_index], |
| 76 | byte_postfix[byte_post_index]); |
Jens Axboe | 1ec3d69 | 2010-09-16 09:35:34 +0200 | [diff] [blame] | 77 | return buf; |
| 78 | } |