I wrote some code as below:
char abcd [10] = {0x12, 0x34, 0x56, 0x78, 0x11, 0x22, 0x33};
atomic<int> * data = (atomic<int> *)&abcd[1]; // to let data not aligned with 16/32/64 bit
extern void test() {
auto result = data->load(memory_order_relaxed);
//printf("data:%p , %x\n", data, result);
}
The ASM code is:
0000000000001140 <_Z4testv>:
1140: f3 0f 1e fa endbr64
1144: 48 8b 05 d5 2e 00 00 mov 0x2ed5(%rip),%rax # 4020 <data>
114b: 8b 00 mov (%rax),%eax
114d: c3 retq
114e: 66 90 xchg %ax,%ax
If I enable "printf("data:%p , %x\n", data, result);" in test function, the output is: data:0x55f85a60b011 , 11785634
My question is:
The int data is not aligned with 16/32/64 bit, but why atomic load just is "mov (%rax),%eax", as I know, not aligned data is not atomic, seems "lock mov (%rax),%eax" is expected result, what's the reason?
From the printf output 0x11785634:
0000000000004010 <abcd>:
4010: 12 34 56 adc (%rsi,%rdx,2),%dh
4013: 78 11 js 4026 <data+0x6>
4015: 22 33 and (%rbx),%dh
from the print output, data is read form byte4011 to byte4044, the unaligned 4 bytes read can atomic?