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

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