Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(460)

Side by Side Diff: base/numerics/saturated_arithmetic.h

Issue 2499783002: Move SaturatedArithmetic from Blink to base (Closed)
Patch Set: Revert flag addition Created 4 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « base/BUILD.gn ('k') | base/numerics/saturated_arithmetic_arm.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright 2016 The Chromium 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 #ifndef BASE_NUMERICS_SATURATED_ARITHMETIC_H_
6 #define BASE_NUMERICS_SATURATED_ARITHMETIC_H_
7
8 #include <stdint.h>
9
10 #include <limits>
11
12 #include "base/compiler_specific.h"
13
14 #if defined(ARCH_CPU_ARM_FAMILY) && defined(ARCH_CPU_32_BITS) && \
15 defined(COMPILER_GCC) && !defined(OS_NACL) && __OPTIMIZE__
16
17 // If we're building ARM 32-bit on GCC we replace the C++ versions with some
18 // native ARM assembly for speed.
19 #include "base/numerics/saturated_arithmetic_arm.h"
20
21 #else
22
23 namespace base {
24
25 inline int32_t SaturatedAddition(int32_t a, int32_t b) {
26 uint32_t ua = a;
27 uint32_t ub = b;
28 uint32_t result = ua + ub;
29
30 // Can only overflow if the signed bit of the two values match. If the
31 // signed bit of the result and one of the values differ it overflowed.
32 // The branch compiles to a CMOVNS instruction on x86.
33 if (~(ua ^ ub) & (result ^ ua) & (1 << 31))
34 return std::numeric_limits<int>::max() + (ua >> 31);
35
36 return result;
37 }
38
39 inline int32_t SaturatedSubtraction(int32_t a, int32_t b) {
40 uint32_t ua = a;
41 uint32_t ub = b;
42 uint32_t result = ua - ub;
43
44 // Can only overflow if the signed bit of the two input values differ. If
45 // the signed bit of the result and the first value differ it overflowed.
46 // The branch compiles to a CMOVNS instruction on x86.
47 if ((ua ^ ub) & (result ^ ua) & (1 << 31))
48 return std::numeric_limits<int>::max() + (ua >> 31);
49
50 return result;
51 }
52
53 inline int32_t SaturatedNegative(int32_t a) {
54 if (UNLIKELY(a == std::numeric_limits<int>::min()))
55 return std::numeric_limits<int>::max();
56 return -a;
57 }
58
59 inline int GetMaxSaturatedSetResultForTesting(int fractional_shift) {
60 // For C version the set function maxes out to max int, this differs from
61 // the ARM asm version, see saturated_arithmetic_arm.h for the equivalent asm
62 // version.
63 return std::numeric_limits<int>::max();
64 }
65
66 inline int GetMinSaturatedSetResultForTesting(int fractional_shift) {
67 return std::numeric_limits<int>::min();
68 }
69
70 template <int fractional_shift>
71 inline int SaturatedSet(int value) {
72 const int kIntMaxForLayoutUnit =
73 std::numeric_limits<int>::max() >> fractional_shift;
74
75 const int kIntMinForLayoutUnit =
76 std::numeric_limits<int>::min() >> fractional_shift;
77
78 if (value > kIntMaxForLayoutUnit)
79 return std::numeric_limits<int>::max();
80
81 if (value < kIntMinForLayoutUnit)
82 return std::numeric_limits<int>::min();
83
84 return value << fractional_shift;
85 }
86
87 template <int fractional_shift>
88 inline int SaturatedSet(unsigned value) {
89 const unsigned kIntMaxForLayoutUnit =
90 std::numeric_limits<int>::max() >> fractional_shift;
91
92 if (value >= kIntMaxForLayoutUnit)
93 return std::numeric_limits<int>::max();
94
95 return value << fractional_shift;
96 }
97
98 } // namespace base
99
100 #endif // CPU(ARM) && COMPILER(GCC)
101 #endif // BASE_NUMERICS_SATURATED_ARITHMETIC_H_
OLDNEW
« no previous file with comments | « base/BUILD.gn ('k') | base/numerics/saturated_arithmetic_arm.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698