[PATCH] libosmocore[master]: Add bitvec_set_u8() helper

This is merely a historical archive of years 2008-2021, before the migration to mailman3.

A maintained and still updated list archive can be found at https://lists.osmocom.org/hyperkitty/list/gerrit-log@lists.osmocom.org/.

Max gerrit-no-reply at lists.osmocom.org
Tue Jan 9 14:23:41 UTC 2018


Review at  https://gerrit.osmocom.org/5699

Add bitvec_set_u8() helper

It's similar to bitvec_set_u64() but does not work with L/H bits. Most
of the time (at least in 3GPP TS 44.018 §10.5.2.16 IA Rest Octets) we
only set small values. Having separate helper allows to avoid
unnecessary type conversions. The additional advantage over existing
bitvec_set_uint() is the explicit length check. Tests are adjusted
accordingly.

Change-Id: I725cf0f4e4c58dbe1e961d80f9bb34d7635cc975
Related: OS#1526
---
M include/osmocom/core/bitvec.h
M src/bitvec.c
M tests/bitvec/bitvec_test.c
3 files changed, 34 insertions(+), 12 deletions(-)


  git pull ssh://gerrit.osmocom.org:29418/libosmocore refs/changes/99/5699/1

diff --git a/include/osmocom/core/bitvec.h b/include/osmocom/core/bitvec.h
index c9bab39..dd2019b 100644
--- a/include/osmocom/core/bitvec.h
+++ b/include/osmocom/core/bitvec.h
@@ -59,6 +59,7 @@
 int bitvec_get_bit_high(struct bitvec *bv);
 int bitvec_set_bits(struct bitvec *bv, const enum bit_value *bits, unsigned int count);
 int bitvec_set_u64(struct bitvec *bv, uint64_t v, uint8_t num_bits, bool use_lh);
+int bitvec_set_u8(struct bitvec *bv, uint8_t v, uint8_t num_bits);
 int bitvec_set_uint(struct bitvec *bv, unsigned int in, unsigned int count);
 int bitvec_get_uint(struct bitvec *bv, unsigned int num_bits);
 int bitvec_find_bit_pos(const struct bitvec *bv, unsigned int n, enum bit_value val);
diff --git a/src/bitvec.c b/src/bitvec.c
index 884eb02..3c9ed98 100644
--- a/src/bitvec.c
+++ b/src/bitvec.c
@@ -221,6 +221,27 @@
  *  \param[in] bv bit vector.
  *  \param[in] v mask representing which bits needs to be set.
  *  \param[in] num_bits number of meaningful bits in the mask.
+ *  \return 0 on success; negative in case of error. */
+int bitvec_set_u8(struct bitvec *bv, uint8_t v, uint8_t num_bits)
+{
+	uint8_t i;
+
+	if (num_bits > 8)
+		return -E2BIG;
+
+	for (i = 0; i < num_bits; i++) {
+		int rc = bitvec_set_bit(bv, (v & ((uint64_t)1 << (num_bits - i - 1))) ? 1 : 0);
+		if (rc != 0)
+			return rc;
+	}
+
+	return 0;
+}
+
+/*! set multiple bits (based on numeric value) at current pos.
+ *  \param[in] bv bit vector.
+ *  \param[in] v mask representing which bits needs to be set.
+ *  \param[in] num_bits number of meaningful bits in the mask.
  *  \param[in] use_lh whether to interpret the bits as L/H values or as 0/1.
  *  \return 0 on success; negative in case of error. */
 int bitvec_set_u64(struct bitvec *bv, uint64_t v, uint8_t num_bits, bool use_lh)
diff --git a/tests/bitvec/bitvec_test.c b/tests/bitvec/bitvec_test.c
index d0bc30c..a868dba 100644
--- a/tests/bitvec/bitvec_test.c
+++ b/tests/bitvec/bitvec_test.c
@@ -88,11 +88,11 @@
 		/* Write to bitvec */
 		memset(data, 0x00, sizeof(data));
 		bv.cur_bit = i;
