Skip to content

Instantly share code, notes, and snippets.

@qrealka
Last active June 22, 2019 01:11
Show Gist options
  • Save qrealka/273c4ef773ccce645c57d8c7725b9add to your computer and use it in GitHub Desktop.
Save qrealka/273c4ef773ccce645c57d8c7725b9add to your computer and use it in GitHub Desktop.
div 128
#include <immintrin.h>
// https://stackoverflow.com/a/8456388
// https://docs.microsoft.com/en-us/cpp/intrinsics/div128?view=vs-2019
#pragma code_seg(".text")
const unsigned char sdiv128Data[] =
{
0x48, 0x89, 0xD0, // mov rax,rdx
0x48, 0x89, 0xCA, // mov rdx,rcx
0x49, 0xF7, 0xF8, // idiv r8
0x49, 0x89, 0x11, // mov [r9],rdx
0xC3 // ret
};
__int64 (__fastcall *sdiv128)(__int64 numhi,
__int64 numlo,
__int64 den,
__int64* rem) =
(__int64 (__fastcall *)(__int64,
__int64,
__int64,
__int64*))&sdiv128Data;
__int64 div_mod(__int64 hi, __int64 low, __int64 divisor, __int64* reminder) {
return _div128(hi, low, divisor, reminder);
}
int main()
{
__int64 sr;
sdiv128(-1, -6, -2, &sr);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment