-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathcc.h
1907 lines (1616 loc) · 45.4 KB
/
cc.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
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
/* -----------------------------------------------------------------------------
*
* Copyright (c) 2014-2019 Alexis Naveros.
*
* This software is provided 'as-is', without any express or implied
* warranty. In no event will the authors be held liable for any damages
* arising from the use of this software.
*
* Permission is granted to anyone to use this software for any purpose,
* including commercial applications, and to alter it and redistribute it
* freely, subject to the following restrictions:
*
* 1. The origin of this software must not be misrepresented; you must not
* claim that you wrote the original software. If you use this software
* in a product, an acknowledgment in the product documentation would be
* appreciated but is not required.
* 2. Altered source versions must be plainly marked as such, and must not be
* misrepresented as being the original software.
* 3. This notice may not be removed or altered from any source distribution.
*
* -----------------------------------------------------------------------------
*/
#ifndef CC_H
#define CC_H
#include <sys/time.h>
#include <stdarg.h>
////
#if defined(__linux__) || defined(__gnu_linux__) || defined(__linux) || defined(__linux)
#define CC_LINUX (1)
#define CC_UNIX (1)
#elif defined(__APPLE__)
#define CC_OSX (1)
#define CC_UNIX (1)
#elif defined(__unix__) || defined(__unix) || defined(unix)
#define CC_UNIX (1)
#elif defined(_WIN64) || defined(__WIN64__) || defined(WIN64)
#define CC_WIN64 (1)
#define CC_WIN32 (1)
#define CC_WINDOWS (1)
#elif defined(_WIN32) || defined(__WIN32__) || defined(WIN32)
#define CC_WIN32 (1)
#define CC_WINDOWS (1)
#endif
#if __MINGW64__
#define CC_MINGW32 (1)
#define CC_MINGW64 (1)
#elif __MINGW32__
#define CC_MINGW32 (1)
#endif
#if defined(__amd64__) || defined(__amd64) || defined(__x86_64__) || defined(__x86_64) || defined(_M_X64) || defined(_M_AMD64)
#define CC_ARCH_AMD64 (1)
#endif
#if defined(i386) || defined(__i386) || defined(__i386__) || defined(__i386) || defined(__IA32__) || defined(_M_IX86) || defined(__X86__) || defined(_X86_)
#define CC_ARCH_IA32 (1)
#endif
#if defined(__arm__) || defined(_ARM) || defined(_M_ARM) || defined(__arm)
#define CC_ARCH_ARM (1)
#endif
#if defined(__aarch64__)
#define CC_ARCH_ARM64 (1)
#endif
#if defined(__ARM_NEON) || defined(__ARM_NEON__)
#define CC_CAP_NEON (1)
#endif
#if ( __SSE2__ || _M_X64 || _M_IX86_FP >= 2 )
#define CC_CAP_SSE2 (1)
#endif
#if ( __SSE__ || _M_X64 || _M_IX86_FP >= 1 )
#define CC_CAP_SSE (1)
#endif
#if __AVX__
#define CC_CAP_AVX (1)
#endif
#if __AVX2__
#define CC_CAP_AVX2 (1)
#endif
////
#ifndef ADDRESS
#define ADDRESS(p,o) ((void *)(((char *)p)+(o)))
#endif
#ifndef ADDRESSDIFF
#define ADDRESSDIFF(a,b) (((char *)a)-((char *)b))
#endif
#if defined(__GNUC__) || defined(__INTEL_COMPILER) || defined(__clang__)
#define CC_NOINLINE __attribute__((noinline))
#define CC_ALWAYSINLINE __attribute__((always_inline))
#define CC_LIKELY(x) __builtin_expect(!!(x), 1)
#define CC_UNLIKELY(x) __builtin_expect(!!(x), 0)
#elif defined(_MSC_VER)
#define CC_NOINLINE __declspec(noinline)
#define CC_ALWAYSINLINE __forceinline
#define CC_LIKELY(x) (x)
#define CC_UNLIKELY(x) (x)
#else
#define CC_NOINLINE
#define CC_ALWAYSINLINE
#define CC_LIKELY(x) (x)
#define CC_UNLIKELY(x) (x)
#endif
#if CC_UNIX
#define CC_DIR_SEPARATOR_CHAR '/'
#define CC_DIR_SEPARATOR_STRING "/"
#elif CC_WINDOWS
#define CC_DIR_SEPARATOR_CHAR '\\'
#define CC_DIR_SEPARATOR_STRING "\\"
#else
#define CC_DIR_SEPARATOR_CHAR '/'
#define CC_DIR_SEPARATOR_STRING "/"
#endif
#if CC_WINDOWS
#define CC_LL "I64"
#define CC_LLD "%I64d"
#define CC_LLU "%I64u"
#define CC_LLX "%I64x"
#else
#define CC_LL "ll"
#define CC_LLD "%lld"
#define CC_LLU "%llu"
#define CC_LLX "%llx"
#endif
#ifndef CPUCONF_CACHE_LINE_SIZE
#define CPUCONF_CACHE_LINE_SIZE (64)
#endif
#define CC_SIZEOF_ALIGN4(x) ((sizeof(x)+0x3)&~0x3)
#define CC_SIZEOF_ALIGN8(x) ((sizeof(x)+0x7)&~0x7)
#define CC_SIZEOF_ALIGN16(x) ((sizeof(x)+0xF)&~0xF)
#define CC_SIZEOF_ALIGN32(x) ((sizeof(x)+0x1F)&~0x1F)
#define CC_SIZEOF_ALIGN64(x) ((sizeof(x)+0x3F)&~0x3F)
#define CC_SIZEOF_ALIGN_CACHELINE(x) ((sizeof(x)+CPUCONF_CACHE_LINE_SIZE-1)&~(CPUCONF_CACHE_LINE_SIZE-1))
////
#define CC_MIN(x,y) ((x)>(y)?(y):(x))
#define CC_MAX(x,y) ((x)<(y)?(y):(x))
#define CC_CLAMP(x,min,max) ((x)<(min)?(min):((x)>(max)?(max):(x)))
#define CC_MAX_INT8(x,y) (((int8_t)x)-((((int8_t)x)-((int8_t)y))&((((int8_t)x)-((int8_t)y))>>7)))
#define CC_MAX_INT16(x,y) (((int16_t)x)-((((int16_t)x)-((int16_t)y))&((((int16_t)x)-((int16_t)y))>>15)))
#define CC_MAX_INT32(x,y) (((int32_t)x)-((((int32_t)x)-((int32_t)y))&((((int32_t)x)-((int32_t)y))>>31)))
#define CC_MAX_INT64(x,y) (((int64_t)x)-((((int64_t)x)-((int64_t)y))&((((int64_t)x)-((int64_t)y))>>63)))
#define CC_MIN_INT8(x,y) (((int8_t)y)+((((int8_t)x)-((int8_t)y))&((((int8_t)x)-((int8_t)y))>>7)))
#define CC_MIN_INT16(x,y) (((int16_t)y)+((((int16_t)x)-((int16_t)y))&((((int16_t)x)-((int16_t)y))>>15)))
#define CC_MIN_INT32(x,y) (((int32_t)y)+((((int32_t)x)-((int32_t)y))&((((int32_t)x)-((int32_t)y))>>31)))
#define CC_MIN_INT64(x,y) (((int64_t)y)+((((int64_t)x)-((int64_t)y))&((((int64_t)x)-((int64_t)y))>>63)))
#define CC_SHIFTDIV_INT8(value,shift) ({uint8_t _s=((uint8_t)value)>>7;((int8_t)((value)+(_s<<shift)-_s))>>shift;})
#define CC_SHIFTDIV_INT16(value,shift) ({uint16_t _s=((uint16_t)value)>>15;((int16_t)((value)+(_s<<shift)-_s))>>shift;})
#define CC_SHIFTDIV_INT32(value,shift) ({uint32_t _s=((uint32_t)value)>>31;((int32_t)((value)+(_s<<shift)-_s))>>shift;})
#define CC_SHIFTDIV_INT64(value,shift) ({uint64_t _s=((uint64_t)value)>>63;((int64_t)((value)+(_s<<shift)-_s))>>shift;})
#define CC_SHIFTDIVROUND(value,shift) ((value>>shift)+(((value&((1<<shift)-1))<<1)>=(1<<shift)))
#define CC_SHIFTDIVROUND_INT8(value,shift) ((value>>shift)+((((value&((1<<shift)-1))-((uint8_t)value>>7))<<1)>=(1<<shift)))
#define CC_SHIFTDIVROUND_INT16(value,shift) ((value>>shift)+((((value&((1<<shift)-1))-((uint16_t)value>>15))<<1)>=(1<<shift)))
#define CC_SHIFTDIVROUND_INT32(value,shift) ((value>>shift)+((((value&((1<<shift)-1))-((uint32_t)value>>31))<<1)>=(1<<shift)))
#define CC_SHIFTDIVROUND_INT64(value,shift) ((value>>shift)+((((value&((1<<shift)-1))-((uint64_t)value>>63))<<1)>=(1<<shift)))
#define CC_NUMBITS2(n) ((n&2)?1:0)
#define CC_NUMBITS4(n) ((n&(0xC))?(2+CC_NUMBITS2(n>>2)):(CC_NUMBITS2(n)))
#define CC_NUMBITS8(n) ((n&0xF0)?(4+CC_NUMBITS4(n>>4)):(CC_NUMBITS4(n)))
#define CC_NUMBITS16(n) ((n&0xFF00)?(8+CC_NUMBITS8(n>>8)):(CC_NUMBITS8(n)))
#define CC_NUMBITS32(n) ((n&0xFFFF0000)?(16+CC_NUMBITS16(n>>16)):(CC_NUMBITS16(n)))
#define CC_NUMBITS(n) (n==0?0:CC_NUMBITS32(n)+1)
////
#define CC_STRINGIFY_IN(s) #s
#define CC_STRINGIFY(s) CC_STRINGIFY_IN(s)
#define CC_CONCATENATE_IN(s,n) s ## n
#define CC_CONCATENATE(s,n) CC_CONCATENATE_IN(s,n)
#define CC_STRINGIFY_BYTE_0 "\x00"
#define CC_STRINGIFY_BYTE_1 "\x01"
#define CC_STRINGIFY_BYTE_2 "\x02"
#define CC_STRINGIFY_BYTE_3 "\x03"
#define CC_STRINGIFY_BYTE_4 "\x04"
#define CC_STRINGIFY_BYTE_5 "\x05"
#define CC_STRINGIFY_BYTE_6 "\x06"
#define CC_STRINGIFY_BYTE_7 "\x07"
#define CC_STRINGIFY_BYTE_8 "\x08"
#define CC_STRINGIFY_BYTE_9 "\x09"
#define CC_STRINGIFY_BYTE_10 "\x0a"
#define CC_STRINGIFY_BYTE_11 "\x0b"
#define CC_STRINGIFY_BYTE_12 "\x0c"
#define CC_STRINGIFY_BYTE_13 "\x0d"
#define CC_STRINGIFY_BYTE_14 "\x0e"
#define CC_STRINGIFY_BYTE_15 "\x0f"
#define CC_STRINGIFY_BYTE_16 "\x10"
#define CC_STRINGIFY_BYTE_17 "\x11"
#define CC_STRINGIFY_BYTE_18 "\x12"
#define CC_STRINGIFY_BYTE_19 "\x13"
#define CC_STRINGIFY_BYTE_20 "\x14"
#define CC_STRINGIFY_BYTE_21 "\x15"
#define CC_STRINGIFY_BYTE_22 "\x16"
#define CC_STRINGIFY_BYTE_23 "\x17"
#define CC_STRINGIFY_BYTE_24 "\x18"
#define CC_STRINGIFY_BYTE_25 "\x19"
#define CC_STRINGIFY_BYTE_26 "\x1a"
#define CC_STRINGIFY_BYTE_27 "\x1b"
#define CC_STRINGIFY_BYTE_28 "\x1c"
#define CC_STRINGIFY_BYTE_29 "\x1d"
#define CC_STRINGIFY_BYTE_30 "\x1e"
#define CC_STRINGIFY_BYTE_31 "\x1f"
#define CC_STRINGIFY_BYTE_32 "\x20"
#define CC_STRINGIFY_BYTE_33 "\x21"
#define CC_STRINGIFY_BYTE_34 "\x22"
#define CC_STRINGIFY_BYTE_35 "\x23"
#define CC_STRINGIFY_BYTE_36 "\x24"
#define CC_STRINGIFY_BYTE_37 "\x25"
#define CC_STRINGIFY_BYTE_38 "\x26"
#define CC_STRINGIFY_BYTE_39 "\x27"
#define CC_STRINGIFY_BYTE_40 "\x28"
#define CC_STRINGIFY_BYTE_41 "\x29"
#define CC_STRINGIFY_BYTE_42 "\x2a"
#define CC_STRINGIFY_BYTE_43 "\x2b"
#define CC_STRINGIFY_BYTE_44 "\x2c"
#define CC_STRINGIFY_BYTE_45 "\x2d"
#define CC_STRINGIFY_BYTE_46 "\x2e"
#define CC_STRINGIFY_BYTE_47 "\x2f"
#define CC_STRINGIFY_BYTE_48 "\x30"
#define CC_STRINGIFY_BYTE_49 "\x31"
#define CC_STRINGIFY_BYTE_50 "\x32"
#define CC_STRINGIFY_BYTE_51 "\x33"
#define CC_STRINGIFY_BYTE_52 "\x34"
#define CC_STRINGIFY_BYTE_53 "\x35"
#define CC_STRINGIFY_BYTE_54 "\x36"
#define CC_STRINGIFY_BYTE_55 "\x37"
#define CC_STRINGIFY_BYTE_56 "\x38"
#define CC_STRINGIFY_BYTE_57 "\x39"
#define CC_STRINGIFY_BYTE_58 "\x3a"
#define CC_STRINGIFY_BYTE_59 "\x3b"
#define CC_STRINGIFY_BYTE_60 "\x3c"
#define CC_STRINGIFY_BYTE_61 "\x3d"
#define CC_STRINGIFY_BYTE_62 "\x3e"
#define CC_STRINGIFY_BYTE_63 "\x3f"
#define CC_STRINGIFY_BYTE_64 "\x40"
#define CC_STRINGIFY_BYTE_65 "\x41"
#define CC_STRINGIFY_BYTE_66 "\x42"
#define CC_STRINGIFY_BYTE_67 "\x43"
#define CC_STRINGIFY_BYTE_68 "\x44"
#define CC_STRINGIFY_BYTE_69 "\x45"
#define CC_STRINGIFY_BYTE_70 "\x46"
#define CC_STRINGIFY_BYTE_71 "\x47"
#define CC_STRINGIFY_BYTE_72 "\x48"
#define CC_STRINGIFY_BYTE_73 "\x49"
#define CC_STRINGIFY_BYTE_74 "\x4a"
#define CC_STRINGIFY_BYTE_75 "\x4b"
#define CC_STRINGIFY_BYTE_76 "\x4c"
#define CC_STRINGIFY_BYTE_77 "\x4d"
#define CC_STRINGIFY_BYTE_78 "\x4e"
#define CC_STRINGIFY_BYTE_79 "\x4f"
#define CC_STRINGIFY_BYTE_80 "\x50"
#define CC_STRINGIFY_BYTE_81 "\x51"
#define CC_STRINGIFY_BYTE_82 "\x52"
#define CC_STRINGIFY_BYTE_83 "\x53"
#define CC_STRINGIFY_BYTE_84 "\x54"
#define CC_STRINGIFY_BYTE_85 "\x55"
#define CC_STRINGIFY_BYTE_86 "\x56"
#define CC_STRINGIFY_BYTE_87 "\x57"
#define CC_STRINGIFY_BYTE_88 "\x58"
#define CC_STRINGIFY_BYTE_89 "\x59"
#define CC_STRINGIFY_BYTE_90 "\x5a"
#define CC_STRINGIFY_BYTE_91 "\x5b"
#define CC_STRINGIFY_BYTE_92 "\x5c"
#define CC_STRINGIFY_BYTE_93 "\x5d"
#define CC_STRINGIFY_BYTE_94 "\x5e"
#define CC_STRINGIFY_BYTE_95 "\x5f"
#define CC_STRINGIFY_BYTE_96 "\x60"
#define CC_STRINGIFY_BYTE_97 "\x61"
#define CC_STRINGIFY_BYTE_98 "\x62"
#define CC_STRINGIFY_BYTE_99 "\x63"
#define CC_STRINGIFY_BYTE_100 "\x64"
#define CC_STRINGIFY_BYTE_101 "\x65"
#define CC_STRINGIFY_BYTE_102 "\x66"
#define CC_STRINGIFY_BYTE_103 "\x67"
#define CC_STRINGIFY_BYTE_104 "\x68"
#define CC_STRINGIFY_BYTE_105 "\x69"
#define CC_STRINGIFY_BYTE_106 "\x6a"
#define CC_STRINGIFY_BYTE_107 "\x6b"
#define CC_STRINGIFY_BYTE_108 "\x6c"
#define CC_STRINGIFY_BYTE_109 "\x6d"
#define CC_STRINGIFY_BYTE_110 "\x6e"
#define CC_STRINGIFY_BYTE_111 "\x6f"
#define CC_STRINGIFY_BYTE_112 "\x70"
#define CC_STRINGIFY_BYTE_113 "\x71"
#define CC_STRINGIFY_BYTE_114 "\x72"
#define CC_STRINGIFY_BYTE_115 "\x73"
#define CC_STRINGIFY_BYTE_116 "\x74"
#define CC_STRINGIFY_BYTE_117 "\x75"
#define CC_STRINGIFY_BYTE_118 "\x76"
#define CC_STRINGIFY_BYTE_119 "\x77"
#define CC_STRINGIFY_BYTE_120 "\x78"
#define CC_STRINGIFY_BYTE_121 "\x79"
#define CC_STRINGIFY_BYTE_122 "\x7a"
#define CC_STRINGIFY_BYTE_123 "\x7b"
#define CC_STRINGIFY_BYTE_124 "\x7c"
#define CC_STRINGIFY_BYTE_125 "\x7d"
#define CC_STRINGIFY_BYTE_126 "\x7e"
#define CC_STRINGIFY_BYTE_127 "\x7f"
#define CC_STRINGIFY_BYTE_128 "\x80"
#define CC_STRINGIFY_BYTE_129 "\x81"
#define CC_STRINGIFY_BYTE_130 "\x82"
#define CC_STRINGIFY_BYTE_131 "\x83"
#define CC_STRINGIFY_BYTE_132 "\x84"
#define CC_STRINGIFY_BYTE_133 "\x85"
#define CC_STRINGIFY_BYTE_134 "\x86"
#define CC_STRINGIFY_BYTE_135 "\x87"
#define CC_STRINGIFY_BYTE_136 "\x88"
#define CC_STRINGIFY_BYTE_137 "\x89"
#define CC_STRINGIFY_BYTE_138 "\x8a"
#define CC_STRINGIFY_BYTE_139 "\x8b"
#define CC_STRINGIFY_BYTE_140 "\x8c"
#define CC_STRINGIFY_BYTE_141 "\x8d"
#define CC_STRINGIFY_BYTE_142 "\x8e"
#define CC_STRINGIFY_BYTE_143 "\x8f"
#define CC_STRINGIFY_BYTE_144 "\x90"
#define CC_STRINGIFY_BYTE_145 "\x91"
#define CC_STRINGIFY_BYTE_146 "\x92"
#define CC_STRINGIFY_BYTE_147 "\x93"
#define CC_STRINGIFY_BYTE_148 "\x94"
#define CC_STRINGIFY_BYTE_149 "\x95"
#define CC_STRINGIFY_BYTE_150 "\x96"
#define CC_STRINGIFY_BYTE_151 "\x97"
#define CC_STRINGIFY_BYTE_152 "\x98"
#define CC_STRINGIFY_BYTE_153 "\x99"
#define CC_STRINGIFY_BYTE_154 "\x9a"
#define CC_STRINGIFY_BYTE_155 "\x9b"
#define CC_STRINGIFY_BYTE_156 "\x9c"
#define CC_STRINGIFY_BYTE_157 "\x9d"
#define CC_STRINGIFY_BYTE_158 "\x9e"
#define CC_STRINGIFY_BYTE_159 "\x9f"
#define CC_STRINGIFY_BYTE_160 "\xa0"
#define CC_STRINGIFY_BYTE_161 "\xa1"
#define CC_STRINGIFY_BYTE_162 "\xa2"
#define CC_STRINGIFY_BYTE_163 "\xa3"
#define CC_STRINGIFY_BYTE_164 "\xa4"
#define CC_STRINGIFY_BYTE_165 "\xa5"
#define CC_STRINGIFY_BYTE_166 "\xa6"
#define CC_STRINGIFY_BYTE_167 "\xa7"
#define CC_STRINGIFY_BYTE_168 "\xa8"
#define CC_STRINGIFY_BYTE_169 "\xa9"
#define CC_STRINGIFY_BYTE_170 "\xaa"
#define CC_STRINGIFY_BYTE_171 "\xab"
#define CC_STRINGIFY_BYTE_172 "\xac"
#define CC_STRINGIFY_BYTE_173 "\xad"
#define CC_STRINGIFY_BYTE_174 "\xae"
#define CC_STRINGIFY_BYTE_175 "\xaf"
#define CC_STRINGIFY_BYTE_176 "\xb0"
#define CC_STRINGIFY_BYTE_177 "\xb1"
#define CC_STRINGIFY_BYTE_178 "\xb2"
#define CC_STRINGIFY_BYTE_179 "\xb3"
#define CC_STRINGIFY_BYTE_180 "\xb4"
#define CC_STRINGIFY_BYTE_181 "\xb5"
#define CC_STRINGIFY_BYTE_182 "\xb6"
#define CC_STRINGIFY_BYTE_183 "\xb7"
#define CC_STRINGIFY_BYTE_184 "\xb8"
#define CC_STRINGIFY_BYTE_185 "\xb9"
#define CC_STRINGIFY_BYTE_186 "\xba"
#define CC_STRINGIFY_BYTE_187 "\xbb"
#define CC_STRINGIFY_BYTE_188 "\xbc"
#define CC_STRINGIFY_BYTE_189 "\xbd"
#define CC_STRINGIFY_BYTE_190 "\xbe"
#define CC_STRINGIFY_BYTE_191 "\xbf"
#define CC_STRINGIFY_BYTE_192 "\xc0"
#define CC_STRINGIFY_BYTE_193 "\xc1"
#define CC_STRINGIFY_BYTE_194 "\xc2"
#define CC_STRINGIFY_BYTE_195 "\xc3"
#define CC_STRINGIFY_BYTE_196 "\xc4"
#define CC_STRINGIFY_BYTE_197 "\xc5"
#define CC_STRINGIFY_BYTE_198 "\xc6"
#define CC_STRINGIFY_BYTE_199 "\xc7"
#define CC_STRINGIFY_BYTE_200 "\xc8"
#define CC_STRINGIFY_BYTE_201 "\xc9"
#define CC_STRINGIFY_BYTE_202 "\xca"
#define CC_STRINGIFY_BYTE_203 "\xcb"
#define CC_STRINGIFY_BYTE_204 "\xcc"
#define CC_STRINGIFY_BYTE_205 "\xcd"
#define CC_STRINGIFY_BYTE_206 "\xce"
#define CC_STRINGIFY_BYTE_207 "\xcf"
#define CC_STRINGIFY_BYTE_208 "\xd0"
#define CC_STRINGIFY_BYTE_209 "\xd1"
#define CC_STRINGIFY_BYTE_210 "\xd2"
#define CC_STRINGIFY_BYTE_211 "\xd3"
#define CC_STRINGIFY_BYTE_212 "\xd4"
#define CC_STRINGIFY_BYTE_213 "\xd5"
#define CC_STRINGIFY_BYTE_214 "\xd6"
#define CC_STRINGIFY_BYTE_215 "\xd7"
#define CC_STRINGIFY_BYTE_216 "\xd8"
#define CC_STRINGIFY_BYTE_217 "\xd9"
#define CC_STRINGIFY_BYTE_218 "\xda"
#define CC_STRINGIFY_BYTE_219 "\xdb"
#define CC_STRINGIFY_BYTE_220 "\xdc"
#define CC_STRINGIFY_BYTE_221 "\xdd"
#define CC_STRINGIFY_BYTE_222 "\xde"
#define CC_STRINGIFY_BYTE_223 "\xdf"
#define CC_STRINGIFY_BYTE_224 "\xe0"
#define CC_STRINGIFY_BYTE_225 "\xe1"
#define CC_STRINGIFY_BYTE_226 "\xe2"
#define CC_STRINGIFY_BYTE_227 "\xe3"
#define CC_STRINGIFY_BYTE_228 "\xe4"
#define CC_STRINGIFY_BYTE_229 "\xe5"
#define CC_STRINGIFY_BYTE_230 "\xe6"
#define CC_STRINGIFY_BYTE_231 "\xe7"
#define CC_STRINGIFY_BYTE_232 "\xe8"
#define CC_STRINGIFY_BYTE_233 "\xe9"
#define CC_STRINGIFY_BYTE_234 "\xea"
#define CC_STRINGIFY_BYTE_235 "\xeb"
#define CC_STRINGIFY_BYTE_236 "\xec"
#define CC_STRINGIFY_BYTE_237 "\xed"
#define CC_STRINGIFY_BYTE_238 "\xee"
#define CC_STRINGIFY_BYTE_239 "\xef"
#define CC_STRINGIFY_BYTE_240 "\xf0"
#define CC_STRINGIFY_BYTE_241 "\xf1"
#define CC_STRINGIFY_BYTE_242 "\xf2"
#define CC_STRINGIFY_BYTE_243 "\xf3"
#define CC_STRINGIFY_BYTE_244 "\xf4"
#define CC_STRINGIFY_BYTE_245 "\xf5"
#define CC_STRINGIFY_BYTE_246 "\xf6"
#define CC_STRINGIFY_BYTE_247 "\xf7"
#define CC_STRINGIFY_BYTE_248 "\xf8"
#define CC_STRINGIFY_BYTE_249 "\xf9"
#define CC_STRINGIFY_BYTE_250 "\xfa"
#define CC_STRINGIFY_BYTE_251 "\xfb"
#define CC_STRINGIFY_BYTE_252 "\xfc"
#define CC_STRINGIFY_BYTE_253 "\xfd"
#define CC_STRINGIFY_BYTE_254 "\xfe"
#define CC_STRINGIFY_BYTE_255 "\xff"
#define CC_STRINGIFY_BYTE(x) CC_STRINGIFY_BYTE_##x
////
enum
{
CC_TYPE_UINT8,
CC_TYPE_INT8,
CC_TYPE_UINT16,
CC_TYPE_INT16,
CC_TYPE_UINT32,
CC_TYPE_INT32,
CC_TYPE_UINT64,
CC_TYPE_INT64,
CC_TYPE_FLOAT,
CC_TYPE_DOUBLE,
CC_TYPE_COUNT
};
extern const size_t ccTypeSize[CC_TYPE_COUNT];
////
uint32_t ccHash32Data( void *data, int size );
uint32_t ccHash32Int32( uint32_t data );
uint32_t ccHash32Int64( uint64_t data );
uint32_t ccHash32Array32( uint32_t *data, int count );
uint32_t ccHash32Array64( uint64_t *data, int count );
static inline CC_ALWAYSINLINE uint32_t ccHash32Int16Inline( uint32_t i )
{
uint32_t hash;
hash = ( i << 16 ) ^ i;
hash += hash >> 11;
hash ^= hash << 3;
hash += hash >> 5;
hash ^= hash << 4;
hash += hash >> 17;
hash ^= hash << 25;
hash += hash >> 6;
return hash;
}
static inline CC_ALWAYSINLINE uint32_t ccHash32Int32Inline( uint32_t i )
{
uint32_t hash;
hash = i & 0xFFFF;
hash = ( ( hash << 16 ) ^ hash ) ^ ( ( i & 0xFFFF0000 ) >> 5 );
hash += hash >> 11;
hash ^= hash << 3;
hash += hash >> 5;
hash ^= hash << 4;
hash += hash >> 17;
hash ^= hash << 25;
hash += hash >> 6;
return hash;
}
static inline CC_ALWAYSINLINE uint32_t ccHash32Int64Inline( uint64_t i )
{
uint32_t hash;
hash = i & 0xFFFF;
hash = ( ( hash << 16 ) ^ hash ) ^ ( ( (uint32_t)( i >> 16 ) & 0xFFFF ) << 11 );
hash += ( hash >> 11 ) + ( (uint32_t)( i >> 32 ) & 0xFFFF );
hash = ( ( hash << 16 ) ^ hash ) ^ (uint32_t)( ( i >> 37 ) & 0x7FFF800 );
hash += hash >> 11;
hash ^= hash << 3;
hash += hash >> 5;
hash ^= hash << 4;
hash += hash >> 17;
hash ^= hash << 25;
hash += hash >> 6;
return hash;
}
static inline CC_ALWAYSINLINE uint32_t ccHash32Data3Inline( uint8_t *data )
{
uint32_t hash;
hash = 0;
hash += ( (uint32_t)data[1] << 8 ) | (uint32_t)data[0];
hash ^= hash << 16;
hash ^= (uint32_t)data[2] << 18;
hash += hash >> 11;
hash ^= hash << 3;
hash += hash >> 5;
hash ^= hash << 4;
hash += hash >> 17;
hash ^= hash << 25;
hash += hash >> 6;
return hash;
}
static inline CC_ALWAYSINLINE uint32_t ccHash32Data4Inline( uint8_t *data )
{
uint32_t hash;
hash = 0;
hash += ( (uint32_t)data[1] << 8 ) | (uint32_t)data[0];
hash = ( hash << 16 ) ^ ( ( ( (uint32_t)data[3] << 19 ) | ( (uint32_t)data[2] << 11 ) ) ^ hash );
hash += hash >> 11;
hash ^= hash << 3;
hash += hash >> 5;
hash ^= hash << 4;
hash += hash >> 17;
hash ^= hash << 25;
hash += hash >> 6;
return hash;
}
static inline CC_ALWAYSINLINE uint32_t ccHash32Data5Inline( uint8_t *data )
{
uint32_t hash;
hash = 0;
hash += ( (uint32_t)data[1] << 8 ) | (uint32_t)data[0];
hash = ( hash << 16 ) ^ ( ( ( (uint32_t)data[3] << 19 ) | ( (uint32_t)data[2] << 11 ) ) ^ hash );
hash += hash >> 11;
hash += (uint32_t)data[4];
hash ^= hash << 10;
hash += hash >> 1;
hash ^= hash << 3;
hash += hash >> 5;
hash ^= hash << 4;
hash += hash >> 17;
hash ^= hash << 25;
hash += hash >> 6;
return hash;
}
static inline CC_ALWAYSINLINE uint32_t ccHash32Data6Inline( uint8_t *data )
{
uint32_t hash;
hash = 0;
hash += ( (uint32_t)data[1] << 8 ) | (uint32_t)data[0];
hash = ( hash << 16 ) ^ ( ( ( (uint32_t)data[3] << 19 ) | ( (uint32_t)data[2] << 11 ) ) ^ hash );
hash += hash >> 11;
hash += ( (uint32_t)data[5] << 8 ) | (uint32_t)data[4];
hash ^= hash << 11;
hash += hash >> 17;
hash ^= hash << 3;
hash += hash >> 5;
hash ^= hash << 4;
hash += hash >> 17;
hash ^= hash << 25;
hash += hash >> 6;
return hash;
}
static inline CC_ALWAYSINLINE uint32_t ccHash32Data7Inline( uint8_t *data )
{
uint32_t hash;
hash = 0;
hash += ( (uint32_t)data[1] << 8 ) | (uint32_t)data[0];
hash = ( hash << 16 ) ^ ( ( ( (uint32_t)data[3] << 19 ) | ( (uint32_t)data[2] << 11 ) ) ^ hash );
hash += hash >> 11;
data = ADDRESS( data, 4 );
hash += ( (uint32_t)data[5] << 8 ) | (uint32_t)data[4];
hash ^= hash << 16;
hash ^= (uint32_t)data[6] << 18;
hash += hash >> 11;
hash ^= hash << 3;
hash += hash >> 5;
hash ^= hash << 4;
hash += hash >> 17;
hash ^= hash << 25;
hash += hash >> 6;
return hash;
}
static inline CC_ALWAYSINLINE uint32_t ccHash32Data8Inline( uint8_t *data )
{
uint32_t hash;
hash = 0;
hash += ( (uint32_t)data[1] << 8 ) | (uint32_t)data[0];
hash = ( hash << 16 ) ^ ( ( ( (uint32_t)data[3] << 19 ) | ( (uint32_t)data[2] << 11 ) ) ^ hash );
hash += hash >> 11;
hash += ( (uint32_t)data[5] << 8 ) | (uint32_t)data[4];
hash = ( hash << 16 ) ^ ( ( ( (uint32_t)data[7] << 19 ) | ( (uint32_t)data[6] << 11 ) ) ^ hash );
hash += hash >> 11;
hash ^= hash << 3;
hash += hash >> 5;
hash ^= hash << 4;
hash += hash >> 17;
hash ^= hash << 25;
hash += hash >> 6;
return hash;
}
////
typedef struct
{
uint32_t a;
uint32_t b;
uint32_t c;
uint32_t d;
} ccQuickRandState32;
static inline CC_ALWAYSINLINE uint32_t ccQuickRand32( ccQuickRandState32 *randstate )
{
uint32_t e;
e = randstate->a - ( ( randstate->b << 27 ) | ( randstate->b >> (32-27) ) );
randstate->a = randstate->b ^ ( ( randstate->c << 17 ) | ( randstate->c >> (32-17) ) );
randstate->b = randstate->c + randstate->d;
randstate->c = randstate->d + e;
randstate->d = e + randstate->a;
return randstate->d;
}
static inline CC_ALWAYSINLINE void ccQuickRand32Seed( ccQuickRandState32 *randstate, uint32_t seed )
{
uint32_t i;
randstate->a = 0xf1ea5eed;
randstate->b = seed;
randstate->c = seed;
randstate->d = seed;
for( i = 0 ; i < 20 ; i++ )
ccQuickRand32( randstate );
return;
}
static inline CC_ALWAYSINLINE void ccQuickRand32SeedFast( ccQuickRandState32 *randstate, uint32_t seed0, uint32_t seed1, uint32_t seed2 )
{
uint32_t i;
randstate->a = 0xf1ea5eed;
randstate->b = seed0;
randstate->c = seed1;
randstate->d = seed2;
for( i = 0 ; i < 4 ; i++ )
ccQuickRand32( randstate );
return;
}
typedef struct
{
uint64_t a;
uint64_t b;
uint64_t c;
uint64_t d;
} ccQuickRandState64;
static inline CC_ALWAYSINLINE uint64_t ccQuickRand64( ccQuickRandState64 *randstate )
{
uint64_t e;
e = randstate->a - ( ( randstate->b << 7 ) | ( randstate->b >> (64-7) ) );
randstate->a = randstate->b ^ ( ( randstate->c << 13 ) | ( randstate->c >> (64-13) ) );
randstate->b = randstate->c + ( ( randstate->d << 37 ) | ( randstate->d >> (64-37) ) );
randstate->c = randstate->d + e;
randstate->d = e + randstate->a;
return randstate->d;
}
static inline CC_ALWAYSINLINE void ccQuickRand64Seed( ccQuickRandState64 *randstate, uint64_t seed )
{
uint64_t i;
randstate->a = 0xf1ea5eed;
randstate->b = seed;
randstate->c = seed;
randstate->d = seed;
for( i = 0 ; i < 20 ; i++ )
ccQuickRand64( randstate );
return;
}
////
int ccMemCmp( void *s0, void *s1, int size );
int ccMemCmp32( uint32_t *s0, uint32_t *s1, int count );
int ccMemCmp64( uint64_t *s0, uint64_t *s1, int count );
int ccMemCmpRetSize( void *s0, void *s1, int size );
static inline CC_ALWAYSINLINE int ccMemCmpInline( void *s0, void *s1, int size )
{
int i;
unsigned char *t0, *t1;
t0 = s0;
t1 = s1;
for( i = 0 ; i < size ; i++ )
{
if( t0[i] != t1[i] )
return 0;
}
return 1;
}
static inline CC_ALWAYSINLINE int ccMemCmpSizeInline( void *s0, void *s1, int size )
{
int i;
unsigned char *t0, *t1;
t0 = s0;
t1 = s1;
for( i = 0 ; i < size ; i++ )
{
if( t0[i] != t1[i] )
break;
}
return i;
}
////
uint8_t ccLog2Int8( uint8_t i );
uint16_t ccLog2Int16( uint16_t i );
uint32_t ccLog2Int32( uint32_t i );
uint64_t ccLog2Int64( uint64_t i );
#if CPUCONF_LONG_SIZE == 8
#define ccLog2IntL(v) ccLog2Int64(v)
#else
#define ccLog2IntL(v) ccLog2Int32(v)
#endif
////
static inline CC_ALWAYSINLINE int8_t ccPowInt8( int8_t base, int exp )
{
int result;
result = 1;
while( exp )
{
if( exp & 1 )
result *= base;
exp >>= 1;
base *= base;
}
return result;
}
static inline CC_ALWAYSINLINE int16_t ccPowInt16( int16_t base, int exp )
{
int result;
result = 1;
while( exp )
{
if( exp & 1 )
result *= base;
exp >>= 1;
base *= base;
}
return result;
}
static inline CC_ALWAYSINLINE int32_t ccPowInt32( int32_t base, int exp )
{
int result;
result = 1;
while( exp )
{
if( exp & 1 )
result *= base;
exp >>= 1;
base *= base;
}
return result;
}
static inline CC_ALWAYSINLINE int64_t ccPowInt64( int64_t base, int exp )
{
int result;
result = 1;
while( exp )
{
if( exp & 1 )
result *= base;
exp >>= 1;
base *= base;
}
return result;
}
////
static inline CC_ALWAYSINLINE uint8_t ccMergeIntMask8( uint8_t i0, uint8_t i1, uint8_t mask )
{
return i0 ^ ( ( i0 ^ i1 ) & mask );
}
static inline CC_ALWAYSINLINE uint16_t ccMergeIntMask16( uint16_t i0, uint16_t i1, uint16_t mask )
{
return i0 ^ ( ( i0 ^ i1 ) & mask );
}
static inline CC_ALWAYSINLINE uint32_t ccMergeIntMask32( uint32_t i0, uint32_t i1, uint32_t mask )
{
return i0 ^ ( ( i0 ^ i1 ) & mask );
}
static inline CC_ALWAYSINLINE uint64_t ccMergeIntMask64( uint64_t i0, uint64_t i1, uint64_t mask )
{
return i0 ^ ( ( i0 ^ i1 ) & mask );
}
#if CPUCONF_LONG_SIZE == 8
#define ccMergeIntMaskL(v) ccMergeIntMask64(v)
#else
#define ccMergeIntMaskL(v) ccMergeIntMask32(v)
#endif
////
static inline CC_ALWAYSINLINE int ccCountBits64( uint64_t i )
{
int r;
for( r = 0 ; i ; r++ )
i &= i - 1;
return r;
}
static inline CC_ALWAYSINLINE int ccCountBits32( uint32_t v )
{
int c;
v = v - ( ( v >> 1 ) & 0x55555555 );
v = ( v & 0x33333333 ) + ( ( v >> 2 ) & 0x33333333 );
c = ( ( ( v + ( v >> 4 ) ) & 0xF0F0F0F ) * 0x1010101 ) >> 24;
return c;
}
////
static inline CC_ALWAYSINLINE int ccTrailingCount32( uint32_t v )
{
int c;
if( v & 0x1 )
c = 0;
else
{
c = 1;
if( !( v & 0xffff ) )
{
v >>= 16;
c += 16;
}
if( !( v & 0xff ) )
{
v >>= 8;
c += 8;
}
if( !( v & 0xf ) )
{
v >>= 4;
c += 4;
}
if( !( v & 0x3 ) )
{
v >>= 2;
c += 2;
}
c -= v & 0x1;
}
return c;
}
static inline CC_ALWAYSINLINE int ccTrailingCount64( uint64_t v )
{
int c;
if( v & 0x1 )
c = 0;
else
{
c = 1;
if( !( v & 0xffffffff ) )
{
v >>= 32;
c += 32;
}
if( !( v & 0xffff ) )
{
v >>= 16;
c += 16;
}
if( !( v & 0xff ) )
{
v >>= 8;
c += 8;
}
if( !( v & 0xf ) )
{
v >>= 4;
c += 4;
}
if( !( v & 0x3 ) )
{
v >>= 2;
c += 2;
}
c -= v & 0x1;
}
return c;
}
////
static inline CC_ALWAYSINLINE uint32_t ccReverseBits32( uint32_t value )
{
uint32_t result;
int shift;
result = value;
shift = 32-1;
for( value >>= 1 ; value ; value >>= 1 )
{
result <<= 1;
result |= value & 1;
shift--;
}
result <<= shift;
return result;
}
static inline CC_ALWAYSINLINE uint64_t ccReverseBits64( uint64_t value )
{
uint64_t result;
int shift;
result = value;
shift = 64-1;
for( value >>= 1 ; value ; value >>= 1 )
{
result <<= 1;
result |= value & 1;
shift--;