Improve the performance of ebitarray_set_range with a memset, math, and ebitarray_set_unsafe
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#include <EmberLib/Util/BitArray.h>
|
||||
|
||||
@@ -38,35 +39,6 @@ void ebitarray_destroy(BitArray *array)
|
||||
}
|
||||
}
|
||||
|
||||
//TODO: Real error codes plz?
|
||||
int8 ebitarray_set_range(BitArray *array, uint32 start, uint32 end, ebool value)
|
||||
{
|
||||
//TODO: Alter this for machines where bytes are != 8 num_bits
|
||||
int8 failure = 0;
|
||||
if(!array) return -1;
|
||||
if(end > array->num_bits) return -2;
|
||||
while(start % 8 != 0 && start <= end)
|
||||
{
|
||||
failure = ebitarray_set(array, start++, value);
|
||||
if(failure)
|
||||
return failure;
|
||||
}
|
||||
|
||||
//Start is guaranteed to be byte aligned
|
||||
uint32 byte = start/8;
|
||||
uint8 byte_val = value ? 0xFFu : 0x00u;
|
||||
while(end - start >= 8)
|
||||
{
|
||||
array->data[byte++] = byte_val;
|
||||
start+=8;
|
||||
}
|
||||
while(start <= end && !failure)
|
||||
{
|
||||
failure = ebitarray_set(array, start++, value);
|
||||
}
|
||||
return failure;
|
||||
}
|
||||
|
||||
/*
|
||||
* @brief Unsafe version of the ebitarray_set that assumes the index is in-bounds and the bitval is 1 or 0.
|
||||
*/
|
||||
@@ -97,6 +69,32 @@ int8 ebitarray_set(BitArray *array, uint32 index, ebool value)
|
||||
return 0;
|
||||
}
|
||||
|
||||
//TODO: Real error codes plz?
|
||||
//TODO: Alter this for machines where bytes are != 8 num_bits
|
||||
int8 ebitarray_set_range(BitArray *array, uint32 start, uint32 end, ebool value)
|
||||
{
|
||||
// Sanity Check
|
||||
if(!array) return -1;
|
||||
if(end > array->num_bits) return -2;
|
||||
|
||||
value = (value != 0); // Force value to 1 or 0
|
||||
while(start % 8 != 0 && start <= end)
|
||||
{
|
||||
ebitarray_set_unsafe(array, start++, value);
|
||||
}
|
||||
|
||||
//Start is guaranteed to be byte aligned
|
||||
const uint32 start_byte = start >> 3; // start/8
|
||||
const uint32 end_byte = end >> 3; // end/8
|
||||
uint8 byte_val = value ? 0xFFu : 0x00u;
|
||||
memset(array->data + start_byte, byte_val, end_byte-start_byte);
|
||||
while(start <= end)
|
||||
{
|
||||
ebitarray_set_unsafe(array, start++, value);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
ebool ebitarray_get(BitArray *array, uint32 index)
|
||||
{
|
||||
uint32 byte = index >> 3; /* index/8 */
|
||||
|
||||
Reference in New Issue
Block a user