GFXprim
2D bitmap graphics library with emphasis on speed and correctness
Loading...
Searching...
No Matches
gp_get_set_bits.h File Reference

Helper macros to get and set bits given offset and length. More...

Go to the source code of this file.

Macros

#define GP_GET_BITS(offset, len, val)
 Helper macros to read parts of words.
 
#define GP_GET_BITS4_LE(offset, len, buf)
 Little endian align-safe get bits.
 
#define GP_GET_BITS4_BE(offset, len, buf)
 Big endian align-safe get bits.
 
#define GP_GET_BITS3_LE(offset, len, buf)
 Little endian align-safe get bits.
 
#define GP_GET_BITS3_BE(offset, len, buf)
 Big endian align-safe get bits.
 
#define GP_GET_BITS2_LE(offset, len, buf)
 Little endian align-safe get bits.
 
#define GP_GET_BITS2_BE(offset, len, buf)
 Big endian align-safe get bits.
 
#define GP_GET_BITS1(offset, len, buf)
 Align-safe get bits.
 
#define GP_CLEAR_BITS(offset, len, dest)
 Clear len bits at offset in dest.
 
#define GP_SET_BITS(offset, len, dest, val)
 Set len bits at offset in dest from val.
 
#define GP_SET_BITS1(offset, len, dest, val)
 Align-safe set bits.
 
#define GP_SET_BITS2_LE(offset, len, dest, val)
 Little endian align-safe set bits.
 
#define GP_SET_BITS2_BE(offset, len, dest, val)
 Big endian align-safe set bits.
 
#define GP_SET_BITS3_LE(offset, len, dest, val)
 Little endian align-safe set bits.
 
#define GP_SET_BITS3_BE(offset, len, dest, val)
 Big endian align-safe set bits.
 
#define GP_SET_BITS4_LE(offset, len, dest, val)
 Little endian align-safe set bits.
 
#define GP_SET_BITS4_BE(offset, len, dest, val)
 Big endian align-safe set bits.
 

Detailed Description

Helper macros to get and set bits given offset and length.

The GP_GET_BITS() and GP_SET_BITS() works ONLY on aligned data types. Which means that you can only pass value that is suitably aligned for it's type, for example passing an 32 bit integer is OK, passing a char buffer casted to 32 bit integer is not (unless you made sure that the start address is multiple of 4). These macros also work with the machine endianity if pixels are packed in foreign endianity, the data has to be swapped properly at the input or output.

The align-safe variants first gets the value from a buffer, byte by byte and then uses the GP_GET_BITS() or GP_SET_BITS(). The number in their name tells how much bytes are touched and the endianity suffix describes the endianity these macros work with.

Definition in file gp_get_set_bits.h.

Macro Definition Documentation

◆ GP_CLEAR_BITS

#define GP_CLEAR_BITS ( offset,
len,
dest )
Value:
((dest) &= ~(((((typeof(dest))1) << (len)) - 1) << (offset)))

Clear len bits at offset in dest.

Operates on 8, 16, and 32 bit values, depending on the type of dest, which has to be unsigned.

Parameters
offsetNumber of bits to keep on the left side.
lenNumber of bits to clear.
destA value to operate on.

Definition at line 175 of file gp_get_set_bits.h.

◆ GP_GET_BITS

#define GP_GET_BITS ( offset,
len,
val )
Value:
(sizeof(val) * 8 <= len ? \
(val)>>(offset) : \
((val)>>(offset)) & (((((typeof(val))1)<<(len)) - 1)))

Helper macros to read parts of words.

Parameters
offsetHow much to shift the val left.
lenHow much bits from val should we keep.
valA value to be shifted and masked.
Returns
Shifted lenght bits at offset of value.

Definition at line 36 of file gp_get_set_bits.h.

◆ GP_GET_BITS1

#define GP_GET_BITS1 ( offset,
len,
buf )
Value:
({ \
uint8_t v; \
v = ((uint8_t *)buf)[0]; \
\
GP_GET_BITS(offset, len, v); \
})

