【发布时间】: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。这到底是怎么失败的?