1 #ifndef R2_TYPES_BASE_H
2 #define R2_TYPES_BASE_H
3 
4 #include <ctype.h>
5 #include <sys/types.h>
6 #include <limits.h>
7 
8 #define cut8 const unsigned char
9 #define ut64 unsigned long long
10 #define st64 long long
11 #define ut32 unsigned int
12 #define st32 int
13 #define ut16 unsigned short
14 #define st16 short
15 #define ut8 unsigned char
16 #define st8 signed char
17 #define boolt int
18 
19 #if defined(_MSC_VER)
20 # define R_ALIGNED(x) __declspec(align(x))
21 #else
22 # define R_ALIGNED(x) __attribute__((aligned(x)))
23 #endif
24 
25 typedef R_ALIGNED(1) ut16 uut16;
26 typedef R_ALIGNED(1) ut32 uut32;
27 typedef R_ALIGNED(1) ut64 uut64;
28 typedef R_ALIGNED(1) st16 ust16;
29 typedef R_ALIGNED(1) st32 ust32;
30 typedef R_ALIGNED(1) st64 ust64;
31 
32 typedef union {
33 	ut8 v8;
34 	ut16 v16;
35 	ut32 v32;
36 	ut64 v64;
37 } utAny;
38 
39 typedef struct _ut80 {
40 	ut64 Low;
41 	ut16 High;
42 } ut80;
43 typedef struct _ut96 {
44 	ut64 Low;
45 	ut32 High;
46 } ut96;
47 typedef struct _ut128 {
48 	ut64 Low;
49 	st64 High;
50 } ut128;
51 typedef struct _ut256 {
52 	ut128 Low;
53 	ut128 High;
54 } ut256;
55 typedef struct _utX {
56 	ut80 v80;
57 	ut96 v96;
58 	ut128 v128;
59 	ut256 v256;
60 } utX;
61 
62 #include <stdbool.h>
63 
64 #define R_EMPTY { 0 }
65 #define R_EMPTY2 {{ 0 }}
66 
67 /* limits */
68 #undef UT64_MAX
69 #undef UT64_GT0
70 #undef UT64_LT0
71 #undef UT64_MIN
72 #undef UT32_MAX
73 #undef UT32_MIN
74 #undef UT16_MIN
75 #undef UT8_MIN
76 #define ST64_MAX ((st64)0x7FFFFFFFFFFFFFFFULL)
77 #define ST64_MIN ((st64)(-ST64_MAX-1))
78 #define UT64_MAX 0xFFFFFFFFFFFFFFFFULL
79 #define UT64_GT0 0x8000000000000000ULL
80 #define UT64_LT0 0x7FFFFFFFFFFFFFFFULL
81 #define UT64_MIN 0ULL
82 #define UT64_32U 0xFFFFFFFF00000000ULL
83 #define UT64_16U 0xFFFFFFFFFFFF0000ULL
84 #define UT64_8U  0xFFFFFFFFFFFFFF00ULL
85 #define UT32_MIN 0U
86 #define UT16_MIN 0U
87 #define UT32_GT0 0x80000000U
88 #define UT32_LT0 0x7FFFFFFFU
89 #define ST32_MAX 0x7FFFFFFF
90 #define ST32_MIN (-ST32_MAX-1)
91 #define UT32_MAX 0xFFFFFFFFU
92 #define UT32_MIN 0U
93 #define ST16_MAX 0x7FFF
94 #define ST16_MIN (-ST16_MAX-1)
95 #define UT16_GT0 0x8000U
96 #define UT16_MAX 0xFFFFU
97 #define ST8_MAX  0x7F
98 #define ST8_MIN  (-ST8_MAX - 1)
99 #define UT8_GT0  0x80U
100 #define UT8_MAX  0xFFU
101 #define UT8_MIN  0x00U
102 #define ASCII_MIN 32
103 #define ASCII_MAX 127
104 
105 #if SSIZE_MAX == ST32_MAX
106 #define SZT_MAX  UT32_MAX
107 #define SZT_MIN  UT32_MIN
108 #define SSZT_MAX  ST32_MAX
109 #define SSZT_MIN  ST32_MIN
110 #else
111 #define SZT_MAX  UT64_MAX
112 #define SZT_MIN  UT64_MIN
113 #define SSZT_MAX  ST64_MAX
114 #define SSZT_MIN  ST64_MIN
115 #endif
116 
117 #define UT64_ALIGN(x) (x + (x - (x % sizeof (ut64))))
118 #define UT32_ALIGN(x) (x + (x - (x % sizeof (ut32))))
119 #define UT16_ALIGN(x) (x + (x - (x % sizeof (ut16))))
120 
121 #define UT32_LO(x) ((ut32)((x)&UT32_MAX))
122 #define UT32_HI(x) ((ut32)(((ut64)(x))>>32)&UT32_MAX)
123 
124 #define R_BETWEEN(x,y,z) (((y)>=(x)) && ((y)<=(z)))
125 #define R_ROUND(x,y) ((x)%(y))?(x)+((y)-((x)%(y))):(x)
126 #define R_DIM(x,y,z) (((x)<(y))?(y):((x)>(z))?(z):(x))
127 #ifndef R_MAX_DEFINED
128 #define R_MAX(x,y) (((x)>(y))?(x):(y))
129 #define R_MAX_DEFINED
130 #endif
131 #ifndef R_MIN_DEFINED
132 #define R_MIN(x,y) (((x)>(y))?(y):(x))
133 #define R_MIN_DEFINED
134 #endif
135 #define R_ABS(x) (((x)<0)?-(x):(x))
136 #define R_BTW(x,y,z) (((x)>=(y))&&((y)<=(z)))?y:x
137 
138 #include "r_types_overflow.h"
139 
140 /* copied from bithacks.h */
141 #define B_IS_SET(x, n)   (((x) & (1ULL << (n)))? 1: 0)
142 #define B_SET(x, n)      ((x) |= (1ULL << (n)))
143 #define B_EVEN(x)        (((x) & 1) == 0)
144 #define B_ODD(x)         (!B_EVEN((x)))
145 #define B_UNSET(x, n)    ((x) &= ~(1ULL << (n)))
146 #define B_TOGGLE(x, n)   ((x) ^= (1ULL << (n)))
147 
148 #define B11111 31
149 #define B11110 30
150 #define B11101 29
151 #define B11100 28
152 #define B11011 27
153 #define B11010 26
154 #define B11001 25
155 #define B11000 24
156 #define B10111 23
157 #define B10110 22
158 #define B10101 21
159 #define B10100 20
160 #define B10011 19
161 #define B10010 18
162 #define B10001 17
163 #define B10000 16
164 #define B1111 15
165 #define B1110 14
166 #define B1101 13
167 #define B1100 12
168 #define B1011 11
169 #define B1010 10
170 #define B1001 9
171 #define B1000 8
172 #define B0111 7
173 #define B0110 6
174 #define B0101 5
175 #define B0100 4
176 #define B0011 3
177 #define B0010 2
178 #define B0001 1
179 #define B0000 0
180 #undef B
181 #define B4(a,b,c,d) ((a<<12)|(b<<8)|(c<<4)|(d))
182 
183 /* portable non-c99 inf/nan types */
184 #if !defined(INFINITY)
185 #define INFINITY (1.0f/0.0f)
186 #endif
187 
188 #if !defined(NAN)
189 #define NAN (0.0f/0.0f)
190 #endif
191 
192 /* A workaround against libc headers redefinition of __attribute__:
193  * Standard include has lines like
194  * #if (GCC_VERSION < 2007)
195  * # define __attribute__(x)
196  * #endif
197  * So we have do remove this define for TinyCC compiler
198  */
199 #if defined(__TINYC__) && (GCC_VERSION < 2007)
200 #undef __attribute__
201 #endif
202 
203 #ifdef _MSC_VER
204 #define R_PACKED( __Declaration__ ) __pragma( pack(push, 1) ) __Declaration__ __pragma( pack(pop) )
205 #undef INFINITY
206 #undef NAN
207 #elif defined(__GNUC__) || defined(__TINYC__)
208 #define R_PACKED( __Declaration__ ) __Declaration__ __attribute__((__packed__))
209 #endif
210 
211 #if APPLE_SDK_IPHONESIMULATOR
212 #undef LIBC_HAVE_FORK
213 #define LIBC_HAVE_FORK 0
214 #undef DEBUGGER
215 #define DEBUGGER 0
216 #endif
217 
218 #define HEAPTYPE(x) \
219 	static x* x##_new(x n) {\
220 		x *m = malloc(sizeof (x));\
221 		return m? *m = n, m: m; \
222 	}
223 
224 #endif // R2_TYPES_BASE_H
225