ATLAS Offline Software
Loading...
Searching...
No Matches
GlobalSim::BitField< Lo, Hi, AuxValue, Value, Signed > Class Template Reference

#include <BitSpec.h>

Collaboration diagram for GlobalSim::BitField< Lo, Hi, AuxValue, Value, Signed >:

Classes

struct  Params

Public Types

using value_type = Value
using bits_type = std::bitset<width>
using encoder_type = std::function<bits_type(Value)>
using decoder_type = std::function<std::pair<Value,Value>(bits_type)>

Public Member Functions

constexpr BitField (std::string_view name, std::string_view auxvar, std::string_view description, encoder_type encoder=nullptr, decoder_type decoder=nullptr, Value scale=0, Value offset=0)
constexpr BitField (const Params &p)
constexpr std::string_view name () const
constexpr std::string_view description () const
Value value (const SG::AuxElement &obj) const
Value value (const SG::AuxElement &obj, bool truncate) const
AuxValue & store (SG::AuxElement &obj, Value value) const
bits_type encode (Value value) const
template<typename... Args>
bits_type encode (const std::variant< Args... > &value) const
bits_type bits (const SG::AuxElement &obj) const
std::pair< Value, Value > range (bits_type bits) const
Value decode (bits_type bits) const
template<std::size_t N>
bits_type extract (const std::bitset< N > &packed) const
template<std::size_t N>
Value decodeFrom (const std::bitset< N > &packed) const
template<std::size_t N>
void decodeAndAssignFrom (const std::bitset< N > &packed, SG::AuxElement &obj) const
template<std::size_t N>
void pack (std::bitset< N > &result, const SG::AuxElement &obj) const

Static Public Member Functions

static std::int64_t code (bits_type bits)

Public Attributes

AuxSpec auxspec

Static Public Attributes

static constexpr unsigned lo = Lo
static constexpr unsigned hi = Hi
static constexpr unsigned width = Hi - Lo + 1

Private Attributes

std::string_view m_name
std::string_view m_description
encoder_type m_encoder
decoder_type m_decoder
SG::ConstAccessor< AuxValue > m_acc
SG::Accessor< AuxValue > m_wacc

Detailed Description

template<unsigned Lo, unsigned Hi, typename AuxValue, typename Value = AuxValue, bool Signed = false>
class GlobalSim::BitField< Lo, Hi, AuxValue, Value, Signed >

Definition at line 122 of file BitSpec.h.

Member Typedef Documentation

◆ bits_type

template<unsigned Lo, unsigned Hi, typename AuxValue, typename Value = AuxValue, bool Signed = false>
using GlobalSim::BitField< Lo, Hi, AuxValue, Value, Signed >::bits_type = std::bitset<width>

Definition at line 143 of file BitSpec.h.

◆ decoder_type

template<unsigned Lo, unsigned Hi, typename AuxValue, typename Value = AuxValue, bool Signed = false>
using GlobalSim::BitField< Lo, Hi, AuxValue, Value, Signed >::decoder_type = std::function<std::pair<Value,Value>(bits_type)>

Definition at line 146 of file BitSpec.h.

◆ encoder_type

template<unsigned Lo, unsigned Hi, typename AuxValue, typename Value = AuxValue, bool Signed = false>
using GlobalSim::BitField< Lo, Hi, AuxValue, Value, Signed >::encoder_type = std::function<bits_type(Value)>

Definition at line 145 of file BitSpec.h.

◆ value_type

template<unsigned Lo, unsigned Hi, typename AuxValue, typename Value = AuxValue, bool Signed = false>
using GlobalSim::BitField< Lo, Hi, AuxValue, Value, Signed >::value_type = Value

Definition at line 142 of file BitSpec.h.

Constructor & Destructor Documentation

◆ BitField() [1/2]

template<unsigned Lo, unsigned Hi, typename AuxValue, typename Value = AuxValue, bool Signed = false>
GlobalSim::BitField< Lo, Hi, AuxValue, Value, Signed >::BitField ( std::string_view name,
std::string_view auxvar,
std::string_view description,
encoder_type encoder = nullptr,
decoder_type decoder = nullptr,
Value scale = 0,
Value offset = 0 )
inlineconstexpr

Definition at line 148 of file BitSpec.h.

