OpenMPCD
ImplementationDetails/Bitset.hpp
Go to the documentation of this file.
1 /**
2  * @file
3  * Implements functionality of the `OpenMPCD::CUDA::Bitset` class.
4  */
5 
6 #ifndef OPENMPCD_CUDA_IMPLEMENTATIONDETAILS_BITSET_HPP
7 #define OPENMPCD_CUDA_IMPLEMENTATIONDETAILS_BITSET_HPP
8 
10 
12 
13 
14 namespace OpenMPCD
15 {
16 namespace CUDA
17 {
18 
20 Bitset::Bitset(const std::size_t bitCount_)
21  : storage(
22  new unsigned int[(bitCount + bitsPerElement - 1) / bitsPerElement]),
23  bitCount(bitCount_)
24 {
25 }
26 
29 {
30  delete[] storage;
31 }
32 
34 std::size_t Bitset::getBitCount() const
35 {
36  return bitCount;
37 }
38 
40 void Bitset::set(const std::size_t bit, const bool value)
41 {
43 
44  const std::size_t element = bit / bitsPerElement;
45  const std::size_t bitInElement = bit % bitsPerElement;
46 
47  if(value)
48  {
49  atomicOr(&storage[element], 1 << bitInElement);
50  }
51  else
52  {
53  atomicAnd(&storage[element], ~(1U << bitInElement));
54  }
55 }
56 
58 bool Bitset::get(const std::size_t bit) const
59 {
61 
62  const std::size_t element = bit / bitsPerElement;
63  const std::size_t bitInElement = bit % bitsPerElement;
64 
65  return (storage[element] >> bitInElement) & 1;
66 }
67 
69 void Bitset::setAll(const bool value)
70 {
71  const std::size_t elementCount =
72  (bitCount + bitsPerElement - 1) / bitsPerElement;
73 
74  for(std::size_t i = 0; i < elementCount; ++i)
75  storage[i] = value ? -1 : 0;
76 }
77 
78 } //namespace CUDA
79 } //namespace OpenMPCD
80 
81 
82 #endif //OPENMPCD_CUDA_IMPLEMENTATIONDETAILS_BITSET_HPP
OpenMPCD::CUDA::Bitset::~Bitset
OPENMPCD_CUDA_DEVICE ~Bitset()
The destructor.
Definition: ImplementationDetails/Bitset.hpp:28
OpenMPCD::CUDA::Bitset::Bitset
OPENMPCD_CUDA_DEVICE Bitset(const std::size_t bitCount_)
The constructor.
Definition: ImplementationDetails/Bitset.hpp:20
OPENMPCD_DEBUG_ASSERT
#define OPENMPCD_DEBUG_ASSERT(assertion)
Asserts that the given expression evaluates to true, but only if OPENMPCD_DEBUG is defined.
Definition: OPENMPCD_DEBUG_ASSERT.hpp:88
OpenMPCD::CUDA::Bitset::set
OPENMPCD_CUDA_DEVICE void set(const std::size_t bit, const bool value)
Sets the given bit to the given value.
Definition: ImplementationDetails/Bitset.hpp:40
OPENMPCD_CUDA_DEVICE
#define OPENMPCD_CUDA_DEVICE
Denotes a function to be callable from a CUDA Device.
Definition: Macros.hpp:25
Bitset.hpp
OpenMPCD::CUDA::Bitset::get
OPENMPCD_CUDA_DEVICE bool get(const std::size_t bit) const
Returns the given bit.
Definition: ImplementationDetails/Bitset.hpp:58
OpenMPCD::CUDA::Bitset::setAll
OPENMPCD_CUDA_DEVICE void setAll(const bool value)
Sets all bits to the given value.
Definition: ImplementationDetails/Bitset.hpp:69
OPENMPCD_DEBUG_ASSERT.hpp
OpenMPCD::CUDA::Bitset::getBitCount
OPENMPCD_CUDA_DEVICE std::size_t getBitCount() const
Returns the number of accessible bits.
Definition: ImplementationDetails/Bitset.hpp:34