Align-safe get bits.

Reads byte then shifts and masks it.

Parameters
offsetNumber of bits to shift to left.
lenA number of bits to return.
bufA pointer to a starting byte of the buffer.

Definition at line 158 of file gp_get_set_bits.h.

Referenced by gp_getpixel_raw_1BPP_DB(), gp_getpixel_raw_1BPP_UB(), gp_getpixel_raw_2BPP_DB(), gp_getpixel_raw_2BPP_UB(), gp_getpixel_raw_4BPP_DB(), and gp_getpixel_raw_4BPP_UB().

◆ GP_GET_BITS2_BE

#define GP_GET_BITS2_BE ( offset,
len,
buf )
Value:
({ \
uint16_t v; \
v = ((uint8_t *)buf)[1]; \
v |= ((uint8_t *)buf)[0]<<8; \
\
GP_GET_BITS(offset, len, v); \
})

Big endian align-safe get bits.

Reads two bytes byte by byte, composes then shifts and masks them.

Parameters
offsetNumber of bits to shift to left.
lenA number of bits to return.
bufA pointer to a starting byte of the buffer.

Definition at line 141 of file gp_get_set_bits.h.

◆ GP_GET_BITS2_LE

#define GP_GET_BITS2_LE ( offset,
len,
buf )
Value:
({ \
uint16_t v; \
v = ((uint8_t *)buf)[0]; \
v |= ((uint8_t *)buf)[1]<<8; \
\
GP_GET_BITS(offset, len, v); \
})

Little endian align-safe get bits.

Reads two bytes byte by byte, composes then shifts and masks them.

Parameters
offsetNumber of bits to shift to left.
lenA number of bits to return.
bufA pointer to a starting byte of the buffer.

Definition at line 124 of file gp_get_set_bits.h.

◆ GP_GET_BITS3_BE

#define GP_GET_BITS3_BE ( offset,
len,
buf )
Value:
({ \
uint32_t v; \
v = ((uint8_t *)buf)[2]; \
v |= ((uint8_t *)buf)[1]<<8; \
v |= ((uint8_t *)buf)[0]<<16; \
\
GP_GET_BITS(offset, len, v); \
})

Big endian align-safe get bits.

Reads three bytes byte by byte, composes then shifts and masks them.

Parameters
offsetNumber of bits to shift to left.
lenA number of bits to return.
bufA pointer to a starting byte of the buffer.

Definition at line 106 of file gp_get_set_bits.h.

◆ GP_GET_BITS3_LE

#define GP_GET_BITS3_LE ( offset,
len,
buf )
Value:
({ \
uint32_t v; \
v = ((uint8_t *)buf)[0]; \
v |= ((uint8_t *)buf)[1]<<8; \
v |= ((uint8_t *)buf)[2]<<16; \
\
GP_GET_BITS(offset, len, v); \
})

Little endian align-safe get bits.

Reads three bytes byte by byte, composes then shifts and masks them.

Parameters
offsetNumber of bits to shift to left.
lenA number of bits to return.
bufA pointer to a starting byte of the buffer.

Definition at line 88 of file gp_get_set_bits.h.

Referenced by gp_getpixel_raw_18BPP_DB(), and gp_getpixel_raw_24BPP().

◆ GP_GET_BITS4_BE

#define GP_GET_BITS4_BE ( offset,
len,
buf )
Value:
({ \
uint32_t v; \
v = ((uint8_t *)buf)[3]; \
v |= ((uint8_t *)buf)[2]<<8; \
v |= ((uint8_t *)buf)[1]<<16; \
v |= ((uint8_t *)buf)[0]<<24; \
\
GP_GET_BITS(offset, len, v); \
})

Big endian align-safe get bits.

Reads four bytes byte by byte, composes then shifts and masks them.

Parameters
offsetNumber of bits to shift to left.
lenA number of bits to return.
bufA pointer to a starting byte of the buffer.

