-
Notifications
You must be signed in to change notification settings - Fork 2.2k
/
Copy pathopencl_misc.h
638 lines (542 loc) · 17.1 KB
/
opencl_misc.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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
/*
* OpenCL common macros
*
* Copyright (c) 2014-2020, magnum
* This software is hereby released to the general public under
* the following terms: Redistribution and use in source and binary
* forms, with or without modification, are permitted.
*
* NOTICE: After changes in headers, with nvidia driver you probably
* need to drop cached kernels to ensure the changes take effect:
*
* rm -fr ~/.nv/ComputeCache
*
*/
#ifndef _OPENCL_MISC_H
#define _OPENCL_MISC_H
#include "opencl_device_info.h"
/* Note: long is *always* 64-bit in OpenCL */
typedef uchar uint8_t;
typedef char int8_t;
typedef ushort uint16_t;
typedef short int16_t;
typedef uint uint32_t;
typedef int int32_t;
typedef ulong uint64_t;
typedef long int64_t;
#if __SIZEOF_HOST_SIZE_T__ == 8 /* This is set by opencl_common.c */
typedef uint64_t host_size_t;
#else
typedef uint32_t host_size_t;
#endif
#ifndef NULL
#define NULL ((void*)0)
#endif
/* Compatibility with non-clang compilers. */
#ifndef __has_builtin
#define __has_builtin(x) 0
#endif
#if __llvm__ || __has_builtin(__builtin_expect)
#define likely(x) __builtin_expect(!!(x), 1)
#define unlikely(x) __builtin_expect(!!(x), 0)
#else
#define likely(x) (x)
#define unlikely(x) (x)
#endif
/*
* Some runtimes/drivers breaks on using inline, others breaks on lack of it,
* yet others require use of static as well.
*
* Only usable in device code
*/
#if _OPENCL_COMPILER
#if __MESA__
#define inline // empty!
#elif __POCL__
// Do nothing (POCL complains if we redefine)
#elif gpu_amd(DEVICE_INFO) // We really target ROCM here
#define inline static inline
#else
// Do nothing
#endif
#endif /* _OPENCL_COMPILER */
/*
* "Copy" of the one in dyna_salt.h (we only need it to be right size,
* bitfields are not allowed in OpenCL)
*/
typedef struct dyna_salt_t {
host_size_t salt_cmp_size;
host_size_t bitfield_and_offset;
} dyna_salt;
#ifndef MIN
#define MIN(a,b) ((a)<(b)?(a):(b))
#endif
#ifndef MAX
#define MAX(a,b) ((a)>(b)?(a):(b))
#endif
/*
* Host code may pass -DV_WIDTH=2 or some other width.
*/
#if V_WIDTH > 1
#define MAYBE_VECTOR_UINT VECTOR(uint, V_WIDTH)
#define MAYBE_VECTOR_ULONG VECTOR(ulong, V_WIDTH)
#else
#define MAYBE_VECTOR_UINT uint
#define MAYBE_VECTOR_ULONG ulong
#define SCALAR 1
#endif
#if SCALAR && 0 /* Used for testing */
#define HAVE_LUT3 1
inline uint lut3(uint x, uint y, uint z, uchar m)
{
uint i;
uint r = 0;
for (i = 0; i < sizeof(uint) * 8; i++)
r |= (uint)((m >> ( (((x >> i) & 1) << 2) |
(((y >> i) & 1) << 1) |
((z >> i) & 1) )) & 1) << i;
return r;
}
#endif
/*
* Apparently nvidias can optimize stuff better (ending up in *better* LUT
* use) with the basic formulas instead of bitselect ones. Most formats
* show no difference but pwsafe does.
*/
#if !gpu_nvidia(DEVICE_INFO)
#define USE_BITSELECT 1
#endif
#if SM_MAJOR == 1
#define OLD_NVIDIA 1
#endif
#if cpu(DEVICE_INFO)
#define HAVE_ANDNOT 1
#endif
#if SCALAR && SM_MAJOR >= 5 && (DEV_VER_MAJOR > 352 || (DEV_VER_MAJOR == 352 && DEV_VER_MINOR >= 21))
#define HAVE_LUT3 1
inline uint lut3(uint a, uint b, uint c, uint imm)
{
uint r;
asm("lop3.b32 %0, %1, %2, %3, %4;"
: "=r" (r)
: "r" (a), "r" (b), "r" (c), "i" (imm));
return r;
}
#if 0 /* This does no good */
#define HAVE_LUT3_64 1
inline ulong lut3_64(ulong a, ulong b, ulong c, uint imm)
{
ulong t, r;
asm("lop3.b32 %0, %1, %2, %3, %4;"
: "=r" (t)
: "r" ((uint)a), "r" ((uint)b), "r" ((uint)c), "i" (imm));
r = t;
asm("lop3.b32 %0, %1, %2, %3, %4;"
: "=r" (t)
: "r" ((uint)(a >> 32)), "r" ((uint)(b >> 32)), "r" ((uint)(c >> 32)), "i" (imm));
return r + (t << 32);
}
#endif
#endif
#if defined cl_amd_media_ops && !__MESA__ && gpu_amd(DEVICE_INFO)
#pragma OPENCL EXTENSION cl_amd_media_ops : enable
#define BITALIGN(hi, lo, s) amd_bitalign((hi), (lo), (s))
#elif SCALAR && SM_MAJOR > 3 || (SM_MAJOR == 3 && SM_MINOR >= 2)
inline uint funnel_shift_right(uint hi, uint lo, uint s)
{
uint r;
asm("shf.r.wrap.b32 %0, %1, %2, %3;"
: "=r" (r)
: "r" (lo), "r" (hi), "r" (s));
return r;
}
inline uint funnel_shift_right_imm(uint hi, uint lo, uint s)
{
uint r;
asm("shf.r.wrap.b32 %0, %1, %2, %3;"
: "=r" (r)
: "r" (lo), "r" (hi), "i" (s));
return r;
}
#define BITALIGN(hi, lo, s) funnel_shift_right(hi, lo, s)
#define BITALIGN_IMM(hi, lo, s) funnel_shift_right_imm(hi, lo, s)
#else
#define BITALIGN(hi, lo, s) (((hi) << (32 - (s))) | ((lo) >> (s)))
#endif
#ifndef BITALIGN_IMM
#define BITALIGN_IMM(hi, lo, s) BITALIGN(hi, lo, s)
#endif
#define CONCAT(TYPE,WIDTH) TYPE ## WIDTH
#define VECTOR(x, y) CONCAT(x, y)
/* Workaround for problem seen with 9600GT */
#ifndef MAYBE_CONSTANT
#if OLD_NVIDIA
#define MAYBE_CONSTANT __global const
#else
#define MAYBE_CONSTANT __constant
#endif
#endif
#define block_swap32(W, len) for (uint i = 0; i < len; i++) W[i] = SWAP32(W[i])
#define block_swap64(W, len) for (uint i = 0; i < len; i++) W[i] = SWAP64(W[i])
inline ushort SWAP16(ushort x)
{
return ((x << 8) + (x >> 8));
}
#if USE_BITSELECT
inline uint SWAP32(uint x)
{
return bitselect(rotate(x, 24U), rotate(x, 8U), 0x00FF00FFU);
}
#define SWAP64(n) bitselect( \
bitselect(rotate(n, 24UL), \
rotate(n, 8UL), 0x000000FF000000FFUL), \
bitselect(rotate(n, 56UL), \
rotate(n, 40UL), 0x00FF000000FF0000UL), \
0xFFFF0000FFFF0000UL)
#else
inline uint SWAP32(uint x)
{
x = rotate(x, 16U);
return ((x & 0x00FF00FF) << 8) + ((x >> 8) & 0x00FF00FF);
}
// You would not believe how many driver bugs variants of this macro reveal
#define SWAP64(n) \
(((n) << 56) | (((n) & 0xff00) << 40) | \
(((n) & 0xff0000) << 24) | (((n) & 0xff000000) << 8) | \
(((n) >> 8) & 0xff000000) | (((n) >> 24) & 0xff0000) | \
(((n) >> 40) & 0xff00) | ((n) >> 56))
#endif
#if SCALAR
#define VSWAP32 SWAP32
#else
/* Vector-capable swap32() */
inline MAYBE_VECTOR_UINT VSWAP32(MAYBE_VECTOR_UINT x)
{
x = rotate(x, 16U);
return ((x & 0x00FF00FF) << 8) + ((x >> 8) & 0x00FF00FF);
}
#endif
/*
* These macros must not require alignment of (b).
*/
#define GET_UINT32(n, b, i) \
{ \
(n) = ((uint) (b)[(i)] ) \
| ((uint) (b)[(i) + 1] << 8) \
| ((uint) (b)[(i) + 2] << 16) \
| ((uint) (b)[(i) + 3] << 24); \
}
#define PUT_UINT32(n, b, i) \
{ \
(b)[(i) ] = (uchar) ((n) ); \
(b)[(i) + 1] = (uchar) ((n) >> 8); \
(b)[(i) + 2] = (uchar) ((n) >> 16); \
(b)[(i) + 3] = (uchar) ((n) >> 24); \
}
#define GET_UINT32BE(n, b, i) \
{ \
(n) = ((uint) (b)[(i)] << 24) \
| ((uint) (b)[(i) + 1] << 16) \
| ((uint) (b)[(i) + 2] << 8) \
| ((uint) (b)[(i) + 3] ); \
}
#define PUT_UINT32BE(n, b, i) \
{ \
(b)[(i) ] = (uchar) ((n) >> 24); \
(b)[(i) + 1] = (uchar) ((n) >> 16); \
(b)[(i) + 2] = (uchar) ((n) >> 8); \
(b)[(i) + 3] = (uchar) ((n) ); \
}
#define PUT_UINT64(n, b, i) \
{ \
(b)[(i) ] = (uchar) ((n) ); \
(b)[(i) + 1] = (uchar) ((ulong)(n) >> 8); \
(b)[(i) + 2] = (uchar) ((ulong)(n) >> 16); \
(b)[(i) + 3] = (uchar) ((ulong)(n) >> 24); \
(b)[(i) + 4] = (uchar) ((ulong)(n) >> 32); \
(b)[(i) + 5] = (uchar) ((ulong)(n) >> 40); \
(b)[(i) + 6] = (uchar) ((ulong)(n) >> 48); \
(b)[(i) + 7] = (uchar) ((ulong)(n) >> 56); \
}
#define GET_UINT64BE(n, b, i) \
{ \
(n) = ((ulong) (b)[(i)] << 56) \
| ((ulong) (b)[(i) + 1] << 48) \
| ((ulong) (b)[(i) + 2] << 40) \
| ((ulong) (b)[(i) + 3] << 32) \
| ((ulong) (b)[(i) + 4] << 24) \
| ((ulong) (b)[(i) + 5] << 16) \
| ((ulong) (b)[(i) + 6] << 8) \
| ((ulong) (b)[(i) + 7] ); \
}
#define PUT_UINT64BE(n, b, i) \
{ \
(b)[(i) ] = (uchar) ((ulong)(n) >> 56); \
(b)[(i) + 1] = (uchar) ((ulong)(n) >> 48); \
(b)[(i) + 2] = (uchar) ((ulong)(n) >> 40); \
(b)[(i) + 3] = (uchar) ((ulong)(n) >> 32); \
(b)[(i) + 4] = (uchar) ((ulong)(n) >> 24); \
(b)[(i) + 5] = (uchar) ((ulong)(n) >> 16); \
(b)[(i) + 6] = (uchar) ((ulong)(n) >> 8); \
(b)[(i) + 7] = (uchar) ((n) ); \
}
/*
* Allow some strict aliasing violations, for nvidia only.
* These require (b) to be aligned!
*/
#if gpu_nvidia(DEVICE_INFO)
#define ALLOW_ALIASING_VIOLATIONS 1
#if __ENDIAN_LITTLE__
#define GET_UINT32_ALIGNED(n, b, i) (n) = ((uint*)(b))[(i) >> 2]
#define PUT_UINT32_ALIGNED(n, b, i) ((uint*)(b))[(i) >> 2] = (n)
#define GET_UINT32BE_ALIGNED(n, b, i) (n) = SWAP32(((uint*)(b))[(i) >> 2])
#define PUT_UINT32BE_ALIGNED(n, b, i) ((uint*)(b))[(i) >> 2] = SWAP32(n)
#define PUT_UINT64_ALIGNED(n, b, i) ((ulong*)(b))[(i) >> 3] = (n)
#define GET_UINT64BE_ALIGNED(n, b, i) (n) = SWAP64(((ulong*)(b))[(i) >> 3])
#define PUT_UINT64BE_ALIGNED(n, b, i) ((ulong*)(b))[(i) >> 3] = SWAP64(n)
#else
#define GET_UINT32_ALIGNED(n, b, i) (n) = SWAP32(((uint*)(b))[(i) >> 2])
#define PUT_UINT32_ALIGNED(n, b, i) ((uint*)(b))[(i) >> 2] = SWAP32(n)
#define GET_UINT32BE_ALIGNED(n, b, i) (n) = ((uint*)(b))[(i) >> 2]
#define PUT_UINT32BE_ALIGNED(n, b, i) ((uint*)(b))[(i) >> 2] = (n)
#define PUT_UINT64_ALIGNED(n, b, i) ((ulong*)(b))[(i) >> 3] = SWAP64(n)
#define GET_UINT64BE_ALIGNED(n, b, i) (n) = ((ulong*)(b))[(i) >> 3]
#define PUT_UINT64BE_ALIGNED(n, b, i) ((ulong*)(b))[(i) >> 3] = (n)
#endif
#endif
/* Any device can do 8-bit reads BUT these macros are scalar only! */
#define GETCHAR(buf, index) (((uchar*)(buf))[(index)])
#define GETCHAR_G(buf, index) (((__global uchar*)(buf))[(index)])
#define GETCHAR_L(buf, index) (((__local uchar*)(buf))[(index)])
#define GETCHAR_BE(buf, index) (((uchar*)(buf))[(index) ^ 3])
#define GETCHAR_MC(buf, index) (((MAYBE_CONSTANT uchar*)(buf))[(index)])
#define LASTCHAR_BE(buf, index, val) (buf)[(index)>>2] = ((buf)[(index)>>2] & (0xffffff00U << ((((index) & 3) ^ 3) << 3))) + ((val) << ((((index) & 3) ^ 3) << 3))
#define LASTCHAR_BE64(buf, index, val) (buf)[(index)>>3] = ((buf)[(index)>>3] & (0xffffffffffffff00UL << ((((index) & 7) ^ 7) << 3))) + ((ulong)(val) << ((((index) & 7) ^ 7) << 3))
#define LASTCHAR(buf, index, val) (buf)[(index)>>2] = ((buf)[(index)>>2] & (0xffffff00U << (((index) & 3) << 3))) + ((val) << (((index) & 3) << 3))
#if no_byte_addressable(DEVICE_INFO) || !SCALAR || (gpu_amd(DEVICE_INFO) && defined(AMD_PUTCHAR_NOCAST))
/* 32-bit stores */
#define PUTCHAR(buf, index, val) (buf)[(index)>>2] = ((buf)[(index)>>2] & ~(0xffU << (((index) & 3) << 3))) + ((val) << (((index) & 3) << 3))
#define PUTCHAR_G PUTCHAR
#define PUTCHAR_L PUTCHAR
#define PUTCHAR_BE(buf, index, val) (buf)[(index)>>2] = ((buf)[(index)>>2] & ~(0xffU << ((((index) & 3) ^ 3) << 3))) + ((val) << ((((index) & 3) ^ 3) << 3))
#define PUTCHAR_BE_G PUTCHAR_BE
#define PUTSHORT(buf, index, val) (buf)[(index)>>1] = ((buf)[(index)>>1] & ~(0xffffU << (((index) & 1) << 4))) + ((val) << (((index) & 1) << 4))
#define PUTSHORT_BE(buf, index, val) (buf)[(index)>>1] = ((buf)[(index)>>1] & ~(0xffffU << ((((index) & 1) ^ 3) << 4))) + ((val) << ((((index) & 1) ^ 3) << 4))
#define XORCHAR(buf, index, val) (buf)[(index)>>2] = ((buf)[(index)>>2]) ^ ((val) << (((index) & 3) << 3))
#define XORCHAR_BE(buf, index, val) (buf)[(index)>>2] = ((buf)[(index)>>2]) ^ ((val) << ((((index) & 3) ^ 3) << 3))
#else
/* 8-bit stores */
#define PUTCHAR(buf, index, val) ((uchar*)(buf))[index] = (val)
#define PUTCHAR_G(buf, index, val) ((__global uchar*)(buf))[(index)] = (val)
#define PUTCHAR_L(buf, index, val) ((__local uchar*)(buf))[(index)] = (val)
#define PUTCHAR_BE(buf, index, val) ((uchar*)(buf))[(index) ^ 3] = (val)
#define PUTCHAR_BE_G(buf, index, val) ((__global uchar*)(buf))[(index) ^ 3] = (val)
#define PUTSHORT(buf, index, val) ((ushort*)(buf))[index] = (val)
#define PUTSHORT_BE(buf, index, val) ((ushort*)(buf))[(index) ^ 1] = (val)
#define XORCHAR(buf, index, val) ((uchar*)(buf))[(index)] ^= (val)
#define XORCHAR_BE(buf, index, val) ((uchar*)(buf))[(index) ^ 3] ^= (val)
#endif
inline int check_pkcs_pad(const uchar *data, int len, int blocksize)
{
int pad_len, padding, real_len;
if (len & (blocksize - 1) || len < blocksize)
return -1;
pad_len = data[len - 1];
if (pad_len < 1 || pad_len > blocksize)
return -1;
real_len = len - pad_len;
data += real_len;
padding = pad_len;
while (pad_len--)
if (*data++ != padding)
return -1;
return real_len;
}
/*
* Use with some caution. Memory type agnostic and if both src and dst are
* 8-bit types, this works like a normal memcpy.
*
* If src and dst are larger but same size, it will still work fine but
* 'count' is number of ELEMENTS and not BYTES.
*
* If src and dst are different size types, you will get what you asked for...
*/
#define memcpy_macro(dst, src, count) do { \
uint _memcpy_c = count; \
for (uint _memcpy_i = 0; _memcpy_i < _memcpy_c; _memcpy_i++) \
(dst)[_memcpy_i] = (src)[_memcpy_i]; \
} while (0)
/*
* memcpy functions. Until we require OpenCL 2.0, you need to pick the one
* that corresponds to the source- and destination memory type(s).
*/
/* src and dst are private mem */
inline void memcpy_pp(void* restrict dst, const void* restrict src, uint count)
{
const char *s = src;
char *d = dst;
while (count--)
*d++ = *s++;
}
/* src is private mem, dst is global mem */
inline void memcpy_pg(__global void* restrict dst, const void* restrict src, uint count)
{
const char *s = src;
__global char *d = dst;
while (count--)
*d++ = *s++;
}
/* src is global mem, dst is private mem */
inline void memcpy_gp(void* restrict dst, __global const void* restrict src, uint count)
{
__global const char *s = src;
char *d = dst;
while (count--)
*d++ = *s++;
}
/* src is constant mem, dst is private mem */
inline void memcpy_cp(void* restrict dst, __constant void* restrict src, uint count)
{
__constant char *s = src;
char *d = dst;
while (count--)
*d++ = *s++;
}
/* src is MAYBE_CONSTANT mem, dst is private mem */
inline void memcpy_mcp(void* restrict dst, MAYBE_CONSTANT void* restrict src, uint count)
{
MAYBE_CONSTANT char *s = src;
char *d = dst;
while (count--)
*d++ = *s++;
}
/* dst is private mem */
inline void memset_p(void *p, uint val, uint count)
{
char *d = p;
while (count--)
*d++ = val;
}
/* dst is global mem */
inline void memset_g(__global void *p, uint val, uint count)
{
__global char *d = p;
while (count--)
*d++ = val;
}
/* s1 and s2 are private mem */
inline int memcmp_pp(const void *s1, const void *s2, uint size)
{
const uchar *a = s1;
const uchar *b = s2;
while (size--)
if (*a++ != *b++)
return 1;
return 0;
}
/* s1 is private mem, s2 is global mem */
inline int memcmp_pg(const void *s1, __global const void *s2, uint size)
{
const uchar *a = s1;
__global const uchar *b = s2;
while (size--)
if (*a++ != *b++)
return 1;
return 0;
}
/* s1 is private mem, s2 is constant mem */
inline int memcmp_pc(const void *s1, __constant const void *s2, uint size)
{
const uchar *a = s1;
__constant const uchar *b = s2;
while (size--)
if (*a++ != *b++)
return 1;
return 0;
}
/* s1 is global mem, s2 is constant mem */
inline int memcmp_gc(__global const void *s1, __constant void *s2, uint size)
{
__global const uchar *a = s1;
__constant uchar *b = s2;
while (size--)
if (*a++ != *b++)
return 1;
return 0;
}
/* s1 is private mem, s2 is MAYBE_CONSTANT mem */
inline int memcmp_pmc(const void *s1, MAYBE_CONSTANT void *s2, uint size)
{
const uchar *a = s1;
MAYBE_CONSTANT uchar *b = s2;
while (size--)
if (*a++ != *b++)
return 1;
return 0;
}
/* haystack is private mem, needle is constant mem */
inline int memmem_pc(const void *haystack, size_t haystack_len,
__constant const void *needle, size_t needle_len)
{
const char *haystack_ = haystack;
__constant const char *needle_ = needle;
int hash = 0;
int hay_hash = 0;
const char *last;
size_t i;
if (haystack_len < needle_len)
return 0;
if (!needle_len)
return 1;
for (i = needle_len; i; --i) {
hash += *needle_++;
hay_hash += *haystack_++;
}
haystack_ = (char*)haystack;
needle_ = (__constant char*)needle;
for (last = haystack_ + (haystack_len - needle_len + 1); haystack_ < last; ++haystack_) {
if (hash == hay_hash && *haystack_ == *needle_ && !memcmp_pc(haystack_, needle_, needle_len))
return 1;
hay_hash -= *haystack_;
hay_hash += *(haystack_+needle_len);
}
return 0;
}
#define STRINGIZE2(s) #s
#define STRINGIZE(s) STRINGIZE2(s)
/*
* The below macros need to be called with pointer already cast to
* uchar pointer of its type - such as "(__constant uchar*)salt->u"
* because type is unknown to us so void* can't help us.
*/
#define dump_le(x, size) dump_stuff_msg(STRINGIZE(x), x, size)
#define dump_be(x, size) dump_stuff_be_msg(STRINGIZE(x), x, size)
#define dump_be64(x, size) dump_stuff_be64_msg(STRINGIZE(x), x, size)
#define dump_stuff_msg(msg, x, size) do { \
printf("%s : ", msg); \
for (uint xedni_ = 0; xedni_ < (uint)size; xedni_++) { \
printf("%02x", (x)[xedni_]); \
if (xedni_ % 4 == 3) \
printf(" "); \
} \
printf("\n"); \
} while (0)
#define dump_stuff_be_msg(msg, x, size) do { \
printf("%s : ", msg); \
for (uint xedni_ = 0; xedni_ < (uint)size; xedni_++) { \
printf("%02x", (x)[xedni_ ^ 3]); \
if (xedni_ % 4 == 3) \
printf(" "); \
} \
printf("\n"); \
} while (0)
#define dump_stuff_be64_msg(msg, x, size) do { \
printf("%s : ", msg); \
for (uint xedni_ = 0; xedni_ < (uint)size; xedni_++) { \
printf("%02x", (x)[xedni_ ^ 7]); \
if (xedni_ % 4 == 3) \
printf(" "); \
} \
printf("\n"); \
} while (0)
#endif