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

#include <BitSpec.h>

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

Classes

struct  Params

Public Types

using value_type = Value
using bits_type = Bitset<width,Clamped,Signed>
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, double centre=0.5, bool lower_inc=true)
constexpr BitField (const Params &p)
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
template<typename... Args>
std::pair< std::int64_t, std::pair< bool, bool > > encodeAndCheckBoundary (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
std::string_view name
std::string_view description

Static Public Attributes

static constexpr unsigned lo = Lo
static constexpr unsigned hi = Hi
static constexpr unsigned width = Hi - Lo + 1
static constexpr auto maxCode = bits_type::max
static constexpr auto minCode = bits_type::min

Private Attributes

encoder_type m_encoder
decoder_type m_decoder
Value m_valueoffset {}
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, bool Clamped = true>
class GlobalSim::BitField< Lo, Hi, AuxValue, Value, Signed, Clamped >

Definition at line 205 of file BitSpec.h.

Member Typedef Documentation

◆ bits_type

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

Definition at line 226 of file BitSpec.h.

◆ decoder_type

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

Definition at line 232 of file BitSpec.h.

◆ encoder_type

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

Definition at line 231 of file BitSpec.h.

◆ value_type

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

Definition at line 225 of file BitSpec.h.

Constructor & Destructor Documentation

◆ BitField() [1/2]

template<unsigned Lo, unsigned Hi, typename AuxValue, typename Value = AuxValue, bool Signed = false, bool Clamped = true>
GlobalSim::BitField< Lo, Hi, AuxValue, Value, Signed, Clamped >::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,
double centre = 0.5,
bool lower_inc = true )
inlineconstexpr

Definition at line 234 of file BitSpec.h.

242 name(name_),
247 if(!m_encoder && !m_decoder && scale) {
248 if constexpr (Signed) {
249 // need to cast to signed int, as casting a negative float to unsigned int is undefined behaviour
250 // was previously then explicitly casting to uint64_t when bits_type was a std::bitset
251 // which worked. But now using clampable bitsets, the behaviour isn't quite the same
252 if(lower_inc) {
254 return bits_type{static_cast<int64_t>(std::floor((v - offset) / scale))};
255 };
256 } else {
258 return bits_type{static_cast<int64_t>((std::ceil((v - offset) / scale) - 1))};
259 };
260 }
261 } else {
263 return bits_type{static_cast<uint64_t>(lower_inc ? std::floor((v - offset) / scale) : (
264 std::ceil((v - offset) / scale) - 1))};
265 };
266 }
268 const double c = static_cast<double>(code(bits));
269 return {static_cast<Value>(offset + c*scale),
270 static_cast<Value>(offset + (c+1.0)*scale) };
271 };
272 }
274 {
276 }
277 }
static std::int64_t code(bits_type bits)
Definition BitSpec.h:413
std::string_view name
Definition BitSpec.h:510
std::string_view description
Definition BitSpec.h:511
encoder_type m_encoder
Definition BitSpec.h:516
decoder_type m_decoder
Definition BitSpec.h:517
bits_type bits(const SG::AuxElement &obj) const
Definition BitSpec.h:427
SG::Accessor< AuxValue > m_wacc
Definition BitSpec.h:521
SG::ConstAccessor< AuxValue > m_acc
Definition BitSpec.h:520
Bitset< width, Clamped, Signed > bits_type
Definition BitSpec.h:226

◆ BitField() [2/2]

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

Definition at line 294 of file BitSpec.h.

294: BitField(p.name,p.auxvar.empty()?p.name:p.auxvar,p.description,p.encoder,p.decoder,p.scale,p.offset,p.centre,p.lower_incl) { }
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, double centre=0.5, bool lower_inc=true)
Definition BitSpec.h:234

Member Function Documentation

◆ bits()

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

Definition at line 427 of file BitSpec.h.

427 {
428 return encode(value(obj));
429 }
bits_type encode(Value value) const
Definition BitSpec.h:356
Value value(const SG::AuxElement &obj) const
Definition BitSpec.h:308

◆ code()

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

Definition at line 413 of file BitSpec.h.

