3
|
1 /* fix for MSVC ...evil! */ |
|
2 #ifdef _MSC_VER |
|
3 #define CONST64(n) n ## ui64 |
|
4 typedef unsigned __int64 ulong64; |
|
5 #else |
|
6 #define CONST64(n) n ## ULL |
|
7 typedef unsigned long long ulong64; |
|
8 #endif |
|
9 |
|
10 /* this is the "32-bit at least" data type |
|
11 * Re-define it to suit your platform but it must be at least 32-bits |
|
12 */ |
143
|
13 #if defined(__x86_64__) |
|
14 typedef unsigned ulong32; |
|
15 #else |
|
16 typedef unsigned long ulong32; |
|
17 #endif |
3
|
18 |
|
19 /* ---- HELPER MACROS ---- */ |
|
20 #ifdef ENDIAN_NEUTRAL |
|
21 |
|
22 #define STORE32L(x, y) \ |
|
23 { (y)[3] = (unsigned char)(((x)>>24)&255); (y)[2] = (unsigned char)(((x)>>16)&255); \ |
|
24 (y)[1] = (unsigned char)(((x)>>8)&255); (y)[0] = (unsigned char)((x)&255); } |
|
25 |
|
26 #define LOAD32L(x, y) \ |
|
27 { x = ((unsigned long)((y)[3] & 255)<<24) | \ |
|
28 ((unsigned long)((y)[2] & 255)<<16) | \ |
|
29 ((unsigned long)((y)[1] & 255)<<8) | \ |
|
30 ((unsigned long)((y)[0] & 255)); } |
|
31 |
|
32 #define STORE64L(x, y) \ |
|
33 { (y)[7] = (unsigned char)(((x)>>56)&255); (y)[6] = (unsigned char)(((x)>>48)&255); \ |
|
34 (y)[5] = (unsigned char)(((x)>>40)&255); (y)[4] = (unsigned char)(((x)>>32)&255); \ |
|
35 (y)[3] = (unsigned char)(((x)>>24)&255); (y)[2] = (unsigned char)(((x)>>16)&255); \ |
|
36 (y)[1] = (unsigned char)(((x)>>8)&255); (y)[0] = (unsigned char)((x)&255); } |
|
37 |
|
38 #define LOAD64L(x, y) \ |
|
39 { x = (((ulong64)((y)[7] & 255))<<56)|(((ulong64)((y)[6] & 255))<<48)| \ |
|
40 (((ulong64)((y)[5] & 255))<<40)|(((ulong64)((y)[4] & 255))<<32)| \ |
|
41 (((ulong64)((y)[3] & 255))<<24)|(((ulong64)((y)[2] & 255))<<16)| \ |
|
42 (((ulong64)((y)[1] & 255))<<8)|(((ulong64)((y)[0] & 255))); } |
|
43 |
|
44 #define STORE32H(x, y) \ |
|
45 { (y)[0] = (unsigned char)(((x)>>24)&255); (y)[1] = (unsigned char)(((x)>>16)&255); \ |
|
46 (y)[2] = (unsigned char)(((x)>>8)&255); (y)[3] = (unsigned char)((x)&255); } |
|
47 |
|
48 #define LOAD32H(x, y) \ |
|
49 { x = ((unsigned long)((y)[0] & 255)<<24) | \ |
|
50 ((unsigned long)((y)[1] & 255)<<16) | \ |
|
51 ((unsigned long)((y)[2] & 255)<<8) | \ |
|
52 ((unsigned long)((y)[3] & 255)); } |
|
53 |
|
54 #define STORE64H(x, y) \ |
|
55 { (y)[0] = (unsigned char)(((x)>>56)&255); (y)[1] = (unsigned char)(((x)>>48)&255); \ |
|
56 (y)[2] = (unsigned char)(((x)>>40)&255); (y)[3] = (unsigned char)(((x)>>32)&255); \ |
|
57 (y)[4] = (unsigned char)(((x)>>24)&255); (y)[5] = (unsigned char)(((x)>>16)&255); \ |
|
58 (y)[6] = (unsigned char)(((x)>>8)&255); (y)[7] = (unsigned char)((x)&255); } |
|
59 |
|
60 #define LOAD64H(x, y) \ |
|
61 { x = (((ulong64)((y)[0] & 255))<<56)|(((ulong64)((y)[1] & 255))<<48) | \ |
|
62 (((ulong64)((y)[2] & 255))<<40)|(((ulong64)((y)[3] & 255))<<32) | \ |
|
63 (((ulong64)((y)[4] & 255))<<24)|(((ulong64)((y)[5] & 255))<<16) | \ |
|
64 (((ulong64)((y)[6] & 255))<<8)|(((ulong64)((y)[7] & 255))); } |
|
65 |
|
66 #endif /* ENDIAN_NEUTRAL */ |
|
67 |
|
68 #ifdef ENDIAN_LITTLE |
|
69 |
|
70 #define STORE32H(x, y) \ |
|
71 { (y)[0] = (unsigned char)(((x)>>24)&255); (y)[1] = (unsigned char)(((x)>>16)&255); \ |
|
72 (y)[2] = (unsigned char)(((x)>>8)&255); (y)[3] = (unsigned char)((x)&255); } |
|
73 |
|
74 #define LOAD32H(x, y) \ |
|
75 { x = ((unsigned long)((y)[0] & 255)<<24) | \ |
|
76 ((unsigned long)((y)[1] & 255)<<16) | \ |
|
77 ((unsigned long)((y)[2] & 255)<<8) | \ |
|
78 ((unsigned long)((y)[3] & 255)); } |
|
79 |
|
80 #define STORE64H(x, y) \ |
|
81 { (y)[0] = (unsigned char)(((x)>>56)&255); (y)[1] = (unsigned char)(((x)>>48)&255); \ |
|
82 (y)[2] = (unsigned char)(((x)>>40)&255); (y)[3] = (unsigned char)(((x)>>32)&255); \ |
|
83 (y)[4] = (unsigned char)(((x)>>24)&255); (y)[5] = (unsigned char)(((x)>>16)&255); \ |
|
84 (y)[6] = (unsigned char)(((x)>>8)&255); (y)[7] = (unsigned char)((x)&255); } |
|
85 |
|
86 #define LOAD64H(x, y) \ |
|
87 { x = (((ulong64)((y)[0] & 255))<<56)|(((ulong64)((y)[1] & 255))<<48) | \ |
|
88 (((ulong64)((y)[2] & 255))<<40)|(((ulong64)((y)[3] & 255))<<32) | \ |
|
89 (((ulong64)((y)[4] & 255))<<24)|(((ulong64)((y)[5] & 255))<<16) | \ |
|
90 (((ulong64)((y)[6] & 255))<<8)|(((ulong64)((y)[7] & 255))); } |
|
91 |
|
92 #ifdef ENDIAN_32BITWORD |
|
93 |
|
94 #define STORE32L(x, y) \ |
|
95 { unsigned long __t = (x); memcpy(y, &__t, 4); } |
|
96 |
|
97 #define LOAD32L(x, y) \ |
|
98 memcpy(&(x), y, 4); |
|
99 |
|
100 #define STORE64L(x, y) \ |
|
101 { (y)[7] = (unsigned char)(((x)>>56)&255); (y)[6] = (unsigned char)(((x)>>48)&255); \ |
|
102 (y)[5] = (unsigned char)(((x)>>40)&255); (y)[4] = (unsigned char)(((x)>>32)&255); \ |
|
103 (y)[3] = (unsigned char)(((x)>>24)&255); (y)[2] = (unsigned char)(((x)>>16)&255); \ |
|
104 (y)[1] = (unsigned char)(((x)>>8)&255); (y)[0] = (unsigned char)((x)&255); } |
|
105 |
|
106 #define LOAD64L(x, y) \ |
|
107 { x = (((ulong64)((y)[7] & 255))<<56)|(((ulong64)((y)[6] & 255))<<48)| \ |
|
108 (((ulong64)((y)[5] & 255))<<40)|(((ulong64)((y)[4] & 255))<<32)| \ |
|
109 (((ulong64)((y)[3] & 255))<<24)|(((ulong64)((y)[2] & 255))<<16)| \ |
|
110 (((ulong64)((y)[1] & 255))<<8)|(((ulong64)((y)[0] & 255))); } |
|
111 |
|
112 #else /* 64-bit words then */ |
|
113 |
|
114 #define STORE32L(x, y) \ |
|
115 { unsigned long __t = (x); memcpy(y, &__t, 4); } |
|
116 |
|
117 #define LOAD32L(x, y) \ |
|
118 { memcpy(&(x), y, 4); x &= 0xFFFFFFFF; } |
|
119 |
|
120 #define STORE64L(x, y) \ |
|
121 { ulong64 __t = (x); memcpy(y, &__t, 8); } |
|
122 |
|
123 #define LOAD64L(x, y) \ |
|
124 { memcpy(&(x), y, 8); } |
|
125 |
|
126 #endif /* ENDIAN_64BITWORD */ |
|
127 |
|
128 #endif /* ENDIAN_LITTLE */ |
|
129 |
|
130 #ifdef ENDIAN_BIG |
|
131 #define STORE32L(x, y) \ |
143
|
132 { (y)[3] = (unsigned char)(((x)>>24)&255); (y)[2] = (unsigned char)(((x)>>16)&255); \ |
|
133 (y)[1] = (unsigned char)(((x)>>8)&255); (y)[0] = (unsigned char)((x)&255); } |
3
|
134 |
|
135 #define LOAD32L(x, y) \ |
143
|
136 { x = ((unsigned long)((y)[3] & 255)<<24) | \ |
|
137 ((unsigned long)((y)[2] & 255)<<16) | \ |
|
138 ((unsigned long)((y)[1] & 255)<<8) | \ |
|
139 ((unsigned long)((y)[0] & 255)); } |
3
|
140 |
|
141 #define STORE64L(x, y) \ |
143
|
142 { (y)[7] = (unsigned char)(((x)>>56)&255); (y)[6] = (unsigned char)(((x)>>48)&255); \ |
|
143 (y)[5] = (unsigned char)(((x)>>40)&255); (y)[4] = (unsigned char)(((x)>>32)&255); \ |
|
144 (y)[3] = (unsigned char)(((x)>>24)&255); (y)[2] = (unsigned char)(((x)>>16)&255); \ |
|
145 (y)[1] = (unsigned char)(((x)>>8)&255); (y)[0] = (unsigned char)((x)&255); } |
3
|
146 |
|
147 #define LOAD64L(x, y) \ |
143
|
148 { x = (((ulong64)((y)[7] & 255))<<56)|(((ulong64)((y)[6] & 255))<<48) | \ |
|
149 (((ulong64)((y)[5] & 255))<<40)|(((ulong64)((y)[4] & 255))<<32) | \ |
|
150 (((ulong64)((y)[3] & 255))<<24)|(((ulong64)((y)[2] & 255))<<16) | \ |
|
151 (((ulong64)((y)[1] & 255))<<8)|(((ulong64)((y)[0] & 255))); } |
3
|
152 |
|
153 #ifdef ENDIAN_32BITWORD |
|
154 |
|
155 #define STORE32H(x, y) \ |
|
156 { unsigned long __t = (x); memcpy(y, &__t, 4); } |
|
157 |
|
158 #define LOAD32H(x, y) \ |
|
159 memcpy(&(x), y, 4); |
|
160 |
|
161 #define STORE64H(x, y) \ |
143
|
162 { (y)[0] = (unsigned char)(((x)>>56)&255); (y)[1] = (unsigned char)(((x)>>48)&255); \ |
|
163 (y)[2] = (unsigned char)(((x)>>40)&255); (y)[3] = (unsigned char)(((x)>>32)&255); \ |
|
164 (y)[4] = (unsigned char)(((x)>>24)&255); (y)[5] = (unsigned char)(((x)>>16)&255); \ |
|
165 (y)[6] = (unsigned char)(((x)>>8)&255); (y)[7] = (unsigned char)((x)&255); } |
3
|
166 |
|
167 #define LOAD64H(x, y) \ |
143
|
168 { x = (((ulong64)((y)[0] & 255))<<56)|(((ulong64)((y)[1] & 255))<<48)| \ |
|
169 (((ulong64)((y)[2] & 255))<<40)|(((ulong64)((y)[3] & 255))<<32)| \ |
|
170 (((ulong64)((y)[4] & 255))<<24)|(((ulong64)((y)[5] & 255))<<16)| \ |
|
171 (((ulong64)((y)[6] & 255))<<8)| (((ulong64)((y)[7] & 255))); } |
3
|
172 |
|
173 #else /* 64-bit words then */ |
|
174 |
|
175 #define STORE32H(x, y) \ |
|
176 { unsigned long __t = (x); memcpy(y, &__t, 4); } |
|
177 |
|
178 #define LOAD32H(x, y) \ |
|
179 { memcpy(&(x), y, 4); x &= 0xFFFFFFFF; } |
|
180 |
|
181 #define STORE64H(x, y) \ |
|
182 { ulong64 __t = (x); memcpy(y, &__t, 8); } |
|
183 |
|
184 #define LOAD64H(x, y) \ |
|
185 { memcpy(&(x), y, 8); } |
|
186 |
|
187 #endif /* ENDIAN_64BITWORD */ |
|
188 #endif /* ENDIAN_BIG */ |
|
189 |
|
190 #define BSWAP(x) ( ((x>>24)&0x000000FFUL) | ((x<<24)&0xFF000000UL) | \ |
|
191 ((x>>8)&0x0000FF00UL) | ((x<<8)&0x00FF0000UL) ) |
|
192 |
|
193 #ifdef _MSC_VER |
|
194 |
|
195 /* instrinsic rotate */ |
|
196 #include <stdlib.h> |
|
197 #pragma intrinsic(_lrotr,_lrotl) |
|
198 #define ROR(x,n) _lrotr(x,n) |
|
199 #define ROL(x,n) _lrotl(x,n) |
|
200 |
143
|
201 #elif defined(__GNUC__) && (defined(__i386__) || defined(__x86_64__)) && !defined(INTEL_CC) |
3
|
202 |
143
|
203 static inline unsigned ROL(unsigned word, int i) |
3
|
204 { |
|
205 __asm__("roll %%cl,%0" |
|
206 :"=r" (word) |
|
207 :"0" (word),"c" (i)); |
|
208 return word; |
|
209 } |
|
210 |
143
|
211 static inline unsigned ROR(unsigned word, int i) |
3
|
212 { |
|
213 __asm__("rorl %%cl,%0" |
|
214 :"=r" (word) |
|
215 :"0" (word),"c" (i)); |
|
216 return word; |
|
217 } |
|
218 |
|
219 #else |
|
220 |
|
221 /* rotates the hard way */ |
|
222 #define ROL(x, y) ( (((unsigned long)(x)<<(unsigned long)((y)&31)) | (((unsigned long)(x)&0xFFFFFFFFUL)>>(unsigned long)(32-((y)&31)))) & 0xFFFFFFFFUL) |
|
223 #define ROR(x, y) ( ((((unsigned long)(x)&0xFFFFFFFFUL)>>(unsigned long)((y)&31)) | ((unsigned long)(x)<<(unsigned long)(32-((y)&31)))) & 0xFFFFFFFFUL) |
|
224 |
|
225 #endif |
|
226 |
143
|
227 #if defined(__GNUCC__) && defined(__x86_64__) |
|
228 |
|
229 static inline unsigned long ROL64(unsigned long word, int i) |
|
230 { |
|
231 __asm__("rolq %%cl,%0" |
|
232 :"=r" (word) |
|
233 :"0" (word),"c" (i)); |
|
234 return word; |
|
235 } |
|
236 |
|
237 static inline unsigned long ROR64(unsigned long word, int i) |
|
238 { |
|
239 __asm__("rorq %%cl,%0" |
|
240 :"=r" (word) |
|
241 :"0" (word),"c" (i)); |
|
242 return word; |
|
243 } |
|
244 |
|
245 #else |
|
246 |
3
|
247 #define ROL64(x, y) \ |
|
248 ( (((x)<<((ulong64)(y)&63)) | \ |
|
249 (((x)&CONST64(0xFFFFFFFFFFFFFFFF))>>((ulong64)64-((y)&63)))) & CONST64(0xFFFFFFFFFFFFFFFF)) |
|
250 |
|
251 #define ROR64(x, y) \ |
|
252 ( ((((x)&CONST64(0xFFFFFFFFFFFFFFFF))>>((ulong64)(y)&CONST64(63))) | \ |
|
253 ((x)<<((ulong64)(64-((y)&CONST64(63)))))) & CONST64(0xFFFFFFFFFFFFFFFF)) |
|
254 |
143
|
255 #endif |
|
256 |
3
|
257 #undef MAX |
|
258 #undef MIN |
|
259 #define MAX(x, y) ( ((x)>(y))?(x):(y) ) |
|
260 #define MIN(x, y) ( ((x)<(y))?(x):(y) ) |
|
261 |
|
262 /* extract a byte portably */ |
|
263 #ifdef _MSC_VER |
|
264 #define byte(x, n) ((unsigned char)((x) >> (8 * (n)))) |
|
265 #else |
|
266 #define byte(x, n) (((x) >> (8 * (n))) & 255) |
|
267 #endif |