source: src/AST/Bitfield.hpp @ a86b1b85

ADTarm-ehast-experimentalenumforall-pointer-decayjacob/cs343-translationnew-astnew-ast-unique-exprpthread-emulationqualifiedEnum
Last change on this file since a86b1b85 was 93744b5, checked in by Andrew Beach <ajbeach@…>, 5 years ago

The bitfield template makes sure it is working on an unsigned val, instead of int or float.

  • Property mode set to 100644
File size: 2.0 KB
Line 
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 : Andrew Beach
12// Last Modified On : Wed Jun 5 10:00:00 2019
13// Update Count     : 2
14//
15
16#pragma once
17
18#include <strings.h>   // for ffs
19#include <type_traits> // for is_unsigned
20
21/// Make a type a bitfield.
22/// Include in type definition to add operators. Used to simulate inheritance because union
23/// does not allow it. Requires type to have `unsigned val` field
24/// @param BFType  Name of containing type
25template<typename T>
26struct bitfield : public T {
27        using T::val;
28        using val_t = decltype(val);
29        static_assert(sizeof(T) == sizeof(unsigned int), "Type has incorrect size");
30        static_assert(std::is_unsigned<val_t>::value, "Bitfield val field is not unsigned.");
31
32        constexpr bitfield() : T( 0 ) {}
33        constexpr bitfield( val_t v ) : T( v ) {}
34
35        bool operator[]( val_t i ) const { return val & (1 << i); }
36        bool any() const { return val != 0; }
37        void reset() { val = 0; }
38        int ffs() { return ::ffs( val ) - 1; }
39
40        bitfield operator&=( bitfield other ) {
41                val &= other.val; return *this;
42        }
43        bitfield operator&( bitfield other ) const {
44                bitfield q = other;
45                q &= *this;
46                return q;
47        }
48        bitfield operator|=( bitfield other ) {
49                val |= other.val; return *this;
50        }
51        bitfield operator|( bitfield other ) const {
52                bitfield q = other;
53                q |= *this;
54                return q;
55        }
56        bitfield operator-=( bitfield other ) {
57                val &= ~other.val; return *this;
58        }
59};
60
61template<typename T>
62inline bool operator== ( const bitfield<T> & a, const bitfield<T> & b ) {
63        return a.val == b.val;
64}
65
66template<typename T>
67inline bool operator!= ( const bitfield<T> & a, const bitfield<T> & b ) {
68        return !(a == b);
69}
70
71// Local Variables: //
72// tab-width: 4 //
73// mode: c++ //
74// compile-command: "make install" //
75// End: //
Note: See TracBrowser for help on using the repository browser.