413 {
414 auto value = bits.to_ullong();
415 if constexpr (Signed) {
416 if (bits[width - 1]) {
417 value -= (std::uint64_t{1} << width);
418 }
419 }
420 return static_cast<std::int64_t>(value);
421 }
static constexpr unsigned width
Definition BitSpec.h:214

◆ decode()

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

Definition at line 446 of file BitSpec.h.

446 {
447 const auto [lo,hi] = range(bits);
448 if (lo == hi) {
449 return lo;
450 }
451
453 return (lo+m_valueoffset*(hi-lo));
454 } else {
455 // No well-defined midpoint for this Value type -- fall back to
456 // the lower bound rather than attempting arithmetic on it.
457 return lo;
458 }
459 }
std::pair< Value, Value > range(bits_type bits) const
Definition BitSpec.h:435
static constexpr unsigned lo
Definition BitSpec.h:212
static constexpr unsigned hi
Definition BitSpec.h:213

◆ decodeAndAssignFrom()

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

Definition at line 487 of file BitSpec.h.

487 {
489 }
Value decode(bits_type bits) const
Definition BitSpec.h:446
bits_type extract(const std::bitset< N > &packed) const
Definition BitSpec.h:466
AuxValue & store(SG::AuxElement &obj, Value value) const
Definition BitSpec.h:330

◆ decodeFrom()

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

Definition at line 482 of file BitSpec.h.

482 {
483 return decode(extract(packed));
484 }

◆ encode() [1/2]

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

Definition at line 372 of file BitSpec.h.

372 {
373 return std::visit(
374 [this](const auto& v) -> bits_type {
375 using T = std::decay_t<decltype(v)>;
376
377 if constexpr (std::is_convertible_v<T, Value>) {
378 return encode(
379 static_cast<Value>(v));
380 }
381 else {
383 "Variant alternative cannot be converted "
384 "to BitField value type");
385 }
386 },
387 value);
388 }

◆ encode() [2/2]

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

Definition at line 356 of file BitSpec.h.

356 {
357 if (m_encoder)
358 return m_encoder(value);
359
360 // Default behaviour: integral conversion.
361
362 // should we check for out-of-range given size of bitset?
363
364 return bits_type{
365 static_cast<unsigned long long>(value)
366 };
367 }

◆ encodeAndCheckBoundary()

template<unsigned Lo, unsigned Hi, typename AuxValue, typename Value = AuxValue, bool Signed = false, bool Clamped = true>
template<typename... Args>
std::pair< std::int64_t, std::pair< bool, bool > > GlobalSim::BitField< Lo, Hi, AuxValue, Value, Signed, Clamped >::encodeAndCheckBoundary ( const std::variant< Args... > & value) const
inline

Definition at line 393 of file BitSpec.h.

393 {
394 return std::visit(
395 [this](const auto& v) -> std::pair<std::int64_t,std::pair<bool,bool>> {
396 using T = std::decay_t<decltype(v)>;
397
398 if constexpr (std::is_convertible_v<T, Value>) {
399 auto bits = encode(static_cast<Value>(v));
400 auto r = range(bits);
401 return {code(bits),{v==r.first,v==r.second}};
402 } else {
404 "Variant alternative cannot be converted "
405 "to BitField value type");
406 }
407 },
408 value);
409 }

◆ extract()

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

Definition at line 466 of file BitSpec.h.

466 {
467 static_assert(Hi < N, "BitField extends beyond the specification width");
468
470
471 for (unsigned i = 0; i < width; ++i)
472 result[i] = packed[Lo + i];
473
474 return result;
475 }

◆ pack()

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

Definition at line 496 of file BitSpec.h.

498 {
499 static_assert(
500 Hi < N,
501 "BitField extends beyond the specification width");
502
503 const bits_type field_bits = bits(obj);
504
505 for (unsigned i = 0; i < width; ++i)
506 result[Lo + i] = field_bits[i];
507 }

◆ range()

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

Definition at line 435 of file BitSpec.h.

435 {
436 if (m_decoder)
437 return m_decoder(bits);
438 const Value v = static_cast<Value>(code(bits));
439 return { v, v };
440 }

