From 294db60a4e51568649d7bfccda22dd8ffa0e3f81 Mon Sep 17 00:00:00 2001 From: Sylvain Munaut Date: Sun, 24 Oct 2010 13:01:56 +0200 Subject: utils: Add various bit manipulation related utilites Signed-off-by: Sylvain Munaut --- include/Makefile.am | 1 + include/gapk/Makefile.am | 1 + include/gapk/utils.h | 104 +++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 106 insertions(+) create mode 100644 include/gapk/Makefile.am create mode 100644 include/gapk/utils.h (limited to 'include') diff --git a/include/Makefile.am b/include/Makefile.am index e69de29..b16ce5c 100644 --- a/include/Makefile.am +++ b/include/Makefile.am @@ -0,0 +1 @@ +SUBDIRS = gapk diff --git a/include/gapk/Makefile.am b/include/gapk/Makefile.am new file mode 100644 index 0000000..ef8800a --- /dev/null +++ b/include/gapk/Makefile.am @@ -0,0 +1 @@ +noinst_HEADERS = utils.h diff --git a/include/gapk/utils.h b/include/gapk/utils.h new file mode 100644 index 0000000..3b02049 --- /dev/null +++ b/include/gapk/utils.h @@ -0,0 +1,104 @@ +/* Various helpers */ + +/* + * This file is part of gapk (GSM Audio Pocket Knife). + * + * gapk is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * gapk is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with gapk. If not, see . + */ + +#ifndef __GAPK_UTILS_H__ +#define __GAPK_UTILS_H__ + +#include + +static inline int +msb_get_bit(const uint8_t *buf, int bn) +{ + int pos_byte = bn >> 3; + int pos_bit = 7 - (bn & 7); + + return (buf[pos_byte] >> pos_bit) & 1; +} + +static inline void +msb_put_bit(uint8_t *buf, int bn, int bit) +{ + int pos_byte = bn >> 3; + int pos_bit = 7 - (bn & 7); + + if (bit) + buf[pos_byte] |= (1 << pos_bit); + else + buf[pos_byte] &= ~(1 << pos_bit); +} + +static inline void +msb_set_bit(uint8_t *buf, int bn) +{ + int pos_byte = bn >> 3; + int pos_bit = 7 - (bn & 7); + + buf[pos_byte] |= (1 << pos_bit); +} + +static inline void +msb_clr_bit(uint8_t *buf, int bn) +{ + int pos_byte = bn >> 3; + int pos_bit = 7 - (bn & 7); + + buf[pos_byte] &= ~(1 << pos_bit); +} + + +static inline int +lsb_get_bit(const uint8_t *buf, int bn) +{ + int pos_byte = bn >> 3; + int pos_bit = bn & 7; + + return (buf[pos_byte] >> pos_bit) & 1; +} + +static inline void +lsb_put_bit(uint8_t *buf, int bn, int bit) +{ + int pos_byte = bn >> 3; + int pos_bit = bn & 7; + + if (bit) + buf[pos_byte] |= (1 << pos_bit); + else + buf[pos_byte] &= ~(1 << pos_bit); +} + +static inline void +lsb_set_bit(uint8_t *buf, int bn) +{ + int pos_byte = bn >> 3; + int pos_bit = bn & 7; + + buf[pos_byte] |= (1 << pos_bit); +} + +static inline void +lsb_clr_bit(uint8_t *buf, int bn) +{ + int pos_byte = bn >> 3; + int pos_bit = bn & 7; + + buf[pos_byte] &= ~(1 << pos_bit); +} + +#endif /* __GAPK_UTILS_H__ */ -- cgit v1.2.3