是否可以保证Complex Float变量在内存中对齐8字节? [英] Is it guaranteed that Complex Float variables will be 8-byte aligned in memory?

查看:109
本文介绍了是否可以保证Complex Float变量在内存中对齐8字节?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在C99中,定义了新的复杂类型.我试图了解编译器是否可以利用此知识来优化内存访问.这些类型为complex float的对象(A-F)是否保证在内存中是8字节对齐的?

In C99 the new complex types were defined. I am trying to understand whether a compiler can take advantage of this knowledge in optimizing memory accesses. Are these objects (A-F) of type complex float guaranteed to be 8-byte aligned in memory?


#include "complex.h"
typedef complex float cfloat;

cfloat A;
cfloat B[10];

void func(cfloat C, cfloat *D)
{
    cfloat E;
    cfloat F[10];
}

请注意,对于D,问题与D指向的对象有关,而不与指针存储本身有关.而且,如果假定它是对齐的,那么如何确定传递的地址是实际的复数,而不是来自另一种(非8对齐)类型的转换?

Note that for D, the question relates to the object pointed to by D, not to the pointer storage itself. And, if that is assumed aligned, how can one be sure that the address passed is of an actual complex and not a cast from another (non 8-aligned) type?

更新1:我可能在关于D指针的最后一条评论中回答了自己. B/c无法知道将哪个地址分配给函数调用的参数,也无法保证将其对齐8.这可以通过__builtin_assumed_aligned()函数解决.

UPDATE 1: I probably answered myself in the last comment regarding the D pointer. B/c there is no way to know what address will be assigned to the parameter of the function call, there is no way to guarantee that it will be 8-aligned. This is solvable via the __builtin_assumed_aligned() function.

其他变量的问题仍然悬而未决.

The question is still open for the other variables.

更新2:我发布了推荐答案

保证float complex具有与两个float的数组相同的内存布局和对齐方式(第6.2.5节).确切的对齐方式将由您的编译器或平台定义.您可以肯定地说的是float complex至少与float对齐 .

A float complex is guaranteed to have the same memory layout and alignment as an array of two float (§6.2.5). Exactly what that alignment will be is defined by your compiler or platform. All you can say for sure is that a float complex is at least as aligned as a float.

如果假定它是对齐的,那么如何确定传递的地址是实际的复数,而不是来自另一种(非8对齐的)类型的强制转换?

if that is assumed aligned, how can one be sure that the address passed is of an actual complex and not a cast from another (non 8-aligned) type?

如果调用者向您传递了一个未充分对齐的指针,则这是未定义的行为,并且是其代码中的错误(第6.3.2.3节).您不需要支持它(尽管您可以选择支持).

If your caller passes you an insufficiently-aligned pointer, that's undefined behavior and a bug in their code (§6.3.2.3). You don't need to support that (though you may choose to).

这篇关于是否可以保证Complex Float变量在内存中对齐8字节?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