Definition at line 69 of file gp_get_set_bits.h.

◆ GP_GET_BITS4_LE

#define GP_GET_BITS4_LE ( offset,
len,
buf )
Value:
({ \
uint32_t v; \
v = ((uint8_t *)buf)[0]; \
v |= ((uint8_t *)buf)[1]<<8; \
v |= ((uint8_t *)buf)[2]<<16; \
v |= ((uint8_t *)buf)[3]<<24; \
\
GP_GET_BITS(offset, len, v); \
})

Little endian align-safe get bits.

Reads four bytes byte by byte, composes then shifts and masks them.

Parameters
offsetNumber of bits to shift to left.
lenA number of bits to return.
bufA pointer to a starting byte of the buffer.

Definition at line 50 of file gp_get_set_bits.h.

◆ GP_SET_BITS

#define GP_SET_BITS ( offset,
len,
dest,
val )
Value:
do { \
GP_CLEAR_BITS(offset, len, dest); \
((dest) |= ((val)<<(offset))); \
} while (0)

Set len bits at offset in dest from val.

Operates on 8, 16, and 32 bit values, depending on the type of dest, which has to be unsigned.

Parameters
offsetNumber of bits to keep on the left side.
lenNumber of bits to clear.
destA value to operate on.
valA value to written to dest.

Definition at line 189 of file gp_get_set_bits.h.

◆ GP_SET_BITS1

#define GP_SET_BITS1 ( offset,
len,
dest,
val )
Value:
do { \
uint8_t v = ((uint8_t *)dest)[0]; \
GP_SET_BITS(offset, len, v, val); \
((uint8_t *)dest)[0] = v; \
} while (0)

Align-safe set bits.

Gets a single byte from dest, combines it with len bytes from value at offset and writes it back.

Parameters
offsetAn offset in the byte.
lenA number of bits to write.
destA buffer to write the data to.
valA value to be written.

Definition at line 205 of file gp_get_set_bits.h.

Referenced by gp_putpixel_raw_1BPP_DB(), gp_putpixel_raw_1BPP_UB(), gp_putpixel_raw_2BPP_DB(), gp_putpixel_raw_2BPP_UB(), gp_putpixel_raw_4BPP_DB(), and gp_putpixel_raw_4BPP_UB().

◆ GP_SET_BITS2_BE

#define GP_SET_BITS2_BE ( offset,
len,
dest,
val )
Value:
do { \
uint16_t v; \
v = ((uint8_t *)dest)[1]; \
v |= ((uint8_t *)dest)[0]<<8; \
\
GP_SET_BITS(offset, len, v, val); \
\
((uint8_t *)dest)[1] = 0xff & v; \
((uint8_t *)dest)[0] = 0xff & (v >> 8); \
} while (0)

Big endian align-safe set bits.

Gets two bytes from dest, combines them with len bytes from value at offset and writes it back.

Parameters
offsetAn offset in the byte.
lenA number of bits to write.
destA buffer to write the data to.
valA value to be written.

Definition at line 244 of file gp_get_set_bits.h.

◆ GP_SET_BITS2_LE

#define GP_SET_BITS2_LE ( offset,
len,
dest,
val )
Value:
do { \
uint16_t v; \
v = ((uint8_t *)dest)[0]; \
v |= ((uint8_t *)dest)[1]<<8; \
\
GP_SET_BITS(offset, len, v, val); \
\
((uint8_t *)dest)[0] = 0xff & v; \
((uint8_t *)dest)[1] = 0xff & (v >> 8); \
} while (0)

Little endian align-safe set bits.

Gets two bytes from dest, combines them with len bytes from value at offset and writes it back.

Parameters
offsetAn offset in the byte.
lenA number of bits to write.
destA buffer to write the data to.
valA value to be written.

Definition at line 222 of file gp_get_set_bits.h.

◆ GP_SET_BITS3_BE

