summaryrefslogtreecommitdiffstats
path: root/util/suffix.c
blob: f010f3b6a9c46629ef1a36aea6782fed9ae9db61 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
// SPDX-License-Identifier: GPL-2.0-or-later
/*
 *
 * Copyright 2014 PMC-Sierra, Inc.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 *
 */

/*
 *
 *   Author: Logan Gunthorpe
 *
 *   Date:   Oct 23 2014
 *
 *   Description:
 *     Functions for dealing with number suffixes
 *
 */

#include "suffix.h"
#include "common.h"

#include <stdlib.h>
#include <ctype.h>
#include <errno.h>
#include <math.h>
#include <float.h>
#include <limits.h>
#include <locale.h>
#include <stdio.h>

static struct si_suffix {
	long double magnitude;
	unsigned int exponent;
	const char *suffix;
} si_suffixes[] = {
	{1e30, 30, "Q"},
	{1e27, 27, "R"},
	{1e24, 24, "Y"},
	{1e21, 21, "Z"},
	{1e18, 18, "E"},
	{1e15, 15, "P"},
	{1e12, 12, "T"},
	{1e9, 9, "G"},
	{1e6, 6, "M"},
	{1e3, 3, "k"},
};

const char *suffix_si_get(double *value)
{
	long double value_ld = *value;
	const char *suffix = suffix_si_get_ld(&value_ld);

	*value = value_ld;

	return suffix;
}

static bool suffix_si_check(const char val)
{
	int i;
	struct si_suffix *s;

	for (i = 0; i < ARRAY_SIZE(si_suffixes); i++) {
		s = &si_suffixes[i];

		if (val == *s->suffix)
			return true;
	}

	return false;
}

int suffix_si_parse(const char *str, char **endptr, uint64_t *val)
{
	unsigned long long num, frac = 0;
	char *sep, *tmp;
	int frac_len = 0, len, i;

	num = strtoull(str, endptr, 0);
	if (str == *endptr ||
	    ((num == ULLONG_MAX) && errno == ERANGE))
		return -EINVAL;

	/* simple number, no decimal point not suffix */
	if ((*endptr)[0] == '\0') {
		*val = num;
		return 0;
	}

	/* get rid of the decimal point */
	sep = localeconv()->decimal_point;
	if (sep)
		len = strlen(sep);
	else
		len = 0;

	for (i = 0; i < len; i++) {
		if (suffix_si_check((*endptr)[i]))
			break;
		if (((*endptr)[i] == '\0') || (*endptr)[i] != sep[i])
			return -EINVAL;
	}

	if (suffix_si_check((*endptr)[i])) {
		if ((*endptr)[i + 1] != '\0')
			return -EINVAL;
	} else {
		*endptr += len;
		tmp = *endptr;

		/* extract the digits after decimal point */
		frac = strtoull(tmp, endptr, 0);
		if (tmp == *endptr ||
		    ((frac == ULLONG_MAX) && errno == ERANGE))
			return -EINVAL;

		/* test that we have max one character as suffix */
		if ((*endptr)[0] != '\0' && (*endptr)[1] != '\0')
			return -EINVAL;

		frac_len = *endptr - tmp;
	}

	for (i = 0; i < ARRAY_SIZE(si_suffixes); i++) {
		struct si_suffix *s = &si_suffixes[i];

		if ((*endptr)[0] != s->suffix[0])
			continue;

		/* we should check for overflow */
		for (int j = 0; j < s->exponent; j++)
			num *= 10;

		if (s->exponent > frac_len) {
			for (int j = 0; j < s->exponent - frac_len;  j++)
				frac *= 10;
		} else if (s->exponent < frac_len) {
			for (int j = 0; j < frac_len - s->exponent;  j++)
				frac /= 10;
		} else {
			frac = 0;
		}

		*val = num + frac;
		return 0;
	}

	if ((*endptr)[0] != '\0')
		return -EINVAL;

	*val = num;
	return 0;
}

const char *suffix_si_get_ld(long double *value)
{
	int i;

	for (i = 0; i < ARRAY_SIZE(si_suffixes); i++) {
		struct si_suffix *s = &si_suffixes[i];

		if (*value >= s->magnitude) {
			*value /= s->magnitude;
			return s->suffix;
		}
	}

	return "";
}

static struct binary_suffix {
	int shift;
	const char *suffix;
} binary_suffixes[] = {
	{50, "Pi"},
	{40, "Ti"},
	{30, "Gi"},
	{20, "Mi"},
	{10, "Ki"},
};

const char *suffix_binary_get(long long *value)
{
	int i;

	for (i = 0; i < ARRAY_SIZE(binary_suffixes); i++) {
		struct binary_suffix *s = &binary_suffixes[i];

		if (llabs(*value) >= (1LL << s->shift)) {
			*value =
			    (*value + (1LL << (s->shift - 1))) / (1LL << s->shift);
			return s->suffix;
		}
	}

	return "";
}

const char *suffix_dbinary_get(double *value)
{
	int i;

	for (i = 0; i < ARRAY_SIZE(binary_suffixes); i++) {
		struct binary_suffix *s = &binary_suffixes[i];

		if (fabs(*value) >= (1LL << s->shift)) {
			*value = *value / (1LL << s->shift);
			return s->suffix;
		}
	}

	return "";
}

int suffix_binary_parse(const char *str, char **endptr, uint64_t *val)
{
	uint64_t ret;
	int i;

	ret = strtoull(str, endptr, 0);
	if (str == *endptr ||
	    ((ret == ULLONG_MAX) && errno == ERANGE))
		return -EINVAL;

	if (str == *endptr) {
		*val = ret;
		return 0;
	}

	/* simple number, no decimal point, no suffix */
	if ((*endptr)[0] == '\0') {
		*val = ret;
		return 0;
	}

	for (i = 0; i < ARRAY_SIZE(binary_suffixes); i++) {
		struct binary_suffix *s = &binary_suffixes[i];

		if (tolower((*endptr)[0]) == tolower(s->suffix[0]) &&
		    (s->suffix[0] != '\0' &&
		     (((*endptr)[0] != '\0' &&
		       (*endptr)[1] != '\0' &&
		       (*endptr)[2] == '\0') &&
		      (tolower((*endptr)[1]) == tolower(s->suffix[1]))))) {
			ret <<= s->shift;
			*val = ret;
			return 0;
		}
	}

	return -EINVAL;
}