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

Side by Side Diff: src/conversions-inl.h

Issue 2577143002: [runtime] Add PositiveNumberToUint32 helper to avoid double to uint roundtrip (Closed)
Patch Set: avoid overflows Created 4 years 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 | « src/conversions.h ('k') | src/objects.h » ('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 2011 the V8 project authors. All rights reserved. 1 // Copyright 2011 the V8 project 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 V8_CONVERSIONS_INL_H_ 5 #ifndef V8_CONVERSIONS_INL_H_
6 #define V8_CONVERSIONS_INL_H_ 6 #define V8_CONVERSIONS_INL_H_
7 7
8 #include <float.h> // Required for DBL_MAX and on Win32 for finite() 8 #include <float.h> // Required for DBL_MAX and on Win32 for finite()
9 #include <limits.h> // Required for INT_MAX etc. 9 #include <limits.h> // Required for INT_MAX etc.
10 #include <stdarg.h> 10 #include <stdarg.h>
(...skipping 110 matching lines...) Expand 10 before | Expand all | Expand 10 after
121 return !IsMinusZero(value) && value >= 0 && value <= kMaxUInt32 && 121 return !IsMinusZero(value) && value >= 0 && value <= kMaxUInt32 &&
122 value == FastUI2D(FastD2UI(value)); 122 value == FastUI2D(FastD2UI(value));
123 } 123 }
124 124
125 125
126 int32_t NumberToInt32(Object* number) { 126 int32_t NumberToInt32(Object* number) {
127 if (number->IsSmi()) return Smi::cast(number)->value(); 127 if (number->IsSmi()) return Smi::cast(number)->value();
128 return DoubleToInt32(number->Number()); 128 return DoubleToInt32(number->Number());
129 } 129 }
130 130
131
132 uint32_t NumberToUint32(Object* number) { 131 uint32_t NumberToUint32(Object* number) {
133 if (number->IsSmi()) return Smi::cast(number)->value(); 132 if (number->IsSmi()) return Smi::cast(number)->value();
134 return DoubleToUint32(number->Number()); 133 return DoubleToUint32(number->Number());
135 } 134 }
136 135
136 uint32_t PositiveNumberToUint32(Object* number) {
137 if (number->IsSmi()) {
138 int value = Smi::cast(number)->value();
139 if (value <= 0) return 0;
140 return value;
141 }
142 DCHECK(number->IsHeapNumber());
143 double value = number->Number();
144 // Catch all values smaller than 1 and use the double-negation trick for NANs.
145 if (!(value >= 1)) return 0;
146 uint32_t max = std::numeric_limits<uint32_t>::max();
147 if (value < max) return static_cast<uint32_t>(value);
148 return max;
149 }
150
137 int64_t NumberToInt64(Object* number) { 151 int64_t NumberToInt64(Object* number) {
138 if (number->IsSmi()) return Smi::cast(number)->value(); 152 if (number->IsSmi()) return Smi::cast(number)->value();
139 return static_cast<int64_t>(number->Number()); 153 return static_cast<int64_t>(number->Number());
140 } 154 }
141 155
142 bool TryNumberToSize(Object* number, size_t* result) { 156 bool TryNumberToSize(Object* number, size_t* result) {
143 // Do not create handles in this function! Don't use SealHandleScope because 157 // Do not create handles in this function! Don't use SealHandleScope because
144 // the function can be used concurrently. 158 // the function can be used concurrently.
145 if (number->IsSmi()) { 159 if (number->IsSmi()) {
146 int value = Smi::cast(number)->value(); 160 int value = Smi::cast(number)->value();
(...skipping 623 matching lines...) Expand 10 before | Expand all | Expand 10 after
770 buffer[buffer_pos] = '\0'; 784 buffer[buffer_pos] = '\0';
771 785
772 double converted = Strtod(Vector<const char>(buffer, buffer_pos), exponent); 786 double converted = Strtod(Vector<const char>(buffer, buffer_pos), exponent);
773 return (sign == NEGATIVE) ? -converted : converted; 787 return (sign == NEGATIVE) ? -converted : converted;
774 } 788 }
775 789
776 } // namespace internal 790 } // namespace internal
777 } // namespace v8 791 } // namespace v8
778 792
779 #endif // V8_CONVERSIONS_INL_H_ 793 #endif // V8_CONVERSIONS_INL_H_
OLDNEW
« no previous file with comments | « src/conversions.h ('k') | src/objects.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698