]> pd.if.org Git - uuid/blob - tomcrypt_macros.h
remove unused variable from makefile
[uuid] / tomcrypt_macros.h
1 /* fix for MSVC ...evil! */
2 #if 0
3 #ifdef _MSC_VER
4    #define CONST64(n) n ## ui64
5    typedef unsigned __int64 ulong64;
6 #else
7    #define CONST64(n) n ## ULL
8    typedef unsigned long long ulong64;
9 #endif
10
11 /* this is the "32-bit at least" data type 
12  * Re-define it to suit your platform but it must be at least 32-bits 
13  */
14 #if defined(__x86_64__) || (defined(__sparc__) && defined(__arch64__))
15    typedef unsigned ulong32;
16 #else
17    typedef unsigned long ulong32;
18 #endif
19 #endif
20
21 /* ---- HELPER MACROS ---- */
22 #ifdef ENDIAN_NEUTRAL
23
24 #define STORE32L(x, y)                                                                     \
25      { (y)[3] = (unsigned char)(((x)>>24)&255); (y)[2] = (unsigned char)(((x)>>16)&255);   \
26        (y)[1] = (unsigned char)(((x)>>8)&255); (y)[0] = (unsigned char)((x)&255); }
27
28 #define LOAD32L(x, y)                            \
29      { x = ((unsigned long)((y)[3] & 255)<<24) | \
30            ((unsigned long)((y)[2] & 255)<<16) | \
31            ((unsigned long)((y)[1] & 255)<<8)  | \
32            ((unsigned long)((y)[0] & 255)); }
33
34 #define STORE64L(x, y)                                                                     \
35      { (y)[7] = (unsigned char)(((x)>>56)&255); (y)[6] = (unsigned char)(((x)>>48)&255);   \
36        (y)[5] = (unsigned char)(((x)>>40)&255); (y)[4] = (unsigned char)(((x)>>32)&255);   \
37        (y)[3] = (unsigned char)(((x)>>24)&255); (y)[2] = (unsigned char)(((x)>>16)&255);   \
38        (y)[1] = (unsigned char)(((x)>>8)&255); (y)[0] = (unsigned char)((x)&255); }
39
40 #define LOAD64L(x, y)                                                       \
41      { x = (((ulong64)((y)[7] & 255))<<56)|(((ulong64)((y)[6] & 255))<<48)| \
42            (((ulong64)((y)[5] & 255))<<40)|(((ulong64)((y)[4] & 255))<<32)| \
43            (((ulong64)((y)[3] & 255))<<24)|(((ulong64)((y)[2] & 255))<<16)| \
44            (((ulong64)((y)[1] & 255))<<8)|(((ulong64)((y)[0] & 255))); }
45
46 #define STORE32H(x, y)                                                                     \
47      { (y)[0] = (unsigned char)(((x)>>24)&255); (y)[1] = (unsigned char)(((x)>>16)&255);   \
48        (y)[2] = (unsigned char)(((x)>>8)&255); (y)[3] = (unsigned char)((x)&255); }
49
50 #define LOAD32H(x, y)                            \
51      { x = ((unsigned long)((y)[0] & 255)<<24) | \
52            ((unsigned long)((y)[1] & 255)<<16) | \
53            ((unsigned long)((y)[2] & 255)<<8)  | \
54            ((unsigned long)((y)[3] & 255)); }
55
56 #define STORE64H(x, y)                                                                     \
57    { (y)[0] = (unsigned char)(((x)>>56)&255); (y)[1] = (unsigned char)(((x)>>48)&255);     \
58      (y)[2] = (unsigned char)(((x)>>40)&255); (y)[3] = (unsigned char)(((x)>>32)&255);     \
59      (y)[4] = (unsigned char)(((x)>>24)&255); (y)[5] = (unsigned char)(((x)>>16)&255);     \
60      (y)[6] = (unsigned char)(((x)>>8)&255); (y)[7] = (unsigned char)((x)&255); }
61
62 #define LOAD64H(x, y)                                                      \
63    { x = (((ulong64)((y)[0] & 255))<<56)|(((ulong64)((y)[1] & 255))<<48) | \
64          (((ulong64)((y)[2] & 255))<<40)|(((ulong64)((y)[3] & 255))<<32) | \
65          (((ulong64)((y)[4] & 255))<<24)|(((ulong64)((y)[5] & 255))<<16) | \
66          (((ulong64)((y)[6] & 255))<<8)|(((ulong64)((y)[7] & 255))); }
67
68 #endif /* ENDIAN_NEUTRAL */
69
70 #ifdef ENDIAN_LITTLE
71
72 #if !defined(LTC_NO_BSWAP) && (defined(INTEL_CC) || (defined(__GNUC__) && (defined(__DJGPP__) || defined(__CYGWIN__) || defined(__MINGW32__) || defined(__i386__) || defined(__x86_64__))))
73
74 #define STORE32H(x, y)           \
75 asm __volatile__ (               \
76    "bswapl %0     \n\t"          \
77    "movl   %0,(%1)\n\t"          \
78    "bswapl %0     \n\t"          \
79       ::"r"(x), "r"(y));
80
81 #define LOAD32H(x, y)          \
82 asm __volatile__ (             \
83    "movl (%1),%0\n\t"          \
84    "bswapl %0\n\t"             \
85    :"=r"(x): "r"(y));
86
87 #else
88
89 #define STORE32H(x, y)                                                                     \
90      { (y)[0] = (unsigned char)(((x)>>24)&255); (y)[1] = (unsigned char)(((x)>>16)&255);   \
91        (y)[2] = (unsigned char)(((x)>>8)&255); (y)[3] = (unsigned char)((x)&255); }
92
93 #define LOAD32H(x, y)                            \
94      { x = ((unsigned long)((y)[0] & 255)<<24) | \
95            ((unsigned long)((y)[1] & 255)<<16) | \
96            ((unsigned long)((y)[2] & 255)<<8)  | \
97            ((unsigned long)((y)[3] & 255)); }
98
99 #endif
100
101
102 /* x86_64 processor */
103 #if !defined(LTC_NO_BSWAP) && (defined(__GNUC__) && defined(__x86_64__))
104
105 #define STORE64H(x, y)           \
106 asm __volatile__ (               \
107    "bswapq %0     \n\t"          \
108    "movq   %0,(%1)\n\t"          \
109    "bswapq %0     \n\t"          \
110       ::"r"(x), "r"(y));
111
112 #define LOAD64H(x, y)          \
113 asm __volatile__ (             \
114    "movq (%1),%0\n\t"          \
115    "bswapq %0\n\t"             \
116    :"=r"(x): "r"(y));
117
118 #else
119
120 #define STORE64H(x, y)                                                                     \
121    { (y)[0] = (unsigned char)(((x)>>56)&255); (y)[1] = (unsigned char)(((x)>>48)&255);     \
122      (y)[2] = (unsigned char)(((x)>>40)&255); (y)[3] = (unsigned char)(((x)>>32)&255);     \
123      (y)[4] = (unsigned char)(((x)>>24)&255); (y)[5] = (unsigned char)(((x)>>16)&255);     \
124      (y)[6] = (unsigned char)(((x)>>8)&255); (y)[7] = (unsigned char)((x)&255); }
125
126 #define LOAD64H(x, y)                                                      \
127    { x = (((ulong64)((y)[0] & 255))<<56)|(((ulong64)((y)[1] & 255))<<48) | \
128          (((ulong64)((y)[2] & 255))<<40)|(((ulong64)((y)[3] & 255))<<32) | \
129          (((ulong64)((y)[4] & 255))<<24)|(((ulong64)((y)[5] & 255))<<16) | \
130          (((ulong64)((y)[6] & 255))<<8)|(((ulong64)((y)[7] & 255))); }
131
132 #endif
133
134 #ifdef ENDIAN_32BITWORD 
135
136 #define STORE32L(x, y)        \
137      { ulong32  __t = (x); XMEMCPY(y, &__t, 4); }
138
139 #define LOAD32L(x, y)         \
140      XMEMCPY(&(x), y, 4);
141
142 #define STORE64L(x, y)                                                                     \
143      { (y)[7] = (unsigned char)(((x)>>56)&255); (y)[6] = (unsigned char)(((x)>>48)&255);   \
144        (y)[5] = (unsigned char)(((x)>>40)&255); (y)[4] = (unsigned char)(((x)>>32)&255);   \
145        (y)[3] = (unsigned char)(((x)>>24)&255); (y)[2] = (unsigned char)(((x)>>16)&255);   \
146        (y)[1] = (unsigned char)(((x)>>8)&255); (y)[0] = (unsigned char)((x)&255); }
147
148 #define LOAD64L(x, y)                                                       \
149      { x = (((ulong64)((y)[7] & 255))<<56)|(((ulong64)((y)[6] & 255))<<48)| \
150            (((ulong64)((y)[5] & 255))<<40)|(((ulong64)((y)[4] & 255))<<32)| \
151            (((ulong64)((y)[3] & 255))<<24)|(((ulong64)((y)[2] & 255))<<16)| \
152            (((ulong64)((y)[1] & 255))<<8)|(((ulong64)((y)[0] & 255))); }
153
154 #else /* 64-bit words then  */
155
156 #define STORE32L(x, y)        \
157      { ulong32 __t = (x); XMEMCPY(y, &__t, 4); }
158
159 #define LOAD32L(x, y)         \
160      { XMEMCPY(&(x), y, 4); x &= 0xFFFFFFFF; }
161
162 #define STORE64L(x, y)        \
163      { ulong64 __t = (x); XMEMCPY(y, &__t, 8); }
164
165 #define LOAD64L(x, y)         \
166     { XMEMCPY(&(x), y, 8); }
167
168 #endif /* ENDIAN_64BITWORD */
169
170 #endif /* ENDIAN_LITTLE */
171
172 #ifdef ENDIAN_BIG
173 #define STORE32L(x, y)                                                                     \
174      { (y)[3] = (unsigned char)(((x)>>24)&255); (y)[2] = (unsigned char)(((x)>>16)&255);   \
175        (y)[1] = (unsigned char)(((x)>>8)&255); (y)[0] = (unsigned char)((x)&255); }
176
177 #define LOAD32L(x, y)                            \
178      { x = ((unsigned long)((y)[3] & 255)<<24) | \
179            ((unsigned long)((y)[2] & 255)<<16) | \
180            ((unsigned long)((y)[1] & 255)<<8)  | \
181            ((unsigned long)((y)[0] & 255)); }
182
183 #define STORE64L(x, y)                                                                     \
184    { (y)[7] = (unsigned char)(((x)>>56)&255); (y)[6] = (unsigned char)(((x)>>48)&255);     \
185      (y)[5] = (unsigned char)(((x)>>40)&255); (y)[4] = (unsigned char)(((x)>>32)&255);     \
186      (y)[3] = (unsigned char)(((x)>>24)&255); (y)[2] = (unsigned char)(((x)>>16)&255);     \
187      (y)[1] = (unsigned char)(((x)>>8)&255); (y)[0] = (unsigned char)((x)&255); }
188
189 #define LOAD64L(x, y)                                                      \
190    { x = (((ulong64)((y)[7] & 255))<<56)|(((ulong64)((y)[6] & 255))<<48) | \
191          (((ulong64)((y)[5] & 255))<<40)|(((ulong64)((y)[4] & 255))<<32) | \
192          (((ulong64)((y)[3] & 255))<<24)|(((ulong64)((y)[2] & 255))<<16) | \
193          (((ulong64)((y)[1] & 255))<<8)|(((ulong64)((y)[0] & 255))); }
194
195 #ifdef ENDIAN_32BITWORD 
196
197 #define STORE32H(x, y)        \
198      { ulong32 __t = (x); XMEMCPY(y, &__t, 4); }
199
200 #define LOAD32H(x, y)         \
201      XMEMCPY(&(x), y, 4);
202
203 #define STORE64H(x, y)                                                                     \
204      { (y)[0] = (unsigned char)(((x)>>56)&255); (y)[1] = (unsigned char)(((x)>>48)&255);   \
205        (y)[2] = (unsigned char)(((x)>>40)&255); (y)[3] = (unsigned char)(((x)>>32)&255);   \
206        (y)[4] = (unsigned char)(((x)>>24)&255); (y)[5] = (unsigned char)(((x)>>16)&255);   \
207        (y)[6] = (unsigned char)(((x)>>8)&255);  (y)[7] = (unsigned char)((x)&255); }
208
209 #define LOAD64H(x, y)                                                       \
210      { x = (((ulong64)((y)[0] & 255))<<56)|(((ulong64)((y)[1] & 255))<<48)| \
211            (((ulong64)((y)[2] & 255))<<40)|(((ulong64)((y)[3] & 255))<<32)| \
212            (((ulong64)((y)[4] & 255))<<24)|(((ulong64)((y)[5] & 255))<<16)| \
213            (((ulong64)((y)[6] & 255))<<8)| (((ulong64)((y)[7] & 255))); }
214
215 #else /* 64-bit words then  */
216
217 #define STORE32H(x, y)        \
218      { ulong32 __t = (x); XMEMCPY(y, &__t, 4); }
219
220 #define LOAD32H(x, y)         \
221      { XMEMCPY(&(x), y, 4); x &= 0xFFFFFFFF; }
222
223 #define STORE64H(x, y)        \
224      { ulong64 __t = (x); XMEMCPY(y, &__t, 8); }
225
226 #define LOAD64H(x, y)         \
227     { XMEMCPY(&(x), y, 8); }
228
229 #endif /* ENDIAN_64BITWORD */
230 #endif /* ENDIAN_BIG */
231
232 #define BSWAP(x)  ( ((x>>24)&0x000000FFUL) | ((x<<24)&0xFF000000UL)  | \
233                     ((x>>8)&0x0000FF00UL)  | ((x<<8)&0x00FF0000UL) )
234
235
236 /* 32-bit Rotates */
237 #if defined(_MSC_VER)
238
239 /* instrinsic rotate */
240 #include <stdlib.h>
241 #pragma intrinsic(_lrotr,_lrotl)
242 #define ROR(x,n) _lrotr(x,n)
243 #define ROL(x,n) _lrotl(x,n)
244 #define RORc(x,n) _lrotr(x,n)
245 #define ROLc(x,n) _lrotl(x,n)
246
247 #elif !defined(__STRICT_ANSI__) && defined(__GNUC__) && (defined(__i386__) || defined(__x86_64__)) && !defined(INTEL_CC) && !defined(LTC_NO_ASM)
248
249 static inline unsigned ROL(unsigned word, int i)
250 {
251    asm ("roll %%cl,%0"
252       :"=r" (word)
253       :"0" (word),"c" (i));
254    return word;
255 }
256
257 static inline unsigned ROR(unsigned word, int i)
258 {
259    asm ("rorl %%cl,%0"
260       :"=r" (word)
261       :"0" (word),"c" (i));
262    return word;
263 }
264
265 #ifndef LTC_NO_ROLC
266
267 static inline unsigned ROLc(unsigned word, const int i)
268 {
269    asm ("roll %2,%0"
270       :"=r" (word)
271       :"0" (word),"I" (i));
272    return word;
273 }
274
275 static inline unsigned RORc(unsigned word, const int i)
276 {
277    asm ("rorl %2,%0"
278       :"=r" (word)
279       :"0" (word),"I" (i));
280    return word;
281 }
282
283 #else
284
285 #define ROLc ROL
286 #define RORc ROR
287
288 #endif
289
290 #elif !defined(__STRICT_ANSI__) && defined(LTC_PPC32)
291
292 static inline unsigned ROL(unsigned word, int i)
293 {
294    asm ("rotlw %0,%0,%2"
295       :"=r" (word)
296       :"0" (word),"r" (i));
297    return word;
298 }
299
300 static inline unsigned ROR(unsigned word, int i)
301 {
302    asm ("rotlw %0,%0,%2"
303       :"=r" (word)
304       :"0" (word),"r" (32-i));
305    return word;
306 }
307
308 #ifndef LTC_NO_ROLC
309
310 static inline unsigned ROLc(unsigned word, const int i)
311 {
312    asm ("rotlwi %0,%0,%2"
313       :"=r" (word)
314       :"0" (word),"I" (i));
315    return word;
316 }
317
318 static inline unsigned RORc(unsigned word, const int i)
319 {
320    asm ("rotrwi %0,%0,%2"
321       :"=r" (word)
322       :"0" (word),"I" (i));
323    return word;
324 }
325
326 #else
327
328 #define ROLc ROL
329 #define RORc ROR
330
331 #endif
332
333
334 #else
335
336 /* rotates the hard way */
337 #define ROL(x, y) ( (((unsigned long)(x)<<(unsigned long)((y)&31)) | (((unsigned long)(x)&0xFFFFFFFFUL)>>(unsigned long)(32-((y)&31)))) & 0xFFFFFFFFUL)
338 #define ROR(x, y) ( ((((unsigned long)(x)&0xFFFFFFFFUL)>>(unsigned long)((y)&31)) | ((unsigned long)(x)<<(unsigned long)(32-((y)&31)))) & 0xFFFFFFFFUL)
339 #define ROLc(x, y) ( (((unsigned long)(x)<<(unsigned long)((y)&31)) | (((unsigned long)(x)&0xFFFFFFFFUL)>>(unsigned long)(32-((y)&31)))) & 0xFFFFFFFFUL)
340 #define RORc(x, y) ( ((((unsigned long)(x)&0xFFFFFFFFUL)>>(unsigned long)((y)&31)) | ((unsigned long)(x)<<(unsigned long)(32-((y)&31)))) & 0xFFFFFFFFUL)
341
342 #endif
343
344
345 /* 64-bit Rotates */
346 #if !defined(__STRICT_ANSI__) && defined(__GNUC__) && defined(__x86_64__) && !defined(LTC_NO_ASM)
347
348 static inline unsigned long ROL64(unsigned long word, int i)
349 {
350    asm("rolq %%cl,%0"
351       :"=r" (word)
352       :"0" (word),"c" (i));
353    return word;
354 }
355
356 static inline unsigned long ROR64(unsigned long word, int i)
357 {
358    asm("rorq %%cl,%0"
359       :"=r" (word)
360       :"0" (word),"c" (i));
361    return word;
362 }
363
364 #ifndef LTC_NO_ROLC
365
366 static inline unsigned long ROL64c(unsigned long word, const int i)
367 {
368    asm("rolq %2,%0"
369       :"=r" (word)
370       :"0" (word),"J" (i));
371    return word;
372 }
373
374 static inline unsigned long ROR64c(unsigned long word, const int i)
375 {
376    asm("rorq %2,%0"
377       :"=r" (word)
378       :"0" (word),"J" (i));
379    return word;
380 }
381
382 #else /* LTC_NO_ROLC */
383
384 #define ROL64c ROL64
385 #define ROR64c ROR64
386
387 #endif
388
389 #else /* Not x86_64  */
390
391 #define ROL64(x, y) \
392     ( (((x)<<((ulong64)(y)&63)) | \
393       (((x)&CONST64(0xFFFFFFFFFFFFFFFF))>>((ulong64)64-((y)&63)))) & CONST64(0xFFFFFFFFFFFFFFFF))
394
395 #define ROR64(x, y) \
396     ( ((((x)&CONST64(0xFFFFFFFFFFFFFFFF))>>((ulong64)(y)&CONST64(63))) | \
397       ((x)<<((ulong64)(64-((y)&CONST64(63)))))) & CONST64(0xFFFFFFFFFFFFFFFF))
398
399 #define ROL64c(x, y) \
400     ( (((x)<<((ulong64)(y)&63)) | \
401       (((x)&CONST64(0xFFFFFFFFFFFFFFFF))>>((ulong64)64-((y)&63)))) & CONST64(0xFFFFFFFFFFFFFFFF))
402
403 #define ROR64c(x, y) \
404     ( ((((x)&CONST64(0xFFFFFFFFFFFFFFFF))>>((ulong64)(y)&CONST64(63))) | \
405       ((x)<<((ulong64)(64-((y)&CONST64(63)))))) & CONST64(0xFFFFFFFFFFFFFFFF))
406
407 #endif
408
409 #ifndef MAX
410    #define MAX(x, y) ( ((x)>(y))?(x):(y) )
411 #endif
412
413 #ifndef MIN
414    #define MIN(x, y) ( ((x)<(y))?(x):(y) )
415 #endif
416
417 /* extract a byte portably */
418 #ifdef _MSC_VER
419    #define byte(x, n) ((unsigned char)((x) >> (8 * (n))))
420 #else
421    #define byte(x, n) (((x) >> (8 * (n))) & 255)
422 #endif   
423
424 /* $Source: /cvs/libtom/libtomcrypt/src/headers/tomcrypt_macros.h,v $ */
425 /* $Revision: 1.15 $ */
426 /* $Date: 2006/11/29 23:43:57 $ */