-		rc = bitvec_set_uint(&bv, 0x7e, 8);
+		rc = bitvec_set_u8(&bv, 0x7e, 8);
 		OSMO_ASSERT(rc >= 0);
 		rc = bitvec_set_bytes(&bv, in, in_size);
 		OSMO_ASSERT(rc >= 0);
-		rc = bitvec_set_uint(&bv, 0x7e, 8);
+		rc = bitvec_set_u8(&bv, 0x7e, 8);
 		OSMO_ASSERT(rc >= 0);
 
 		printf("bitvec: %s\n", osmo_hexdump(bv.data, bv.data_len));
@@ -193,11 +193,11 @@
 
 	printf("test shifting...\n");
 
-	bitvec_set_uint(&bv, 0x0E, 7);
+	bitvec_set_u8(&bv, 0x0E, 7);
 	test_shift(&bv, 3);
 	test_shift(&bv, 17);
 	bitvec_set_uint(&bv, 0, 32);
-	bitvec_set_uint(&bv, 0x0A, 7);
+	bitvec_set_u8(&bv, 0x0A, 7);
 	test_shift(&bv, 24);
 
 	printf("checking RL functions...\n");
@@ -208,12 +208,12 @@
 	test_rl(&bv);
 	bitvec_shiftl(&bv, 18);
 	test_rl(&bv);
-	bitvec_set_uint(&bv, 0x0F, 8);
+	bitvec_set_u8(&bv, 0x0F, 8);
 	test_rl(&bv);
 	bitvec_zero(&bv);
-	bitvec_set_uint(&bv, 0xFF, 8);
+	bitvec_set_u8(&bv, 0xFF, 8);
 	test_rl(&bv);
-	bitvec_set_uint(&bv, 0xFE, 7);
+	bitvec_set_u8(&bv, 0xFE, 7);
 	test_rl(&bv);
 	bitvec_set_uint(&bv, 0, 17);
 	test_rl(&bv);
@@ -260,12 +260,12 @@
 	printf("\nbitvec_runlength....\n");
 
 	bitvec_zero(&bv);
-	bitvec_set_uint(&bv, 0xff, 8);
+	bitvec_set_u8(&bv, 0xff, 8);
 	bv.cur_bit -= 8;
 	test_bitvec_rl_curbit(&bv, 1, 64, 8);
 
 	bitvec_zero(&bv);
-	bitvec_set_uint(&bv, 0xfc, 8);
+	bitvec_set_u8(&bv, 0xfc, 8);
 	bv.cur_bit -= 8;
 	test_bitvec_rl_curbit(&bv, 1, 64, 6);
 
@@ -273,13 +273,13 @@
 	test_bitvec_rl_curbit(&bv, 0, 52, 52);
 
 	bitvec_zero(&bv);
-	bitvec_set_uint(&bv, 0xfc, 8);
+	bitvec_set_u8(&bv, 0xfc, 8);
 	bv.cur_bit -= 2;
 	test_bitvec_rl_curbit(&bv, 0, 64, 58);
 
 	bitvec_zero(&bv);
-	bitvec_set_uint(&bv, 0x07, 8);
-	bitvec_set_uint(&bv, 0xf8, 8);
+	bitvec_set_u8(&bv, 0x07, 8);
+	bitvec_set_u8(&bv, 0xf8, 8);
 	bv.cur_bit -= 11;
 	test_bitvec_rl_curbit(&bv, 1, 64, 8);
 

-- 
To view, visit https://gerrit.osmocom.org/5699
To unsubscribe, visit https://gerrit.osmocom.org/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I725cf0f4e4c58dbe1e961d80f9bb34d7635cc975
Gerrit-PatchSet: 1
Gerrit-Project: libosmocore
Gerrit-Branch: master
Gerrit-Owner: Max <msuraev at sysmocom.de>



More information about the gerrit-log mailing list