可能是(true)属性的意义是什么 [英] What's the point of the probably(true) attribute

查看:115
本文介绍了可能是(true)属性的意义是什么的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是在 cppreference 上阅读了一些有关C ++中属性的信息.他们在那里提到了housing(true)属性,现在我想知道它的好处是什么.不幸的是,我无法在网上找到更多信息.

I was just reading something about attributes in C++ on cppreference. They mentioned the probably(true) attribute there and now I'm wondering what it's good for. Unfortunately I couldn't find further information on the web.

这是处理器在执行过程中使用的某种分支预测吗?

Is this some kind of branch prediction that a processor uses during execution?

推荐答案

是的.它用于为编译器提供有关if语句的更多信息,以便它可以根据目标微体系结构

Yes, exactly. It is used to give the compiler more information about the if statement so that it can generate the optimal code according to the target micro-architecture

虽然每个微体系结构都有其了解分支可能性的方法,但我们可以从英特尔优化手册中举一个简单的例子

While each micro-architecture has its ways to be informed about the likelihood of a branch, we can take a simple example from the Intel Optimization manual

汇编/编译器编码规则3.(对M的影响,对H的普遍性) 静态分支预测算法:将条件分支之后的后备代码设为 具有前进目标的分支的可能目标,并根据条件生成后备代码 分支是具有反向目标的分支的不太可能的目标.

Assembly/Compiler Coding Rule 3. (M impact, H generality) Arrange code to be consistent with the static branch prediction algorithm: make the fall-through code following a conditional branch be the likely target for a branch with a forward target, and make the fall-through code following a conditional branch be the unlikely target for a branch with a backward target.

简而言之,对前向分支的静态预测是不采用(因此,对分支后的代码进行推测性执行,这是可能的路径),而对后向分支的静态预测则是 (因此分支之后的代码不会被推测性地执行).

Simply put, the static prediction for forward branches is not-taken (so the code after the branch is speculatively executed, it's the likely path) while for backward branches is taken (so the code after the branch is not speculatively executed).

为GCC考虑以下代码:

Consider this code for GCC:

#define probably_true(x) __builtin_expect(!!(x), 1)
#define probably_false(x) __builtin_expect(!!(x), 0)

int foo(int a, int b, int c)
{
    if (probably_true(a==2))
        return a + b*c;
    else
        return a*b + 2*c;
}

我在其中使用内置 __builtin_expect 模拟[[problably(true)]].

Where I used the built-in __builtin_expect to simulate a [[problably(true)]].

这被编译成

foo(int, int, int):
        cmp     edi, 2           ;Compare a and 2
        jne     .L2              ;If not equals jumps to .L2

        ;This is the likely path (fall-through of a forward branch)

        ;return a + b*c;

.L2:
        ;This is the unlikely path (target of a forward branch)

        ;return a*b + 2*c;

        ret

我在这里为您节省了一些汇编代码.
如果将probably_true替换为probably_false,代码将变为:

Where I spared you some assembly code.
If you replace the probably_true with probably_false the code becomes:

foo(int, int, int):
        cmp     edi, 2           ;Compare a and 2
        je     .L5               ;If equals jumps to .L5

        ;This is the likely path (fall-through of a forward branch)

        ;return a*b + 2*c;

.L5:
        ;This is the unlikely path (target of a forward branch)

        ;return a + b*c;

        ret

您可以在codebolt.org上使用此示例.

You can play with with this example at codebolt.org.

这篇关于可能是(true)属性的意义是什么的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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