This repository has been archived by the owner on Jul 15, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
numeric.h
110 lines (94 loc) · 4.03 KB
/
numeric.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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
/* dtls -- a very basic DTLS implementation
*
* Copyright (C) 2011 Olaf Bergmann <[email protected]>
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without
* restriction, including without limitation the rights to use, copy,
* modify, merge, publish, distribute, sublicense, and/or sell copies
* of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#ifndef _NUMERIC_H_
#define _NUMERIC_H_
#include <stdint.h>
#ifndef min
#define min(A,B) ((A) <= (B) ? (A) : (B))
#endif
#ifndef max
#define max(A,B) ((A) < (B) ? (B) : (A))
#endif
/**
* Increments given \p Var of type \p Type by \c 1.
*
* \hideinitializer
*/
#define inc_uint(Type,Var) { \
int i = sizeof(Type); \
while (i && !++((Var)[--i])); \
}
/* this one is for consistency... */
#define dtls_int_to_uint8(Field,Value) do { \
*(unsigned char*)(Field) = (Value) & 0xff; \
} while(0)
#define dtls_int_to_uint16(Field,Value) do { \
*(unsigned char*)(Field) = ((Value) >> 8) & 0xff; \
*(((unsigned char*)(Field))+1) = ((Value) & 0xff); \
} while(0)
#define dtls_int_to_uint24(Field,Value) do { \
*(unsigned char*)(Field) = ((Value) >> 16) & 0xff; \
dtls_int_to_uint16((((unsigned char*)(Field))+1),Value); \
} while(0)
#define dtls_int_to_uint32(Field,Value) do { \
*(unsigned char*)(Field) = ((Value) >> 24) & 0xff; \
*(((unsigned char*)(Field))+1) = ((Value) >> 16) & 0xff; \
*(((unsigned char*)(Field))+2) = ((Value) >> 8) & 0xff; \
*(((unsigned char*)(Field))+3) = (Value) & 0xff; \
} while(0)
#define dtls_ulong_to_uint48(Field,Value) do { \
*(unsigned char*)(Field) = ((Value) >> 40) & 0xff; \
*(((unsigned char*)(Field))+1) = ((Value) >> 32) & 0xff; \
*(((unsigned char*)(Field))+2) = ((Value) >> 24) & 0xff; \
*(((unsigned char*)(Field))+3) = ((Value) >> 16) & 0xff; \
*(((unsigned char*)(Field))+4) = ((Value) >> 8) & 0xff; \
*(((unsigned char*)(Field))+5) = (Value) & 0xff; \
} while(0)
#define dtls_ulong_to_uint64(Field,Value) do { \
*(unsigned char*)(Field) = ((Value) >> 56) & 0xff; \
*(((unsigned char*)(Field))+1) = ((Value) >> 48) & 0xff; \
*(((unsigned char*)(Field))+2) = ((Value) >> 40) & 0xff; \
*(((unsigned char*)(Field))+3) = ((Value) >> 32) & 0xff; \
*(((unsigned char*)(Field))+4) = ((Value) >> 24) & 0xff; \
*(((unsigned char*)(Field))+5) = ((Value) >> 16) & 0xff; \
*(((unsigned char*)(Field))+6) = ((Value) >> 8) & 0xff; \
*(((unsigned char*)(Field))+7) = (Value) & 0xff; \
} while(0)
#define dtls_uint8_to_int(Field) \
(*(unsigned char*)(Field) & 0xFF)
#define dtls_uint16_to_int(Field) \
(((*(unsigned char*)(Field)) << 8) | (*(((unsigned char*)(Field))+1)))
#define dtls_uint24_to_int(Field) \
(((*(((unsigned char*)(Field)))) << 16) \
| ((*(((unsigned char*)(Field))+1)) << 8) \
| ((*(((unsigned char*)(Field))+2))))
#define dtls_uint48_to_ulong(Field) \
(((uint64_t) *(unsigned char*)(Field)) << 40) \
| (((uint64_t) *(((unsigned char*)(Field))+1)) << 32) \
| (((uint64_t) *(((unsigned char*)(Field))+2)) << 24) \
| (((uint64_t) *(((unsigned char*)(Field))+3)) << 16) \
| (((uint64_t) *(((unsigned char*)(Field))+4)) << 8) \
| (((uint64_t) *(((unsigned char*)(n))+5)))
#endif /* _NUMERIC_H_ */