From: Nathael Pajani Date: Mon, 21 Apr 2014 19:30:06 +0000 (+0200) Subject: Utility functions. X-Git-Url: http://git.techno-innov.fr/?a=commitdiff_plain;h=8d2db25745e8d24df327a5203ea32bcc6e410cb4;p=soft%2Flpc122x%2Fcore Utility functions. --- diff --git a/include/core/system.h b/include/core/system.h index 9ebc2ee..a631b1f 100644 --- a/include/core/system.h +++ b/include/core/system.h @@ -33,6 +33,7 @@ /* Error Values, from glibc errno.h and errno-base.h */ #define EIO 5 /* Bad one: Input or Output error. */ +#define E2BIG 7 /* Argument list too long or Data size beyond buffer size */ #define EAGAIN 11 /* Device already in use */ #define EFAULT 14 /* Address error */ #define EBUSY 16 /* Device or ressource Busy */ diff --git a/include/lib/stddef.h b/include/lib/stddef.h new file mode 100644 index 0000000..b410eb6 --- /dev/null +++ b/include/lib/stddef.h @@ -0,0 +1,27 @@ +/**************************************************************************** + * lib/stddef.h + * + * Copyright 2014 Nathael Pajani + * + r + * This program 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, see . + * + *************************************************************************** */ + +#ifndef LIB_STDDEF_H +#define LIB_STDDEF_H + +#define offsetof(type, member) __builtin_offsetof (type, member) + +#endif /* LIB_STDDEF_H */ diff --git a/include/lib/utils.h b/include/lib/utils.h new file mode 100644 index 0000000..9a0a867 --- /dev/null +++ b/include/lib/utils.h @@ -0,0 +1,53 @@ +/**************************************************************************** + * lib/utils.c + * + * Copyright 2014 Nathael Pajani + * + * This program 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, see . + * + *************************************************************************** */ + +#ifndef LIB_UTILS_H +#define LIB_UTILS_H + +/***************************************************************************** */ +/* Library routines */ +/***************************************************************************** */ + +#include +#include + + +/***************************************************************************** */ +/* Bit twidling hacks. + * http://graphics.stanford.edu/~seander/bithacks.html + */ + +/* Counting consecutive trailing or leading zero bits (or finding bit indices) + * The ARM Cortex M0 core does not have the __builtin_clz() and __builtin_ctz() + * instructions. + */ + +/* Count leading zeroes + * The following function is an effitient way to implement __builtin_clz(). + */ +uint8_t clz(uint32_t x); + +/* Count traling zeroes + * The following function is an effitient way to implement __builtin_ctz(). + */ +uint8_t ctz(uint32_t x); + +#endif /* LIB_UTILS_H */ + diff --git a/lib/utils.c b/lib/utils.c new file mode 100644 index 0000000..a207d08 --- /dev/null +++ b/lib/utils.c @@ -0,0 +1,68 @@ +/* + * lib/utils.c + * + * Library routines.. + * + */ + +#include +#include + + +/***************************************************************************** */ +/* Bit twidling hacks. + * http://graphics.stanford.edu/~seander/bithacks.html + */ + +/* Counting consecutive trailing or leading zero bits (or finding bit indices) + * The ARM Cortex M0 core does not have the __builtin_clz() and __builtin_ctz() + * instructions. + */ + +/* Count leading zeroes + * The following function is an effitient way to implement __builtin_clz(). + */ +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}; + unsigned int r = 32; + if (x >= 0x10000) { /* Quicker than (x & 0xFFFF0000) on a 32bit arm arch */ + r -= 16; + x >>= 16; + } + if (x & 0xFF00) { + r -= 8; + x >>= 8; + } + if (x & 0xF0) { + r -= 4; + x >>= 4; + } + return r - bval[x]; +} + +/* Count traling zeroes + * The following function is an effitient way to implement __builtin_ctz(). + */ +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}; + unsigned int r = 0; + if (x & 0x1) { + /* special case for odd value (assumed to happen half of the time) */ + return r; + } + if ((x & 0xFFFF) == 0) { + r += 16; + x >>= 16; + } + if ((x & 0xFF) == 0) { + r += 8; + x >>= 8; + } + if ((x & 0xF) == 0) { + r += 4; + x >>= 4; + } + return r + bval[(x & 0x0F)]; +}