问题描述
我了解字对齐,这使cpu在将整数读入寄存器时只需要读一次.
I understand word-alignment, which makes the cpu only need to read once when reading an integer into a register.
但是 8字节对齐(假设32位系统)对于双精度"是必要的吗?有什么好处?如果用于存储双精度"的空间仅为4字节对齐,将会发生什么情况?
But is 8-byte alignment (let's assume 32bit system) for "double" necessary? What is the benefit? What will happen if the space for storing a "double" is just 4-byte alignment?
推荐答案
有许多硬件组件可能会因未对齐的装载或存储而受到不利影响.
There are multiple hardware components that may be adversely affected by unaligned loads or stores.
- 到内存的接口可能是八个字节宽,并且只能以八个字节的倍数访问内存.装入未对齐的八字节双精度数则需要在总线上进行两次读取.存储的情况更糟,因为对齐的八字节存储区可以简单地将八个字节写入内存,但是未对齐的八字节存储区必须读取两个八字节的存储区,将新数据与旧数据合并,然后写入两个八字节的存储区
- 高速缓存行通常为32或64字节.如果将八个字节的对象对齐为八个字节的倍数,则每个对象仅在一个高速缓存行中.如果它们未对齐,则某些对象将部分位于一个高速缓存行中,而部分则位于另一高速缓存行中.然后,加载或存储这些对象需要使用两条高速缓存行而不是一条.这种影响发生在所有级别的缓存中(在现代处理器中,这三个级别并不罕见).
- 内存系统页面通常为512字节或更多.同样,每个对齐的对象仅在一页中,但是一些未对齐的对象在多页中.被访问的每个页面都需要硬件资源:虚拟地址必须转换为物理地址,这可能需要访问转换表,并且必须检测到地址冲突. (处理器可能同时运行多个加载和存储操作.即使您的程序看起来是单线程的,处理器也会提前读取指令并尝试执行它可能的指令.因此,处理器可能会在执行之前启动加载指令指令已经完成,但是,为确保不会引起错误,处理器会检查每个加载指令,以确保它没有从先前存储指令正在更改的地址中加载.部分已加载数据必须单独检查.)
系统对不对齐操作的响应因系统而异.一些系统被设计为仅支持对齐的访问.在这些情况下,未对齐的访问会导致导致程序终止的异常,或者会导致执行特殊处理程序的异常,这些处理程序会在软件中模拟未对齐的操作(通过执行对齐的操作并根据需要合并数据).诸如此类的软件处理程序比硬件操作要慢得多.
The response of the system to unaligned operations varies from system to system. Some systems are designed to support only aligned accesses. In these cases, unaligned accesses either cause exceptions that lead to program termination or exceptions that cause execution of special handlers that emulate unaligned operations in software (by performing aligned operations and merging the data as necessary). Software handlers such as these are much slower than hardware operations.
某些系统支持未对齐访问,但是与对齐访问相比,这通常消耗更多的硬件资源.在最佳情况下,硬件将执行两项操作,而不是一项.但是某些硬件被设计为像对齐操作一样开始操作,然后在发现操作未对齐时中止操作,并使用硬件中的不同路径重新开始以处理未对齐的操作.在这种系统中,未对齐的访问会严重影响性能,尽管它不如软件处理未对齐的访问的系统严重.
Some systems support unaligned accesses, but this usually consumes more hardware resources than aligned accesses. In the best case, the hardware performs two operations instead of one. But some hardware is designed to start operations as if they were aligned and then, upon discovering the operation is not aligned, to abort it and start over using different paths in the hardware to handle the unaligned operation. In such systems, unaligned accesses have a significant performance penalty, although it is not as great as in systems where software handles unaligned accesses.
在某些系统中,硬件可能具有多个负载存储执行单元,它们可以执行未对齐访问所需的两个操作,就像一个单元可以执行对齐访问的操作一样快.因此,不对齐访问不会直接导致性能下降.但是,由于多个执行单元由于未对齐的访问而处于忙碌状态,因此它们无法执行其他操作.因此,通常以并行方式执行许多负载存储操作的程序,与未对齐的访问相比,未对齐的访问将执行得更慢.
In some systems, the hardware may have multiple load-store execution units that can perform the two operations required of unaligned accesses just as quickly as one unit can perform the operation of aligned accesses. So there is no direct performance degradation of unaligned accesses. However, because multiple execution units are kept busy by unaligned accesses, they are unavailable to perform other operations. Thus, programs that perform many load-store operations, normally in parallel, will execute more slowly with unaligned accesses than with aligned accesses.
这篇关于8字节对齐是否为"double"?类型是否必要?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!