[2bb4a01] | 1 | //
|
---|
| 2 | // Cforall Version 1.0.0 Copyright (C) 2015 University of Waterloo
|
---|
| 3 | //
|
---|
| 4 | // The contents of this file are covered under the licence agreement in the
|
---|
| 5 | // file "LICENCE" distributed with Cforall.
|
---|
| 6 | //
|
---|
| 7 | // Bitfield.hpp --
|
---|
| 8 | //
|
---|
| 9 | // Author : Aaron B. Moss
|
---|
| 10 | // Created On : Thu May 9 10:00:00 2019
|
---|
| 11 | // Last Modified By : Aaron B. Moss
|
---|
| 12 | // Last Modified On : Thu May 9 10:00:00 2019
|
---|
| 13 | // Update Count : 1
|
---|
| 14 | //
|
---|
| 15 |
|
---|
| 16 | #pragma once
|
---|
| 17 |
|
---|
| 18 | #include <strings.h> // for ffs
|
---|
| 19 |
|
---|
| 20 | /// Make a type a bitfield.
|
---|
| 21 | /// Include in type definition to add operators. Used to simulate inheritance because union
|
---|
| 22 | /// does not allow it. Requires type to have `unsigned val` field
|
---|
| 23 | /// @param BFType Name of containing type
|
---|
| 24 | #define MakeBitfield( BFType ) \
|
---|
| 25 | constexpr BFType() : val( 0 ) {} \
|
---|
| 26 | constexpr BFType( unsigned int v ) : val( v ) {} \
|
---|
| 27 | bool operator[]( unsigned int i ) const { return val & (1 << i); } \
|
---|
| 28 | bool any() const { return val != 0; } \
|
---|
| 29 | void reset() { val = 0; } \
|
---|
| 30 | int ffs() { return ::ffs( val ) - 1; } \
|
---|
| 31 | BFType operator&=( BFType other ) { \
|
---|
| 32 | val &= other.val; return *this; \
|
---|
| 33 | } \
|
---|
| 34 | BFType operator&( BFType other ) const { \
|
---|
| 35 | BFType q = other; \
|
---|
| 36 | q &= *this; \
|
---|
| 37 | return q; \
|
---|
| 38 | } \
|
---|
| 39 | BFType operator|=( BFType other ) { \
|
---|
| 40 | val |= other.val; return *this; \
|
---|
| 41 | } \
|
---|
| 42 | BFType operator|( BFType other ) const { \
|
---|
| 43 | BFType q = other; \
|
---|
| 44 | q |= *this; \
|
---|
| 45 | return q; \
|
---|
| 46 | } \
|
---|
| 47 | BFType operator-=( BFType other ) { \
|
---|
| 48 | val &= ~other.val; return *this; \
|
---|
| 49 | }
|
---|
| 50 |
|
---|
| 51 | /// Adds default printing operator to a bitfield type.
|
---|
| 52 | /// Include in definition to add print function, requires other bitfield operators.
|
---|
| 53 | /// @param N Number of bits in bitfield
|
---|
| 54 | #define MakeBitfieldPrint( N ) \
|
---|
| 55 | static const char* Names[]; \
|
---|
| 56 | void print( std::ostream & os ) const { \
|
---|
| 57 | if ( (*this).any() ) { \
|
---|
| 58 | for ( unsigned int i = 0; i < N; i += 1 ) { \
|
---|
| 59 | if ( (*this)[i] ) { \
|
---|
| 60 | os << Names[i] << ' '; \
|
---|
| 61 | } \
|
---|
| 62 | } \
|
---|
| 63 | } \
|
---|
| 64 | }
|
---|
| 65 |
|
---|
| 66 | // Local Variables: //
|
---|
| 67 | // tab-width: 4 //
|
---|
| 68 | // mode: c++ //
|
---|
| 69 | // compile-command: "make install" //
|
---|
| 70 | // End: //
|
---|