This repository has been archived by the owner on Mar 26, 2023. It is now read-only.
forked from Cyan4973/xxHash
-
Notifications
You must be signed in to change notification settings - Fork 0
/
xxhsum.c
1829 lines (1578 loc) · 67.6 KB
/
xxhsum.c
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
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* xxhsum - Command line interface for xxhash algorithms
* Copyright (C) Yann Collet 2013-present
*
* GPL v2 License
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*
* You can contact the author at :
* - xxHash homepage : http://www.xxhash.com
* - xxHash source repository : https://github.com/Cyan4973/xxHash
*/
/* xxhsum :
* Provides hash value of a file content, or a list of files, or stdin
* Display convention is Big Endian, for both 32 and 64 bits algorithms
*/
/* ************************************
* Compiler Options
**************************************/
/* MS Visual */
#if defined(_MSC_VER) || defined(_WIN32)
# define _CRT_SECURE_NO_WARNINGS /* removes visual warnings */
#endif
/* Under Linux at least, pull in the *64 commands */
#ifndef _LARGEFILE64_SOURCE
# define _LARGEFILE64_SOURCE
#endif
/* ************************************
* Includes
**************************************/
#include <stdlib.h> /* malloc, calloc, free, exit */
#include <stdio.h> /* fprintf, fopen, ftello64, fread, stdin, stdout, _fileno (when present) */
#include <string.h> /* strcmp */
#include <sys/types.h> /* stat, stat64, _stat64 */
#include <sys/stat.h> /* stat, stat64, _stat64 */
#include <time.h> /* clock_t, clock, CLOCKS_PER_SEC */
#include <assert.h> /* assert */
#include <errno.h> /* errno */
#include "xxhash.h"
#define XXH_STATIC_LINKING_ONLY /* *_state_t */
#include "xxhash.h" /* note : intentional double include, for validation.
* this test ensures that xxhash.h can be included in any order. */
/* ************************************
* OS-Specific Includes
**************************************/
#if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__)) /* UNIX-like OS */ \
|| defined(__midipix__) || defined(__VMS))
# if (defined(__APPLE__) && defined(__MACH__)) || defined(__SVR4) || defined(_AIX) || defined(__hpux) /* POSIX.1-2001 (SUSv3) conformant */ \
|| defined(__DragonFly__) || defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__) /* BSD distros */
# define PLATFORM_POSIX_VERSION 200112L
# else
# if defined(__linux__) || defined(__linux)
# ifndef _POSIX_C_SOURCE
# define _POSIX_C_SOURCE 200112L /* use feature test macro */
# endif
# endif
# include <unistd.h> /* declares _POSIX_VERSION */
# if defined(_POSIX_VERSION) /* POSIX compliant */
# define PLATFORM_POSIX_VERSION _POSIX_VERSION
# else
# define PLATFORM_POSIX_VERSION 0
# endif
# endif
#endif
#if !defined(PLATFORM_POSIX_VERSION)
# define PLATFORM_POSIX_VERSION -1
#endif
#if (defined(__linux__) && (PLATFORM_POSIX_VERSION >= 1)) \
|| (PLATFORM_POSIX_VERSION >= 200112L) \
|| defined(__DJGPP__) \
|| defined(__MSYS__)
# include <unistd.h> /* isatty */
# define IS_CONSOLE(stdStream) isatty(fileno(stdStream))
#elif defined(MSDOS) || defined(OS2) || defined(__CYGWIN__)
# include <io.h> /* _isatty */
# define IS_CONSOLE(stdStream) _isatty(_fileno(stdStream))
#elif defined(WIN32) || defined(_WIN32)
# include <io.h> /* _isatty */
# include <windows.h> /* DeviceIoControl, HANDLE, FSCTL_SET_SPARSE */
# include <stdio.h> /* FILE */
static __inline int IS_CONSOLE(FILE* stdStream) {
DWORD dummy;
return _isatty(_fileno(stdStream)) && GetConsoleMode((HANDLE)_get_osfhandle(_fileno(stdStream)), &dummy);
}
#else
# define IS_CONSOLE(stdStream) 0
#endif
#if defined(MSDOS) || defined(OS2) || defined(WIN32) || defined(_WIN32)
# include <fcntl.h> /* _O_BINARY */
# include <io.h> /* _setmode, _fileno, _get_osfhandle */
# if !defined(__DJGPP__)
# include <windows.h> /* DeviceIoControl, HANDLE, FSCTL_SET_SPARSE */
# include <winioctl.h> /* FSCTL_SET_SPARSE */
# define SET_BINARY_MODE(file) { int const unused=_setmode(_fileno(file), _O_BINARY); (void)unused; }
# define SET_SPARSE_FILE_MODE(file) { DWORD dw; DeviceIoControl((HANDLE) _get_osfhandle(_fileno(file)), FSCTL_SET_SPARSE, 0, 0, 0, 0, &dw, 0); }
# else
# define SET_BINARY_MODE(file) setmode(fileno(file), O_BINARY)
# define SET_SPARSE_FILE_MODE(file)
# endif
#else
# define SET_BINARY_MODE(file)
# define SET_SPARSE_FILE_MODE(file)
#endif
#if !defined(S_ISREG)
# define S_ISREG(x) (((x) & S_IFMT) == S_IFREG)
#endif
/* ************************************
* Basic Types
**************************************/
#ifndef MEM_MODULE
# define MEM_MODULE
# if defined (__STDC_VERSION__) && __STDC_VERSION__ >= 199901L /* C99 */
# include <stdint.h>
typedef uint8_t U8;
typedef uint32_t U32;
typedef uint64_t U64;
# else
# include <limits.h>
typedef unsigned char U8;
# if UINT_MAX == 0xFFFFFFFFUL
typedef unsigned int U32;
# else
typedef unsigned long U32;
# endif
typedef unsigned long long U64;
# endif
#endif
static unsigned BMK_isLittleEndian(void)
{
const union { U32 u; U8 c[4]; } one = { 1 }; /* don't use static : performance detrimental */
return one.c[0];
}
/* *************************************
* Constants
***************************************/
#define LIB_VERSION XXH_VERSION_MAJOR.XXH_VERSION_MINOR.XXH_VERSION_RELEASE
#define QUOTE(str) #str
#define EXPAND_AND_QUOTE(str) QUOTE(str)
#define PROGRAM_VERSION EXPAND_AND_QUOTE(LIB_VERSION)
/* Show compiler versions in WELCOME_MESSAGE. VERSION_FMT will return the printf specifiers,
* and VERSION will contain the comma separated list of arguments to the VERSION_FMT string. */
#if defined(__clang_version__)
/* Clang does its own thing. */
# ifdef __apple_build_version__
# define VERSION_FMT ", Apple Clang %s"
# else
# define VERSION_FMT ", Clang %s"
# endif
# define VERSION __clang_version__
#elif defined(__VERSION__)
/* GCC and ICC */
# define VERSION_FMT ", %s"
# ifdef __INTEL_COMPILER /* icc adds its prefix */
# define VERSION_STRING __VERSION__
# else /* assume GCC */
# define VERSION "GCC " __VERSION__
# endif
#elif defined(_MSC_FULL_VER) && defined(_MSC_BUILD)
/* "For example, if the version number of the Visual C++ compiler is 15.00.20706.01, the _MSC_FULL_VER macro
* evaluates to 150020706." https://docs.microsoft.com/en-us/cpp/preprocessor/predefined-macros?view=vs-2017 */
# define VERSION _MSC_FULL_VER / 10000000 % 100, _MSC_FULL_VER / 100000 % 100, _MSC_FULL_VER % 100000, _MSC_BUILD
# define VERSION_FMT ", MSVC %02i.%02i.%05i.%02i"
#elif defined(__TINYC__)
/* tcc stores its version in the __TINYC__ macro. */
# define VERSION_FMT ", tcc %i.%i.%i"
# define VERSION __TINYC__ / 10000 % 100, __TINYC__ / 100 % 100, __TINYC__ % 100
#else
# define VERSION_FMT "%s"
# define VERSION ""
#endif
/* makes the next part easier */
#if defined(__x86_64__) || defined(_M_AMD64) || defined(_M_X64)
# define ARCH_X86 "x86_64"
#elif defined(__i386__) || defined(_M_X86) || defined(_M_X86_FP)
# define ARCH_X86 "i386"
#endif
/* Try to detect the architecture. */
#if defined(ARCH_X86)
# if defined(__AVX2__)
# define ARCH ARCH_X86 " + AVX2"
# elif defined(__AVX__)
# define ARCH ARCH_X86 " + AVX"
# elif defined(__SSE2__)
# define ARCH ARCH_X86 " + SSE2"
# else
# define ARCH ARCH_X86
# endif
#elif defined(__aarch64__) || defined(__arm64__) || defined(_M_ARM64)
# define ARCH "aarch64"
#elif defined(__arm__) || defined(__thumb__) || defined(__thumb2__) || defined(_M_ARM)
# if defined(__ARM_NEON) || defined(__ARM_NEON__)
# define ARCH "arm + NEON"
# else
# define ARCH "arm"
# endif
#elif defined(__powerpc64__) || defined(__ppc64__) || defined(__PPC64__)
# if defined(__GNUC__) && defined(__POWER9_VECTOR__)
# define ARCH "ppc64 + POWER9 vector"
# elif defined(__GNUC__) && defined(__POWER8_VECTOR__)
# define ARCH "ppc64 + POWER8 vector"
# else
# define ARCH "ppc64"
# endif
#elif defined(__powerpc__) || defined(__ppc__) || defined(__PPC__)
# define ARCH "ppc"
#elif defined(__AVR)
# define ARCH "AVR"
#elif defined(__mips64)
# define ARCH "mips64"
#elif defined(__mips)
# define ARCH "mips"
#else
# define ARCH "unknown"
#endif
static const int g_nbBits = (int)(sizeof(void*)*8);
static const char g_lename[] = "little endian";
static const char g_bename[] = "big endian";
#define ENDIAN_NAME (BMK_isLittleEndian() ? g_lename : g_bename)
static const char author[] = "Yann Collet";
#define WELCOME_MESSAGE(exename) "%s %s (%i-bits %s %s)" VERSION_FMT ", by %s \n", \
exename, PROGRAM_VERSION, g_nbBits, ARCH, ENDIAN_NAME, VERSION, author
#define KB *( 1<<10)
#define MB *( 1<<20)
#define GB *(1U<<30)
static size_t XXH_DEFAULT_SAMPLE_SIZE = 100 KB;
#define NBLOOPS 3 /* Default number of benchmark iterations */
#define TIMELOOP_S 1
#define TIMELOOP (TIMELOOP_S * CLOCKS_PER_SEC) /* target timing per iteration */
#define TIMELOOP_MIN (TIMELOOP / 2) /* minimum timing to validate a result */
#define XXHSUM32_DEFAULT_SEED 0 /* Default seed for algo_xxh32 */
#define XXHSUM64_DEFAULT_SEED 0 /* Default seed for algo_xxh64 */
#define MAX_MEM (2 GB - 64 MB)
static const char stdinName[] = "-";
typedef enum { algo_xxh32, algo_xxh64, algo_xxh128 } algoType;
static const algoType g_defaultAlgo = algo_xxh64; /* required within main() & usage() */
/* <16 hex char> <SPC> <SPC> <filename> <'\0'>
* '4096' is typical Linux PATH_MAX configuration. */
#define DEFAULT_LINE_LENGTH (sizeof(XXH64_hash_t) * 2 + 2 + 4096 + 1)
/* Maximum acceptable line length. */
#define MAX_LINE_LENGTH (32 KB)
/* ************************************
* Display macros
**************************************/
#define DISPLAY(...) fprintf(stderr, __VA_ARGS__)
#define DISPLAYRESULT(...) fprintf(stdout, __VA_ARGS__)
#define DISPLAYLEVEL(l, ...) do { if (g_displayLevel>=l) DISPLAY(__VA_ARGS__); } while (0)
static int g_displayLevel = 2;
/* ************************************
* Local variables
**************************************/
static U32 g_nbIterations = NBLOOPS;
/* ************************************
* Benchmark Functions
**************************************/
static clock_t BMK_clockSpan( clock_t start )
{
return clock() - start; /* works even if overflow; Typical max span ~ 30 mn */
}
static size_t BMK_findMaxMem(U64 requiredMem)
{
size_t const step = 64 MB;
void* testmem = NULL;
requiredMem = (((requiredMem >> 26) + 1) << 26);
requiredMem += 2*step;
if (requiredMem > MAX_MEM) requiredMem = MAX_MEM;
while (!testmem) {
if (requiredMem > step) requiredMem -= step;
else requiredMem >>= 1;
testmem = malloc ((size_t)requiredMem);
}
free (testmem);
/* keep some space available */
if (requiredMem > step) requiredMem -= step;
else requiredMem >>= 1;
return (size_t)requiredMem;
}
static U64 BMK_GetFileSize(const char* infilename)
{
int r;
#if defined(_MSC_VER)
struct _stat64 statbuf;
r = _stat64(infilename, &statbuf);
#else
struct stat statbuf;
r = stat(infilename, &statbuf);
#endif
if (r || !S_ISREG(statbuf.st_mode)) return 0; /* No good... */
return (U64)statbuf.st_size;
}
typedef U32 (*hashFunction)(const void* buffer, size_t bufferSize, U32 seed);
static U32 localXXH32(const void* buffer, size_t bufferSize, U32 seed) { return XXH32(buffer, bufferSize, seed); }
static U32 localXXH64(const void* buffer, size_t bufferSize, U32 seed) { return (U32)XXH64(buffer, bufferSize, seed); }
static U32 localXXH3_64b(const void* buffer, size_t bufferSize, U32 seed) { (void)seed; return (U32)XXH3_64bits(buffer, bufferSize); }
static U32 localXXH3_64b_seeded(const void* buffer, size_t bufferSize, U32 seed) { return (U32)XXH3_64bits_withSeed(buffer, bufferSize, seed); }
static U32 localXXH3_128b(const void* buffer, size_t bufferSize, U32 seed) { (void)seed; return (U32)(XXH3_128bits(buffer, bufferSize).low64); }
static U32 localXXH3_128b_seeded(const void* buffer, size_t bufferSize, U32 seed) { return (U32)(XXH3_128bits_withSeed(buffer, bufferSize, seed).low64); }
static void BMK_benchHash(hashFunction h, const char* hName, const void* buffer, size_t bufferSize)
{
U32 nbh_perIteration = (U32)((300 MB) / (bufferSize+1)) + 1; /* first loop conservatively aims for 300 MB/s */
U32 iterationNb;
double fastestH = 100000000.;
DISPLAYLEVEL(2, "\r%70s\r", ""); /* Clean display line */
if (g_nbIterations<1) g_nbIterations=1;
for (iterationNb = 1; iterationNb <= g_nbIterations; iterationNb++) {
U32 r=0;
clock_t cStart;
DISPLAYLEVEL(2, "%1u-%-22.22s : %10u ->\r", (unsigned)iterationNb, hName, (unsigned)bufferSize);
cStart = clock();
while (clock() == cStart); /* starts clock() at its exact beginning */
cStart = clock();
{ U32 u;
for (u=0; u<nbh_perIteration; u++)
r += h(buffer, bufferSize, u);
}
if (r==0) DISPLAYLEVEL(3,".\r"); /* do something with r to defeat compiler "optimizing" away hash */
{ clock_t const nbTicks = BMK_clockSpan(cStart);
double const ticksPerHash = ((double)nbTicks / TIMELOOP) / nbh_perIteration;
if (nbTicks < TIMELOOP_MIN) {
/* not enough time spent in benchmarking, risk of rounding bias */
if (nbTicks == 0) { /* faster than resolution timer */
nbh_perIteration *= 100;
} else {
/* update nbh_perIteration so that next round last approximately 1 second */
double nbh_perSecond = (1 / ticksPerHash) + 1;
if (nbh_perSecond > (double)(4000U<<20)) nbh_perSecond = (double)(4000U<<20); /* avoid overflow */
nbh_perIteration = (U32)nbh_perSecond;
}
iterationNb--; /* try again */
continue;
}
if (ticksPerHash < fastestH) fastestH = ticksPerHash;
DISPLAYLEVEL(2, "%1u-%-22.22s : %10u -> %8.0f it/s (%7.1f MB/s) \r",
(unsigned)iterationNb, hName, (unsigned)bufferSize,
(double)1 / fastestH,
((double)bufferSize / (1 MB)) / fastestH );
}
{ double nbh_perSecond = (1 / fastestH) + 1;
if (nbh_perSecond > (double)(4000U<<20)) nbh_perSecond = (double)(4000U<<20); /* avoid overflow */
nbh_perIteration = (U32)nbh_perSecond;
}
}
DISPLAYLEVEL(1, "%-24.24s : %10u -> %8.0f it/s (%7.1f MB/s) \n",
hName, (unsigned)bufferSize,
(double)1 / fastestH,
((double)bufferSize / (1 MB)) / fastestH );
if (g_displayLevel<1)
DISPLAYLEVEL(0, "%u, ", (unsigned)((double)1 / fastestH));
}
/* BMK_benchMem():
* specificTest : 0 == run all tests, 1+ run only specific test
* buffer : is supposed 8-bytes aligned (if malloc'ed, it should be)
* the real allocated size of buffer is supposed to be >= (bufferSize+3).
* @return : 0 on success, 1 if error (invalid mode selected) */
static int BMK_benchMem(const void* buffer, size_t bufferSize, U32 specificTest)
{
assert((((size_t)buffer) & 8) == 0); /* ensure alignment */
/* XXH32 bench */
if ((specificTest==0) | (specificTest==1))
BMK_benchHash(localXXH32, "XXH32", buffer, bufferSize);
/* Bench XXH32 on Unaligned input */
if ((specificTest==0) | (specificTest==2))
BMK_benchHash(localXXH32, "XXH32 unaligned", ((const char*)buffer)+1, bufferSize);
/* Bench XXH64 */
if ((specificTest==0) | (specificTest==3))
BMK_benchHash(localXXH64, "XXH64", buffer, bufferSize);
/* Bench XXH64 on Unaligned input */
if ((specificTest==0) | (specificTest==4))
BMK_benchHash(localXXH64, "XXH64 unaligned", ((const char*)buffer)+3, bufferSize);
/* Bench XXH3 */
if ((specificTest==0) | (specificTest==5))
BMK_benchHash(localXXH3_64b, "XXH3_64b", buffer, bufferSize);
/* Bench XXH3 on Unaligned input */
if ((specificTest==0) | (specificTest==6))
BMK_benchHash(localXXH3_64b, "XXH3_64b unaligned", ((const char*)buffer)+3, bufferSize);
/* Bench XXH3 */
if ((specificTest==0) | (specificTest==7))
BMK_benchHash(localXXH3_64b_seeded, "XXH3_64b seeded", buffer, bufferSize);
/* Bench XXH3 on Unaligned input */
if ((specificTest==0) | (specificTest==8))
BMK_benchHash(localXXH3_64b_seeded, "XXH3_64b seeded unaligned", ((const char*)buffer)+3, bufferSize);
/* Bench XXH3 */
if ((specificTest==0) | (specificTest==9))
BMK_benchHash(localXXH3_128b, "XXH128", buffer, bufferSize);
/* Bench XXH3 on Unaligned input */
if ((specificTest==0) | (specificTest==10))
BMK_benchHash(localXXH3_128b, "XXH128 unaligned", ((const char*)buffer)+3, bufferSize);
/* Bench XXH3 */
if ((specificTest==0) | (specificTest==11))
BMK_benchHash(localXXH3_128b_seeded, "XXH128 seeded", buffer, bufferSize);
/* Bench XXH3 on Unaligned input */
if ((specificTest==0) | (specificTest==12))
BMK_benchHash(localXXH3_128b_seeded, "XXH128 seeded unaligned", ((const char*)buffer)+3, bufferSize);
if (specificTest > 12) {
DISPLAY("Benchmark mode invalid.\n");
return 1;
}
return 0;
}
static size_t BMK_selectBenchedSize(const char* fileName)
{ U64 const inFileSize = BMK_GetFileSize(fileName);
size_t benchedSize = (size_t) BMK_findMaxMem(inFileSize);
if ((U64)benchedSize > inFileSize) benchedSize = (size_t)inFileSize;
if (benchedSize < inFileSize) {
DISPLAY("Not enough memory for '%s' full size; testing %i MB only...\n", fileName, (int)(benchedSize>>20));
}
return benchedSize;
}
static int BMK_benchFiles(const char** fileNamesTable, int nbFiles, U32 specificTest)
{
int result = 0;
int fileIdx;
for (fileIdx=0; fileIdx<nbFiles; fileIdx++) {
const char* const inFileName = fileNamesTable[fileIdx];
assert(inFileName != NULL);
{
FILE* const inFile = fopen( inFileName, "rb" );
size_t const benchedSize = BMK_selectBenchedSize(inFileName);
char* const buffer = (char*)calloc(benchedSize+16+3, 1);
void* const alignedBuffer = (buffer+15) - (((size_t)(buffer+15)) & 0xF); /* align on next 16 bytes */
/* Checks */
if (inFile==NULL){
DISPLAY("Error: Could not open '%s': %s.\n", inFileName, strerror(errno));
free(buffer);
return 11;
}
if(!buffer) {
DISPLAY("\nError: Out of memory.\n");
fclose(inFile);
return 12;
}
/* Fill input buffer */
DISPLAYLEVEL(2, "\rLoading %s... \n", inFileName);
{ size_t const readSize = fread(alignedBuffer, 1, benchedSize, inFile);
fclose(inFile);
if(readSize != benchedSize) {
DISPLAY("\nError: Could not read '%s': %s.\n", inFileName, strerror(errno));
free(buffer);
return 13;
} }
/* bench */
result |= BMK_benchMem(alignedBuffer, benchedSize, specificTest);
free(buffer);
}
}
return result;
}
static int BMK_benchInternal(size_t keySize, U32 specificTest)
{
void* const buffer = calloc(keySize+16+3, 1);
if (!buffer) {
DISPLAY("\nError: Out of memory.\n");
return 12;
}
{ const void* const alignedBuffer = ((char*)buffer+15) - (((size_t)((char*)buffer+15)) & 0xF); /* align on next 16 bytes */
/* bench */
DISPLAYLEVEL(1, "Sample of ");
if (keySize > 10 KB) {
DISPLAYLEVEL(1, "%u KB", (unsigned)(keySize >> 10));
} else {
DISPLAYLEVEL(1, "%u bytes", (unsigned)keySize);
}
DISPLAYLEVEL(1, "... \n");
{ int const result = BMK_benchMem(alignedBuffer, keySize, specificTest);
free(buffer);
return result;
} }
}
/* ************************************************
* Self-test :
* ensure results consistency accross platforms
*********************************************** */
static void BMK_checkResult32(XXH32_hash_t r1, XXH32_hash_t r2)
{
static int nbTests = 1;
if (r1!=r2) {
DISPLAY("\rError: 32-bit hash test %i: Internal sanity check failed!\n", nbTests);
DISPLAY("\rGot 0x%08X, expected 0x%08X.\n", (unsigned)r1, (unsigned)r2);
DISPLAY("\rNote: If you modified the hash functions, make sure to either update the values\n"
"or temporarily comment out the tests in BMK_sanityCheck.\n");
exit(1);
}
nbTests++;
}
static void BMK_checkResult64(XXH64_hash_t r1, XXH64_hash_t r2)
{
static int nbTests = 1;
if (r1!=r2) {
DISPLAY("\rError: 64-bit hash test %i: Internal sanity check failed!\n", nbTests);
DISPLAY("\rGot 0x%08X%08XULL, expected 0x%08X%08XULL.\n",
(unsigned)(r1>>32), (unsigned)r1, (unsigned)(r2>>32), (unsigned)r2);
DISPLAY("\rNote: If you modified the hash functions, make sure to either update the values\n"
"or temporarily comment out the tests in BMK_sanityCheck.\n");
exit(1);
}
nbTests++;
}
static void BMK_checkResult128(XXH128_hash_t r1, XXH128_hash_t r2)
{
static int nbTests = 1;
if ((r1.low64 != r2.low64) || (r1.high64 != r2.high64)) {
DISPLAY("\rError: 128-bit hash test %i: Internal sanity check failed.\n", nbTests);
DISPLAY("\rGot { 0x%08X%08XULL, 0x%08X%08XULL }, expected { 0x%08X%08XULL, %08X%08XULL } \n",
(unsigned)(r1.low64>>32), (unsigned)r1.low64, (unsigned)(r1.high64>>32), (unsigned)r1.high64,
(unsigned)(r2.low64>>32), (unsigned)r2.low64, (unsigned)(r2.high64>>32), (unsigned)r2.high64 );
DISPLAY("\rNote: If you modified the hash functions, make sure to either update the values\n"
"or temporarily comment out the tests in BMK_sanityCheck.\n");
exit(1);
}
nbTests++;
}
static void BMK_testXXH32(const void* data, size_t len, U32 seed, U32 Nresult)
{
XXH32_state_t state;
size_t pos;
if (len>0) assert(data != NULL);
BMK_checkResult32(XXH32(data, len, seed), Nresult);
(void)XXH32_reset(&state, seed);
(void)XXH32_update(&state, data, len);
BMK_checkResult32(XXH32_digest(&state), Nresult);
(void)XXH32_reset(&state, seed);
for (pos=0; pos<len; pos++)
(void)XXH32_update(&state, ((const char*)data)+pos, 1);
BMK_checkResult32(XXH32_digest(&state), Nresult);
}
static void BMK_testXXH64(const void* data, size_t len, U64 seed, U64 Nresult)
{
XXH64_state_t state;
size_t pos;
if (len>0) assert(data != NULL);
BMK_checkResult64(XXH64(data, len, seed), Nresult);
(void)XXH64_reset(&state, seed);
(void)XXH64_update(&state, data, len);
BMK_checkResult64(XXH64_digest(&state), Nresult);
(void)XXH64_reset(&state, seed);
for (pos=0; pos<len; pos++)
(void)XXH64_update(&state, ((const char*)data)+pos, 1);
BMK_checkResult64(XXH64_digest(&state), Nresult);
}
static void BMK_testXXH3(const void* data, size_t len, U64 seed, U64 Nresult)
{
if (len>0) assert(data != NULL);
{ U64 const Dresult = XXH3_64bits_withSeed(data, len, seed);
BMK_checkResult64(Dresult, Nresult);
}
/* check that the no-seed variant produces same result as seed==0 */
if (seed == 0) {
U64 const Dresult = XXH3_64bits(data, len);
BMK_checkResult64(Dresult, Nresult);
}
/* streaming API test */
{ XXH3_state_t state;
/* single ingestion */
(void)XXH3_64bits_reset_withSeed(&state, seed);
(void)XXH3_64bits_update(&state, data, len);
BMK_checkResult64(XXH3_64bits_digest(&state), Nresult);
if (len > 3) {
/* 2 ingestions */
(void)XXH3_64bits_reset_withSeed(&state, seed);
(void)XXH3_64bits_update(&state, data, 3);
(void)XXH3_64bits_update(&state, (const char*)data+3, len-3);
BMK_checkResult64(XXH3_64bits_digest(&state), Nresult);
}
/* byte by byte ingestion */
{ size_t pos;
(void)XXH3_64bits_reset_withSeed(&state, seed);
for (pos=0; pos<len; pos++)
(void)XXH3_64bits_update(&state, ((const char*)data)+pos, 1);
BMK_checkResult64(XXH3_64bits_digest(&state), Nresult);
} }
}
static void BMK_testXXH3_withSecret(const void* data, size_t len, const void* secret, size_t secretSize, U64 Nresult)
{
if (len>0) assert(data != NULL);
{ U64 const Dresult = XXH3_64bits_withSecret(data, len, secret, secretSize);
BMK_checkResult64(Dresult, Nresult);
}
/* streaming API test */
{ XXH3_state_t state;
(void)XXH3_64bits_reset_withSecret(&state, secret, secretSize);
(void)XXH3_64bits_update(&state, data, len);
BMK_checkResult64(XXH3_64bits_digest(&state), Nresult);
/* byte by byte ingestion */
{ size_t pos;
(void)XXH3_64bits_reset_withSecret(&state, secret, secretSize);
for (pos=0; pos<len; pos++)
(void)XXH3_64bits_update(&state, ((const char*)data)+pos, 1);
BMK_checkResult64(XXH3_64bits_digest(&state), Nresult);
} }
}
void BMK_testXXH128(const void* data, size_t len, U64 seed, XXH128_hash_t Nresult)
{
{ XXH128_hash_t const Dresult = XXH3_128bits_withSeed(data, len, seed);
BMK_checkResult128(Dresult, Nresult);
}
/* check that XXH128() is identical to XXH3_128bits_withSeed() */
{ XXH128_hash_t const Dresult2 = XXH128(data, len, seed);
BMK_checkResult128(Dresult2, Nresult);
}
/* check that the no-seed variant produces same result as seed==0 */
if (seed == 0) {
XXH128_hash_t const Dresult = XXH3_128bits(data, len);
BMK_checkResult128(Dresult, Nresult);
}
/* streaming API test */
{ XXH3_state_t state;
/* single ingestion */
(void)XXH3_128bits_reset_withSeed(&state, seed);
(void)XXH3_128bits_update(&state, data, len);
BMK_checkResult128(XXH3_128bits_digest(&state), Nresult);
if (len > 3) {
/* 2 ingestions */
(void)XXH3_128bits_reset_withSeed(&state, seed);
(void)XXH3_128bits_update(&state, data, 3);
(void)XXH3_128bits_update(&state, (const char*)data+3, len-3);
BMK_checkResult128(XXH3_128bits_digest(&state), Nresult);
}
/* byte by byte ingestion */
{ size_t pos;
(void)XXH3_128bits_reset_withSeed(&state, seed);
for (pos=0; pos<len; pos++)
(void)XXH3_128bits_update(&state, ((const char*)data)+pos, 1);
BMK_checkResult128(XXH3_128bits_digest(&state), Nresult);
} }
}
#define SANITY_BUFFER_SIZE 2243
static void BMK_sanityCheck(void)
{
const U32 prime = 2654435761U;
const U64 prime64 = 11400714785074694797ULL;
U8 sanityBuffer[SANITY_BUFFER_SIZE];
U64 byteGen = prime;
int i;
for (i=0; i<SANITY_BUFFER_SIZE; i++) {
sanityBuffer[i] = (U8)(byteGen>>56);
byteGen *= prime64;
}
BMK_testXXH32(NULL, 0, 0, 0x02CC5D05);
BMK_testXXH32(NULL, 0, prime, 0x36B78AE7);
BMK_testXXH32(sanityBuffer, 1, 0, 0xCF65B03E);
BMK_testXXH32(sanityBuffer, 1, prime, 0xB4545AA4);
BMK_testXXH32(sanityBuffer, 14, 0, 0x1208E7E2);
BMK_testXXH32(sanityBuffer, 14, prime, 0x6AF1D1FE);
BMK_testXXH32(sanityBuffer,222, 0, 0x5BD11DBD);
BMK_testXXH32(sanityBuffer,222, prime, 0x58803C5F);
BMK_testXXH64(NULL , 0, 0, 0xEF46DB3751D8E999ULL);
BMK_testXXH64(NULL , 0, prime, 0xAC75FDA2929B17EFULL);
BMK_testXXH64(sanityBuffer, 1, 0, 0xE934A84ADB052768ULL);
BMK_testXXH64(sanityBuffer, 1, prime, 0x5014607643A9B4C3ULL);
BMK_testXXH64(sanityBuffer, 4, 0, 0x9136A0DCA57457EEULL);
BMK_testXXH64(sanityBuffer, 14, 0, 0x8282DCC4994E35C8ULL);
BMK_testXXH64(sanityBuffer, 14, prime, 0xC3BD6BF63DEB6DF0ULL);
BMK_testXXH64(sanityBuffer,222, 0, 0xB641AE8CB691C174ULL);
BMK_testXXH64(sanityBuffer,222, prime, 0x20CB8AB7AE10C14AULL);
BMK_testXXH3(NULL, 0, 0, 0); /* zero-length hash is always 0 */
BMK_testXXH3(NULL, 0, prime64, 0);
BMK_testXXH3(sanityBuffer, 1, 0, 0x7198D737CFE7F386ULL); /* 1 - 3 */
BMK_testXXH3(sanityBuffer, 1, prime64, 0xB70252DB7161C2BDULL); /* 1 - 3 */
BMK_testXXH3(sanityBuffer, 6, 0, 0x22CBF5F3E1F6257CULL); /* 4 - 8 */
BMK_testXXH3(sanityBuffer, 6, prime64, 0x6398631C12AB94CEULL); /* 4 - 8 */
BMK_testXXH3(sanityBuffer, 12, 0, 0xD5361CCEEBB5A0CCULL); /* 9 - 16 */
BMK_testXXH3(sanityBuffer, 12, prime64, 0xC4C125E75A808C3DULL); /* 9 - 16 */
BMK_testXXH3(sanityBuffer, 24, 0, 0x46796F3F78B20F6BULL); /* 17 - 32 */
BMK_testXXH3(sanityBuffer, 24, prime64, 0x60171A7CD0A44C10ULL); /* 17 - 32 */
BMK_testXXH3(sanityBuffer, 48, 0, 0xD8D4D3590D136E11ULL); /* 33 - 64 */
BMK_testXXH3(sanityBuffer, 48, prime64, 0x05441F2AEC2A1296ULL); /* 33 - 64 */
BMK_testXXH3(sanityBuffer, 80, 0, 0xA1DC8ADB3145B86AULL); /* 65 - 96 */
BMK_testXXH3(sanityBuffer, 80, prime64, 0xC9D55256965B7093ULL); /* 65 - 96 */
BMK_testXXH3(sanityBuffer, 112, 0, 0xE43E5717A61D3759ULL); /* 97 -128 */
BMK_testXXH3(sanityBuffer, 112, prime64, 0x5A5F89A3FECE44A5ULL); /* 97 -128 */
BMK_testXXH3(sanityBuffer, 195, 0, 0x6F747739CBAC22A5ULL); /* 129-240 */
BMK_testXXH3(sanityBuffer, 195, prime64, 0x33368E23C7F95810ULL); /* 129-240 */
BMK_testXXH3(sanityBuffer, 403, 0, 0x4834389B15D981E8ULL); /* one block, last stripe is overlapping */
BMK_testXXH3(sanityBuffer, 403, prime64, 0x85CE5DFFC7B07C87ULL); /* one block, last stripe is overlapping */
BMK_testXXH3(sanityBuffer, 512, 0, 0x6A1B982631F059A8ULL); /* one block, finishing at stripe boundary */
BMK_testXXH3(sanityBuffer, 512, prime64, 0x10086868CF0ADC99ULL); /* one block, finishing at stripe boundary */
BMK_testXXH3(sanityBuffer,2048, 0, 0xEFEFD4449323CDD4ULL); /* 2 blocks, finishing at block boundary */
BMK_testXXH3(sanityBuffer,2048, prime64, 0x01C85E405ECA3F6EULL); /* 2 blocks, finishing at block boundary */
BMK_testXXH3(sanityBuffer,2240, 0, 0x998C0437486672C7ULL); /* 3 blocks, finishing at stripe boundary */
BMK_testXXH3(sanityBuffer,2240, prime64, 0x4ED38056B87ABC7FULL); /* 3 blocks, finishing at stripe boundary */
BMK_testXXH3(sanityBuffer,2243, 0, 0xA559D20581D742D3ULL); /* 3 blocks, last stripe is overlapping */
BMK_testXXH3(sanityBuffer,2243, prime64, 0x96E051AB57F21FC8ULL); /* 3 blocks, last stripe is overlapping */
{ const void* const secret = sanityBuffer + 7;
const size_t secretSize = XXH3_SECRET_SIZE_MIN + 11;
BMK_testXXH3_withSecret(NULL, 0, secret, secretSize, 0); /* zero-length hash is always 0 */
BMK_testXXH3_withSecret(sanityBuffer, 1, secret, secretSize, 0x7F69735D618DB3F0ULL); /* 1 - 3 */
BMK_testXXH3_withSecret(sanityBuffer, 6, secret, secretSize, 0xBFCC7CB1B3554DCEULL); /* 4 - 8 */
BMK_testXXH3_withSecret(sanityBuffer, 12, secret, secretSize, 0x8C50DC90AC9206FCULL); /* 9 - 16 */
BMK_testXXH3_withSecret(sanityBuffer, 24, secret, secretSize, 0x1CD2C2EE9B9A0928ULL); /* 17 - 32 */
BMK_testXXH3_withSecret(sanityBuffer, 48, secret, secretSize, 0xA785256D9D65D514ULL); /* 33 - 64 */
BMK_testXXH3_withSecret(sanityBuffer, 80, secret, secretSize, 0x6F3053360D21BBB7ULL); /* 65 - 96 */
BMK_testXXH3_withSecret(sanityBuffer, 112, secret, secretSize, 0x560E82D25684154CULL); /* 97 -128 */
BMK_testXXH3_withSecret(sanityBuffer, 195, secret, secretSize, 0xBA5BDDBC5A767B11ULL); /* 129-240 */
BMK_testXXH3_withSecret(sanityBuffer, 403, secret, secretSize, 0xFC3911BBA656DB58ULL); /* one block, last stripe is overlapping */
BMK_testXXH3_withSecret(sanityBuffer, 512, secret, secretSize, 0x306137DD875741F1ULL); /* one block, finishing at stripe boundary */
BMK_testXXH3_withSecret(sanityBuffer,2048, secret, secretSize, 0x2836B83880AD3C0CULL); /* >= 2 blocks, at least one scrambling */
BMK_testXXH3_withSecret(sanityBuffer,2243, secret, secretSize, 0x3446E248A00CB44AULL); /* >= 2 blocks, at least one scrambling, last stripe unaligned */
}
{ XXH128_hash_t const expected = { 0, 0 };
BMK_testXXH128(NULL, 0, 0, expected); /* zero-length hash is { seed, -seed } by default */
}
{ XXH128_hash_t const expected = { 0, 0 };
BMK_testXXH128(NULL, 0, prime, expected);
}
{ XXH128_hash_t const expected = { 0x7198D737CFE7F386ULL, 0x153C28D2A04DC807ULL };
BMK_testXXH128(sanityBuffer, 1, 0, expected); /* 1-3 */
}
{ XXH128_hash_t const expected = { 0x8E05996EC27C0F46ULL, 0x89A7484EC876D545ULL };
BMK_testXXH128(sanityBuffer, 1, prime, expected); /* 1-3 */
}
{ XXH128_hash_t const expected = { 0x22CBF5F3E1F6257CULL, 0xD4E6C2B94FFC3BFAULL };
BMK_testXXH128(sanityBuffer, 6, 0, expected); /* 4-8 */
}
{ XXH128_hash_t const expected = { 0x97B28D3079F8541FULL, 0xEFC0B954298E6555ULL };
BMK_testXXH128(sanityBuffer, 6, prime, expected); /* 4-8 */
}
{ XXH128_hash_t const expected = { 0x9044570967199F91ULL, 0x738EE3E642A85165ULL };
BMK_testXXH128(sanityBuffer, 12, 0, expected); /* 9-16 */
}
{ XXH128_hash_t const expected = { 0xE3C75A78FE67D411ULL, 0xD4396DA60355312BULL };
BMK_testXXH128(sanityBuffer, 12, prime, expected); /* 9-16 */
}
{ XXH128_hash_t const expected = { 0x3FD725B2AABCF17DULL, 0x140592647F61C3E1ULL };
BMK_testXXH128(sanityBuffer, 24, 0, expected); /* 17-32 */
}
{ XXH128_hash_t const expected = { 0x9A09D0F4A694DC09ULL, 0x1291B0C7375510E3ULL };
BMK_testXXH128(sanityBuffer, 24, prime, expected); /* 17-32 */
}
{ XXH128_hash_t const expected = { 0x891306BA9DD1D15BULL, 0x32A41AEEC6DE94DEULL };
BMK_testXXH128(sanityBuffer, 48, 0, expected); /* 33-64 */
}
{ XXH128_hash_t const expected = { 0xA199D324899B838EULL, 0x9BB6C003E18B3F75ULL };
BMK_testXXH128(sanityBuffer, 48, prime, expected); /* 33-64 */
}
{ XXH128_hash_t const expected = { 0x33AA30F9947E2743ULL, 0x46307D818EC98842ULL };
BMK_testXXH128(sanityBuffer, 81, 0, expected); /* 65-96 */
}
{ XXH128_hash_t const expected = { 0xAAF9F05DA0993E3CULL, 0x01752B9AFA24C856ULL };
BMK_testXXH128(sanityBuffer, 81, prime, expected); /* 65-96 */
}
{ XXH128_hash_t const expected = { 0x01EE4637BFB66A1BULL, 0xE5CF6E0E85E92048ULL };
BMK_testXXH128(sanityBuffer, 103, 0, expected); /* 97-128 */
}
{ XXH128_hash_t const expected = { 0x784D8A364F48D048ULL, 0x9010B884DAA01151ULL };
BMK_testXXH128(sanityBuffer, 103, prime, expected); /* 97-128 */
}
{ XXH128_hash_t const expected = { 0x5FA77B9DFE8B5CAEULL, 0x2834B37CEC6A753FULL };
BMK_testXXH128(sanityBuffer, 192, 0, expected); /* 129-240 */
}
{ XXH128_hash_t const expected = { 0x75441CE0359A979AULL, 0x399E2847427B3904ULL };
BMK_testXXH128(sanityBuffer, 192, prime, expected); /* 129-240 */
}
{ XXH128_hash_t const expected = { 0xB02CC10BCFE61194ULL, 0xA27C9ABC8C06E4DDULL };
BMK_testXXH128(sanityBuffer, 222, 0, expected); /* 129-240 */
}
{ XXH128_hash_t const expected = { 0x972CB9C6BD8123EDULL, 0x3488C87B4B6FCE5FULL };
BMK_testXXH128(sanityBuffer, 222, prime, expected); /* 129-240 */
}
{ XXH128_hash_t const expected = { 0xB0C48E6D18E9D084ULL, 0xB16FC17E992FF45DULL };
BMK_testXXH128(sanityBuffer, 403, 0, expected); /* one block, last stripe is overlapping */
}
{ XXH128_hash_t const expected = { 0x0A1D320C9520871DULL, 0xCE11CB376EC93252ULL };
BMK_testXXH128(sanityBuffer, 403, prime64, expected); /* one block, last stripe is overlapping */
}
{ XXH128_hash_t const expected = { 0xA03428558AC97327ULL, 0x4ECF51281BA406F7ULL };
BMK_testXXH128(sanityBuffer, 512, 0, expected); /* one block, finishing at stripe boundary */
}
{ XXH128_hash_t const expected = { 0xAF67A482D6C893F2ULL, 0x1382D92F25B84D90ULL };
BMK_testXXH128(sanityBuffer, 512, prime64, expected); /* one block, finishing at stripe boundary */
}
{ XXH128_hash_t const expected = { 0x21901B416B3B9863ULL, 0x212AF8E6326F01E0ULL };
BMK_testXXH128(sanityBuffer,2048, 0, expected); /* two blocks, finishing at block boundary */
}
{ XXH128_hash_t const expected = { 0xBDBB2282577DADECULL, 0xF78CDDC2C9A9A692ULL };
BMK_testXXH128(sanityBuffer,2048, prime, expected); /* two blocks, finishing at block boundary */
}
{ XXH128_hash_t const expected = { 0x00AD52FA9385B6FEULL, 0xC705BAD3356CE302ULL };
BMK_testXXH128(sanityBuffer,2240, 0, expected); /* two blocks, ends at stripe boundary */
}
{ XXH128_hash_t const expected = { 0x10FD0072EC68BFAAULL, 0xE1312F3458817F15ULL };
BMK_testXXH128(sanityBuffer,2240, prime, expected); /* two blocks, ends at stripe boundary */
}
{ XXH128_hash_t const expected = { 0x970C91411533862CULL, 0x4BBD06FF7BFF0AB1ULL };
BMK_testXXH128(sanityBuffer,2237, 0, expected); /* two blocks, ends at stripe boundary */
}
{ XXH128_hash_t const expected = { 0xD80282846D814431ULL, 0x14EBB157B84D9785ULL };
BMK_testXXH128(sanityBuffer,2237, prime, expected); /* two blocks, ends at stripe boundary */
}
DISPLAYLEVEL(3, "\r%70s\r", ""); /* Clean display line */
DISPLAYLEVEL(3, "Sanity check -- all tests ok\n");
}
/* ********************************************************
* File Hashing
**********************************************************/
static void BMK_display_LittleEndian(const void* ptr, size_t length)
{
const U8* p = (const U8*)ptr;
size_t idx;
for (idx=length-1; idx<length; idx--) /* intentional underflow to negative to detect end */
DISPLAYRESULT("%02x", p[idx]);
}
static void BMK_display_BigEndian(const void* ptr, size_t length)
{
const U8* p = (const U8*)ptr;
size_t idx;
for (idx=0; idx<length; idx++)
DISPLAYRESULT("%02x", p[idx]);
}
typedef union {
XXH32_hash_t xxh32;
XXH64_hash_t xxh64;
XXH128_hash_t xxh128;
} Multihash;
/* BMK_hashStream :
* read data from inFile,
* generating incremental hash of type hashType,
* using buffer of size blockSize for temporary storage. */
static Multihash
BMK_hashStream(FILE* inFile,
algoType hashType,
void* buffer, size_t blockSize)
{
XXH32_state_t state32;
XXH64_state_t state64;
XXH3_state_t state128;
/* Init */
(void)XXH32_reset(&state32, XXHSUM32_DEFAULT_SEED);
(void)XXH64_reset(&state64, XXHSUM64_DEFAULT_SEED);
(void)XXH3_128bits_reset(&state128);
/* Load file & update hash */
{ size_t readSize = 1;
while (readSize) {
readSize = fread(buffer, 1, blockSize, inFile);
switch(hashType)
{
case algo_xxh32:
(void)XXH32_update(&state32, buffer, readSize);
break;
case algo_xxh64:
(void)XXH64_update(&state64, buffer, readSize);
break;
case algo_xxh128:
(void)XXH3_128bits_update(&state128, buffer, readSize);
break;
default:
assert(0);
}
} }
{ Multihash finalHash;
switch(hashType)
{