2 Copyright (C) 2002-2022 CERN for the benefit of the ATLAS collaboration
5 * @file CaloCellPackerUtils.icc
8 * @brief Utilities for compacting calorimeter cells.
12 #include "CxxUtils/AthUnlikelyMacros.h"
15 namespace CaloCellPackerUtils {
19 * @brief Shift and mask a value into the bitfield.
20 * @param x The input value.
21 * @return The value shifted and masked to go into the bitfield.
24 unsigned int Bitfield::in (unsigned int x) const
26 return (x&m_mask)<<m_shift;
31 * @brief Extract a value from the bitfield.
32 * @param x The input bitfield.
33 * @return The value extracted from the bitfield.
36 unsigned int Bitfield::out (unsigned int x) const
38 return (x>>m_shift)&m_mask;
43 * @brief Shift and mask a value into the bitfield.
44 * @param x The input value.
45 * @return The converted value shifted and masked to go into the bitfield.
48 unsigned int Floatfield::in (double x) const
50 // Handle over/underflow.
51 if (ATH_UNLIKELY(x >= m_xmax))
52 return Bitfield::in (m_mask);
53 else if (ATH_UNLIKELY(x <= m_xmin))
56 // Convert to an int, and pack.
57 return Bitfield::in (static_cast<unsigned int> ((x - m_xmin) * m_ifact));
62 * @brief Extract a value from the bitfield.
63 * @param x The input bitfield.
64 * @param underflag[out] Set to 1 if the value was the lowest possible.
65 * @return The value extracted from the bitfield.
68 double Floatfield::out (unsigned int x, int& underflow) const
71 unsigned int xx = Bitfield::out(x);
74 if (ATH_UNLIKELY(!xx)) {
79 // Convert back to a float.
80 // Note: the cast to int here is important for performance: converting
81 // an unsigned to a double takes much longer than converting an int.
83 return m_xmin + ((int)xx+0.5) * m_fact;
88 * @brief Shift and mask a value into the bitfield.
89 * @param x The input value.
90 * @return The converted value shifted and masked to go into the bitfield.
93 unsigned int Floatfield2::in (double x) const
95 // Handle over/underflow.
96 if (ATH_UNLIKELY(x >= m_xmax))
97 return Bitfield::in (m_mask);
98 else if (ATH_UNLIKELY(x <= 0))
101 // Convert to an int, and pack.
102 return Bitfield::in (static_cast<unsigned int> (x * m_ifact));
107 * @brief Extract a value from the bitfield.
108 * @param x The input bitfield.
109 * @return The value extracted from the bitfield.
112 double Floatfield2::out (unsigned int x) const
115 unsigned int xx = Bitfield::out(x);
118 // (The branch hint here gives a small but measureable improvement.)
119 if (ATH_UNLIKELY(xx == 0)) return 0;
121 // Convert back to a float.
122 // Note: the cast to int here is important for performance: converting
123 // an unsigned to a double takes much longer than converting an int.
124 return ((int)xx+0.5) * m_fact;
128 } // namespace CaloCellPackerUtils