能可能/不可能宏用户空间code使用? [英] Can likely/unlikely macros be used in user-space code?

查看:141
本文介绍了能可能/不可能宏用户空间code使用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了在Linux内核code这2个宏。我知道他们是在分支的情况下编译器(GCC)的优化指令。我的问题是,我们可以利用在用户空间code这些宏?它会给任何优化?任何实际的例子将是非常有益的。

I came across these 2 macros in Linux kernel code. I know they are instructions to compiler (gcc) for optimizations in case of branching. My question is, can we use these macros in user space code? Will it give any optimization? Any example will be very helpful.

推荐答案

是的,他们可以。 在Linux内核,它们被定义为

Yes they can. In the Linux kernel, they are defined as

#define likely(x)       __builtin_expect(!!(x), 1)
#define unlikely(x)     __builtin_expect(!!(x), 0)

该__builtin_expect宏是使用分支prediction GCC特有的宏定义;他们告诉处理器的条件是否有可能是真实的,从而使处理器能够美元分公司的正确的方P $ pfetch说明。

The __builtin_expect macros are GCC specific macros that use the branch prediction; they tell the processor whether a condition is likely to be true, so that the processor can prefetch instructions on the correct "side" of the branch.

您应该包裹定义在IFDEF,以确保其他编译器编译:

You should wrap the defines in an ifdef to ensure compilation on other compilers:

#ifdef __GNUC__
#define likely(x)       __builtin_expect(!!(x), 1)
#define unlikely(x)     __builtin_expect(!!(x), 0)
#else
#define likely(x)       (x)
#define unlikely(x)     (x)
#endif

这一定会让你的优化,如果你用它来做正确的分支predictions。

It will definitely give you optimizations if you use it for correct branch predictions.

这篇关于能可能/不可能宏用户空间code使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