xref: /dpdk/lib/eal/include/rte_branch_prediction.h (revision 719834a6849e1daf4a70ff7742bbcc3ae7e25607)
199a2dd95SBruce Richardson /* SPDX-License-Identifier: BSD-3-Clause
299a2dd95SBruce Richardson  * Copyright(c) 2010-2014 Intel Corporation
399a2dd95SBruce Richardson  */
499a2dd95SBruce Richardson 
599a2dd95SBruce Richardson /**
699a2dd95SBruce Richardson  * @file
799a2dd95SBruce Richardson  * Branch Prediction Helpers in RTE
899a2dd95SBruce Richardson  */
999a2dd95SBruce Richardson 
1099a2dd95SBruce Richardson #ifndef _RTE_BRANCH_PREDICTION_H_
1199a2dd95SBruce Richardson #define _RTE_BRANCH_PREDICTION_H_
1299a2dd95SBruce Richardson 
1399a2dd95SBruce Richardson /**
1499a2dd95SBruce Richardson  * Check if a branch is likely to be taken.
1599a2dd95SBruce Richardson  *
1699a2dd95SBruce Richardson  * This compiler builtin allows the developer to indicate if a branch is
1799a2dd95SBruce Richardson  * likely to be taken. Example:
1899a2dd95SBruce Richardson  *
1999a2dd95SBruce Richardson  *   if (likely(x > 1))
2099a2dd95SBruce Richardson  *      do_stuff();
2199a2dd95SBruce Richardson  */
2299a2dd95SBruce Richardson #ifndef likely
23*51574a4fSTyler Retzlaff #ifdef RTE_TOOLCHAIN_MSVC
24*51574a4fSTyler Retzlaff #define likely(x)	(!!(x))
25*51574a4fSTyler Retzlaff #else
2699a2dd95SBruce Richardson #define likely(x)	__builtin_expect(!!(x), 1)
27*51574a4fSTyler Retzlaff #endif
2899a2dd95SBruce Richardson #endif /* likely */
2999a2dd95SBruce Richardson 
3099a2dd95SBruce Richardson /**
3199a2dd95SBruce Richardson  * Check if a branch is unlikely to be taken.
3299a2dd95SBruce Richardson  *
3399a2dd95SBruce Richardson  * This compiler builtin allows the developer to indicate if a branch is
3499a2dd95SBruce Richardson  * unlikely to be taken. Example:
3599a2dd95SBruce Richardson  *
3699a2dd95SBruce Richardson  *   if (unlikely(x < 1))
3799a2dd95SBruce Richardson  *      do_stuff();
3899a2dd95SBruce Richardson  */
3999a2dd95SBruce Richardson #ifndef unlikely
40*51574a4fSTyler Retzlaff #ifdef RTE_TOOLCHAIN_MSVC
41*51574a4fSTyler Retzlaff #define unlikely(x)	(!!(x))
42*51574a4fSTyler Retzlaff #else
4399a2dd95SBruce Richardson #define unlikely(x)	__builtin_expect(!!(x), 0)
44*51574a4fSTyler Retzlaff #endif
4599a2dd95SBruce Richardson #endif /* unlikely */
4699a2dd95SBruce Richardson 
4799a2dd95SBruce Richardson #endif /* _RTE_BRANCH_PREDICTION_H_ */
48