VTK-m  2.0
ZFPBlockReader.h
Go to the documentation of this file.
1 //============================================================================
2 // Copyright (c) Kitware, Inc.
3 // All rights reserved.
4 // See LICENSE.txt for details.
5 //
6 // This software is distributed WITHOUT ANY WARRANTY; without even
7 // the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
8 // PURPOSE. See the above copyright notice for more information.
9 //============================================================================
10 #ifndef vtk_m_worklet_zfp_block_reader_h
11 #define vtk_m_worklet_zfp_block_reader_h
12 
14 
15 namespace vtkm
16 {
17 namespace worklet
18 {
19 namespace zfp
20 {
21 
22 using Word = vtkm::UInt64;
23 
24 template <vtkm::Int32 block_size, typename WordsPortalType>
26 {
27  const WordsPortalType& Words;
30 
33 
36 
37  VTKM_EXEC
38  BlockReader(const WordsPortalType& words, const int& maxbits, const int& block_idx)
39  : Words(words)
40  , m_maxbits(maxbits)
41  , MaxIndex(words.GetNumberOfValues() - 1)
42  {
43  Index = vtkm::Id(static_cast<size_t>(block_idx * maxbits) / (sizeof(Word) * 8));
44  m_buffer = static_cast<Word>(Words.Get(Index));
45  m_current_bit = (block_idx * maxbits) % vtkm::Int32((sizeof(Word) * 8));
46 
48  m_block_idx = block_idx;
49  }
50 
51  inline VTKM_EXEC unsigned int read_bit()
52  {
54  ++m_current_bit;
55  m_buffer >>= 1;
56  // handle moving into next word
57  if (m_current_bit >= vtkm::Int32(sizeof(Word) * 8))
58  {
59  m_current_bit = 0;
60  ++Index;
61  if (Index > MaxIndex)
62  return false;
63  m_buffer = static_cast<Word>(Words.Get(Index));
64  }
65  return bit;
66  }
67 
68 
69  // note this assumes that n_bits is <= 64
70  inline VTKM_EXEC vtkm::UInt64 read_bits(const int& n_bits)
71  {
72  vtkm::UInt64 bits;
73  // rem bits will always be positive
74  vtkm::Int32 rem_bits = vtkm::Int32(sizeof(Word) * 8) - m_current_bit;
75 
76  vtkm::Int32 first_read = vtkm::Min(rem_bits, n_bits);
77  // first mask
78  Word mask = ((Word)1 << ((first_read))) - 1;
79  bits = m_buffer & mask;
80  m_buffer >>= n_bits;
81  m_current_bit += first_read;
82  vtkm::Int32 next_read = 0;
83  if (n_bits >= rem_bits)
84  {
85  m_current_bit = 0;
86  // just read in 0s if someone asks for more bits past the end of the array.
87  // not sure what the best way to deal with this i
88  Index = vtkm::Min(MaxIndex, Index + 1);
89  m_buffer = static_cast<Word>(Words.Get(Index));
90  next_read = n_bits - first_read;
91  }
92 
93  // this is basically a no-op when first read constained
94  // all the bits. TODO: if we have aligned reads, this could
95  // be a conditional without divergence
96  mask = ((Word)1 << ((next_read))) - 1;
97  bits += (m_buffer & mask) << first_read;
98  m_buffer >>= next_read;
99  m_current_bit += next_read;
100  return bits;
101  }
102 
103 private:
105 
106 }; // block reader
107 
108 } // namespace zfp
109 } // namespace worklet
110 } // namespace vtkm
111 #endif // vtk_m_worklet_zfp_block_reader_h
vtkm::worklet::zfp::BlockReader
Definition: ZFPBlockReader.h:25
VTKM_EXEC
#define VTKM_EXEC
Definition: ExportMacros.h:51
vtkm
Groups connected points that have the same field value.
Definition: Atomic.h:19
vtkm::worklet::zfp::BlockReader::read_bit
VTKM_EXEC unsigned int read_bit()
Definition: ZFPBlockReader.h:51
vtkm::worklet::zfp::BlockReader::m_block_idx
int m_block_idx
Definition: ZFPBlockReader.h:29
ZFPTypeInfo.h
vtkm::worklet::zfp::BlockReader::m_buffer
Word m_buffer
Definition: ZFPBlockReader.h:34
vtkm::Id
vtkm::Int32 Id
Represents an ID (index into arrays).
Definition: Types.h:191
vtkm::worklet::zfp::BlockReader::Words
const WordsPortalType & Words
Definition: ZFPBlockReader.h:27
vtkm::worklet::zfp::BlockReader::m_current_bit
vtkm::Int32 m_current_bit
Definition: ZFPBlockReader.h:31
vtkm::worklet::zfp::BlockReader::read_bits
VTKM_EXEC vtkm::UInt64 read_bits(const int &n_bits)
Definition: ZFPBlockReader.h:70
vtkm::worklet::zfp::BlockReader::BlockReader
VTKM_EXEC BlockReader()
Definition: ZFPBlockReader.h:104
vtkm::worklet::zfp::Word
vtkm::UInt64 Word
Definition: ZFPBlockReader.h:22
vtkm::worklet::zfp::BlockReader::BlockReader
VTKM_EXEC BlockReader(const WordsPortalType &words, const int &maxbits, const int &block_idx)
Definition: ZFPBlockReader.h:38
vtkm::UInt32
uint32_t UInt32
Definition: Types.h:161
vtkm::Int32
int32_t Int32
Definition: Types.h:160
vtkm::worklet::zfp::BlockReader::m_maxbits
const vtkm::Int32 m_maxbits
Definition: ZFPBlockReader.h:28
vtkm::worklet::zfp::BlockReader::Index
vtkm::Id Index
Definition: ZFPBlockReader.h:32
vtkm::worklet::zfp::BlockReader::MaxIndex
const vtkm::Id MaxIndex
Definition: ZFPBlockReader.h:35