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.
 
 
 
 

46 lines
1.4 KiB

  1. /* this header file comes from libowfat, http://www.fefe.de/libowfat/ */
  2. #ifndef SAFEMULT_H
  3. #define SAFEMULT_H
  4. #include "uint16.h"
  5. #include "uint32.h"
  6. #include "uint64.h"
  7. #ifdef __cplusplus
  8. extern "C" {
  9. #endif
  10. #if defined(__GNUC__) && (__GNUC__ >= 5)
  11. /* for historical reasons, the mult interface uses 0 for failure and 1
  12. * for success, while the builtins (and my own addition and
  13. * subtraction routines in rangecheck.h) do it the other way around. */
  14. static inline int umult16(uint16 a,uint16 b,uint16* c) { return !__builtin_mul_overflow(a,b,c); }
  15. static inline int imult16( int16 a, int16 b, int16* c) { return !__builtin_mul_overflow(a,b,c); }
  16. static inline int umult32(uint32 a,uint32 b,uint32* c) { return !__builtin_mul_overflow(a,b,c); }
  17. static inline int imult32( int32 a, int32 b, int32* c) { return !__builtin_mul_overflow(a,b,c); }
  18. static inline int umult64(uint64 a,uint64 b,uint64* c) { return !__builtin_mul_overflow(a,b,c); }
  19. static inline int imult64( int64 a, int64 b, int64* c) { return !__builtin_mul_overflow(a,b,c); }
  20. #else
  21. /* return 0 for overflow, 1 for ok */
  22. int umult16(uint16 a,uint16 b,uint16* c);
  23. int imult16( int16 a, int16 b, int16* c);
  24. int umult32(uint32 a,uint32 b,uint32* c);
  25. int imult32( int32 a, int32 b, int32* c);
  26. int umult64(uint64 a,uint64 b,uint64* c);
  27. int imult64( int64 a, int64 b, int64* c);
  28. #endif
  29. #ifdef __cplusplus
  30. }
  31. #endif
  32. #endif