Last active
September 22, 2020 00:37
-
-
Save xypron/9ad954dc65899caea4f4 to your computer and use it in GitHub Desktop.
Bit reversed order rearrangement of array in O(n) time.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
For Fast Fourier Transformations an important step is the rearrangement of the | |
data in a bit reversed order. | |
The implemention below shows how to do this inplace or as copy in O(n) runtime. | |
*/ | |
/* | |
The MIT License (MIT) | |
Copyright (c) 2015, Heinrich Schuchardt <xypron.glpk@gmx.de> | |
Permission is hereby granted, free of charge, to any person obtaining a copy | |
of this software and associated documentation files (the "Software"), to deal | |
in the Software without restriction, including without limitation the rights | |
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
copies of the Software, and to permit persons to whom the Software is | |
furnished to do so, subject to the following conditions: | |
The above copyright notice and this permission notice shall be included in | |
all copies or substantial portions of the Software. | |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | |
THE SOFTWARE. | |
*/ | |
#include <malloc.h> | |
#include <stdio.h> | |
#include <stdlib.h> | |
#include <string.h> | |
/** | |
* Rearranges array in bit reversed order. | |
* | |
* @param a data | |
* @param n number of elements, must be power of two | |
* | |
* Runtime is O(n). | |
*/ | |
void bro(int *a, int n) | |
{ | |
int i; | |
int lowbit; | |
int carry; | |
int counter = 0; | |
int buf; | |
lowbit = n >> 1; | |
for (i = 1; i < n; i++) { | |
/* | |
* Increment bit reversed counter. | |
*/ | |
carry = lowbit; | |
while(counter & carry) { | |
counter ^= carry; | |
carry >>= 1; | |
} | |
counter ^= carry; | |
if (counter > i) { | |
/* | |
* Swap | |
*/ | |
buf = a[i]; | |
a[i] = a[counter]; | |
a[counter] = buf; | |
} | |
} | |
} | |
/** | |
* Copy array in bit reversed order. | |
* | |
* @param a input | |
* @param b output | |
* @param n number of elements, must be power of two | |
* | |
* Runtime is O(n). | |
*/ | |
void brocopy(int *a, int *b, int n) | |
{ | |
int i; | |
int lowbit; | |
int carry; | |
int counter = 0; | |
lowbit = n >> 1; | |
b[0] = a[0]; | |
for (i = 1; i < n; i++) { | |
/* | |
* Increment bit reversed counter. | |
*/ | |
carry = lowbit; | |
while(counter & carry) { | |
counter ^= carry; | |
carry >>= 1; | |
} | |
counter ^= carry; | |
/* | |
* Copy. | |
*/ | |
b[counter] = a[i]; | |
b[i] = a[counter]; | |
} | |
} | |
/** | |
* Rearranges array in bit reversed order using merge sort. | |
* | |
* @param a data | |
* @param b work area | |
* @param n number of elements, must be power of two | |
* | |
* Runtime is O(n log(n)). | |
* The algorithm is optimized to profit from caching, | |
* furthermore it can easily be parallelized. | |
*/ | |
void bromerge(int *a, int *b, int n) | |
{ | |
int o, m, i; | |
int n2 = n >> 1; | |
int *c[3]; | |
int *p1, *p2, *p3; | |
c[0] = b; | |
c[1] = a; | |
for(o = 1; o < n2; o <<= 1) { | |
c[2] = c[1]; | |
c[1] = c[0]; | |
c[0] = c[2]; | |
p1 = c[0]; | |
p2 = c[1]; | |
p3 = p1 + n2; | |
for(m = 0; m < n; m += (o << 1)) { | |
memcpy(p2, p1, o * sizeof(int)); | |
p2 += o; | |
p1 += o; | |
memcpy(p2, p3, o * sizeof(int)); | |
p2 += o; | |
p3 += o; | |
} | |
} | |
if (c[0] == a) | |
memcpy(a, b, n * sizeof(int)); | |
} | |
/** | |
* Prints array. | |
* | |
* @param a array | |
* @param n number of elements | |
*/ | |
static void print(int *a, int n) | |
{ | |
int m = -1; | |
int i, j; | |
int mask = 1; | |
int b; | |
for (i = n; i != 0; i >>= 1) | |
++m; | |
for (i = 1; i < m; ++i) | |
mask <<= 1; | |
for (i = 0; i < n; ++i) { | |
printf("%08x ", i); | |
b = a[i]; | |
for (j = 0; j < m; ++j) { | |
printf("%1x", (b & mask) ? 1 : 0); | |
b <<= 1; | |
} | |
printf("\n"); | |
} | |
printf("\n"); | |
} | |
/** | |
* Fills an array with numbers 0..n-1. | |
* Then the array is rearranged in bit reversed order. | |
* The array is output before and after the rearrangement. | |
*/ | |
int main(int argc, char *argv[]) | |
{ | |
int m = 6; | |
int n = 1 << m; | |
int i; | |
int *a = malloc(n * sizeof(int)); | |
int *b = malloc(n * sizeof(int)); | |
for (i = 0; i < n; ++i) { | |
a[i] = i; | |
} | |
print(a, n); | |
bromerge(a, b, n); | |
print(a, n); | |
free(a); | |
free(b); | |
return EXIT_SUCCESS; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment