CXXIter 0.2
Loading...
Searching...
No Matches
SaturatingArithmetic.h
1#pragma once
2
4namespace CXXIter::util {
5
6 //TODO: unit-tests
7
9 template<typename T>
10 struct SaturatingArithmetic {
11 T value;
12 SaturatingArithmetic(T value) : value(value) {}
13 T get() const { return value; }
14
15 SaturatingArithmetic<T> operator+(T o) {
16 T res = value + o;
17 res |= -(res < value);
18 return res;
19 }
20 SaturatingArithmetic<T> operator-(T o) {
21 T res = value - o;
22 res &= -(res <= value);
23 return res;
24 }
25 SaturatingArithmetic<T> operator/(T o) {
26 return value / o;
27 }
28 };
29
30}