ATLAS Offline Software
ap_fixed.h
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2024 CERN for the benefit of the ATLAS collaboration
3 */
4 
5 #ifndef GLOBALSIM_AP_FIXED_H
6 #define GLOBALSIM_AP_FIXED_H
7 
9 ATLAS_NO_CHECK_FILE_THREAD_SAFETY; // due to statics used for debugging
10 
11 #include <cstddef>
12 #include <sstream>
13 
14 /*
15  * representation of a fixed point number.
16  * A fixed point number has a fixed width and precision.
17  * This implementation uses an C++ int type to store the bits.
18  * so allowing fast integer arithmetic.
19  */
20 
21 
22 namespace GlobalSim {
23 
24 
25  struct Round{};
26  struct Trunc{};
27  struct XilDef{};
28 
29  template<std::size_t width, typename T>
30  constexpr T max_to_overflow() {
31  T t{0};
32  static_assert(8*sizeof(t) >= width, "ap_fixed underlying int to small");
33  for (std::size_t i = 0; i <= sizeof(t)*8-width; ++i){
34  T bit{1};
35  t = t+(bit<<i);
36  }
37 
38  t = t << (width-1);
39  return t;
40  }
41 
42  template <std::size_t width,
43  std::size_t dp,
44  typename S=XilDef,
45  typename T=int16_t,
46  typename WS=int32_t>
47  struct ap_fixed {
48 
49  T m_value = T{0};
50  static constexpr T m_overflow_mask = max_to_overflow<width, T>();
51 
52  static inline bool s_check_overflow{false};
53  static inline bool s_print_value{false};
54  static inline bool s_debug{s_check_overflow or s_print_value};
55 
56  bool m_ovflw{false};
57  friend std::ostream& operator<<(std::ostream& os,
59  os << ap.m_value << ' ' << double(ap);
60  return os;
61  }
62 
63  ap_fixed() = default;
64 
65  ap_fixed(const double d) requires(std::is_same_v<S, Round>):
66  m_value(d * double (1<< dp) + (d >= 0 ? 0.5 : -0.5)){
67  test_overflow();
68  }
69 
70  ap_fixed(const double d) requires(std::is_same_v<S, XilDef>):
71  m_value(d * double (1<< dp) + (d >= 0 ? 0. : -1.0)){
72  test_overflow();
73  }
74 
75 
76  operator double() const{
77  return double(this->m_value) / double(1 << dp);
78  }
79 
80 
81  static ap_fixed form(T v) {
82  ap_fixed k; k.m_value = v; k.test_overflow();return k;
83  }
84 
85  const ap_fixed operator + (const ap_fixed& f) const {
86  return form(this->m_value + f.m_value);
87  }
88 
89 
90  const ap_fixed& operator += (const ap_fixed& f) {
91  this->m_value += f.m_value;
92  test_overflow();
93  return *this;
94  }
95 
96  ap_fixed operator - (const ap_fixed& f) const {
97  return form(this->m_value - f.m_value);
98  }
99 
100 
101  const ap_fixed& operator -= (const ap_fixed& f) {
102  this->m_value -= f.m_value;
103  test_overflow();
104  return *this;
105  }
106 
107  ap_fixed operator * (const ap_fixed& f) const {
108  return form((WS(this->m_value) * WS(f.m_value)) >> dp);
109  }
110 
111  const ap_fixed& operator *= (const ap_fixed& f) {
112  this->m_value = (WS(this->m_value) * WS(f.m_value) >> dp);
113  test_overflow();
114  return *this;
115  }
116 
117 
118  ap_fixed operator / (const ap_fixed& f) const {
119  return form((WS(this->m_value) << dp) / WS(f.m_value));
120  }
121 
122  const ap_fixed& operator /= (const ap_fixed& f) {
123  this->m_value = ((WS(this->m_value) << dp) / WS(f.m_value));
124  test_overflow();
125  return *this;
126  }
127 
128  // negation
130  return form(-this->m_value);
131  }
132 
133  void test_overflow() {
134 
135  if (m_value > 0) {
136  if (m_value & m_overflow_mask) {
137  m_ovflw=true;
138  }
139  } else {
140  if (-m_value & m_overflow_mask) {
141  m_ovflw=true;
142  }
143  }
144 
145  if (m_ovflw) {
146  std::stringstream ss;
147  T val = std::abs(m_value);
148 
149  ss << "ap_fixed overflow. val: "
150  << m_value << " abs(val): " << val
151  << ' ' << std::hex << val
152  << " masked " << (val & m_overflow_mask);
153  throw std::out_of_range(ss.str());
154  }
155  }
156  };
157 }
158 
159 #endif
GlobalSim::ap_fixed::operator/
ap_fixed operator/(const ap_fixed &f) const
Definition: ap_fixed.h:118
TileDCSDataPlotter.dp
dp
Definition: TileDCSDataPlotter.py:840
GlobalSim::ap_fixed::operator*=
const ap_fixed & operator*=(const ap_fixed &f)
Definition: ap_fixed.h:111
GlobalSim::Round
Definition: ap_fixed.h:25
PowhegControl_ttHplus_NLO.ss
ss
Definition: PowhegControl_ttHplus_NLO.py:83
GlobalSim::ap_fixed::operator-
ap_fixed operator-() const
Definition: ap_fixed.h:129
GlobalSim::ap_fixed::s_debug
static bool s_debug
Definition: ap_fixed.h:54
hist_file_dump.d
d
Definition: hist_file_dump.py:137
GlobalSim::ap_fixed::m_value
T m_value
Definition: ap_fixed.h:49
GlobalSim::max_to_overflow
constexpr T max_to_overflow()
Definition: ap_fixed.h:30
read_hist_ntuple.t
t
Definition: read_hist_ntuple.py:5
JetTiledMap::S
@ S
Definition: TiledEtaPhiMap.h:44
xAOD::int16_t
setScaleOne setStatusOne setSaturated int16_t
Definition: gFexGlobalRoI_v1.cxx:55
python.AtlRunQueryParser.ap
ap
Definition: AtlRunQueryParser.py:826
GlobalSim::ap_fixed::operator<<
friend std::ostream & operator<<(std::ostream &os, const ap_fixed< width, dp, S, T, WS > ap)
Definition: ap_fixed.h:57
GlobalSim::ap_fixed::form
static ap_fixed form(T v)
Definition: ap_fixed.h:81
GlobalSim::ap_fixed::operator*
ap_fixed operator*(const ap_fixed &f) const
Definition: ap_fixed.h:107
GlobalSim
AlgTool to obtain a selection of eFex RoIs read in from the event store.
Definition: dump.h:8
GlobalSim::ap_fixed::m_ovflw
bool m_ovflw
Definition: ap_fixed.h:56
GlobalSim::ap_fixed::ap_fixed
ap_fixed(const double d) requires(std
Definition: ap_fixed.h:65
lumiFormat.i
int i
Definition: lumiFormat.py:85
GlobalSim::ap_fixed::test_overflow
void test_overflow()
Definition: ap_fixed.h:133
GlobalSim::ap_fixed
Definition: ap_fixed.h:47
hist_file_dump.f
f
Definition: hist_file_dump.py:135
xAOD::double
double
Definition: CompositeParticle_v1.cxx:159
GlobalSim::ap_fixed::s_print_value
static bool s_print_value
Definition: ap_fixed.h:53
ATLAS_NO_CHECK_FILE_THREAD_SAFETY
ATLAS_NO_CHECK_FILE_THREAD_SAFETY
Definition: ap_fixed.h:9
ReadFromCoolCompare.os
os
Definition: ReadFromCoolCompare.py:231
GlobalSim::ap_fixed::s_check_overflow
static bool s_check_overflow
Definition: ap_fixed.h:52
GlobalSim::ap_fixed::operator/=
const ap_fixed & operator/=(const ap_fixed &f)
Definition: ap_fixed.h:122
GlobalSim::ap_fixed::m_overflow_mask
static constexpr T m_overflow_mask
Definition: ap_fixed.h:50
python.PyAthena.v
v
Definition: PyAthena.py:154
GlobalSim::XilDef
Definition: ap_fixed.h:27
Base_Fragment.width
width
Definition: Sherpa_i/share/common/Base_Fragment.py:59
Pythia8_RapidityOrderMPI.val
val
Definition: Pythia8_RapidityOrderMPI.py:14
GlobalSim::ap_fixed::ap_fixed
ap_fixed()=default
GlobalSim::ap_fixed::operator-=
const ap_fixed & operator-=(const ap_fixed &f)
Definition: ap_fixed.h:101
GlobalSim::ap_fixed::operator+
const ap_fixed operator+(const ap_fixed &f) const
Definition: ap_fixed.h:85
GlobalSim::ap_fixed::operator+=
const ap_fixed & operator+=(const ap_fixed &f)
Definition: ap_fixed.h:90
GlobalSim::Trunc
Definition: ap_fixed.h:26
checker_macros.h
Define macros for attributes used to control the static checker.
fitman.k
k
Definition: fitman.py:528