Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 14 additions & 3 deletions lib/base/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include <cstddef>
#include <cstdint>
#include <cstdio>
#include <limits>
#include <memory>
#include <string>
#include <type_traits>
Expand All @@ -33,13 +34,23 @@ constexpr inline size_t RoundUpToBlockDim(size_t dim) {
return (dim + 7) & ~static_cast<size_t>(7);
}

static inline bool JPEGLI_MAYBE_UNUSED SafeAdd(const uint64_t a,
const uint64_t b,
uint64_t& sum) {
template <typename U,
class = typename std::enable_if<std::is_unsigned<U>::value>::type>
static inline bool SafeAdd(const U a, const U b, U& sum) {
sum = a + b;
return sum >= a; // no need to check b - either sum >= both or < both.
}

template <typename U,
class = typename std::enable_if<std::is_unsigned<U>::value>::type>
static inline bool SafeMul(const U a, const U b, U& product) {
product = 0;
if (a == 0 || b == 0) return true;
if (b > (std::numeric_limits<U>::max() / a)) return false;
product = a * b;
return true;
}

template <typename T1, typename T2>
constexpr inline T1 DivCeil(T1 a, T2 b) {
return (a + b - 1) / b;
Expand Down
Loading