156 m_name(name),
161 if(!m_encoder && !m_decoder && scale) {
163 return bits_type{ static_cast<uint64_t>(std::round((v-offset)/scale)) };
164 };
166 const double c = static_cast<double>(code(bits));
167 return {static_cast<Value>(offset + (c-0.5)*scale),
168 static_cast<Value>(offset + (c+0.5)*scale) };
169 };
170 }
171 }
constexpr std::string_view name() const
Definition BitSpec.h:192
std::string_view m_description
Definition BitSpec.h:385
static std::int64_t code(bits_type bits)
Definition BitSpec.h:284
SG::ConstAccessor< AuxValue > m_acc
Definition BitSpec.h:390
bits_type bits(const SG::AuxElement &obj) const
Definition BitSpec.h:298
constexpr std::string_view description() const
Definition BitSpec.h:194
std::bitset< width > bits_type
Definition BitSpec.h:143
std::string_view m_name
Definition BitSpec.h:384
decoder_type m_decoder
Definition BitSpec.h:388
SG::Accessor< AuxValue > m_wacc
Definition BitSpec.h:391
encoder_type m_encoder
Definition BitSpec.h:387

◆ BitField() [2/2]

template<unsigned Lo, unsigned Hi, typename AuxValue, typename Value = AuxValue, bool Signed = false>
GlobalSim::BitField< Lo, Hi, AuxValue, Value, Signed >::BitField ( const Params & p)
inlineconstexpr

Definition at line 186 of file BitSpec.h.

186: BitField(p.name,p.auxvar,p.description,p.encoder,p.decoder,p.scale,p.offset) { }
constexpr BitField(std::string_view name, std::string_view auxvar, std::string_view description, encoder_type encoder=nullptr, decoder_type decoder=nullptr, Value scale=0, Value offset=0)
Definition BitSpec.h:148

Member Function Documentation

◆ bits()

template<unsigned Lo, unsigned Hi, typename AuxValue, typename Value = AuxValue, bool Signed = false>
bits_type GlobalSim::BitField< Lo, Hi, AuxValue, Value, Signed >::bits ( const SG::AuxElement & obj) const
inline

Definition at line 298 of file BitSpec.h.

298 {
299 return encode(value(obj));
300 }
Value value(const SG::AuxElement &obj) const
Definition BitSpec.h:200
bits_type encode(Value value) const
Definition BitSpec.h:248

◆ code()

template<unsigned Lo, unsigned Hi, typename AuxValue, typename Value = AuxValue, bool Signed = false>
std::int64_t GlobalSim::BitField< Lo, Hi, AuxValue, Value, Signed >::code ( bits_type bits)
inlinestatic

Definition at line 284 of file BitSpec.h.

284 {
285 auto value = bits.to_ullong();
286 if constexpr (Signed) {
287 if (bits[width - 1]) {
288 value -= (std::uint64_t{1} << width);
289 }
290 }
291 return static_cast<std::int64_t>(value);
292 }
static constexpr unsigned width
Definition BitSpec.h:131

◆ decode()

template<unsigned Lo, unsigned Hi, typename AuxValue, typename Value = AuxValue, bool Signed = false>
Value GlobalSim::BitField< Lo, Hi, AuxValue, Value, Signed >::decode ( bits_type bits) const
inline

Definition at line 317 of file BitSpec.h.

317 {
318 const auto [lo,hi] = range(bits);
319 if (lo == hi) {
320 return lo;
321 }
322
324 return static_cast<Value>(
325 (static_cast<double>(lo) + static_cast<double>(hi)) / 2.0);
326 } else {
327 // No well-defined midpoint for this Value type -- fall back to
328 // the lower bound rather than attempting arithmetic on it.
329 return lo;
330 }
331 }
static constexpr unsigned lo
Definition BitSpec.h:129
static constexpr unsigned hi
Definition BitSpec.h:130
std::pair< Value, Value > range(bits_type bits) const
Definition BitSpec.h:306

◆ decodeAndAssignFrom()

template<unsigned Lo, unsigned Hi, typename AuxValue, typename Value = AuxValue, bool Signed = false>
template<std::size_t N>
void GlobalSim::BitField< Lo, Hi, AuxValue, Value, Signed >::decodeAndAssignFrom ( const std::bitset< N > & packed,
SG::AuxElement & obj ) const
inline

Definition at line 359 of file BitSpec.h.

359 {
361 }
bits_type extract(const std::bitset< N > &packed) const
Definition BitSpec.h:338
AuxValue & store(SG::AuxElement &obj, Value value) const
Definition BitSpec.h:222
Value decode(bits_type bits) const
Definition BitSpec.h:317

◆ decodeFrom()

template<unsigned Lo, unsigned Hi, typename AuxValue, typename Value = AuxValue, bool Signed = false>
template<std::size_t N>
Value GlobalSim::BitField< Lo, Hi, AuxValue, Value, Signed >::decodeFrom ( const std::bitset< N > & packed) const
inline

Definition at line 354 of file BitSpec.h.

354 {
355 return decode(extract(packed));
356 }

◆ description()

