From 18e97f6dd5457538c7bf39c6abcf346f5a080ca5 Mon Sep 17 00:00:00 2001 From: authmillenon Date: Sun, 3 Nov 2013 14:32:25 +0100 Subject: [PATCH] Use GCC/Clang builtins for bit arithmetics --- core/bitarithm.c | 27 +-------------------------- core/include/bitarithm.h | 14 +++++--------- sys/include/posix_io.h | 24 +++++++++++++++++++++++- 3 files changed, 29 insertions(+), 36 deletions(-) diff --git a/core/bitarithm.c b/core/bitarithm.c index dec36185d3..e43d97b808 100644 --- a/core/bitarithm.c +++ b/core/bitarithm.c @@ -11,6 +11,7 @@ * \{ * \file * \author Kaspar Schleiser + * \author Martin Lenders * \} */ @@ -39,29 +40,3 @@ number_of_highest_bit(unsigned v) return r; } -/*---------------------------------------------------------------------------*/ -unsigned -number_of_lowest_bit(register unsigned v) -{ - register unsigned r = 0; - - while ((v & 0x01) == 0) { - v >>= 1; - r++; - }; - - return r; -} -/*---------------------------------------------------------------------------*/ -unsigned -number_of_bits_set(unsigned v) -{ - unsigned c; // c accumulates the total bits set in v - - for (c = 0; v; c++) { - v &= v - 1; // clear the least significant bit set - } - - return c; -} - diff --git a/core/include/bitarithm.h b/core/include/bitarithm.h index 83eacb1d68..1bca83acb1 100644 --- a/core/include/bitarithm.h +++ b/core/include/bitarithm.h @@ -13,6 +13,7 @@ * @file * @author Freie Universität Berlin, Computer Systems & Telematics * @author Kaspar Schleiser + * @author Martin Lenders */ #ifndef BITARITHM_H_ @@ -78,22 +79,17 @@ unsigned number_of_highest_bit(unsigned v); /** * @brief Returns the number of the lowest '1' bit in a value - * @param[in] v Input value - must be unequal to '0', otherwise the - * function will produce an infinite loop - * @return Bit Number - * - * Source: http://graphics.stanford.edu/~seander/bithacks.html#IntegerLogObvious + * @param[in] v Input value + * @return Bit Number, 0 for least significant bit, -1 if input is 0 */ -unsigned number_of_lowest_bit(register unsigned v); +#define number_of_lowest_bit(v) (__builtin_ffs(v)-1) /** * @brief Returns the number of bits set in a value * @param[in] v Input value * @return Number of set bits - * - * Source: http://graphics.stanford.edu/~seander/bithacks.html#IntegerLogObvious */ -unsigned number_of_bits_set(unsigned v); +#define number_of_bits_set(v) (__builtin_popcount(v)) /** * @} diff --git a/sys/include/posix_io.h b/sys/include/posix_io.h index bb9ad426a4..b4fe27378e 100644 --- a/sys/include/posix_io.h +++ b/sys/include/posix_io.h @@ -1,3 +1,23 @@ +/** + * Copyright (C) 2013 INRIA. + * + * This file subject to the terms and conditions of the GNU Lesser General + * Public License. See the file LICENSE in the top level directory for more + * details. + */ + +/** + * @addtogroup posix + * @{ + * @file + * @brief POSIX-like IO + * + * @author Freie Universität Berlin + * @author Kaspar Schleiser + * @author Stephan Zeisberg + * @author Oliver Hahm + * @author Martin Lenders + */ #ifndef __READ_H #define __READ_H @@ -15,5 +35,7 @@ int posix_open(int pid, int flags); int posix_close(int pid); int posix_read(int pid, char *buffer, int bufsize); int posix_write(int pid, char *buffer, int bufsize); - +/** + * @} + */ #endif /* __READ_H */