ATLAS Offline Software
vec_fb.h
Go to the documentation of this file.
1 // This file's extension implies that it's C, but it's really -*- C++ -*-.
2 /*
3  * Copyright (C) 2002-2023 CERN for the benefit of the ATLAS collaboration.
4  */
16 #ifndef CXXUTILS_VEC_FB_H
17 #define CXXUTILS_VEC_FB_H
18 
19 #include <initializer_list>
20 #include <type_traits>
21 #include <algorithm>
22 #include <cstdint>
23 
24 #include "boost/integer.hpp"
25 
26 namespace CxxUtils {
29 template<typename T, size_t N>
30 struct alignas(N*sizeof(T)) vec_fb
31 {
32  static_assert(std::is_arithmetic_v<T>, "Element type is not arithmetic");
33  static_assert(N > 0 && (N & (N - 1)) == 0, "Size not a power or 2");
34  // cppcheck-suppress uninitMemberVar; deliberate
35  vec_fb() = default;
36  vec_fb(const vec_fb&) = default;
37  vec_fb& operator=(const vec_fb&) = default;
38  vec_fb(std::initializer_list<T> init)
39  {
40  std::copy(init.begin(), init.end(), m_arr);
41  std::fill(m_arr + init.size(), m_arr + N, T());
42  }
43 
44  T operator[](size_t n) const { return m_arr[n]; }
45  T& operator[](size_t n) { return m_arr[n]; }
46 
47  T m_arr[N];
48 };
49 
50 // Helper: Given a vectorized class, find another vectorized class
51 // that uses integers of the same size as the original class.
52 template <typename T, size_t N>
53 using ivec = vec_fb<typename boost::int_t<sizeof(T) * 8>::exact, N>;
54 
55 // Define binary operations.
56 // For each operation, define
57 // V1 OP V2
58 // V OP S
59 // S OP V
60 // V1 OP= V2
61 // V OP= S
62 
63 #define BINOP(op) \
64  template<typename T, size_t N> \
65  inline vec_fb<T, N> operator op(const vec_fb<T, N>& a, \
66  const vec_fb<T, N>& b) \
67  { \
68  vec_fb<T, N> c; \
69  for (size_t i = 0; i < N; ++i) \
70  c.m_arr[i] = a.m_arr[i] op b.m_arr[i]; \
71  return c; \
72  } \
73  template<typename T, size_t N, typename U> \
74  inline vec_fb<T, N> operator op(const vec_fb<T, N>& a, U b) \
75  { \
76  vec_fb<T, N> c; \
77  for (size_t i = 0; i < N; ++i) \
78  c.m_arr[i] = a.m_arr[i] op b; \
79  return c; \
80  } \
81  template<typename T, size_t N, typename U> \
82  inline vec_fb<T, N> operator op(U a, const vec_fb<T, N>& b) \
83  { \
84  vec_fb<T, N> c; \
85  for (size_t i = 0; i < N; ++i) \
86  c.m_arr[i] = a op b.m_arr[i]; \
87  return c; \
88  } \
89  template<typename T, size_t N> \
90  inline vec_fb<T, N>& operator op##=(vec_fb<T, N>& a, const vec_fb<T, N>& b) \
91  { \
92  for (size_t i = 0; i < N; ++i) \
93  a.m_arr[i] op## = b.m_arr[i]; \
94  return a; \
95  } \
96  template<typename T, size_t N, typename U> \
97  inline vec_fb<T, N>& operator op##=(vec_fb<T, N>& a, U b) \
98  { \
99  for (size_t i = 0; i < N; ++i) \
100  a.m_arr[i] op## = b; \
101  return a; \
102  }
103 
104 BINOP(+)
105 BINOP(-)
106 BINOP(*)
107 BINOP(/)
108 BINOP(^)
109 BINOP(|)
110 BINOP(&)
111 BINOP(%)
112 BINOP(>>)
113 BINOP(<<)
114 
115 #undef BINOP
116 
117 // Define unary operations.
118 
119 #define UNOP(op) \
120  template<typename T, size_t N> \
121  inline vec_fb<T, N> operator op(const vec_fb<T, N>& a) \
122  { \
123  vec_fb<T, N> c; \
124  for (size_t i = 0; i < N; ++i) \
125  c.m_arr[i] = op a.m_arr[i]; \
126  return c; \
127  }
128 
129 UNOP(-)
130 UNOP(~)
131 
132 #undef UNOP
133 
134 // Define relational operations.
135 
136 #define RELOP(op) \
137  template<typename T, size_t N> \
138  inline ivec<T, N> operator op(const vec_fb<T, N>& a, const vec_fb<T, N>& b) \
139  { \
140  ivec<T, N> c; \
141  for (size_t i = 0; i < N; ++i) \
142  c.m_arr[i] = a.m_arr[i] op b.m_arr[i]; \
143  return c; \
144  }
145 
146 RELOP(==)
147 RELOP(!=)
148 RELOP(<)
149 RELOP(<=)
150 RELOP(>)
151 RELOP(>=)
152 
153 #undef RELOP
154 
156 template<typename T, size_t N>
157 inline ivec<T, N>
159 {
160  ivec<T, N> c;
161  for (size_t i = 0; i < N; ++i)
162  c.m_arr[i] = a.m_arr[i] == 0;
163  return c;
164 }
165 
167 template<typename T, size_t N>
168 inline ivec<T, N>
170 {
171  ivec<T, N> c;
172  for (size_t i = 0; i < N; ++i)
173  c.m_arr[i] = (a.m_arr[i] != 0) & (b.m_arr[i] != 0);
174  return c;
175 }
176 
178 template<typename T, size_t N, class U>
179 inline ivec<T, N>
181 {
182  ivec<T, N> c;
183  for (size_t i = 0; i < N; ++i)
184  c.m_arr[i] = a ? b.m_arr[i] != 0 : 0;
185  return c;
186 }
187 
189 template<typename T, size_t N, class U>
190 inline ivec<T, N>
192 {
193  ivec<T, N> c;
194  for (size_t i = 0; i < N; ++i)
195  c.m_arr[i] = (a.m_arr[i] != 0) & (b ? -1 : 0);
196  return c;
197 }
198 
200 template<typename T, size_t N>
201 inline ivec<T, N>
203 {
204  ivec<T, N> c;
205  for (size_t i = 0; i < N; ++i)
206  c.m_arr[i] = (a.m_arr[i] != 0) | (b.m_arr[i] != 0);
207  return c;
208 }
209 
210 } // namespace CxxUtils
211 
212 #endif // CXXUTILS_VEC_FB_H
CxxUtils::vec_fb::vec_fb
vec_fb(const vec_fb &)=default
RELOP
#define RELOP(op)
Definition: vec_fb.h:136
CxxUtils::operator&&
ivec< T, N > operator&&(const vec_fb< T, N > &a, const vec_fb< T, N > &b)
V1 && V2.
Definition: vec_fb.h:169
CxxUtils::operator!
ivec< T, N > operator!(const vec_fb< T, N > &a)
Negation.
Definition: vec_fb.h:158
BINOP
#define BINOP(op)
Definition: vec_fb.h:63
JetTiledMap::N
@ N
Definition: TiledEtaPhiMap.h:44
CxxUtils::vec_fb
Definition: vec_fb.h:31
CxxUtils::vec_fb::operator=
vec_fb & operator=(const vec_fb &)=default
lumiFormat.i
int i
Definition: lumiFormat.py:85
CxxUtils::operator||
ivec< T, N > operator||(const vec_fb< T, N > &a, const vec_fb< T, N > &b)
V1 || V2.
Definition: vec_fb.h:202
beamspotman.n
n
Definition: beamspotman.py:731
CxxUtils
Definition: aligned_vector.h:29
UNOP
#define UNOP(op)
Definition: vec_fb.h:119
CxxUtils::vec_fb::vec_fb
vec_fb()=default
fill
void fill(H5::Group &out_file, size_t iterations)
Definition: test-hdf5-writer.cxx:95
plotBeamSpotMon.b
b
Definition: plotBeamSpotMon.py:77
python.PyKernel.init
def init(v_theApp, v_rootStream=None)
Definition: PyKernel.py:45
CxxUtils::vec_fb::operator[]
T & operator[](size_t n)
Definition: vec_fb.h:45
CxxUtils::vec_fb::operator[]
T operator[](size_t n) const
Definition: vec_fb.h:44
a
TList * a
Definition: liststreamerinfos.cxx:10
calibdata.copy
bool copy
Definition: calibdata.py:27
CxxUtils::vec_fb::vec_fb
vec_fb(std::initializer_list< T > init)
Definition: vec_fb.h:38
CxxUtils::vec_fb::m_arr
T m_arr[N]
Definition: vec_fb.h:47
python.compressB64.c
def c
Definition: compressB64.py:93