Mirror of :pserver:cvs@cvs.fefe.de:/cvs libowfat https://www.fefe.de/libowfat/
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

63 lines
1.3 KiB

  1. #if defined(__GNUC__) && (__GNUC__ >= 5)
  2. #include "uint64.h"
  3. int umult64(uint64 a,uint64 b,uint64* c) { return !__builtin_mul_overflow(a,b,c); }
  4. #else
  5. #include "haveuint128.h"
  6. #include "safemult.h"
  7. #if defined(HAVE_UINT128)
  8. int umult64(uint64 a,uint64 b,uint64* c) {
  9. __uint128_t x=((__uint128_t)a)*b;
  10. if ((*c=(uint64)x) != x) return 0;
  11. return 1;
  12. }
  13. #else
  14. /* return 1 for overflow, 0 for ok */
  15. int umult64(uint64 a,uint64 b,uint64* c) {
  16. uint32 ahi=a>>32;
  17. uint32 alo=(a&0xffffffff);
  18. uint32 bhi=b>>32;
  19. uint32 blo=(b&0xffffffff);
  20. // a=ahi*x+alo, b=bhi*x+blo
  21. // a*b = (ahi*x+alo) * (bhi*x+blo)
  22. // = ahi*x*bhi*x + ahi*x*blo + alo*bhi*x + alo*blo
  23. // -> overflow if ahi*bhi != zero */
  24. if (ahi && bhi) return 0;
  25. a=(uint64)(ahi)*blo+(uint64)(alo)*bhi;
  26. if (a>0xffffffff) return 0;
  27. {
  28. uint64 x=(uint64)(alo)*blo;
  29. if (x+(a<<32) < x) return 0;
  30. *c=x+(a<<32);
  31. }
  32. return 1;
  33. }
  34. #endif
  35. #endif
  36. #ifdef UNITTEST
  37. #include <assert.h>
  38. int main() {
  39. uint64 a;
  40. assert(umult64(4,0x8000000000000000ull,&a)==0);
  41. assert(umult64(16,0x4500000000000000ull,&a)==0); // make sure we don't fall for "if a*b<a && a*b<b"
  42. assert(umult64(5,10,&a)==1 && a==50);
  43. assert(umult64(0x60000000,0x10000000,&a)==1 && a==0x600000000000000);
  44. return 0;
  45. }
  46. #endif