简体   繁体   English

Qt,GCC,SSE和堆栈对齐

[英]Qt, GCC, SSE and stack alignment

I'm trying to make a program compiled with GCC and using Qt and SSE intrinsics. 我正在尝试使用GCC编译程序并使用Qt和SSE内在函数。 It seems that when one of my functions is called by Qt, the stack alignment is not preserved. 似乎当我的一个函数被Qt调用时,堆栈对齐不会被保留。 Here's a short example to illustrate what I mean : 这是一个简短的例子来说明我的意思:

#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;
}

Here's the output: 这是输出:

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

Configuration: 组态:

  • Intel Core2 英特尔酷睿2
  • WinXP, SP3 WinXP,SP3
  • GCC 4.4 (Mingw included in the Qt SDK 2010.01) GCC 4.4(Mingw包含在Qt SDK 2010.01中)

I tried to compile the example program with the same options as those I saw in the Qt makefile : 我尝试使用与我在Qt makefile中看到的相同的选项编译示例程序:

-O2 -Wall -frtti -fexceptions -mthreads

,link options: ,链接选项:

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

Now I don't know in which directions to search. 现在我不知道在哪个方向搜索。 Any hints would be appreciated. 任何提示将不胜感激。 Thanks! 谢谢!

Fabien 法比恩

You can use the option -mstackrealign to do that without adding attributes to your source code: 您可以使用选项-mstackrealign执行此操作,而无需向源代码添加属性:

-mstackrealign Realign the stack at entry. -mstackrealign在入口处重新对齐堆栈。 On the Intel x86, the -mstackrealign option will generate an alternate prologue and epilogue that realigns the runtime stack if necessary. 在Intel x86上,-mstackrealign选项将生成备用序言和结尾,以便在必要时重新调整运行时堆栈。 This supports mixing legacy codes that keep a 4-byte aligned stack with modern codes that keep a 16-byte stack for SSE compatibility. 这支持将保留4字节对齐堆栈的传统代码与现代代码混合,后者保持16字节堆栈以实现SSE兼容性。 See also the attribute force_align_arg_pointer, applicable to individual functions. 另请参见适用于各个函数的属性force_align_arg_pointer。

(from the GCC docs ) (来自海湾合作委员会文件

__attribute__((force_align_arg_pointer)) void paintEvent(QPaintEvent *);

made it work! 让它工作! Does anybody have a better solution? 有人有更好的解决方案吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM