forked from kohler/masstree-beta
-
Notifications
You must be signed in to change notification settings - Fork 0
/
compiler.hh
1073 lines (979 loc) · 31.1 KB
/
compiler.hh
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
/* Masstree
* Eddie Kohler, Yandong Mao, Robert Morris
* Copyright (c) 2012-2013 President and Fellows of Harvard College
* Copyright (c) 2012-2013 Massachusetts Institute of Technology
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, subject to the conditions
* listed in the Masstree LICENSE file. These conditions include: you must
* preserve this copyright notice, and you cannot mention the copyright
* holders in advertising related to the Software without their permission.
* The Software is provided WITHOUT ANY WARRANTY, EXPRESS OR IMPLIED. This
* notice is a summary of the Masstree LICENSE file; the license in that file
* is legally binding.
*/
#ifndef COMPILER_HH
#define COMPILER_HH 1
#include <stdint.h>
#define __STDC_FORMAT_MACROS
#include <inttypes.h>
#include <arpa/inet.h>
#if HAVE_TYPE_TRAITS
#include <type_traits>
#endif
#define arraysize(a) (sizeof(a) / sizeof((a)[0]))
#define likely(x) __builtin_expect(!!(x), 1)
#define unlikely(x) __builtin_expect(!!(x), 0)
#if !HAVE_CXX_STATIC_ASSERT
#define static_assert(x, msg) switch (x) case 0: case !!(x):
#endif
#if !HAVE_CXX_CONSTEXPR
#define constexpr const
#endif
#if HAVE_OFF_T_IS_LONG_LONG
#define PRIdOFF_T "lld"
#else
#define PRIdOFF_T "ld"
#endif
#if HAVE_SIZE_T_IS_UNSIGNED_LONG_LONG
#define PRIdSIZE_T "llu"
#define PRIdSSIZE_T "lld"
#elif HAVE_SIZE_T_IS_UNSIGNED_LONG
#define PRIdSIZE_T "lu"
#define PRIdSSIZE_T "ld"
#else
#define PRIdSIZE_T "u"
#define PRIdSSIZE_T "d"
#endif
#if (__i386__ || __x86_64__) && !defined(__x86__)
# define __x86__ 1
#endif
#define PREFER_X86 1
#define ALLOW___SYNC_BUILTINS 1
#if !defined(HAVE_INDIFFERENT_ALIGMENT) && (__i386__ || __x86_64__ || __arch_um__)
# define HAVE_INDIFFERENT_ALIGNMENT 1
#endif
/** @brief Return the index of the most significant bit set in @a x.
* @return 0 if @a x = 0; otherwise the index of first bit set, where the
* most significant bit is numbered 1.
*/
inline int ffs_msb(unsigned x) {
return (x ? __builtin_clz(x) + 1 : 0);
}
/** @overload */
inline int ffs_msb(unsigned long x) {
return (x ? __builtin_clzl(x) + 1 : 0);
}
/** @overload */
inline int ffs_msb(unsigned long long x) {
return (x ? __builtin_clzll(x) + 1 : 0);
}
/** @brief Compiler fence.
*
* Prevents reordering of loads and stores by the compiler. Not intended to
* synchronize the processor's caches. */
inline void fence() {
asm volatile("" : : : "memory");
}
/** @brief Acquire fence. */
inline void acquire_fence() {
asm volatile("" : : : "memory");
}
/** @brief Release fence. */
inline void release_fence() {
asm volatile("" : : : "memory");
}
/** @brief Compiler fence that relaxes the processor.
Use this in spinloops, for example. */
inline void relax_fence() {
asm volatile("pause" : : : "memory"); // equivalent to "rep; nop"
}
/** @brief Full memory fence. */
inline void memory_fence() {
asm volatile("mfence" : : : "memory");
}
/** @brief Do-nothing function object. */
struct do_nothing {
void operator()() {
}
template <typename T>
void operator()(T) {
}
};
/** @brief Function object that calls fence(). */
struct fence_function {
void operator()() {
fence();
}
};
/** @brief Function object that calls relax_fence(). */
struct relax_fence_function {
void operator()() {
relax_fence();
}
};
/** @brief Function object that calls relax_fence() with backoff. */
struct backoff_fence_function {
backoff_fence_function()
: count_(0) {
}
void operator()() {
for (int i = count_; i >= 0; --i)
relax_fence();
count_ = ((count_ << 1) | 1) & 15;
}
private:
int count_;
};
template <int SIZE, typename BARRIER> struct sized_compiler_operations;
template <typename B> struct sized_compiler_operations<1, B> {
typedef char type;
static inline type xchg(type* object, type new_value) {
asm volatile("xchgb %0,%1"
: "+q" (new_value), "+m" (*object));
B()();
return new_value;
}
static inline type val_cmpxchg(type* object, type expected, type desired) {
#if __x86__ && (PREFER_X86 || !HAVE___SYNC_VAL_COMPARE_AND_SWAP)
asm volatile("lock; cmpxchgb %2,%1"
: "+a" (expected), "+m" (*object)
: "r" (desired) : "cc");
B()();
return expected;
#else
return __sync_val_compare_and_swap(object, expected, desired);
#endif
}
static inline bool bool_cmpxchg(type* object, type expected, type desired) {
#if HAVE___SYNC_BOOL_COMPARE_AND_SWAP && ALLOW___SYNC_BUILTINS
return __sync_bool_compare_and_swap(object, expected, desired);
#else
bool result;
asm volatile("lock; cmpxchgb %3,%1; sete %b2"
: "+a" (expected), "+m" (*object), "=q" (result)
: "q" (desired) : "cc");
B()();
return result;
#endif
}
static inline type fetch_and_add(type *object, type addend) {
#if __x86__ && (PREFER_X86 || !HAVE___SYNC_FETCH_AND_ADD)
asm volatile("lock; xaddb %0,%1"
: "+q" (addend), "+m" (*object) : : "cc");
B()();
return addend;
#else
return __sync_fetch_and_add(object, addend);
#endif
}
static inline void atomic_or(type* object, type addend) {
#if __x86__
asm volatile("lock; orb %0,%1"
: "=r" (addend), "+m" (*object) : : "cc");
B()();
#else
__sync_fetch_and_or(object, addend);
#endif
}
};
template <typename B> struct sized_compiler_operations<2, B> {
#if SIZEOF_SHORT == 2
typedef short type;
#else
typedef int16_t type;
#endif
static inline type xchg(type* object, type new_value) {
asm volatile("xchgw %0,%1"
: "+r" (new_value), "+m" (*object));
B()();
return new_value;
}
static inline type val_cmpxchg(type* object, type expected, type desired) {
#if __x86__ && (PREFER_X86 || !HAVE___SYNC_VAL_COMPARE_AND_SWAP)
asm volatile("lock; cmpxchgw %2,%1"
: "+a" (expected), "+m" (*object)
: "r" (desired) : "cc");
B()();
return expected;
#else
return __sync_val_compare_and_swap(object, expected, desired);
#endif
}
static inline bool bool_cmpxchg(type* object, type expected, type desired) {
#if HAVE___SYNC_BOOL_COMPARE_AND_SWAP && ALLOW___SYNC_BUILTINS
return __sync_bool_compare_and_swap(object, expected, desired);
#else
bool result;
asm volatile("lock; cmpxchgw %3,%1; sete %b2"
: "+a" (expected), "+m" (*object), "=q" (result)
: "r" (desired) : "cc");
B()();
return result;
#endif
}
static inline type fetch_and_add(type* object, type addend) {
#if __x86__ && (PREFER_X86 || !HAVE___SYNC_FETCH_AND_ADD)
asm volatile("lock; xaddw %0,%1"
: "+r" (addend), "+m" (*object) : : "cc");
B()();
return addend;
#else
return __sync_fetch_and_add(object, addend);
#endif
}
static inline void atomic_or(type* object, type addend) {
#if __x86__
asm volatile("lock; orw %0,%1"
: "=r" (addend), "+m" (*object) : : "cc");
B()();
#else
__sync_fetch_and_or(object, addend);
#endif
}
};
template <typename B> struct sized_compiler_operations<4, B> {
#if SIZEOF_INT == 4
typedef int type;
#else
typedef int32_t type;
#endif
static inline type xchg(type* object, type new_value) {
asm volatile("xchgl %0,%1"
: "+r" (new_value), "+m" (*object));
B()();
return new_value;
}
static inline type val_cmpxchg(type* object, type expected, type desired) {
#if __x86__ && (PREFER_X86 || !HAVE___SYNC_VAL_COMPARE_AND_SWAP)
asm volatile("lock; cmpxchgl %2,%1"
: "+a" (expected), "+m" (*object)
: "r" (desired) : "cc");
B()();
return expected;
#else
return __sync_val_compare_and_swap(object, expected, desired);
#endif
}
static inline bool bool_cmpxchg(type* object, type expected, type desired) {
#if HAVE___SYNC_BOOL_COMPARE_AND_SWAP && ALLOW___SYNC_BUILTINS
return __sync_bool_compare_and_swap(object, expected, desired);
#else
bool result;
asm volatile("lock; cmpxchgl %3,%1; sete %b2"
: "+a" (expected), "+m" (*object), "=q" (result)
: "r" (desired) : "cc");
B()();
return result;
#endif
}
static inline type fetch_and_add(type *object, type addend) {
#if __x86__ && (PREFER_X86 || !HAVE___SYNC_FETCH_AND_ADD)
asm volatile("lock; xaddl %0,%1"
: "+r" (addend), "+m" (*object) : : "cc");
B()();
return addend;
#else
return __sync_fetch_and_add(object, addend);
#endif
}
static inline void atomic_or(type* object, type addend) {
#if __x86__
asm volatile("lock; orl %0,%1"
: "=r" (addend), "+m" (*object) : : "cc");
B()();
#else
__sync_fetch_and_or(object, addend);
#endif
}
};
template <typename B> struct sized_compiler_operations<8, B> {
#if SIZEOF_LONG_LONG == 8
typedef long long type;
#elif SIZEOF_LONG == 8
typedef long type;
#else
typedef int64_t type;
#endif
#if __x86_64__
static inline type xchg(type* object, type new_value) {
asm volatile("xchgq %0,%1"
: "+r" (new_value), "+m" (*object));
B()();
return new_value;
}
#endif
static inline type val_cmpxchg(type* object, type expected, type desired) {
#if __x86_64__ && (PREFER_X86 || !HAVE___SYNC_VAL_COMPARE_AND_SWAP_8)
asm volatile("lock; cmpxchgq %2,%1"
: "+a" (expected), "+m" (*object)
: "r" (desired) : "cc");
B()();
return expected;
#elif __i386__ && (PREFER_X86 || !HAVE___SYNC_VAL_COMPARE_AND_SWAP_8)
uint32_t expected_low(expected), expected_high(expected >> 32),
desired_low(desired), desired_high(desired >> 32);
asm volatile("lock; cmpxchg8b %2"
: "+a" (expected_low), "+d" (expected_high), "+m" (*object)
: "b" (desired_low), "c" (desired_high) : "cc");
B()();
return ((uint64_t) expected_high << 32) | expected_low;
#elif HAVE___SYNC_VAL_COMPARE_AND_SWAP_8
return __sync_val_compare_and_swap(object, expected, desired);
#endif
}
static inline bool bool_cmpxchg(type* object, type expected, type desired) {
#if HAVE___SYNC_BOOL_COMPARE_AND_SWAP_8 && ALLOW___SYNC_BUILTINS
return __sync_bool_compare_and_swap(object, expected, desired);
#elif __x86_64__
bool result;
asm volatile("lock; cmpxchgq %3,%1; sete %b2"
: "+a" (expected), "+m" (*object), "=q" (result)
: "r" (desired) : "cc");
B()();
return result;
#else
uint32_t expected_low(expected), expected_high(expected >> 32),
desired_low(desired), desired_high(desired >> 32);
bool result;
asm volatile("lock; cmpxchg8b %2; sete %b4"
: "+a" (expected_low), "+d" (expected_high),
"+m" (*object), "=q" (result)
: "b" (desired_low), "c" (desired_high) : "cc");
B()();
return result;
#endif
}
#if __x86_64__ || HAVE___SYNC_FETCH_AND_ADD_8
static inline type fetch_and_add(type* object, type addend) {
# if __x86_64__ && (PREFER_X86 || !HAVE___SYNC_FETCH_AND_ADD_8)
asm volatile("lock; xaddq %0,%1"
: "+r" (addend), "+m" (*object) : : "cc");
B()();
return addend;
# else
return __sync_fetch_and_add(object, addend);
# endif
}
#endif
#if __x86_64__ || HAVE___SYNC_FETCH_AND_OR_8
static inline void atomic_or(type* object, type addend) {
#if __x86_64__
asm volatile("lock; orq %0,%1"
: "=r" (addend), "+m" (*object) : : "cc");
B()();
#else
__sync_fetch_and_or(object, addend);
#endif
}
#endif
};
template<typename T>
inline T xchg(T* object, T new_value) {
typedef sized_compiler_operations<sizeof(T), fence_function> sco_t;
typedef typename sco_t::type type;
return (T) sco_t::xchg((type*) object, (type) new_value);
}
inline int8_t xchg(int8_t* object, int new_value) {
return xchg(object, (int8_t) new_value);
}
inline uint8_t xchg(uint8_t* object, int new_value) {
return xchg(object, (uint8_t) new_value);
}
inline int16_t xchg(int16_t* object, int new_value) {
return xchg(object, (int16_t) new_value);
}
inline uint16_t xchg(uint16_t* object, int new_value) {
return xchg(object, (uint16_t) new_value);
}
inline unsigned xchg(unsigned* object, int new_value) {
return xchg(object, (unsigned) new_value);
}
/** @brief Atomic compare and exchange. Return actual old value.
* @param object pointer to memory value
* @param expected old value
* @param desired new value
* @return actual old value
*
* Acts like an atomic version of:
* @code
* T actual(*object);
* if (actual == expected)
* *object = desired;
* return actual;
* @endcode */
template <typename T>
inline T cmpxchg(T* object, T expected, T desired) {
typedef sized_compiler_operations<sizeof(T), fence_function> sco_t;
typedef typename sco_t::type type;
return (T) sco_t::val_cmpxchg((type*) object, (type) expected, (type) desired);
}
inline unsigned cmpxchg(unsigned *object, int expected, int desired) {
return cmpxchg(object, unsigned(expected), unsigned(desired));
}
/** @brief Atomic compare and exchange. Return true iff swap succeeds.
* @param object pointer to memory value
* @param expected old value
* @param desired new value
* @return true if swap succeeded, false otherwise
*
* Acts like an atomic version of:
* @code
* T actual(*object);
* if (actual == expected) {
* *object = desired;
* return true;
* } else
* return false;
* @endcode */
template <typename T>
inline bool bool_cmpxchg(T* object, T expected, T desired) {
typedef sized_compiler_operations<sizeof(T), fence_function> sco_t;
typedef typename sco_t::type type;
return sco_t::bool_cmpxchg((type*) object, (type) expected, (type) desired);
}
inline bool bool_cmpxchg(uint8_t* object, int expected, int desired) {
return bool_cmpxchg(object, uint8_t(expected), uint8_t(desired));
}
inline bool bool_cmpxchg(unsigned *object, int expected, int desired) {
return bool_cmpxchg(object, unsigned(expected), unsigned(desired));
}
/** @brief Atomic fetch-and-add. Return the old value.
* @param object pointer to integer
* @param addend value to add
* @return old value */
template <typename T>
inline T fetch_and_add(T* object, T addend) {
typedef sized_compiler_operations<sizeof(T), fence_function> sco_t;
typedef typename sco_t::type type;
return (T) sco_t::fetch_and_add((type*) object, (type) addend);
}
template <typename T>
inline T* fetch_and_add(T** object, int addend) {
typedef sized_compiler_operations<sizeof(T*), fence_function> sco_t;
typedef typename sco_t::type type;
return (T*) sco_t::fetch_and_add((type*) object, (type) (addend * sizeof(T)));
}
inline int8_t fetch_and_add(int8_t* object, int addend) {
return fetch_and_add(object, int8_t(addend));
}
inline uint8_t fetch_and_add(uint8_t* object, int addend) {
return fetch_and_add(object, uint8_t(addend));
}
inline int16_t fetch_and_add(int16_t* object, int addend) {
return fetch_and_add(object, int16_t(addend));
}
inline uint16_t fetch_and_add(uint16_t* object, int addend) {
return fetch_and_add(object, uint16_t(addend));
}
inline unsigned fetch_and_add(unsigned* object, int addend) {
return fetch_and_add(object, unsigned(addend));
}
inline unsigned long fetch_and_add(unsigned long* object, int addend) {
return fetch_and_add(object, (unsigned long)(addend));
}
/** @brief Test-and-set lock acquire. */
template <typename T>
inline void test_and_set_acquire(T* object) {
typedef sized_compiler_operations<sizeof(T), do_nothing> sco_t;
typedef typename sco_t::type type;
while (sco_t::xchg((type*) object, (type) 1))
relax_fence();
acquire_fence();
}
/** @brief Test-and-set lock release. */
template <typename T>
inline void test_and_set_release(T* object) {
release_fence();
*object = T();
}
/** @brief Atomic fetch-and-or. Returns nothing.
* @param object pointer to integer
* @param addend value to or */
template <typename T>
inline void atomic_or(T* object, T addend) {
typedef sized_compiler_operations<sizeof(T), fence_function> sco_t;
typedef typename sco_t::type type;
sco_t::atomic_or((type*) object, (type) addend);
}
inline void atomic_or(int8_t* object, int addend) {
atomic_or(object, int8_t(addend));
}
inline void atomic_or(uint8_t* object, int addend) {
atomic_or(object, uint8_t(addend));
}
inline void atomic_or(int16_t* object, int addend) {
atomic_or(object, int16_t(addend));
}
inline void atomic_or(uint16_t* object, int addend) {
atomic_or(object, uint16_t(addend));
}
inline void atomic_or(unsigned* object, int addend) {
atomic_or(object, unsigned(addend));
}
inline void atomic_or(unsigned long* object, int addend) {
atomic_or(object, (unsigned long)(addend));
}
// prefetch instruction
inline void prefetch(const void *ptr) {
#ifdef NOPREFETCH
(void) ptr;
#else
typedef struct { char x[64]; } cacheline_t;
asm volatile("prefetcht0 %0" : : "m" (*(const cacheline_t *)ptr));
#endif
}
inline void prefetchnta(const void *ptr) {
#ifdef NOPREFETCH
(void) ptr;
#else
typedef struct { char x[64]; } cacheline_t;
asm volatile("prefetchnta %0" : : "m" (*(const cacheline_t *)ptr));
#endif
}
template <typename T>
struct value_prefetcher {
void operator()(T) {
}
};
template <typename T>
struct value_prefetcher<T *> {
void operator()(T *p) {
prefetch((const void *) p);
}
};
// stolen from Linux
inline uint64_t ntohq(uint64_t val) {
#ifdef __i386__
union {
struct {
uint32_t a;
uint32_t b;
} s;
uint64_t u;
} v;
v.u = val;
asm("bswapl %0; bswapl %1; xchgl %0,%1"
: "+r" (v.s.a), "+r" (v.s.b));
return v.u;
#else /* __i386__ */
asm("bswapq %0" : "+r" (val));
return val;
#endif
}
inline uint64_t htonq(uint64_t val) {
return ntohq(val);
}
/** Bit counting. */
/** @brief Return the number of leading 0 bits in @a x.
* @pre @a x != 0
*
* "Leading" means "most significant." */
#if HAVE___BUILTIN_CLZ
inline int clz(int x) {
return __builtin_clz(x);
}
inline int clz(unsigned x) {
return __builtin_clz(x);
}
#endif
#if HAVE___BUILTIN_CLZL
inline int clz(long x) {
return __builtin_clzl(x);
}
inline int clz(unsigned long x) {
return __builtin_clzl(x);
}
#endif
#if HAVE___BUILTIN_CLZLL
inline int clz(long long x) {
return __builtin_clzll(x);
}
inline int clz(unsigned long long x) {
return __builtin_clzll(x);
}
#endif
/** @brief Return the number of trailing 0 bits in @a x.
* @pre @a x != 0
*
* "Trailing" means "least significant." */
#if HAVE___BUILTIN_CTZ
inline int ctz(int x) {
return __builtin_ctz(x);
}
inline int ctz(unsigned x) {
return __builtin_ctz(x);
}
#endif
#if HAVE___BUILTIN_CTZL
inline int ctz(long x) {
return __builtin_ctzl(x);
}
inline int ctz(unsigned long x) {
return __builtin_ctzl(x);
}
#endif
#if HAVE___BUILTIN_CTZLL
inline int ctz(long long x) {
return __builtin_ctzll(x);
}
inline int ctz(unsigned long long x) {
return __builtin_ctzll(x);
}
#endif
template <typename T, typename U>
inline T iceil(T x, U y) {
U mod = x % y;
return x + (mod ? y - mod : 0);
}
template <typename T>
inline T iceil_log2(T x) {
return T(1) << (sizeof(T) * 8 - clz(x) - !(x & (x - 1)));
}
template <typename T>
inline T ifloor_log2(T x) {
return T(1) << (sizeof(T) * 8 - 1 - clz(x));
}
/** @brief Return the index of the lowest 0 nibble in @a x.
*
* 0 is the lowest-order nibble. Returns -1 if no nibbles are 0. */
template <typename T>
inline int find_lowest_zero_nibble(T x) {
#if SIZEOF_LONG_LONG == 16
T h = T(0x88888888888888888888888888888888ULL), l = T(0x11111111111111111111111111111111ULL);
#else
T h = T(0x8888888888888888ULL), l = T(0x1111111111111111ULL);
#endif
T t = h & (x - l) & ~x;
return t ? ctz(t) >> 2 : -1;
}
/** @brief Translate @a x to network byte order.
*
* Compare htons/htonl/htonq. host_to_net_order is particularly useful in
* template functions, where the type to be translated to network byte order
* is unknown. */
inline unsigned char host_to_net_order(unsigned char x) {
return x;
}
/** @overload */
inline signed char host_to_net_order(signed char x) {
return x;
}
/** @overload */
inline char host_to_net_order(char x) {
return x;
}
/** @overload */
inline short host_to_net_order(short x) {
return htons(x);
}
/** @overload */
inline unsigned short host_to_net_order(unsigned short x) {
return htons(x);
}
/** @overload */
inline int host_to_net_order(int x) {
return htonl(x);
}
/** @overload */
inline unsigned host_to_net_order(unsigned x) {
return htonl(x);
}
#if SIZEOF_LONG == 4
/** @overload */
inline long host_to_net_order(long x) {
return htonl(x);
}
/** @overload */
inline unsigned long host_to_net_order(unsigned long x) {
return htonl(x);
}
#elif SIZEOF_LONG == 8
/** @overload */
inline long host_to_net_order(long x) {
return htonq(x);
}
/** @overload */
inline unsigned long host_to_net_order(unsigned long x) {
return htonq(x);
}
#endif
#if SIZEOF_LONG_LONG == 8
/** @overload */
inline long long host_to_net_order(long long x) {
return htonq(x);
}
/** @overload */
inline unsigned long long host_to_net_order(unsigned long long x) {
return htonq(x);
}
#endif
#if !HAVE_INT64_T_IS_LONG && !HAVE_INT64_T_IS_LONG_LONG
/** @overload */
inline int64_t host_to_net_order(int64_t x) {
return htonq(x);
}
/** @overload */
inline uint64_t host_to_net_order(uint64_t x) {
return htonq(x);
}
#endif
/** @brief Translate @a x to host byte order.
*
* Compare ntohs/ntohl/ntohq. net_to_host_order is particularly useful in
* template functions, where the type to be translated to network byte order
* is unknown. */
inline unsigned char net_to_host_order(unsigned char x) {
return x;
}
/** @overload */
inline signed char net_to_host_order(signed char x) {
return x;
}
/** @overload */
inline char net_to_host_order(char x) {
return x;
}
/** @overload */
inline short net_to_host_order(short x) {
return ntohs(x);
}
/** @overload */
inline unsigned short net_to_host_order(unsigned short x) {
return ntohs(x);
}
/** @overload */
inline int net_to_host_order(int x) {
return ntohl(x);
}
/** @overload */
inline unsigned net_to_host_order(unsigned x) {
return ntohl(x);
}
#if SIZEOF_LONG == 4
/** @overload */
inline long net_to_host_order(long x) {
return ntohl(x);
}
/** @overload */
inline unsigned long net_to_host_order(unsigned long x) {
return ntohl(x);
}
#elif SIZEOF_LONG == 8
/** @overload */
inline long net_to_host_order(long x) {
return ntohq(x);
}
/** @overload */
inline unsigned long net_to_host_order(unsigned long x) {
return ntohq(x);
}
#endif
#if SIZEOF_LONG_LONG == 8
/** @overload */
inline long long net_to_host_order(long long x) {
return ntohq(x);
}
/** @overload */
inline unsigned long long net_to_host_order(unsigned long long x) {
return ntohq(x);
}
#endif
#if !HAVE_INT64_T_IS_LONG && !HAVE_INT64_T_IS_LONG_LONG
/** @overload */
inline int64_t net_to_host_order(int64_t x) {
return ntohq(x);
}
/** @overload */
inline uint64_t net_to_host_order(uint64_t x) {
return ntohq(x);
}
#endif
inline uint64_t read_pmc(uint32_t ecx) {
uint32_t a, d;
__asm __volatile("rdpmc" : "=a"(a), "=d"(d) : "c"(ecx));
return ((uint64_t)a) | (((uint64_t)d) << 32);
}
inline uint64_t read_tsc(void)
{
uint32_t low, high;
asm volatile("rdtsc" : "=a" (low), "=d" (high));
return ((uint64_t)low) | (((uint64_t)high) << 32);
}
template <typename T>
inline int compare(T a, T b) {
if (a == b)
return 0;
else
return a < b ? -1 : 1;
}
/** Type traits **/
namespace mass {
template <typename T> struct type_synonym {
typedef T type;
};
#if HAVE_CXX_TEMPLATE_ALIAS && HAVE_TYPE_TRAITS
template <typename T, T V>
using integral_constant = std::integral_constant<T, V>;
typedef std::true_type true_type;
typedef std::false_type false_type;
#else
template <typename T, T V>
struct integral_constant {
typedef integral_constant<T, V> type;
typedef T value_type;
static constexpr T value = V;
};
template <typename T, T V> constexpr T integral_constant<T, V>::value;
typedef integral_constant<bool, true> true_type;
typedef integral_constant<bool, false> false_type;
#endif
#if HAVE_CXX_TEMPLATE_ALIAS && HAVE_TYPE_TRAITS
template <bool B, typename T, typename F>
using conditional = std::conditional<B, T, F>;
#else
template <bool B, typename T, typename F> struct conditional {};
template <typename T, typename F> struct conditional<true, T, F> {
typedef T type;
};
template <typename T, typename F> struct conditional<false, T, F> {
typedef F type;
};
#endif
#if HAVE_CXX_TEMPLATE_ALIAS && HAVE_TYPE_TRAITS
template <typename T> using remove_const = std::remove_const<T>;
template <typename T> using remove_volatile = std::remove_volatile<T>;
template <typename T> using remove_cv = std::remove_cv<T>;
#else
template <typename T> struct remove_const : public type_synonym<T> {};
template <typename T> struct remove_const<const T> : public type_synonym<T> {};
template <typename T> struct remove_volatile : public type_synonym<T> {};
template <typename T> struct remove_volatile<volatile T> : public type_synonym<T> {};
template <typename T> struct remove_cv {
typedef typename remove_const<typename remove_volatile<T>::type>::type type;
};
#endif
#if HAVE_CXX_TEMPLATE_ALIAS && HAVE_TYPE_TRAITS
template <typename T> using is_pointer = std::is_pointer<T>;
#else
template <typename T> struct is_pointer_helper : public false_type {};
template <typename T> struct is_pointer_helper<T*> : public true_type {};
template <typename T> struct is_pointer
: public integral_constant<bool, is_pointer_helper<typename remove_cv<T>::type>::value> {};
#endif
#if HAVE_CXX_TEMPLATE_ALIAS && HAVE_TYPE_TRAITS
template <typename T> using is_reference = std::is_reference<T>;
#else
template <typename T> struct is_reference_helper : public false_type {};
template <typename T> struct is_reference_helper<T&> : public true_type {};
#if HAVE_CXX_RVALUE_REFERENCES
template <typename T> struct is_reference_helper<T&&> : public true_type {};
#endif
template <typename T> struct is_reference
: public integral_constant<bool, is_reference_helper<typename remove_cv<T>::type>::value> {};
#endif
#if HAVE_CXX_TEMPLATE_ALIAS && HAVE_TYPE_TRAITS
template <typename T> using make_unsigned = std::make_unsigned<T>;
template <typename T> using make_signed = std::make_signed<T>;
#else
template <typename T> struct make_unsigned {};
template <> struct make_unsigned<char> : public type_synonym<unsigned char> {};
template <> struct make_unsigned<signed char> : public type_synonym<unsigned char> {};
template <> struct make_unsigned<unsigned char> : public type_synonym<unsigned char> {};
template <> struct make_unsigned<short> : public type_synonym<unsigned short> {};
template <> struct make_unsigned<unsigned short> : public type_synonym<unsigned short> {};
template <> struct make_unsigned<int> : public type_synonym<unsigned> {};
template <> struct make_unsigned<unsigned> : public type_synonym<unsigned> {};
template <> struct make_unsigned<long> : public type_synonym<unsigned long> {};
template <> struct make_unsigned<unsigned long> : public type_synonym<unsigned long> {};
template <> struct make_unsigned<long long> : public type_synonym<unsigned long long> {};
template <> struct make_unsigned<unsigned long long> : public type_synonym<unsigned long long> {};
template <typename T> struct make_signed {};
template <> struct make_signed<char> : public type_synonym<signed char> {};
template <> struct make_signed<signed char> : public type_synonym<signed char> {};
template <> struct make_signed<unsigned char> : public type_synonym<signed char> {};
template <> struct make_signed<short> : public type_synonym<short> {};
template <> struct make_signed<unsigned short> : public type_synonym<short> {};
template <> struct make_signed<int> : public type_synonym<int> {};
template <> struct make_signed<unsigned> : public type_synonym<int> {};
template <> struct make_signed<long> : public type_synonym<long> {};
template <> struct make_signed<unsigned long> : public type_synonym<long> {};
template <> struct make_signed<long long> : public type_synonym<long long> {};
template <> struct make_signed<unsigned long long> : public type_synonym<long long> {};
#endif
/** @class is_trivially_copyable
@brief Template determining whether T may be copied by memcpy.
is_trivially_copyable<T> is equivalent to true_type if T has a trivial
copy constructor, false_type if it does not. */
#if HAVE_CXX_TEMPLATE_ALIAS && HAVE_TYPE_TRAITS && HAVE_STD_IS_TRIVIALLY_COPYABLE
template <typename T> using is_trivially_copyable = std::is_trivially_copyable<T>;
#elif HAVE___HAS_TRIVIAL_COPY
template <typename T> struct is_trivially_copyable : public integral_constant<bool, __has_trivial_copy(T)> {};
#else
template <typename T> struct is_trivially_copyable : public false_type {};
template <> struct is_trivially_copyable<unsigned char> : public true_type {};
template <> struct is_trivially_copyable<signed char> : public true_type {};
template <> struct is_trivially_copyable<char> : public true_type {};
template <> struct is_trivially_copyable<unsigned short> : public true_type {};