add operators for self-typed arithmetic

This commit is contained in:
Paul Davis 2020-08-03 14:01:49 -06:00
parent cb78984c63
commit 709df7a08f

View File

@ -71,12 +71,19 @@ class alignas(16) int62_t {
int62_t& operator= (int62_t const & other) { v.store (other.v.load()); return *this; }
int62_t operator- () const { int64_t tmp = v; return int62_t (flagged (tmp), -int62(tmp)); }
int62_t operator+ (int n) const { int64_t tmp = v; return int62_t (flagged (tmp), int62 (tmp) + n); }
int62_t operator- (int n) const { int64_t tmp = v; return int62_t (flagged (tmp), int62 (tmp) - n); }
int62_t operator* (int n) const { int64_t tmp = v; return int62_t (flagged (tmp), int62 (tmp) * n); }
int62_t operator/ (int n) const { int64_t tmp = v; return int62_t (flagged (tmp), int62 (tmp) / n); }
int62_t operator% (int n) const { int64_t tmp = v; return int62_t (flagged (tmp), int62 (tmp) % n); }
int62_t operator+ (int62_t n) const { int64_t tmp = v; return int62_t (flagged (tmp), int62 (tmp) + n); }
int62_t operator- (int62_t n) const { int64_t tmp = v; return int62_t (flagged (tmp), int62 (tmp) - n); }
int62_t operator* (int62_t n) const { int64_t tmp = v; return int62_t (flagged (tmp), int62 (tmp) * n); }
int62_t operator/ (int62_t n) const { int64_t tmp = v; return int62_t (flagged (tmp), int62 (tmp) / n); }
int62_t operator% (int62_t n) const { int64_t tmp = v; return int62_t (flagged (tmp), int62 (tmp) % n); }
/* comparison operators .. will throw if the two objects have different
* flag settings (which is assumed to indicate that they differ in some
* important respect, and thus should not have their values compared)
@ -96,6 +103,8 @@ class alignas(16) int62_t {
operator int64_t() const { return int62(v); }
int62_t abs() const { int64_t tmp = v; return int62_t (flagged(tmp), ::abs(int62(tmp))); }
int62_t& operator+= (int64_t n) {
while (1) {
int64_t oldval (v);