other.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. /*
  2. * Copyright 2012 Institute of Communication and Computer Systems (ICCS)
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. *
  16. */
  17. /**
  18. * @file bitmap/other.c
  19. * @author Ilias Pliotas, Ioannis Koutras
  20. * @date September 2012
  21. * @brief Helping functions for bitmap-organized raw blocks
  22. */
  23. #include "bitmap/bitmap_other.h"
  24. /** Copy the elements of one bitmap array to another.
  25. *
  26. * @param dest The destination bitmap array.
  27. * @param src The source bitmap array.
  28. * @param elements The number of elements
  29. */
  30. void copy_array(BMAP_EL_TYPE *dest, BMAP_EL_TYPE *src, size_t elements) {
  31. for(size_t i = 0; i < elements; ++i) {
  32. dest[i] = src[i];
  33. }
  34. }
  35. /** Shift right by n bits all the elements of a BMAP_EL_TYPE array
  36. *
  37. * @param array The array to be shifted right
  38. * @param n The number of bits to be shifted
  39. * @param elements The number of elements
  40. */
  41. void shift_array(BMAP_EL_TYPE *array, size_t n, size_t elements) {
  42. int i;
  43. BMAP_EL_TYPE mask1, mask2, guard_mask;
  44. // Move element-wise at first
  45. while(n >= BMAP_EL_SIZE_BITS) {
  46. for(i = 1; i < (int) elements; i++) {
  47. array[i - 1] = array[i];
  48. }
  49. array[elements - 1] = 0;
  50. n -= BMAP_EL_SIZE_BITS;
  51. }
  52. // Check if there are still bits to be shifted
  53. if(n > 0) {
  54. mask1 = (BMAP_EL_TYPE) 0;
  55. guard_mask = ((BMAP_EL_TYPE) 1 << n) - 1;
  56. for(i = (int) elements - 1; i >= 0; --i) {
  57. mask2 = array[i] & guard_mask;
  58. array[i] >>= n;
  59. array[i] |= mask1 << (BMAP_EL_SIZE_BITS - n);
  60. mask1 = mask2;
  61. }
  62. }
  63. }
  64. /** Perform bitwsie add on every element of a BMAP_EL_TYPE arrays; store the
  65. * result on the first array
  66. */
  67. void add_arrays(BMAP_EL_TYPE *array1, BMAP_EL_TYPE *array2, size_t elements) {
  68. for(size_t i = 0; i < elements; i++) {
  69. array1[i] &= array2[i];
  70. }
  71. }
  72. /** Compute the previous power of 2 which is <= n */
  73. size_t prev_pow2(size_t n) {
  74. unsigned int last_for_n_bits, i;
  75. /* size_t is the data type of the argument
  76. *
  77. * We multiply it by 8 to get the bits and then divide it by 2 to get the
  78. * previous size, ergo multiply by 4.
  79. */
  80. last_for_n_bits = sizeof(size_t) * 4;
  81. i = 1;
  82. while(i <= last_for_n_bits) {
  83. n |= n >> i;
  84. i <<= 1;
  85. }
  86. return ((n >> 1) + 1);
  87. }
  88. /** Makes a bit mask of 1's
  89. * @param start_pos The starting position of 1's
  90. * @param length The amount of 1's
  91. */
  92. BMAP_EL_TYPE make_bit_mask(size_t start_pos, size_t length) {
  93. BMAP_EL_TYPE init;
  94. init = (BMAP_EL_TYPE) 1;
  95. // length should be <= BMAP_EL_SIZE_BITS in any case
  96. if (length < BMAP_EL_SIZE_BITS) {
  97. init <<= length ;
  98. init--;
  99. init <<= start_pos - 1;
  100. return init;
  101. } else {
  102. return ~((BMAP_EL_TYPE) 0);
  103. }
  104. }