返回值优化,并用C复制省略 [英] Return value optimization and copy elision in C

查看:143
本文介绍了返回值优化,并用C复制省略的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有些人不知道它的可以传递和返回结构通过用C 值。我的问题是关于编译器使得在返回C.待办事项C编译器结构时不必要的复制,如GCC使用返回值优化(RVO)优化或这是一个C ++只概念?我的一切都了解RVO和复制省略是关于C ++。

Some people are not aware that it's possible to pass and return structs by value in C. My question is about the compiler making unnecessary copies when returning structs in C. Do C compilers such as GCC use Return value optimization(RVO) optimization or is this a C++ only concept? Everything I have read about RVO and copy elision is in regards to C++.

让我们来看一个例子。
我目前正在实施<一个href=\"https://en.wikipedia.org/wiki/Quadruple-$p$pcision_floating-point_format#Double-double_arithmetic\">double-double在C数据类型
(或者更确切地说,浮浮下手,因为我发现很容易单元测试)。请看下面的code。

Let's consider an example. I'm currently implementing a double-double data type in C (or rather float-float to start with because I find it easy to unit test). Consider the following code.

typedef struct {
    float hi;
    float lo;
} doublefloat;

doublefloat quick_two_sum(float a, float b) {
    float s = a + b;
    float e = b - (s - a);
    return (doublefloat){s, e};
}

请问编译器让 doublefloat 价值我返回的临时副本,也可以在临时副本被省略?

Will the compiler make a temporary copy of the doublefloat value I return or can the temporary copy be elided?

有关命名返回值优化(NRVO c)中有什么?我有另一个函数

What about named return value optimization (NRVO) in C? I have another function

doublefloat df64_add(doublefloat a, doublefloat b) {
    doublefloat s, t;
    s = two_sum(a.hi, b.hi);
    t = two_sum(a.lo, b.lo);
    s.lo += t.hi;
    s = quick_two_sum(s.hi, s.lo);
    s.lo += t.lo;
    s = quick_two_sum(s.hi, s.lo);
    return s;
}

在这种情况下,我回到了一个名为结构。在这种情况下,临时副本被省略?

In this case i'm returning a named struct. Can the temporary copy in this case be elided?

应该说,这是C和我在这里使用了code例子只是举例普通的问题(当我优化这个我将使用SIMD与内部函数反正)。我知道,我可以看看汇编输出,看看编译器做什么,但我认为这是一个有趣的问题仍然

It should be stated that this is a general question for C and that the code examples I have used here are only examples (when I optimize this I will be using SIMD with intrinsics anyway). I'm aware that I could look at the assembly output to see what the compiler does but I think this is an interesting question nevertheless.

推荐答案

RVO / NRVO显然在C中的为假设规则允许的。

RVO/NRVO are clearly allowed under the "as-if" rule in C.

在C ++中,你可以得到可观察到的副作用,因为你已经超载的构造函数,析构函数,和/或赋值运算符给那些副作用(例如,打印出来的东西时,这些行动之一发生),但在C你没有任何过载的那些运营商的能力,以及内置的那些有没有可观察到的副作用。

In C++ you can get observable side-effects because you've overloaded the constructor, destructor, and/or assignment operator to give those side effects (e.g., print something out when one of those operations happens), but in C you don't have any ability to overload those operators, and the built-in ones have no observable side effects.

如果没有超载他们,你得到复制省略,因此​​没有没有观察到的副作用,从做停止编译器。

Without overloading them, you get no observable side-effects from copy elision, and therefore nothing to stop a compiler from doing it.

这篇关于返回值优化,并用C复制省略的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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