forked from ridiculousfish/libdivide
-
Notifications
You must be signed in to change notification settings - Fork 0
/
libdivide.h
2799 lines (2538 loc) · 142 KB
/
libdivide.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
/* libdivide.h
Copyright 2010 ridiculous_fish
*/
#if defined(_WIN32) || defined(WIN32)
#define LIBDIVIDE_WINDOWS 1
#endif
#if defined(_MSC_VER)
#define LIBDIVIDE_VC 1
#endif
#ifdef __cplusplus
#include <cstdlib>
#include <cstdio>
#include <cassert>
#else
#include <stdlib.h>
#include <stdio.h>
#include <assert.h>
#endif
#if ! LIBDIVIDE_HAS_STDINT_TYPES && ! LIBDIVIDE_VC
/* Visual C++ still doesn't ship with stdint.h (!) */
#include <stdint.h>
#define LIBDIVIDE_HAS_STDINT_TYPES 1
#endif
#if ! LIBDIVIDE_HAS_STDINT_TYPES
typedef __int32 int32_t;
typedef unsigned __int32 uint32_t;
typedef __int64 int64_t;
typedef unsigned __int64 uint64_t;
typedef __int8 int8_t;
typedef unsigned __int8 uint8_t;
#endif
#if LIBDIVIDE_USE_SSE2
#if LIBDIVIDE_VC
#include <mmintrin.h>
#endif
#include <emmintrin.h>
#endif
#if LIBDIVIDE_USE_NEON
#include <arm_neon.h>
#endif
#ifndef __has_builtin
#define __has_builtin(x) 0 // Compatibility with non-clang compilers.
#endif
#ifdef __ICC
#define HAS_INT128_T 0
#else
#define HAS_INT128_T __LP64__
#endif
#if defined(__x86_64__) || defined(_WIN64) || defined(_M_64)
#define LIBDIVIDE_IS_X86_64 1
#endif
#if defined(__i386__)
#define LIBDIVIDE_IS_i386 1
#endif
#if __GNUC__ || __clang__
#define LIBDIVIDE_GCC_STYLE_ASM 1
#endif
/* libdivide may use the pmuldq (vector signed 32x32->64 mult instruction) which is in SSE 4.1. However, signed multiplication can be emulated efficiently with unsigned multiplication, and SSE 4.1 is currently rare, so it is OK to not turn this on */
#ifdef LIBDIVIDE_USE_SSE4_1
#include <smmintrin.h>
#endif
#ifdef __cplusplus
/* We place libdivide within the libdivide namespace, and that goes in an anonymous namespace so that the functions are only visible to files that #include this header and don't get external linkage. At least that's the theory. */
namespace {
namespace libdivide {
#endif
/* Explanation of "more" field: bit 6 is whether to use shift path. If we are using the shift path, bit 7 is whether the divisor is negative in the signed case; in the unsigned case it is 0. Bits 0-4 is shift value (for shift path or mult path). In 32 bit case, bit 5 is always 0. We use bit 7 as the "negative divisor indicator" so that we can use sign extension to efficiently go to a full-width -1.
u32: [0-4] shift value
[5] ignored
[6] add indicator
[7] shift path
s32: [0-4] shift value
[5] shift path
[6] add indicator
[7] indicates negative divisor
u64: [0-5] shift value
[6] add indicator
[7] shift path
s64: [0-5] shift value
[6] add indicator
[7] indicates negative divisor
magic number of 0 indicates shift path (we ran out of bits!)
*/
enum {
LIBDIVIDE_32_SHIFT_MASK = 0x1F,
LIBDIVIDE_64_SHIFT_MASK = 0x3F,
LIBDIVIDE_ADD_MARKER = 0x40,
LIBDIVIDE_U32_SHIFT_PATH = 0x80,
LIBDIVIDE_U64_SHIFT_PATH = 0x80,
LIBDIVIDE_S32_SHIFT_PATH = 0x20,
LIBDIVIDE_NEGATIVE_DIVISOR = 0x80
};
struct libdivide_u32_t {
uint32_t magic;
uint8_t more;
};
struct libdivide_s32_t {
int32_t magic;
uint8_t more;
};
struct libdivide_u64_t {
uint64_t magic;
uint8_t more;
};
struct libdivide_s64_t {
int64_t magic;
uint8_t more;
};
#ifndef LIBDIVIDE_API
#ifdef __cplusplus
/* In C++, we don't want our public functions to be static, because they are arguments to templates and static functions can't do that. They get internal linkage through virtue of the anonymous namespace. In C, they should be static. */
#define LIBDIVIDE_API
#else
#define LIBDIVIDE_API static
#endif
#endif
LIBDIVIDE_API struct libdivide_s32_t libdivide_s32_gen(int32_t y);
LIBDIVIDE_API struct libdivide_u32_t libdivide_u32_gen(uint32_t y);
LIBDIVIDE_API struct libdivide_s64_t libdivide_s64_gen(int64_t y);
LIBDIVIDE_API struct libdivide_u64_t libdivide_u64_gen(uint64_t y);
LIBDIVIDE_API int32_t libdivide_s32_do(int32_t numer, const struct libdivide_s32_t *denom);
LIBDIVIDE_API uint32_t libdivide_u32_do(uint32_t numer, const struct libdivide_u32_t *denom);
LIBDIVIDE_API int64_t libdivide_s64_do(int64_t numer, const struct libdivide_s64_t *denom);
LIBDIVIDE_API uint64_t libdivide_u64_do(uint64_t y, const struct libdivide_u64_t *denom);
LIBDIVIDE_API int libdivide_u32_get_algorithm(const struct libdivide_u32_t *denom);
LIBDIVIDE_API uint32_t libdivide_u32_do_alg0(uint32_t numer, const struct libdivide_u32_t *denom);
LIBDIVIDE_API uint32_t libdivide_u32_do_alg1(uint32_t numer, const struct libdivide_u32_t *denom);
LIBDIVIDE_API uint32_t libdivide_u32_do_alg2(uint32_t numer, const struct libdivide_u32_t *denom);
LIBDIVIDE_API int libdivide_u64_get_algorithm(const struct libdivide_u64_t *denom);
LIBDIVIDE_API uint64_t libdivide_u64_do_alg0(uint64_t numer, const struct libdivide_u64_t *denom);
LIBDIVIDE_API uint64_t libdivide_u64_do_alg1(uint64_t numer, const struct libdivide_u64_t *denom);
LIBDIVIDE_API uint64_t libdivide_u64_do_alg2(uint64_t numer, const struct libdivide_u64_t *denom);
LIBDIVIDE_API int libdivide_s32_get_algorithm(const struct libdivide_s32_t *denom);
LIBDIVIDE_API int32_t libdivide_s32_do_alg0(int32_t numer, const struct libdivide_s32_t *denom);
LIBDIVIDE_API int32_t libdivide_s32_do_alg1(int32_t numer, const struct libdivide_s32_t *denom);
LIBDIVIDE_API int32_t libdivide_s32_do_alg2(int32_t numer, const struct libdivide_s32_t *denom);
LIBDIVIDE_API int32_t libdivide_s32_do_alg3(int32_t numer, const struct libdivide_s32_t *denom);
LIBDIVIDE_API int32_t libdivide_s32_do_alg4(int32_t numer, const struct libdivide_s32_t *denom);
LIBDIVIDE_API int libdivide_s64_get_algorithm(const struct libdivide_s64_t *denom);
LIBDIVIDE_API int64_t libdivide_s64_do_alg0(int64_t numer, const struct libdivide_s64_t *denom);
LIBDIVIDE_API int64_t libdivide_s64_do_alg1(int64_t numer, const struct libdivide_s64_t *denom);
LIBDIVIDE_API int64_t libdivide_s64_do_alg2(int64_t numer, const struct libdivide_s64_t *denom);
LIBDIVIDE_API int64_t libdivide_s64_do_alg3(int64_t numer, const struct libdivide_s64_t *denom);
LIBDIVIDE_API int64_t libdivide_s64_do_alg4(int64_t numer, const struct libdivide_s64_t *denom);
#if LIBDIVIDE_USE_SSE2
#define LIBDIVIDE_VEC128 1
typedef __m128i libdivide_4s32_t;
typedef __m128i libdivide_2s64_t;
typedef __m128i libdivide_4u32_t;
typedef __m128i libdivide_2u64_t;
#elif LIBDIVIDE_USE_NEON
#define LIBDIVIDE_VEC64 1
#define LIBDIVIDE_VEC128 1
#define LIBDIVIDE_VEC256 1
typedef int32x2_t libdivide_2s32_t;
typedef int32x4_t libdivide_4s32_t;
typedef int32x4x2_t libdivide_8s32_t;
typedef int64x1_t libdivide_1s64_t;
typedef int64x2_t libdivide_2s64_t;
typedef int64x2x2_t libdivide_4s64_t;
typedef uint32x2_t libdivide_2u32_t;
typedef uint32x4_t libdivide_4u32_t;
typedef uint32x4x2_t libdivide_8u32_t;
typedef uint64x1_t libdivide_1u64_t;
typedef uint64x2_t libdivide_2u64_t;
typedef uint64x2x2_t libdivide_4u64_t;
#elif LIBDIVIDE_USE_VECTOR
#define LIBDIVIDE_VEC64 1
#define LIBDIVIDE_VEC128 1
#define LIBDIVIDE_VEC256 1
typedef int32_t libdivide_2s32_t __attribute__((__vector_size__(8)));
typedef int32_t libdivide_4s32_t __attribute__((__vector_size__(16)));
typedef int32_t libdivide_8s32_t __attribute__((__vector_size__(32)));
typedef int64_t libdivide_1s64_t __attribute__((__vector_size__(8)));
typedef int64_t libdivide_2s64_t __attribute__((__vector_size__(16)));
typedef int64_t libdivide_4s64_t __attribute__((__vector_size__(32)));
typedef int64_t libdivide_8s64_t __attribute__((__vector_size__(64)));
typedef uint32_t libdivide_2u32_t __attribute__((__vector_size__(8)));
typedef uint32_t libdivide_4u32_t __attribute__((__vector_size__(16)));
typedef uint32_t libdivide_8u32_t __attribute__((__vector_size__(32)));
typedef uint64_t libdivide_1u64_t __attribute__((__vector_size__(8)));
typedef uint64_t libdivide_2u64_t __attribute__((__vector_size__(16)));
typedef uint64_t libdivide_4u64_t __attribute__((__vector_size__(32)));
typedef uint64_t libdivide_8u64_t __attribute__((__vector_size__(64)));
#if HAS_INT128_T
typedef __int128_t libdivide_1s128_t __attribute__((__vector_size__(16)));
typedef __int128_t libdivide_2s128_t __attribute__((__vector_size__(32)));
typedef __int128_t libdivide_4s128_t __attribute__((__vector_size__(64)));
typedef __int128_t libdivide_8s128_t __attribute__((__vector_size__(128)));
typedef __uint128_t libdivide_1u128_t __attribute__((__vector_size__(16)));
typedef __uint128_t libdivide_2u128_t __attribute__((__vector_size__(32)));
typedef __uint128_t libdivide_4u128_t __attribute__((__vector_size__(64)));
typedef __uint128_t libdivide_8u128_t __attribute__((__vector_size__(128)));
#endif
#endif
#if LIBDIVIDE_VEC64
LIBDIVIDE_API libdivide_2s32_t libdivide_2s32_do_vector(libdivide_2s32_t numers, const struct libdivide_s32_t * denom);
LIBDIVIDE_API libdivide_1s64_t libdivide_1s64_do_vector(libdivide_1s64_t numers, const struct libdivide_s64_t * denom);
LIBDIVIDE_API libdivide_2u32_t libdivide_2u32_do_vector(libdivide_2u32_t numers, const struct libdivide_u32_t * denom);
LIBDIVIDE_API libdivide_1u64_t libdivide_1u64_do_vector(libdivide_1u64_t numers, const struct libdivide_u64_t * denom);
LIBDIVIDE_API libdivide_2u32_t libdivide_2u32_do_vector_alg0(libdivide_2u32_t numers, const struct libdivide_u32_t * denom);
LIBDIVIDE_API libdivide_2u32_t libdivide_2u32_do_vector_alg1(libdivide_2u32_t numers, const struct libdivide_u32_t * denom);
LIBDIVIDE_API libdivide_2u32_t libdivide_2u32_do_vector_alg2(libdivide_2u32_t numers, const struct libdivide_u32_t * denom);
LIBDIVIDE_API libdivide_2s32_t libdivide_2s32_do_vector_alg0(libdivide_2s32_t numers, const struct libdivide_s32_t * denom);
LIBDIVIDE_API libdivide_2s32_t libdivide_2s32_do_vector_alg1(libdivide_2s32_t numers, const struct libdivide_s32_t * denom);
LIBDIVIDE_API libdivide_2s32_t libdivide_2s32_do_vector_alg2(libdivide_2s32_t numers, const struct libdivide_s32_t * denom);
LIBDIVIDE_API libdivide_2s32_t libdivide_2s32_do_vector_alg3(libdivide_2s32_t numers, const struct libdivide_s32_t * denom);
LIBDIVIDE_API libdivide_2s32_t libdivide_2s32_do_vector_alg4(libdivide_2s32_t numers, const struct libdivide_s32_t * denom);
LIBDIVIDE_API libdivide_1u64_t libdivide_1u64_do_vector_alg0(libdivide_1u64_t numers, const struct libdivide_u64_t * denom);
LIBDIVIDE_API libdivide_1u64_t libdivide_1u64_do_vector_alg1(libdivide_1u64_t numers, const struct libdivide_u64_t * denom);
LIBDIVIDE_API libdivide_1u64_t libdivide_1u64_do_vector_alg2(libdivide_1u64_t numers, const struct libdivide_u64_t * denom);
LIBDIVIDE_API libdivide_1s64_t libdivide_1s64_do_vector_alg0(libdivide_1s64_t numers, const struct libdivide_s64_t * denom);
LIBDIVIDE_API libdivide_1s64_t libdivide_1s64_do_vector_alg1(libdivide_1s64_t numers, const struct libdivide_s64_t * denom);
LIBDIVIDE_API libdivide_1s64_t libdivide_1s64_do_vector_alg2(libdivide_1s64_t numers, const struct libdivide_s64_t * denom);
LIBDIVIDE_API libdivide_1s64_t libdivide_1s64_do_vector_alg3(libdivide_1s64_t numers, const struct libdivide_s64_t * denom);
LIBDIVIDE_API libdivide_1s64_t libdivide_1s64_do_vector_alg4(libdivide_1s64_t numers, const struct libdivide_s64_t * denom);
#endif
#if LIBDIVIDE_VEC128
LIBDIVIDE_API libdivide_4s32_t libdivide_4s32_do_vector(libdivide_4s32_t numers, const struct libdivide_s32_t * denom);
LIBDIVIDE_API libdivide_2s64_t libdivide_2s64_do_vector(libdivide_2s64_t numers, const struct libdivide_s64_t * denom);
LIBDIVIDE_API libdivide_4u32_t libdivide_4u32_do_vector(libdivide_4u32_t numers, const struct libdivide_u32_t * denom);
LIBDIVIDE_API libdivide_2u64_t libdivide_2u64_do_vector(libdivide_2u64_t numers, const struct libdivide_u64_t * denom);
LIBDIVIDE_API libdivide_4u32_t libdivide_4u32_do_vector_alg0(libdivide_4u32_t numers, const struct libdivide_u32_t * denom);
LIBDIVIDE_API libdivide_4u32_t libdivide_4u32_do_vector_alg1(libdivide_4u32_t numers, const struct libdivide_u32_t * denom);
LIBDIVIDE_API libdivide_4u32_t libdivide_4u32_do_vector_alg2(libdivide_4u32_t numers, const struct libdivide_u32_t * denom);
LIBDIVIDE_API libdivide_4s32_t libdivide_4s32_do_vector_alg0(libdivide_4s32_t numers, const struct libdivide_s32_t * denom);
LIBDIVIDE_API libdivide_4s32_t libdivide_4s32_do_vector_alg1(libdivide_4s32_t numers, const struct libdivide_s32_t * denom);
LIBDIVIDE_API libdivide_4s32_t libdivide_4s32_do_vector_alg2(libdivide_4s32_t numers, const struct libdivide_s32_t * denom);
LIBDIVIDE_API libdivide_4s32_t libdivide_4s32_do_vector_alg3(libdivide_4s32_t numers, const struct libdivide_s32_t * denom);
LIBDIVIDE_API libdivide_4s32_t libdivide_4s32_do_vector_alg4(libdivide_4s32_t numers, const struct libdivide_s32_t * denom);
LIBDIVIDE_API libdivide_2u64_t libdivide_2u64_do_vector_alg0(libdivide_2u64_t numers, const struct libdivide_u64_t * denom);
LIBDIVIDE_API libdivide_2u64_t libdivide_2u64_do_vector_alg1(libdivide_2u64_t numers, const struct libdivide_u64_t * denom);
LIBDIVIDE_API libdivide_2u64_t libdivide_2u64_do_vector_alg2(libdivide_2u64_t numers, const struct libdivide_u64_t * denom);
LIBDIVIDE_API libdivide_2s64_t libdivide_2s64_do_vector_alg0(libdivide_2s64_t numers, const struct libdivide_s64_t * denom);
LIBDIVIDE_API libdivide_2s64_t libdivide_2s64_do_vector_alg1(libdivide_2s64_t numers, const struct libdivide_s64_t * denom);
LIBDIVIDE_API libdivide_2s64_t libdivide_2s64_do_vector_alg2(libdivide_2s64_t numers, const struct libdivide_s64_t * denom);
LIBDIVIDE_API libdivide_2s64_t libdivide_2s64_do_vector_alg3(libdivide_2s64_t numers, const struct libdivide_s64_t * denom);
LIBDIVIDE_API libdivide_2s64_t libdivide_2s64_do_vector_alg4(libdivide_2s64_t numers, const struct libdivide_s64_t * denom);
#endif
#if LIBDIVIDE_VEC256
LIBDIVIDE_API libdivide_8s32_t libdivide_8s32_do_vector(libdivide_8s32_t numers, const struct libdivide_s32_t * denom);
LIBDIVIDE_API libdivide_4s64_t libdivide_4s64_do_vector(libdivide_4s64_t numers, const struct libdivide_s64_t * denom);
LIBDIVIDE_API libdivide_8u32_t libdivide_8u32_do_vector(libdivide_8u32_t numers, const struct libdivide_u32_t * denom);
LIBDIVIDE_API libdivide_4u64_t libdivide_4u64_do_vector(libdivide_4u64_t numers, const struct libdivide_u64_t * denom);
LIBDIVIDE_API libdivide_8u32_t libdivide_8u32_do_vector_alg0(libdivide_8u32_t numers, const struct libdivide_u32_t * denom);
LIBDIVIDE_API libdivide_8u32_t libdivide_8u32_do_vector_alg1(libdivide_8u32_t numers, const struct libdivide_u32_t * denom);
LIBDIVIDE_API libdivide_8u32_t libdivide_8u32_do_vector_alg2(libdivide_8u32_t numers, const struct libdivide_u32_t * denom);
LIBDIVIDE_API libdivide_8s32_t libdivide_8s32_do_vector_alg0(libdivide_8s32_t numers, const struct libdivide_s32_t * denom);
LIBDIVIDE_API libdivide_8s32_t libdivide_8s32_do_vector_alg1(libdivide_8s32_t numers, const struct libdivide_s32_t * denom);
LIBDIVIDE_API libdivide_8s32_t libdivide_8s32_do_vector_alg2(libdivide_8s32_t numers, const struct libdivide_s32_t * denom);
LIBDIVIDE_API libdivide_8s32_t libdivide_8s32_do_vector_alg3(libdivide_8s32_t numers, const struct libdivide_s32_t * denom);
LIBDIVIDE_API libdivide_8s32_t libdivide_8s32_do_vector_alg4(libdivide_8s32_t numers, const struct libdivide_s32_t * denom);
LIBDIVIDE_API libdivide_4u64_t libdivide_4u64_do_vector_alg0(libdivide_4u64_t numers, const struct libdivide_u64_t * denom);
LIBDIVIDE_API libdivide_4u64_t libdivide_4u64_do_vector_alg1(libdivide_4u64_t numers, const struct libdivide_u64_t * denom);
LIBDIVIDE_API libdivide_4u64_t libdivide_4u64_do_vector_alg2(libdivide_4u64_t numers, const struct libdivide_u64_t * denom);
LIBDIVIDE_API libdivide_4s64_t libdivide_4s64_do_vector_alg0(libdivide_4s64_t numers, const struct libdivide_s64_t * denom);
LIBDIVIDE_API libdivide_4s64_t libdivide_4s64_do_vector_alg1(libdivide_4s64_t numers, const struct libdivide_s64_t * denom);
LIBDIVIDE_API libdivide_4s64_t libdivide_4s64_do_vector_alg2(libdivide_4s64_t numers, const struct libdivide_s64_t * denom);
LIBDIVIDE_API libdivide_4s64_t libdivide_4s64_do_vector_alg3(libdivide_4s64_t numers, const struct libdivide_s64_t * denom);
LIBDIVIDE_API libdivide_4s64_t libdivide_4s64_do_vector_alg4(libdivide_4s64_t numers, const struct libdivide_s64_t * denom);
#endif
#define libdivide_s32_do_vector libdivide_4s32_do_vector
#define libdivide_s64_do_vector libdivide_2s64_do_vector
#define libdivide_u32_do_vector libdivide_4u32_do_vector
#define libdivide_u64_do_vector libdivide_2u64_do_vector
#define libdivide_s32_do_vector_alg0 libdivide_4s32_do_vector_alg0
#define libdivide_s32_do_vector_alg1 libdivide_4s32_do_vector_alg1
#define libdivide_s32_do_vector_alg2 libdivide_4s32_do_vector_alg2
#define libdivide_s32_do_vector_alg3 libdivide_4s32_do_vector_alg3
#define libdivide_s32_do_vector_alg4 libdivide_4s32_do_vector_alg4
#define libdivide_s64_do_vector_alg0 libdivide_2s64_do_vector_alg0
#define libdivide_s64_do_vector_alg1 libdivide_2s64_do_vector_alg1
#define libdivide_s64_do_vector_alg2 libdivide_2s64_do_vector_alg2
#define libdivide_s64_do_vector_alg3 libdivide_2s64_do_vector_alg3
#define libdivide_s64_do_vector_alg4 libdivide_2s64_do_vector_alg4
#define libdivide_u32_do_vector_alg0 libdivide_4u32_do_vector_alg0
#define libdivide_u32_do_vector_alg1 libdivide_4u32_do_vector_alg1
#define libdivide_u32_do_vector_alg2 libdivide_4u32_do_vector_alg2
#define libdivide_u64_do_vector_alg0 libdivide_2u64_do_vector_alg0
#define libdivide_u64_do_vector_alg1 libdivide_2u64_do_vector_alg1
#define libdivide_u64_do_vector_alg2 libdivide_2u64_do_vector_alg2
//////// Internal Utility Functions
static inline uint32_t libdivide__mullhi_u32(uint32_t x, uint32_t y) {
uint64_t xl = x, yl = y;
uint64_t rl = xl * yl;
return (uint32_t)(rl >> 32);
}
static inline int32_t libdivide__mullhi_s32(int32_t x, int32_t y) {
int64_t xl = x, yl = y;
int64_t rl = xl * yl;
return (int32_t)(rl >> 32); //needs to be arithmetic shift
}
static uint64_t libdivide__mullhi_u64(uint64_t x, uint64_t y) {
#if HAS_INT128_T
__uint128_t xl = x, yl = y;
__uint128_t rl = xl * yl;
return (uint64_t)(rl >> 64);
#else
//full 128 bits are x0 * y0 + (x0 * y1 << 32) + (x1 * y0 << 32) + (x1 * y1 << 64)
const uint32_t mask = 0xFFFFFFFF;
const uint32_t x0 = (uint32_t)(x & mask), x1 = (uint32_t)(x >> 32);
const uint32_t y0 = (uint32_t)(y & mask), y1 = (uint32_t)(y >> 32);
const uint32_t x0y0_hi = libdivide__mullhi_u32(x0, y0);
const uint64_t x0y1 = x0 * (uint64_t)y1;
const uint64_t x1y0 = x1 * (uint64_t)y0;
const uint64_t x1y1 = x1 * (uint64_t)y1;
uint64_t temp = x1y0 + x0y0_hi;
uint64_t temp_lo = temp & mask, temp_hi = temp >> 32;
return x1y1 + temp_hi + ((temp_lo + x0y1) >> 32);
#endif
}
static inline int64_t libdivide__mullhi_s64(int64_t x, int64_t y) {
#if HAS_INT128_T
__int128_t xl = x, yl = y;
__int128_t rl = xl * yl;
return (int64_t)(rl >> 64);
#else
//full 128 bits are x0 * y0 + (x0 * y1 << 32) + (x1 * y0 << 32) + (x1 * y1 << 64)
const uint32_t mask = 0xFFFFFFFF;
const uint32_t x0 = (uint32_t)(x & mask), y0 = (uint32_t)(y & mask);
const int32_t x1 = (int32_t)(x >> 32), y1 = (int32_t)(y >> 32);
const uint32_t x0y0_hi = libdivide__mullhi_u32(x0, y0);
const int64_t t = x1*(int64_t)y0 + x0y0_hi;
const int64_t w1 = x0*(int64_t)y1 + (t & mask);
return x1*(int64_t)y1 + (t >> 32) + (w1 >> 32);
#endif
}
#if LIBDIVIDE_USE_SSE2
static inline __m128i libdivide__u64_to_m128(uint64_t x) {
#if LIBDIVIDE_VC
//64 bit windows doesn't seem to have an implementation of any of these load intrinsics, and 32 bit Visual C++ crashes
_declspec(align(16)) uint64_t temp[2] = {x, x};
return _mm_load_si128((const __m128i*)temp);
#elif defined(__ICC)
uint64_t __attribute__((aligned(16))) temp[2] = {x,x};
return _mm_load_si128((const __m128i*)temp);
#elif __clang__
// clang does not provide this intrinsic either
return (__m128i){(int64_t)x, (int64_t)x};
#else
// everyone else gets it right
return _mm_set1_epi64x(x);
#endif
}
static inline __m128i libdivide_get_FFFFFFFF00000000(void) {
//returns the same as _mm_set1_epi64(0xFFFFFFFF00000000ULL) without touching memory
__m128i result = _mm_set1_epi8(-1); //optimizes to pcmpeqd on OS X
return _mm_slli_epi64(result, 32);
}
static inline __m128i libdivide_get_00000000FFFFFFFF(void) {
//returns the same as _mm_set1_epi64(0x00000000FFFFFFFFULL) without touching memory
__m128i result = _mm_set1_epi8(-1); //optimizes to pcmpeqd on OS X
result = _mm_srli_epi64(result, 32);
return result;
}
static inline __m128i libdivide_get_0000FFFF(void) {
//returns the same as _mm_set1_epi32(0x0000FFFFULL) without touching memory
__m128i result = _mm_setzero_si128();
result = _mm_cmpeq_epi8(result, result); //all 1s
result = _mm_srli_epi32(result, 16);
return result;
}
static inline __m128i libdivide_s64_signbits(__m128i v) {
//we want to compute v >> 63, that is, _mm_srai_epi64(v, 63). But there is no 64 bit shift right arithmetic instruction in SSE2. So we have to fake it by first duplicating the high 32 bit values, and then using a 32 bit shift. Another option would be to use _mm_srli_epi64(v, 63) and then subtract that from 0, but that approach appears to be substantially slower for unknown reasons
__m128i hiBitsDuped = _mm_shuffle_epi32(v, _MM_SHUFFLE(3, 3, 1, 1));
__m128i signBits = _mm_srai_epi32(hiBitsDuped, 31);
return signBits;
}
/* Returns an __m128i whose low 32 bits are equal to amt and has zero elsewhere. */
static inline __m128i libdivide_u32_to_m128i(uint32_t amt) {
return _mm_set_epi32(0, 0, 0, amt);
}
static inline __m128i libdivide_s64_shift_right_vector(__m128i v, int amt) {
//implementation of _mm_sra_epi64. Here we have two 64 bit values which are shifted right to logically become (64 - amt) values, and are then sign extended from a (64 - amt) bit number.
const int b = 64 - amt;
__m128i m = libdivide__u64_to_m128(1ULL << (b - 1));
__m128i x = _mm_srl_epi64(v, libdivide_u32_to_m128i(amt));
__m128i result = _mm_sub_epi64(_mm_xor_si128(x, m), m); //result = x^m - m
return result;
}
/* Here, b is assumed to contain one 32 bit value repeated four times. If it did not, the function would not work. */
static inline __m128i libdivide__mullhi_u32_flat_vector(__m128i a, __m128i b) {
__m128i hi_product_0Z2Z = _mm_srli_epi64(_mm_mul_epu32(a, b), 32);
__m128i a1X3X = _mm_srli_epi64(a, 32);
__m128i hi_product_Z1Z3 = _mm_and_si128(_mm_mul_epu32(a1X3X, b), libdivide_get_FFFFFFFF00000000());
return _mm_or_si128(hi_product_0Z2Z, hi_product_Z1Z3); // = hi_product_0123
}
/* Here, y is assumed to contain one 64 bit value repeated twice. */
static inline __m128i libdivide_mullhi_u64_flat_vector(__m128i x, __m128i y) {
//full 128 bits are x0 * y0 + (x0 * y1 << 32) + (x1 * y0 << 32) + (x1 * y1 << 64)
const __m128i mask = libdivide_get_00000000FFFFFFFF();
const __m128i x0 = _mm_and_si128(x, mask), x1 = _mm_srli_epi64(x, 32); //x0 is low half of 2 64 bit values, x1 is high half in low slots
const __m128i y0 = _mm_and_si128(y, mask), y1 = _mm_srli_epi64(y, 32);
const __m128i x0y0_hi = _mm_srli_epi64(_mm_mul_epu32(x0, y0), 32); //x0 happens to have the low half of the two 64 bit values in 32 bit slots 0 and 2, so _mm_mul_epu32 computes their full product, and then we shift right by 32 to get just the high values
const __m128i x0y1 = _mm_mul_epu32(x0, y1);
const __m128i x1y0 = _mm_mul_epu32(x1, y0);
const __m128i x1y1 = _mm_mul_epu32(x1, y1);
const __m128i temp = _mm_add_epi64(x1y0, x0y0_hi);
__m128i temp_lo = _mm_and_si128(temp, mask), temp_hi = _mm_srli_epi64(temp, 32);
temp_lo = _mm_srli_epi64(_mm_add_epi64(temp_lo, x0y1), 32);
temp_hi = _mm_add_epi64(x1y1, temp_hi);
return _mm_add_epi64(temp_lo, temp_hi);
}
/* y is one 64 bit value repeated twice */
static inline __m128i libdivide_mullhi_s64_flat_vector(__m128i x, __m128i y) {
__m128i p = libdivide_mullhi_u64_flat_vector(x, y);
__m128i t1 = _mm_and_si128(libdivide_s64_signbits(x), y);
p = _mm_sub_epi64(p, t1);
__m128i t2 = _mm_and_si128(libdivide_s64_signbits(y), x);
p = _mm_sub_epi64(p, t2);
return p;
}
#ifdef LIBDIVIDE_USE_SSE4_1
/* b is one 32 bit value repeated four times. */
static inline __m128i libdivide_mullhi_s32_flat_vector(__m128i a, __m128i b) {
__m128i hi_product_0Z2Z = _mm_srli_epi64(_mm_mul_epi32(a, b), 32);
__m128i a1X3X = _mm_srli_epi64(a, 32);
__m128i hi_product_Z1Z3 = _mm_and_si128(_mm_mul_epi32(a1X3X, b), libdivide_get_FFFFFFFF00000000());
return _mm_or_si128(hi_product_0Z2Z, hi_product_Z1Z3); // = hi_product_0123
}
#else
/* SSE2 does not have a signed multiplication instruction, but we can convert unsigned to signed pretty efficiently. Again, b is just a 32 bit value repeated four times. */
static inline __m128i libdivide_mullhi_s32_flat_vector(__m128i a, __m128i b) {
__m128i p = libdivide__mullhi_u32_flat_vector(a, b);
__m128i t1 = _mm_and_si128(_mm_srai_epi32(a, 31), b); //t1 = (a >> 31) & y, arithmetic shift
__m128i t2 = _mm_and_si128(_mm_srai_epi32(b, 31), a);
p = _mm_sub_epi32(p, t1);
p = _mm_sub_epi32(p, t2);
return p;
}
#endif
#elif LIBDIVIDE_USE_NEON
static inline int32x2_t libdivide_mullhi_2s32_flat_vector(int32x2_t x, int32x2_t y) {
int64x2_t r64 = vmull_s32( x, y );
r64 = vreinterpretq_s64_u64( vshrq_n_u64( vreinterpretq_u64_s64(r64), 32 ) );
int32x2_t r = vmovn_s64( r64 );
return r;
}
static inline int32x4_t libdivide_mullhi_4s32_flat_vector(int32x4_t x, int32x4_t y) {
int64x2_t rlo = vmull_s32( vget_low_s32(x), vget_low_s32(y) );
int64x2_t rhi = vmull_s32( vget_high_s32(x), vget_high_s32(y) );
rlo = vreinterpretq_s64_u64( vshrq_n_u64( vreinterpretq_u64_s64(rlo), 32 ) );
rhi = vreinterpretq_s64_u64( vshrq_n_u64( vreinterpretq_u64_s64(rhi), 32 ) );
int32x4_t r = vcombine_s32( vmovn_s64( rlo ), vmovn_s64( rhi ) );
return r;
}
static inline int32x4x2_t libdivide_mullhi_8s32_flat_vector(int32x4x2_t x, int32x4x2_t y) {
int32x4x2_t r;
r.val[0] = libdivide_mullhi_4s32_flat_vector( x.val[0], y.val[0] );
r.val[1] = libdivide_mullhi_4s32_flat_vector( x.val[1], y.val[1] );
return r;
}
static inline uint32x2_t libdivide_mullhi_2u32_flat_vector(uint32x2_t x, uint32x2_t y) {
uint64x2_t r64 = vmull_u32( x, y );
r64 = vshrq_n_u64( r64, 32 );
uint32x2_t r = vmovn_u64( r64 );
return r;
}
static inline uint32x4_t libdivide_mullhi_4u32_flat_vector(uint32x4_t x, uint32x4_t y) {
uint64x2_t rlo = vmull_u32( vget_low_u32(x), vget_low_u32(y) );
uint64x2_t rhi = vmull_u32( vget_high_u32(x), vget_high_u32(y) );
rlo = vshrq_n_u64( rlo, 32 );
rhi = vshrq_n_u64( rhi, 32 );
uint32x4_t r = vcombine_u32( vmovn_u64( rlo ), vmovn_u64( rhi ) );
return r;
}
static inline uint32x4x2_t libdivide_mullhi_8u32_flat_vector(uint32x4x2_t x, uint32x4x2_t y) {
uint32x4x2_t r;
r.val[0] = libdivide_mullhi_4u32_flat_vector( x.val[0], y.val[0] );
r.val[1] = libdivide_mullhi_4u32_flat_vector( x.val[1], y.val[1] );
return r;
}
static inline int64x1_t libdivide_mullhi_1s64_flat_vector(int64x1_t x, int64x1_t y) {
int64x1_t r = vdup_n_s64(0);
r = vset_lane_s64( libdivide__mullhi_s64( vget_lane_s64(x,0), vget_lane_s64(y,0) ), r, 0 );
return r;
}
static inline int64x2_t libdivide_mullhi_2s64_flat_vector(int64x2_t x, int64x2_t y) {
#if 1
int64x2_t r = vdupq_n_s64(0);
r = vsetq_lane_s64( libdivide__mullhi_s64( vgetq_lane_s64(x,0), vgetq_lane_s64(y,0) ), r, 0 );
r = vsetq_lane_s64( libdivide__mullhi_s64( vgetq_lane_s64(x,1), vgetq_lane_s64(y,1) ), r, 1 );
return r;
#else
int32x2_t x0 = vmovn_s64(x);
int32x2_t y0 = vmovn_s64(y);
int32x2_t x1 = vmovn_s64( vshrq_n_s64( x, 32 ) );
int32x2_t y1 = vmovn_s64( vshrq_n_s64( y, 32 ) );
int64x2_t x0y0_hi = vreinterpretq_s64_u64( vshrq_n_u64( vmull_u32( vreinterpret_u32_s32(x0), vreinterpret_u32_s32(y0) ), 32 ) );
int64x2_t t = vmlal_s32( x0y0_hi, x1, y0 );
int64x2_t w1 = vmlal_s32( vmovl_s32( vmovn_s64(t) ), x0, y1 );
return vmlal_s32( vaddq_s64( vshrq_n_s64( t, 32 ), vshrq_n_s64( w1, 32 ) ), x1, y1 );
#endif
}
static inline int64x2x2_t libdivide_mullhi_4s64_flat_vector(int64x2x2_t x, int64x2x2_t y) {
int64x2x2_t r;
r.val[0] = libdivide_mullhi_2s64_flat_vector( x.val[0], y.val[0] );
r.val[1] = libdivide_mullhi_2s64_flat_vector( x.val[1], y.val[1] );
return r;
}
static inline uint64x1_t libdivide_mullhi_1u64_flat_vector(uint64x1_t x, uint64x1_t y) {
uint64x2_t xy = vcombine_u64( x, y );
uint32x2_t x0y0 = vmovn_u64( xy );
uint32x2_t x1y1 = vmovn_u64( vshrq_n_u64( xy, 32 ) );
uint32x2_t y0x0 = vrev64_u32( x0y0 );
uint32x2_t y1x1 = vrev64_u32( x1y1 );
uint64x2_t x0y0_hi = vshrq_n_u64( vmull_u32( x0y0, y0x0 ), 32 );
uint64x2_t temp = vmlal_u32( x0y0_hi, x1y1, y0x0 );
uint64x2_t temp_lo = vshrq_n_u64( vshlq_n_u64( temp, 32 ), 32 );
uint64x2_t temp_hi = vshrq_n_u64( temp, 32 );
return vadd_u64( vget_low_u64( vmlal_u32( temp_hi, x1y1, y1x1 ) ), vshr_n_u64( vget_low_u64( vmlal_u32( temp_lo, x0y0, y1x1 ) ), 32 ) );
}
static inline uint64x2_t libdivide_mullhi_2u64_flat_vector(uint64x2_t x, uint64x2_t y) {
uint32x2_t x0 = vmovn_u64(x);
uint32x2_t y0 = vmovn_u64(y);
uint32x2_t x1 = vmovn_u64( vshrq_n_u64( x, 32 ) );
uint32x2_t y1 = vmovn_u64( vshrq_n_u64( y, 32 ) );
uint64x2_t x0y0_hi = vshrq_n_u64( vmull_u32( x0, y0 ), 32 );
uint64x2_t temp = vmlal_u32( x0y0_hi, x1, y0 );
uint64x2_t temp_lo = vshrq_n_u64( vshlq_n_u64( temp, 32 ), 32 );
uint64x2_t temp_hi = vshrq_n_u64( temp, 32 );
return vaddq_u64( vmlal_u32( temp_hi, x1, y1 ), vshrq_n_u64( vmlal_u32( temp_lo, x0, y1 ), 32 ) );
}
static inline uint64x2x2_t libdivide_mullhi_4u64_flat_vector(uint64x2x2_t x, uint64x2x2_t y) {
uint64x2x2_t r;
r.val[0] = libdivide_mullhi_2u64_flat_vector( x.val[0], y.val[0] );
r.val[1] = libdivide_mullhi_2u64_flat_vector( x.val[1], y.val[1] );
return r;
}
#elif LIBDIVIDE_USE_VECTOR
static inline libdivide_2s32_t libdivide_mullhi_2s32_flat_vector(libdivide_2s32_t x, libdivide_2s32_t y) {
#if 0
return (libdivide_2s32_t) {
libdivide__mullhi_s32( x[0], y[0] ),
libdivide__mullhi_s32( x[1], y[1] ) };
#else
libdivide_2s64_t xl = (libdivide_2s64_t) { x[0], x[1] };
libdivide_2s64_t yl = (libdivide_2s64_t) { y[0], y[1] };
libdivide_2s64_t rl = (xl * yl) >> (libdivide_2s64_t) { 32, 32 };
return (libdivide_2s32_t) { (int32_t)(rl[0]), (int32_t)(rl[1]) };
#endif
}
static inline libdivide_4s32_t libdivide_mullhi_4s32_flat_vector(libdivide_4s32_t x, libdivide_4s32_t y) {
#if 0
return (libdivide_4s32_t) {
libdivide__mullhi_s32( x[0], y[0] ),
libdivide__mullhi_s32( x[1], y[1] ),
libdivide__mullhi_s32( x[2], y[2] ),
libdivide__mullhi_s32( x[3], y[3] ) };
#else
libdivide_4s64_t xl = (libdivide_4s64_t) { x[0], x[1], x[2], x[3] };
libdivide_4s64_t yl = (libdivide_4s64_t) { y[0], y[1], y[2], y[3] };
libdivide_4s64_t rl = (xl * yl) >> (libdivide_4s64_t) { 32, 32, 32, 32 };
return (libdivide_4s32_t) { (int32_t)(rl[0]), (int32_t)(rl[1]), (int32_t)(rl[2]), (int32_t)(rl[3]) };
#endif
}
static inline libdivide_8s32_t libdivide_mullhi_8s32_flat_vector(libdivide_8s32_t x, libdivide_8s32_t y) {
#if 0
return (libdivide_8s32_t) {
libdivide__mullhi_s32( x[0], y[0] ),
libdivide__mullhi_s32( x[1], y[1] ),
libdivide__mullhi_s32( x[2], y[2] ),
libdivide__mullhi_s32( x[3], y[3] ),
libdivide__mullhi_s32( x[4], y[4] ),
libdivide__mullhi_s32( x[5], y[5] ),
libdivide__mullhi_s32( x[6], y[6] ),
libdivide__mullhi_s32( x[7], y[7] ) };
#else
libdivide_8s64_t xl = (libdivide_8s64_t) { x[0], x[1], x[2], x[3], x[4], x[5], x[6], x[7] };
libdivide_8s64_t yl = (libdivide_8s64_t) { y[0], y[1], y[2], y[3], y[4], y[5], y[6], y[7] };
libdivide_8s64_t rl = (xl * yl) >> (libdivide_8s64_t) { 32, 32, 32, 32, 32, 32, 32, 32 };
return (libdivide_8s32_t) { (int32_t)(rl[0]), (int32_t)(rl[1]), (int32_t)(rl[2]), (int32_t)(rl[3]), (int32_t)(rl[4]), (int32_t)(rl[5]), (int32_t)(rl[6]), (int32_t)(rl[7]) };
#endif
}
static inline libdivide_2u32_t libdivide_mullhi_2u32_flat_vector(libdivide_2u32_t x, libdivide_2u32_t y) {
#if 0
return (libdivide_2u32_t) {
libdivide__mullhi_u32( x[0], y[0] ),
libdivide__mullhi_u32( x[1], y[1] ) };
#else
libdivide_2u64_t xl = (libdivide_2u64_t) { x[0], x[1] };
libdivide_2u64_t yl = (libdivide_2u64_t) { y[0], y[1] };
libdivide_2u64_t rl = (xl * yl) >> (libdivide_2u64_t) { 32, 32 };
return (libdivide_2u32_t) { (uint32_t)(rl[0]), (uint32_t)(rl[1]) };
#endif
}
static inline libdivide_4u32_t libdivide_mullhi_4u32_flat_vector(libdivide_4u32_t x, libdivide_4u32_t y) {
#if 0
return (libdivide_4u32_t) {
libdivide__mullhi_u32( x[0], y[0] ),
libdivide__mullhi_u32( x[1], y[1] ),
libdivide__mullhi_u32( x[2], y[2] ),
libdivide__mullhi_u32( x[3], y[3] ) };
#else
libdivide_4u64_t xl = (libdivide_4u64_t) { x[0], x[1], x[2], x[3] };
libdivide_4u64_t yl = (libdivide_4u64_t) { y[0], y[1], y[2], y[3] };
libdivide_4u64_t rl = (xl * yl) >> (libdivide_4u64_t) { 32, 32, 32, 32 };
return (libdivide_4u32_t) { (uint32_t)(rl[0]), (uint32_t)(rl[1]), (uint32_t)(rl[2]), (uint32_t)(rl[3]) };
#endif
}
static inline libdivide_8u32_t libdivide_mullhi_8u32_flat_vector(libdivide_8u32_t x, libdivide_8u32_t y) {
#if 0
return (libdivide_8u32_t) {
libdivide__mullhi_u32( x[0], y[0] ),
libdivide__mullhi_u32( x[1], y[1] ),
libdivide__mullhi_u32( x[2], y[2] ),
libdivide__mullhi_u32( x[3], y[3] ),
libdivide__mullhi_u32( x[4], y[4] ),
libdivide__mullhi_u32( x[5], y[5] ),
libdivide__mullhi_u32( x[6], y[6] ),
libdivide__mullhi_u32( x[7], y[7] ) };
#else
libdivide_8u64_t xl = (libdivide_8u64_t) { x[0], x[1], x[2], x[3], x[4], x[5], x[6], x[7] };
libdivide_8u64_t yl = (libdivide_8u64_t) { y[0], y[1], y[2], y[3], y[4], y[5], y[6], y[7] };
libdivide_8u64_t rl = (xl * yl) >> (libdivide_8u64_t) { 32, 32, 32, 32, 32, 32, 32, 32 };
return (libdivide_8u32_t) { (uint32_t)(rl[0]), (uint32_t)(rl[1]), (uint32_t)(rl[2]), (uint32_t)(rl[3]), (uint32_t)(rl[4]), (uint32_t)(rl[5]), (uint32_t)(rl[6]), (uint32_t)(rl[7]) };
#endif
}
static inline libdivide_1s64_t libdivide_mullhi_1s64_flat_vector(libdivide_1s64_t x, libdivide_1s64_t y) {
#if HAS_INT128_T
libdivide_1s128_t xl = (libdivide_1s128_t) { x[0] };
libdivide_1s128_t yl = (libdivide_1s128_t) { y[0] };
libdivide_1s128_t rl = (xl * yl) >> (libdivide_1u128_t) { 64 };
return (libdivide_1s64_t) { (int64_t)(rl[0]) };
#else
return (libdivide_1s64_t) {
libdivide__mullhi_s64( x[0], y[0] ) };
#endif
}
static inline libdivide_2s64_t libdivide_mullhi_2s64_flat_vector(libdivide_2s64_t x, libdivide_2s64_t y) {
#if HAS_INT128_T
libdivide_2s128_t xl = (libdivide_2s128_t) { x[0], x[1] };
libdivide_2s128_t yl = (libdivide_2s128_t) { y[0], y[1] };
libdivide_2s128_t rl = (xl * yl) >> (libdivide_2u128_t) { 64, 64 };
return (libdivide_2s64_t) { (int64_t)(rl[0]), (int64_t)(rl[1]) };
#else
return (libdivide_2s64_t) {
libdivide__mullhi_s64( x[0], y[0] ),
libdivide__mullhi_s64( x[1], y[1] ) };
#endif
}
static inline libdivide_4s64_t libdivide_mullhi_4s64_flat_vector(libdivide_4s64_t x, libdivide_4s64_t y) {
#if HAS_INT128_T
libdivide_4s128_t xl = (libdivide_4s128_t) { x[0], x[1], x[1], x[2] };
libdivide_4s128_t yl = (libdivide_4s128_t) { y[0], y[1], y[1], y[2] };
libdivide_4s128_t rl = (xl * yl) >> (libdivide_4u128_t) { 64, 64, 64, 64 };
return (libdivide_4s64_t) { (int64_t)(rl[0]), (int64_t)(rl[1]), (int64_t)(rl[2]), (int64_t)(rl[3]) };
#else
return (libdivide_4s64_t) {
libdivide__mullhi_s64( x[0], y[0] ),
libdivide__mullhi_s64( x[1], y[1] ),
libdivide__mullhi_s64( x[2], y[2] ),
libdivide__mullhi_s64( x[3], y[3] ) };
#endif
}
static inline libdivide_1u64_t libdivide_mullhi_1u64_flat_vector(libdivide_1u64_t x, libdivide_1u64_t y) {
#if HAS_INT128_T
libdivide_1u128_t xl = (libdivide_1u128_t) { x[0] };
libdivide_1u128_t yl = (libdivide_1u128_t) { y[0] };
libdivide_1u128_t rl = (xl * yl) >> (libdivide_1u128_t) { 64 };
return (libdivide_1u64_t) { (uint64_t)(rl[0]) };
#else
return (libdivide_1u64_t) {
libdivide__mullhi_u64( x[0], y[0] ) };
#endif
}
static inline libdivide_2u64_t libdivide_mullhi_2u64_flat_vector(libdivide_2u64_t x, libdivide_2u64_t y) {
#if HAS_INT128_T
libdivide_2u128_t xl = (libdivide_2u128_t) { x[0], x[1] };
libdivide_2u128_t yl = (libdivide_2u128_t) { y[0], y[1] };
libdivide_2u128_t rl = (xl * yl) >> (libdivide_2u128_t) { 64, 64 };
return (libdivide_2u64_t) { (uint64_t)(rl[0]), (uint64_t)(rl[1]) };
#else
return (libdivide_2u64_t) {
libdivide__mullhi_u64( x[0], y[0] ),
libdivide__mullhi_u64( x[1], y[1] ) };
#endif
}
static inline libdivide_4u64_t libdivide_mullhi_4u64_flat_vector(libdivide_4u64_t x, libdivide_4u64_t y) {
#if HAS_INT128_T
libdivide_4u128_t xl = (libdivide_4u128_t) { x[0], x[1], x[1], x[2] };
libdivide_4u128_t yl = (libdivide_4u128_t) { y[0], y[1], y[1], y[2] };
libdivide_4u128_t rl = (xl * yl) >> (libdivide_4u128_t) { 64, 64, 64, 64 };
return (libdivide_4u64_t) { (uint64_t)(rl[0]), (uint64_t)(rl[1]), (uint64_t)(rl[2]), (uint64_t)(rl[3]) };
#else
return (libdivide_4u64_t) {
libdivide__mullhi_u64( x[0], y[0] ),
libdivide__mullhi_u64( x[1], y[1] ),
libdivide__mullhi_u64( x[2], y[2] ),
libdivide__mullhi_u64( x[3], y[3] ) };
#endif
}
#endif
static inline int32_t libdivide__count_trailing_zeros32(uint32_t val) {
#if __GNUC__ || __has_builtin(__builtin_ctz)
/* Fast way to count trailing zeros */
return __builtin_ctz(val);
#else
/* Dorky way to count trailing zeros. Note that this hangs for val = 0! */
int32_t result = 0;
val = (val ^ (val - 1)) >> 1; // Set v's trailing 0s to 1s and zero rest
while (val) {
val >>= 1;
result++;
}
return result;
#endif
}
static inline int32_t libdivide__count_trailing_zeros64(uint64_t val) {
#if __LP64__ && (__GNUC__ || __has_builtin(__builtin_ctzll))
/* Fast way to count trailing zeros. Note that we disable this in 32 bit because gcc does something horrible - it calls through to a dynamically bound function. */
return __builtin_ctzll(val);
#else
/* Pretty good way to count trailing zeros. Note that this hangs for val = 0! */
uint32_t lo = (uint32_t)(val & 0xFFFFFFFF);
if (lo != 0) return libdivide__count_trailing_zeros32(lo);
return 32 + libdivide__count_trailing_zeros32((uint32_t)(val >> 32));
#endif
}
static inline int32_t libdivide__count_leading_zeros32(uint32_t val) {
#if __GNUC__ || __has_builtin(__builtin_clz)
/* Fast way to count leading zeros */
return __builtin_clz(val);
#else
/* Dorky way to count leading zeros. Note that this hangs for val = 0! */
int32_t result = 0;
while (! (val & (1U << 31))) {
val <<= 1;
result++;
}
return result;
#endif
}
static inline int32_t libdivide__count_leading_zeros64(uint64_t val) {
#if __GNUC__ || __has_builtin(__builtin_clzll)
/* Fast way to count leading zeros */
return __builtin_clzll(val);
#else
/* Dorky way to count leading zeros. Note that this hangs for val = 0! */
int32_t result = 0;
while (! (val & (1ULL << 63))) {
val <<= 1;
result++;
}
return result;
#endif
}
//libdivide_64_div_32_to_32: divides a 64 bit uint {u1, u0} by a 32 bit uint {v}. The result must fit in 32 bits. Returns the quotient directly and the remainder in *r
#if (LIBDIVIDE_IS_i386 || LIBDIVIDE_IS_X86_64) && LIBDIVIDE_GCC_STYLE_ASM
static uint32_t libdivide_64_div_32_to_32(uint32_t u1, uint32_t u0, uint32_t v, uint32_t *r) {
uint32_t result;
__asm__("divl %[v]"
: "=a"(result), "=d"(*r)
: [v] "r"(v), "a"(u0), "d"(u1)
);
return result;
}
#else
static uint32_t libdivide_64_div_32_to_32(uint32_t u1, uint32_t u0, uint32_t v, uint32_t *r) {
uint64_t n = (((uint64_t)u1) << 32) | u0;
uint32_t result = (uint32_t)(n / v);
*r = (uint32_t)(n - result * (uint64_t)v);
return result;
}
#endif
#if LIBDIVIDE_IS_X86_64 && LIBDIVIDE_GCC_STYLE_ASM
static uint64_t libdivide_128_div_64_to_64(uint64_t u1, uint64_t u0, uint64_t v, uint64_t *r) {
//u0 -> rax
//u1 -> rdx
//divq
uint64_t result;
__asm__("divq %[v]"
: "=a"(result), "=d"(*r)
: [v] "r"(v), "a"(u0), "d"(u1)
);
return result;
}
#else
/* Code taken from Hacker's Delight, http://www.hackersdelight.org/HDcode/divlu.c . License permits inclusion here per http://www.hackersdelight.org/permissions.htm
*/
static uint64_t libdivide_128_div_64_to_64(uint64_t u1, uint64_t u0, uint64_t v, uint64_t *r) {
const uint64_t b = (1ULL << 32); // Number base (16 bits).
uint64_t un1, un0, // Norm. dividend LSD's.
vn1, vn0, // Norm. divisor digits.
q1, q0, // Quotient digits.
un64, un21, un10,// Dividend digit pairs.
rhat; // A remainder.
int s; // Shift amount for norm.
if (u1 >= v) { // If overflow, set rem.
if (r != NULL) // to an impossible value,
*r = (uint64_t)(-1); // and return the largest
return (uint64_t)(-1);} // possible quotient.
/* count leading zeros */
s = libdivide__count_leading_zeros64(v); // 0 <= s <= 63.
v = v << s; // Normalize divisor.
vn1 = v >> 32; // Break divisor up into
vn0 = v & 0xFFFFFFFF; // two 32-bit digits.
un64 = (u1 << s) | ((u0 >> (64 - s)) & (-s >> 31));
un10 = u0 << s; // Shift dividend left.
un1 = un10 >> 32; // Break right half of
un0 = un10 & 0xFFFFFFFF; // dividend into two digits.
q1 = un64/vn1; // Compute the first
rhat = un64 - q1*vn1; // quotient digit, q1.
again1:
if (q1 >= b || q1*vn0 > b*rhat + un1) {
q1 = q1 - 1;
rhat = rhat + vn1;
if (rhat < b) goto again1;}
un21 = un64*b + un1 - q1*v; // Multiply and subtract.
q0 = un21/vn1; // Compute the second
rhat = un21 - q0*vn1; // quotient digit, q0.
again2:
if (q0 >= b || q0*vn0 > b*rhat + un0) {
q0 = q0 - 1;
rhat = rhat + vn1;
if (rhat < b) goto again2;}
if (r != NULL) // If remainder is wanted,
*r = (un21*b + un0 - q0*v) >> s; // return it.
return q1*b + q0;
}
#endif
#if LIBDIVIDE_ASSERTIONS_ON
#define LIBDIVIDE_ASSERT(x) do { if (! (x)) { fprintf(stderr, "Assertion failure on line %ld: %s\n", (long)__LINE__, #x); exit(-1); } } while (0)
#else
#define LIBDIVIDE_ASSERT(x)
#endif
#ifndef LIBDIVIDE_HEADER_ONLY
////////// UINT32
struct libdivide_u32_t libdivide_u32_gen(uint32_t d) {
struct libdivide_u32_t result;
if ((d & (d - 1)) == 0) {
result.magic = 0;
result.more = libdivide__count_trailing_zeros32(d) | LIBDIVIDE_U32_SHIFT_PATH;
}
else {
const uint32_t floor_log_2_d = 31 - libdivide__count_leading_zeros32(d);
uint8_t more;
uint32_t rem, proposed_m;
proposed_m = libdivide_64_div_32_to_32(1U << floor_log_2_d, 0, d, &rem);
LIBDIVIDE_ASSERT(rem > 0 && rem < d);
const uint32_t e = d - rem;
/* This power works if e < 2**floor_log_2_d. */
if (e < (1U << floor_log_2_d)) {
/* This power works */
more = floor_log_2_d;
}
else {
/* We have to use the general 33-bit algorithm. We need to compute (2**power) / d. However, we already have (2**(power-1))/d and its remainder. By doubling both, and then correcting the remainder, we can compute the larger division. */
proposed_m += proposed_m; //don't care about overflow here - in fact, we expect it
const uint32_t twice_rem = rem + rem;
if (twice_rem >= d || twice_rem < rem) proposed_m += 1;
more = floor_log_2_d | LIBDIVIDE_ADD_MARKER;
}
result.magic = 1 + proposed_m;
result.more = more;
//result.more's shift should in general be ceil_log_2_d. But if we used the smaller power, we subtract one from the shift because we're using the smaller power. If we're using the larger power, we subtract one from the shift because it's taken care of by the add indicator. So floor_log_2_d happens to be correct in both cases.
}
return result;
}
uint32_t libdivide_u32_do(uint32_t numer, const struct libdivide_u32_t *denom) {
uint8_t more = denom->more;
if (more & LIBDIVIDE_U32_SHIFT_PATH) {
return numer >> (more & LIBDIVIDE_32_SHIFT_MASK);
}
else {
uint32_t q = libdivide__mullhi_u32(denom->magic, numer);
if (more & LIBDIVIDE_ADD_MARKER) {
uint32_t t = ((numer - q) >> 1) + q;
return t >> (more & LIBDIVIDE_32_SHIFT_MASK);
}
else {
return q >> more; //all upper bits are 0 - don't need to mask them off
}
}
}
int libdivide_u32_get_algorithm(const struct libdivide_u32_t *denom) {
uint8_t more = denom->more;
if (more & LIBDIVIDE_U32_SHIFT_PATH) return 0;