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