2006-08-27 13:57:06 +00:00
|
|
|
#ifdef __x86_64__
|
|
|
|
|
2006-08-27 22:23:45 +00:00
|
|
|
/* WARNING: this only works if compiled with -fomit-frame-pointer */
|
2006-08-27 13:57:06 +00:00
|
|
|
void imult64() {
|
|
|
|
asm volatile(
|
|
|
|
"xchgq %rdx,%rsi\n"
|
|
|
|
"movq %rdi,%rax\n"
|
|
|
|
"imulq %rdx\n"
|
|
|
|
"jc 1f\n" /* overflow */
|
|
|
|
"movq %rax,(%rsi)\n"
|
|
|
|
"xorq %rax,%rax\n"
|
|
|
|
"inc %rax\n"
|
|
|
|
"ret\n"
|
|
|
|
"1:\n"
|
|
|
|
"xorq %rax,%rax\n"
|
|
|
|
/* the closing ret is renerated by gcc */
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
2003-08-22 15:03:10 +00:00
|
|
|
#include "safemult.h"
|
|
|
|
|
|
|
|
int imult64(int64 a,int64 b,int64* c) {
|
|
|
|
int neg=(a<0);
|
2004-01-06 23:35:06 +00:00
|
|
|
uint64 d;
|
2003-08-22 15:03:10 +00:00
|
|
|
if (neg) a=-a;
|
|
|
|
if (b<0) { neg^=1; b=-b; }
|
2005-07-15 20:57:07 +00:00
|
|
|
if (!umult64(a,b,&d)) return 0;
|
|
|
|
if (d > 0x7fffffffffffffffULL + neg) return 0;
|
2004-01-06 23:35:06 +00:00
|
|
|
*c=(neg?-d:d);
|
2003-09-05 21:25:51 +00:00
|
|
|
return 1;
|
2003-08-22 15:03:10 +00:00
|
|
|
}
|
|
|
|
|
2006-08-27 13:57:06 +00:00
|
|
|
#endif
|