Why is alignment important?

I know that some processors fail with inconsistent data, while others, such as oh-so-common x86, will only be slower with this.

My question is why? Why is it harder for an x86 processor to get data from a 0x12345679 pointer than from a 0x12345678 pointer? To be clear, I know that page errors can occur if the data is on multiple pages, and I understand that more data may need to be extracted from memory (one part for the beginning of the value and one for the end), but this is not always true. and that’s not what my question is about. I ask, why is it always slower?

Suppose the memory starts at 0x10000000 . Why is it harder for a processor to get 2 short bytes from 0x10000001 than from 0x10000002 ? Why is it harder to get a 4-byte int from 0x10000001 than from 0x10000000 ? And so on.

+6
alignment cpu byte
source share
3 answers

A processor can only access memory in different ways. This is a consequence of the interaction between the processor and memory.

When a processor supports non-standard reads, what actually happens is a processor that produces two separate reads (or one for a larger read) and stapling parts together, so it’s slower than aligned reading.

+3
source share

Because the data bus is wider than eight bits.

Suppose the data bus is 32 bits. To get 16 bits from the address 0x10000001, it must receive four bytes, which starts with 0x10000000 and shift the value to get two bytes in the middle.

To get 16 bits from the address 0x10000003, he must get the words starting with 0x10000000 and 0x10000004, and use one byte from each value.

+4
source share

One example: if the data bus has 32 bits and the 32-bit value is not on the 32-bit boundary, the bytes must be extracted in more than one operation and moved to load the value into the processor register correctly.

+1
source share

All Articles