libosmocore  0.9.3
Osmocom core library
include/osmocom/core/bits.h
Go to the documentation of this file.
00001 #pragma once
00002 
00003 #include <stdint.h>
00004 #include <stddef.h>
00005 
00006 #include <osmocom/core/bit16gen.h>
00007 #include <osmocom/core/bit32gen.h>
00008 #include <osmocom/core/bit64gen.h>
00009 
00018 typedef int8_t  sbit_t;         
00019 typedef uint8_t ubit_t;         
00020 typedef uint8_t pbit_t;         
00022 /*
00023    NOTE on the endianess of pbit_t:
00024    Bits in a pbit_t are ordered MSB first, i.e. 0x80 is the first bit.
00025    Bit i in a pbit_t array is array[i/8] & (1<<(7-i%8))
00026 */
00027 
00031 static inline unsigned int osmo_pbit_bytesize(unsigned int num_bits)
00032 {
00033         unsigned int pbit_bytesize = num_bits / 8;
00034 
00035         if (num_bits % 8)
00036                 pbit_bytesize++;
00037 
00038         return pbit_bytesize;
00039 }
00040 
00041 int osmo_ubit2pbit(pbit_t *out, const ubit_t *in, unsigned int num_bits);
00042 
00043 int osmo_pbit2ubit(ubit_t *out, const pbit_t *in, unsigned int num_bits);
00044 
00045 int osmo_ubit2pbit_ext(pbit_t *out, unsigned int out_ofs,
00046                        const ubit_t *in, unsigned int in_ofs,
00047                        unsigned int num_bits, int lsb_mode);
00048 
00049 int osmo_pbit2ubit_ext(ubit_t *out, unsigned int out_ofs,
00050                        const pbit_t *in, unsigned int in_ofs,
00051                        unsigned int num_bits, int lsb_mode);
00052 
00053 
00054 /* BIT REVERSAL */
00055 
00057 enum osmo_br_mode {
00059         OSMO_BR_BITS_IN_DWORD   = 31,
00061         OSMO_BR_BYTES_IN_DWORD  = 24,
00063         OSMO_BR_BITS_IN_BYTE    = 7,
00065         OSMO_BR_WORD_SWAP       = 16,
00066 };
00067 
00069 uint32_t osmo_bit_reversal(uint32_t x, enum osmo_br_mode k);
00070 
00071 /* \brief reverse the bits within each byte of a 32bit word */
00072 uint32_t osmo_revbytebits_32(uint32_t x);
00073 
00074 /* \brief reverse the bits within a byte */
00075 uint32_t osmo_revbytebits_8(uint8_t x);
00076 
00077 /* \brief reverse the bits of each byte in a given buffer */
00078 void osmo_revbytebits_buf(uint8_t *buf, int len);
00079 
00085 static inline uint16_t osmo_rol16(uint16_t in, unsigned shift)
00086 {
00087         return (in << shift) | (in >> (16 - shift));
00088 }
00089