1/* 2 * Copyright 2009, Haiku, Inc. All Rights Reserved. 3 * Distributed under the terms of the MIT License. 4 * 5 * Authors: 6 * Brecht Machiels, brecht@mos6581.org 7 */ 8 9#ifndef _BINARY_UTILS_H 10#define _BINARY_UTILS_H 11 12 13/* macro for a one-bit bitmask 14*/ 15 16#define BIT(n) (1ULL << n) 17 18/* macro/templates to create bitmasks 19*/ 20 21#define BITMASK(high, low) ((unsigned long long)(BitMask<high, low>::value)) 22 23template<int h, int l> 24struct BitMask 25{ 26 enum { value = (BitMask<h-l-1,0>::value + 27 (1ULL << h - l)) << l }; 28}; 29 30 31template<> 32struct BitMask<-1,0> 33{ 34 enum { value = 0ULL }; 35}; 36 37 38/* macro/templates to enter binary constants 39*/ 40 41#define BINARY(binstring) ((unsigned long long)(Binary<0##binstring>::value)) 42 43// http://www.eptacom.net/pubblicazioni/pub_eng/binary.html 44template<const unsigned long long N> struct Binary 45{ 46 enum { value = (N % 8ULL) + 2ULL * Binary<N / 8ULL>::value }; 47}; 48 49 50template<> 51struct Binary<0> 52{ 53 enum { value = 0ULL } ; 54}; 55 56 57/* macro/templates to determine offset 58 59 NOTE: broken for high bit indices 60*/ 61 62#define MASKOFFSET(mask) (MaskOffset<mask, (mask & 1UL)>::count) 63 64template<const unsigned long mask, unsigned int firstBit> 65struct MaskOffset 66{ 67 enum { count = MaskOffset<(mask >> 1), ((mask >> 1) & 1UL)>::count + 1 }; 68}; 69 70 71template<const unsigned long mask> 72struct MaskOffset<mask, 1> 73{ 74 enum { count = 0 }; 75}; 76 77#endif // _BINARY_UTILS_H 78 79