| OLD | NEW |
| 1 // Copyright 2006-2008 the V8 project authors. All rights reserved. | 1 // Copyright 2011 the V8 project authors. All rights reserved. |
| 2 // Redistribution and use in source and binary forms, with or without | 2 // Redistribution and use in source and binary forms, with or without |
| 3 // modification, are permitted provided that the following conditions are | 3 // modification, are permitted provided that the following conditions are |
| 4 // met: | 4 // met: |
| 5 // | 5 // |
| 6 // * Redistributions of source code must retain the above copyright | 6 // * Redistributions of source code must retain the above copyright |
| 7 // notice, this list of conditions and the following disclaimer. | 7 // notice, this list of conditions and the following disclaimer. |
| 8 // * Redistributions in binary form must reproduce the above | 8 // * Redistributions in binary form must reproduce the above |
| 9 // copyright notice, this list of conditions and the following | 9 // copyright notice, this list of conditions and the following |
| 10 // disclaimer in the documentation and/or other materials provided | 10 // disclaimer in the documentation and/or other materials provided |
| 11 // with the distribution. | 11 // with the distribution. |
| (...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 70 } | 70 } |
| 71 | 71 |
| 72 | 72 |
| 73 static inline double DoubleToInteger(double x) { | 73 static inline double DoubleToInteger(double x) { |
| 74 if (isnan(x)) return 0; | 74 if (isnan(x)) return 0; |
| 75 if (!isfinite(x) || x == 0) return x; | 75 if (!isfinite(x) || x == 0) return x; |
| 76 return (x >= 0) ? floor(x) : ceil(x); | 76 return (x >= 0) ? floor(x) : ceil(x); |
| 77 } | 77 } |
| 78 | 78 |
| 79 | 79 |
| 80 uint8_t ClampToUInt8(double double_value) { |
| 81 if (!(double_value > 0)) { |
| 82 // NaN and less than zero clamp to zero. |
| 83 return 0; |
| 84 } else if (double_value > 255) { |
| 85 // Greater than 255 clamp to 255. |
| 86 return 255; |
| 87 } else { |
| 88 // Other doubles are rounded to the nearest integer. |
| 89 return static_cast<uint8_t>(double_value + 0.5); |
| 90 } |
| 91 } |
| 92 |
| 93 |
| 80 int32_t NumberToInt32(Object* number) { | 94 int32_t NumberToInt32(Object* number) { |
| 81 if (number->IsSmi()) return Smi::cast(number)->value(); | 95 if (number->IsSmi()) return Smi::cast(number)->value(); |
| 82 return DoubleToInt32(number->Number()); | 96 return DoubleToInt32(number->Number()); |
| 83 } | 97 } |
| 84 | 98 |
| 85 | 99 |
| 86 uint32_t NumberToUint32(Object* number) { | 100 uint32_t NumberToUint32(Object* number) { |
| 87 if (number->IsSmi()) return Smi::cast(number)->value(); | 101 if (number->IsSmi()) return Smi::cast(number)->value(); |
| 88 return DoubleToUint32(number->Number()); | 102 return DoubleToUint32(number->Number()); |
| 89 } | 103 } |
| 90 | 104 |
| 91 | 105 |
| 106 uint8_t NumberToClampedUInt8(Object* number) |
| 107 { |
| 108 if (number->IsSmi()) { |
| 109 int32_t value = Smi::cast(number)->value(); |
| 110 if (value < 0) { |
| 111 return 0; |
| 112 } else if (value > 255) { |
| 113 return 255; |
| 114 } else { |
| 115 return static_cast<uint8_t>(value); |
| 116 } |
| 117 } else if (number->IsHeapNumber()) { |
| 118 return ClampToUInt8(number->Number()); |
| 119 } else { |
| 120 // Clamp undefined to zero (default). All other types have been |
| 121 // converted to a number type further up in the call chain. |
| 122 ASSERT(number->IsUndefined()); |
| 123 return 0; |
| 124 } |
| 125 } |
| 126 |
| 127 |
| 92 int32_t DoubleToInt32(double x) { | 128 int32_t DoubleToInt32(double x) { |
| 93 int32_t i = FastD2I(x); | 129 int32_t i = FastD2I(x); |
| 94 if (FastI2D(i) == x) return i; | 130 if (FastI2D(i) == x) return i; |
| 95 static const double two32 = 4294967296.0; | 131 static const double two32 = 4294967296.0; |
| 96 static const double two31 = 2147483648.0; | 132 static const double two31 = 2147483648.0; |
| 97 if (!isfinite(x) || x == 0) return 0; | 133 if (!isfinite(x) || x == 0) return 0; |
| 98 if (x < 0 || x >= two32) x = modulo(x, two32); | 134 if (x < 0 || x >= two32) x = modulo(x, two32); |
| 99 x = (x >= 0) ? floor(x) : ceil(x) + two32; | 135 x = (x >= 0) ? floor(x) : ceil(x) + two32; |
| 100 return (int32_t) ((x >= two31) ? x - two32 : x); | 136 return (int32_t) ((x >= two31) ? x - two32 : x); |
| 101 } | 137 } |
| 102 | 138 |
| 103 | 139 |
| 104 } } // namespace v8::internal | 140 } } // namespace v8::internal |
| 105 | 141 |
| 106 #endif // V8_CONVERSIONS_INL_H_ | 142 #endif // V8_CONVERSIONS_INL_H_ |
| OLD | NEW |