complex_bit_reverse.c (4570B)
1 /* 2 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved. 3 * 4 * Use of this source code is governed by a BSD-style license 5 * that can be found in the LICENSE file in the root of the source 6 * tree. An additional intellectual property rights grant can be found 7 * in the file PATENTS. All contributing project authors may 8 * be found in the AUTHORS file in the root of the source tree. 9 */ 10 11 #include "common_audio/signal_processing/include/signal_processing_library.h" 12 13 /* Tables for data buffer indexes that are bit reversed and thus need to be 14 * swapped. Note that, index_7[{0, 2, 4, ...}] are for the left side of the swap 15 * operations, while index_7[{1, 3, 5, ...}] are for the right side of the 16 * operation. Same for index_8. 17 */ 18 19 /* Indexes for the case of stages == 7. */ 20 static const int16_t index_7[112] = { 21 1, 64, 2, 32, 3, 96, 4, 16, 5, 80, 6, 48, 7, 112, 9, 72, 22 10, 40, 11, 104, 12, 24, 13, 88, 14, 56, 15, 120, 17, 68, 18, 36, 23 19, 100, 21, 84, 22, 52, 23, 116, 25, 76, 26, 44, 27, 108, 29, 92, 24 30, 60, 31, 124, 33, 66, 35, 98, 37, 82, 38, 50, 39, 114, 41, 74, 25 43, 106, 45, 90, 46, 58, 47, 122, 49, 70, 51, 102, 53, 86, 55, 118, 26 57, 78, 59, 110, 61, 94, 63, 126, 67, 97, 69, 81, 71, 113, 75, 105, 27 77, 89, 79, 121, 83, 101, 87, 117, 91, 109, 95, 125, 103, 115, 111, 123}; 28 29 /* Indexes for the case of stages == 8. */ 30 static const int16_t index_8[240] = { 31 1, 128, 2, 64, 3, 192, 4, 32, 5, 160, 6, 96, 7, 224, 8, 32 16, 9, 144, 10, 80, 11, 208, 12, 48, 13, 176, 14, 112, 15, 240, 33 17, 136, 18, 72, 19, 200, 20, 40, 21, 168, 22, 104, 23, 232, 25, 34 152, 26, 88, 27, 216, 28, 56, 29, 184, 30, 120, 31, 248, 33, 132, 35 34, 68, 35, 196, 37, 164, 38, 100, 39, 228, 41, 148, 42, 84, 43, 36 212, 44, 52, 45, 180, 46, 116, 47, 244, 49, 140, 50, 76, 51, 204, 37 53, 172, 54, 108, 55, 236, 57, 156, 58, 92, 59, 220, 61, 188, 62, 38 124, 63, 252, 65, 130, 67, 194, 69, 162, 70, 98, 71, 226, 73, 146, 39 74, 82, 75, 210, 77, 178, 78, 114, 79, 242, 81, 138, 83, 202, 85, 40 170, 86, 106, 87, 234, 89, 154, 91, 218, 93, 186, 94, 122, 95, 250, 41 97, 134, 99, 198, 101, 166, 103, 230, 105, 150, 107, 214, 109, 182, 110, 42 118, 111, 246, 113, 142, 115, 206, 117, 174, 119, 238, 121, 158, 123, 222, 43 125, 190, 127, 254, 131, 193, 133, 161, 135, 225, 137, 145, 139, 209, 141, 44 177, 143, 241, 147, 201, 149, 169, 151, 233, 155, 217, 157, 185, 159, 249, 45 163, 197, 167, 229, 171, 213, 173, 181, 175, 245, 179, 205, 183, 237, 187, 46 221, 191, 253, 199, 227, 203, 211, 207, 243, 215, 235, 223, 251, 239, 247}; 47 48 void WebRtcSpl_ComplexBitReverse(int16_t* __restrict complex_data, int stages) { 49 /* For any specific value of stages, we know exactly the indexes that are 50 * bit reversed. Currently (Feb. 2012) in WebRTC the only possible values of 51 * stages are 7 and 8, so we use tables to save unnecessary iterations and 52 * calculations for these two cases. 53 */ 54 if (stages == 7 || stages == 8) { 55 int m = 0; 56 int length = 112; 57 const int16_t* index = index_7; 58 59 if (stages == 8) { 60 length = 240; 61 index = index_8; 62 } 63 64 /* Decimation in time. Swap the elements with bit-reversed indexes. */ 65 for (m = 0; m < length; m += 2) { 66 /* We declare a int32_t* type pointer, to load both the 16-bit real 67 * and imaginary elements from complex_data in one instruction, reducing 68 * complexity. 69 */ 70 int32_t* complex_data_ptr = (int32_t*)complex_data; 71 int32_t temp = 0; 72 73 temp = complex_data_ptr[index[m]]; /* Real and imaginary */ 74 complex_data_ptr[index[m]] = complex_data_ptr[index[m + 1]]; 75 complex_data_ptr[index[m + 1]] = temp; 76 } 77 } else { 78 int m = 0, mr = 0, l = 0; 79 int n = 1 << stages; 80 int nn = n - 1; 81 82 /* Decimation in time - re-order data */ 83 for (m = 1; m <= nn; ++m) { 84 int32_t* complex_data_ptr = (int32_t*)complex_data; 85 int32_t temp = 0; 86 87 /* Find out indexes that are bit-reversed. */ 88 l = n; 89 do { 90 l >>= 1; 91 } while (l > nn - mr); 92 mr = (mr & (l - 1)) + l; 93 94 if (mr <= m) { 95 continue; 96 } 97 98 /* Swap the elements with bit-reversed indexes. 99 * This is similar to the loop in the stages == 7 or 8 cases. 100 */ 101 temp = complex_data_ptr[m]; /* Real and imaginary */ 102 complex_data_ptr[m] = complex_data_ptr[mr]; 103 complex_data_ptr[mr] = temp; 104 } 105 } 106 }