10 #ifndef vtk_m_cont_ArrayHandleBitField_h
11 #define vtk_m_cont_ArrayHandleBitField_h
25 template <
typename BitPortalType>
26 class ArrayPortalBitField
29 using ValueType = bool;
32 explicit ArrayPortalBitField(
const BitPortalType& portal) noexcept
38 explicit ArrayPortalBitField(BitPortalType&& portal) noexcept
39 : BitPortal{ std::move(portal) }
43 ArrayPortalBitField() noexcept = default;
44 ArrayPortalBitField(const ArrayPortalBitField&) noexcept = default;
45 ArrayPortalBitField(ArrayPortalBitField&&) noexcept = default;
46 ArrayPortalBitField& operator=(const ArrayPortalBitField&) noexcept = default;
47 ArrayPortalBitField& operator=(ArrayPortalBitField&&) noexcept = default;
50 vtkm::
Id GetNumberOfValues() const noexcept {
return this->BitPortal.GetNumberOfBits(); }
53 ValueType
Get(
vtkm::Id index)
const noexcept {
return this->BitPortal.GetBit(index); }
56 void Set(
vtkm::Id index, ValueType value)
const
60 this->BitPortal.SetBitAtomic(index, value);
64 BitPortalType BitPortal;
72 class Storage<bool, StorageTagBitField>
74 using BitPortalType = vtkm::cont::detail::BitPortal;
75 using BitPortalConstType = vtkm::cont::detail::BitPortalConst;
78 static constexpr
vtkm::Id BlockSize = vtkm::cont::detail::BitFieldTraits::BlockSize;
82 using ReadPortalType = vtkm::cont::internal::ArrayPortalBitField<BitPortalConstType>;
83 using WritePortalType = vtkm::cont::internal::ArrayPortalBitField<BitPortalType>;
85 VTKM_CONT static std::vector<vtkm::cont::internal::Buffer> CreateBuffers()
87 return std::vector<vtkm::cont::internal::Buffer>(1);
91 const std::vector<vtkm::cont::internal::Buffer>& buffers,
95 const vtkm::Id bytesNeeded = (numberOfBits + CHAR_BIT - 1) / CHAR_BIT;
96 const vtkm::Id blocksNeeded = (bytesNeeded + BlockSize - 1) / BlockSize;
97 const vtkm::Id numBytes = blocksNeeded * BlockSize;
100 "BitField Allocation: %llu bits, blocked up to %s bytes.",
101 static_cast<unsigned long long>(numberOfBits),
104 buffers[0].SetNumberOfBytes(numBytes, preserve, token);
105 buffers[0].GetMetaData<vtkm::cont::internal::BitFieldMetaData>().NumberOfBits = numberOfBits;
109 const std::vector<vtkm::cont::internal::Buffer>&)
115 const std::vector<vtkm::cont::internal::Buffer>& buffers)
119 buffers[0].GetMetaData<vtkm::cont::internal::BitFieldMetaData>().NumberOfBits;
120 VTKM_ASSERT((buffers[0].GetNumberOfBytes() * CHAR_BIT) >= numberOfBits);
124 VTKM_CONT static void Fill(
const std::vector<vtkm::cont::internal::Buffer>& buffers,
135 vtkm::Id totalBitsInArray = GetNumberOfValues(buffers);
136 if (endBit >= totalBitsInArray)
138 endBit = ((totalBitsInArray + (wordNumBits - 1)) / wordNumBits) * wordNumBits;
140 if (((startBit % wordNumBits) == 0) && ((endBit % wordNumBits) == 0))
142 WordType fillWord = (fillValue ? ~WordType{ 0 } : WordType{ 0 });
143 buffers[0].Fill(&fillWord, wordTypeSize, startBit / CHAR_BIT, endBit / CHAR_BIT, token);
145 else if (((startBit % CHAR_BIT) == 0) && ((endBit % CHAR_BIT) == 0))
148 buffers[0].Fill(&fillWord, 1, startBit / CHAR_BIT, endBit / CHAR_BIT, token);
156 VTKM_CONT static ReadPortalType CreateReadPortal(
157 const std::vector<vtkm::cont::internal::Buffer>& buffers,
162 vtkm::Id numberOfBits = GetNumberOfValues(buffers);
163 VTKM_ASSERT((buffers[0].GetNumberOfBytes() * CHAR_BIT) >= numberOfBits);
165 return ReadPortalType(
166 BitPortalConstType(buffers[0].ReadPointerDevice(device, token), numberOfBits));
169 VTKM_CONT static WritePortalType CreateWritePortal(
170 const std::vector<vtkm::cont::internal::Buffer>& buffers,
175 vtkm::Id numberOfBits = GetNumberOfValues(buffers);
176 VTKM_ASSERT((buffers[0].GetNumberOfBytes() * CHAR_BIT) >= numberOfBits);
178 return WritePortalType(
179 BitPortalType(buffers[0].WritePointerDevice(device, token), numberOfBits));
197 :
Superclass(std::vector<
vtkm::cont::internal::Buffer>(1, bitField.GetBuffer()))
216 #endif // vtk_m_cont_ArrayHandleBitField_h