GFXprim
2D bitmap graphics library with emphasis on speed and correctness
Loading...
Searching...
No Matches
gp_get_set_bits.h
Go to the documentation of this file.
1// SPDX-License-Identifier: LGPL-2.1-or-later
2/*
3 * Copyright (C) 2011 Tomas Gavenciak <gavento@ucw.cz>
4 * Copyright (C) 2011-2025 Cyril Hrubis <metan@ucw.cz>
5 */
6
24#ifndef CORE_GP_GET_SET_BITS_H
25#define CORE_GP_GET_SET_BITS_H
26
36#define GP_GET_BITS(offset, len, val) \
37 (sizeof(val) * 8 <= len ? \
38 (val)>>(offset) : \
39 ((val)>>(offset)) & (((((typeof(val))1)<<(len)) - 1)))
40
50#define GP_GET_BITS4_LE(offset, len, buf) ({ \
51 uint32_t v; \
52 v = ((uint8_t *)buf)[0]; \
53 v |= ((uint8_t *)buf)[1]<<8; \
54 v |= ((uint8_t *)buf)[2]<<16; \
55 v |= ((uint8_t *)buf)[3]<<24; \
56 \
57 GP_GET_BITS(offset, len, v); \
58})
59
69#define GP_GET_BITS4_BE(offset, len, buf) ({ \
70 uint32_t v; \
71 v = ((uint8_t *)buf)[3]; \
72 v |= ((uint8_t *)buf)[2]<<8; \
73 v |= ((uint8_t *)buf)[1]<<16; \
74 v |= ((uint8_t *)buf)[0]<<24; \
75 \
76 GP_GET_BITS(offset, len, v); \
77})
78
88#define GP_GET_BITS3_LE(offset, len, buf) ({ \
89 uint32_t v; \
90 v = ((uint8_t *)buf)[0]; \
91 v |= ((uint8_t *)buf)[1]<<8; \
92 v |= ((uint8_t *)buf)[2]<<16; \
93 \
94 GP_GET_BITS(offset, len, v); \
95})
96
106#define GP_GET_BITS3_BE(offset, len, buf) ({ \
107 uint32_t v; \
108 v = ((uint8_t *)buf)[2]; \
109 v |= ((uint8_t *)buf)[1]<<8; \
110 v |= ((uint8_t *)buf)[0]<<16; \
111 \
112 GP_GET_BITS(offset, len, v); \
113})
114
124#define GP_GET_BITS2_LE(offset, len, buf) ({ \
125 uint16_t v; \
126 v = ((uint8_t *)buf)[0]; \
127 v |= ((uint8_t *)buf)[1]<<8; \
128 \
129 GP_GET_BITS(offset, len, v); \
130})
131
141#define GP_GET_BITS2_BE(offset, len, buf) ({ \
142 uint16_t v; \
143 v = ((uint8_t *)buf)[1]; \
144 v |= ((uint8_t *)buf)[0]<<8; \
145 \
146 GP_GET_BITS(offset, len, v); \
147})
148
158#define GP_GET_BITS1(offset, len, buf) ({ \
159 uint8_t v; \
160 v = ((uint8_t *)buf)[0]; \
161 \
162 GP_GET_BITS(offset, len, v); \
163})
164
175#define GP_CLEAR_BITS(offset, len, dest) \
176 ((dest) &= ~(((((typeof(dest))1) << (len)) - 1) << (offset)))
177
189#define GP_SET_BITS(offset, len, dest, val) do { \
190 GP_CLEAR_BITS(offset, len, dest); \
191 ((dest) |= ((val)<<(offset))); \
192} while (0)
193
205#define GP_SET_BITS1(offset, len, dest, val) do { \
206 uint8_t v = ((uint8_t *)dest)[0]; \
207 GP_SET_BITS(offset, len, v, val); \
208 ((uint8_t *)dest)[0] = v; \
209} while (0)
210
222#define GP_SET_BITS2_LE(offset, len, dest, val) do { \
223 uint16_t v; \
224 v = ((uint8_t *)dest)[0]; \
225 v |= ((uint8_t *)dest)[1]<<8; \
226 \
227 GP_SET_BITS(offset, len, v, val); \
228 \
229 ((uint8_t *)dest)[0] = 0xff & v; \
230 ((uint8_t *)dest)[1] = 0xff & (v >> 8); \
231} while (0)
232
244#define GP_SET_BITS2_BE(offset, len, dest, val) do { \
245 uint16_t v; \
246 v = ((uint8_t *)dest)[1]; \
247 v |= ((uint8_t *)dest)[0]<<8; \
248 \
249 GP_SET_BITS(offset, len, v, val); \
250 \
251 ((uint8_t *)dest)[1] = 0xff & v; \
252 ((uint8_t *)dest)[0] = 0xff & (v >> 8); \
253} while (0)
254
266#define GP_SET_BITS3_LE(offset, len, dest, val) do { \
267 uint32_t v; \
268 v = ((uint8_t *)dest)[0]; \
269 v |= ((uint8_t *)dest)[1]<<8; \
270 v |= ((uint8_t *)dest)[2]<<16; \
271 \
272 GP_SET_BITS(offset, len, v, val); \
273 \
274 ((uint8_t *)dest)[0] = 0xff & v; \
275 ((uint8_t *)dest)[1] = 0xff & (v >> 8); \
276 ((uint8_t *)dest)[2] = 0xff & (v >> 16); \
277} while (0)
278
290#define GP_SET_BITS3_BE(offset, len, dest, val) do { \
291 uint32_t v; \
292 v = ((uint8_t *)dest)[2]; \
293 v |= ((uint8_t *)dest)[1]<<8; \
294 v |= ((uint8_t *)dest)[0]<<16; \
295 \
296 GP_SET_BITS(offset, len, v, val); \
297 \
298 ((uint8_t *)dest)[2] = 0xff & v; \
299 ((uint8_t *)dest)[1] = 0xff & (v >> 8); \
300 ((uint8_t *)dest)[0] = 0xff & (v >> 16); \
301} while (0)
302
314#define GP_SET_BITS4_LE(offset, len, dest, val) do { \
315 uint32_t v; \
316 v = ((uint8_t *)dest)[0]; \
317 v |= ((uint8_t *)dest)[1]<<8; \
318 v |= ((uint8_t *)dest)[2]<<16; \
319 v |= ((uint8_t *)dest)[3]<<24; \
320 \
321 GP_SET_BITS(offset, len, v, val); \
322 \
323 ((uint8_t *)dest)[0] = 0xff & v; \
324 ((uint8_t *)dest)[1] = 0xff & (v >> 8); \
325 ((uint8_t *)dest)[2] = 0xff & (v >> 16); \
326 ((uint8_t *)dest)[3] = 0xff & (v >> 24); \
327} while (0)
328
340#define GP_SET_BITS4_BE(offset, len, dest, val) do { \
341 uint32_t v; \
342 v = ((uint8_t *)dest)[3]; \
343 v |= ((uint8_t *)dest)[2]<<8; \
344 v |= ((uint8_t *)dest)[1]<<16; \
345 v |= ((uint8_t *)dest)[0]<<24; \
346 \
347 GP_SET_BITS(offset, len, v, val); \
348 \
349 ((uint8_t *)dest)[3] = 0xff & v; \
350 ((uint8_t *)dest)[2] = 0xff & (v >> 8); \
351 ((uint8_t *)dest)[1] = 0xff & (v >> 16); \
352 ((uint8_t *)dest)[0] = 0xff & (v >> 24); \
353} while (0)
354
355#endif /* CORE_GP_GET_SET_BITS_H */