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

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