Flutter Engine Uber Docs
Docs for the entire Flutter Engine repo.
 
Loading...
Searching...
No Matches
safe_math.cc
Go to the documentation of this file.
1// Copyright 2013 The Flutter Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include <limits>
6
8#include "third_party/abseil-cpp/absl/numeric/int128.h"
9
10namespace fml {
11
12size_t SafeMath::mul(size_t x, size_t y) {
13 return sizeof(size_t) == sizeof(uint64_t) ? mul64(x, y) : mul32(x, y);
14}
15
16uint32_t SafeMath::mul32(uint32_t x, uint32_t y) {
17 uint64_t big_x = x;
18 uint64_t big_y = y;
19 uint64_t result = big_x * big_y;
20 if (result >> 32) {
21 overflow_detected_ = true;
22 }
23 return static_cast<uint32_t>(result);
24}
25
26uint64_t SafeMath::mul64(uint64_t x, uint64_t y) {
27 if (x <= std::numeric_limits<uint32_t>::max() &&
28 y <= std::numeric_limits<uint32_t>::max()) {
29 return x * y;
30 }
31
32 absl::uint128 big_x = x;
33 absl::uint128 big_y = y;
34 absl::uint128 result = big_x * big_y;
35 if (absl::Uint128High64(result)) {
36 overflow_detected_ = true;
37 }
38 return absl::Uint128Low64(result);
39}
40
41} // namespace fml
size_t mul(size_t x, size_t y)
Definition safe_math.cc:12
int32_t x
double y