【问题标题】:Code fails to execute when compiled with these flags使用这些标志编译时代码无法执行
【发布时间】:2011-10-04 13:23:49
【问题描述】:

我的代码正在尝试查找信号的熵(存储在“数据”和“帧间”中 - 在完整代码中,这些将包含信号,这里我只是输入了一些随机值)。当我使用“gcc temp.c”编译时,它编译并运行良好。 输出:

entropy: 40.174477
features: 0022FD06
features[0]: 40
entropy: 40

但是当我使用 'gcc -mstackrealign -msse -Os -ftree-vectorize temp.c' 编译时,它会编译,但无法在第 48 行之后执行。它需要具有所有四个标志才能失败 - 任意三个它们运行良好。

代码可能看起来很奇怪——我已经从一个更大的程序中删除了失败的部分。我对编译器标志的作用只有最模糊的概念,其他人将它们放入(通常有更多,但我发现这些是坏的)。

非常感谢所有帮助!

#include <stdint.h>
#include <inttypes.h>
#include <stdio.h>
#include <math.h>

static void calc_entropy(volatile int16_t *features, const int16_t* data,
const int16_t* interframe, int frame_length);


int main()
{
    int frame_length = 128;
    int16_t data[128] = {1, 2, 3, 4};
    int16_t interframe[128] = {1, 1, 1};

    int16_t a = 0;
    int16_t* features = &a;

    calc_entropy(features, data, interframe, frame_length);
    features += 1;

    fprintf(stderr, "\nentropy: %d", a);

    return 0;
}

static void calc_entropy(volatile int16_t *features, const int16_t* data,
const int16_t* interframe, int frame_length)
{
    float histo[65536] = {0};
    float* histo_zero = histo + 32768;
    volatile float entropy = 0.0f;
    int i;

    for(i=0; i<frame_length; i++){
        histo_zero[data[i]]++;
        histo_zero[interframe[i]]++;
    }

    for(i=-32768; i < 32768; i++){
        if(histo_zero[i])
            entropy -= histo_zero[i]*logf(histo_zero[i]/(float)(frame_length*2));
    }

    fprintf(stderr, "\nentropy: %f", entropy);

    fprintf(stderr, "\nfeatures: %p", features);
    features[0] = entropy; //execution fails here
    fprintf(stderr, "\nfeatures[0]: %d", features[0]);
}

编辑:我使用的是 gcc 4.5.2,采用 x86 架构。另外,如果我在运行 ubuntu (gcc -lm -mstackrealign -msse -Os -ftree-vectorize temp.c) 的 VirtualBox 上编译并运行它,它会正确执行。

Edit2:我明白了

entropy: 40.174477
features: 00000000

然后来自 windows 的消息告诉我程序已停止运行。

Edit3:在我最初发布问题后的五个月内,我已将问题更新到 gcc 4.7.0,现在代码运行良好。我回到 gcc 4.5.2,但它失败了。还是不知道为什么!

【问题讨论】:

  • 我应该数到第 48 行吗?在失败的地方添加评论。
  • 我打算尽量减少你的代码,但它适用于我,gcc 4.5.2,x86。这到底是怎么失败的?

标签: c compiler-optimization


【解决方案1】:
ottavio@magritte:/tmp$ gcc x.c -o x -lm -mstackrealign -msse -Os -ftree-vectorize
ottavio@magritte:/tmp$ ./x 

entropy: 40.174477
features: 0x7fff5fe151ce
features[0]: 40
entropy: 40
ottavio@magritte;/tmp$ gcc x.c -o x -lm
ottavio@magritte:/tmp$ ./x 

entropy: 40.174477
features: 0x7fffd7eff73e
features[0]: 40
entropy: 40
ottavio@magritte:/tmp$

那么,它有什么问题呢? gcc 4.6.1 和 x86_64 架构。

【讨论】:

    【解决方案2】:

    它似乎也在这里运行,我看到的唯一可能很时髦的是你正在获取一个 16 位值(特征 [0])并转换一个 32 位浮点数(熵)

    features[0] = entropy; //execution fails here
    

    到那个值,这当然会减少它。

    这应该没关系,但最重要的是,看看将 int16_t 值更改为 int32_t 值是否有任何区别。

    【讨论】:

    • 我试过了(用 gcc 4.5.2 编译),恐怕它没有用。自从提出这个问题以来,我已经更新到 gcc 4.7.0,并且它工作正常。不过感谢您的回答!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-02-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-23
    • 2020-07-27
    相关资源
    最近更新 更多