1
0
mirror of https://github.com/RIOT-OS/RIOT.git synced 2024-12-29 04:50:03 +01:00
RIOT/sys/include/bitfield.h
Martine Lenders a6623f834f bitfield: unify order
Currently the bitfield type mixes up the order of bits: While the byte
order is big-endian (most significant byte first), the bit order of each
single byte within the bitfield is little-endian (most significant bit
last). While this isn't a problem for most applications of the bitfield
type it becomes one when sending the bitfield over the network (as done
e.g. in the [ACKs of Selective Fragment Recovery][SFR-ACKs]).

This change unifies byte order and bit order to both be most
significant bX first.

[SFR-ACKs]: https://tools.ietf.org/html/draft-ietf-6lo-fragment-recovery-07
2019-11-20 18:53:11 +01:00

111 lines
2.5 KiB
C

/*
* Copyright (C) 2015 INRIA
*
* This file is subject to the terms and conditions of the GNU Lesser
* General Public License v2.1. See the file LICENSE in the top level
* directory for more details.
*/
/**
* @defgroup sys_bitfield Bitfields
* @ingroup sys
* @brief Bitfields of arbitrary length
*
* The bitfields in this module have their most significant bytes first and
* their most significant bits within each byte of the bitfield also first.
*
* @file
* @{
*
* @brief bitfields operations on bitfields of arbitrary length
*
* @note Code taken mostly from
* <a href="http://stackoverflow.com/questions/1590893/error-trying-to-define-a-1-024-bit-128-byte-bit-field">
* Stackoverflow, User Christoph</a>
*
* @author Oliver Hahm <oliver.hahm@inria.fr>
*/
#ifndef BITFIELD_H
#define BITFIELD_H
#include <stdint.h>
#include <stdbool.h>
#include <stddef.h>
#ifdef __cplusplus
extern "C" {
#endif
/**
* @brief Declare a bitfield of a given size
*
* @note SIZE should be a constant expression. This avoids variable length
* arrays.
*/
#define BITFIELD(NAME, SIZE) uint8_t NAME[((SIZE) + 7) / 8]
/**
* @brief Set the bit to 1
*
* @param[in,out] field The bitfield
* @param[in] idx The number of the bit to set
*/
static inline void bf_set(uint8_t field[], size_t idx)
{
field[idx / 8] |= (1u << (7 - (idx % 8)));
}
/**
* @brief Clear the bit
*
* @param[in,out] field The bitfield
* @param[in] idx The number of the bit to clear
*/
static inline void bf_unset(uint8_t field[], size_t idx)
{
field[idx / 8] &= ~(1u << (7 - (idx % 8)));
}
/**
* @brief Toggle the bit
*
* @param[in,out] field The bitfield
* @param[in] idx The number of the bit to toggle
*/
static inline void bf_toggle(uint8_t field[], size_t idx)
{
field[idx / 8] ^= (1u << (7 - (idx % 8)));
}
/**
* @brief Check if the bet is set
*
* @param[in,out] field The bitfield
* @param[in] idx The number of the bit to check
*/
static inline bool bf_isset(uint8_t field[], size_t idx)
{
return (field[idx / 8] & (1u << (7 - (idx % 8))));
}
/**
* @brief Atomically get the number of an unset bit and set it
*
* This function can be used to record e.g., empty entries in an array.
*
* @param[in,out] field The bitfield
* @param[in] size The size of the bitfield
*
* @return number of bit that was set
* @return -1 if no bit was unset
*/
int bf_get_unset(uint8_t field[], int size);
#ifdef __cplusplus
}
#endif
/** @} */
#endif /* BITFIELD_H */