From 7ff89e529341b69f25f13899a09b17a120a79482 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=9F=B3=E6=99=93=E4=BC=9F?= <39303645+Shixiaowei02@users.noreply.github.com> Date: Mon, 31 Aug 2020 10:29:31 +0800 Subject: [PATCH] fix likely macros, test=develop (#4208) --- lite/utils/charconv.h | 49 ++++++++++++++++++++----------------------- 1 file changed, 23 insertions(+), 26 deletions(-) diff --git a/lite/utils/charconv.h b/lite/utils/charconv.h index 9d82bec785..ee458e549c 100644 --- a/lite/utils/charconv.h +++ b/lite/utils/charconv.h @@ -18,15 +18,8 @@ #include #include #include -#include // NOLINT - -#ifndef likely -#define likely(x) __builtin_expect((x), 1) -#endif - -#ifndef unlikely -#define unlikely(x) __builtin_expect((x), 0) -#endif +#include // NOLINT +#include "lite/utils/variant.h" // for likely namespace paddle { namespace lite { @@ -45,12 +38,15 @@ struct from_chars_result { }; /* + * Reference: + * https://github.com/VerizonDigital/json_parser + * * Most C++ environments use ASCII as the development character * set. Array `kAsciiToInt` is the look-up table implementation * of the following functions: * * static inline uint8_t get_char_val(char c) { - * if (likely(c >= '0' && c <= '9')) + * if (LIKELY(c >= '0' && c <= '9')) * return c - '0'; * if (c >= 'A' && c <= 'Z') * return 10 + (c - 'A'); @@ -104,7 +100,8 @@ constexpr uint8_t kAsciiToInt[256] = { UCHAR_MAX, UCHAR_MAX, UCHAR_MAX, UCHAR_MAX, UCHAR_MAX, UCHAR_MAX, UCHAR_MAX, UCHAR_MAX, UCHAR_MAX}; -/* function: aton_unsigned +/* + * function: aton_unsigned * brief: Convert a string constant to an unsigned integer. * parameters: * str(const char*): input string constant. @@ -122,30 +119,30 @@ from_chars_result aton_unsigned(const char* str, from_chars_result result; result.ptr = str; - if (unlikely(!str || len <= 0)) { + if (UNLIKELY(!str || len <= 0)) { result.ec = std::errc::invalid_argument; return result; } uint64_t val = 0; - if (unlikely(*str == '-')) { + if (UNLIKELY(*str == '-')) { result.ec = std::errc::result_out_of_range; return result; } - if (unlikely(*str == '+')) { + if (UNLIKELY(*str == '+')) { ++str; --len; } int i = 0; for (; i < len; ++i) { uint8_t cv = kAsciiToInt[reinterpret_cast(str[i])]; - if (unlikely(cv >= base)) { + if (UNLIKELY(cv >= base)) { value = static_cast(val); result.ptr = str + i; return result; } // Handling integer values that may exceed the range represented by the // basic type. - if (unlikely(i > std::numeric_limits::digits10 + 1) && + if (UNLIKELY(i > std::numeric_limits::digits10 + 1) && i == std::numeric_limits::digits10) { uint64_t mx = static_cast(std::numeric_limits::max()); if (val > mx / 10 || mx - (val * base) < cv) { @@ -154,12 +151,12 @@ from_chars_result aton_unsigned(const char* str, return result; } } - if (likely(i != 10)) { + if (LIKELY(i != 10)) { val *= base; } val += cv; } - if (unlikely(i > std::numeric_limits::digits10 + 1 || + if (UNLIKELY(i > std::numeric_limits::digits10 + 1 || (i > std::numeric_limits::digits10 && val > static_cast(std::numeric_limits::max())))) { value = static_cast(std::numeric_limits::max()); @@ -188,7 +185,7 @@ from_chars_result aton_signed(const char* str, from_chars_result result; result.ptr = str; - if (unlikely(!str || len <= 0)) { + if (UNLIKELY(!str || len <= 0)) { result.ec = std::errc::invalid_argument; return result; } @@ -201,18 +198,18 @@ from_chars_result aton_signed(const char* str, int i = 0; for (; i < len; ++i) { uint8_t cv = kAsciiToInt[reinterpret_cast(str[i])]; - if (unlikely(cv >= base)) { + if (UNLIKELY(cv >= base)) { value = static_cast(val); result.ptr = str + i; return result; } - if (likely(i != 0)) { + if (LIKELY(i != 0)) { val *= base; } val += cv; } - if (likely(!negative)) { - if (unlikely(i > std::numeric_limits::digits10 + 1 || + if (LIKELY(!negative)) { + if (UNLIKELY(i > std::numeric_limits::digits10 + 1 || (i > std::numeric_limits::digits10 && val > static_cast(std::numeric_limits::max())))) { value = static_cast(std::numeric_limits::max()); @@ -251,7 +248,7 @@ from_chars_result aton_float(const char* str, int len, T& value) { // NOLINT result.ptr = str; const uint8_t base = 10; - if (unlikely(!str || len <= 0)) { + if (UNLIKELY(!str || len <= 0)) { result.ec = std::errc::invalid_argument; return result; } @@ -273,7 +270,7 @@ from_chars_result aton_float(const char* str, int len, T& value) { // NOLINT break; } uint8_t cv = kAsciiToInt[reinterpret_cast(c)]; - if (unlikely(cv >= base)) { + if (UNLIKELY(cv >= base)) { value = static_cast(lval); result.ptr = str + i; return result; @@ -287,7 +284,7 @@ from_chars_result aton_float(const char* str, int len, T& value) { // NOLINT if (-1 != dot_pos) { for (; i < len; ++i) { uint8_t cv = kAsciiToInt[reinterpret_cast(str[i])]; - if (unlikely(cv >= base)) { + if (UNLIKELY(cv >= base)) { result.ptr = str + i; return result; } -- GitLab