Received: from eggs.gnu.org ([2001:4830:134:3::10]:44156) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1VfmBI-0003HT-Oa for qemu-devel@nongnu.org; Mon, 11 Nov 2013 02:45:16 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1VfmB5-0005Xl-IQ for qemu-devel@nongnu.org; Mon, 11 Nov 2013 02:45:08 -0500 Received: from e23smtp04.au.ibm.com ([202.81.31.146]:60452) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1VfmB4-0005W1-Ry for qemu-devel@nongnu.org; Mon, 11 Nov 2013 02:44:55 -0500 Received: from /spool/local by e23smtp04.au.ibm.com with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted for from ; Mon, 11 Nov 2013 17:44:48 +1000 From: Alexey Kardashevskiy Date: Mon, 11 Nov 2013 18:44:34 +1100 Message-Id: <1384155875-26999-6-git-send-email-aik@ozlabs.ru> In-Reply-To: <1384155875-26999-1-git-send-email-aik@ozlabs.ru> References: <1384155875-26999-1-git-send-email-aik@ozlabs.ru> Subject: [Qemu-devel] [PATCH v3 5/6] bitops: add BITNR macro List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: qemu-devel@nongnu.org Cc: Alexey Kardashevskiy , qemu-ppc@nongnu.org, Alexander Graf , =?UTF-8?q?Andreas=20F=C3=A4rber?= This adds a macro to calculate the highest bit set. If used on constant values, no code will be generated. Signed-off-by: Alexey Kardashevskiy --- include/qemu/bitops.h | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/include/qemu/bitops.h b/include/qemu/bitops.h index 304c90c..98ba42a 100644 --- a/include/qemu/bitops.h +++ b/include/qemu/bitops.h @@ -23,6 +23,18 @@ #define BIT_WORD(nr) ((nr) / BITS_PER_LONG) #define BITS_TO_LONGS(nr) DIV_ROUND_UP(nr, BITS_PER_BYTE * sizeof(long)) +#define __BITNR(m, n) ((m) == ((m) & (1<<(n)))) ? (n) : +#define BITNR(m) \ + __BITNR((m), 31) __BITNR((m), 30) __BITNR((m), 29) __BITNR((m), 28) \ + __BITNR((m), 27) __BITNR((m), 26) __BITNR((m), 25) __BITNR((m), 24) \ + __BITNR((m), 23) __BITNR((m), 22) __BITNR((m), 21) __BITNR((m), 20) \ + __BITNR((m), 19) __BITNR((m), 18) __BITNR((m), 17) __BITNR((m), 16) \ + __BITNR((m), 15) __BITNR((m), 14) __BITNR((m), 13) __BITNR((m), 12) \ + __BITNR((m), 11) __BITNR((m), 10) __BITNR((m), 9) __BITNR((m), 8) \ + __BITNR((m), 7) __BITNR((m), 6) __BITNR((m), 5) __BITNR((m), 4) \ + __BITNR((m), 3) __BITNR((m), 2) __BITNR((m), 1) __BITNR((m), 0) \ + -1 + /** * set_bit - Set a bit in memory * @nr: the bit to set -- 1.8.4.rc4