我的代码试图找到信号的熵(存储在'data'和'interframe'中 - 在完整的代码中,这些将包含信号,这里我只是放入一些随机值)。当我用'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,但它失败了。还是不知道为什么!
答案 0 :(得分:0)
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架构。
答案 1 :(得分:0)
它似乎也在这里运行,我唯一看到的可能是时髦的是你正在采用16位值(功能[0])并转换32位浮点数(熵)
features[0] = entropy; //execution fails here
进入该值,这当然会削减它。
这应该没关系,但是对于它,看看如果将int16_t值更改为int32_t值会有什么不同。