flat assembler
Message board for the users of flat assembler.

Index > Main > unaligned small data loading in a register

Author
Thread Post new topic Reply to topic
sylware



Joined: 23 Oct 2020
Posts: 717
Location: Marseille/France
sylware 15 Nov 2026, 14:06
Should I byte-load this data, or go bigger loads (short, dword) but unaligned?

(on modern µarchitectures)
Post 15 Nov 2026, 14:06
View user's profile Send private message Reply with quote
bitRAKE



Joined: 21 Jul 2003
Posts: 4680
Location: vpcmpistri
bitRAKE 15 Nov 2026, 17:20
It's always cache line. Do "LOCK XADD BYTE [RBX], AL" and the cache line is being locked.

So, when ask, "Do we go larger?" the performance question should be, "Do we cross a cache line?"

You say unaligned and that means at least 1/64 (word) or 3/64 (dword) of the time the instruction is paying double. (Assuming 64 byte cache line.)

If the data is already in the cache this can be a small cost indeed.
Post 15 Nov 2026, 17:20
View user's profile Send private message Visit poster's website Reply with quote
bitRAKE



Joined: 21 Jul 2003
Posts: 4680
Location: vpcmpistri
bitRAKE 15 Nov 2026, 17:47
bitRAKE wrote:
If the data is already in the cache this can be a small cost indeed.
OF course, it's more complicated than that. Modern processors have automatic prefetch. This means it's trying to predict reads and get that data closer to the processor.

So, the next question we should ask, "Is the load predictable?"
Code:
tier    cycles
L1      3 - 5
L2      10 - 15
L3      30 - 70
mem     150 - 300
TLB     800 (miss)    
Of course, it's more complicated than that: TLB (Translation Lookaside Buffer) misses can compound these cache access latencies during memory paging. This means a random page straddling read could cost ~2000 cycles (or much worse).
Post 15 Nov 2026, 17:47
View user's profile Send private message Visit poster's website Reply with quote
sylware



Joined: 23 Oct 2020
Posts: 717
Location: Marseille/France
sylware 15 Nov 2026, 22:42
Let's presume the data is in one L1 cache line, but unaligned.

Byte loading with many instructions or less instructions but with unaligned big loads?

EDIT: it seems there is near 0 penalty, namely would mean it is better to use less machine instructions hence unaligned big memory loads. But it is obviously a machine instruction which becomes "heavy/complicated" the second you cross a cache line boundary in big loads. I rather not use that feature/optimization from a µarchitecture unless in some hardware specific super hardcore hot loops.
Post 15 Nov 2026, 22:42
View user's profile Send private message Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  


< Last Thread | Next Thread >
Forum Rules:
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Copyright © 1999-2026, Tomasz Grysztar. Also on GitHub, YouTube.

Website powered by rwasa.