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

Side by Side Diff: base/numerics/saturated_arithmetic_arm.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/numerics/saturated_arithmetic.h ('k') | base/numerics/saturated_arithmetic_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #ifndef SaturatedArithmeticARM_h 5 #ifndef BASE_NUMERICS_SATURATED_ARITHMETIC_ARM_H_
6 #define SaturatedArithmeticARM_h 6 #define BASE_NUMERICS_SATURATED_ARITHMETIC_ARM_H_
7 7
8 #include "wtf/CPU.h"
9 #include <limits> 8 #include <limits>
10 #include <stdint.h>
11 9
12 ALWAYS_INLINE int32_t saturatedAddition(int32_t a, int32_t b) { 10 namespace base {
11
12 inline int32_t SaturatedAddition(int32_t a, int32_t b) {
13 int32_t result; 13 int32_t result;
14 14
15 asm("qadd %[output],%[first],%[second]" 15 asm("qadd %[output],%[first],%[second]"
16 : [output] "=r"(result) 16 : [output] "=r"(result)
17 : [first] "r"(a), [second] "r"(b)); 17 : [first] "r"(a), [second] "r"(b));
18 18
19 return result; 19 return result;
20 } 20 }
21 21
22 ALWAYS_INLINE int32_t saturatedSubtraction(int32_t a, int32_t b) { 22 inline int32_t SaturatedSubtraction(int32_t a, int32_t b) {
23 int32_t result; 23 int32_t result;
24 24
25 asm("qsub %[output],%[first],%[second]" 25 asm("qsub %[output],%[first],%[second]"
26 : [output] "=r"(result) 26 : [output] "=r"(result)
27 : [first] "r"(a), [second] "r"(b)); 27 : [first] "r"(a), [second] "r"(b));
28 28
29 return result; 29 return result;
30 } 30 }
31 31
32 ALWAYS_INLINE int32_t saturatedNegative(int32_t a) { 32 inline int32_t SaturatedNegative(int32_t a) {
33 return saturatedSubtraction(0, a); 33 return SaturatedSubtraction(0, a);
34 } 34 }
35 35
36 inline int getMaxSaturatedSetResultForTesting(int FractionalShift) { 36 inline int GetMaxSaturatedSetResultForTesting(int fractional_shift) {
37 // For ARM Asm version the set function maxes out to the biggest 37 // For ARM Asm version the set function maxes out to the biggest
38 // possible integer part with the fractional part zero'd out. 38 // possible integer part with the fractional part zero'd out.
39 // e.g. 0x7fffffc0. 39 // e.g. 0x7fffffc0.
40 return std::numeric_limits<int>::max() & ~((1 << FractionalShift) - 1); 40 return std::numeric_limits<int>::max() & ~((1 << fractional_shift) - 1);
41 } 41 }
42 42
43 inline int getMinSaturatedSetResultForTesting(int FractionalShift) { 43 inline int GetMinSaturatedSetResultForTesting(int fractional_shift) {
44 return std::numeric_limits<int>::min(); 44 return std::numeric_limits<int>::min();
45 } 45 }
46 46
47 template <int FractionalShift> 47 template <int fractional_shift>
48 ALWAYS_INLINE int saturatedSet(int value) { 48 inline int SaturatedSet(int value) {
49 // Figure out how many bits are left for storing the integer part of 49 // Figure out how many bits are left for storing the integer part of
50 // the fixed point number, and saturate our input to that 50 // the fixed point number, and saturate our input to that
51 enum { Saturate = 32 - FractionalShift }; 51 enum { Saturate = 32 - fractional_shift };
52 52
53 int result; 53 int result;
54 54
55 // The following ARM code will Saturate the passed value to the number of 55 // The following ARM code will Saturate the passed value to the number of
56 // bits used for the whole part of the fixed point representation, then 56 // bits used for the whole part of the fixed point representation, then
57 // shift it up into place. This will result in the low <FractionShift> bits 57 // shift it up into place. This will result in the low <FractionShift> bits
58 // all being 0's. When the value saturates this gives a different result 58 // all being 0's. When the value saturates this gives a different result
59 // to from the C++ case; in the C++ code a saturated value has all the low 59 // to from the C++ case; in the C++ code a saturated value has all the low
60 // bits set to 1 (for a +ve number at least). This cannot be done rapidly 60 // bits set to 1 (for a +ve number at least). This cannot be done rapidly
61 // in ARM ... we live with the difference, for the sake of speed. 61 // in ARM ... we live with the difference, for the sake of speed.
62 62
63 asm("ssat %[output],%[saturate],%[value]\n\t" 63 asm("ssat %[output],%[saturate],%[value]\n\t"
64 "lsl %[output],%[shift]" 64 "lsl %[output],%[shift]"
65 : [output] "=r"(result) 65 : [output] "=r"(result)
66 : [value] "r"(value), [saturate] "n"(Saturate), 66 : [value] "r"(value), [saturate] "n"(Saturate),
67 [shift] "n"(FractionalShift)); 67 [shift] "n"(fractional_shift));
68 68
69 return result; 69 return result;
70 } 70 }
71 71
72 template <int FractionalShift> 72 template <int fractional_shift>
73 ALWAYS_INLINE int saturatedSet(unsigned value) { 73 inline int SaturatedSet(unsigned value) {
74 // Here we are being passed an unsigned value to saturate, 74 // Here we are being passed an unsigned value to saturate,
75 // even though the result is returned as a signed integer. The ARM 75 // even though the result is returned as a signed integer. The ARM
76 // instruction for unsigned saturation therefore needs to be given one 76 // instruction for unsigned saturation therefore needs to be given one
77 // less bit (i.e. the sign bit) for the saturation to work correctly; hence 77 // less bit (i.e. the sign bit) for the saturation to work correctly; hence
78 // the '31' below. 78 // the '31' below.
79 enum { Saturate = 31 - FractionalShift }; 79 enum { Saturate = 31 - fractional_shift };
80 80
81 // The following ARM code will Saturate the passed value to the number of 81 // The following ARM code will Saturate the passed value to the number of
82 // bits used for the whole part of the fixed point representation, then 82 // bits used for the whole part of the fixed point representation, then
83 // shift it up into place. This will result in the low <FractionShift> bits 83 // shift it up into place. This will result in the low <FractionShift> bits
84 // all being 0's. When the value saturates this gives a different result 84 // all being 0's. When the value saturates this gives a different result
85 // to from the C++ case; in the C++ code a saturated value has all the low 85 // to from the C++ case; in the C++ code a saturated value has all the low
86 // bits set to 1. This cannot be done rapidly in ARM, so we live with the 86 // bits set to 1. This cannot be done rapidly in ARM, so we live with the
87 // difference, for the sake of speed. 87 // difference, for the sake of speed.
88 88
89 int result; 89 int result;
90 90
91 asm("usat %[output],%[saturate],%[value]\n\t" 91 asm("usat %[output],%[saturate],%[value]\n\t"
92 "lsl %[output],%[shift]" 92 "lsl %[output],%[shift]"
93 : [output] "=r"(result) 93 : [output] "=r"(result)
94 : [value] "r"(value), [saturate] "n"(Saturate), 94 : [value] "r"(value), [saturate] "n"(Saturate),
95 [shift] "n"(FractionalShift)); 95 [shift] "n"(fractional_shift));
96 96
97 return result; 97 return result;
98 } 98 }
99 99
100 #endif // SaturatedArithmeticARM_h 100 } // namespace base
101
102 #endif // BASE_NUMERICS_SATURATED_ARITHMETIC_ARM_H_
OLDNEW
« no previous file with comments | « base/numerics/saturated_arithmetic.h ('k') | base/numerics/saturated_arithmetic_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698