bits_utils.h 7.6 KB

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