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