Avoid undefined behavior.
[tinc] / src / ed25519 / fixedint.h
1 /*
2     Portable header to provide the 32 and 64 bits type.
3
4     Not a compatible replacement for <stdint.h>, do not blindly use it as such.
5 */
6
7 #ifndef __TINC_FIXEDINT_H__
8 #define __TINC_FIXEDINT_H__
9
10 #if ((defined(__STDC__) && __STDC__ && __STDC_VERSION__ >= 199901L) || (defined(__WATCOMC__) && (defined(_STDINT_H_INCLUDED) || __WATCOMC__ >= 1250)) || (defined(__GNUC__) && (defined(_STDINT_H) || defined(_STDINT_H_) || defined(__UINT_FAST64_TYPE__)) )) && !defined(FIXEDINT_H_INCLUDED)
11     #include <stdint.h>
12     #define FIXEDINT_H_INCLUDED
13
14     #if defined(__WATCOMC__) && __WATCOMC__ >= 1250 && !defined(UINT64_C)
15         #include <limits.h>
16         #define UINT64_C(x) (x + (UINT64_MAX - UINT64_MAX))
17     #endif
18 #endif
19
20
21 #ifndef FIXEDINT_H_INCLUDED
22     #define FIXEDINT_H_INCLUDED
23
24     /* (u)int32_t */
25     #ifndef uint32_t
26         #if (ULONG_MAX == 0xffffffffUL)
27             typedef unsigned long uint32_t;
28         #elif (UINT_MAX == 0xffffffffUL)
29             typedef unsigned int uint32_t;
30         #elif (USHRT_MAX == 0xffffffffUL)
31             typedef unsigned short uint32_t;
32         #endif
33     #endif
34
35
36     #ifndef int32_t
37         #if (LONG_MAX == 0x7fffffffL)
38             typedef signed long int32_t;
39         #elif (INT_MAX == 0x7fffffffL)
40             typedef signed int int32_t;
41         #elif (SHRT_MAX == 0x7fffffffL)
42             typedef signed short int32_t;
43         #endif
44     #endif
45
46
47     /* (u)int64_t */
48     #if (defined(__STDC__) && defined(__STDC_VERSION__) && __STDC__ && __STDC_VERSION__ >= 199901L)
49         typedef long long int64_t;
50         typedef unsigned long long uint64_t;
51
52         #define UINT64_C(v) v ##ULL
53         #define INT64_C(v) v ##LL
54     #elif defined(__GNUC__)
55         __extension__ typedef long long int64_t;
56         __extension__ typedef unsigned long long uint64_t;
57
58         #define UINT64_C(v) v ##ULL
59         #define INT64_C(v) v ##LL
60     #elif defined(__MWERKS__) || defined(__SUNPRO_C) || defined(__SUNPRO_CC) || defined(__APPLE_CC__) || defined(_LONG_LONG) || defined(_CRAYC)
61         typedef long long int64_t;
62         typedef unsigned long long uint64_t;
63
64         #define UINT64_C(v) v ##ULL
65         #define INT64_C(v) v ##LL
66     #elif (defined(__WATCOMC__) && defined(__WATCOM_INT64__)) || (defined(_MSC_VER) && _INTEGRAL_MAX_BITS >= 64) || (defined(__BORLANDC__) && __BORLANDC__ > 0x460) || defined(__alpha) || defined(__DECC)
67         typedef __int64 int64_t;
68         typedef unsigned __int64 uint64_t;
69
70         #define UINT64_C(v) v ##UI64
71         #define INT64_C(v) v ##I64
72     #endif
73 #endif
74
75 static inline unsigned char shlu8(unsigned char a, uint32_t b) {
76         return a << b;
77 }
78
79 static inline int32_t shl32(uint32_t a, uint32_t b) {
80         return a << b;
81 }
82
83 static inline int64_t shl64(uint64_t a, uint32_t b) {
84         return a << b;
85 }
86
87 static inline uint64_t shlu64(uint64_t a, uint32_t b) {
88         return a << b;
89 }
90
91 #endif