Chinaunix

标题: 字节对齐问题 [打印本页]

作者: weishigoname    时间: 2012-05-15 15:19
标题: 字节对齐问题
struct tet
{
        //long long p;
        // long  ss[100];
         long  ss[100];
       
};
struct test
{       
        //long long p[3];
        char c;
        struct tet ss;
       
        //short cc;
}__attribute__((aligned));

上面这个 sizeof(struct test) 为 416

把 long  ss[100]; 改成 char  ss[100]; 为112
把 long  ss[100]; 改成 short  ss[100]; 为208
把 long  ss[100]; 改成long long  ss[100]; 为 816

不知道这是怎么对齐的, char c放前放后都一样
作者: bruceteen    时间: 2012-05-15 15:44
这几个数都能被16整除
作者: L_kernel    时间: 2012-05-15 16:00
align只是对齐而已,关系的应该是其起始地址。

packed关注的才是大小吧?
作者: weishigoname    时间: 2012-05-15 16:22
回复 2# bruceteen


    struct pp
{
        //long long pp;
        char c;
        //short cc;
}__attribute__((aligned));
看你的回复后,试了下这个 sizeof(struct pp) 是 16
呵呵,默认16字节对齐阿
作者: weishigoname    时间: 2012-05-15 16:32
我试了一下把__attribute__((aligned))去掉
发现 sizeof(struct test)又分别是101, 202,404,804
为啥最后一个不是808呢 ?
作者: bruceteen    时间: 2012-05-15 18:35
回复 5# weishigoname
说明gcc在32位平台中默认状态下 long long 也是按4字节对齐的
我猜超过机器字长对齐没什么意义吧
作者: weishigoname    时间: 2012-05-15 19:35
回复 6# bruceteen


    这让我感到迷惑
为什么加__attribute__((aligned)) 会变成 16字节对齐 ?
不加又成不超过4字节,按理说,如果加__attribute__((aligned))后,就应该是4字节对齐阿
作者: folklore    时间: 2012-05-15 19:48
回复 6# bruceteen

in x86, it seems so...
it seems no cpu instruction that can access data that large than cpu word.

but dma (i/o) device can do it.
so if your device has this ability and this ability is required, it is needed to override the aligned setting of gcc

   
作者: bruceteen    时间: 2012-05-15 20:08
weishigoname 发表于 2012-05-15 19:35
如果加__attribute__((aligned))后,就应该是4字节对齐阿


我依稀记得aligned不指定对齐字节数,就是使用最大字节数,而不是默认字节数
作者: bruceteen    时间: 2012-05-15 20:12
回复 8# folklore
妇科老人你好啊,他乡遇故知吆,哈哈
你说的我明白,像以前写MMX/SSE等鸟代码就要对齐得很大
作者: walleeee    时间: 2012-05-15 20:12
回复 9# bruceteen


    16字节对齐居多。
作者: safedead    时间: 2012-05-16 09:43
[quote][size=2][color=#000]walleeee 发表于 2012-05-15 20:12[/color] [url=forum.php?mod=redirect&goto=findpost&pid=22101886&ptid=3743667][img]static/image/common/back.gif[/img][/url][/size]
回复 9# bruceteen


[/quote]

昨天看Intel的一篇程序开发优化手册,多次提到16字节对齐,这是其中一段
...
Assembly/Compiler Coding Rule 45. (H impact, H generality) Align data on
natural operand size address boundaries. If the data will be accessed with vector
instruction loads and stores, align the data on 16-byte boundaries.
...

文中还多次强调要时刻记住cache line的大小是64字节(当前主流Intel x86/x64),尽量不要跨64字节边界访问以提高CACHE的效率
....
Misaligned data access can incur significant performance penalties. This is particu-
larly true for cache line splits. The size of a cache line is 64 bytes in the Pentium 4 and
other recent Intel processors, including processors based on Intel Core microarchi-
tecture.

....

On Intel Core 2 Duo, Intel Core Duo, Intel Core Solo, Pentium 4, Intel Xeon and
Pentium M processors, memory coherence is maintained on 64-byte cache lines
(rather than 32-byte cache lines. as in earlier processors). This can increase the
opportunity for false sharing.


这里我有个疑问,那就是针对64字节cache line大小而产生的优化手段一般是编译器干的事情吧
C程序员需要考虑吗?
作者: folklore    时间: 2012-05-16 10:31
回复 10# bruceteen
**. Happy every day~~


   
作者: walleeee    时间: 2012-05-16 14:34
回复 12# safedead


    不要考虑




欢迎光临 Chinaunix (http://bbs.chinaunix.net/) Powered by Discuz! X3.2