-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBits.h
More file actions
1084 lines (883 loc) · 34 KB
/
Bits.h
File metadata and controls
1084 lines (883 loc) · 34 KB
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
#ifndef EMATTSAN_BITS_H
#define EMATTSAN_BITS_H
//----------------------------------------------------------------------
#include <limits>
#include <cstring>
//----------------------------------------------------------------------
namespace emattsan
{
//----------------------------------------------------------------------
namespace bits
{
//----------------------------------------------------------------------
struct Signed; // only declaration; used template parameter for expressing number signed
struct Unsigned; // only declaration; used template parameter for expressing number unsigned
template<int SIZE, typename T> class Bits;
//----------------------------------------------------------------------
namespace detail
{
//----------------------------------------------------------------------
template<int N> struct Reserved; // only declaration; used expressing reserved bits size
//----------------------------------------------------------------------
template<int SIZE>
struct MultiByte
{
typedef unsigned char block_type;
typedef MultiByte signed_value_type;
typedef MultiByte unsigned_value_type;
typedef MultiByte value_type;
typedef Unsigned sign_type;
typedef const MultiByte& arg_type;
typedef const MultiByte& const_arg_type;
typedef MultiByte& ref_arg_type;
typedef MultiByte& result_type;
typedef const MultiByte& const_result_type;
typedef block_type mask_type;
static const int BlockSize = std::numeric_limits<block_type>::digits;
static const int Size = SIZE;
static const int Length = (Size + BlockSize - 1) / BlockSize;
static const unsigned int Capacity = Length * BlockSize;
MultiByte()
{
std::memset(value_, 0, Length);
}
MultiByte(const MultiByte& other)
{
std::memcpy(value_, other.value_, Length);
}
template<int M>
MultiByte(const MultiByte<M>& other)
{
if(Length < M)
{
std::memcpy(value_, (other.value_ + M - Length), Length);
}
else
{
std::memset(value_, 0, Length - M);
std::memcpy((value_ + Length - M), other.value_, M);
}
}
int bitAt(int pos) const
{
return ((0 <= pos) && (pos < Size)) ? (value_[Length - (pos / BlockSize) - 1] >> (pos % BlockSize)) : 0;
}
block_type blockAt(int pos) const
{
return ((0 <= pos) && (pos < Length)) ? value_[pos] : 0;
}
block_type value_[Length];
};
//----------------------------------------------------------------------
template<typename T>
struct TraitsPrimitive;
template<>
struct TraitsPrimitive<signed char>
{
typedef signed char signed_value_type;
typedef unsigned char unsigned_value_type;
typedef signed char value_type;
typedef Signed sign_type;
};
template<>
struct TraitsPrimitive<unsigned char>
{
typedef signed char signed_value_type;
typedef unsigned char unsigned_value_type;
typedef unsigned char value_type;
typedef Unsigned sign_type;
};
template<>
struct TraitsPrimitive<signed short>
{
typedef signed short signed_value_type;
typedef unsigned short unsigned_value_type;
typedef signed short value_type;
typedef Signed sign_type;
};
template<>
struct TraitsPrimitive<unsigned short>
{
typedef signed short signed_value_type;
typedef unsigned short unsigned_value_type;
typedef unsigned short value_type;
typedef Unsigned sign_type;
};
template<>
struct TraitsPrimitive<signed int>
{
typedef signed int signed_value_type;
typedef unsigned int unsigned_value_type;
typedef signed int value_type;
typedef Signed sign_type;
};
template<>
struct TraitsPrimitive<unsigned int>
{
typedef signed int signed_value_type;
typedef unsigned int unsigned_value_type;
typedef unsigned int value_type;
typedef Unsigned sign_type;
};
template<>
struct TraitsPrimitive<signed long>
{
typedef signed long signed_value_type;
typedef unsigned long unsigned_value_type;
typedef signed long value_type;
typedef Signed sign_type;
};
template<>
struct TraitsPrimitive<unsigned long>
{
typedef signed long signed_value_type;
typedef unsigned long unsigned_value_type;
typedef unsigned long value_type;
typedef Unsigned sign_type;
};
template<typename T>
struct Traits
{
typedef typename TraitsPrimitive<T>::signed_value_type signed_value_type;
typedef typename TraitsPrimitive<T>::unsigned_value_type unsigned_value_type;
typedef typename TraitsPrimitive<T>::value_type value_type;
typedef typename TraitsPrimitive<T>::sign_type sign_type;
typedef value_type arg_type;
typedef const value_type const_arg_type;
typedef value_type& ref_arg_type;
typedef value_type result_type;
typedef const value_type const_result_type;
typedef unsigned_value_type mask_type;
static const unsigned int Capacity = std::numeric_limits<unsigned_value_type>::digits;
};
template<int N>
struct Traits<MultiByte<N> >
{
typedef MultiByte<N> multibyte;
typedef typename multibyte::signed_value_type signed_value_type;
typedef typename multibyte::unsigned_value_type unsigned_value_type;
typedef typename multibyte::value_type value_type;
typedef typename multibyte::sign_type sign_type;
typedef typename multibyte::arg_type arg_type;
typedef typename multibyte::const_arg_type const_arg_type;
typedef typename multibyte::ref_arg_type ref_arg_type;
typedef typename multibyte::result_type result_type;
typedef typename multibyte::const_result_type const_result_type;
typedef unsigned char mask_type;
static const unsigned int Capacity = multibyte::Capacity;
};
//----------------------------------------------------------------------
template<int N, typename S = Unsigned>
struct Fit
{
template<typename SIGN, bool LE_CHAR_SIZE, bool LE_SHORT_SIZE, bool LE_INT_SIZE, bool LE_LONG_SIZE>
struct _
{
typedef Traits<MultiByte<N> > traits;
};
template<typename SIGN, bool LE_SHORT_SIZE, bool LE_INT_SIZE, bool LE_LONG_SIZE>
struct _<SIGN, true, LE_SHORT_SIZE, LE_INT_SIZE, LE_LONG_SIZE>
{
typedef Traits<unsigned char> traits;
};
template<typename SIGN, bool LE_INT_SIZE, bool LE_LONG_SIZE>
struct _<SIGN, false, true, LE_INT_SIZE, LE_LONG_SIZE>
{
typedef Traits<unsigned short> traits;
};
template<typename SIGN, bool LE_LONG_SIZE>
struct _<SIGN, false, false, true, LE_LONG_SIZE>
{
typedef Traits<unsigned int> traits;
};
template<bool LE_SHORT_SIZE, bool LE_INT_SIZE, bool LE_LONG_SIZE>
struct _<Signed, true, LE_SHORT_SIZE, LE_INT_SIZE, LE_LONG_SIZE>
{
typedef Traits<signed char> traits;
};
template<bool LE_INT_SIZE, bool LE_LONG_SIZE>
struct _<Signed, false, true, LE_INT_SIZE, LE_LONG_SIZE>
{
typedef Traits<signed short> traits;
};
template<bool LE_LONG_SIZE>
struct _<Signed, false, false, true, LE_LONG_SIZE>
{
typedef Traits<signed int> traits;
};
typedef typename _< S,
(N <= Traits<unsigned char >::Capacity),
(N <= Traits<unsigned short>::Capacity),
(N <= Traits<unsigned int >::Capacity),
(N <= Traits<unsigned long >::Capacity)
>::traits traits;
typedef typename traits::signed_value_type signed_value_type;
typedef typename traits::unsigned_value_type unsigned_value_type;
typedef typename traits::value_type value_type;
typedef typename traits::sign_type sign_type;
typedef typename traits::arg_type arg_type;
typedef typename traits::const_arg_type const_arg_type;
typedef typename traits::ref_arg_type ref_arg_type;
typedef typename traits::result_type result_type;
typedef typename traits::const_result_type const_result_type;
};
template<typename T> struct TraitsType;
template<> struct TraitsType<char> { typedef Traits<char> traits; };
template<> struct TraitsType<signed char> { typedef Traits<signed char> traits; };
template<> struct TraitsType<unsigned char> { typedef Traits<unsigned char> traits; };
template<> struct TraitsType<signed short> { typedef Traits<signed short> traits; };
template<> struct TraitsType<unsigned short> { typedef Traits<unsigned short> traits; };
template<> struct TraitsType<signed int> { typedef Traits<signed int> traits; };
template<> struct TraitsType<unsigned int> { typedef Traits<unsigned int> traits; };
template<> struct TraitsType<signed long> { typedef Traits<signed long> traits; };
template<> struct TraitsType<unsigned long> { typedef Traits<unsigned long> traits; };
template<int N> struct TraitsType<MultiByte<N> > { typedef Traits<MultiByte<N> > traits; };
template<typename T, int N>
struct Mask
{
// only declaration; for error message when invalid template parameter is used
struct ERROR__INVALID_Bits_SIZE__ONLY_CAN_USE_FROM_ONE_TO_CONTAINER_DIGIT_SIZE;
template<int M, bool GREATER_THAN_ZERO, bool LESS_THAN_M, bool EQUAL_M>
struct _
{
static const T value = ERROR__INVALID_Bits_SIZE__ONLY_CAN_USE_FROM_ONE_TO_CONTAINER_DIGIT_SIZE::value;
};
template<int M>
struct _<M, true, true, false>
{
static const T value = static_cast<T>((1u << M) - 1);
};
template<int M>
struct _<M, true, false, true>
{
static const T value = static_cast<T>(-1);
};
template<int M>
struct _<M, true, false, false>
{
static const T value = static_cast<T>((1u << (M % std::numeric_limits<unsigned char>::digits)) - 1);;
};
static const T value = _< N,
0 < N,
N < Traits<T>::Capacity,
N == Traits<T>::Capacity
>::value;
static const T msb = value ^ (value >> 1);
};
template<int N>
struct Mask<Unsigned, N>
{
static const unsigned char value = N % std::numeric_limits<unsigned char>::digits;
};
template<typename T, int SIZE, typename S>
struct Trimmer
{
static const typename T::mask_type mask = Mask<typename T::mask_type, SIZE>::value;
static const typename T::mask_type msb = mask ^ (mask >> 1);
static void trim(typename T::ref_arg_type n)
{
n &= mask;
if((n & msb) != 0) // if MSB is high then ...
{
n |= ~mask; // ... padding the left side with 1
}
}
};
template<typename T, int SIZE>
struct Trimmer<T, SIZE, Unsigned>
{
static const typename T::mask_type mask = Mask<typename T::mask_type, SIZE>::value;
static void trim(typename T::ref_arg_type n)
{
n &= mask;
}
};
template<int N, int SIZE>
struct Trimmer<Traits<MultiByte<N> >, SIZE, Unsigned>
{
typedef MultiByte<N> multibyte;
static const typename multibyte::mask_type mask = Mask<typename multibyte::mask_type, SIZE>::value;
static typename multibyte::const_result_type trim(typename multibyte::ref_arg_type n)
{
n.value_[0] &= mask;
return n;
}
};
template<int SIZE, typename T = typename Fit<SIZE>::value_type>
struct Container
{
static const int Size = SIZE;
template<int S, typename U> struct _ { typedef U value_type; };
template<int S> struct _<S, Signed> { typedef typename Fit<Size, Signed>::value_type value_type; };
template<int S> struct _<S, Unsigned> { typedef typename Fit<Size, Unsigned>::value_type value_type; };
typedef typename TraitsType<typename _<SIZE, T>::value_type>::traits traits;
typedef typename traits::signed_value_type signed_value_type;
typedef typename traits::unsigned_value_type unsigned_value_type;
typedef typename traits::value_type value_type;
typedef typename traits::sign_type sign_type;
typedef typename traits::arg_type arg_type;
typedef typename traits::const_arg_type const_arg_type;
typedef typename traits::ref_arg_type ref_arg_type;
typedef typename traits::result_type result_type;
typedef typename traits::const_result_type const_result_type;
typedef typename traits::mask_type mask_type;
static const int Capacity = traits::Capacity;
static void trim(ref_arg_type n)
{
Trimmer<traits, Size, sign_type>::trim(n);
}
static const_result_type getSequence(const_arg_type value)
{
return static_cast<unsigned_value_type>(value) & Mask<mask_type, Size>::value;
};
};
//----------------------------------------------------------------------
template<int SIZE, typename T>
class BitsBase
{
public:
typedef Container<SIZE, T> container;
typedef typename container::signed_value_type signed_value_type;
typedef typename container::unsigned_value_type unsigned_value_type;
typedef typename container::value_type value_type;
typedef typename container::sign_type sign_type;
typedef typename container::arg_type arg_type;
typedef typename container::const_arg_type const_arg_type;
typedef typename container::ref_arg_type ref_arg_type;
typedef typename container::result_type result_type;
typedef typename container::const_result_type const_result_type;
static const int Size = SIZE;
static const int Capacity = container::Capacity;
BitsBase() : value_()
{
}
BitsBase(const_arg_type value) : value_(value)
{
trim(value_);
}
void set(const_arg_type value)
{
value_ = value;
trim(value_);
}
const_result_type get() const
{
return value_;
}
static void trim(ref_arg_type n)
{
container::trim(n);
}
void setSequence(const_arg_type value)
{
value_ = value;
trim(value_);
}
const_result_type getSequence() const
{
return container::getSequence(value_);
}
private:
value_type value_;
};
//----------------------------------------------------------------------
template<typename LHS, typename RHS>
class PackBase
{
public:
static const int Size = LHS::Size + RHS::Size;
typedef Container<Size> container;
typedef typename container::value_type value_type;
typedef typename container::arg_type arg_type;
typedef typename container::const_arg_type const_arg_type;
typedef typename container::ref_arg_type ref_arg_type;
typedef typename container::result_type result_type;
typedef typename container::const_result_type const_result_type;
PackBase(LHS& lhs, RHS& rhs) : lhs_(lhs), rhs_(rhs)
{
}
void setSequence(const_arg_type value)
{
rhs_.setSequence(value);
lhs_.setSequence(value >> RHS::Size);
}
const_result_type getSequence() const
{
return (lhs_.getSequence() << RHS::Size) | rhs_.getSequence();
}
private:
LHS& lhs_;
RHS& rhs_;
};
template<typename LHS, int N>
class PackBase<LHS, void (*)(Reserved<N>*)>
{
public:
static const int Size = LHS::Size + N;
typedef Container<Size> container;
typedef typename container::value_type value_type;
typedef typename container::arg_type arg_type;
typedef typename container::const_arg_type const_arg_type;
typedef typename container::ref_arg_type ref_arg_type;
typedef typename container::result_type result_type;
typedef typename container::const_result_type const_result_type;
PackBase(LHS& lhs, void (*)(Reserved<N>*)) : lhs_(lhs)
{
}
void setSequence(const_arg_type value)
{
lhs_.setSequence(value >> N);
}
const_result_type getSequence() const
{
return lhs_.getSequence() << N;
}
private:
LHS& lhs_;
};
template<typename LHS, typename RHS>
class ConstPackBase
{
public:
static const int Size = LHS::Size + RHS::Size;
typedef Container<Size> container;
typedef typename container::value_type value_type;
typedef typename container::arg_type arg_type;
typedef typename container::const_arg_type const_arg_type;
typedef typename container::ref_arg_type ref_arg_type;
typedef typename container::result_type result_type;
typedef typename container::const_result_type const_result_type;
ConstPackBase(const LHS& lhs, const RHS& rhs) : lhs_(lhs), rhs_(rhs)
{
}
const_result_type getSequence() const
{
return (lhs_.getSequence() << RHS::Size) | rhs_.getSequence();
}
private:
const LHS& lhs_;
const RHS& rhs_;
};
template<typename LHS, int N>
class ConstPackBase<LHS, void (*)(Reserved<N>*)>
{
public:
static const int Size = LHS::Size + N;
typedef Container<Size> container;
typedef typename container::value_type value_type;
typedef typename container::arg_type arg_type;
typedef typename container::const_arg_type const_arg_type;
typedef typename container::ref_arg_type ref_arg_type;
typedef typename container::result_type result_type;
typedef typename container::const_result_type const_result_type;
ConstPackBase(const LHS& lhs, void (*)(Reserved<N>*)) : lhs_(lhs)
{
}
const_result_type getSequence() const
{
return lhs_.getSequence() << N;
}
private:
const LHS& lhs_;
};
//----------------------------------------------------------------------
template<typename T, typename U>
struct Sign
{
template<bool C, typename T1, typename T2> struct _ { typedef T1 type; };
template<typename T1, typename T2> struct _<false, T1, T2> { typedef T2 type; };
typedef typename _<std::numeric_limits<T>::is_signed && std::numeric_limits<U>::is_signed, Signed, Unsigned>::type sign_type;
};
template<typename T>
struct Sign<T, Unsigned>
{
typedef Unsigned sign_type;
};
template<typename U>
struct Sign<Unsigned, U>
{
typedef Unsigned sign_type;
};
template<>
struct Sign<Unsigned, Unsigned>
{
typedef Unsigned sign_type;
};
template<>
struct Sign<Signed, Signed>
{
typedef Signed sign_type;
};
template<int N, int M, typename T, typename U>
struct Result
{
static const int Size = (N < M) ? M : N;
typedef typename Sign<T, U>::sign_type sign_type;
typedef Bits<Size, typename detail::Fit<Size, sign_type>::value_type> result_type;
};
//----------------------------------------------------------------------
} // namespace detail
//----------------------------------------------------------------------
template<int SIZE, typename T = Unsigned>
class Bits : private detail::BitsBase<SIZE, T>
{
public:
typedef detail::BitsBase<SIZE, T> super;
typedef typename super::signed_value_type signed_value_type;
typedef typename super::unsigned_value_type unsigned_value_type;
typedef typename super::value_type value_type;
typedef typename super::sign_type sign_type;
typedef typename super::arg_type arg_type;
typedef typename super::const_arg_type const_arg_type;
typedef typename super::ref_arg_type ref_arg_type;
typedef typename super::result_type result_type;
typedef typename super::const_result_type const_result_type;
static const int Size = super::Size;
static const int Capacity = super::Capacity;
static int size()
{
return Size;
}
Bits() : super()
{
}
explicit Bits(const_arg_type n) : super(n)
{
}
template<int M, typename U>
explicit Bits(const Bits<M, U>& bits) : super(bits.get())
{
}
Bits& set(const_arg_type n)
{
super::set(n);
return *this;
}
const_result_type get() const
{
return super::get();
}
void setSequence(const_arg_type value)
{
super::setSequence(value);
}
const_result_type getSequence() const
{
return super::getSequence();
}
operator const_result_type () const
{
return get();
}
const Bits& operator + () const
{
return *this;
}
Bits operator - () const
{
return Bits(-super::get());
}
Bits operator ~ () const
{
return Bits(~super::get());
}
Bits& operator ++ ()
{
*this += 1;
return *this;
}
Bits operator ++ (int)
{
Bits result(*this);
++*this;
return result;
}
Bits& operator -- ()
{
*this -= 1;
return *this;
}
Bits operator -- (int)
{
Bits result(*this);
--*this;
return result;
}
Bits& operator = (const_arg_type n)
{
return set(n);
}
Bits& operator += (const_arg_type n)
{
return set(super::get() + n);
}
Bits& operator -= (const_arg_type n)
{
return set(super::get() - n);
}
Bits& operator *= (const_arg_type n)
{
return set(super::get() * n);
}
Bits& operator /= (const_arg_type n)
{
return set(super::get() / n);
}
Bits& operator %= (const_arg_type n)
{
return set(super::get() % n);
}
Bits& operator |= (const_arg_type n)
{
return set(super::get() | n);
}
Bits& operator &= (const_arg_type n)
{
return set(super::get() & n);
}
Bits& operator ^= (const_arg_type n)
{
return set(super::get() ^ n);
}
Bits& operator <<= (int n)
{
super::set(super::get() << n);
return *this;
}
Bits& operator >>= (int n)
{
super::set(super::get() >> n);
return *this;
}
friend inline Bits operator << (const Bits& lhs, int rhs)
{
return Bits(lhs.get() << rhs);
}
friend inline Bits operator >> (const Bits& lhs, int rhs)
{
return Bits(lhs.get() >> rhs);
}
};
//----------------------------------------------------------------------
template<typename LHS, typename RHS> class ConstPack;
template<typename LHS, typename RHS>
class Pack : public detail::PackBase<LHS, RHS>
{
public:
typedef detail::PackBase<LHS, RHS> super;
static const int Size = super::Size;
typedef typename super::value_type value_type;
typedef typename super::arg_type arg_type;
typedef typename super::const_arg_type const_arg_type;
typedef typename super::ref_arg_type ref_arg_type;
typedef typename super::result_type result_type;
typedef typename super::const_result_type const_result_type;
static int size()
{
return Size;
}
Pack(LHS& lhs, RHS& rhs) : super(lhs, rhs)
{
}
Pack& operator = (const_arg_type value)
{
setSequence(value);
return *this;
}
Pack& operator = (const Pack& value)
{
setSequence(value.getSequence());
return *this;
}
operator result_type () const
{
return super::getSequence();
}
template<int M, typename U>
Pack<Pack, Bits<M, U> > operator , (Bits<M, U>& rhs)
{
return Pack<Pack, Bits<M, U> >(*this, rhs);
}
template<int M>
Pack<Pack, void (*)(detail::Reserved<M>*)> operator , (void (*rhs)(detail::Reserved<M>*))
{
return Pack<Pack, void (*)(detail::Reserved<M>*)>(*this, rhs);
}
template<int M, typename U>
ConstPack<Pack, Bits<M, U> > operator , (const Bits<M, U>& rhs) const
{
return ConstPack<Pack, Bits<M, U> >(*this, rhs);
}
template<typename L, typename R>
ConstPack<Pack, Pack<L, R> > operator , (const Pack<L, R>& rhs) const
{
return ConstPack<Pack, Pack<L, R> >(*this, rhs);
}
template<typename L, typename R>
ConstPack<Pack, ConstPack<L, R> > operator , (const ConstPack<L, R>& rhs) const
{
return ConstPack<Pack, ConstPack<L, R> >(*this, rhs);
}
template<int M>
ConstPack<Pack, void (*)(detail::Reserved<M>*)> operator , (void (*rhs)(detail::Reserved<M>*)) const
{
return ConstPack<Pack, void (*)(detail::Reserved<M>*)>(*this, rhs);
}
};
template<typename LHS, typename RHS>
class ConstPack : public detail::ConstPackBase<LHS, RHS>
{
public:
typedef detail::ConstPackBase<LHS, RHS> super;
static const int Size = super::Size;
typedef typename super::value_type value_type;
typedef typename super::arg_type arg_type;
typedef typename super::const_arg_type const_arg_type;
typedef typename super::ref_arg_type ref_arg_type;
typedef typename super::result_type result_type;
typedef typename super::const_result_type const_result_type;
static int size()
{
return Size;
}
ConstPack(const LHS& lhs, const RHS& rhs) : super(lhs, rhs)
{
}
operator result_type () const
{
return super::getSequence();
}
template<int M, typename U>
ConstPack<ConstPack, Bits<M, U> > operator , (const Bits<M, U>& rhs) const
{
return ConstPack<ConstPack, Bits<M, U> >(*this, rhs);
}
template<typename L, typename R>
ConstPack<ConstPack, Pack<L, R> > operator , (const Pack<L, R>& rhs) const
{
return ConstPack<ConstPack, Pack<L, R> >(*this, rhs);
}
template<typename L, typename R>
ConstPack<ConstPack, ConstPack<L, R> > operator , (const ConstPack<L, R>& rhs) const
{
return ConstPack<ConstPack, ConstPack<L, R> >(*this, rhs);
}
template<int M>
ConstPack<ConstPack, void (*)(detail::Reserved<M>*)> operator , (void (*rhs)(detail::Reserved<M>*)) const
{
return ConstPack<ConstPack, void (*)(detail::Reserved<M>*)>(*this, rhs);
}
};
//----------------------------------------------------------------------
#define EMATTSAN_BITS_DEFINE_OP(op) \
\
template<int N, typename T, int M, typename U> \
inline typename detail::Result<N, M, T, U>::result_type \
operator op (const Bits<N, T>& lhs, const Bits<M, U>& rhs) \
{ \
return typename detail::Result<N, M, T, U>::result_type(lhs.get() op rhs.get()); \
} \
\
template<int N, typename T> \
inline typename detail::Result<N, N, T, typename Bits<N, T>::signed_value_type>::result_type \
operator op (const Bits<N, T>& lhs, typename Bits<N, T>::signed_value_type rhs) \
{ \
return lhs op Bits<N, typename Bits<N, T>::signed_value_type>(rhs); \
} \
\
template<int N, typename T> \
inline typename detail::Result<N, N, T, typename Bits<N, T>::signed_value_type>::result_type \
operator op (typename Bits<N, T>::signed_value_type lhs, const Bits<N, T>& rhs) \
{ \
return Bits<N, typename Bits<N, T>::signed_value_type>(lhs) op rhs; \
} \
\
template<int N, typename T> \
inline typename detail::Result<N, N, T, typename Bits<N, T>::unsigned_value_type>::result_type \
operator op (const Bits<N, T>& lhs, typename Bits<N, T>::unsigned_value_type rhs) \
{ \
return lhs op Bits<N, typename Bits<N, T>::unsigned_value_type>(rhs); \
} \
\
template<int N, typename T> \
inline typename detail::Result<N, N, T, typename Bits<N, T>::unsigned_value_type>::result_type \
operator op (typename Bits<N, T>::unsigned_value_type lhs, const Bits<N, T>& rhs) \
{ \
return Bits<N, typename Bits<N, T>::unsigned_value_type>(lhs) op rhs; \
} \
\
template<int N, typename T> \
inline typename detail::Result<N, N, T, signed int>::result_type \
operator op (const Bits<N, T>& lhs, signed int rhs) \
{ \
return lhs op Bits<N, typename Bits<N, T>::signed_value_type>(rhs); \
} \
\
template<int N, typename T> \
inline typename detail::Result<N, N, T, signed int>::result_type \