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
540 B

  1. #if defined(__GNUC__) && (__GNUC__ >= 5)
  2. #include "uint32.h"
  3. int imult32( int32 a, int32 b, int32* c) { return !__builtin_mul_overflow(a,b,c); }
  4. #else
  5. #include "safemult.h"
  6. int imult32(int32 a,int32 b,int32* c) {
  7. int64 x=(int64)a*b;
  8. if ((int32)x != x) return 0;
  9. *c=x;
  10. return 1;
  11. }
  12. #endif
  13. #ifdef UNITTEST
  14. #include <assert.h>
  15. int main() {
  16. int32 b;
  17. assert(imult32(0x40000000,2,&b)==0);
  18. assert(imult32(-0x40000000,2,&b)==1 && b==-0x80000000ll);
  19. assert(imult32(0x3fffffff,2,&b)==1 && b==0x7ffffffe);
  20. return 0;
  21. }
  22. #endif