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