hv: move atoi and strtol_dec to debug directory

This patch moves `atoi` and `strtol_dec` to debug directory
since they are only used by code under debug directory.

Tracked-On: #861
Signed-off-by: Shiqing Gao <shiqing.gao@intel.com>
Acked-by: Anthony Xu <anthony.xu@intel.com>
This commit is contained in:
Shiqing Gao 2018-12-20 11:08:27 +08:00 committed by wenlingz
parent 32d6aa97f9
commit 714814f97e
3 changed files with 102 additions and 94 deletions

91
hypervisor/debug/string.c Normal file
View File

@ -0,0 +1,91 @@
/*
* Copyright (C) 2018 Intel Corporation. All rights reserved.
*
* SPDX-License-Identifier: BSD-3-Clause
*/
#include <hypervisor.h>
/*
* Convert a string to a long integer - decimal support only.
*/
int64_t strtol_deci(const char *nptr)
{
const char *s = nptr;
char c;
uint64_t acc, cutoff, cutlim;
int32_t neg = 0, any;
uint64_t base = 10UL;
/*
* Skip white space and pick up leading +/- sign if any.
*/
do {
c = *s;
s++;
} while (is_space(c));
if (c == '-') {
neg = 1;
c = *s;
s++;
} else if (c == '+') {
c = *s;
s++;
} else {
/* No sign character. */
}
/*
* Compute the cutoff value between legal numbers and illegal
* numbers. That is the largest legal value, divided by the
* base. An input number that is greater than this value, if
* followed by a legal input character, is too big. One that
* is equal to this value may be valid or not; the limit
* between valid and invalid numbers is then based on the last
* digit. For instance, if the range for longs is
* [-2147483648..2147483647] and the input base is 10,
* cutoff will be set to 214748364 and cutlim to either
* 7 (neg==0) or 8 (neg==1), meaning that if we have accumulated
* a value > 214748364, or equal but the next digit is > 7 (or 8),
* the number is too big, and we will return a range error.
*
* Set any if any `digits' consumed; make it negative to indicate
* overflow.
*/
cutoff = (neg != 0) ? LONG_MIN : LONG_MAX;
cutlim = cutoff % base;
cutoff /= base;
acc = 0UL;
any = 0;
while ((c >= '0') && (c <= '9')) {
c -= '0';
if ((acc > cutoff) ||
((acc == cutoff) && ((uint64_t)c > cutlim))) {
any = -1;
break;
} else {
acc *= base;
acc += (uint64_t)c;
}
c = *s;
s++;
}
if (any < 0) {
acc = (neg != 0) ? LONG_MIN : LONG_MAX;
} else if (neg != 0) {
acc = ~acc + 1UL;
} else {
/* There is no overflow and no leading '-' exists. In such case
* acc already holds the right number. No action required. */
}
return (long)acc;
}
int32_t atoi(const char *str)
{
return (int32_t)strtol_deci(str);
}

View File

@ -19,6 +19,16 @@ union u_qword {
};
/* MACRO related to string */
#define ULONG_MAX ((uint64_t)(~0UL)) /* 0xFFFFFFFF */
#define LONG_MAX (ULONG_MAX >> 1U) /* 0x7FFFFFFF */
#define LONG_MIN (~LONG_MAX) /* 0x80000000 */
static inline bool is_space(char c)
{
return ((c == ' ') || (c == '\t'));
}
/* Function prototypes */
void udelay(uint32_t us);
int32_t strcmp(const char *s1_arg, const char *s2_arg);

View File

@ -5,16 +5,7 @@
#include <hypervisor.h>
#define ULONG_MAX ((uint64_t)(~0UL)) /* 0xFFFFFFFF */
#define LONG_MAX (ULONG_MAX >> 1U) /* 0x7FFFFFFF */
#define LONG_MIN (~LONG_MAX) /* 0x80000000 */
static inline bool is_space(char c)
{
return ((c == ' ') || (c == '\t'));
}
static inline char hex_digit_value (char ch)
static inline char hex_digit_value(char ch)
{
char c;
if (('0' <= ch) && (ch <= '9')) {
@ -29,85 +20,6 @@ static inline char hex_digit_value (char ch)
return c;
}
/*
* Convert a string to a long integer - decimal support only.
*/
int64_t strtol_deci(const char *nptr)
{
const char *s = nptr;
char c;
uint64_t acc, cutoff, cutlim;
int32_t neg = 0, any;
uint64_t base = 10UL;
/*
* Skip white space and pick up leading +/- sign if any.
*/
do {
c = *s;
s++;
} while (is_space(c));
if (c == '-') {
neg = 1;
c = *s;
s++;
} else if (c == '+') {
c = *s;
s++;
} else {
/* No sign character. */
}
/*
* Compute the cutoff value between legal numbers and illegal
* numbers. That is the largest legal value, divided by the
* base. An input number that is greater than this value, if
* followed by a legal input character, is too big. One that
* is equal to this value may be valid or not; the limit
* between valid and invalid numbers is then based on the last
* digit. For instance, if the range for longs is
* [-2147483648..2147483647] and the input base is 10,
* cutoff will be set to 214748364 and cutlim to either
* 7 (neg==0) or 8 (neg==1), meaning that if we have accumulated
* a value > 214748364, or equal but the next digit is > 7 (or 8),
* the number is too big, and we will return a range error.
*
* Set any if any `digits' consumed; make it negative to indicate
* overflow.
*/
cutoff = (neg != 0) ? LONG_MIN : LONG_MAX;
cutlim = cutoff % base;
cutoff /= base;
acc = 0UL;
any = 0;
while ((c >= '0') && (c <= '9')) {
c -= '0';
if ((acc > cutoff) ||
((acc == cutoff) && ((uint64_t)c > cutlim))) {
any = -1;
break;
} else {
acc *= base;
acc += (uint64_t)c;
}
c = *s;
s++;
}
if (any < 0) {
acc = (neg != 0) ? LONG_MIN : LONG_MAX;
} else if (neg != 0) {
acc = ~acc + 1UL;
} else {
/* There is no overflow and no leading '-' exists. In such case
* acc already holds the right number. No action required. */
}
return (int64_t)acc;
}
/*
* Convert a string to an uint64_t integer - hexadecimal support only.
*/
@ -157,11 +69,6 @@ uint64_t strtoul_hex(const char *nptr)
return acc;
}
int32_t atoi(const char *str)
{
return (int32_t)strtol_deci(str);
}
char *strchr(char *s_arg, char ch)
{
char *s = s_arg;