Skip to content

Commit

Permalink
Move likely/unlikely to compiler.h
Browse files Browse the repository at this point in the history
Signed-off-by: Yuxuan Shui <yshuiv7@gmail.com>
  • Loading branch information
yshui committed Dec 15, 2018
1 parent b73dce1 commit 68873ef
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 8 deletions.
6 changes: 0 additions & 6 deletions src/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -121,12 +121,6 @@
#define MSTR_(s) #s
#define MSTR(s) MSTR_(s)

/// @brief Wrapper for gcc branch prediction builtin, for likely branch.
#define likely(x) __builtin_expect(!!(x), 1)

/// @brief Wrapper for gcc branch prediction builtin, for unlikely branch.
#define unlikely(x) __builtin_expect(!!(x), 0)

// Use #s here to prevent macro expansion
/// Macro used for shortening some debugging code.
#define CASESTRRET(s) case s: return #s
Expand Down
5 changes: 4 additions & 1 deletion src/compiler.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
// SPDX-License-Identifier: MPL-2.0
// Copyright (c) 2018 Yuxuan Shui <yshuiv7@gmail.com>
#pragma once
#define auto __auto_type

#define auto __auto_type
#define likely(x) __builtin_expect(!!(x), 1)
#define unlikely(x) __builtin_expect(!!(x), 0)
4 changes: 3 additions & 1 deletion src/utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
#include <stdio.h>
#include <unistd.h>

#include "compiler.h"

#define ARR_SIZE(arr) (sizeof(arr)/sizeof(arr[0]))

#ifdef __FAST_MATH__
Expand Down Expand Up @@ -104,7 +106,7 @@ normalize_d(double d) {
*/
static inline void *
allocchk_(const char *func_name, void *ptr) {
if (!ptr) {
if (unlikely(!ptr)) {
// Since memory allocation failed, we try to print
// this error message without any memory allocation.
const char msg[] = "(): Failed to allocate memory\n";
Expand Down

0 comments on commit 68873ef

Please sign in to comment.