template<unsigned Lo, unsigned Hi, typename AuxValue, typename Value = AuxValue, bool Signed = false>
std::string_view GlobalSim::BitField< Lo, Hi, AuxValue, Value, Signed >::description ( ) const
inlineconstexpr

Definition at line 194 of file BitSpec.h.

194{ return m_description; }

◆ encode() [1/2]

template<unsigned Lo, unsigned Hi, typename AuxValue, typename Value = AuxValue, bool Signed = false>
template<typename... Args>
bits_type GlobalSim::BitField< Lo, Hi, AuxValue, Value, Signed >::encode ( const std::variant< Args... > & value) const
inline

Definition at line 264 of file BitSpec.h.

264 {
265 return std::visit(
266 [this](const auto& v) -> bits_type {
267 using T = std::decay_t<decltype(v)>;
268
269 if constexpr (std::is_convertible_v<T, Value>) {
270 return encode(
271 static_cast<Value>(v));
272 }
273 else {
275 "Variant alternative cannot be converted "
276 "to BitField value type");
277 }
278 },
279 value);
280 }

◆ encode() [2/2]

template<unsigned Lo, unsigned Hi, typename AuxValue, typename Value = AuxValue, bool Signed = false>
bits_type GlobalSim::BitField< Lo, Hi, AuxValue, Value, Signed >::encode ( Value value) const
inline

Definition at line 248 of file BitSpec.h.

248 {
249 if (m_encoder)
250 return m_encoder(value);
251
252 // Default behaviour: integral conversion.
253
254 // should we check for out-of-range given size of bitset?
255
256 return bits_type{
257 static_cast<unsigned long long>(value)
258 };
259 }

◆ extract()

template<unsigned Lo, unsigned Hi, typename AuxValue, typename Value = AuxValue, bool Signed = false>
template<std::size_t N>
bits_type GlobalSim::BitField< Lo, Hi, AuxValue, Value, Signed >::extract ( const std::bitset< N > & packed) const
inline

Definition at line 338 of file BitSpec.h.

338 {
339 static_assert(Hi < N, "BitField extends beyond the specification width");
340
342
343 for (unsigned i = 0; i < width; ++i)
344 result[i] = packed[Lo + i];
345
346 return result;
347 }

◆ name()

template<unsigned Lo, unsigned Hi, typename AuxValue, typename Value = AuxValue, bool Signed = false>
std::string_view GlobalSim::BitField< Lo, Hi, AuxValue, Value, Signed >::name ( ) const
inlineconstexpr

Definition at line 192 of file BitSpec.h.

192{ return m_name; }

◆ pack()

template<unsigned Lo, unsigned Hi, typename AuxValue, typename Value = AuxValue, bool Signed = false>
template<std::size_t N>
void GlobalSim::BitField< Lo, Hi, AuxValue, Value, Signed >::pack ( std::bitset< N > & result,
const SG::AuxElement & obj ) const
inline

Definition at line 368 of file BitSpec.h.

370 {
371 static_assert(
372 Hi < N,
373 "BitField extends beyond the specification width");
374
375 const bits_type field_bits = bits(obj);
376
377 for (unsigned i = 0; i < width; ++i)
378 result[Lo + i] = field_bits[i];
379 }

◆ range()

template<unsigned Lo, unsigned Hi, typename AuxValue, typename Value = AuxValue, bool Signed = false>
std::pair< Value, Value > GlobalSim::BitField< Lo, Hi, AuxValue, Value, Signed >::range ( bits_type bits) const
inline

Definition at line 306 of file BitSpec.h.

306 {
307 if (m_decoder)
308 return m_decoder(bits);
309 const Value v = static_cast<Value>(code(bits));
310 return { v, v };
311 }

◆ store()

template<unsigned Lo, unsigned Hi, typename AuxValue, typename Value = AuxValue, bool Signed = false>
AuxValue & GlobalSim::BitField< Lo, Hi, AuxValue, Value, Signed >::store ( SG::AuxElement & obj,
Value value ) const
inline

Definition at line 222 of file BitSpec.h.

222 {
223 if constexpr (std::is_integral_v<AuxValue>)
224 { // ensures have bitwise operators, has_mask is not constexpr so must hide this from invalid AuxValue types
225 // leaving this commented as may revert to constexpr again at some point
226 // if constexpr (has_mask) {
227 // auto& val = m_wacc(obj);
228 // val = (val & ~Mask) | ((value << shift) & Mask);
229 if (auxspec.has_mask) {
230 auto &val = m_wacc(obj);
231 //avoid unintended sign extension, make everything unsigned
233 const auto mask = static_cast<unsigned_aux_type>(auxspec.mask);
234 const auto current = static_cast<unsigned_aux_type>(val);
235 const auto encoded = (static_cast<unsigned_aux_type>(value) << auxspec.shift) & mask;
236 val = static_cast<AuxValue>((current & ~mask) | encoded);
237 return val;
238 }
239 }
240 return (m_wacc(obj) = value);
241
242 }

