Skip to content

Commit

Permalink
Add C10_LIKELY/C10_UNLIKELY macros (pytorch#12932)
Browse files Browse the repository at this point in the history
Summary:
Pull Request resolved: pytorch#12932

I was looking at some assembly for some code I was working on,
and felt a desire to have likely()/unlikely() macros.  I checked
if we already had them, and we didn't.  This commit adds them,
and fixes up all known use sites to make use of it.

Reviewed By: Maratyszcza

Differential Revision: D10488399

fbshipit-source-id: 7476da208907480d49f02b37c7345c17d85c3db7
  • Loading branch information
ezyang authored and facebook-github-bot committed Oct 22, 2018
1 parent 8d3e7e2 commit 8c51462
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 3 deletions.
20 changes: 20 additions & 0 deletions c10/macros/Macros.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,4 +61,24 @@ namespace c10 {} // namespace c10
#define C10_NORETURN __attribute__((noreturn))
#endif

// C10_LIKELY/C10_UNLIKELY
//
// These macros provide parentheses, so you can use these macros as:
//
// if C10_LIKELY(some_expr) {
// ...
// }
//
// NB: static_cast to boolean is mandatory in C++, because __builtin_expect
// takes a long argument, which means you may trigger the wrong conversion
// without it.
//
#if defined(__GNUC__) || defined(__ICL) || defined(__clang__)
#define C10_LIKELY(expr) (__builtin_expect(static_cast<bool>(expr), 1))
#define C10_UNLIKELY(expr) (__builtin_expect(static_cast<bool>(expr), 0))
#else
#define C10_LIKELY(expr) (expr)
#define C10_UNLIKELY(expr) (expr)
#endif

#endif // C10_MACROS_MACROS_H_
8 changes: 5 additions & 3 deletions caffe2/share/contrib/depthwise/depthwise3x3_conv_op.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
#include "caffe2/operators/conv_op.h"
#include "caffe2/operators/conv_pool_op_base.h"

#include "c10/macros/Macros.h"

#ifdef __ARM_NEON__
#include <arm_neon.h>
#endif
Expand Down Expand Up @@ -163,11 +165,11 @@ void runDepthwise3x3Conv(
int ih = oth * 2 - args.pad_rows;
int iw = otw * 2 - args.pad_cols;
// fast-path, all accesses in-bounds
if (__builtin_expect(
if (C10_LIKELY(
ih >= 0 && iw >= 0 && ih + 3 < args.in_rows &&
iw + 3 < args.in_cols && 2 * oth + 1 < args.out_rows &&
2 * otw + 1 < args.out_cols,
1)) {
2 * otw + 1 < args.out_cols
)) {
float32x4x4_t input_tile;
for (int row = 0; row < 4; ++row) {
input_tile.val[row] =
Expand Down

0 comments on commit 8c51462

Please sign in to comment.