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.
22template <class STREAM>
24BitPacker<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.
39template <class STREAM>
41BitPacker<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.
53template <class STREAM>
55void 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.
76template <class STREAM>
78BitPacker8<STREAM>::BitPacker8 (STREAM& stream)
88 * @param nbits Must be 8.
89 * @param stream Output stream object.
91template <class STREAM>
93BitPacker8<STREAM>::BitPacker8 (uint8_t /*nbits*/, STREAM& stream)
104 * This may flush buffered data to the output stream.
106template <class STREAM>
108BitPacker8<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.
120template <class STREAM>
122void BitPacker8<STREAM>::pack (uint32_t dat)
124 m_buf |= (dat << m_nbuf);
135 * @brief Constructor.
136 * @param stream Output stream object.
138template <class STREAM>
140BitPacker16<STREAM>::BitPacker16 (STREAM& stream)
149 * @brief Constructor.
150 * @param nbits Must be 16.
151 * @param stream Output stream object.
153template <class STREAM>
155BitPacker16<STREAM>::BitPacker16 (uint8_t /*nbits*/, STREAM& stream)
166 * This may flush buffered data to the output stream.
168template <class STREAM>
170BitPacker16<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.
182template <class STREAM>
184void BitPacker16<STREAM>::pack (uint32_t dat)
186 m_buf |= (dat << m_nbuf);
196} // namespace CxxUtils