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

Side by Side Diff: third_party/WebKit/Source/wtf/asm/SaturatedArithmeticARM.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 | « third_party/WebKit/Source/wtf/SaturatedArithmeticTest.cpp ('k') | tools/ubsan/blacklist.txt » ('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 2014 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 SaturatedArithmeticARM_h
6 #define SaturatedArithmeticARM_h
7
8 #include "wtf/CPU.h"
9 #include <limits>
10 #include <stdint.h>
11
12 ALWAYS_INLINE int32_t saturatedAddition(int32_t a, int32_t b) {
13 int32_t result;
14
15 asm("qadd %[output],%[first],%[second]"
16 : [output] "=r"(result)
17 : [first] "r"(a), [second] "r"(b));
18
19 return result;
20 }
21
22 ALWAYS_INLINE int32_t saturatedSubtraction(int32_t a, int32_t b) {
23 int32_t result;
24
25 asm("qsub %[output],%[first],%[second]"
26 : [output] "=r"(result)
27 : [first] "r"(a), [second] "r"(b));
28
29 return result;
30 }
31
32 ALWAYS_INLINE int32_t saturatedNegative(int32_t a) {
33 return saturatedSubtraction(0, a);
34 }
35
36 inline int getMaxSaturatedSetResultForTesting(int FractionalShift) {
37 // For ARM Asm version the set function maxes out to the biggest
38 // possible integer part with the fractional part zero'd out.
39 // e.g. 0x7fffffc0.
40 return std::numeric_limits<int>::max() & ~((1 << FractionalShift) - 1);
41 }
42
43 inline int getMinSaturatedSetResultForTesting(int FractionalShift) {
44 return std::numeric_limits<int>::min();
45 }
46
47 template <int FractionalShift>
48 ALWAYS_INLINE int saturatedSet(int value) {
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
51 enum { Saturate = 32 - FractionalShift };
52
53 int result;
54
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
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
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
61 // in ARM ... we live with the difference, for the sake of speed.
62
63 asm("ssat %[output],%[saturate],%[value]\n\t"
64 "lsl %[output],%[shift]"
65 : [output] "=r"(result)
66 : [value] "r"(value), [saturate] "n"(Saturate),
67 [shift] "n"(FractionalShift));
68
69 return result;
70 }
71
72 template <int FractionalShift>
73 ALWAYS_INLINE int saturatedSet(unsigned value) {
74 // Here we are being passed an unsigned value to saturate,
75 // even though the result is returned as a signed integer. The ARM
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
78 // the '31' below.
79 enum { Saturate = 31 - FractionalShift };
80
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
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
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
87 // difference, for the sake of speed.
88
89 int result;
90
91 asm("usat %[output],%[saturate],%[value]\n\t"
92 "lsl %[output],%[shift]"
93 : [output] "=r"(result)
94 : [value] "r"(value), [saturate] "n"(Saturate),
95 [shift] "n"(FractionalShift));
96
97 return result;
98 }
99
100 #endif // SaturatedArithmeticARM_h
OLDNEW
« no previous file with comments | « third_party/WebKit/Source/wtf/SaturatedArithmeticTest.cpp ('k') | tools/ubsan/blacklist.txt » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698