[BITS] introduce new packed/unpacked bit conversion routines

This commit is contained in:
Harald Welte 2011-01-19 10:10:16 +01:00
parent c035ec63f5
commit 2230c133a6
4 changed files with 94 additions and 2 deletions

View File

@ -1,4 +1,4 @@
osmocore_HEADERS = signal.h linuxlist.h timer.h select.h msgb.h \
osmocore_HEADERS = signal.h linuxlist.h timer.h select.h msgb.h bits.h \
tlv.h bitvec.h comp128.h statistics.h gsm_utils.h utils.h \
gsmtap.h write_queue.h rsl.h gsm48.h rxlev_stat.h mncc.h \
gsm48_ie.h logging.h gsm0808.h rate_ctr.h gsmtap_util.h \

27
include/osmocore/bits.h Normal file
View File

@ -0,0 +1,27 @@
#ifndef _OSMO_BITS_H
#define _OSMO_BITS_H
#include <stdint.h>
typedef uint8_t sbit_t; /* soft bit (-127...127) */
typedef uint8_t ubit_t; /* unpacked bit (0 or 1) */
typedef uint8_t pbit_t; /* packed bis (8 bits in a byte) */
/* determine how many bytes we would need for 'num_bits' packed bits */
static inline unsigned int osmo_pbit_bytesize(unsigned int num_bits)
{
unsigned int pbit_bytesize = num_bits / 8;
if (num_bits % 8)
pbit_bytesize++;
return pbit_bytesize;
}
/* convert unpacked bits to packed bits, return length in bytes */
int osmo_ubit2pbit(pbit_t *out, const ubit_t *in, unsigned int num_bits);
/* convert packed bits to unpacked bits, return length in bytes */
int osmo_pbit2ubit(ubit_t *out, const pbit_t *in, unsigned int num_bits);
#endif

View File

@ -9,7 +9,7 @@ AM_CFLAGS = -fPIC -Wall
lib_LTLIBRARIES = libosmocore.la
libosmocore_la_SOURCES = timer.c select.c signal.c msgb.c rxlev_stat.c \
libosmocore_la_SOURCES = timer.c select.c signal.c msgb.c rxlev_stat.c bits.c \
tlv_parser.c bitvec.c comp128.c gsm_utils.c statistics.c \
write_queue.c utils.c rsl.c gsm48.c gsm48_ie.c \
logging.c gsm0808.c rate_ctr.c gsmtap_util.c \

65
src/bits.c Normal file
View File

@ -0,0 +1,65 @@
#include <stdint.h>
#include <osmocore/bits.h>
/* convert unpacked bits to packed bits, return length in bytes */
int osmo_ubit2pbit(pbit_t *out, const ubit_t *in, unsigned int num_bits)
{
unsigned int i;
uint8_t curbyte = 0;
pbit_t *outptr = out;
for (i = 0; i < num_bits; i++) {
uint8_t bitnum = 7 - (i % 8);
curbyte |= (in[i] << bitnum);
if (i > 0 && i % 8 == 0) {
*outptr++ = curbyte;
curbyte = 0;
}
}
/* we have a non-modulo-8 bitcount */
if (i % 8)
*outptr++ = curbyte;
return outptr - out;
}
/* convert packed bits to unpacked bits, return length in bytes */
int osmo_pbit2ubit(ubit_t *out, const pbit_t *in, unsigned int num_bits)
{
unsigned int i;
ubit_t *cur = out;
ubit_t *limit = out + num_bits;
for (i = 0; i < (num_bits/8)+1; i++) {
pbit_t byte = in[i];
*cur++ = (byte >> 7) & 1;
if (cur >= limit)
break;
*cur++ = (byte >> 6) & 1;
if (cur >= limit)
break;
*cur++ = (byte >> 5) & 1;
if (cur >= limit)
break;
*cur++ = (byte >> 4) & 1;
if (cur >= limit)
break;
*cur++ = (byte >> 3) & 1;
if (cur >= limit)
break;
*cur++ = (byte >> 2) & 1;
if (cur >= limit)
break;
*cur++ = (byte >> 1) & 1;
if (cur >= limit)
break;
*cur++ = (byte >> 0) & 1;
if (cur >= limit)
break;
}
return cur - out;
}