keldon85

bitreversal.c

Sep 24th, 2014
258
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.06 KB | None | 0 0
  1. /*Copyright 2014 Keldon Alleyne
  2.  
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6.  
  7.     http://www.apache.org/licenses/LICENSE-2.0
  8.  
  9. Unless required by applicable law or agreed to in writing, software
  10. distributed under the License is distributed on an "AS IS" BASIS,
  11. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. See the License for the specific language governing permissions and
  13. limitations under the License.*/
  14.  
  15. /* This file contains a fast method for creating bit reversal lookups
  16. in O(N) time using little more than N adds in the process
  17. (and 2 log N bit shifts).
  18.  
  19. Another method is provided that takes a 256 entry bit reversal table,
  20. and calculates the bit reversal in 6 bit shifts, and 6 logical operations.
  21. */
  22.  
  23. /*** bitreversal.h ***/
  24. void CreateBitReverseTable(int *table,int size);
  25. unsigned int ReverseBitsWithTable(unsigned int n,
  26.         const unsigned int *table);
  27. unsigned int ReverseBits(unsigned int n);
  28. int ReverseBitsAndShift(unsigned int n, int right_shift);
  29.  
  30. /*** bitreversal.c ***/
  31. void ReverseBitsHelper(int *table, int size,
  32.     int build_size, int next_bit);
  33.  
  34. unsigned int ReverseBitsWithTable(unsigned int n,
  35.         const unsigned int *table)
  36. {
  37.     unsigned int n0 = n & 0xffU;
  38.     unsigned int n1 = (n >> 8) & 0xffU;
  39.     unsigned int n2 = (n >> 16) & 0xffU;
  40.     unsigned int n3 = (n >> 24);
  41.     unsigned int reversed =
  42.         reverse_table[n0] << 24 |
  43.         reverse_table[n1] << 16 |
  44.         reverse_table[n2] << 8 |
  45.         reverse_table[n3];
  46.     return reversed;
  47.  
  48. }
  49.  
  50. unsigned int ReverseBits(unsigned int n) {
  51.     unsigned int reverse_table[256];
  52.     CreateBitReverseTable((int*)reverse_table,256);
  53. }
  54.  
  55. int ReverseBitsAndShift(unsigned int n, int right_shift) {
  56.     return ReverseBits(n) >> right_shift;
  57. }
  58.  
  59. void CreateBitReverseTable(int *table,int size) {
  60.     int build_size;
  61.     int next_bit;
  62.     build_size = 1;
  63.     next_bit = size >> 1;
  64.  
  65.     table[0] = 0;
  66.     while(build_size <= size) {
  67.         ReverseBitsHelper(table, size, build_size, next_bit);
  68.         build_size <<= 1;
  69.         next_bit >>= 1;
  70.     }
  71. }
  72. void ReverseBitsHelper(int *table, int size, int build_size, int next_bit) {
  73.     int write_index;
  74.     int end;
  75.     int read_index;
  76.  
  77.     int shifted_source;
  78.     int msb;
  79.     int next_value;
  80.  
  81.     if(build_size >= size) {
  82.         return;
  83.     }
  84.     read_index = 0;
  85.     end = build_size << 1;
  86.     for(write_index = build_size;
  87.         write_index < end;
  88.         ++write_index, ++read_index)
  89.     {
  90.         next_value = table[read_index] + next_bit;
  91.         table[write_index] = next_value;
  92.     }
  93. }
  94.  
  95.  
  96. /*** bitreversal_test.c ***/
  97. #include <stdio.h>
  98.  
  99. int main() {
  100.     const int kNBits = 3;
  101.     const int N = 1 << kNBits;
  102.     int i;
  103.     int table[N];
  104.     CreateBitReverseTable(table,N);
  105.     for(i = 0; i < N; ++i) {
  106.         // printf("%d:%d\n", i, table[i]);
  107.         printf("%d:%d\n", i, ReverseBitsAndShift(i,32-kNBits));
  108.     }
  109.     return 0;
  110. }
Advertisement
Add Comment
Please, Sign In to add comment