-
Notifications
You must be signed in to change notification settings - Fork 3
/
safemult.h
45 lines (32 loc) · 1.36 KB
/
safemult.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
/* this header file comes from libowfat, http://www.fefe.de/libowfat/ */
#ifndef SAFEMULT_H
#define SAFEMULT_H
#include "uint16.h"
#include "uint32.h"
#include "uint64.h"
#ifdef __cplusplus
extern "C" {
#endif
#if defined(__GNUC__) && (__GNUC__ >= 5)
/* for historical reasons, the mult interface uses 0 for failure and 1
* for success, while the builtins (and my own addition and
* subtraction routines in rangecheck.h) do it the other way around. */
static inline int umult16(uint16 a,uint16 b,uint16* c) { return !__builtin_mul_overflow(a,b,c); }
static inline int imult16( int16 a, int16 b, int16* c) { return !__builtin_mul_overflow(a,b,c); }
static inline int umult32(uint32 a,uint32 b,uint32* c) { return !__builtin_mul_overflow(a,b,c); }
static inline int imult32( int32 a, int32 b, int32* c) { return !__builtin_mul_overflow(a,b,c); }
static inline int umult64(uint64 a,uint64 b,uint64* c) { return !__builtin_mul_overflow(a,b,c); }
static inline int imult64( int64 a, int64 b, int64* c) { return !__builtin_mul_overflow(a,b,c); }
#else
/* return 0 for overflow, 1 for ok */
int umult16(uint16 a,uint16 b,uint16* c);
int imult16( int16 a, int16 b, int16* c);
int umult32(uint32 a,uint32 b,uint32* c);
int imult32( int32 a, int32 b, int32* c);
int umult64(uint64 a,uint64 b,uint64* c);
int imult64( int64 a, int64 b, int64* c);
#endif
#ifdef __cplusplus
}
#endif
#endif