2025-02-16 12:16:19 +01:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
2025-02-16 11:09:01 +01:00
|
|
|
/*
|
|
|
|
*
|
|
|
|
* 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 <stdlib.h>
|
|
|
|
#include <ctype.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <math.h>
|
|
|
|
|
|
|
|
static struct si_suffix {
|
2025-02-16 12:20:48 +01:00
|
|
|
long double magnitude;
|
2025-02-16 11:09:01 +01:00
|
|
|
const char *suffix;
|
|
|
|
} si_suffixes[] = {
|
2025-02-16 12:22:16 +01:00
|
|
|
{1e30, "Q"},
|
|
|
|
{1e27, "R"},
|
|
|
|
{1e24, "Y"},
|
|
|
|
{1e21, "Z"},
|
|
|
|
{1e18, "E"},
|
|
|
|
{1e15, "P"},
|
|
|
|
{1e12, "T"},
|
|
|
|
{1e9, "G"},
|
|
|
|
{1e6, "M"},
|
|
|
|
{1e3, "k"},
|
|
|
|
{1e0, ""},
|
|
|
|
{0}
|
2025-02-16 11:09:01 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
const char *suffix_si_get(double *value)
|
2025-02-16 12:20:48 +01:00
|
|
|
{
|
|
|
|
long double value_ld = *value;
|
|
|
|
const char *suffix = suffix_si_get_ld(&value_ld);
|
|
|
|
|
|
|
|
*value = value_ld;
|
|
|
|
|
|
|
|
return suffix;
|
|
|
|
}
|
|
|
|
|
2025-02-16 12:22:16 +01:00
|
|
|
uint64_t suffix_si_parse(const char *value, bool *suffixed)
|
2025-02-16 12:20:48 +01:00
|
|
|
{
|
2025-02-16 12:22:16 +01:00
|
|
|
char *suffix;
|
|
|
|
double ret;
|
|
|
|
struct si_suffix *s;
|
2025-02-16 12:21:44 +01:00
|
|
|
|
2025-02-16 12:22:16 +01:00
|
|
|
errno = 0;
|
|
|
|
ret = strtod(value, &suffix);
|
|
|
|
if (errno)
|
|
|
|
return 0;
|
2025-02-16 12:20:48 +01:00
|
|
|
|
2025-02-16 12:22:16 +01:00
|
|
|
for (s = si_suffixes; s->magnitude != 0; s++) {
|
|
|
|
if (suffix[0] == s->suffix[0]) {
|
|
|
|
if (suffixed && suffix[0] != '\0')
|
|
|
|
*suffixed = true;
|
|
|
|
return ret *= s->magnitude;
|
2025-02-16 12:20:48 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2025-02-16 12:22:16 +01:00
|
|
|
if (suffix[0] != '\0')
|
|
|
|
errno = EINVAL;
|
2025-02-16 12:20:48 +01:00
|
|
|
|
2025-02-16 12:22:16 +01:00
|
|
|
return (uint64_t)ret;
|
2025-02-16 12:20:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
const char *suffix_si_get_ld(long double *value)
|
2025-02-16 11:09:01 +01:00
|
|
|
{
|
2025-02-16 12:22:16 +01:00
|
|
|
struct si_suffix *s;
|
2025-02-16 11:09:01 +01:00
|
|
|
|
2025-02-16 12:22:16 +01:00
|
|
|
for (s = si_suffixes; s->magnitude != 0; s++) {
|
2025-02-16 11:09:01 +01:00
|
|
|
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"},
|
2025-02-16 12:22:16 +01:00
|
|
|
{0, ""}
|
2025-02-16 11:09:01 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
const char *suffix_binary_get(long long *value)
|
|
|
|
{
|
2025-02-16 12:22:16 +01:00
|
|
|
struct binary_suffix *s;
|
2025-02-16 11:09:01 +01:00
|
|
|
|
2025-02-16 12:22:16 +01:00
|
|
|
for (s = binary_suffixes; s->shift != 0; s++) {
|
2025-02-16 11:09:01 +01:00
|
|
|
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)
|
|
|
|
{
|
2025-02-16 12:22:16 +01:00
|
|
|
struct binary_suffix *s;
|
2025-02-16 11:09:01 +01:00
|
|
|
|
2025-02-16 12:22:16 +01:00
|
|
|
for (s = binary_suffixes; s->shift != 0; s++) {
|
2025-02-16 11:09:01 +01:00
|
|
|
if (fabs(*value) >= (1LL << s->shift)) {
|
|
|
|
*value = *value / (1LL << s->shift);
|
|
|
|
return s->suffix;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
2025-02-16 12:22:16 +01:00
|
|
|
uint64_t suffix_binary_parse(const char *value)
|
2025-02-16 11:09:01 +01:00
|
|
|
{
|
2025-02-16 12:22:16 +01:00
|
|
|
char *suffix;
|
|
|
|
errno = 0;
|
|
|
|
uint64_t ret = strtoull(value, &suffix, 0);
|
|
|
|
if (errno)
|
2025-02-16 11:09:01 +01:00
|
|
|
return 0;
|
|
|
|
|
2025-02-16 12:22:16 +01:00
|
|
|
struct binary_suffix *s;
|
|
|
|
for (s = binary_suffixes; s->shift != 0; s++) {
|
|
|
|
if (tolower(suffix[0]) == tolower(s->suffix[0])) {
|
2025-02-16 11:09:01 +01:00
|
|
|
ret <<= s->shift;
|
2025-02-16 12:22:16 +01:00
|
|
|
return ret;
|
2025-02-16 11:09:01 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2025-02-16 12:22:16 +01:00
|
|
|
if (suffix[0] != '\0')
|
|
|
|
errno = EINVAL;
|
|
|
|
|
|
|
|
return ret;
|
2025-02-16 11:09:01 +01:00
|
|
|
}
|