]> git.sesse.net Git - bcachefs-tools-debian/blob - c_src/linux/string_helpers.c
rust: bump rpassword to v7.x
[bcachefs-tools-debian] / c_src / linux / string_helpers.c
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Helpers for formatting and printing strings
4  *
5  * Copyright 31 August 2008 James Bottomley
6  * Copyright (C) 2013, Intel Corporation
7  */
8 #include <linux/bug.h>
9 #include <linux/kernel.h>
10 #include <linux/math64.h>
11 #include <linux/export.h>
12 #include <linux/ctype.h>
13 #include <linux/device.h>
14 #include <linux/errno.h>
15 #include <linux/fs.h>
16 #include <linux/limits.h>
17 #include <linux/slab.h>
18 #include <linux/string.h>
19 #include <linux/string_helpers.h>
20
21 /**
22  * string_get_size - get the size in the specified units
23  * @size:       The size to be converted in blocks
24  * @blk_size:   Size of the block (use 1 for size in bytes)
25  * @units:      units to use (powers of 1000 or 1024)
26  * @buf:        buffer to format to
27  * @len:        length of buffer
28  *
29  * This function returns a string formatted to 3 significant figures
30  * giving the size in the required units.  @buf should have room for
31  * at least 9 bytes and will always be zero terminated.
32  *
33  */
34 int string_get_size(u64 size, u64 blk_size, const enum string_size_units units,
35                     char *buf, int len)
36 {
37         static const char *const units_10[] = {
38                 "B", "kB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"
39         };
40         static const char *const units_2[] = {
41                 "B", "KiB", "MiB", "GiB", "TiB", "PiB", "EiB", "ZiB", "YiB"
42         };
43         static const char *const *const units_str[] = {
44                 [STRING_UNITS_10] = units_10,
45                 [STRING_UNITS_2] = units_2,
46         };
47         static const unsigned int divisor[] = {
48                 [STRING_UNITS_10] = 1000,
49                 [STRING_UNITS_2] = 1024,
50         };
51         static const unsigned int rounding[] = { 500, 50, 5 };
52         int i = 0, j;
53         u32 remainder = 0, sf_cap;
54         char tmp[12];
55         const char *unit;
56
57         tmp[0] = '\0';
58
59         if (blk_size == 0)
60                 size = 0;
61         if (size == 0)
62                 goto out;
63
64         /* This is Napier's algorithm.  Reduce the original block size to
65          *
66          * coefficient * divisor[units]^i
67          *
68          * we do the reduction so both coefficients are just under 32 bits so
69          * that multiplying them together won't overflow 64 bits and we keep
70          * as much precision as possible in the numbers.
71          *
72          * Note: it's safe to throw away the remainders here because all the
73          * precision is in the coefficients.
74          */
75         while (blk_size >> 32) {
76                 do_div(blk_size, divisor[units]);
77                 i++;
78         }
79
80         while (size >> 32) {
81                 do_div(size, divisor[units]);
82                 i++;
83         }
84
85         /* now perform the actual multiplication keeping i as the sum of the
86          * two logarithms */
87         size *= blk_size;
88
89         /* and logarithmically reduce it until it's just under the divisor */
90         while (size >= divisor[units]) {
91                 remainder = do_div(size, divisor[units]);
92                 i++;
93         }
94
95         /* work out in j how many digits of precision we need from the
96          * remainder */
97         sf_cap = size;
98         for (j = 0; sf_cap*10 < 1000; j++)
99                 sf_cap *= 10;
100
101         if (units == STRING_UNITS_2) {
102                 /* express the remainder as a decimal.  It's currently the
103                  * numerator of a fraction whose denominator is
104                  * divisor[units], which is 1 << 10 for STRING_UNITS_2 */
105                 remainder *= 1000;
106                 remainder >>= 10;
107         }
108
109         /* add a 5 to the digit below what will be printed to ensure
110          * an arithmetical round up and carry it through to size */
111         remainder += rounding[j];
112         if (remainder >= 1000) {
113                 remainder -= 1000;
114                 size += 1;
115         }
116
117         if (j) {
118                 snprintf(tmp, sizeof(tmp), ".%03u", remainder);
119                 tmp[j+1] = '\0';
120         }
121
122  out:
123         if (i >= ARRAY_SIZE(units_2))
124                 unit = "UNK";
125         else
126                 unit = units_str[units][i];
127
128         return snprintf(buf, len, "%u%s %s", (u32)size, tmp, unit);
129 }
130 EXPORT_SYMBOL(string_get_size);