eigen/test/OffByOneScalar.h
Antonio Sanchez 1e6c6c1576 Replace memset with fill to work for non-trivial scalars.
For custom scalars, zero is not necessarily represented by
a zeroed-out memory block (e.g. gnu MPFR). We therefore
cannot rely on `memset` if we want to fill a matrix or tensor
with zeroes. Instead, we should rely on `fill`, which for trivial
types does end up getting converted to a `memset` under-the-hood
(at least with gcc/clang).

Requires adding a `fill(begin, end, v)` to `TensorDevice`.

Replaced all potentially bad instances of memset with fill.

Fixes #2245.
2021-07-08 18:34:41 +00:00

29 lines
578 B
C++

// A Scalar with internal representation T+1 so that zero is internally
// represented by T(1). This is used to test memory fill.
//
template<typename T>
class OffByOneScalar {
public:
OffByOneScalar() : val_(1) {}
OffByOneScalar(const OffByOneScalar& other) {
*this = other;
}
OffByOneScalar& operator=(const OffByOneScalar& other) {
val_ = other.val_;
return *this;
}
OffByOneScalar(T val) : val_(val + 1) {}
OffByOneScalar& operator=(T val) {
val_ = val + 1;
}
operator T() const {
return val_ - 1;
}
private:
T val_;
};