diff options
author | Sylvain Munaut <tnt@246tNt.com> | 2010-10-24 13:01:56 +0200 |
---|---|---|
committer | Sylvain Munaut <tnt@246tNt.com> | 2010-10-29 11:49:33 +0200 |
commit | 294db60a4e51568649d7bfccda22dd8ffa0e3f81 (patch) | |
tree | f39d62030dc8b33158c49856a3f5ed8a8dc1af43 /include | |
parent | 96b1c3b2b444b7d9c3105c22352cb4415d8e2086 (diff) |
utils: Add various bit manipulation related utilites
Signed-off-by: Sylvain Munaut <tnt@246tNt.com>
Diffstat (limited to 'include')
-rw-r--r-- | include/Makefile.am | 1 | ||||
-rw-r--r-- | include/gapk/Makefile.am | 1 | ||||
-rw-r--r-- | include/gapk/utils.h | 104 |
3 files changed, 106 insertions, 0 deletions
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 <http://www.gnu.org/licenses/>. + */ + +#ifndef __GAPK_UTILS_H__ +#define __GAPK_UTILS_H__ + +#include <stdint.h> + +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__ */ |