GNU Radio Manual and C++ API Reference  3.7.7
The Free & Open Software Radio Ecosystem
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
volk_32u_byteswap.h
Go to the documentation of this file.
1 /* -*- c++ -*- */
2 /*
3  * Copyright 2012, 2014 Free Software Foundation, Inc.
4  *
5  * This file is part of GNU Radio
6  *
7  * GNU Radio is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 3, or (at your option)
10  * any later version.
11  *
12  * GNU Radio is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with GNU Radio; see the file COPYING. If not, write to
19  * the Free Software Foundation, Inc., 51 Franklin Street,
20  * Boston, MA 02110-1301, USA.
21  */
22 
23 /*!
24  * \page volk_32u_byteswap
25  *
26  * \b Overview
27  *
28  * Byteswaps (in-place) an aligned vector of int32_t's.
29  *
30  * <b>Dispatcher Prototype</b>
31  * \code
32  * void volk_32u_byteswap(uint32_t* intsToSwap, unsigned int num_points)
33  * \endcode
34  *
35  * \b Inputs
36  * \li intsToSwap: The vector of data to byte swap.
37  * \li num_points: The number of data points.
38  *
39  * \b Outputs
40  * \li intsToSwap: returns as an in-place calculation.
41  *
42  * \b Example
43  * \code
44  * int N = 10;
45  * unsigned int alignment = volk_get_alignment();
46  *
47  * uint32_t bitstring[] = {0x0, 0x1, 0xf, 0xffffffff,
48  * 0x5a5a5a5a, 0xa5a5a5a5, 0x2a2a2a2a,
49  * 0xffffffff, 0x32, 0x64};
50  * uint32_t hamming_distance = 0;
51  *
52  * printf("byteswap vector =\n");
53  * for(unsigned int ii=0; ii<N; ++ii){
54  * printf(" %.8x\n", bitstring[ii]);
55  * }
56  *
57  * volk_32u_byteswap(bitstring, N);
58  *
59  * printf("byteswapped vector =\n");
60  * for(unsigned int ii=0; ii<N; ++ii){
61  * printf(" %.8x\n", bitstring[ii]);
62  * }
63  * \endcode
64  */
65 
66 #ifndef INCLUDED_volk_32u_byteswap_u_H
67 #define INCLUDED_volk_32u_byteswap_u_H
68 
69 #include <inttypes.h>
70 #include <stdio.h>
71 
72 #ifdef LV_HAVE_SSE2
73 #include <emmintrin.h>
74 
75 static inline void volk_32u_byteswap_u_sse2(uint32_t* intsToSwap, unsigned int num_points){
76  unsigned int number = 0;
77 
78  uint32_t* inputPtr = intsToSwap;
79  __m128i input, byte1, byte2, byte3, byte4, output;
80  __m128i byte2mask = _mm_set1_epi32(0x00FF0000);
81  __m128i byte3mask = _mm_set1_epi32(0x0000FF00);
82 
83  const uint64_t quarterPoints = num_points / 4;
84  for(;number < quarterPoints; number++){
85  // Load the 32t values, increment inputPtr later since we're doing it in-place.
86  input = _mm_loadu_si128((__m128i*)inputPtr);
87  // Do the four shifts
88  byte1 = _mm_slli_epi32(input, 24);
89  byte2 = _mm_slli_epi32(input, 8);
90  byte3 = _mm_srli_epi32(input, 8);
91  byte4 = _mm_srli_epi32(input, 24);
92  // Or bytes together
93  output = _mm_or_si128(byte1, byte4);
94  byte2 = _mm_and_si128(byte2, byte2mask);
95  output = _mm_or_si128(output, byte2);
96  byte3 = _mm_and_si128(byte3, byte3mask);
97  output = _mm_or_si128(output, byte3);
98  // Store the results
99  _mm_storeu_si128((__m128i*)inputPtr, output);
100  inputPtr += 4;
101  }
102 
103  // Byteswap any remaining points:
104  number = quarterPoints*4;
105  for(; number < num_points; number++){
106  uint32_t outputVal = *inputPtr;
107  outputVal = (((outputVal >> 24) & 0xff) | ((outputVal >> 8) & 0x0000ff00) | ((outputVal << 8) & 0x00ff0000) | ((outputVal << 24) & 0xff000000));
108  *inputPtr = outputVal;
109  inputPtr++;
110  }
111 }
112 #endif /* LV_HAVE_SSE2 */
113 
114 
115 #ifdef LV_HAVE_NEON
116 #include <arm_neon.h>
117 
118 static inline void volk_32u_byteswap_neon(uint32_t* intsToSwap, unsigned int num_points){
119  uint32_t* inputPtr = intsToSwap;
120  unsigned int number = 0;
121  unsigned int n8points = num_points / 8;
122 
123  uint8x8x4_t input_table;
124  uint8x8_t int_lookup01, int_lookup23, int_lookup45, int_lookup67;
125  uint8x8_t swapped_int01, swapped_int23, swapped_int45, swapped_int67;
126 
127  /* these magic numbers are used as byte-indeces in the LUT.
128  they are pre-computed to save time. A simple C program
129  can calculate them; for example for lookup01:
130  uint8_t chars[8] = {24, 16, 8, 0, 25, 17, 9, 1};
131  for(ii=0; ii < 8; ++ii) {
132  index += ((uint64_t)(*(chars+ii))) << (ii*8);
133  }
134  */
135  int_lookup01 = vcreate_u8(74609667900706840);
136  int_lookup23 = vcreate_u8(219290013576860186);
137  int_lookup45 = vcreate_u8(363970359253013532);
138  int_lookup67 = vcreate_u8(508650704929166878);
139 
140  for(number = 0; number < n8points; ++number){
141  input_table = vld4_u8((uint8_t*) inputPtr);
142  swapped_int01 = vtbl4_u8(input_table, int_lookup01);
143  swapped_int23 = vtbl4_u8(input_table, int_lookup23);
144  swapped_int45 = vtbl4_u8(input_table, int_lookup45);
145  swapped_int67 = vtbl4_u8(input_table, int_lookup67);
146  vst1_u8((uint8_t*) inputPtr, swapped_int01);
147  vst1_u8((uint8_t*) (inputPtr+2), swapped_int23);
148  vst1_u8((uint8_t*) (inputPtr+4), swapped_int45);
149  vst1_u8((uint8_t*) (inputPtr+6), swapped_int67);
150 
151  inputPtr += 8;
152  }
153 
154  for(number = n8points * 8; number < num_points; ++number){
155  uint32_t output = *inputPtr;
156  output = (((output >> 24) & 0xff) | ((output >> 8) & 0x0000ff00) | ((output << 8) & 0x00ff0000) | ((output << 24) & 0xff000000));
157 
158  *inputPtr = output;
159  inputPtr++;
160  }
161 }
162 #endif /* LV_HAVE_NEON */
163 
164 
165 #ifdef LV_HAVE_GENERIC
166 
167 static inline void volk_32u_byteswap_generic(uint32_t* intsToSwap, unsigned int num_points){
168  uint32_t* inputPtr = intsToSwap;
169 
170  unsigned int point;
171  for(point = 0; point < num_points; point++){
172  uint32_t output = *inputPtr;
173  output = (((output >> 24) & 0xff) | ((output >> 8) & 0x0000ff00) | ((output << 8) & 0x00ff0000) | ((output << 24) & 0xff000000));
174 
175  *inputPtr = output;
176  inputPtr++;
177  }
178 }
179 #endif /* LV_HAVE_GENERIC */
180 
181 
182 #endif /* INCLUDED_volk_32u_byteswap_u_H */
183 #ifndef INCLUDED_volk_32u_byteswap_a_H
184 #define INCLUDED_volk_32u_byteswap_a_H
185 
186 #include <inttypes.h>
187 #include <stdio.h>
188 
189 
190 #ifdef LV_HAVE_SSE2
191 #include <emmintrin.h>
192 
193 
194 static inline void volk_32u_byteswap_a_sse2(uint32_t* intsToSwap, unsigned int num_points){
195  unsigned int number = 0;
196 
197  uint32_t* inputPtr = intsToSwap;
198  __m128i input, byte1, byte2, byte3, byte4, output;
199  __m128i byte2mask = _mm_set1_epi32(0x00FF0000);
200  __m128i byte3mask = _mm_set1_epi32(0x0000FF00);
201 
202  const uint64_t quarterPoints = num_points / 4;
203  for(;number < quarterPoints; number++){
204  // Load the 32t values, increment inputPtr later since we're doing it in-place.
205  input = _mm_load_si128((__m128i*)inputPtr);
206  // Do the four shifts
207  byte1 = _mm_slli_epi32(input, 24);
208  byte2 = _mm_slli_epi32(input, 8);
209  byte3 = _mm_srli_epi32(input, 8);
210  byte4 = _mm_srli_epi32(input, 24);
211  // Or bytes together
212  output = _mm_or_si128(byte1, byte4);
213  byte2 = _mm_and_si128(byte2, byte2mask);
214  output = _mm_or_si128(output, byte2);
215  byte3 = _mm_and_si128(byte3, byte3mask);
216  output = _mm_or_si128(output, byte3);
217  // Store the results
218  _mm_store_si128((__m128i*)inputPtr, output);
219  inputPtr += 4;
220  }
221 
222  // Byteswap any remaining points:
223  number = quarterPoints*4;
224  for(; number < num_points; number++){
225  uint32_t outputVal = *inputPtr;
226  outputVal = (((outputVal >> 24) & 0xff) | ((outputVal >> 8) & 0x0000ff00) | ((outputVal << 8) & 0x00ff0000) | ((outputVal << 24) & 0xff000000));
227  *inputPtr = outputVal;
228  inputPtr++;
229  }
230 }
231 #endif /* LV_HAVE_SSE2 */
232 
233 
234 #ifdef LV_HAVE_GENERIC
235 
236 static inline void volk_32u_byteswap_a_generic(uint32_t* intsToSwap, unsigned int num_points){
237  uint32_t* inputPtr = intsToSwap;
238 
239  unsigned int point;
240  for(point = 0; point < num_points; point++){
241  uint32_t output = *inputPtr;
242  output = (((output >> 24) & 0xff) | ((output >> 8) & 0x0000ff00) | ((output << 8) & 0x00ff0000) | ((output << 24) & 0xff000000));
243 
244  *inputPtr = output;
245  inputPtr++;
246  }
247 }
248 #endif /* LV_HAVE_GENERIC */
249 
250 
251 
252 
253 #endif /* INCLUDED_volk_32u_byteswap_a_H */
unsigned char uint8_t
Definition: stdint.h:78
unsigned int uint32_t
Definition: stdint.h:80
unsigned __int64 uint64_t
Definition: stdint.h:90