2 Copyright (C) 2002-2017 CERN for the benefit of the ATLAS collaboration
7 * @file CxxUtils/BitPacker.icc
8 * @author scott snyder <snyder@bnl.gov>
10 * @brief Helper to pack a set of values bitwise into a stream.
19 * @param nbits Number of bits per item to use in the packed representation.
20 * @param stream Output stream object.
22 template <class STREAM>
24 BitPacker<STREAM>::BitPacker (uint8_t nbits, STREAM& stream)
30 assert (m_nbits > 0 && m_nbits <= 32);
37 * This may flush buffered data to the output stream.
39 template <class STREAM>
41 BitPacker<STREAM>::~BitPacker()
49 * @brief Pack one value to the stream.
50 * @param x The value to pack.
51 * The upper bits should all be clear.
53 template <class STREAM>
55 void BitPacker<STREAM>::pack (uint32_t dat)
57 const uint8_t totbits = 8*sizeof(m_buf);
59 m_buf |= (dat << m_nbuf);
61 if (m_nbuf >= totbits) {
67 m_buf = dat >> (m_nbits - m_nbuf);
74 * @param stream Output stream object.
76 template <class STREAM>
78 BitPacker8<STREAM>::BitPacker8 (STREAM& stream)
88 * @param nbits Must be 8.
89 * @param stream Output stream object.
91 template <class STREAM>
93 BitPacker8<STREAM>::BitPacker8 (uint8_t /*nbits*/, STREAM& stream)
104 * This may flush buffered data to the output stream.
106 template <class STREAM>
108 BitPacker8<STREAM>::~BitPacker8()
116 * @brief Pack one value to the stream.
117 * @param x The value to pack.
118 * The upper bits should all be clear.
120 template <class STREAM>
122 void BitPacker8<STREAM>::pack (uint32_t dat)
124 m_buf |= (dat << m_nbuf);
135 * @brief Constructor.
136 * @param stream Output stream object.
138 template <class STREAM>
140 BitPacker16<STREAM>::BitPacker16 (STREAM& stream)
149 * @brief Constructor.
150 * @param nbits Must be 16.
151 * @param stream Output stream object.
153 template <class STREAM>
155 BitPacker16<STREAM>::BitPacker16 (uint8_t /*nbits*/, STREAM& stream)
166 * This may flush buffered data to the output stream.
168 template <class STREAM>
170 BitPacker16<STREAM>::~BitPacker16()
178 * @brief Pack one value to the stream.
179 * @param x The value to pack.
180 * The upper bits should all be clear.
182 template <class STREAM>
184 void BitPacker16<STREAM>::pack (uint32_t dat)
186 m_buf |= (dat << m_nbuf);
196 } // namespace CxxUtils