FastLED 3.6.0
Loading...
Searching...
No Matches
bitswap.h
Go to the documentation of this file.
1#ifndef __INC_BITSWAP_H
2#define __INC_BITSWAP_H
3
4#include "FastLED.h"
5
8
9FASTLED_NAMESPACE_BEGIN
10
11
12#if defined(FASTLED_ARM) || defined(FASTLED_ESP8266) || defined(FASTLED_DOXYGEN)
14typedef union {
15 uint8_t raw;
16 struct {
17 uint32_t a0:1;
18 uint32_t a1:1;
19 uint32_t a2:1;
20 uint32_t a3:1;
21 uint32_t a4:1;
22 uint32_t a5:1;
23 uint32_t a6:1;
24 uint32_t a7:1;
25 };
26} just8bits;
27
29typedef struct {
30 uint32_t a0:1;
31 uint32_t a1:1;
32 uint32_t a2:1;
33 uint32_t a3:1;
34 uint32_t a4:1;
35 uint32_t a5:1;
36 uint32_t a6:1;
37 uint32_t a7:1;
38 uint32_t b0:1;
39 uint32_t b1:1;
40 uint32_t b2:1;
41 uint32_t b3:1;
42 uint32_t b4:1;
43 uint32_t b5:1;
44 uint32_t b6:1;
45 uint32_t b7:1;
46 uint32_t c0:1;
47 uint32_t c1:1;
48 uint32_t c2:1;
49 uint32_t c3:1;
50 uint32_t c4:1;
51 uint32_t c5:1;
52 uint32_t c6:1;
53 uint32_t c7:1;
54 uint32_t d0:1;
55 uint32_t d1:1;
56 uint32_t d2:1;
57 uint32_t d3:1;
58 uint32_t d4:1;
59 uint32_t d5:1;
60 uint32_t d6:1;
61 uint32_t d7:1;
62} sub4;
63
65typedef union {
66 uint32_t word[2];
67 uint8_t bytes[8];
68 struct {
71 };
73
74
80#define SWAPSA(X,N) out. X ## 0 = in.a.a ## N; \
81 out. X ## 1 = in.a.b ## N; \
82 out. X ## 2 = in.a.c ## N; \
83 out. X ## 3 = in.a.d ## N;
84
90#define SWAPSB(X,N) out. X ## 0 = in.b.a ## N; \
91 out. X ## 1 = in.b.b ## N; \
92 out. X ## 2 = in.b.c ## N; \
93 out. X ## 3 = in.b.d ## N;
94
100#define SWAPS(X,N) out. X ## 0 = in.a.a ## N; \
101 out. X ## 1 = in.a.b ## N; \
102 out. X ## 2 = in.a.c ## N; \
103 out. X ## 3 = in.a.d ## N; \
104 out. X ## 4 = in.b.a ## N; \
105 out. X ## 5 = in.b.b ## N; \
106 out. X ## 6 = in.b.c ## N; \
107 out. X ## 7 = in.b.d ## N;
108
109
111__attribute__((always_inline)) inline void swapbits8(bitswap_type in, bitswap_type & out) {
112
113 // SWAPS(a.a,7);
114 // SWAPS(a.b,6);
115 // SWAPS(a.c,5);
116 // SWAPS(a.d,4);
117 // SWAPS(b.a,3);
118 // SWAPS(b.b,2);
119 // SWAPS(b.c,1);
120 // SWAPS(b.d,0);
121
122 // SWAPSA(a.a,7);
123 // SWAPSA(a.b,6);
124 // SWAPSA(a.c,5);
125 // SWAPSA(a.d,4);
126 //
127 // SWAPSB(a.a,7);
128 // SWAPSB(a.b,6);
129 // SWAPSB(a.c,5);
130 // SWAPSB(a.d,4);
131 //
132 // SWAPSA(b.a,3);
133 // SWAPSA(b.b,2);
134 // SWAPSA(b.c,1);
135 // SWAPSA(b.d,0);
136 // //
137 // SWAPSB(b.a,3);
138 // SWAPSB(b.b,2);
139 // SWAPSB(b.c,1);
140 // SWAPSB(b.d,0);
141
142 for(int i = 0; i < 8; ++i) {
143 just8bits work;
144 work.a3 = in.word[0] >> 31;
145 work.a2 = in.word[0] >> 23;
146 work.a1 = in.word[0] >> 15;
147 work.a0 = in.word[0] >> 7;
148 in.word[0] <<= 1;
149 work.a7 = in.word[1] >> 31;
150 work.a6 = in.word[1] >> 23;
151 work.a5 = in.word[1] >> 15;
152 work.a4 = in.word[1] >> 7;
153 in.word[1] <<= 1;
154 out.bytes[i] = work.raw;
155 }
156}
157
159__attribute__((always_inline)) inline void slowswap(unsigned char *A, unsigned char *B) {
160
161 for(int row = 0; row < 7; ++row) {
162 uint8_t x = A[row];
163
164 uint8_t bit = (1<<row);
165 unsigned char *p = B;
166 for(uint32_t mask = 1<<7 ; mask ; mask >>= 1) {
167 if(x & mask) {
168 *p++ |= bit;
169 } else {
170 *p++ &= ~bit;
171 }
172 }
173 // B[7] |= (x & 0x01) << row; x >>= 1;
174 // B[6] |= (x & 0x01) << row; x >>= 1;
175 // B[5] |= (x & 0x01) << row; x >>= 1;
176 // B[4] |= (x & 0x01) << row; x >>= 1;
177 // B[3] |= (x & 0x01) << row; x >>= 1;
178 // B[2] |= (x & 0x01) << row; x >>= 1;
179 // B[1] |= (x & 0x01) << row; x >>= 1;
180 // B[0] |= (x & 0x01) << row; x >>= 1;
181 }
182}
183
187void transpose8x1_noinline(unsigned char *A, unsigned char *B);
188
190__attribute__((always_inline)) inline void transpose8x1(unsigned char *A, unsigned char *B) {
191 uint32_t x, y, t;
192
193 // Load the array and pack it into x and y.
194 y = *(unsigned int*)(A);
195 x = *(unsigned int*)(A+4);
196
197 // pre-transform x
198 t = (x ^ (x >> 7)) & 0x00AA00AA; x = x ^ t ^ (t << 7);
199 t = (x ^ (x >>14)) & 0x0000CCCC; x = x ^ t ^ (t <<14);
200
201 // pre-transform y
202 t = (y ^ (y >> 7)) & 0x00AA00AA; y = y ^ t ^ (t << 7);
203 t = (y ^ (y >>14)) & 0x0000CCCC; y = y ^ t ^ (t <<14);
204
205 // final transform
206 t = (x & 0xF0F0F0F0) | ((y >> 4) & 0x0F0F0F0F);
207 y = ((x << 4) & 0xF0F0F0F0) | (y & 0x0F0F0F0F);
208 x = t;
209
210 *((uint32_t*)B) = y;
211 *((uint32_t*)(B+4)) = x;
212}
213
216__attribute__((always_inline)) inline void transpose8x1_MSB(unsigned char *A, unsigned char *B) {
217 uint32_t x, y, t;
218
219 // Load the array and pack it into x and y.
220 y = *(unsigned int*)(A);
221 x = *(unsigned int*)(A+4);
222
223 // pre-transform x
224 t = (x ^ (x >> 7)) & 0x00AA00AA; x = x ^ t ^ (t << 7);
225 t = (x ^ (x >>14)) & 0x0000CCCC; x = x ^ t ^ (t <<14);
226
227 // pre-transform y
228 t = (y ^ (y >> 7)) & 0x00AA00AA; y = y ^ t ^ (t << 7);
229 t = (y ^ (y >>14)) & 0x0000CCCC; y = y ^ t ^ (t <<14);
230
231 // final transform
232 t = (x & 0xF0F0F0F0) | ((y >> 4) & 0x0F0F0F0F);
233 y = ((x << 4) & 0xF0F0F0F0) | (y & 0x0F0F0F0F);
234 x = t;
235
236 B[7] = y; y >>= 8;
237 B[6] = y; y >>= 8;
238 B[5] = y; y >>= 8;
239 B[4] = y;
240
241 B[3] = x; x >>= 8;
242 B[2] = x; x >>= 8;
243 B[1] = x; x >>= 8;
244 B[0] = x; /* */
245}
246
249template<int m, int n>
250__attribute__((always_inline)) inline void transpose8(unsigned char *A, unsigned char *B) {
251 uint32_t x, y, t;
252
253 // Load the array and pack it into x and y.
254 if(m == 1) {
255 y = *(unsigned int*)(A);
256 x = *(unsigned int*)(A+4);
257 } else {
258 x = (A[0]<<24) | (A[m]<<16) | (A[2*m]<<8) | A[3*m];
259 y = (A[4*m]<<24) | (A[5*m]<<16) | (A[6*m]<<8) | A[7*m];
260 }
261
262 // pre-transform x
263 t = (x ^ (x >> 7)) & 0x00AA00AA; x = x ^ t ^ (t << 7);
264 t = (x ^ (x >>14)) & 0x0000CCCC; x = x ^ t ^ (t <<14);
265
266 // pre-transform y
267 t = (y ^ (y >> 7)) & 0x00AA00AA; y = y ^ t ^ (t << 7);
268 t = (y ^ (y >>14)) & 0x0000CCCC; y = y ^ t ^ (t <<14);
269
270 // final transform
271 t = (x & 0xF0F0F0F0) | ((y >> 4) & 0x0F0F0F0F);
272 y = ((x << 4) & 0xF0F0F0F0) | (y & 0x0F0F0F0F);
273 x = t;
274
275 B[7*n] = y; y >>= 8;
276 B[6*n] = y; y >>= 8;
277 B[5*n] = y; y >>= 8;
278 B[4*n] = y;
279
280 B[3*n] = x; x >>= 8;
281 B[2*n] = x; x >>= 8;
282 B[n] = x; x >>= 8;
283 B[0] = x;
284 // B[0]=x>>24; B[n]=x>>16; B[2*n]=x>>8; B[3*n]=x>>0;
285 // B[4*n]=y>>24; B[5*n]=y>>16; B[6*n]=y>>8; B[7*n]=y>>0;
286}
287
288#endif
289
290FASTLED_NAMESPACE_END
291
292#endif
central include file for FastLED, defines the CFastLED class/object
void transpose8x1_MSB(unsigned char *A, unsigned char *B)
Simplified form of bits rotating function.
Definition bitswap.h:216
void slowswap(unsigned char *A, unsigned char *B)
Slow version of the 8 byte by 8 bit rotation.
Definition bitswap.h:159
void transpose8(unsigned char *A, unsigned char *B)
Templated bit-rotating function.
Definition bitswap.h:250
void transpose8x1_noinline(unsigned char *A, unsigned char *B)
Simplified form of bits rotating function.
Definition bitswap.cpp:9
void swapbits8(bitswap_type in, bitswap_type &out)
Do an 8-byte by 8-bit rotation.
Definition bitswap.h:111
void transpose8x1(unsigned char *A, unsigned char *B)
Simplified form of bits rotating function.
Definition bitswap.h:190
Structure representing 32 bits of access.
Definition bitswap.h:29
uint32_t c3
byte 'c', bit 3 (0x00080000)
Definition bitswap.h:49
uint32_t d6
byte 'd', bit 6 (0x40000000)
Definition bitswap.h:60
uint32_t b6
byte 'b', bit 6 (0x00004000)
Definition bitswap.h:44
uint32_t c4
byte 'c', bit 4 (0x00100000)
Definition bitswap.h:50
uint32_t c6
byte 'c', bit 6 (0x00400000)
Definition bitswap.h:52
uint32_t a4
byte 'a', bit 4 (0x00000010)
Definition bitswap.h:34
uint32_t c1
byte 'c', bit 1 (0x00020000)
Definition bitswap.h:47
uint32_t d7
byte 'd', bit 7 (0x80000000)
Definition bitswap.h:61
uint32_t a3
byte 'a', bit 3 (0x00000008)
Definition bitswap.h:33
uint32_t a7
byte 'a', bit 7 (0x00000080)
Definition bitswap.h:37
uint32_t d5
byte 'd', bit 5 (0x20000000)
Definition bitswap.h:59
uint32_t a5
byte 'a', bit 5 (0x00000020)
Definition bitswap.h:35
uint32_t b0
byte 'b', bit 0 (0x00000100)
Definition bitswap.h:38
uint32_t c7
byte 'c', bit 7 (0x00800000)
Definition bitswap.h:53
uint32_t b3
byte 'b', bit 3 (0x00000800)
Definition bitswap.h:41
uint32_t d1
byte 'd', bit 1 (0x02000000)
Definition bitswap.h:55
uint32_t a1
byte 'a', bit 1 (0x00000002)
Definition bitswap.h:31
uint32_t b1
byte 'b', bit 1 (0x00000200)
Definition bitswap.h:39
uint32_t b5
byte 'b', bit 5 (0x00002000)
Definition bitswap.h:43
uint32_t d0
byte 'd', bit 0 (0x01000000)
Definition bitswap.h:54
uint32_t b7
byte 'b', bit 7 (0x00008000)
Definition bitswap.h:45
uint32_t b2
byte 'b', bit 2 (0x00000400)
Definition bitswap.h:40
uint32_t a2
byte 'a', bit 2 (0x00000004)
Definition bitswap.h:32
uint32_t a0
byte 'a', bit 0 (0x00000000)
Definition bitswap.h:30
uint32_t d2
byte 'd', bit 2 (0x04000000)
Definition bitswap.h:56
uint32_t c5
byte 'c', bit 5 (0x00200000)
Definition bitswap.h:51
uint32_t b4
byte 'b', bit 4 (0x00001000)
Definition bitswap.h:42
uint32_t d3
byte 'd', bit 3 (0x08000000)
Definition bitswap.h:57
uint32_t a6
byte 'a', bit 6 (0x00000040)
Definition bitswap.h:36
uint32_t c0
byte 'c', bit 0 (0x00010000)
Definition bitswap.h:46
uint32_t d4
byte 'd', bit 4 (0x10000000)
Definition bitswap.h:58
uint32_t c2
byte 'c', bit 2 (0x00040000)
Definition bitswap.h:48
Union containing a full 8 bytes to swap the bit orientation on.
Definition bitswap.h:65
sub4 b
32-bit access struct for bit swapping, lower four bytes (word[1] or bytes[4-7])
Definition bitswap.h:70
uint32_t word[2]
two 32-bit values to load for swapping
Definition bitswap.h:66
uint8_t bytes[8]
eight 8-bit values to load for swapping
Definition bitswap.h:67
sub4 a
32-bit access struct for bit swapping, upper four bytes (word[0] or bytes[0-3])
Definition bitswap.h:69
Structure representing 8 bits of access.
Definition bitswap.h:14
uint32_t a5
bit 5 (0x20)
Definition bitswap.h:22
uint32_t a0
bit 0 (0x01)
Definition bitswap.h:17
uint32_t a4
bit 4 (0x10)
Definition bitswap.h:21
uint32_t a3
bit 3 (0x08)
Definition bitswap.h:20
uint32_t a6
bit 6 (0x40)
Definition bitswap.h:23
uint8_t raw
the entire byte
Definition bitswap.h:15
uint32_t a2
bit 2 (0x04)
Definition bitswap.h:19
uint32_t a7
bit 7 (0x80)
Definition bitswap.h:24
uint32_t a1
bit 1 (0x02)
Definition bitswap.h:18