8

我正在尝试制作一个使用 GCC 编译并使用 Qt 和 SSE 内在函数的程序。似乎当我的一个函数被 Qt 调用时,堆栈对齐不会被保留。这是一个简短的例子来说明我的意思:

#include <cstdio>
#include <emmintrin.h>
#include <QtGui/QApplication.h>
#include <QtGui/QWidget.h>


class Widget: public QWidget {
public:
    void paintEvent(QPaintEvent *) {
        __m128 a;
        printf("a: 0x%08x\n", ((void *) &a));
    }
};


int main(int argc, char** argv)
{
    QApplication application(argc, argv);
    Widget w;
    w.paintEvent(NULL); // Called from here, my function behaves correctly
    w.show();
    w.update();
    // Qt will call Widget::paintEvent and my __m128 will not be
    // aligned on 16 bytes as it should
    application.processEvents();

    return 0;
}

这是输出:

a: 0x0023ff40 // OK, that's aligned on 16 bytes
a: 0x0023d14c // Not aligned!

配置:

  • 英特尔酷睿2
  • 操作系统,SP3
  • GCC 4.4(Mingw 包含在 Qt SDK 2010.01 中)

我尝试使用与我在 Qt makefile 中看到的相同的选项来编译示例程序:

-O2 -Wall -frtti -fexceptions -mthreads

,链接选项:

-enable-stdcall-fixup -Wl,-enable-auto-import -Wl,-enable-runtime-pseudo-reloc -Wl,-s -mthreads

现在我不知道在哪个方向搜索。任何提示将不胜感激。谢谢!

法比安

4

2 回答 2

10

您可以使用该选项-mstackrealign来执行此操作,而无需向源代码添加属性:

-mstackrealign 在入口处重新对齐堆栈。在 Intel x86 上,-mstackrealign 选项将生成备用序言和尾声,必要时重新对齐运行时堆栈。这支持将保留 4 字节对齐堆栈的旧代码与保留 16 字节堆栈以实现 SSE 兼容性的现代代码混合。另请参见适用于单个函数的属性 force_align_arg_pointer。

(来自GCC 文档

于 2010-03-06T17:05:42.657 回答
5
__attribute__((force_align_arg_pointer)) void paintEvent(QPaintEvent *);

让它工作!有人有更好的解决方案吗?

于 2010-03-05T14:40:12.773 回答