Files
slsDetectorPackage/slsSupportLib/include/sls/bit_utils.h
Erik Fröjdh 9f72688b9c
All checks were successful
Build and Deploy on local RHEL9 / build (push) Successful in 2m3s
Build on RHEL9 docker image / build (push) Successful in 3m58s
Build and Deploy on local RHEL8 / build (push) Successful in 5m0s
Build on RHEL8 docker image / build (push) Successful in 5m7s
Run Simulator Tests on local RHEL9 / build (push) Successful in 14m40s
Run Simulator Tests on local RHEL8 / build (push) Successful in 17m4s
Adding offset to RegisterAddress (#1413)
* implemented + and += for RegisterAddres and tests plus test cleanup
2026-03-05 09:32:21 +01:00

120 lines
3.4 KiB
C++

// SPDX-License-Identifier: LGPL-3.0-or-other
// Copyright (C) 2021 Contributors to the SLS Detector Package
#pragma once
#include <bitset>
#include <cstdint>
#include <vector>
namespace sls {
template <typename T> std::vector<int> getSetBits(T val) {
constexpr size_t bitsPerByte = 8;
constexpr size_t numBits = sizeof(T) * bitsPerByte;
std::bitset<numBits> bs(val);
std::vector<int> set_bits;
set_bits.reserve(bs.count());
for (size_t i = 0; i < bs.size(); ++i) {
if (bs[i]) {
set_bits.push_back(static_cast<int>(i));
}
}
return set_bits;
}
class RegisterAddress {
private:
uint32_t value_{0};
public:
constexpr RegisterAddress() noexcept = default;
constexpr explicit RegisterAddress(uint32_t value) : value_(value) {}
std::string str() const;
constexpr uint32_t value() const noexcept { return value_; }
constexpr bool operator==(const RegisterAddress &other) const {
return (value_ == other.value_);
}
constexpr bool operator!=(const RegisterAddress &other) const {
return (value_ != other.value_);
}
constexpr RegisterAddress &operator+=(uint32_t offset) noexcept {
value_ += offset;
return *this;
}
constexpr RegisterAddress operator+(uint32_t offset) const noexcept {
RegisterAddress tmp(*this);
tmp += offset;
return tmp;
}
};
class BitAddress {
private:
RegisterAddress addr_{0};
uint32_t bitPos_{0};
public:
constexpr BitAddress() noexcept = default;
BitAddress(RegisterAddress address, uint32_t bitPosition);
std::string str() const;
constexpr RegisterAddress address() const noexcept { return addr_; }
constexpr uint32_t bitPosition() const noexcept { return bitPos_; }
constexpr bool operator==(const BitAddress &other) const {
return (addr_ == other.addr_ && bitPos_ == other.bitPos_);
}
constexpr bool operator!=(const BitAddress &other) const {
return !(*this == other);
}
};
class RegisterValue {
private:
uint32_t value_{0};
public:
constexpr RegisterValue() noexcept = default;
explicit constexpr RegisterValue(uint32_t value) noexcept : value_(value) {}
std::string str() const;
constexpr uint32_t value() const noexcept { return value_; }
constexpr RegisterValue &operator|=(const RegisterValue &rhs) noexcept {
value_ |= rhs.value();
return *this;
}
constexpr RegisterValue operator|(const RegisterValue &rhs) const noexcept {
RegisterValue tmp(*this);
tmp |= rhs;
return tmp;
}
constexpr RegisterValue &operator|=(uint32_t rhs) noexcept {
value_ |= rhs;
return *this;
}
constexpr RegisterValue operator|(uint32_t rhs) const noexcept {
RegisterValue tmp(*this);
tmp |= rhs;
return tmp;
}
constexpr bool operator==(const RegisterValue &other) const noexcept {
return value_ == other.value_;
}
constexpr bool operator!=(const RegisterValue &other) const noexcept {
return value_ != other.value_;
}
};
std::ostream &operator<<(std::ostream &os, const RegisterAddress &r);
std::ostream &operator<<(std::ostream &os, const BitAddress &r);
std::ostream &operator<<(std::ostream &os, const RegisterValue &r);
constexpr RegisterAddress operator+(uint32_t offset,
const RegisterAddress &addr) noexcept {
return addr + offset;
}
} // namespace sls