| 1 |
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
| 2 |
/* vim: set ts=8 sts=2 et sw=2 tw=80: */ |
| 3 |
/* This Source Code Form is subject to the terms of the Mozilla Public |
| 4 |
* License, v. 2.0. If a copy of the MPL was not distributed with this |
| 5 |
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
| 6 |
|
| 7 |
/* Provides saturation arithmetics for scalar types. */ |
| 8 |
|
| 9 |
#ifndef mozilla_Saturate_h |
| 10 |
#define mozilla_Saturate_h |
| 11 |
|
| 12 |
#include <limits> |
| 13 |
#include <stdint.h> |
| 14 |
#include <type_traits> |
| 15 |
#include <utility> |
| 16 |
|
| 17 |
#include "mozilla/Attributes.h" |
| 18 |
|
| 19 |
namespace mozilla { |
| 20 |
namespace detail { |
| 21 |
|
| 22 |
/** |
| 23 |
* |SaturateOp<T>| wraps scalar values for saturation arithmetics. Usage: |
| 24 |
* |
| 25 |
* uint32_t value = 1; |
| 26 |
* |
| 27 |
* ++SaturateOp<uint32_t>(value); // value is 2 |
| 28 |
* --SaturateOp<uint32_t>(value); // value is 1 |
| 29 |
* --SaturateOp<uint32_t>(value); // value is 0 |
| 30 |
* --SaturateOp<uint32_t>(value); // value is still 0 |
| 31 |
* |
| 32 |
* Please add new operators when required. |
| 33 |
* |
| 34 |
* |SaturateOp<T>| will saturate at the minimum and maximum values of |
| 35 |
* type T. If you need other bounds, implement a clamped-type class and |
| 36 |
* specialize the type traits accordingly. |
| 37 |
*/ |
| 38 |
template <typename T> |
| 39 |
class SaturateOp { |
| 40 |
public: |
| 41 |
explicit SaturateOp(T& aValue) : mValue(aValue) { |
| 42 |
// We should actually check for |std::is_scalar<T>::value| to be |
| 43 |
// true, but this type trait is not available everywhere. Relax |
| 44 |
// this assertion if you want to use floating point values as well. |
| 45 |
static_assert(std::is_integral_v<T>, |
| 46 |
"Integral type required in instantiation"); |
| 47 |
} |
| 48 |
|
| 49 |
// Add and subtract operators |
| 50 |
|
| 51 |
T operator+(const T& aRhs) const { return T(mValue) += aRhs; } |
| 52 |
|
| 53 |
T operator-(const T& aRhs) const { return T(mValue) -= aRhs; } |
| 54 |
|
| 55 |
// Compound operators |
| 56 |
|
| 57 |
const T& operator+=(const T& aRhs) const { |
| 58 |
const T min = std::numeric_limits<T>::min(); |
| 59 |
const T max = std::numeric_limits<T>::max(); |
| 60 |
|
| 61 |
if (aRhs > static_cast<T>(0)) { |
| 62 |
mValue = (max - aRhs) < mValue ? max : mValue + aRhs; |
| 63 |
} else { |
| 64 |
mValue = (min - aRhs) > mValue ? min : mValue + aRhs; |
| 65 |
} |
| 66 |
return mValue; |
| 67 |
} |
| 68 |
|
| 69 |
const T& operator-=(const T& aRhs) const { |
| 70 |
const T min = std::numeric_limits<T>::min(); |
| 71 |
const T max = std::numeric_limits<T>::max(); |
| 72 |
|
| 73 |
if (aRhs > static_cast<T>(0)) { |
| 74 |
mValue = (min + aRhs) > mValue ? min : mValue - aRhs; |
| 75 |
} else { |
| 76 |
mValue = (max + aRhs) < mValue ? max : mValue - aRhs; |
| 77 |
} |
| 78 |
return mValue; |
| 79 |
} |
| 80 |
|
| 81 |
// Increment and decrement operators |
| 82 |
|
| 83 |
const T& operator++() const // prefix |
| 84 |
{ |
| 85 |
return operator+=(static_cast<T>(1)); |
| 86 |
} |
| 87 |
|
| 88 |
T operator++(int) const // postfix |
| 89 |
{ |
| 90 |
const T value(mValue); |
| 91 |
operator++(); |
| 92 |
return value; |
| 93 |
} |
| 94 |
|
| 95 |
const T& operator--() const // prefix |
| 96 |
{ |
| 97 |
return operator-=(static_cast<T>(1)); |
| 98 |
} |
| 99 |
|
| 100 |
T operator--(int) const // postfix |
| 101 |
{ |
| 102 |
const T value(mValue); |
| 103 |
operator--(); |
| 104 |
return value; |
| 105 |
} |
| 106 |
|
| 107 |
private: |
| 108 |
SaturateOp(const SaturateOp<T>&) = delete; |
| 109 |
SaturateOp(SaturateOp<T>&&) = delete; |
| 110 |
SaturateOp& operator=(const SaturateOp<T>&) = delete; |
| 111 |
SaturateOp& operator=(SaturateOp<T>&&) = delete; |
| 112 |
|
| 113 |
T& mValue; |
| 114 |
}; |
| 115 |
|
| 116 |
/** |
| 117 |
* |Saturate<T>| is a value type for saturation arithmetics. It's |
| 118 |
* built on top of |SaturateOp<T>|. |
| 119 |
*/ |
| 120 |
template <typename T> |
| 121 |
class Saturate { |
| 122 |
public: |
| 123 |
Saturate() = default; |
| 124 |
MOZ_IMPLICIT Saturate(const Saturate<T>&) = default; |
| 125 |
|
| 126 |
MOZ_IMPLICIT Saturate(Saturate<T>&& aValue) { |
| 127 |
mValue = std::move(aValue.mValue); |
| 128 |
} |
| 129 |
|
| 130 |
explicit Saturate(const T& aValue) : mValue(aValue) {} |
| 131 |
|
| 132 |
const T& value() const { return mValue; } |
| 133 |
|
| 134 |
// Compare operators |
| 135 |
|
| 136 |
bool operator==(const Saturate<T>& aRhs) const { |
| 137 |
return mValue == aRhs.mValue; |
| 138 |
} |
| 139 |
|
| 140 |
bool operator!=(const Saturate<T>& aRhs) const { return !operator==(aRhs); } |
| 141 |
|
| 142 |
bool operator==(const T& aRhs) const { return mValue == aRhs; } |
| 143 |
|
| 144 |
bool operator!=(const T& aRhs) const { return !operator==(aRhs); } |
| 145 |
|
| 146 |
// Assignment operators |
| 147 |
|
| 148 |
Saturate<T>& operator=(const Saturate<T>&) = default; |
| 149 |
|
| 150 |
Saturate<T>& operator=(Saturate<T>&& aRhs) { |
| 151 |
mValue = std::move(aRhs.mValue); |
| 152 |
return *this; |
| 153 |
} |
| 154 |
|
| 155 |
// Add and subtract operators |
| 156 |
|
| 157 |
Saturate<T> operator+(const Saturate<T>& aRhs) const { |
| 158 |
Saturate<T> lhs(mValue); |
| 159 |
return lhs += aRhs.mValue; |
| 160 |
} |
| 161 |
|
| 162 |
Saturate<T> operator+(const T& aRhs) const { |
| 163 |
Saturate<T> lhs(mValue); |
| 164 |
return lhs += aRhs; |
| 165 |
} |
| 166 |
|
| 167 |
Saturate<T> operator-(const Saturate<T>& aRhs) const { |
| 168 |
Saturate<T> lhs(mValue); |
| 169 |
return lhs -= aRhs.mValue; |
| 170 |
} |
| 171 |
|
| 172 |
Saturate<T> operator-(const T& aRhs) const { |
| 173 |
Saturate<T> lhs(mValue); |
| 174 |
return lhs -= aRhs; |
| 175 |
} |
| 176 |
|
| 177 |
// Compound operators |
| 178 |
|
| 179 |
Saturate<T>& operator+=(const Saturate<T>& aRhs) { |
| 180 |
SaturateOp<T>(mValue) += aRhs.mValue; |
| 181 |
return *this; |
| 182 |
} |
| 183 |
|
| 184 |
Saturate<T>& operator+=(const T& aRhs) { |
| 185 |
SaturateOp<T>(mValue) += aRhs; |
| 186 |
return *this; |
| 187 |
} |
| 188 |
|
| 189 |
Saturate<T>& operator-=(const Saturate<T>& aRhs) { |
| 190 |
SaturateOp<T>(mValue) -= aRhs.mValue; |
| 191 |
return *this; |
| 192 |
} |
| 193 |
|
| 194 |
Saturate<T>& operator-=(const T& aRhs) { |
| 195 |
SaturateOp<T>(mValue) -= aRhs; |
| 196 |
return *this; |
| 197 |
} |
| 198 |
|
| 199 |
// Increment and decrement operators |
| 200 |
|
| 201 |
Saturate<T>& operator++() // prefix |
| 202 |
{ |
| 203 |
++SaturateOp<T>(mValue); |
| 204 |
return *this; |
| 205 |
} |
| 206 |
|
| 207 |
Saturate<T> operator++(int) // postfix |
| 208 |
{ |
| 209 |
return Saturate<T>(SaturateOp<T>(mValue)++); |
| 210 |
} |
| 211 |
|
| 212 |
Saturate<T>& operator--() // prefix |
| 213 |
{ |
| 214 |
--SaturateOp<T>(mValue); |
| 215 |
return *this; |
| 216 |
} |
| 217 |
|
| 218 |
Saturate<T> operator--(int) // postfix |
| 219 |
{ |
| 220 |
return Saturate<T>(SaturateOp<T>(mValue)--); |
| 221 |
} |
| 222 |
|
| 223 |
private: |
| 224 |
T mValue; |
| 225 |
}; |
| 226 |
|
| 227 |
} // namespace detail |
| 228 |
|
| 229 |
typedef detail::Saturate<int8_t> SaturateInt8; |
| 230 |
typedef detail::Saturate<int16_t> SaturateInt16; |
| 231 |
typedef detail::Saturate<int32_t> SaturateInt32; |
| 232 |
typedef detail::Saturate<uint8_t> SaturateUint8; |
| 233 |
typedef detail::Saturate<uint16_t> SaturateUint16; |
| 234 |
typedef detail::Saturate<uint32_t> SaturateUint32; |
| 235 |
|
| 236 |
} // namespace mozilla |
| 237 |
|
| 238 |
template <typename LhsT, typename RhsT> |
| 239 |
bool operator==(LhsT aLhs, const mozilla::detail::Saturate<RhsT>& aRhs) { |
| 240 |
return aRhs.operator==(static_cast<RhsT>(aLhs)); |
| 241 |
} |
| 242 |
|
| 243 |
template <typename LhsT, typename RhsT> |
| 244 |
bool operator!=(LhsT aLhs, const mozilla::detail::Saturate<RhsT>& aRhs) { |
| 245 |
return !(aLhs == aRhs); |
| 246 |
} |
| 247 |
|
| 248 |
#endif // mozilla_Saturate_h |