Skip to content

Commit

Permalink
[struct_pack] update rule for enabling [[likely]] (alibaba#696)
Browse files Browse the repository at this point in the history
MSVC intentionally defined __cplusplus to 199711L, ignoring the actual standard, hence MACRO definitions of `SP_LIKELY` and `SP_UNLIKELY` go to wrong branches in MSVC build.
  • Loading branch information
scuzqy authored Jun 21, 2024
1 parent 1f1ba52 commit 8c46806
Showing 1 changed file with 3 additions and 8 deletions.
11 changes: 3 additions & 8 deletions include/ylt/struct_pack/marco.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,19 +31,14 @@
#define STRUCT_PACK_MAY_INLINE inline
#endif

#if __has_cpp_attribute(likely) && __cplusplus >= 202002L
#if __has_cpp_attribute(likely) && __has_cpp_attribute(unlikely)
#define SP_LIKELY(expr) (expr) [[likely]]
#elif __GNUC__
#define SP_LIKELY(expr) (__builtin_expect(!!(expr), 1))
#else
#define SP_LIKELY(expr) (expr)
#endif

#if __has_cpp_attribute(unlikely) && __cplusplus >= 202002L
#define SP_UNLIKELY(expr) (expr) [[unlikely]]
#elif __GNUC__
#define SP_LIKELY(expr) (__builtin_expect(!!(expr), 1))
#define SP_UNLIKELY(expr) (__builtin_expect(!!(expr), 0))
#else
#define SP_LIKELY(expr) (expr)
#define SP_UNLIKELY(expr) (expr)
#endif

Expand Down

0 comments on commit 8c46806

Please sign in to comment.