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

Side by Side Diff: runtime/platform/utils.h

Issue 9112050: Simplify integer arithmetic code. (Closed) Base URL: http://dart.googlecode.com/svn/branches/bleeding_edge/dart/
Patch Set: '' Created 8 years, 11 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
« runtime/lib/integers.cc ('K') | « runtime/lib/integers.cc ('k') | no next file » | 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 Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file
2 // for details. All rights reserved. Use of this source code is governed by a 2 // for details. All rights reserved. Use of this source code is governed by a
3 // BSD-style license that can be found in the LICENSE file. 3 // BSD-style license that can be found in the LICENSE file.
4 4
5 #ifndef PLATFORM_UTILS_H_ 5 #ifndef PLATFORM_UTILS_H_
6 #define PLATFORM_UTILS_H_ 6 #define PLATFORM_UTILS_H_
7 7
8 #include "platform/assert.h" 8 #include "platform/assert.h"
9 #include "platform/globals.h" 9 #include "platform/globals.h"
10 10
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after
79 static uint32_t RoundUpToPowerOfTwo(uint32_t x); 79 static uint32_t RoundUpToPowerOfTwo(uint32_t x);
80 static int CountOneBits(uint32_t x); 80 static int CountOneBits(uint32_t x);
81 81
82 // Computes a hash value for the given string. 82 // Computes a hash value for the given string.
83 static uint32_t StringHash(const char* data, int length); 83 static uint32_t StringHash(const char* data, int length);
84 84
85 // Computes a hash value for the given word. 85 // Computes a hash value for the given word.
86 static uint32_t WordHash(word key); 86 static uint32_t WordHash(word key);
87 87
88 // Check whether an N-bit two's-complement representation can hold value. 88 // Check whether an N-bit two's-complement representation can hold value.
89 static inline bool IsInt(int N, word value) { 89 template<typename T>
90 ASSERT((0 < N) && (N < kBitsPerWord)); 90 static inline bool IsInt(int N, T value) {
91 ASSERT((0 < N) && (N < (kBitsPerByte * sizeof(T))));
Ivan Posva 2012/01/18 20:05:22 sizeof(value) is closer to the style guide: http:/
regis 2012/01/18 21:50:12 Done.
91 word limit = static_cast<word>(1) << (N - 1); 92 word limit = static_cast<word>(1) << (N - 1);
Ivan Posva 2012/01/18 20:05:22 You need to cast the 1 to T instead of word here.
regis 2012/01/18 21:50:12 The cast is fixed in patch 5. I have added a test.
92 return (-limit <= value) && (value < limit); 93 return (-limit <= value) && (value < limit);
93 } 94 }
94 95
95 static inline bool IsUint(int N, word value) { 96 template<typename T>
Ivan Posva 2012/01/18 20:05:22 ditto
regis 2012/01/18 21:50:12 Done.
96 ASSERT((0 < N) && (N < kBitsPerWord)); 97 static inline bool IsUint(int N, T value) {
98 ASSERT((0 < N) && (N < (kBitsPerByte * sizeof(T))));
97 word limit = static_cast<word>(1) << N; 99 word limit = static_cast<word>(1) << N;
98 return (0 <= value) && (value < limit); 100 return (0 <= value) && (value < limit);
99 } 101 }
100 102
101 // Check whether the magnitude of value fits in N bits, i.e., whether an 103 // Check whether the magnitude of value fits in N bits, i.e., whether an
102 // (N+1)-bit sign-magnitude representation can hold value. 104 // (N+1)-bit sign-magnitude representation can hold value.
103 static inline bool IsAbsoluteUint(int N, word value) { 105 template<typename T>
Ivan Posva 2012/01/18 20:05:22 ditto
regis 2012/01/18 21:50:12 Done.
104 ASSERT((0 < N) && (N < kBitsPerWord)); 106 static inline bool IsAbsoluteUint(int N, T value) {
107 ASSERT((0 < N) && (N < (kBitsPerByte * sizeof(T))));
105 if (value < 0) value = -value; 108 if (value < 0) value = -value;
106 return IsUint(N, value); 109 return IsUint(N, value);
107 } 110 }
108 111
109 static inline int32_t Low16Bits(int32_t value) { 112 static inline int32_t Low16Bits(int32_t value) {
110 return static_cast<int32_t>(value & 0xffff); 113 return static_cast<int32_t>(value & 0xffff);
111 } 114 }
112 115
113 static inline int32_t High16Bits(int32_t value) { 116 static inline int32_t High16Bits(int32_t value) {
114 return static_cast<int32_t>(value >> 16); 117 return static_cast<int32_t>(value >> 16);
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
146 static char IntToHexDigit(int i) { 149 static char IntToHexDigit(int i) {
147 ASSERT(0 <= i && i < 16); 150 ASSERT(0 <= i && i < 16);
148 if (i < 10) return static_cast<char>('0' + i); 151 if (i < 10) return static_cast<char>('0' + i);
149 return static_cast<char>('A' + (i - 10)); 152 return static_cast<char>('A' + (i - 10));
150 } 153 }
151 }; 154 };
152 155
153 } // namespace dart 156 } // namespace dart
154 157
155 #endif // PLATFORM_UTILS_H_ 158 #endif // PLATFORM_UTILS_H_
OLDNEW
« runtime/lib/integers.cc ('K') | « runtime/lib/integers.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698