Add ubit <-> sbit convertors
diff mbox

Message ID 1459784977-2471-1-git-send-email-msuraev@sysmocom.de
State Not Applicable
Headers show

Commit Message

Max April 4, 2016, 3:49 p.m. UTC
From: Max <msuraev@sysmocom.de>

Move functions for conversion between soft and unpacked bits to main
library as they are generally useful.
---
 include/osmocom/core/bits.h |  3 +++
 src/bits.c                  | 24 ++++++++++++++++++++++++
 tests/conv/conv_test.c      | 23 ++---------------------
 3 files changed, 29 insertions(+), 21 deletions(-)

Comments

Holger Freyther April 5, 2016, 8:51 p.m. UTC | #1
> On 04 Apr 2016, at 17:49, msuraev@sysmocom.de wrote:
> 
> From: Max <msuraev@sysmocom.de>
> 
> Move functions for conversion between soft and unpacked bits to main
> library as they are generally useful.


Hi!



> +void osmo_ubit2sbit(sbit_t *out, const ubit_t *in, unsigned int num_bits)
> +{
> +	int i;
> +	for (i = 0; i < num_bits; i++)
> +		out[i] = in[i] ? -127 : 127;

> -static void
> -ubit_to_sbit(sbit_t *dst, ubit_t *src, int n)

either keep it with 'int' or at least be consistent. Doesn't the compiler warn here anyway? We don't expect to run on systems where sizeof(int) < sizeof(4) and num_bits > INT_MAX is very unlikely for our domain. Anyway. if you decide to to use unsigned (or size_t) then please be consistent and use it for num_bits and the index variable.

thanks
	holger

Patch
diff mbox

diff --git a/include/osmocom/core/bits.h b/include/osmocom/core/bits.h
index 46f0c8b..e082313 100644
--- a/include/osmocom/core/bits.h
+++ b/include/osmocom/core/bits.h
@@ -42,6 +42,9 @@  int osmo_ubit2pbit(pbit_t *out, const ubit_t *in, unsigned int num_bits);
 
 int osmo_pbit2ubit(ubit_t *out, const pbit_t *in, unsigned int num_bits);
 
+void osmo_ubit2sbit(sbit_t *out, const ubit_t *in, unsigned int num_bits);
+void osmo_sbit2ubit(ubit_t *out, const sbit_t *in, unsigned int num_bits);
+
 int osmo_ubit2pbit_ext(pbit_t *out, unsigned int out_ofs,
                        const ubit_t *in, unsigned int in_ofs,
                        unsigned int num_bits, int lsb_mode);
diff --git a/src/bits.c b/src/bits.c
index a0a9d9e..494c825 100644
--- a/src/bits.c
+++ b/src/bits.c
@@ -61,6 +61,30 @@  int osmo_ubit2pbit(pbit_t *out, const ubit_t *in, unsigned int num_bits)
 	return outptr - out;
 }
 
+/*! \brief convert unpacked bits to soft bits
+ *  \param[out] out output buffer of soft bits
+ *  \param[in] in input buffer of unpacked bits
+ *  \param[in] num_bits number of bits
+ */
+void osmo_ubit2sbit(sbit_t *out, const ubit_t *in, unsigned int num_bits)
+{
+	int i;
+	for (i = 0; i < num_bits; i++)
+		out[i] = in[i] ? -127 : 127;
+}
+
+/*! \brief convert soft bits to unpacked bits
+ *  \param[out] out output buffer of unpacked bits
+ *  \param[in] in input buffer of soft bits
+ *  \param[in] num_bits number of bits
+ */
+void osmo_sbit2ubit(ubit_t *out, const sbit_t *in, unsigned int num_bits)
+{
+	int i;
+	for (i = 0; i < num_bits; i++)
+		out[i] = in[i] < 0;
+}
+
 /*! \brief convert packed bits to unpacked bits, return length in bytes
  *  \param[out] out output buffer of unpacked bits
  *  \param[in] in input buffer of packed bits
diff --git a/tests/conv/conv_test.c b/tests/conv/conv_test.c
index d9440f9..de62f23 100644
--- a/tests/conv/conv_test.c
+++ b/tests/conv/conv_test.c
@@ -339,25 +339,6 @@  fill_random(ubit_t *b, int n)
 		b[i] = random() & 1;
 }
 
-static void
-ubit_to_sbit(sbit_t *dst, ubit_t *src, int n)
-{
-	int i;
-	for (i=0; i<n; i++)
-		dst[i] = src[i] ? -127 : 127;
-}
-
-static void sbit_to_ubit(ubit_t *dst, sbit_t *src, int n) __attribute__((unused));
-
-static void
-sbit_to_ubit(ubit_t *dst, sbit_t *src, int n)
-{
-	int i;
-	for (i=0; i<n; i++)
-		dst[i] = src[i] < 0;
-}
-
-
 int main(int argc, char *argv[])
 {
 	const struct conv_test_vector *tst;
@@ -424,7 +405,7 @@  int main(int argc, char *argv[])
 
 			printf("[..] Decoding: ");
 
-			ubit_to_sbit(bs, bu0, l);
+			osmo_ubit2sbit(bs, bu0, l);
 
 			l = osmo_conv_decode(tst->code, bs, bu1);
 			if (l != 0) {
@@ -459,7 +440,7 @@  int main(int argc, char *argv[])
 				return -1;
 			}
 
-			ubit_to_sbit(bs, bu1, l);
+			osmo_ubit2sbit(bs, bu1, l);
 
 			l = osmo_conv_decode(tst->code, bs, bu1);
 			if (l != 0) {