ATLAS Offline Software
CaloCellPackerUtils.icc
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2022 CERN for the benefit of the ATLAS collaboration
3 */
4 /**
5  * @file CaloCellPackerUtils.icc
6  * @author scott snyder
7  * @date Nov 2007
8  * @brief Utilities for compacting calorimeter cells.
9  */
10 
11 
12 #include "CxxUtils/AthUnlikelyMacros.h"
13 
14 
15 namespace CaloCellPackerUtils {
16 
17 
18 /**
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.
22  */
23 inline
24 unsigned int Bitfield::in (unsigned int x) const
25 {
26  return (x&m_mask)<<m_shift;
27 }
28 
29 
30 /**
31  * @brief Extract a value from the bitfield.
32  * @param x The input bitfield.
33  * @return The value extracted from the bitfield.
34  */
35 inline
36 unsigned int Bitfield::out (unsigned int x) const
37 {
38  return (x>>m_shift)&m_mask;
39 }
40 
41 
42 /**
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.
46  */
47 inline
48 unsigned int Floatfield::in (double x) const
49 {
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))
54  return 0;
55 
56  // Convert to an int, and pack.
57  return Bitfield::in (static_cast<unsigned int> ((x - m_xmin) * m_ifact));
58 }
59 
60 
61 /**
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.
66  */
67 inline
68 double Floatfield::out (unsigned int x, int& underflow) const
69 {
70  // Unpack the value.
71  unsigned int xx = Bitfield::out(x);
72 
73  // Did we underflow?
74  if (ATH_UNLIKELY(!xx)) {
75  underflow = 1;
76  return m_xmin;
77  }
78 
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.
82  underflow = 0;
83  return m_xmin + ((int)xx+0.5) * m_fact;
84 }
85 
86 
87 /**
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.
91  */
92 inline
93 unsigned int Floatfield2::in (double x) const
94 {
95  // Handle over/underflow.
96  if (ATH_UNLIKELY(x >= m_xmax))
97  return Bitfield::in (m_mask);
98  else if (ATH_UNLIKELY(x <= 0))
99  return 0;
100 
101  // Convert to an int, and pack.
102  return Bitfield::in (static_cast<unsigned int> (x * m_ifact));
103 }
104 
105 
106 /**
107  * @brief Extract a value from the bitfield.
108  * @param x The input bitfield.
109  * @return The value extracted from the bitfield.
110  */
111 inline
112 double Floatfield2::out (unsigned int x) const
113 {
114  // Unpack the value.
115  unsigned int xx = Bitfield::out(x);
116 
117  // Did we underflow?
118  // (The branch hint here gives a small but measureable improvement.)
119  if (ATH_UNLIKELY(xx == 0)) return 0;
120 
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;
125 }
126 
127 
128 } // namespace CaloCellPackerUtils