Project
Loading...
Searching...
No Matches
eliasDelta.h
Go to the documentation of this file.
1// Copyright 2019-2023 CERN and copyright holders of ALICE O2.
2// See https://alice-o2.web.cern.ch/copyright for details of the copyright holders.
3// All rights not expressly granted are reserved.
4//
5// This software is distributed under the terms of the GNU General Public
6// License v3 (GPL Version 3), copied verbatim in the file "COPYING".
7//
8// In applying this license CERN does not waive the privileges and immunities
9// granted to it by virtue of its status as an Intergovernmental Organization
10// or submit itself to any jurisdiction.
11
15
16#ifndef RANS_INTERNAL_PACK_ELIASDELTA_H_
17#define RANS_INTERNAL_PACK_ELIASDELTA_H_
18
19#include <cstdint>
20#include <cstring>
21
26
27namespace o2::rans::internal
28{
29
30[[nodiscard]] inline BitPtr eliasDeltaEncode(BitPtr dst, uint32_t data)
31{
32 using namespace internal;
33 using namespace utils;
34
35 assert(data > 0);
36
37 const uint32_t highestPow2 = log2UIntNZ(data);
38 const uint32_t nLeadingZeros = log2UIntNZ(highestPow2 + 1);
39
40 packing_type eliasDeltaCode = highestPow2 + 1;
41 eliasDeltaCode = eliasDeltaCode << highestPow2 | bitExtract(data, 0, highestPow2);
42 uint32_t packedSize = nLeadingZeros + nLeadingZeros + 1 + highestPow2;
43
44 return pack(dst, eliasDeltaCode, packedSize);
45};
46
47inline constexpr size_t EliasDeltaDecodeMaxBits = 42;
48
49template <typename dst_T>
50[[nodiscard]] inline dst_T eliasDeltaDecode(BitPtr& srcPos, size_t rBitOffset = EliasDeltaDecodeMaxBits)
51{
52 using namespace internal;
53 using namespace utils;
54 static_assert(sizeof(dst_T) <= sizeof(uint32_t));
55 assert(rBitOffset <= EliasDeltaDecodeMaxBits);
56 constexpr size_t PackingBufferBits = toBits<packing_type>();
57
58 auto unpackedData = unpack<packing_type>(srcPos - rBitOffset, rBitOffset);
59
60 // do delta decoding algorithm
61 unpackedData <<= PackingBufferBits - rBitOffset;
62 const uint32_t nLeadingZeros = __builtin_clzl(unpackedData);
63 uint32_t eliasDeltaBits = 2 * nLeadingZeros + 1;
64 const uint32_t highestPow2 = bitExtract(unpackedData, PackingBufferBits - eliasDeltaBits, nLeadingZeros + 1) - 1;
65 eliasDeltaBits += highestPow2;
66 dst_T decodedValue = static_cast<dst_T>(pow2(highestPow2) + bitExtract(unpackedData, PackingBufferBits - eliasDeltaBits, highestPow2));
67
68 srcPos -= eliasDeltaBits;
69
70 return decodedValue;
71};
72
73} // namespace o2::rans::internal
74
75#endif /* RANS_INTERNAL_PACK_ELIASDELTA_H_ */
Pointer type helper class for bitwise Packing.
common helper classes and functions
helper functionalities useful for packing operations
GLenum GLenum dst
Definition glcorearb.h:1767
GLboolean * data
Definition glcorearb.h:298
packs data into a buffer
dst_T eliasDeltaDecode(BitPtr &srcPos, size_t rBitOffset=EliasDeltaDecodeMaxBits)
Definition eliasDelta.h:50
BitPtr eliasDeltaEncode(BitPtr dst, uint32_t data)
Definition eliasDelta.h:30
constexpr size_t EliasDeltaDecodeMaxBits
Definition eliasDelta.h:47
uint64_t packing_type
Definition utils.h:33
constexpr T log2UIntNZ(T x) noexcept
Definition utils.h:90
uint64_t bitExtract(uint64_t data, uint32_t start, uint32_t length) noexcept
Definition utils.h:58
Common utility functions.