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

Side by Side Diff: third_party/WebKit/Source/wtf/SaturatedArithmetic.h

Issue 2499783002: Move SaturatedArithmetic from Blink to base (Closed)
Patch Set: 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
OLDNEW
(Empty)
1 /*
2 * Copyright (c) 2012, Google Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
6 * met:
7 *
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above
11 * copyright notice, this list of conditions and the following disclaimer
12 * in the documentation and/or other materials provided with the
13 * distribution.
14 * * Neither the name of Google Inc. nor the names of its
15 * contributors may be used to endorse or promote products derived from
16 * this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */
30
31 #ifndef SaturatedArithmetic_h
32 #define SaturatedArithmetic_h
33
34 #include "wtf/CPU.h"
35 #include <limits>
36 #include <stdint.h>
37
38 #if CPU(ARM) && COMPILER(GCC) && __OPTIMIZE__
39
40 // If we're building ARM on GCC we replace the C++ versions with some
41 // native ARM assembly for speed.
42 #include "wtf/asm/SaturatedArithmeticARM.h"
43
44 #else
45
46 ALWAYS_INLINE int32_t saturatedAddition(int32_t a, int32_t b) {
47 uint32_t ua = a;
48 uint32_t ub = b;
49 uint32_t result = ua + ub;
50
51 // Can only overflow if the signed bit of the two values match. If the
52 // signed bit of the result and one of the values differ it overflowed.
53 // The branch compiles to a CMOVNS instruction on x86.
54 if (~(ua ^ ub) & (result ^ ua) & (1 << 31))
55 return std::numeric_limits<int>::max() + (ua >> 31);
56
57 return result;
58 }
59
60 ALWAYS_INLINE int32_t saturatedSubtraction(int32_t a, int32_t b) {
61 uint32_t ua = a;
62 uint32_t ub = b;
63 uint32_t result = ua - ub;
64
65 // Can only overflow if the signed bit of the two input values differ. If
66 // the signed bit of the result and the first value differ it overflowed.
67 // The branch compiles to a CMOVNS instruction on x86.
68 if ((ua ^ ub) & (result ^ ua) & (1 << 31))
69 return std::numeric_limits<int>::max() + (ua >> 31);
70
71 return result;
72 }
73
74 ALWAYS_INLINE int32_t saturatedNegative(int32_t a) {
75 if (UNLIKELY(a == std::numeric_limits<int>::min()))
76 return std::numeric_limits<int>::max();
77 return -a;
78 }
79
80 inline int getMaxSaturatedSetResultForTesting(int FractionalShift) {
81 // For C version the set function maxes out to max int, this differs from
82 // the ARM asm version, see SaturatedArithmetiARM.h for the equivalent asm
83 // version.
84 return std::numeric_limits<int>::max();
85 }
86
87 inline int getMinSaturatedSetResultForTesting(int FractionalShift) {
88 return std::numeric_limits<int>::min();
89 }
90
91 template <int FractionalShift>
92 ALWAYS_INLINE int saturatedSet(int value) {
93 const int intMaxForLayoutUnit =
94 std::numeric_limits<int>::max() >> FractionalShift;
95
96 const int intMinForLayoutUnit =
97 std::numeric_limits<int>::min() >> FractionalShift;
98
99 if (value > intMaxForLayoutUnit)
100 return std::numeric_limits<int>::max();
101
102 if (value < intMinForLayoutUnit)
103 return std::numeric_limits<int>::min();
104
105 return value << FractionalShift;
106 }
107
108 template <int FractionalShift>
109 ALWAYS_INLINE int saturatedSet(unsigned value) {
110 const unsigned intMaxForLayoutUnit =
111 std::numeric_limits<int>::max() >> FractionalShift;
112
113 if (value >= intMaxForLayoutUnit)
114 return std::numeric_limits<int>::max();
115
116 return value << FractionalShift;
117 }
118
119 #endif // CPU(ARM) && COMPILER(GCC)
120 #endif // SaturatedArithmetic_h
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698