◆ value() [1/2]

template<unsigned Lo, unsigned Hi, typename AuxValue, typename Value = AuxValue, bool Signed = false>
Value GlobalSim::BitField< Lo, Hi, AuxValue, Value, Signed >::value ( const SG::AuxElement & obj) const
inline

Definition at line 200 of file BitSpec.h.

200 {
201
202 if constexpr (std::is_integral_v<AuxValue>)
203 { // ensures have bitwise operators
204
205 /*if constexpr(has_mask) {
206 return (m_acc(obj) & Mask) >> shift;*/
207 if (auxspec.has_mask) {
208 return decode((m_acc(obj) & auxspec.mask) >> auxspec.shift);
209 }
210 }
211
212 return m_acc(obj);
213
214 }

◆ value() [2/2]

template<unsigned Lo, unsigned Hi, typename AuxValue, typename Value = AuxValue, bool Signed = false>
Value GlobalSim::BitField< Lo, Hi, AuxValue, Value, Signed >::value ( const SG::AuxElement & obj,
bool truncate ) const
inline

Definition at line 217 of file BitSpec.h.

217 {
218 if(!truncate) return value(obj);
219 return decode(encode(value(obj)));
220 }

Member Data Documentation

◆ auxspec

template<unsigned Lo, unsigned Hi, typename AuxValue, typename Value = AuxValue, bool Signed = false>
AuxSpec GlobalSim::BitField< Lo, Hi, AuxValue, Value, Signed >::auxspec

Definition at line 381 of file BitSpec.h.

◆ hi

template<unsigned Lo, unsigned Hi, typename AuxValue, typename Value = AuxValue, bool Signed = false>
unsigned GlobalSim::BitField< Lo, Hi, AuxValue, Value, Signed >::hi = Hi
staticconstexpr

Definition at line 130 of file BitSpec.h.

◆ lo

template<unsigned Lo, unsigned Hi, typename AuxValue, typename Value = AuxValue, bool Signed = false>
unsigned GlobalSim::BitField< Lo, Hi, AuxValue, Value, Signed >::lo = Lo
staticconstexpr

Definition at line 129 of file BitSpec.h.

◆ m_acc

template<unsigned Lo, unsigned Hi, typename AuxValue, typename Value = AuxValue, bool Signed = false>
SG::ConstAccessor<AuxValue> GlobalSim::BitField< Lo, Hi, AuxValue, Value, Signed >::m_acc
private

Definition at line 390 of file BitSpec.h.

◆ m_decoder

template<unsigned Lo, unsigned Hi, typename AuxValue, typename Value = AuxValue, bool Signed = false>
decoder_type GlobalSim::BitField< Lo, Hi, AuxValue, Value, Signed >::m_decoder
private

Definition at line 388 of file BitSpec.h.

◆ m_description

template<unsigned Lo, unsigned Hi, typename AuxValue, typename Value = AuxValue, bool Signed = false>
std::string_view GlobalSim::BitField< Lo, Hi, AuxValue, Value, Signed >::m_description
private

Definition at line 385 of file BitSpec.h.

◆ m_encoder

template<unsigned Lo, unsigned Hi, typename AuxValue, typename Value = AuxValue, bool Signed = false>
encoder_type GlobalSim::BitField< Lo, Hi, AuxValue, Value, Signed >::m_encoder
private

Definition at line 387 of file BitSpec.h.

◆ m_name

template<unsigned Lo, unsigned Hi, typename AuxValue, typename Value = AuxValue, bool Signed = false>
std::string_view GlobalSim::BitField< Lo, Hi, AuxValue, Value, Signed >::m_name
private

Definition at line 384 of file BitSpec.h.

◆ m_wacc

template<unsigned Lo, unsigned Hi, typename AuxValue, typename Value = AuxValue, bool Signed = false>
SG::Accessor<AuxValue> GlobalSim::BitField< Lo, Hi, AuxValue, Value, Signed >::m_wacc
private

Definition at line 391 of file BitSpec.h.

◆ width

template<unsigned Lo, unsigned Hi, typename AuxValue, typename Value = AuxValue, bool Signed = false>
unsigned GlobalSim::BitField< Lo, Hi, AuxValue, Value, Signed >::width = Hi - Lo + 1
staticconstexpr

Definition at line 131 of file BitSpec.h.


The documentation for this class was generated from the following file: