You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

126 lines
2.8 KiB

  1. /*
  2. Copyright 2011 Jun Wako <wakojun@gmail.com>
  3. This program is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation, either version 2 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>.
  13. */
  14. #include "util.h"
  15. // bit population - return number of on-bit
  16. __attribute__((noinline)) uint8_t bitpop(uint8_t bits) {
  17. uint8_t c;
  18. for (c = 0; bits; c++)
  19. bits &= bits - 1;
  20. return c;
  21. /*
  22. const uint8_t bit_count[] = { 0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4 };
  23. return bit_count[bits>>4] + bit_count[bits&0x0F]
  24. */
  25. }
  26. uint8_t bitpop16(uint16_t bits) {
  27. uint8_t c;
  28. for (c = 0; bits; c++)
  29. bits &= bits - 1;
  30. return c;
  31. }
  32. uint8_t bitpop32(uint32_t bits) {
  33. uint8_t c;
  34. for (c = 0; bits; c++)
  35. bits &= bits - 1;
  36. return c;
  37. }
  38. // most significant on-bit - return highest location of on-bit
  39. // NOTE: return 0 when bit0 is on or all bits are off
  40. __attribute__((noinline)) uint8_t biton(uint8_t bits) {
  41. uint8_t n = 0;
  42. if (bits >> 4) {
  43. bits >>= 4;
  44. n += 4;
  45. }
  46. if (bits >> 2) {
  47. bits >>= 2;
  48. n += 2;
  49. }
  50. if (bits >> 1) {
  51. bits >>= 1;
  52. n += 1;
  53. }
  54. return n;
  55. }
  56. uint8_t biton16(uint16_t bits) {
  57. uint8_t n = 0;
  58. if (bits >> 8) {
  59. bits >>= 8;
  60. n += 8;
  61. }
  62. if (bits >> 4) {
  63. bits >>= 4;
  64. n += 4;
  65. }
  66. if (bits >> 2) {
  67. bits >>= 2;
  68. n += 2;
  69. }
  70. if (bits >> 1) {
  71. bits >>= 1;
  72. n += 1;
  73. }
  74. return n;
  75. }
  76. uint8_t biton32(uint32_t bits) {
  77. uint8_t n = 0;
  78. if (bits >> 16) {
  79. bits >>= 16;
  80. n += 16;
  81. }
  82. if (bits >> 8) {
  83. bits >>= 8;
  84. n += 8;
  85. }
  86. if (bits >> 4) {
  87. bits >>= 4;
  88. n += 4;
  89. }
  90. if (bits >> 2) {
  91. bits >>= 2;
  92. n += 2;
  93. }
  94. if (bits >> 1) {
  95. bits >>= 1;
  96. n += 1;
  97. }
  98. return n;
  99. }
  100. __attribute__((noinline)) uint8_t bitrev(uint8_t bits) {
  101. bits = (bits & 0x0f) << 4 | (bits & 0xf0) >> 4;
  102. bits = (bits & 0b00110011) << 2 | (bits & 0b11001100) >> 2;
  103. bits = (bits & 0b01010101) << 1 | (bits & 0b10101010) >> 1;
  104. return bits;
  105. }
  106. uint16_t bitrev16(uint16_t bits) {
  107. bits = bitrev(bits & 0x00ff) << 8 | bitrev((bits & 0xff00) >> 8);
  108. return bits;
  109. }
  110. uint32_t bitrev32(uint32_t bits) {
  111. bits = (uint32_t)bitrev16(bits & 0x0000ffff) << 16 | bitrev16((bits & 0xffff0000) >> 16);
  112. return bits;
  113. }