未验证 提交 7ff89e52 编写于 作者: 石晓伟 提交者: GitHub

fix likely macros, test=develop (#4208)

上级 31572a49
...@@ -18,15 +18,8 @@ ...@@ -18,15 +18,8 @@
#include <algorithm> #include <algorithm>
#include <climits> #include <climits>
#include <limits> #include <limits>
#include <system_error> // NOLINT #include <system_error> // NOLINT
#include "lite/utils/variant.h" // for likely
#ifndef likely
#define likely(x) __builtin_expect((x), 1)
#endif
#ifndef unlikely
#define unlikely(x) __builtin_expect((x), 0)
#endif
namespace paddle { namespace paddle {
namespace lite { namespace lite {
...@@ -45,12 +38,15 @@ struct from_chars_result { ...@@ -45,12 +38,15 @@ struct from_chars_result {
}; };
/* /*
* Reference:
* https://github.com/VerizonDigital/json_parser
*
* Most C++ environments use ASCII as the development character * Most C++ environments use ASCII as the development character
* set. Array `kAsciiToInt` is the look-up table implementation * set. Array `kAsciiToInt` is the look-up table implementation
* of the following functions: * of the following functions:
* *
* static inline uint8_t get_char_val(char c) { * static inline uint8_t get_char_val(char c) {
* if (likely(c >= '0' && c <= '9')) * if (LIKELY(c >= '0' && c <= '9'))
* return c - '0'; * return c - '0';
* if (c >= 'A' && c <= 'Z') * if (c >= 'A' && c <= 'Z')
* return 10 + (c - 'A'); * return 10 + (c - 'A');
...@@ -104,7 +100,8 @@ constexpr uint8_t kAsciiToInt[256] = { ...@@ -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, 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. * brief: Convert a string constant to an unsigned integer.
* parameters: * parameters:
* str(const char*): input string constant. * str(const char*): input string constant.
...@@ -122,30 +119,30 @@ from_chars_result aton_unsigned(const char* str, ...@@ -122,30 +119,30 @@ from_chars_result aton_unsigned(const char* str,
from_chars_result result; from_chars_result result;
result.ptr = str; result.ptr = str;
if (unlikely(!str || len <= 0)) { if (UNLIKELY(!str || len <= 0)) {
result.ec = std::errc::invalid_argument; result.ec = std::errc::invalid_argument;
return result; return result;
} }
uint64_t val = 0; uint64_t val = 0;
if (unlikely(*str == '-')) { if (UNLIKELY(*str == '-')) {
result.ec = std::errc::result_out_of_range; result.ec = std::errc::result_out_of_range;
return result; return result;
} }
if (unlikely(*str == '+')) { if (UNLIKELY(*str == '+')) {
++str; ++str;
--len; --len;
} }
int i = 0; int i = 0;
for (; i < len; ++i) { for (; i < len; ++i) {
uint8_t cv = kAsciiToInt[reinterpret_cast<const uint8_t&>(str[i])]; uint8_t cv = kAsciiToInt[reinterpret_cast<const uint8_t&>(str[i])];
if (unlikely(cv >= base)) { if (UNLIKELY(cv >= base)) {
value = static_cast<T>(val); value = static_cast<T>(val);
result.ptr = str + i; result.ptr = str + i;
return result; return result;
} }
// Handling integer values that may exceed the range represented by the // Handling integer values that may exceed the range represented by the
// basic type. // basic type.
if (unlikely(i > std::numeric_limits<uint32_t>::digits10 + 1) && if (UNLIKELY(i > std::numeric_limits<uint32_t>::digits10 + 1) &&
i == std::numeric_limits<uint64_t>::digits10) { i == std::numeric_limits<uint64_t>::digits10) {
uint64_t mx = static_cast<uint64_t>(std::numeric_limits<T>::max()); uint64_t mx = static_cast<uint64_t>(std::numeric_limits<T>::max());
if (val > mx / 10 || mx - (val * base) < cv) { if (val > mx / 10 || mx - (val * base) < cv) {
...@@ -154,12 +151,12 @@ from_chars_result aton_unsigned(const char* str, ...@@ -154,12 +151,12 @@ from_chars_result aton_unsigned(const char* str,
return result; return result;
} }
} }
if (likely(i != 10)) { if (LIKELY(i != 10)) {
val *= base; val *= base;
} }
val += cv; val += cv;
} }
if (unlikely(i > std::numeric_limits<T>::digits10 + 1 || if (UNLIKELY(i > std::numeric_limits<T>::digits10 + 1 ||
(i > std::numeric_limits<T>::digits10 && (i > std::numeric_limits<T>::digits10 &&
val > static_cast<uint64_t>(std::numeric_limits<T>::max())))) { val > static_cast<uint64_t>(std::numeric_limits<T>::max())))) {
value = static_cast<T>(std::numeric_limits<T>::max()); value = static_cast<T>(std::numeric_limits<T>::max());
...@@ -188,7 +185,7 @@ from_chars_result aton_signed(const char* str, ...@@ -188,7 +185,7 @@ from_chars_result aton_signed(const char* str,
from_chars_result result; from_chars_result result;
result.ptr = str; result.ptr = str;
if (unlikely(!str || len <= 0)) { if (UNLIKELY(!str || len <= 0)) {
result.ec = std::errc::invalid_argument; result.ec = std::errc::invalid_argument;
return result; return result;
} }
...@@ -201,18 +198,18 @@ from_chars_result aton_signed(const char* str, ...@@ -201,18 +198,18 @@ from_chars_result aton_signed(const char* str,
int i = 0; int i = 0;
for (; i < len; ++i) { for (; i < len; ++i) {
uint8_t cv = kAsciiToInt[reinterpret_cast<const uint8_t&>(str[i])]; uint8_t cv = kAsciiToInt[reinterpret_cast<const uint8_t&>(str[i])];
if (unlikely(cv >= base)) { if (UNLIKELY(cv >= base)) {
value = static_cast<T>(val); value = static_cast<T>(val);
result.ptr = str + i; result.ptr = str + i;
return result; return result;
} }
if (likely(i != 0)) { if (LIKELY(i != 0)) {
val *= base; val *= base;
} }
val += cv; val += cv;
} }
if (likely(!negative)) { if (LIKELY(!negative)) {
if (unlikely(i > std::numeric_limits<T>::digits10 + 1 || if (UNLIKELY(i > std::numeric_limits<T>::digits10 + 1 ||
(i > std::numeric_limits<T>::digits10 && (i > std::numeric_limits<T>::digits10 &&
val > static_cast<int64_t>(std::numeric_limits<T>::max())))) { val > static_cast<int64_t>(std::numeric_limits<T>::max())))) {
value = static_cast<T>(std::numeric_limits<T>::max()); value = static_cast<T>(std::numeric_limits<T>::max());
...@@ -251,7 +248,7 @@ from_chars_result aton_float(const char* str, int len, T& value) { // NOLINT ...@@ -251,7 +248,7 @@ from_chars_result aton_float(const char* str, int len, T& value) { // NOLINT
result.ptr = str; result.ptr = str;
const uint8_t base = 10; const uint8_t base = 10;
if (unlikely(!str || len <= 0)) { if (UNLIKELY(!str || len <= 0)) {
result.ec = std::errc::invalid_argument; result.ec = std::errc::invalid_argument;
return result; return result;
} }
...@@ -273,7 +270,7 @@ from_chars_result aton_float(const char* str, int len, T& value) { // NOLINT ...@@ -273,7 +270,7 @@ from_chars_result aton_float(const char* str, int len, T& value) { // NOLINT
break; break;
} }
uint8_t cv = kAsciiToInt[reinterpret_cast<const uint8_t&>(c)]; uint8_t cv = kAsciiToInt[reinterpret_cast<const uint8_t&>(c)];
if (unlikely(cv >= base)) { if (UNLIKELY(cv >= base)) {
value = static_cast<T>(lval); value = static_cast<T>(lval);
result.ptr = str + i; result.ptr = str + i;
return result; return result;
...@@ -287,7 +284,7 @@ from_chars_result aton_float(const char* str, int len, T& value) { // NOLINT ...@@ -287,7 +284,7 @@ from_chars_result aton_float(const char* str, int len, T& value) { // NOLINT
if (-1 != dot_pos) { if (-1 != dot_pos) {
for (; i < len; ++i) { for (; i < len; ++i) {
uint8_t cv = kAsciiToInt[reinterpret_cast<const uint8_t&>(str[i])]; uint8_t cv = kAsciiToInt[reinterpret_cast<const uint8_t&>(str[i])];
if (unlikely(cv >= base)) { if (UNLIKELY(cv >= base)) {
result.ptr = str + i; result.ptr = str + i;
return result; return result;
} }
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册