• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright 2021 Huawei Technologies Co., Ltd
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #ifndef MINDSPORE_LITE_TOOLS_CONVERTER_QUANTIZER_FSE_BIT_STREAM_H_
18 #define MINDSPORE_LITE_TOOLS_CONVERTER_QUANTIZER_FSE_BIT_STREAM_H_
19 #include <cstdint>
20 #include <cstring>
21 
22 namespace mindspore::lite::quant {
23 class FSEBitStream {
24  public:
25   FSEBitStream() = default;
26 
27   ~FSEBitStream() = default;
28 
29  public:
30   int Create(uint64_t bit_capacity);
31   void Free();
32   void Empty();
33   uint64_t Pop(uint8_t bit_count);
34   void Push(int64_t state, uint8_t bit_count);
35   void Flush();
36   static size_t CountBits(size_t x);
37 
GetCurrChunkIndex()38   int32_t GetCurrChunkIndex() const { return this->curr_chunk_index_; }
GetCurrChunk()39   uint64_t GetCurrChunk() const { return this->curr_chunk_; }
GetCurrBitCount()40   int8_t GetCurrBitCount() const { return this->curr_bit_count_; }
GetChunks()41   uint64_t *GetChunks() const { return this->chunks_; }
GetChunkCount()42   int GetChunkCount() const { return this->chunk_count_; }
43 
SetCurrChunkIndex(int32_t curr_chunk_index)44   void SetCurrChunkIndex(int32_t curr_chunk_index) { this->curr_chunk_index_ = curr_chunk_index; }
SetCurrChunk(uint64_t curr_chunk)45   void SetCurrChunk(uint64_t curr_chunk) { this->curr_chunk_ = curr_chunk; }
SetCurrBitCount(int8_t curr_bit_count)46   void SetCurrBitCount(int8_t curr_bit_count) { this->curr_bit_count_ = curr_bit_count; }
SetChunks(uint64_t * chunks)47   void SetChunks(uint64_t *chunks) { this->chunks_ = chunks; }
SetChunkCount(int chunk_count)48   void SetChunkCount(int chunk_count) { this->chunk_count_ = chunk_count; }
49 
50  private:
51   int32_t curr_chunk_index_{-1};  // the index of the next chunk that we will write to
52   uint64_t curr_chunk_{0};
53   int8_t curr_bit_count_{0};   // the number of bits that are currently written in the register.
54   uint64_t *chunks_{nullptr};  // the actual memory
55   uint64_t chunk_count_{0};    // the number of chunks
56 };
57 }  // namespace mindspore::lite::quant
58 #endif  // MINDSPORE_LITE_TOOLS_CONVERTER_QUANTIZER_
59