对象数组对齐与 __attribute__aligned() 或 alignas()
Object array alignment with __attribute__aligned() Or alignas()?
快速提问伙计们...这些代码刺具有相同的对齐方式吗?
struct sse_t {
float sse_data[4];
};
// the array "cacheline" will be aligned to 64-byte boundary
struct sse_t alignas(64) cacheline[1000000];
或
// every object of type sse_t will be aligned to 64-byte boundary
struct sse_t {
float sse_data[4];
} __attribute((aligned(64)));
struct sse_t cacheline[1000000];
这些代码刺具有相同的对齐方式吗?
差一点。你的两个例子实际上非常不同。
在第一个示例中,您将获得一个sse_t
对象的数组。sse_t
对象仅保证 4 字节对齐。但由于整个数组都对齐到 64 字节,因此每个 sse_t
对象都将正确对齐以供 SSE 访问。
在第二个示例中,您强制每个sse_t
对象对齐到 64 字节。但每个sse_t
对象只有 16 个字节。因此,阵列将大 4 倍。(每个sse_t
对象的末尾将有 48 个字节的填充)。
struct objA {
float sse_data[4];
};
struct objB {
float sse_data[4];
} __attribute((aligned(64)));
int main(){
cout << sizeof(objA) << endl;
cout << sizeof(objB) << endl;
}
输出:
16
64
我很确定第二种情况不是你想要的。
但是为什么要对齐到 64 个字节?http://ideone.com/JNEIBR
#include <iostream>
using namespace std;
struct sse_t1 {
float sse_data[4];
};
// the array "cacheline" will be aligned to 64-byte boundary
struct sse_t1 alignas(64) cacheline1[1000000];
// every object of type sse_t will be aligned to 64-byte boundary
struct sse_t2 {
float sse_data[4];
} __attribute((aligned(64)));
struct sse_t2 cacheline2[1000000];
int main() {
cout << "sizeof(sse_t1) = " << sizeof(sse_t1) << endl;
cout << "sizeof(sse_t2) = " << sizeof(sse_t2) << endl;
cout << "array cacheline1 " << (((size_t)(cacheline1) % 64 == 0)?"aligned to 64":"not aligned to 64") << endl;
cout << "array cacheline2 " << (((size_t)(cacheline2) % 64 == 0)?"aligned to 64":"not aligned to 64") << endl;
cout << "cacheline1[0] - cacheline1[1] = " << (size_t)&(cacheline1[1]) - (size_t)&(cacheline1[0]) << endl;
cout << "cacheline2[0] - cacheline2[1] = " << (size_t)&(cacheline2[1]) - (size_t)&(cacheline2[0]) << endl;
return 0;
}
输出:
sizeof(sse_t1) = 16
sizeof(sse_t2) = 64
array cacheline1 aligned to 64
array cacheline2 aligned to 64
cacheline1[0] - cacheline1[1] = 16
cacheline2[0] - cacheline2[1] = 64
相关文章:
- 编译器如何实现__declspec(align(x)) / __attribute__(aligned(x)))?
- alignas() 对 sizeof() 的影响 - 强制性的?
- 属性语法中的 C++ alignas 说明符
- 为什么 alignas() 不采用参数包?
- 创建alignas(64)int的向量
- 为什么对Aligned STD ::数组的初始自动矢量载荷是标量?(g /clang )
- 对象数组对齐与 __attribute__aligned() 或 alignas()
- __attribute__((aligned(x)) 不适用于动态分配
- alignas(T) 未在模板函数中解析
- 32位平台上结构中的alignas
- 使用alignas对齐结构
- 内存对齐:如何使用alignof / alignas
- C++14标准布局类型是否可以对字段使用“alignas”
- 我可以向编译器查询有关 C++0x "alignas"支持的信息吗?
- alignas specifier vs __attribute__(aligned), c++11
- 如何模拟alignas(T)