Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Naturally aligned memory address

I need to extract a memory address from within an existing 64-bit value, and this address points to a 4K array, the starting value is:

0x000000030c486000

The address I need is stored within bits 51:12, so I extract those bits using:

address = start >> 12 & 0x0000007FFFFFFFFF

This leaves me with the address of:

0x000000000030c486

However, the documentation I'm reading states that the array stored at the address is 4KB in size, and naturally aligned.

I'm a little bit confused over what naturally aligned actually means. I know with page aligned stuff the address normally ends with '000' (although I could be wrong on that).

I'm assuming that as the address taken from the starting value is only 40 bits long, I need to perform an additional bitshifting operation to arrange the bits so that they can be correctly interpreted any further.

If anyone could offer some advice on doing this, I'd appreciate it.

Thanks

like image 658
Tony Avatar asked Mar 19 '13 15:03

Tony


People also ask

What does naturally aligned mean?

Normally, "naturally aligned" means that any item is aligned to at least a multiple of its own size. For example, a 4-byte object is aligned to an address that's a multiple of 4, an 8-byte object is aligned to an address that's a multiple of 8, etc.

What does it mean for memory to be aligned?

Alignment refers to the arrangement of data in memory, and specifically deals with the issue of accessing data as proper units of information from main memory. First we must conceptualize main memory as a contiguous block of consecutive memory locations. Each location contains a fixed number of bits.

What is meant by aligned address?

The alignment of the access refers to the address being a multiple of the transfer size. For example, an aligned 32 bit access will have the bottom 4 bits of the address as 0x0, 0x4, 0x8 and 0xC assuming the memory is byte addressed. An unaligned address is then an address that isn't a multiple of the transfer size.

Why is natural alignment important?

The main purpose of natural alignment is to avoid misaligned access to data members in the structure — which can slow things down (sometimes radically — the DEC Alpha was particularly bad).


3 Answers

Normally, "naturally aligned" means that any item is aligned to at least a multiple of its own size. For example, a 4-byte object is aligned to an address that's a multiple of 4, an 8-byte object is aligned to an address that's a multiple of 8, etc.

For an array, you don't normally look at the size of the whole array, but at the size of an element of the array.

Likewise, for a struct or union, you normally look at the size of the largest element.

like image 177
Jerry Coffin Avatar answered Oct 06 '22 08:10

Jerry Coffin


Natural alignment requires that every N byte access must be aligned on a memory address boundary of N. We can express this in terms of the modulus operator: addr % N must be zero. for examples:

Accessing 4 bytes of memory from address 0x10004 is aligned (0x10004 % 4 = 0).

Accessing 4 bytes of memory from address 0x10005 is unaligned (0x10005 % 4 = 1).

like image 28
Sumit Kumar Avatar answered Oct 06 '22 07:10

Sumit Kumar


From a hardware perspective, memory is typically divided into chunks of some size, such that any or all of the data within a chunk can be read or written in a single operation, but any single operation can only affect data within a single chunk.

A typical 80386-era system would have memory grouped into four-byte chunks. Accessing a two-byte or four-byte value which fit entirely within a single chunk would require one operation. If the value was stored partially in one chunk and partially in another, two operations would be required.

Over the years, chunk sizes have gotten larger than data sizes, to the point that most randomly-placed 32-bit values would fit entirely within a chunk, but a second issue may arise with some processors: if a chunk is e.g. 512 bits (64 bytes) and a 32-bit word is known to be aligned at a multiple of four bytes (32 bits), fetching each bit of the word can come from any of 16 places. If the word weren't known to be aligned, each bit could come from any of 61 places for the cases where the word fits entirely within the chunk. The circuitry to quickly select from among 61 choices is more complex than circuitry to select among 16, and most code will use aligned data, so even in cases where an unaligned word would fit within a single accessible chunk, hardware might still need a little extra time to extract it.

like image 2
supercat Avatar answered Oct 06 '22 07:10

supercat