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.
 
 
 
 

31 lines
540 B

#if defined(__GNUC__) && (__GNUC__ >= 5)
#include "uint32.h"
int imult32( int32 a, int32 b, int32* c) { return !__builtin_mul_overflow(a,b,c); }
#else
#include "safemult.h"
int imult32(int32 a,int32 b,int32* c) {
int64 x=(int64)a*b;
if ((int32)x != x) return 0;
*c=x;
return 1;
}
#endif
#ifdef UNITTEST
#include <assert.h>
int main() {
int32 b;
assert(imult32(0x40000000,2,&b)==0);
assert(imult32(-0x40000000,2,&b)==1 && b==-0x80000000ll);
assert(imult32(0x3fffffff,2,&b)==1 && b==0x7ffffffe);
return 0;
}
#endif