To make it specific, I only want to know why on my 64 bit mac, the Swift compiler says the alignment of some types like Float80
is 16.
To check the memory alignment requirement of a type, I use the alignof
function.
sizeof(Float80) // ~> 16 bytes, it only needs 10 bytes, but because of hardware design decisions it has to be a power of 2
strideof(Float80) // ~> 16 bytes, clear because it is exact on a power of 2, struct types with Float80 in it, can be bigger
alignof(Float80) // ~> 16 bytes, why not 8 bytes, like String ?
I understand the memory alignment of types less or equal the size of the word is beneficial.
sizeof(String) // ~> 24 bytes, clear because 24 is multiple of 8
strideof(String) // ~> 24 bytes, clear because 24 is multiple of 8
alignof(String) // ~> 8 bytes, clear because something greater or equal to 8 bytes should align to 8 bytes
Many types with a bigger memory size footprint like String
(with a size of 24) does have a memory alignment requirement of 8 bytes. I expect that is the size of my CPU/RAM bus in use, because I have a 64 bit mac and os.
I check the size of the type without the last padding with the sizeof
function and with the adding padding to the end with the strideof
function (strideof is more helpful in arrays of structs, Swift then adds bytes to the end to reach the next multiple of the alignment requirement.)
I understand that padding is necessary for types lesser or equal than the size of 8 byte.
But I don't understand why it is advantageous to have a memory alignment requirement bigger than 8 bytes on my 64 bit mac.
Float80 needs 80 bits for its value, that are 10 bytes, with 6 filler bytes.
Here is an image to make it more clear, what I mean. The green positions are allowed for a Float80, the red positions not. The memory is in 8 byte chunks in this picture.