CMS 3D CMS Logo

HGCalVFECompressionImpl.cc
Go to the documentation of this file.
2 
4 
6  : exponentBits_(conf.getParameter<uint32_t>("exponentBits")),
7  mantissaBits_(conf.getParameter<uint32_t>("mantissaBits")),
8  truncationBits_(conf.getParameter<uint32_t>("truncationBits")),
9  rounding_(conf.getParameter<bool>("rounding")) {
10  if (((1 << exponentBits_) + mantissaBits_ - 1) >= 64) {
11  throw cms::Exception("CodespaceCannotFit") << "The code space cannot fit into the unsigned 64-bit space.\n";
12  }
16  : ((1ULL << (mantissaBits_ + truncationBits_ + 1)) - 1) << ((1 << exponentBits_) - 2);
17 }
18 
20  uint32_t& compressedCode,
21  uint64_t& compressedValue) const {
22  // check for saturation
23  if (value > saturationValue_) {
24  compressedCode = saturationCode_;
25  compressedValue = saturationValue_;
26  return;
27  }
28 
29  // count bit length
30  uint32_t bitlen;
31  uint64_t shifted_value = value >> truncationBits_;
32  uint64_t valcopy = shifted_value;
33  for (bitlen = 0; valcopy != 0; valcopy >>= 1, bitlen++) {
34  }
35  if (bitlen <= mantissaBits_) {
36  compressedCode = shifted_value;
37  compressedValue = shifted_value << truncationBits_;
38  return;
39  }
40 
41  // build exponent and mantissa
42  const uint32_t exponent = bitlen - mantissaBits_;
43  const uint64_t mantissa = (shifted_value >> (exponent - 1)) & ~(1ULL << mantissaBits_);
44 
45  // assemble floating-point
46  const uint32_t floatval = (exponent << mantissaBits_) | mantissa;
47 
48  // we will never want to round up maximum code here
49  if (!rounding_ || floatval == saturationCode_) {
50  compressedCode = floatval;
51  compressedValue = ((1ULL << mantissaBits_) | mantissa) << (exponent - 1);
52  } else {
53  const bool roundup = ((shifted_value >> (exponent - 2)) & 1ULL) == 1ULL;
54  if (!roundup) {
55  compressedCode = floatval;
56  compressedValue = ((1ULL << mantissaBits_) | mantissa) << (exponent - 1);
57  } else {
58  compressedCode = floatval + 1;
59  uint64_t rmantissa = mantissa + 1;
60  uint32_t rexponent = exponent;
61  if (rmantissa >= (1ULL << mantissaBits_)) {
62  rexponent++;
63  rmantissa &= ~(1ULL << mantissaBits_);
64  }
65  compressedValue = ((1ULL << mantissaBits_) | rmantissa) << (rexponent - 1);
66  }
67  }
68  compressedValue <<= truncationBits_;
69 }
70 
71 void HGCalVFECompressionImpl::compress(const std::unordered_map<uint32_t, uint32_t>& payload,
72  std::unordered_map<uint32_t, std::array<uint64_t, 2> >& compressed_payload) {
73  for (const auto& item : payload) {
74  const uint64_t value = static_cast<uint64_t>(item.second);
75  uint32_t code(0);
76  uint64_t compressed_value(0);
77  compressSingle(value, code, compressed_value);
78  std::array<uint64_t, 2> compressed_item = {{static_cast<uint64_t>(code), compressed_value}};
79  compressed_payload.emplace(item.first, compressed_item);
80  }
81 }
HGCalVFECompressionImpl(const edm::ParameterSet &conf)
void compressSingle(const uint64_t value, uint32_t &compressedCode, uint64_t &compressedValue) const
Definition: value.py:1
unsigned long long uint64_t
Definition: Time.h:13
void compress(const std::unordered_map< uint32_t, uint32_t > &, std::unordered_map< uint32_t, std::array< uint64_t, 2 > > &)