[PATCH 4/4] Add UNLIKELY and LIKELY macros
Jussi Kivilinna
jussi.kivilinna at iki.fi
Sat Jan 28 14:13:24 CET 2017
* src/g10lib.h (LIKELY, UNLIKELY): New.
(gcry_assert): Use LIKELY for assert check.
(fast_wipememory2_unaligned_head): Use UNLIKELY for unaligned
branching.
* cipher/bufhelp.h (buf_cpy, buf_xor, buf_xor_1, buf_xor_2dst)
(buf_xor_n_copy_2): Ditto.
--
Signed-off-by: Jussi Kivilinna <jussi.kivilinna at iki.fi>
---
0 files changed
diff --git a/cipher/bufhelp.h b/cipher/bufhelp.h
index 3110a1d..b854bc0 100644
--- a/cipher/bufhelp.h
+++ b/cipher/bufhelp.h
@@ -1,5 +1,5 @@
/* bufhelp.h - Some buffer manipulation helpers
- * Copyright (C) 2012 Jussi Kivilinna <jussi.kivilinna at mbnet.fi>
+ * Copyright (C) 2012-2017 Jussi Kivilinna <jussi.kivilinna at iki.fi>
*
* This file is part of Libgcrypt.
*
@@ -20,6 +20,7 @@
#define GCRYPT_BUFHELP_H
+#include "g10lib.h"
#include "bithelp.h"
@@ -88,7 +89,7 @@ buf_cpy(void *_dst, const void *_src, size_t len)
const unsigned int longmask = sizeof(bufhelp_int_t) - 1;
/* Skip fast processing if buffers are unaligned. */
- if (((uintptr_t)dst | (uintptr_t)src) & longmask)
+ if (UNLIKELY(((uintptr_t)dst | (uintptr_t)src) & longmask))
goto do_bytes;
#endif
@@ -124,7 +125,7 @@ buf_xor(void *_dst, const void *_src1, const void *_src2, size_t len)
const unsigned int longmask = sizeof(bufhelp_int_t) - 1;
/* Skip fast processing if buffers are unaligned. */
- if (((uintptr_t)dst | (uintptr_t)src1 | (uintptr_t)src2) & longmask)
+ if (UNLIKELY(((uintptr_t)dst | (uintptr_t)src1 | (uintptr_t)src2) & longmask))
goto do_bytes;
#endif
@@ -160,7 +161,7 @@ buf_xor_1(void *_dst, const void *_src, size_t len)
const unsigned int longmask = sizeof(bufhelp_int_t) - 1;
/* Skip fast processing if buffers are unaligned. */
- if (((uintptr_t)dst | (uintptr_t)src) & longmask)
+ if (UNLIKELY(((uintptr_t)dst | (uintptr_t)src) & longmask))
goto do_bytes;
#endif
@@ -196,7 +197,7 @@ buf_xor_2dst(void *_dst1, void *_dst2, const void *_src, size_t len)
const unsigned int longmask = sizeof(bufhelp_int_t) - 1;
/* Skip fast processing if buffers are unaligned. */
- if (((uintptr_t)src | (uintptr_t)dst1 | (uintptr_t)dst2) & longmask)
+ if (UNLIKELY(((uintptr_t)src | (uintptr_t)dst1 | (uintptr_t)dst2) & longmask))
goto do_bytes;
#endif
@@ -238,8 +239,8 @@ buf_xor_n_copy_2(void *_dst_xor, const void *_src_xor, void *_srcdst_cpy,
const unsigned int longmask = sizeof(bufhelp_int_t) - 1;
/* Skip fast processing if buffers are unaligned. */
- if (((uintptr_t)src_cpy | (uintptr_t)src_xor | (uintptr_t)dst_xor |
- (uintptr_t)srcdst_cpy) & longmask)
+ if (UNLIKELY(((uintptr_t)src_cpy | (uintptr_t)src_xor | (uintptr_t)dst_xor |
+ (uintptr_t)srcdst_cpy) & longmask))
goto do_bytes;
#endif
diff --git a/src/g10lib.h b/src/g10lib.h
index 8ce84b8..0309a83 100644
--- a/src/g10lib.h
+++ b/src/g10lib.h
@@ -75,6 +75,14 @@
#define GCC_ATTR_UNUSED
#endif
+#if __GNUC__ >= 3
+#define LIKELY( expr ) __builtin_expect( !!(expr), 1 )
+#define UNLIKELY( expr ) __builtin_expect( !!(expr), 0 )
+#else
+#define LIKELY( expr ) (!!(expr))
+#define UNLIKELY( expr ) (!!(expr))
+#endif
+
/* Gettext macros. */
#define _(a) _gcry_gettext(a)
@@ -165,15 +173,15 @@ int _gcry_log_verbosity( int level );
#ifdef JNLIB_GCC_M_FUNCTION
#define BUG() _gcry_bug( __FILE__ , __LINE__, __FUNCTION__ )
-#define gcry_assert(expr) ((expr)? (void)0 \
+#define gcry_assert(expr) (LIKELY(expr)? (void)0 \
: _gcry_assert_failed (STR(expr), __FILE__, __LINE__, __FUNCTION__))
#elif __STDC_VERSION__ >= 199901L
#define BUG() _gcry_bug( __FILE__ , __LINE__, __func__ )
-#define gcry_assert(expr) ((expr)? (void)0 \
+#define gcry_assert(expr) (LIKELY(expr)? (void)0 \
: _gcry_assert_failed (STR(expr), __FILE__, __LINE__, __func__))
#else
#define BUG() _gcry_bug( __FILE__ , __LINE__ )
-#define gcry_assert(expr) ((expr)? (void)0 \
+#define gcry_assert(expr) (LIKELY(expr)? (void)0 \
: _gcry_assert_failed (STR(expr), __FILE__, __LINE__))
#endif
@@ -346,7 +354,7 @@ typedef struct fast_wipememory_s
} __attribute__((packed, aligned(1), may_alias)) fast_wipememory_t;
#else
#define fast_wipememory2_unaligned_head(_vptr,_vset,_vlen) do { \
- while((size_t)(_vptr)&(sizeof(FASTWIPE_T)-1) && _vlen) \
+ while(UNLIKELY((size_t)(_vptr)&(sizeof(FASTWIPE_T)-1)) && _vlen) \
{ *_vptr=(_vset); _vptr++; _vlen--; } \
} while(0)
typedef struct fast_wipememory_s
More information about the Gcrypt-devel
mailing list