Consider the code below. We know that a __uint128_t variable is stored in 2 64-bit registers (Assume x64 processor). Requirement is to store the first 64 bits in one unsigned long variable and the next 64 bits in another unsigned long variable.
__uint128_t a = SOMEVALUE;
unsigned long b = a&0xffffffffffffffff;
unsigned long c = a>>64;
Here, b stores the first 64 bits and c the next 64 bits. Is there any other, simpler way to access the 2 registers separately instead of performing & and >> operations? I ask this because for my project, this section of code will be executed for like a trillion+ times. So it's better to verify this doubt first.
Anything with assembly code I can fool around with?
What you have written is probably best, although truncation by casting is easier to read than the long constant. As a rule of thumb, if you write code that's obvious and clear, that's usually easiest for your compiler to see your intent and optimise appropriately.
On Compiler Explorer, I supplied this function:
When compiled for x64 with
gcc -O3, it produces exactly the code you want: