相关疑难解决方法(0)

为什么将0.1f改为0会使性能降低10倍?

为什么这段代码,

const float x[16] = {  1.1,   1.2,   1.3,     1.4,   1.5,   1.6,   1.7,   1.8,
                       1.9,   2.0,   2.1,     2.2,   2.3,   2.4,   2.5,   2.6};
const float z[16] = {1.123, 1.234, 1.345, 156.467, 1.578, 1.689, 1.790, 1.812,
                     1.923, 2.034, 2.145,   2.256, 2.367, 2.478, 2.589, 2.690};
float y[16];
for (int i = 0; i < 16; i++)
{
    y[i] = x[i];
}

for (int j = 0; j < 9000000; j++)
{
    for (int i = 0; i < 16; i++)
    {
        y[i] *= …
Run Code Online (Sandbox Code Playgroud)

c++ floating-point performance compilation visual-studio-2010

1491
推荐指数
5
解决办法
14万
查看次数

为什么Clang优化x*1.0而不是x + 0.0?

为什么Clang会优化此代码中的循环

#include <time.h>
#include <stdio.h>

static size_t const N = 1 << 27;
static double arr[N] = { /* initialize to zero */ };

int main()
{
    clock_t const start = clock();
    for (int i = 0; i < N; ++i) { arr[i] *= 1.0; }
    printf("%u ms\n", (unsigned)(clock() - start) * 1000 / CLOCKS_PER_SEC);
}
Run Code Online (Sandbox Code Playgroud)

但不是这段代码中的循环?

#include <time.h>
#include <stdio.h>

static size_t const N = 1 << 27;
static double arr[N] = { /* initialize to zero */ }; …
Run Code Online (Sandbox Code Playgroud)

c c++ floating-point optimization clang

125
推荐指数
2
解决办法
5523
查看次数

为什么编译器不能用0优化浮点加法?

我有四个身份函数,它们基本上什么都不做。只有乘法1才能通过 clang 优化为单个ret语句。

float id0(float x) {
    return x + 1 - 1;
}

float id1(float x) {
    return x + 0;
}

float id2(float x) {
    return x * 2 / 2;
}

float id3(float x) {
    return x * 1;
}
Run Code Online (Sandbox Code Playgroud)

以下编译器输出是:(clang 10, at -O3)

.LCPI0_0:
        .long   1065353216              # float 1
.LCPI0_1:
        .long   3212836864              # float -1
id0(float):                                # @id0(float)
        addss   xmm0, dword ptr [rip + .LCPI0_0]
        addss   xmm0, dword ptr [rip + .LCPI0_1] …
Run Code Online (Sandbox Code Playgroud)

c c++ optimization compilation compiler-optimization

22
推荐指数
2
解决办法
458
查看次数

c ++附加标识不安全示例(a + 0.0!= a)

MSDN文章中,它提到了启用fp:fast模式时,添加标识(a±0.0 = a,0.0-a = -a)等操作是不安全的.a+0 != a在这种模式下有什么例子吗?

编辑:正如下面提到的那样,这种问题通常在进行比较时出现.我的问题来自比较,psedocode如下所示:

for(i=0;i<v.len;i++)
{
  sum+=v[i];
  if( sum >= threshold) break;
}
Run Code Online (Sandbox Code Playgroud)

添加0(v[i])值后它会中断.在v[i]从计算不是,它被分配.我知道如果我v[i]是从计算开始,那么舍入可能会发挥作用,但为什么即使我给出v[i]零值,我仍然有这个sum < threshold但是sum + v[i] >= threshold

c++ floating-point unsafe

7
推荐指数
1
解决办法
517
查看次数