C++ mpi module for stochmagnet_main Package
functions_bit.h
1 #ifndef FUNCTIONS_BIT_H
2 #define FUNCTIONS_BIT_H
3 
4 //types of the code
5 #include "types.h"
6 #include "functions_type.h"
7 
8 //math header isnan
9 #include <math.h>
10 
11 
12 //bitwise operators
13 //================
14 // a | b | iand
15 // 0 | 0 | 0
16 // 0 | 1 | 0
17 // 1 | 0 | 0
18 // 1 | 1 | 1
19 #define iand(a,b) ( (a) & (b) )
20 
21 // a | b | ior
22 // 0 | 0 | 0
23 // 0 | 1 | 1
24 // 1 | 0 | 1
25 // 1 | 1 | 1
26 #define ior(a,b) ( (a) | (b) )
27 
28 // a | b | inor
29 // 0 | 0 | 1
30 // 0 | 1 | 0
31 // 1 | 0 | 0
32 // 1 | 1 | 0
33 #define inor(a,b) ( ~( (a) | (b) ) )
34 
35 // a | b | ixor
36 // 0 | 0 | 0
37 // 0 | 1 | 1
38 // 1 | 0 | 1
39 // 1 | 1 | 0
40 #define ixor(a,b) ( (a) ^ (b) ) )
41 
42 // a | inot
43 // 0 | 1
44 // 1 | 0
45 #define inot(a) ( 256 - (~(a)) )
46 
47 namespace functions_bit {
48 
49  //bit manipulation
50  //================
51 
52  //a[p]=1
53  template<typename T> requires functions_type::isIntegerType<T>
54  inline static T& bitSet(T& a,const tUCInt& p) {
55  a |= (1ULL<<p);
56  return a;
57  }
58 
59  //a[p]=0
60  template<typename T> requires functions_type::isIntegerType<T>
61  inline static T& bitClear(T& a,const tUCInt& p) {
62  a &= ~(1ULL<<p);
63  return a;
64  }
65  //a[p]=!a[p]
66  template<typename T> requires functions_type::isIntegerType<T>
67  inline static T& bitFlip(T& a,const tUCInt& p) {
68  a ^= (1ULL<<p);
69  return a;
70  }
71 
72  //b=a[p];a[n-1]...a[2]a[1]a[0]
73  template<typename T> requires functions_type::isIntegerType<T>
74  inline static tBoolean bitGet(const T& a,const tUCInt& p) {
75  return (!!(a & (1ULL<<p)));
76  }
77 
78 
79  //a[p]=1 for all p=1 for all p were mask[p]=1
80  template<typename T> requires functions_type::isIntegerType<T>
81  inline static T& bitMaskSet(T& x, const T& mask) {
82  x |= (mask);
83  return x;
84  }
85 
86  //a[p]=0 for all p=1 for all p where mask[p]=1
87  template<typename T> requires functions_type::isIntegerType<T>
88  inline static T& bitMaskClear(T& x, const T& mask) {
89  x &= (~(mask));
90  return x;
91  };
92 
93  //a[p]=!a[p] for all for all p where mask[p]=1
94  template<typename T> requires functions_type::isIntegerType<T>
95  inline static T& bitMaskFlip(T& x, const T& mask) {
96  x ^= mask;
97  return x;
98  }
99 
100  //return true is all a[p]=1 for all p where mask[p]=1
101  template<typename T> requires functions_type::isIntegerType<T>
102  inline static tBoolean bitMaskCheckAll(const T& x, const T& mask) {
103  return (!(~(x) & (mask)));
104  }
105 
106  //return false (0) if it does not exist any p such that a[p]=1 for all p where mask[p]=1
107  //return true is all a[p]=1 for all p where mask[p]=1
108  template<typename T> requires functions_type::isIntegerType<T>
109  inline static tBoolean bitMaskCheckAny(const T& x, const T& mask) {
110  return ((x) & (mask));
111  }
112 
113 }
114 
115 #endif