Skip to content

Commit

Permalink
add unlikely to unhappy path
Browse files Browse the repository at this point in the history
  • Loading branch information
MacroModel committed Dec 17, 2024
1 parent c5d0343 commit be4602d
Showing 1 changed file with 35 additions and 37 deletions.
72 changes: 35 additions & 37 deletions include/fast_io_core_impl/integers/sto/sto_contiguous.h
Original file line number Diff line number Diff line change
Expand Up @@ -949,57 +949,57 @@ template <char8_t base, ::std::integral char_type>
inline constexpr parse_result<char_type const *> scan_shbase_impl(char_type const *first,
char_type const *last) noexcept
{
if (first == last || *first != char_literal_v<u8'0', char_type>)
if (first == last || *first != char_literal_v<u8'0', char_type>) [[unlikely]]
{
return {first, parse_code::invalid};
}
if ((++first) == last)
if ((++first) == last) [[unlikely]]
{
return {first, parse_code::invalid};
}
if constexpr (base == 2 || base == 3 || base == 16)
{
auto ch{*first};
if ((ch != char_literal_v<(base == 2 ? u8'B' : (base == 3 ? u8't' : u8'X')), char_type>)&(
ch != char_literal_v<(base == 2 ? u8'b' : (base == 3 ? u8't' : u8'x')), char_type>))
ch != char_literal_v<(base == 2 ? u8'b' : (base == 3 ? u8't' : u8'x')), char_type>)) [[unlikely]]
{
return {first, parse_code::invalid};
}
++first;
}
else
{
if (*first != char_literal_v<u8'[', char_type>)
if (*first != char_literal_v<u8'[', char_type>) [[unlikely]]
{
return {first, parse_code::invalid};
}
++first;
if ((++first) == last)
if ((++first) == last) [[unlikely]]
{
return {first, parse_code::invalid};
}
constexpr auto digit0{char_literal_v<u8'0' + (base < 10 ? base : base / 10), char_type>};
if (*first != digit0)
if (*first != digit0) [[unlikely]]
{
return {first, parse_code::invalid};
}
if ((++first) == last)
if ((++first) == last) [[unlikely]]
{
return {first, parse_code::invalid};
}
if constexpr (10 < base)
{
constexpr auto digit1{char_literal_v<u8'0' + (base % 10), char_type>};
if (*first != digit1)
if (*first != digit1) [[unlikely]]
{
return {first, parse_code::invalid};
}
if ((++first) == last)
if ((++first) == last) [[unlikely]]
{
return {first, parse_code::invalid};
}
}
if (*first != char_literal_v<u8']', char_type>)
if (*first != char_literal_v<u8']', char_type>) [[unlikely]]
{
return {first, parse_code::invalid};
}
Expand All @@ -1018,7 +1018,7 @@ scan_int_contiguous_none_space_part_define_impl(char_type const *first, char_typ
[[maybe_unused]] bool sign{};
if constexpr (my_signed_integral<T>)
{
if (first == last)
if (first == last) [[unlikely]]
{
return {first, parse_code::invalid};
}
Expand All @@ -1031,7 +1031,7 @@ scan_int_contiguous_none_space_part_define_impl(char_type const *first, char_typ
{
if constexpr (base == 8)
{
if (first == last || *first != char_literal_v<u8'0', char_type>)
if (first == last || *first != char_literal_v<u8'0', char_type>) [[unlikely]]
{
return {first, parse_code::invalid};
}
Expand All @@ -1040,7 +1040,7 @@ scan_int_contiguous_none_space_part_define_impl(char_type const *first, char_typ
else
{
auto phase_ret = scan_shbase_impl<base>(first, last);
if (phase_ret.code != ongoing_parse_code)
if (phase_ret.code != ongoing_parse_code) [[unlikely]]
{
return phase_ret;
}
Expand All @@ -1062,7 +1062,7 @@ scan_int_contiguous_none_space_part_define_impl(char_type const *first, char_typ
{
++first;
first = ::fast_io::details::find_none_zero_simd_impl(first, last);
if (first == last)
if (first == last) [[likely]]
{
t = 0;
return {first, parse_code::ok};
Expand All @@ -1071,7 +1071,7 @@ scan_int_contiguous_none_space_part_define_impl(char_type const *first, char_typ
else
{
++first;
if ((first == last) || (!char_is_digit<base, char_type>(static_cast<unsigned_char_type>(*first))))
if ((first == last) || (!char_is_digit<base, char_type>(static_cast<unsigned_char_type>(*first)))) [[likely]]
{
t = {};
return {first, parse_code::ok};
Expand Down Expand Up @@ -1104,7 +1104,7 @@ scan_int_contiguous_none_space_part_define_impl(char_type const *first, char_typ
if constexpr (smaller_than_uint64)
{
constexpr unsigned_type umax{static_cast<unsigned_type>(-1)};
if (temp > umax)
if (temp > umax) [[unlikely]]
{
return {it, parse_code::overflow};
}
Expand All @@ -1118,7 +1118,7 @@ scan_int_contiguous_none_space_part_define_impl(char_type const *first, char_typ
else [[unlikely]]
{
auto [it2, ec] = scan_int_contiguous_none_simd_space_part_define_impl<base>(first, last, res);
if (ec != parse_code::ok)
if (ec != parse_code::ok) [[unlikely]]
{
return {it2, ec};
}
Expand All @@ -1129,7 +1129,7 @@ scan_int_contiguous_none_space_part_define_impl(char_type const *first, char_typ
#endif
{
auto [it2, ec] = scan_int_contiguous_none_simd_space_part_define_impl<base>(first, last, res);
if (ec != parse_code::ok)
if (ec != parse_code::ok) [[unlikely]]
{
return {it2, ec};
}
Expand All @@ -1139,7 +1139,7 @@ scan_int_contiguous_none_space_part_define_impl(char_type const *first, char_typ
{
constexpr unsigned_type umax{static_cast<unsigned_type>(-1)};
constexpr unsigned_type imax{umax >> 1};
if (res > (static_cast<my_make_unsigned_t<T>>(imax) + sign))
if (res > (static_cast<my_make_unsigned_t<T>>(imax) + sign)) [[unlikely]]
{
return {it, parse_code::overflow};
}
Expand Down Expand Up @@ -1177,7 +1177,7 @@ inline constexpr parse_result<char_type const *> scan_int_contiguous_define_impl
{
if constexpr (base == 8)
{
if (first == last || *first != char_literal_v<u8'0', char_type>)
if (first == last || *first != char_literal_v<u8'0', char_type>) [[unlikely]]
{
return {first, parse_code::invalid};
}
Expand All @@ -1186,7 +1186,7 @@ inline constexpr parse_result<char_type const *> scan_int_contiguous_define_impl
else
{
auto phase_ret = scan_shbase_impl<base>(first, last);
if (phase_ret.code != ongoing_parse_code)
if (phase_ret.code != ongoing_parse_code) [[unlikely]]
{
return phase_ret;
}
Expand Down Expand Up @@ -1310,7 +1310,7 @@ sc_int_ctx_prefix_phase(::std::uint_least8_t &sz, char_type const *first, char_t
}
if constexpr (base == 8)
{
if (*first != char_literal_v<u8'0', char_type>)
if (*first != char_literal_v<u8'0', char_type>) [[unlikely]]
{
return {first, parse_code::invalid};
}
Expand All @@ -1320,7 +1320,7 @@ sc_int_ctx_prefix_phase(::std::uint_least8_t &sz, char_type const *first, char_t
::std::uint_least8_t size_cache{sz};
if (size_cache == 0)
{
if (*first != char_literal_v<u8'0', char_type>)
if (*first != char_literal_v<u8'0', char_type>) [[unlikely]]
{
return {first, parse_code::invalid};
}
Expand All @@ -1338,13 +1338,13 @@ sc_int_ctx_prefix_phase(::std::uint_least8_t &sz, char_type const *first, char_t
{
auto ch{*first};
if ((ch == char_literal_v<(base == 2 ? u8'B' : (base == 3 ? u8't' : u8'X')), char_type>) |
(ch == char_literal_v<(base == 2 ? u8'b' : (base == 3 ? u8't' : u8'x')), char_type>))
(ch == char_literal_v<(base == 2 ? u8'b' : (base == 3 ? u8't' : u8'x')), char_type>)) [[likely]]
{
sz = 0;
++first;
return {first, ongoing_parse_code};
}
else
else [[unlikely]]
{
return {first, parse_code::invalid};
}
Expand All @@ -1353,7 +1353,7 @@ sc_int_ctx_prefix_phase(::std::uint_least8_t &sz, char_type const *first, char_t
{
if (size_cache == 1)
{
if (*first != char_literal_v<u8'[', char_type>)
if (*first != char_literal_v<u8'[', char_type>) [[unlikely]]
{
return {first, parse_code::invalid};
}
Expand All @@ -1366,7 +1366,7 @@ sc_int_ctx_prefix_phase(::std::uint_least8_t &sz, char_type const *first, char_t
constexpr auto digit0{char_literal_v<u8'0' + (base < 10 ? base : base / 10), char_type>};
if (size_cache == 2)
{
if (*first != digit0)
if (*first != digit0) [[unlikely]]
{
return {first, parse_code::invalid};
}
Expand All @@ -1381,7 +1381,7 @@ sc_int_ctx_prefix_phase(::std::uint_least8_t &sz, char_type const *first, char_t
constexpr auto digit1{char_literal_v<u8'0' + (base % 10), char_type>};
if (size_cache == 3)
{
if (*first != digit1)
if (*first != digit1) [[unlikely]]
{
return {first, parse_code::invalid};
}
Expand All @@ -1395,7 +1395,7 @@ sc_int_ctx_prefix_phase(::std::uint_least8_t &sz, char_type const *first, char_t
constexpr ::std::uint_least8_t last_index{base < 10 ? 3 : 4};
if (size_cache == last_index)
{
if (*first != char_literal_v<u8']', char_type>)
if (*first != char_literal_v<u8']', char_type>) [[unlikely]]
{
return {first, parse_code::invalid};
}
Expand Down Expand Up @@ -1443,7 +1443,7 @@ inline constexpr parse_result<char_type const *> sc_int_ctx_zero_phase(scan_inte
}
return {first, parse_code::partial};
}
if (!char_is_digit<base, char_type>(static_cast<unsigned_char_type>(*first)))
if (!char_is_digit<base, char_type>(static_cast<unsigned_char_type>(*first))) [[likely]]
{
return {first, parse_code::ok};
}
Expand Down Expand Up @@ -1472,7 +1472,7 @@ inline constexpr parse_result<char_type const *> sc_int_ctx_digit_phase(State &s
st.integer_phase = scan_integral_context_phase::digit;
return {it, parse_code::partial};
}
if (st.size == 0)
if (st.size == 0) [[likely]]
{
t = {};
return {it, parse_code::ok};
Expand All @@ -1487,7 +1487,7 @@ inline constexpr parse_result<char_type const *> sc_int_ctx_digit_phase(State &s
st.integer_phase = scan_integral_context_phase::overflow;
return {it, parse_code::partial};
}
else
else [[unlikely]]
{
return {it, parse_code::overflow};
}
Expand All @@ -1504,7 +1504,7 @@ inline constexpr parse_result<char_type const *> sc_int_ctx_zero_invalid_phase(c
return {first, parse_code::partial};
}
++first;
if (!char_is_digit<base, char_type>(static_cast<unsigned_char_type>(*first)))
if (!char_is_digit<base, char_type>(static_cast<unsigned_char_type>(*first))) [[likely]]
{
return {first, parse_code::ok};
}
Expand Down Expand Up @@ -1554,7 +1554,7 @@ inline constexpr parse_result<char_type const *> scan_context_define_parse_impl(
if constexpr (!noskipws)
{
auto phase_ret = sc_int_ctx_space_phase(first, last);
if (phase_ret.code != ongoing_parse_code)
if (phase_ret.code != ongoing_parse_code) [[unlikely]]
{
return phase_ret;
}
Expand Down Expand Up @@ -1673,9 +1673,7 @@ inline constexpr parse_code scan_context_eof_define_parse_impl(State &st, T &t)
}
}
case scan_integral_context_phase::digit:
return scan_int_contiguous_none_space_part_define_impl<base>(st.buffer.data(), st.buffer.data() + st.size,
t)
.code;
return scan_int_contiguous_none_space_part_define_impl<base>(st.buffer.data(), st.buffer.data() + st.size, t).code;
case scan_integral_context_phase::overflow:
return parse_code::overflow;
case scan_integral_context_phase::zero_skip:
Expand Down

0 comments on commit be4602d

Please sign in to comment.