/* A basic helper library for bit-manipulation in streams Preliminary version 3 Copyright (C) 2005 Oivind Gylver (oivindg@gmail.com) This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version. This program 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 Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with this library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ #ifndef __TOOLBOX_H__ #define __TOOLBOX_H__ /* General information All bits are numbered as in a stream, MSB->LSB, 0->n. As in, bit positions 0123 4567 .... ..15 8-bit word (MSB) xxxx xxxx (LSB) | 16-bit word (MSB) xxxx xxxx .... ...x (LSB) Get bits from a stream GETBITS_Sx_Ly_zB(word) x = start bit y = bitlength z = bitsize of word word = 1010 1110 n = GETBITS_S0_L3_8B(word); (get bits 0,1,2 and right-shift them to the lsb edge of n) n -> 0000 0101 Replace bits in a stream PUTBITS_Sx_Ly_zB(dst,src) x = start bit y = bitlength z = bitsize of operation Example pseudo code #1; src = 0000 0001 dst = 0110 1100 PUTBITS_S0_L1_8B(dst,src); dst -> 1110 1100 ^ this bit (number 0) was set Example pseudo code #2; src = 0001 0000 0000 1111 (16 bit) msb = 0110 1111 lsb = 0100 1000 PUTBITS_S3_L13_16B(msb,lsb,src); msb -> 0111 1100 ^ ^^^^ these bits (3-7) were set lsb -> 0000 1111 ^^^^ ^^^^ all bits updated */ #define GETBITS_S0_L1_8B(b) (b >> 7) #define GETBITS_S0_L2_8B(b) (b >> 6) #define GETBITS_S0_L3_8B(b) (b >> 5) #define GETBITS_S0_L4_8B(b) (b >> 4) #define GETBITS_S0_L8_8B(b) (b) #define GETBITS_S1_L1_8B(b) ((b & 0x40) >> 6) #define GETBITS_S1_L2_8B(b) ((b & 0x60) >> 5) #define GETBITS_S2_L2_8B(b) ((b & 0x30) >> 4) #define GETBITS_S2_L5_8B(b) ((b & 0x3e) >> 1) #define GETBITS_S2_L6_8B(b) (b & 0x3f) #define GETBITS_S3_L5_8B(b) (b & 0x1f) #define GETBITS_S7_L1_8B(b) (b & 0x01) #define GETBITS_S3_L13_16B(msb,lsb) (((msb & 0x1f) << 8) | lsb) #define GETBITS_S4_L12_16B(msb,lsb) (((msb & 0x0f) << 8) | lsb) #define GETBITS_S0_L16_16B(msb,lsb) ((msb << 8) | lsb) #define PUTBITS_S0_L1_8B(dst,src) { dst &= ~0x80; dst |= (src << 7) & 0x80; } #define PUTBITS_S0_L2_8B(dst,src) { dst &= ~0xc0; dst |= (src << 6) & 0xc0; } #define PUTBITS_S0_L3_8B(dst,src) { dst &= ~0xe0; dst |= (src << 5) & 0xe0; } #define PUTBITS_S0_L4_8B(dst,src) { dst &= ~0xf0; dst |= (src << 4) & 0xf0; } #define PUTBITS_S0_L8_8B(dst,src) { dst = src & 0xff; } #define PUTBITS_S1_L1_8B(dst,src) { dst &= ~0x40; dst |= (src << 6) & 0x40; } #define PUTBITS_S1_L2_8B(dst,src) { dst &= ~0x60; dst |= (src << 5) & 0x60; } #define PUTBITS_S2_L6_8B(dst,src) { dst &= ~0x3f; dst |= src & 0x3f; } #define PUTBITS_S2_L2_8B(dst,src) { dst &= ~0x30; dst |= (src << 4) & 0x30; } #define PUTBITS_S2_L5_8B(dst,src) { dst &= ~0x3e; dst |= (src << 1) & 0x3e; } #define PUTBITS_S3_L5_8B(dst,src) { dst &= ~0x1f; dst |= src & 0x1f; } #define PUTBITS_S7_L1_8B(dst,src) { dst &= ~0x01; dst |= src & 0x01; } #define PUTBITS_S3_L13_16B(msb,lsb,src) \ { msb &= ~0x1f; msb |= (src & 0x1f00) >> 8; lsb = src & 0xff; } #define PUTBITS_S4_L12_16B(msb,lsb,src) \ { msb &= ~0x0f; msb |= (src & 0x0f00) >> 8; lsb = src & 0xff; } #define PUTBITS_S0_L16_16B(msb,lsb,src) \ { msb = (src & 0xff00) >> 8; lsb = src & 0xff; } #endif