CMS 3D CMS Logo

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