Skip to content

Instantly share code, notes, and snippets.

@tkoz0
Last active July 9, 2023 01:02
Show Gist options
  • Save tkoz0/11dc87e7ac8117e888fbfd0f0949a10b to your computer and use it in GitHub Desktop.
Save tkoz0/11dc87e7ac8117e888fbfd0f0949a10b to your computer and use it in GitHub Desktop.
64 bit multiply with 128 bit result, compiles to native amd64 x86_64 instruction with gcc 9.4 -O3
typedef struct { uint64_t lo, hi; } mul64_t;
mul64_t mul64(uint64_t a, uint64_t b)
{
mul64_t ret;
__uint128_t c = (__uint128_t) a * (__uint128_t) b;
ret.hi = c >> 64;
ret.lo = c;
return ret;
}
/*
mul64(unsigned long, unsigned long):
mov rax, rdi
mul rsi
ret
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment