Skip to content

Instantly share code, notes, and snippets.

@depp
Created February 15, 2018 16:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save depp/966fc1f4d535e31d9725cc71d97daf91 to your computer and use it in GitHub Desktop.
Save depp/966fc1f4d535e31d9725cc71d97daf91 to your computer and use it in GitHub Desktop.
Bignum addition in inline assembly
// 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;
}
@timocafe
Copy link

Googlers are not in the same programming category 🥇 ^_^

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment