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.
 
 
 
 

32 lines
651 B

  1. #if defined(__GNUC__) && (__GNUC__ >= 5)
  2. #include "uint32.h"
  3. int umult32(uint32 a,uint32 b,uint32* c) { return !__builtin_mul_overflow(a,b,c); }
  4. #else
  5. #include "safemult.h"
  6. int umult32(uint32 a,uint32 b,uint32* c) {
  7. unsigned long long x=(unsigned long long)a*b;
  8. if (x>0xffffffff) return 0;
  9. *c=x&0xffffffff;
  10. return 1;
  11. }
  12. #endif
  13. #ifdef UNITTEST
  14. #include <assert.h>
  15. int main() {
  16. uint32 a;
  17. assert(umult32(4,0x80000000,&a)==0);
  18. assert(umult32(16,0x45000000,&a)==0); // make sure we don't fall for "if a*b<a && a*b<b"
  19. assert(umult32(5,10,&a)==1 && a==50);
  20. assert(umult32(6,0x10000000,&a)==1 && a==0x60000000);
  21. return 0;
  22. }
  23. #endif