#define GP_SET_BITS3_BE ( offset,
len,
dest,
val )
Value:
do { \
uint32_t v; \
v = ((uint8_t *)dest)[2]; \
v |= ((uint8_t *)dest)[1]<<8; \
v |= ((uint8_t *)dest)[0]<<16; \
\
GP_SET_BITS(offset, len, v, val); \
\
((uint8_t *)dest)[2] = 0xff & v; \
((uint8_t *)dest)[1] = 0xff & (v >> 8); \
((uint8_t *)dest)[0] = 0xff & (v >> 16); \
} while (0)

Big endian align-safe set bits.

Gets three bytes from dest, combines them with len bytes from value at offset and writes it back.

Parameters
offsetAn offset in the byte.
lenA number of bits to write.
destA buffer to write the data to.
valA value to be written.

Definition at line 290 of file gp_get_set_bits.h.

◆ GP_SET_BITS3_LE

#define GP_SET_BITS3_LE ( offset,
len,
dest,
val )
Value:
do { \
uint32_t v; \
v = ((uint8_t *)dest)[0]; \
v |= ((uint8_t *)dest)[1]<<8; \
v |= ((uint8_t *)dest)[2]<<16; \
\
GP_SET_BITS(offset, len, v, val); \
\
((uint8_t *)dest)[0] = 0xff & v; \
((uint8_t *)dest)[1] = 0xff & (v >> 8); \
((uint8_t *)dest)[2] = 0xff & (v >> 16); \
} while (0)

Little endian align-safe set bits.

Gets three bytes from dest, combines them with len bytes from value at offset and writes it back.

Parameters
offsetAn offset in the byte.
lenA number of bits to write.
destA buffer to write the data to.
valA value to be written.

Definition at line 266 of file gp_get_set_bits.h.

Referenced by gp_putpixel_raw_18BPP_DB(), and gp_putpixel_raw_24BPP().

◆ GP_SET_BITS4_BE

#define GP_SET_BITS4_BE ( offset,
len,
dest,
val )
Value:
do { \
uint32_t v; \
v = ((uint8_t *)dest)[3]; \
v |= ((uint8_t *)dest)[2]<<8; \
v |= ((uint8_t *)dest)[1]<<16; \
v |= ((uint8_t *)dest)[0]<<24; \
\
GP_SET_BITS(offset, len, v, val); \
\
((uint8_t *)dest)[3] = 0xff & v; \
((uint8_t *)dest)[2] = 0xff & (v >> 8); \
((uint8_t *)dest)[1] = 0xff & (v >> 16); \
((uint8_t *)dest)[0] = 0xff & (v >> 24); \
} while (0)

Big endian align-safe set bits.

Gets four bytes from dest, combines them with len bytes from value at offset and writes it back.

Parameters
offsetAn offset in the byte.
lenA number of bits to write.
destA buffer to write the data to.
valA value to be written.

Definition at line 340 of file gp_get_set_bits.h.

◆ GP_SET_BITS4_LE

#define GP_SET_BITS4_LE ( offset,
len,
dest,
val )
Value:
do { \
uint32_t v; \
v = ((uint8_t *)dest)[0]; \
v |= ((uint8_t *)dest)[1]<<8; \
v |= ((uint8_t *)dest)[2]<<16; \
v |= ((uint8_t *)dest)[3]<<24; \
\
GP_SET_BITS(offset, len, v, val); \
\
((uint8_t *)dest)[0] = 0xff & v; \
((uint8_t *)dest)[1] = 0xff & (v >> 8); \
((uint8_t *)dest)[2] = 0xff & (v >> 16); \
((uint8_t *)dest)[3] = 0xff & (v >> 24); \
} while (0)

Little endian align-safe set bits.

Gets four bytes from dest, combines them with len bytes from value at offset and writes it back.

Parameters
offsetAn offset in the byte.
lenA number of bits to write.
destA buffer to write the data to.
valA value to be written.

Definition at line 314 of file gp_get_set_bits.h.