From 030228552bba6646d602070558825869b7830db2 Mon Sep 17 00:00:00 2001 From: Nathael Pajani Date: Tue, 27 Sep 2016 22:48:39 +0200 Subject: [PATCH] Updated stdlib et utils libs --- include/lib/stdlib.h | 2 +- include/lib/utils.h | 6 ++++++ lib/stdlib.c | 5 ++++- lib/utils.c | 37 +++++++++++++++++++++++++++++++------ 4 files changed, 42 insertions(+), 8 deletions(-) diff --git a/include/lib/stdlib.h b/include/lib/stdlib.h index 9e302fb..9ae275d 100644 --- a/include/lib/stdlib.h +++ b/include/lib/stdlib.h @@ -28,7 +28,7 @@ * Does not check that the base is respected aside for the use of letters in * number representation. */ -uint32_t strtoul(uint8_t* str, uint8_t base); +uint32_t strtoul(const char* str, char** end, uint8_t base); #endif /* LIB_STDLIB_H */ diff --git a/include/lib/utils.h b/include/lib/utils.h index 838e24d..e8ffac2 100644 --- a/include/lib/utils.h +++ b/include/lib/utils.h @@ -48,5 +48,11 @@ uint8_t clz(uint32_t x); */ uint8_t ctz(uint32_t x); +/* + * Count bits set + */ +uint8_t bits_set(uint32_t x); + + #endif /* LIB_UTILS_H */ diff --git a/lib/stdlib.c b/lib/stdlib.c index 3b9f0fe..c0a1991 100644 --- a/lib/stdlib.c +++ b/lib/stdlib.c @@ -26,7 +26,7 @@ * Does not check that the base is respected aside for the use of letters in * number representation. */ -uint32_t strtoul(uint8_t* str, uint8_t base) +uint32_t strtoul(const char* str, char** end, uint8_t base) { uint32_t val = 0; while (*str != '\0') { @@ -47,6 +47,9 @@ uint32_t strtoul(uint8_t* str, uint8_t base) } break; } + if (end != NULL) { + *end = (char*)str; + } return val; } diff --git a/lib/utils.c b/lib/utils.c index aaaa2a5..42b4453 100644 --- a/lib/utils.c +++ b/lib/utils.c @@ -31,12 +31,15 @@ * instructions. */ + + /* Count leading zeroes - * The following function is an effitient way to implement __builtin_clz(). + * The following function is an efficient way to implement __builtin_clz(), + * or at least a good compromize between memory usage and speed. */ uint8_t clz(uint32_t x) { - static const uint8_t bval[] = {0,1,2,2,3,3,3,3,4,4,4,4,4,4,4,4}; + static const uint8_t bval_clz[] = {0,1,2,2,3,3,3,3,4,4,4,4,4,4,4,4}; unsigned int r = 32; if (x >= 0x10000) { /* Quicker than (x & 0xFFFF0000) on a 32bit arm arch */ r -= 16; @@ -50,15 +53,16 @@ uint8_t clz(uint32_t x) r -= 4; x >>= 4; } - return r - bval[x]; + return r - bval_clz[x]; } /* Count traling zeroes - * The following function is an effitient way to implement __builtin_ctz(). + * The following function is an efficient way to implement __builtin_ctz(), + * or at least a good compromize between memory usage and speed. */ uint8_t ctz(uint32_t x) { - static const uint8_t bval[] = {4,0,1,0,2,0,1,0,3,0,1,0,2,0,1,0}; + static const uint8_t bval_ctz[] = {4,0,1,0,2,0,1,0,3,0,1,0,2,0,1,0}; unsigned int r = 0; if (x & 0x1) { /* special case for odd value (assumed to happen half of the time) */ @@ -76,5 +80,26 @@ uint8_t ctz(uint32_t x) r += 4; x >>= 4; } - return r + bval[(x & 0x0F)]; + return r + bval_ctz[(x & 0x0F)]; +} + + + +/* + * Count bits set + */ +uint8_t bits_set(uint32_t x) +{ + static const uint8_t bval_bsets[] = {0,1,1,2,1,2,2,3,1,2,2,3,2,3,3,4}; + uint8_t r = 0; /* Accumulator for the total bits set in x */ + + r = bval_bsets[x & 0xFF]; + x >>= 8; + r += bval_bsets[x & 0xFF]; + x >>= 8; + r += bval_bsets[x & 0xFF]; + x >>= 8; + r += bval_bsets[x & 0xFF]; + + return r; } -- 2.43.0