Loading [MathJax]/extensions/tex2jax.js
ATLAS Offline Software
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
ap_int.h
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2025 CERN for the benefit of the ATLAS collaboration
3 */
4 
5 #ifndef GLOBALSIM_AP_INT_H
6 #define GLOBALSIM_AP_INT_H
7 
8 #include <cstddef>
9 #include <cstdint>
10 #include <stdexcept>
11 
12 /*
13  * class that represnts an int type of fixed width.
14  * Implemented using a native C++ int type, but checks fopr overflow
15  * is this occurs using the speeciefied bit width.
16  *
17  * Note: possibly ap_fixed with precision = 0 could be used instead of ap_int
18  */
19 
20 namespace GlobalSim {
21 
22  template <std::size_t n_dig, typename T=int16_t>
23  struct ap_int {
24  T m_value{0};
25 
26  bool m_ovflw{false};
27 
28  ap_int() = default;
29 
30  ap_int(const double d) {
31  m_value = T(d + (d >= 0 ? 0.5 : -0.5));
32  test_overflow();
33  }
34 
35  operator int() const{
36  return m_value;
37  }
38 
39 
40  static ap_int form(T v) {
41  ap_int k; k.m_value = v; k.test_overflow(); return k;
42  }
43 
44  const ap_int operator + (const ap_int& f) const {
45  return form(this->m_value + f.m_value);
46  }
47 
48 
49  const ap_int& operator += (const ap_int& f) {
50  this->m_value +=f.m_value;
51  test_overflow();
52  return *this;
53  }
54 
55  ap_int operator - (const ap_int& f) const {
56  return form(this->m_value - f.m_value);
57  }
58 
59 
60  const ap_int& operator -= (const ap_int& f) {
61  this->m_value -= f.m_value;
62  test_overflow();
63  return *this;
64  }
65 
66  ap_int operator * (const ap_int& f) const {
67  return form((WS(this->m_value) * WS(f.m_value)));
68  }
69 
70  const ap_int& operator *= (const ap_int& f) {
71  this->m_value -= (WS(this->m_value) * WS(f.m_value));
72  test_overflow();
73  return *this;
74  }
75 
76 
77  ap_int operator / (const ap_int& f) const {
78  return form((WS(this->m_value)) / WS(f.m_value));
79  }
80 
81  const ap_int& operator /= (const ap_int& f) {
82  this->m_value /= ((WS(this->m_value)) / WS(f.m_value));
83  test_overflow();
84  return *this;
85  }
86 
87  // negation
88  ap_int operator - () const {
89  return form(-this->m_value);
90  }
91 
92  void test_overflow() {
93  auto val = m_value >= 0 ? m_value : -m_value;
94 
95  if (val > (1<< n_dig)) {
96  m_ovflw=true;
97  throw std::runtime_error("ap_int overflow " + std::to_string(m_value));
98  }
99  }
100  };
101 }
102 #endif
GlobalSim::ap_int::operator*=
const ap_int & operator*=(const ap_int &f)
Definition: ap_int.h:70
GlobalSim::ap_int::operator-=
const ap_int & operator-=(const ap_int &f)
Definition: ap_int.h:60
GlobalSim::ap_int::operator+=
const ap_int & operator+=(const ap_int &f)
Definition: ap_int.h:49
GlobalSim::ap_int::operator+
const ap_int operator+(const ap_int &f) const
Definition: ap_int.h:44
GlobalSim::ap_int::operator*
ap_int operator*(const ap_int &f) const
Definition: ap_int.h:66
GlobalSim::ap_int::operator-
ap_int operator-() const
Definition: ap_int.h:88
hist_file_dump.d
d
Definition: hist_file_dump.py:143
GlobalSim::ap_int::m_ovflw
bool m_ovflw
Definition: ap_int.h:26
GlobalSim::ap_int::operator/=
const ap_int & operator/=(const ap_int &f)
Definition: ap_int.h:81
GlobalSim
AlgTool to obtain a selection of eFex RoIs read in from the event store.
Definition: dump.h:8
GlobalSim::ap_int
Definition: ap_int.h:23
GlobalSim::ap_int::ap_int
ap_int(const double d)
Definition: ap_int.h:30
python.LArMinBiasAlgConfig.int
int
Definition: LArMinBiasAlgConfig.py:59
hist_file_dump.f
f
Definition: hist_file_dump.py:141
GlobalSim::ap_int::test_overflow
void test_overflow()
Definition: ap_int.h:92
GlobalSim::ap_int::ap_int
ap_int()=default
ActsTrk::to_string
std::string to_string(const DetectorType &type)
Definition: GeometryDefs.h:34
GlobalSim::ap_int::form
static ap_int form(T v)
Definition: ap_int.h:40
python.PyAthena.v
v
Definition: PyAthena.py:154
Pythia8_RapidityOrderMPI.val
val
Definition: Pythia8_RapidityOrderMPI.py:14
GlobalSim::ap_int::operator/
ap_int operator/(const ap_int &f) const
Definition: ap_int.h:77
GlobalSim::ap_int::m_value
T m_value
Definition: ap_int.h:24
fitman.k
k
Definition: fitman.py:528