GFXprim
2D bitmap graphics library with emphasis on speed and correctness
Loading...
Searching...
No Matches
Macros
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_ALIGNED(offset, len, buf)
 Align-safe get bits.
 
#define GP_GET_BITS3_ALIGNED(offset, len, buf)
 Align-safe get bits.
 
#define GP_GET_BITS2_ALIGNED(offset, len, buf)
 Align-safe get bits.
 
#define GP_GET_BITS1_ALIGNED(offset, len, buf)
 Align-safe get bits.
 
#define GP_CLEAR_BITS(offset, len, dest)    ((dest) &= ~(((((typeof(dest))1) << (len)) - 1) << (offset)))
 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_ALIGNED(offset, len, dest, val)
 Align-safe set bits.
 
#define GP_SET_BITS2_ALIGNED(offset, len, dest, val)
 Align-safe set bits.
 
#define GP_SET_BITS3_ALIGNED(offset, len, dest, val)
 Align-safe set bits.
 
#define GP_SET_BITS4_ALIGNED(offset, len, dest, val)
 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).

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, as we need to touch minimal number of bytes needed.

Definition in file gp_get_set_bits.h.

Macro Definition Documentation

◆ GP_CLEAR_BITS

#define GP_CLEAR_BITS (   offset,
  len,
  dest 
)     ((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 119 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 34 of file gp_get_set_bits.h.

◆ GP_GET_BITS1_ALIGNED

#define GP_GET_BITS1_ALIGNED (   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 102 of file gp_get_set_bits.h.

◆ GP_GET_BITS2_ALIGNED

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

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 85 of file gp_get_set_bits.h.

◆ GP_GET_BITS3_ALIGNED

#define GP_GET_BITS3_ALIGNED (   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); \
})

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 67 of file gp_get_set_bits.h.

◆ GP_GET_BITS4_ALIGNED

#define GP_GET_BITS4_ALIGNED (   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); \
})

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 48 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 133 of file gp_get_set_bits.h.

◆ GP_SET_BITS1_ALIGNED

#define GP_SET_BITS1_ALIGNED (   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 149 of file gp_get_set_bits.h.

◆ GP_SET_BITS2_ALIGNED

#define GP_SET_BITS2_ALIGNED (   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)

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 166 of file gp_get_set_bits.h.

◆ GP_SET_BITS3_ALIGNED

#define GP_SET_BITS3_ALIGNED (   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)

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 188 of file gp_get_set_bits.h.

◆ GP_SET_BITS4_ALIGNED

#define GP_SET_BITS4_ALIGNED (   offset,
  len,
  dest,
  val 
)
Value:
do { \
uint32_t v; \
v = ((uint8_t *)dest)[0]; \
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)

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 212 of file gp_get_set_bits.h.