◆ store()

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

Definition at line 330 of file BitSpec.h.

330 {
331 if constexpr (std::is_integral_v<AuxValue>)
332 { // ensures have bitwise operators, has_mask is not constexpr so must hide this from invalid AuxValue types
333 // leaving this commented as may revert to constexpr again at some point
334 // if constexpr (has_mask) {
335 // auto& val = m_wacc(obj);
336 // val = (val & ~Mask) | ((value << shift) & Mask);
337 if (auxspec.has_mask) {
338 auto &val = m_wacc(obj);
339 //avoid unintended sign extension, make everything unsigned
341 const auto mask = static_cast<unsigned_aux_type>(auxspec.mask);
342 const auto current = static_cast<unsigned_aux_type>(val);
343 const auto encoded = (static_cast<unsigned_aux_type>(value) << auxspec.shift) & mask;
344 val = static_cast<AuxValue>((current & ~mask) | encoded);
345 return val;
346 }
347 }
348 return (m_wacc(obj) = value);
349
350 }

◆ value() [1/2]

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

Definition at line 308 of file BitSpec.h.

308 {
309
310 if constexpr (std::is_integral_v<AuxValue>)
311 { // ensures have bitwise operators
312
313 /*if constexpr(has_mask) {
314 return (m_acc(obj) & Mask) >> shift;*/
315 if (auxspec.has_mask) {
316 return decode((m_acc(obj) & auxspec.mask) >> auxspec.shift);
317 }
318 }
319
320 return m_acc(obj);
321
322 }

◆ value() [2/2]

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

Definition at line 325 of file BitSpec.h.

325 {
326 if(!truncate) return value(obj);
327 return decode(encode(value(obj)));
328 }

Member Data Documentation

◆ auxspec

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

Definition at line 509 of file BitSpec.h.

◆ description

template<unsigned Lo, unsigned Hi, typename AuxValue, typename Value = AuxValue, bool Signed = false, bool Clamped = true>
std::string_view GlobalSim::BitField< Lo, Hi, AuxValue, Value, Signed, Clamped >::description

Definition at line 511 of file BitSpec.h.

◆ hi

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

Definition at line 213 of file BitSpec.h.

◆ lo

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

Definition at line 212 of file BitSpec.h.

◆ m_acc

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

Definition at line 520 of file BitSpec.h.

◆ m_decoder

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

Definition at line 517 of file BitSpec.h.

◆ m_encoder

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

Definition at line 516 of file BitSpec.h.

◆ m_valueoffset

template<unsigned Lo, unsigned Hi, typename AuxValue, typename Value = AuxValue, bool Signed = false, bool Clamped = true>
Value GlobalSim::BitField< Lo, Hi, AuxValue, Value, Signed, Clamped >::m_valueoffset {}
private

Definition at line 518 of file BitSpec.h.

518{}; // used by decode to return a value in the encoder range

◆ m_wacc

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

Definition at line 521 of file BitSpec.h.

◆ maxCode

template<unsigned Lo, unsigned Hi, typename AuxValue, typename Value = AuxValue, bool Signed = false, bool Clamped = true>
auto GlobalSim::BitField< Lo, Hi, AuxValue, Value, Signed, Clamped >::maxCode = bits_type::max
staticconstexpr

Definition at line 228 of file BitSpec.h.

◆ minCode

template<unsigned Lo, unsigned Hi, typename AuxValue, typename Value = AuxValue, bool Signed = false, bool Clamped = true>
auto GlobalSim::BitField< Lo, Hi, AuxValue, Value, Signed, Clamped >::minCode = bits_type::min
staticconstexpr

Definition at line 229 of file BitSpec.h.

◆ name

template<unsigned Lo, unsigned Hi, typename AuxValue, typename Value = AuxValue, bool Signed = false, bool Clamped = true>
std::string_view GlobalSim::BitField< Lo, Hi, AuxValue, Value, Signed, Clamped >::name

Definition at line 510 of file BitSpec.h.

◆ width

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

Definition at line 214 of file BitSpec.h.


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