aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/0001-Add-function-to-get-uninterrupted-bit-run.patch
blob: cc51957101fb8acf196471c8f75c4dc6b71546a7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
From df53ee248964652e64ced971ad5ef7ce954f5bbd Mon Sep 17 00:00:00 2001
From: Pravin Kumarvel <pmanohar@radisys.com>
Date: Thu, 22 Dec 2016 12:06:07 +0530
Subject: [PATCH] Add function to get uninterrupted bit run

Function bitvec_rl_curbit added to get number of  uninterrupted
bits run in vector starting from the current bit till max number
of bits.
Test case is added to check bitvec_rl_curbit.
---
 include/osmocom/core/bitvec.h |  1 +
 src/bitvec.c                  | 45 +++++++++++++++++++++++++++++++++++++++++++
 tests/bitvec/bitvec_test.c    | 41 +++++++++++++++++++++++++++++++++++++++
 tests/bitvec/bitvec_test.ok   |  2 ++
 4 files changed, 89 insertions(+)

diff --git a/include/osmocom/core/bitvec.h b/include/osmocom/core/bitvec.h
index 19e2af8..0e17ba7 100644
--- a/include/osmocom/core/bitvec.h
+++ b/include/osmocom/core/bitvec.h
@@ -89,6 +89,7 @@ char bit_value_to_char(enum bit_value v);
 void bitvec_to_string_r(const struct bitvec *bv, char *str);
 void bitvec_zero(struct bitvec *bv);
 unsigned bitvec_rl(const struct bitvec *bv, bool b);
+unsigned bitvec_rl_curbit(struct bitvec *bv, bool b, int max_bits);
 void bitvec_shiftl(struct bitvec *bv, unsigned int n);
 int16_t bitvec_get_int16_msb(const struct bitvec *bv, unsigned int num_bits);
 unsigned int bitvec_add_array(struct bitvec *bv, const uint32_t *array,
diff --git a/src/bitvec.c b/src/bitvec.c
index 38148ac..c895cff 100644
--- a/src/bitvec.c
+++ b/src/bitvec.c
@@ -575,6 +575,51 @@ unsigned bitvec_rl(const struct bitvec *bv, bool b)
 	return bv->cur_bit;
 }
 
+/*! \brief Return number (bits) of uninterrupted bit run in vector
+ *   starting from the current bit
+ *  \param[in] bv The boolean vector to work on
+ *  \param[in] b The boolean, sequence of 1's or 0's to be checked
+ *  \param[in] max_bits Total Number of Uncmopresed bits
+ *  \returns Number of consecutive bits of \p b in \p bv and cur_bit will
+ *  \go to cur_bit + number of consecutive bit
+ */
+unsigned bitvec_rl_curbit(struct bitvec *bv, bool b, int max_bits)
+{
+	unsigned i = 0;
+	unsigned j = 8;
+	int temp_res = 0;
+	int count = 0;
+	unsigned readIndex = bv->cur_bit;
+	unsigned remaining_bits = max_bits % 8;
+	unsigned remaining_bytes = max_bits / 8;
+	unsigned byte_mask = 0xFF;
+
+	if (readIndex % 8) {
+		for (j -= (readIndex % 8) ; j > 0 ; j--) {
+			if (readIndex < max_bits && bitvec_read_field(bv, &readIndex, 1) == b)
+				temp_res++;
+			else {
+				bv->cur_bit--;
+				return temp_res;
+			}
+		}
+	}
+	for (i = (readIndex / 8);
+			i < (remaining_bits ? remaining_bytes + 1 : remaining_bytes);
+			i++, count++) {
+		if ((b ? byte_mask : 0) != bv->data[i]) {
+			bv->cur_bit = (count * 8 +
+					leading_bits(bv->data[i], b) + readIndex);
+			return count * 8 +
+				leading_bits(bv->data[i], b) + temp_res;
+		}
+	}
+	bv->cur_bit = (temp_res + (count * 8)) + readIndex;
+	if (bv->cur_bit > max_bits)
+		bv->cur_bit = max_bits;
+	return (bv->cur_bit - readIndex + temp_res);
+}
+
 /*! \brief Shifts bitvec to the left, n MSB bits lost */
 void bitvec_shiftl(struct bitvec *bv, unsigned n)
 {
diff --git a/tests/bitvec/bitvec_test.c b/tests/bitvec/bitvec_test.c
index a98a91c..afcc942 100644
--- a/tests/bitvec/bitvec_test.c
+++ b/tests/bitvec/bitvec_test.c
@@ -150,6 +150,18 @@ static inline void test_array_item(unsigned t, struct bitvec *b, unsigned int n,
 	}
 }
 
+static inline void test_bitvec_rl_curbit(struct bitvec *bv, bool b, int max_bits,
+						int result )
+{
+	int num = 0;
+	int readIndex = bv->cur_bit;
+	OSMO_ASSERT(bv->cur_bit < max_bits);
+	num = bitvec_rl_curbit(bv, b, max_bits);
+	readIndex += num;
+	OSMO_ASSERT(bv->cur_bit == readIndex);
+	OSMO_ASSERT(num == result);
+}
+
 static void test_array()
 {
 	struct bitvec b;
@@ -245,6 +257,35 @@ int main(int argc, char **argv)
 
 	test_array();
 
+	printf("\nbitvec_runlength....\n");
+
+	bitvec_zero(&bv);
+	bitvec_set_uint(&bv, 0xff, 8);
+	bv.cur_bit -= 8;
+	test_bitvec_rl_curbit(&bv, 1, 64, 8);
+
+	bitvec_zero(&bv);
+	bitvec_set_uint(&bv, 0xfc, 8);
+	bv.cur_bit -= 8;
+	test_bitvec_rl_curbit(&bv, 1, 64, 6);
+
+	bitvec_zero(&bv);
+	test_bitvec_rl_curbit(&bv, 0, 52, 52);
+
+	bitvec_zero(&bv);
+	bitvec_set_uint(&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);
+	bv.cur_bit -= 11;
+	test_bitvec_rl_curbit(&bv, 1, 64, 8);
+
+	bitvec_zero(&bv);
+	test_bitvec_rl_curbit(&bv, 1, 64, 0);
+
 	printf("\nbitvec ok.\n");
 
 	return 0;
diff --git a/tests/bitvec/bitvec_test.ok b/tests/bitvec/bitvec_test.ok
index e256108..6281973 100644
--- a/tests/bitvec/bitvec_test.ok
+++ b/tests/bitvec/bitvec_test.ok
@@ -166,4 +166,6 @@ bits: 17, est: 1153, real: 1153, x: 0, y: 0
 ........ ........ ........ ........ ........ ........ ........ ........ ........ ........ ........ ........ ........ ........ ........ 
 ........ ........ ........ ........ ........ ........ ........ ........ ........ ........ ........ ........ ........ ........ ........ 
 ........ ........ ........ ........ ........ ........ ........ ........ ........ ........ ........ ........ 
+bitvec_runlength....
+
 bitvec ok.
-- 
1.9.1