2 Copyright (C) 2002-2026 CERN for the benefit of the ATLAS collaboration
5 * @file CaloCellPackerUtils.icc
8 * @brief Utilities for compacting calorimeter cells.
12namespace CaloCellPackerUtils {
16 * @brief Shift and mask a value into the bitfield.
17 * @param x The input value.
18 * @return The value shifted and masked to go into the bitfield.
21unsigned int Bitfield::in (unsigned int x) const
23 return (x&m_mask)<<m_shift;
28 * @brief Extract a value from the bitfield.
29 * @param x The input bitfield.
30 * @return The value extracted from the bitfield.
33unsigned int Bitfield::out (unsigned int x) const
35 return (x>>m_shift)&m_mask;
40 * @brief Shift and mask a value into the bitfield.
41 * @param x The input value.
42 * @return The converted value shifted and masked to go into the bitfield.
45unsigned int Floatfield::in (double x) const
47 // Handle over/underflow.
48 if (x >= m_xmax) [[unlikely]]
49 return Bitfield::in (m_mask);
50 else if (x <= m_xmin) [[unlikely]]
53 // Convert to an int, and pack.
54 return Bitfield::in (static_cast<unsigned int> ((x - m_xmin) * m_ifact));
59 * @brief Extract a value from the bitfield.
60 * @param x The input bitfield.
61 * @param underflag[out] Set to 1 if the value was the lowest possible.
62 * @return The value extracted from the bitfield.
65double Floatfield::out (unsigned int x, int& underflow) const
68 unsigned int xx = Bitfield::out(x);
71 if (!xx) [[unlikely]] {
76 // Convert back to a float.
77 // Note: the cast to int here is important for performance: converting
78 // an unsigned to a double takes much longer than converting an int.
80 return m_xmin + ((int)xx+0.5) * m_fact;
85 * @brief Shift and mask a value into the bitfield.
86 * @param x The input value.
87 * @return The converted value shifted and masked to go into the bitfield.
90unsigned int Floatfield2::in (double x) const
92 // Handle over/underflow.
93 if (x >= m_xmax) [[unlikely]]
94 return Bitfield::in (m_mask);
95 else if (x <= 0) [[unlikely]]
98 // Convert to an int, and pack.
99 return Bitfield::in (static_cast<unsigned int> (x * m_ifact));
104 * @brief Extract a value from the bitfield.
105 * @param x The input bitfield.
106 * @return The value extracted from the bitfield.
109double Floatfield2::out (unsigned int x) const
112 unsigned int xx = Bitfield::out(x);
115 // (The branch hint here gives a small but measureable improvement.)
116 if (xx == 0) [[unlikely]] return 0;
118 // Convert back to a float.
119 // Note: the cast to int here is important for performance: converting
120 // an unsigned to a double takes much longer than converting an int.
121 return ((int)xx+0.5) * m_fact;
125} // namespace CaloCellPackerUtils