blob: fa917b07c43ed875128aa25eb4a61cd4eecda7f6 [file] [log] [blame]
Harald Welte468b6432014-09-11 13:05:51 +08001/*
2 * (C) 2011 by Harald Welte <laforge@gnumonks.org>
3 * (C) 2011 by Sylvain Munaut <tnt@246tNt.com>
4 *
5 * All Rights Reserved
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 */
Harald Welte2230c132011-01-19 10:10:16 +010022
23#include <stdint.h>
24
Pablo Neira Ayuso83419342011-03-22 16:36:13 +010025#include <osmocom/core/bits.h>
Harald Welte2230c132011-01-19 10:10:16 +010026
Harald Welteba6988b2011-08-17 12:46:48 +020027/*! \addtogroup bits
28 * @{
Neels Hofmeyr17518fe2017-06-20 04:35:06 +020029 * Osmocom bit level support code.
30 *
Harald Welteef7a44e2017-10-16 14:18:17 +020031 * This module implements the notion of different bit-fields, such as
32 * - unpacked bits (\ref ubit_t), i.e. 1 bit per byte
33 * - packed bits (\ref pbit_t), i.e. 8 bits per byte
34 * - soft bits (\ref sbit_t), 1 bit per byte from -127 to 127
35 *
Neels Hofmeyr17518fe2017-06-20 04:35:06 +020036 * \file bits.c */
Harald Welteba6988b2011-08-17 12:46:48 +020037
Neels Hofmeyr87e45502017-06-20 00:17:59 +020038/*! convert unpacked bits to packed bits, return length in bytes
Harald Welteba6988b2011-08-17 12:46:48 +020039 * \param[out] out output buffer of packed bits
40 * \param[in] in input buffer of unpacked bits
41 * \param[in] num_bits number of bits
42 */
Harald Welte2230c132011-01-19 10:10:16 +010043int osmo_ubit2pbit(pbit_t *out, const ubit_t *in, unsigned int num_bits)
44{
45 unsigned int i;
46 uint8_t curbyte = 0;
47 pbit_t *outptr = out;
48
49 for (i = 0; i < num_bits; i++) {
50 uint8_t bitnum = 7 - (i % 8);
51
52 curbyte |= (in[i] << bitnum);
53
Christian Vogelc7f84e92011-01-22 22:48:37 +010054 if(i % 8 == 7){
Harald Welte2230c132011-01-19 10:10:16 +010055 *outptr++ = curbyte;
56 curbyte = 0;
57 }
58 }
59 /* we have a non-modulo-8 bitcount */
60 if (i % 8)
61 *outptr++ = curbyte;
62
63 return outptr - out;
64}
65
Neels Hofmeyr87e45502017-06-20 00:17:59 +020066/*! Shift unaligned input to octet-aligned output
Maxe0a7d9e2016-06-17 17:58:52 +020067 * \param[out] out output buffer, unaligned
68 * \param[in] in input buffer, octet-aligned
69 * \param[in] num_nibbles number of nibbles
70 */
71void osmo_nibble_shift_right(uint8_t *out, const uint8_t *in,
72 unsigned int num_nibbles)
73{
74 unsigned int i, num_whole_bytes = num_nibbles / 2;
75 if (!num_whole_bytes)
76 return;
77
78 /* first byte: upper nibble empty, lower nibble from src */
79 out[0] = (in[0] >> 4);
80
81 /* bytes 1.. */
82 for (i = 1; i < num_whole_bytes; i++)
83 out[i] = ((in[i - 1] & 0xF) << 4) | (in[i] >> 4);
84
85 /* shift the last nibble, in case there's an odd count */
86 i = num_whole_bytes;
87 if (num_nibbles & 1)
88 out[i] = ((in[i - 1] & 0xF) << 4) | (in[i] >> 4);
89 else
90 out[i] = (in[i - 1] & 0xF) << 4;
91}
92
Neels Hofmeyr87e45502017-06-20 00:17:59 +020093/*! Shift unaligned input to octet-aligned output
Maxe0a7d9e2016-06-17 17:58:52 +020094 * \param[out] out output buffer, octet-aligned
95 * \param[in] in input buffer, unaligned
96 * \param[in] num_nibbles number of nibbles
97 */
98void osmo_nibble_shift_left_unal(uint8_t *out, const uint8_t *in,
99 unsigned int num_nibbles)
100{
101 unsigned int i, num_whole_bytes = num_nibbles / 2;
102 if (!num_whole_bytes)
103 return;
104
105 for (i = 0; i < num_whole_bytes; i++)
106 out[i] = ((in[i] & 0xF) << 4) | (in[i + 1] >> 4);
107
108 /* shift the last nibble, in case there's an odd count */
109 i = num_whole_bytes;
110 if (num_nibbles & 1)
111 out[i] = (in[i] & 0xF) << 4;
112}
113
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200114/*! convert unpacked bits to soft bits
Maxd8fb1422016-04-06 16:13:00 +0200115 * \param[out] out output buffer of soft bits
116 * \param[in] in input buffer of unpacked bits
117 * \param[in] num_bits number of bits
118 */
119void osmo_ubit2sbit(sbit_t *out, const ubit_t *in, unsigned int num_bits)
120{
121 unsigned int i;
122 for (i = 0; i < num_bits; i++)
123 out[i] = in[i] ? -127 : 127;
124}
125
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200126/*! convert soft bits to unpacked bits
Maxd8fb1422016-04-06 16:13:00 +0200127 * \param[out] out output buffer of unpacked bits
128 * \param[in] in input buffer of soft bits
129 * \param[in] num_bits number of bits
130 */
131void osmo_sbit2ubit(ubit_t *out, const sbit_t *in, unsigned int num_bits)
132{
133 unsigned int i;
134 for (i = 0; i < num_bits; i++)
135 out[i] = in[i] < 0;
136}
137
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200138/*! convert packed bits to unpacked bits, return length in bytes
Harald Welteba6988b2011-08-17 12:46:48 +0200139 * \param[out] out output buffer of unpacked bits
140 * \param[in] in input buffer of packed bits
141 * \param[in] num_bits number of bits
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200142 * \return number of bytes used in \ref out
Harald Welteba6988b2011-08-17 12:46:48 +0200143 */
Harald Welte2230c132011-01-19 10:10:16 +0100144int osmo_pbit2ubit(ubit_t *out, const pbit_t *in, unsigned int num_bits)
145{
146 unsigned int i;
147 ubit_t *cur = out;
148 ubit_t *limit = out + num_bits;
149
150 for (i = 0; i < (num_bits/8)+1; i++) {
151 pbit_t byte = in[i];
152 *cur++ = (byte >> 7) & 1;
153 if (cur >= limit)
154 break;
155 *cur++ = (byte >> 6) & 1;
156 if (cur >= limit)
157 break;
158 *cur++ = (byte >> 5) & 1;
159 if (cur >= limit)
160 break;
161 *cur++ = (byte >> 4) & 1;
162 if (cur >= limit)
163 break;
164 *cur++ = (byte >> 3) & 1;
165 if (cur >= limit)
166 break;
167 *cur++ = (byte >> 2) & 1;
168 if (cur >= limit)
169 break;
170 *cur++ = (byte >> 1) & 1;
171 if (cur >= limit)
172 break;
173 *cur++ = (byte >> 0) & 1;
174 if (cur >= limit)
175 break;
176 }
177 return cur - out;
178}
Sylvain Munautaeb10772011-01-21 12:22:30 +0100179
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200180/*! convert unpacked bits to packed bits (extended options)
Harald Welteba6988b2011-08-17 12:46:48 +0200181 * \param[out] out output buffer of packed bits
182 * \param[in] out_ofs offset into output buffer
183 * \param[in] in input buffer of unpacked bits
184 * \param[in] in_ofs offset into input buffer
185 * \param[in] num_bits number of bits
186 * \param[in] lsb_mode Encode bits in LSB orde instead of MSB
187 * \returns length in bytes (max written offset of output buffer + 1)
188 */
Sylvain Munautaeb10772011-01-21 12:22:30 +0100189int osmo_ubit2pbit_ext(pbit_t *out, unsigned int out_ofs,
190 const ubit_t *in, unsigned int in_ofs,
191 unsigned int num_bits, int lsb_mode)
192{
193 int i, op, bn;
194 for (i=0; i<num_bits; i++) {
195 op = out_ofs + i;
196 bn = lsb_mode ? (op&7) : (7-(op&7));
197 if (in[in_ofs+i])
198 out[op>>3] |= 1 << bn;
199 else
200 out[op>>3] &= ~(1 << bn);
201 }
202 return ((out_ofs + num_bits - 1) >> 3) + 1;
203}
204
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200205/*! convert packed bits to unpacked bits (extended options)
Harald Welteba6988b2011-08-17 12:46:48 +0200206 * \param[out] out output buffer of unpacked bits
207 * \param[in] out_ofs offset into output buffer
208 * \param[in] in input buffer of packed bits
209 * \param[in] in_ofs offset into input buffer
210 * \param[in] num_bits number of bits
211 * \param[in] lsb_mode Encode bits in LSB orde instead of MSB
212 * \returns length in bytes (max written offset of output buffer + 1)
213 */
Sylvain Munautaeb10772011-01-21 12:22:30 +0100214int osmo_pbit2ubit_ext(ubit_t *out, unsigned int out_ofs,
215 const pbit_t *in, unsigned int in_ofs,
216 unsigned int num_bits, int lsb_mode)
217{
218 int i, ip, bn;
219 for (i=0; i<num_bits; i++) {
220 ip = in_ofs + i;
221 bn = lsb_mode ? (ip&7) : (7-(ip&7));
222 out[out_ofs+i] = !!(in[ip>>3] & (1<<bn));
223 }
224 return out_ofs + num_bits;
225}
Harald Welteba6988b2011-08-17 12:46:48 +0200226
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200227/*! generalized bit reversal function
Harald Weltede6e4982012-12-06 21:25:27 +0100228 * \param[in] x the 32bit value to be reversed
229 * \param[in] k the type of reversal requested
230 * \returns the reversed 32bit dword
231 *
232 * This function reverses the bit order within a 32bit word. Depending
233 * on "k", it either reverses all bits in a 32bit dword, or the bytes in
234 * the dword, or the bits in each byte of a dword, or simply swaps the
235 * two 16bit words in a dword. See Chapter 7 "Hackers Delight"
236 */
Harald Welte712691d2011-09-01 14:47:31 +0200237uint32_t osmo_bit_reversal(uint32_t x, enum osmo_br_mode k)
238{
239 if (k & 1) x = (x & 0x55555555) << 1 | (x & 0xAAAAAAAA) >> 1;
240 if (k & 2) x = (x & 0x33333333) << 2 | (x & 0xCCCCCCCC) >> 2;
241 if (k & 4) x = (x & 0x0F0F0F0F) << 4 | (x & 0xF0F0F0F0) >> 4;
242 if (k & 8) x = (x & 0x00FF00FF) << 8 | (x & 0xFF00FF00) >> 8;
243 if (k & 16) x = (x & 0x0000FFFF) << 16 | (x & 0xFFFF0000) >> 16;
244
245 return x;
246}
247
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200248/*! reverse the bit-order in each byte of a dword
Harald Weltede6e4982012-12-06 21:25:27 +0100249 * \param[in] x 32bit input value
250 * \returns 32bit value where bits of each byte have been reversed
251 *
252 * See Chapter 7 "Hackers Delight"
253 */
Harald Welte712691d2011-09-01 14:47:31 +0200254uint32_t osmo_revbytebits_32(uint32_t x)
255{
256 x = (x & 0x55555555) << 1 | (x & 0xAAAAAAAA) >> 1;
257 x = (x & 0x33333333) << 2 | (x & 0xCCCCCCCC) >> 2;
258 x = (x & 0x0F0F0F0F) << 4 | (x & 0xF0F0F0F0) >> 4;
259
260 return x;
261}
262
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200263/*! reverse the bit order in a byte
Harald Weltede6e4982012-12-06 21:25:27 +0100264 * \param[in] x 8bit input value
265 * \returns 8bit value where bits order has been reversed
266 *
267 * See Chapter 7 "Hackers Delight"
268 */
Harald Welte712691d2011-09-01 14:47:31 +0200269uint32_t osmo_revbytebits_8(uint8_t x)
270{
271 x = (x & 0x55) << 1 | (x & 0xAA) >> 1;
272 x = (x & 0x33) << 2 | (x & 0xCC) >> 2;
273 x = (x & 0x0F) << 4 | (x & 0xF0) >> 4;
274
275 return x;
276}
277
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200278/*! reverse bit-order of each byte in a buffer
Katerina Barone-Adesic28c6a02013-02-15 13:27:59 +0100279 * \param[in] buf buffer containing bytes to be bit-reversed
280 * \param[in] len length of buffer in bytes
Harald Weltede6e4982012-12-06 21:25:27 +0100281 *
282 * This function reverses the bits in each byte of the buffer
283 */
Harald Welte712691d2011-09-01 14:47:31 +0200284void osmo_revbytebits_buf(uint8_t *buf, int len)
285{
286 unsigned int i;
287 unsigned int unaligned_cnt;
288 int len_remain = len;
289
290 unaligned_cnt = ((unsigned long)buf & 3);
291 for (i = 0; i < unaligned_cnt; i++) {
292 buf[i] = osmo_revbytebits_8(buf[i]);
293 len_remain--;
294 if (len_remain <= 0)
295 return;
296 }
297
Sylvain Munaut01e06042013-01-03 09:36:16 +0100298 for (i = unaligned_cnt; i + 3 < len; i += 4) {
Max08621a82016-01-21 17:16:56 +0100299 osmo_store32be(osmo_revbytebits_32(osmo_load32be(buf + i)), buf + i);
Harald Welte712691d2011-09-01 14:47:31 +0200300 len_remain -= 4;
301 }
302
303 for (i = len - len_remain; i < len; i++) {
304 buf[i] = osmo_revbytebits_8(buf[i]);
305 len_remain--;
306 }
307}
308
Sylvain Munautdca7d2c2012-04-18 21:53:23 +0200309/*! @} */