ATLAS Offline Software
Loading...
Searching...
No Matches
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
20namespace 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));
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;
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;
63 return *this;
64 }
65
66 ap_int operator * (const ap_int& f) const {
67 return form(this->m_value * f.m_value);
68 }
69
70 const ap_int& operator *= (const ap_int& f) {
71 this->m_value -= this->m_value * f.m_value;
73 return *this;
74 }
75
76
77 ap_int operator / (const ap_int& f) const {
78 return form(this->m_value / f.m_value);
79 }
80
81 const ap_int& operator /= (const ap_int& f) {
82 this->m_value /= this->m_value / f.m_value;
84 return *this;
85 }
86
87 // negation
89 return form(-this->m_value);
90 }
91
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
AlgTool that to test whether expected the TIP values generated by data supplied by eEmMultTestBench c...
ap_int operator*(const ap_int &f) const
Definition ap_int.h:66
const ap_int & operator*=(const ap_int &f)
Definition ap_int.h:70
const ap_int & operator+=(const ap_int &f)
Definition ap_int.h:49
static ap_int form(T v)
Definition ap_int.h:40
const ap_int & operator/=(const ap_int &f)
Definition ap_int.h:81
ap_int(const double d)
Definition ap_int.h:30
const ap_int operator+(const ap_int &f) const
Definition ap_int.h:44
ap_int operator/(const ap_int &f) const
Definition ap_int.h:77
void test_overflow()
Definition ap_int.h:92
ap_int operator-() const
Definition ap_int.h:88
const ap_int & operator-=(const ap_int &f)
Definition ap_int.h:60