GCC 中的 C 函数对齐

发布于 2024-08-15 04:48:38 字数 489 浏览 3 评论 0原文

我正在尝试使用“aligned(16)”属性将函数字节对齐到 16 字节边界。我执行了以下操作: void __attribute__((aligned(16))) function() { }

(来源:http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html)

但是当我编译时 (gcc foo.c ; 没有 makefiles 或链接器脚本使用),我收到以下错误:

FOO.c:99: error:alignment may not bespecified for 'function'

我也尝试对齐到 4,8,32 等,但错误仍然相同。 我需要它来调整基于 powerpc 的处理器的中断服务例程。这样做的正确方法是什么?

I am trying to byte-align a function to 16-byte boundary using the 'aligned(16)' attribute. I did the following: void __attribute__((aligned(16))) function() { }

(Source: http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html)

But when I compile (gcc foo.c ; no makefiles or linker scripts used), I get the following error:

FOO.c:99: error: alignment may not be specified for 'function'

I tried aligning to 4,8,32, etc as well but the error remains the same.
I need this to align an Interrupt Service Routine for a powerpc-based processor. What is the correct way of doing so ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

戏蝶舞 2024-08-22 04:48:38

根据我对这个 GCC 问题的回答,你可以尝试使用#pragma指令,如下所示:

    #pragma GCC push_options
    #pragma GCC optimize ("align-functions=16")

    //add 5 to each element of the int array.
    void add5(int a[20]) {
        int i = 19;
        for(; i > 0; i--) {
            a[i] += 5;
        }
    }

    #pragma GCC pop_options

#pragma push_optionspop_options宏用于控制optimize的范围 pragma 的效果。有关这些宏的更多详细信息,请参阅 GCC 文档

或者,如果您更喜欢GCC 的属性语法< /strong>,您应该能够执行以下操作:

    //add 5 to each element of the int array.
    __attribute__((optimize("align-functions=16")))
    void add5(int a[20]) {
        int i = 19;
        for(; i > 0; i--) {
            a[i] += 5;
        }
    }

Adapting from my answer on this GCC question, you might try using #pragma directives, like so:

    #pragma GCC push_options
    #pragma GCC optimize ("align-functions=16")

    //add 5 to each element of the int array.
    void add5(int a[20]) {
        int i = 19;
        for(; i > 0; i--) {
            a[i] += 5;
        }
    }

    #pragma GCC pop_options

The #pragma push_options and pop_options macros are used to control the scope of the optimize pragma's effect. More details about these macros can be found in the GCC docs.

Alternately, if you prefer GCC's attribute syntax, you should be able to do something like:

    //add 5 to each element of the int array.
    __attribute__((optimize("align-functions=16")))
    void add5(int a[20]) {
        int i = 19;
        for(; i > 0; i--) {
            a[i] += 5;
        }
    }
迷乱花海 2024-08-22 04:48:38

为什么编译时不直接将 -falign-functions=16 传递给 gcc 呢?

Why don't you just pass the -falign-functions=16 to gcc when compiling?

那伤。 2024-08-22 04:48:38

您可能使用的是不支持该属性的旧版本 gcc。您提供的文档链接适用于 gcc 的“当前开发”。浏览各个版本,该属性仅出现在 gcc 4.3 及更高版本的文档中。

You are probably using an older version of gcc that does not support that attribute. The documentation link you provided is for the "current development" of gcc. Looking through the various releases, the attribute only appears in the documentation for gcc 4.3 and beyond.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文