bits_utils.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. /*
  2. * Copyright (c) 2021 The Linux Foundation. All rights reserved.
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions are
  6. * met:
  7. * * Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * * Redistributions in binary form must reproduce the above
  10. * copyright notice, this list of conditions and the following
  11. * disclaimer in the documentation and/or other materials provided
  12. * with the distribution.
  13. * * Neither the name of The Linux Foundation nor the names of its
  14. * contributors may be used to endorse or promote products derived
  15. * from this software without specific prior written permission.
  16. *
  17. * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
  18. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  19. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
  20. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
  21. * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  22. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  23. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
  24. * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  25. * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
  26. * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
  27. * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. */
  29. #ifndef NETWORK_TRAFFIC_BITS_UTILS_H
  30. #define NETWORK_TRAFFIC_BITS_UTILS_H
  31. #include <vector>
  32. #include <bitset>
  33. #define SIZE_OF_BITS(x) (sizeof(x) * CHAR_BIT)
  34. using std::vector;
  35. using std::bitset;
  36. /**
  37. * bitwise get the n-th bit of val.
  38. * @tparam T - some unsigned integer type
  39. * @param num - number to get the bit from
  40. * @param n - index of bit to return
  41. * @return 1 if the n-th bit of val is 1, 0 otherwise
  42. */
  43. template<typename T>
  44. bool getUintNthBit(T num, unsigned int n) {
  45. assert(n >= 0 && n < (SIZE_OF_BITS(num)));
  46. return (num & ( 1 << n )) >> n; // NOLINT(hicpp-signed-bitwise)
  47. }
  48. /**
  49. * Transforms a vector<bool> from a bitset<N>.
  50. * @tparam N - Number of bits.
  51. * @param bits - Bits to transform.
  52. * @return vector<bool> that represents the bitset
  53. */
  54. template<int N>
  55. static vector<bool> bitsetToVector(const bitset<N>& bits){
  56. vector<bool> outVec;
  57. for(int i = 0; i < N; i++){
  58. outVec.insert(outVec.begin(), bits[i]);
  59. }
  60. return outVec;
  61. }
  62. template<typename T>
  63. static vector<bool> uintToVector(T n) {
  64. vector<bool> outVec;
  65. for (int i = 0; i < SIZE_OF_BITS(n); i++) {
  66. outVec.insert(outVec.begin(), getUintNthBit(n, i));
  67. }
  68. return outVec;
  69. }
  70. /**
  71. * Copies a range of bits in an array of integer-like elements to a bitset.
  72. * The range is [bufIndex, N-1]
  73. * @tparam T - The type of the elements in the array.
  74. * @tparam N - The size of the range.
  75. * @param bits - the bits object to copy into.
  76. * @param buf - Input array.
  77. * @param bufIndex - starting bit.
  78. */
  79. template<typename T, unsigned int N>
  80. static void setBitsFromArray(bitset<N>& bits, const T* buf, unsigned int& bufIndex) {
  81. for (int i = N-1; i >= 0; i--) {
  82. unsigned int bitIndex = SIZE_OF_BITS(T) - 1 - (bufIndex % SIZE_OF_BITS(T));
  83. bool bit = getUintNthBit(*(buf + (bufIndex / 8)), bitIndex);
  84. bits.set(i, bit);
  85. bufIndex++;
  86. }
  87. }
  88. template<typename T, unsigned int N>
  89. static void setBitsFromArray(bitset<N>& bits, const T* buf) {
  90. unsigned int idx = 0;
  91. for (int i = N - 1; i >= 0; i--) {
  92. unsigned int bitIndex = SIZE_OF_BITS(T) - 1 - (idx % SIZE_OF_BITS(T));
  93. bool bit = getUintNthBit(*(buf + (idx / 8)), bitIndex);
  94. bits.set(i, bit);
  95. idx++;
  96. }
  97. }
  98. template<typename T>
  99. void setNthBit(T& num, unsigned int n){
  100. assert(n < SIZE_OF_BITS(num));
  101. num |= 1UL << n;
  102. }
  103. /**
  104. * Clears the n-th bit of num
  105. * @tparam T - the type of num
  106. * @param num - the number to clear a bit on
  107. * @param n - the index of the bit to clear
  108. */
  109. template<typename T>
  110. void clearNthBit(T& num, unsigned int n){
  111. assert(n < SIZE_OF_BITS(num));
  112. num &= ~(1UL << n);
  113. }
  114. template<typename T>
  115. void changeNthBit(T& num, unsigned int n, bool bitVal){
  116. assert(n < SIZE_OF_BITS(num));
  117. if(bitVal){
  118. setNthBit(num, n);
  119. } else {
  120. clearNthBit(num, n);
  121. }
  122. }
  123. void toggleLsbMsb(vector<bool>& v, unsigned int intervalSize){
  124. if(v.size() % intervalSize != 0){
  125. return;
  126. }
  127. for(size_t i = 0; i < v.size(); i += intervalSize){
  128. vector<bool> tmp(intervalSize);
  129. for(unsigned int j = 0; j < intervalSize; j++){
  130. tmp[j] = v[i + intervalSize - 1 - j];
  131. }
  132. for(unsigned int j = 0; j < intervalSize; j++){
  133. v[i + j] = tmp[j];
  134. }
  135. }
  136. }
  137. void toggleEndianness(vector<bool>& v, unsigned int wordSize){
  138. if(wordSize % CHAR_BIT != 0 || v.size() % wordSize != 0){
  139. return;
  140. }
  141. for(size_t i = 0; i < v.size(); i += wordSize){
  142. vector<bool> tmp(wordSize);
  143. for(size_t j = 0; j < wordSize; j += CHAR_BIT){
  144. for(size_t k = 0; k < CHAR_BIT; k++){
  145. unsigned int readIdx = i + wordSize - j - CHAR_BIT + k;
  146. unsigned int writeIdx = j + k;
  147. tmp[writeIdx] = v[readIdx];
  148. }
  149. }
  150. for(unsigned int j = 0; j < wordSize; j++){
  151. v[i + j] = tmp[j];
  152. }
  153. }
  154. }
  155. template<typename T, typename W>
  156. W bitWiseConcatenate(T left, T right){
  157. W wide = (static_cast<W>(left) << CHAR_BIT * (sizeof(W) - sizeof(T))) | right;
  158. return wide;
  159. }
  160. template<typename IntType>
  161. void toArray(vector<bool>& v, IntType* buf){
  162. for(unsigned int i = 0; i < v.size(); i++){
  163. if(v[i]){
  164. setNthBit(buf[i / SIZE_OF_BITS(*buf)], i % SIZE_OF_BITS(*buf));
  165. }
  166. }
  167. }
  168. #endif //NETWORK_TRAFFIC_BITS_UTILS_H