Created
February 15, 2018 16:48
-
-
Save depp/966fc1f4d535e31d9725cc71d97daf91 to your computer and use it in GitHub Desktop.
Bignum addition in inline assembly
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// See: https://stackoverflow.com/a/48812277/82294 | |
template <int N> | |
void add(unsigned long long *dest, unsigned long long *src) { | |
__asm__( | |
"movq (%1), %%rax" | |
"\n\taddq %%rax, (%0)" | |
"\n.local add_offset" | |
"\n.set add_offset,0" | |
"\n.rept %P2" | |
"\n.set add_offset,add_offset+8" | |
"\n\tmovq add_offset(%1), %%rax" | |
"\n\tadcq %%rax, add_offset(%0)" | |
"\n.endr" | |
: | |
: "r"(dest), "r"(src), "n"(N-1) | |
: "cc", "memory", "rax"); | |
} | |
#include <cstdio> | |
void print(const char *name, unsigned long long *n) { | |
std::fputs(name, stdout); | |
for (int i = 0; i < 4; i++) { | |
std::printf(" %016llx", n[i]); | |
} | |
std::fputc('\n', stdout); | |
} | |
int main() { | |
unsigned long long x[4] = { | |
0x0123456789abcdefull, | |
0x8000000000000000ull, | |
0x9999000000000000ull, | |
0x9999000000000000ull, | |
}; | |
unsigned long long y[4] = { | |
0x3333333333333333ull, | |
0x8123456789abcdefull, | |
0x9999000000000000ull, | |
0x9999000000000000ull, | |
}; | |
print("x: ", x); | |
print("y: ", y); | |
add<4>(x, y); | |
print("x + y: ", x); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Googlers are not in the same programming category 🥇 ^_^