--- /dev/null
+/**
+ * \file
+ * <!--
+ * This file is part of BeRTOS.
+ *
+ * Bertos is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
+ *
+ * As a special exception, you may use this file as part of a free software
+ * library without restriction. Specifically, if other files instantiate
+ * templates or use macros or inline functions from this file, or you compile
+ * this file and link it with other files to produce an executable, this
+ * file does not by itself cause the resulting executable to be covered by
+ * the GNU General Public License. This exception does not however
+ * invalidate any other reasons why the executable file might be covered by
+ * the GNU General Public License.
+ *
+ * Copyright 2011 Develer S.r.l. (http://www.develer.com/)
+ * -->
+ *
+ * \brief Bitarray module
+ *
+ * \author Daniele Basile <asterix@develer.com>
+ *
+ */
+
+#include "bitarray.h"
+
+#include <string.h>
+
+// De Bruijn constant coefficents.
+static const uint8_t DeBruijn_coefficents[32] =
+{
+ 0, 1, 28, 2, 29, 14, 24, 3, 30, 22, 20, 15, 25, 17, 4, 8,
+ 31, 27, 13, 23, 21, 19, 16, 7, 26, 12, 18, 6, 11, 5, 10, 9
+};
+
+/**
+ * Return the position of the first bit non zero in bitarray
+ *
+ * \param bitx BitArray context
+ * \param idx Starting bit
+ * \param offset Number of bits to test
+ * \return Position in bitarray when firt bit non zero occur, -1 otherwise
+ */
+int bitarray_firstSetBit(BitArray *bitx)
+{
+ ASSERT(bitx);
+
+ uint32_t *b = (uint32_t *)bitx->array;
+ int pos = -1;
+ int curr = 0;
+ int i = 0;
+ size_t bytes = bitx->size;
+
+ while (bytes)
+ {
+ if (bytes < 4)
+ {
+ uint32_t data = 0;
+ memcpy(&data, &b[i], bytes);
+
+ if (data == 0)
+ return -1;
+
+ return (DeBruijn_coefficents[((uint32_t)((data & -data) * 0x077CB531U)) >> 27] + curr);
+ }
+
+ if (b[i])
+ {
+ pos = DeBruijn_coefficents[((uint32_t)((b[i] & -b[i]) * 0x077CB531U)) >> 27] + curr;
+ break;
+ }
+
+ bytes -= 4;
+ curr += 32;
+ i++;
+ }
+
+ return pos;
+}
#define TEST1_LEN 31
#define TEST2_LEN 17
+#define TEST3_LEN 16
+#define TEST4_LEN 23
+#define TEST5_LEN 72
BITARRAY_ALLOC(test1, TEST1_LEN);
BITARRAY_ALLOC(test2, TEST2_LEN);
+BITARRAY_ALLOC(test3, TEST3_LEN);
+BITARRAY_ALLOC(test4, TEST4_LEN);
+BITARRAY_ALLOC(test5, TEST5_LEN);
BitArray bitx1;
BitArray bitx2;
+BitArray bitx3;
+BitArray bitx4;
+BitArray bitx5;
int bitarray_testSetup(void)
{
kdbg_init();
bitarray_init(&bitx1, TEST1_LEN, test1, sizeof(test1));
bitarray_init(&bitx2, TEST2_LEN, test2, sizeof(test2));
+ bitarray_init(&bitx3, TEST3_LEN, test3, sizeof(test3));
+ bitarray_init(&bitx4, TEST4_LEN, test4, sizeof(test4));
+ bitarray_init(&bitx5, TEST5_LEN, test5, sizeof(test5));
return 0;
}
if (bitarray_isFull(&bitx2))
goto error;
+ kprintf("Test 3\n");
+ bitarray_set(&bitx3, 12);
+ bitarray_dump(&bitx3);
+ int pos = 0;
+ pos = bitarray_firstSetBit(&bitx3);
+ if (pos != 12)
+ goto error;
+
+ kprintf("Test 4\n");
+ bitarray_set(&bitx4, TEST4_LEN);
+ bitarray_dump(&bitx4);
+ pos = 0;
+ pos = bitarray_firstSetBit(&bitx4);
+ if (pos != 23)
+ goto error;
+
+ kprintf("Test 5\n");
+ bitarray_set(&bitx5, 71);
+ bitarray_dump(&bitx5);
+ pos = 0;
+ pos = bitarray_firstSetBit(&bitx5);
+ kprintf("pos %d\n", pos);
+ if (pos != 71)
+ goto error;
+
+ kprintf("Test 6\n");
+ bitarray_clear(&bitx5, 71);
+ bitarray_set(&bitx5, 5);
+ bitarray_dump(&bitx5);
+ pos = 0;
+ pos = bitarray_firstSetBit(&bitx5);
+ if (pos != 5)
+ goto error;
+
return 0;
error: