“minimum alignment” 和 “preferred alignment” 之间的区别是什么?

huangapple go评论102阅读模式
英文:

What is the difference between "minimum alignment" and "preferred alignment"?

问题

最近我观察到,在Clang 9.0中,alignof和**__alignof对于unsigned long long**返回不同的值,这个问题已经在https://reviews.llvm.org/D54814中讨论过:

从Clang 8.0和GCC 8.0开始,alignof__alignof在某些情况下返回不同的值。具体而言,alignof_Alignof返回类型的最小对齐方式,而__alignof返回首选对齐方式。

我了解类型对齐,但从未遇到过"最小对齐"和"首选对齐"。

请问有人能帮助我理解这两者究竟是什么,以及它们之间的区别吗?谢谢。

英文:

Recently I observed that on Clang 9.0 alignof and __alignof are returning different values for unsigned long long and the same has been discussed at https://reviews.llvm.org/D54814:

> Starting in Clang 8.0 and GCC 8.0, alignof and __alignof return different values in same cases. Specifically alignof and _Alignof return the minimum alignment for a type, where as __alignof returns the preferred alignment.

I know about type alignment but never came across "minimum alignment" and "preferred alignment".

Could somebody please help me understand what exactly these are and what is the difference? Thanks.

答案1

得分: 1

最小对齐是(在给定平台上)不会导致崩溃的对齐方式。在 x86-64 上,它是一个字节。在 PowerPC 或 Sparc 或 RISC-V 上,可能是 4 或 8 个字节。

首选对齐是通常的对齐方式,例如因为处理器总线或 CPU 缓存。对于 x86-64 上的 unsigned long long,可能是 8 个字节。任何较低对齐的访问都会导致性能损失。

详细信息取决于目标处理器和 ABI。请考虑交叉编译器。

C 或 C++ 的语义并不完全定义和完全规范化。查看 C++ 草案标准:它是用英语编写的,没有规范化。但也可以查看 Frama-C(它有一个用于 C++ 的实验性前端)和 CompCert。阅读关于未定义行为的内容。

英文:

The minimal alignment is (on a given platform) the one which won't give crashes. On x86-64 it is one byte. On PowerPC or Sparc or RISC-V it is probably 4 or 8 bytes.

The preferred alignment is the one which is usual, e.g. because of processor bus or CPU caches. On x86-64 for unsigned long long it probably is 8 bytes. Any less aligned access has a performance penalty.

Details are target processor and ABI specific (for example, see this). Think of cross-compilers.

The semantics of C or of C++ is not perfectly defined and not fully formalized. Look into the C++ draft standard: it is written in English, not formalized. But see also Frama-C (it has an experimental front end for C++) and CompCert. Read about undefined behavior.

huangapple
  • 本文由 发表于 2020年1月6日 22:49:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/59614125.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定