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

Side by Side Diff: gpu/command_buffer/common/gles2_cmd_utils.h

Issue 10916165: Fix SafeAdd and SafeMultiply (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 8 years, 3 months 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 | Annotate | Revision Log
« no previous file with comments | « no previous file | gpu/command_buffer/common/gles2_cmd_utils_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 (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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 // This file is here so other GLES2 related files can have a common set of 5 // This file is here so other GLES2 related files can have a common set of
6 // includes where appropriate. 6 // includes where appropriate.
7 7
8 #ifndef GPU_COMMAND_BUFFER_COMMON_GLES2_CMD_UTILS_H_ 8 #ifndef GPU_COMMAND_BUFFER_COMMON_GLES2_CMD_UTILS_H_
9 #define GPU_COMMAND_BUFFER_COMMON_GLES2_CMD_UTILS_H_ 9 #define GPU_COMMAND_BUFFER_COMMON_GLES2_CMD_UTILS_H_
10 10
11 #include <limits>
11 #include <string> 12 #include <string>
12 #include <vector> 13 #include <vector>
13 14
14 #include "../common/types.h" 15 #include "../common/types.h"
15 #include "gpu/command_buffer/common/gles2_utils_export.h" 16 #include "gpu/command_buffer/common/gles2_utils_export.h"
16 17
17 namespace gpu { 18 namespace gpu {
18 namespace gles2 { 19 namespace gles2 {
19 20
20 // Does a multiply and checks for overflow. If the multiply did not overflow 21 // Does a multiply and checks for overflow. If the multiply did not overflow
21 // returns true. 22 // returns true.
22 template <typename T> 23
23 inline bool SafeMultiply(T a, T b, T* dst) { 24 // Multiplies 2 32 bit unsigned numbers checking for overflow.
25 // If there was no overflow returns true.
26 inline bool SafeMultiplyUint32(uint32 a, uint32 b, uint32* dst) {
24 if (b == 0) { 27 if (b == 0) {
25 *dst = 0; 28 *dst = 0;
26 return true; 29 return true;
27 } 30 }
28 T v = a * b; 31 uint32 v = a * b;
29 if (v / b != a) { 32 if (v / b != a) {
30 *dst = 0; 33 *dst = 0;
31 return false; 34 return false;
32 } 35 }
33 *dst = v; 36 *dst = v;
34 return true; 37 return true;
35 } 38 }
36 39
37 // A wrapper for SafeMultiply to remove the need to cast.
38 inline bool SafeMultiplyUint32(uint32 a, uint32 b, uint32* dst) {
39 return SafeMultiply(a, b, dst);
40 }
41
42 // Does an add checking for overflow. If there was no overflow returns true. 40 // Does an add checking for overflow. If there was no overflow returns true.
43 template <typename T> 41 inline bool SafeAddUint32(uint32 a, uint32 b, uint32* dst) {
44 inline bool SafeAdd(T a, T b, T* dst) {
45 if (a + b < a) { 42 if (a + b < a) {
46 *dst = 0; 43 *dst = 0;
47 return false; 44 return false;
48 } 45 }
49 *dst = a + b; 46 *dst = a + b;
50 return true; 47 return true;
51 } 48 }
52 49
53 // A wrapper for SafeAdd to remove the need to cast. 50 // Does an add checking for overflow. If there was no overflow returns true.
54 inline bool SafeAddUint32(uint32 a, uint32 b, uint32* dst) { 51 inline bool SafeAddInt32(int32 a, int32 b, int32* dst) {
55 return SafeAdd(a, b, dst); 52 int64 sum64 = static_cast<int64>(a) + b;
53 int32 sum32 = static_cast<int32>(sum64);
54 bool safe = sum64 == static_cast<int64>(sum32);
55 *dst = safe ? sum32 : 0;
56 return safe;
56 } 57 }
57 58
58 // Utilties for GLES2 support. 59 // Utilties for GLES2 support.
59 class GLES2_UTILS_EXPORT GLES2Util { 60 class GLES2_UTILS_EXPORT GLES2Util {
60 public: 61 public:
61 static const int kNumFaces = 6; 62 static const int kNumFaces = 6;
62 63
63 // Bits returned by GetChannelsForFormat 64 // Bits returned by GetChannelsForFormat
64 enum ChannelBits { 65 enum ChannelBits {
65 kRed = 0x1, 66 kRed = 0x1,
(...skipping 123 matching lines...) Expand 10 before | Expand all | Expand 10 after
189 bool buffer_preserved_; 190 bool buffer_preserved_;
190 bool share_resources_; 191 bool share_resources_;
191 bool bind_generates_resource_; 192 bool bind_generates_resource_;
192 }; 193 };
193 194
194 } // namespace gles2 195 } // namespace gles2
195 } // namespace gpu 196 } // namespace gpu
196 197
197 #endif // GPU_COMMAND_BUFFER_COMMON_GLES2_CMD_UTILS_H_ 198 #endif // GPU_COMMAND_BUFFER_COMMON_GLES2_CMD_UTILS_H_
198 199
OLDNEW
« no previous file with comments | « no previous file | gpu/command_buffer/common/gles2_cmd_utils_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698