mirror of
https://github.com/TomHarte/CLK.git
synced 2025-08-16 05:27:43 +00:00
Experimentally goes to town on constexpr
.
This commit is contained in:
@@ -52,79 +52,79 @@
|
|||||||
*/
|
*/
|
||||||
template <class T> class WrappedInt {
|
template <class T> class WrappedInt {
|
||||||
public:
|
public:
|
||||||
inline WrappedInt(int l) : length_(l) {}
|
constexpr WrappedInt(int l) : length_(l) {}
|
||||||
inline WrappedInt() : length_(0) {}
|
constexpr WrappedInt() : length_(0) {}
|
||||||
|
|
||||||
inline T &operator =(const T &rhs) {
|
constexpr T &operator =(const T &rhs) {
|
||||||
length_ = rhs.length_;
|
length_ = rhs.length_;
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
inline T &operator +=(const T &rhs) {
|
constexpr T &operator +=(const T &rhs) {
|
||||||
length_ += rhs.length_;
|
length_ += rhs.length_;
|
||||||
return *static_cast<T *>(this);
|
return *static_cast<T *>(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
inline T &operator -=(const T &rhs) {
|
constexpr T &operator -=(const T &rhs) {
|
||||||
length_ -= rhs.length_;
|
length_ -= rhs.length_;
|
||||||
return *static_cast<T *>(this);
|
return *static_cast<T *>(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
inline T &operator ++() {
|
constexpr T &operator ++() {
|
||||||
++ length_;
|
++ length_;
|
||||||
return *static_cast<T *>(this);
|
return *static_cast<T *>(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
inline T &operator ++(int) {
|
constexpr T &operator ++(int) {
|
||||||
length_ ++;
|
length_ ++;
|
||||||
return *static_cast<T *>(this);
|
return *static_cast<T *>(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
inline T &operator --() {
|
constexpr T &operator --() {
|
||||||
-- length_;
|
-- length_;
|
||||||
return *static_cast<T *>(this);
|
return *static_cast<T *>(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
inline T &operator --(int) {
|
constexpr T &operator --(int) {
|
||||||
length_ --;
|
length_ --;
|
||||||
return *static_cast<T *>(this);
|
return *static_cast<T *>(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
inline T &operator %=(const T &rhs) {
|
constexpr T &operator %=(const T &rhs) {
|
||||||
length_ %= rhs.length_;
|
length_ %= rhs.length_;
|
||||||
return *static_cast<T *>(this);
|
return *static_cast<T *>(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
inline T &operator &=(const T &rhs) {
|
constexpr T &operator &=(const T &rhs) {
|
||||||
length_ &= rhs.length_;
|
length_ &= rhs.length_;
|
||||||
return *static_cast<T *>(this);
|
return *static_cast<T *>(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
inline T operator +(const T &rhs) const { return T(length_ + rhs.length_); }
|
constexpr T operator +(const T &rhs) const { return T(length_ + rhs.length_); }
|
||||||
inline T operator -(const T &rhs) const { return T(length_ - rhs.length_); }
|
constexpr T operator -(const T &rhs) const { return T(length_ - rhs.length_); }
|
||||||
|
|
||||||
inline T operator %(const T &rhs) const { return T(length_ % rhs.length_); }
|
constexpr T operator %(const T &rhs) const { return T(length_ % rhs.length_); }
|
||||||
inline T operator &(const T &rhs) const { return T(length_ & rhs.length_); }
|
constexpr T operator &(const T &rhs) const { return T(length_ & rhs.length_); }
|
||||||
|
|
||||||
inline T operator -() const { return T(- length_); }
|
constexpr T operator -() const { return T(- length_); }
|
||||||
|
|
||||||
inline bool operator <(const T &rhs) const { return length_ < rhs.length_; }
|
constexpr bool operator <(const T &rhs) const { return length_ < rhs.length_; }
|
||||||
inline bool operator >(const T &rhs) const { return length_ > rhs.length_; }
|
constexpr bool operator >(const T &rhs) const { return length_ > rhs.length_; }
|
||||||
inline bool operator <=(const T &rhs) const { return length_ <= rhs.length_; }
|
constexpr bool operator <=(const T &rhs) const { return length_ <= rhs.length_; }
|
||||||
inline bool operator >=(const T &rhs) const { return length_ >= rhs.length_; }
|
constexpr bool operator >=(const T &rhs) const { return length_ >= rhs.length_; }
|
||||||
inline bool operator ==(const T &rhs) const { return length_ == rhs.length_; }
|
constexpr bool operator ==(const T &rhs) const { return length_ == rhs.length_; }
|
||||||
inline bool operator !=(const T &rhs) const { return length_ != rhs.length_; }
|
constexpr bool operator !=(const T &rhs) const { return length_ != rhs.length_; }
|
||||||
|
|
||||||
inline bool operator !() const { return !length_; }
|
constexpr bool operator !() const { return !length_; }
|
||||||
// bool operator () is not supported because it offers an implicit cast to int, which is prone silently to permit misuse
|
// bool operator () is not supported because it offers an implicit cast to int, which is prone silently to permit misuse
|
||||||
|
|
||||||
inline int as_int() const { return length_; }
|
constexpr int as_int() const { return length_; }
|
||||||
|
|
||||||
/*!
|
/*!
|
||||||
Severs from @c this the effect of dividing by @c divisor; @c this will end up with
|
Severs from @c this the effect of dividing by @c divisor; @c this will end up with
|
||||||
the value of @c this modulo @c divisor and @c divided by @c divisor is returned.
|
the value of @c this modulo @c divisor and @c divided by @c divisor is returned.
|
||||||
*/
|
*/
|
||||||
inline T divide(const T &divisor) {
|
constexpr T divide(const T &divisor) {
|
||||||
T result(length_ / divisor.length_);
|
T result(length_ / divisor.length_);
|
||||||
length_ %= divisor.length_;
|
length_ %= divisor.length_;
|
||||||
return result;
|
return result;
|
||||||
@@ -134,7 +134,7 @@ template <class T> class WrappedInt {
|
|||||||
Flushes the value in @c this. The current value is returned, and the internal value
|
Flushes the value in @c this. The current value is returned, and the internal value
|
||||||
is reset to zero.
|
is reset to zero.
|
||||||
*/
|
*/
|
||||||
inline T flush() {
|
constexpr T flush() {
|
||||||
T result(length_);
|
T result(length_);
|
||||||
length_ = 0;
|
length_ = 0;
|
||||||
return result;
|
return result;
|
||||||
@@ -150,34 +150,34 @@ template <class T> class WrappedInt {
|
|||||||
/// Describes an integer number of whole cycles: pairs of clock signal transitions.
|
/// Describes an integer number of whole cycles: pairs of clock signal transitions.
|
||||||
class Cycles: public WrappedInt<Cycles> {
|
class Cycles: public WrappedInt<Cycles> {
|
||||||
public:
|
public:
|
||||||
inline Cycles(int l) : WrappedInt<Cycles>(l) {}
|
constexpr Cycles(int l) : WrappedInt<Cycles>(l) {}
|
||||||
inline Cycles() : WrappedInt<Cycles>() {}
|
constexpr Cycles() : WrappedInt<Cycles>() {}
|
||||||
inline Cycles(const Cycles &cycles) : WrappedInt<Cycles>(cycles.length_) {}
|
constexpr Cycles(const Cycles &cycles) : WrappedInt<Cycles>(cycles.length_) {}
|
||||||
};
|
};
|
||||||
|
|
||||||
/// Describes an integer number of half cycles: single clock signal transitions.
|
/// Describes an integer number of half cycles: single clock signal transitions.
|
||||||
class HalfCycles: public WrappedInt<HalfCycles> {
|
class HalfCycles: public WrappedInt<HalfCycles> {
|
||||||
public:
|
public:
|
||||||
inline HalfCycles(int l) : WrappedInt<HalfCycles>(l) {}
|
constexpr HalfCycles(int l) : WrappedInt<HalfCycles>(l) {}
|
||||||
inline HalfCycles() : WrappedInt<HalfCycles>() {}
|
constexpr HalfCycles() : WrappedInt<HalfCycles>() {}
|
||||||
|
|
||||||
inline HalfCycles(const Cycles cycles) : WrappedInt<HalfCycles>(cycles.as_int() * 2) {}
|
constexpr HalfCycles(const Cycles cycles) : WrappedInt<HalfCycles>(cycles.as_int() * 2) {}
|
||||||
inline HalfCycles(const HalfCycles &half_cycles) : WrappedInt<HalfCycles>(half_cycles.length_) {}
|
constexpr HalfCycles(const HalfCycles &half_cycles) : WrappedInt<HalfCycles>(half_cycles.length_) {}
|
||||||
|
|
||||||
/// @returns The number of whole cycles completely covered by this span of half cycles.
|
/// @returns The number of whole cycles completely covered by this span of half cycles.
|
||||||
inline Cycles cycles() {
|
constexpr Cycles cycles() {
|
||||||
return Cycles(length_ >> 1);
|
return Cycles(length_ >> 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Flushes the whole cycles in @c this, subtracting that many from the total stored here.
|
/// Flushes the whole cycles in @c this, subtracting that many from the total stored here.
|
||||||
inline Cycles flush_cycles() {
|
constexpr Cycles flush_cycles() {
|
||||||
Cycles result(length_ >> 1);
|
Cycles result(length_ >> 1);
|
||||||
length_ &= 1;
|
length_ &= 1;
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Flushes the half cycles in @c this, returning the number stored and setting this total to zero.
|
/// Flushes the half cycles in @c this, returning the number stored and setting this total to zero.
|
||||||
inline HalfCycles flush() {
|
constexpr HalfCycles flush() {
|
||||||
HalfCycles result(length_);
|
HalfCycles result(length_);
|
||||||
length_ = 0;
|
length_ = 0;
|
||||||
return result;
|
return result;
|
||||||
@@ -187,7 +187,7 @@ class HalfCycles: public WrappedInt<HalfCycles> {
|
|||||||
Severs from @c this the effect of dividing by @c divisor; @c this will end up with
|
Severs from @c this the effect of dividing by @c divisor; @c this will end up with
|
||||||
the value of @c this modulo @c divisor and @c divided by @c divisor is returned.
|
the value of @c this modulo @c divisor and @c divided by @c divisor is returned.
|
||||||
*/
|
*/
|
||||||
inline Cycles divide_cycles(const Cycles &divisor) {
|
constexpr Cycles divide_cycles(const Cycles &divisor) {
|
||||||
HalfCycles half_divisor = HalfCycles(divisor);
|
HalfCycles half_divisor = HalfCycles(divisor);
|
||||||
Cycles result(length_ / half_divisor.length_);
|
Cycles result(length_ / half_divisor.length_);
|
||||||
length_ %= half_divisor.length_;
|
length_ %= half_divisor.length_;
|
||||||
@@ -203,7 +203,6 @@ template <class T> class HalfClockReceiver: public T {
|
|||||||
public:
|
public:
|
||||||
using T::T;
|
using T::T;
|
||||||
|
|
||||||
using T::run_for;
|
|
||||||
inline void run_for(const HalfCycles half_cycles) {
|
inline void run_for(const HalfCycles half_cycles) {
|
||||||
half_cycles_ += half_cycles;
|
half_cycles_ += half_cycles;
|
||||||
T::run_for(half_cycles_.flush_cycles());
|
T::run_for(half_cycles_.flush_cycles());
|
||||||
|
Reference in New Issue
Block a user