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

Side by Side Diff: runtime/lib/double.cc

Issue 2974633003: Option to truncate integers to 64 bits, part 1 (core VM changes) (Closed)
Patch Set: Corrections in comments Created 3 years, 5 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
« no previous file with comments | « no previous file | runtime/lib/integers.cc » ('j') | runtime/lib/integers.cc » ('J')
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2011, 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 #include "vm/bootstrap_natives.h" 5 #include "vm/bootstrap_natives.h"
6 6
7 #include "platform/math.h" 7 #include "platform/math.h"
8 8
9 #include "vm/dart_entry.h" 9 #include "vm/dart_entry.h"
10 #include "vm/double_conversion.h" 10 #include "vm/double_conversion.h"
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after
72 return Double::New(left / right); 72 return Double::New(left / right);
73 } 73 }
74 74
75 75
76 static RawInteger* DoubleToInteger(double val, const char* error_msg) { 76 static RawInteger* DoubleToInteger(double val, const char* error_msg) {
77 if (isinf(val) || isnan(val)) { 77 if (isinf(val) || isnan(val)) {
78 const Array& args = Array::Handle(Array::New(1)); 78 const Array& args = Array::Handle(Array::New(1));
79 args.SetAt(0, String::Handle(String::New(error_msg))); 79 args.SetAt(0, String::Handle(String::New(error_msg)));
80 Exceptions::ThrowByType(Exceptions::kUnsupported, args); 80 Exceptions::ThrowByType(Exceptions::kUnsupported, args);
81 } 81 }
82 if (FLAG_truncate_ints_to_64_bits) {
83 // TODO(alexmarkov): decide on the double-to-integer conversion semantics
84 // in truncating mode.
85 int64_t ival = 0;
86 if (val <= static_cast<double>(kMinInt64)) {
87 ival = kMinInt64;
88 } else if (val >= static_cast<double>(kMaxInt64)) {
89 ival = kMaxInt64;
90 } else { // Representable in int64_t.
91 ival = static_cast<int64_t>(val);
92 }
93 return Integer::New(ival);
94 }
82 if ((-1.0 < val) && (val < 1.0)) { 95 if ((-1.0 < val) && (val < 1.0)) {
83 return Smi::New(0); 96 return Smi::New(0);
84 } 97 }
85 DoubleInternals internals = DoubleInternals(val); 98 DoubleInternals internals = DoubleInternals(val);
86 ASSERT(!internals.IsSpecial()); // Only Infinity and NaN are special. 99 ASSERT(!internals.IsSpecial()); // Only Infinity and NaN are special.
87 uint64_t significand = internals.Significand(); 100 uint64_t significand = internals.Significand();
88 intptr_t exponent = internals.Exponent(); 101 intptr_t exponent = internals.Exponent();
89 if (exponent <= 0) { 102 if (exponent <= 0) {
90 significand >>= -exponent; 103 significand >>= -exponent;
91 exponent = 0; 104 exponent = 0;
92 } else if (exponent <= 10) { 105 } else if (exponent <= 10) {
93 // A double significand has at most 53 bits. The following shift will 106 // A double significand has at most 53 bits. The following shift will
94 // hence not overflow, and yield an integer of at most 63 bits. 107 // hence not overflow, and yield an integer of at most 63 bits.
95 significand <<= exponent; 108 significand <<= exponent;
96 exponent = 0; 109 exponent = 0;
97 } 110 }
98 // A significand has at most 63 bits (after the shift above). 111 // A significand has at most 63 bits (after the shift above).
99 // The cast to int64_t is hence safe. 112 // The cast to int64_t is hence safe.
100 int64_t ival = static_cast<int64_t>(significand); 113 int64_t ival = static_cast<int64_t>(significand);
101 if (internals.Sign() < 0) { 114 if (internals.Sign() < 0) {
102 ival = -ival; 115 ival = -ival;
103 } 116 }
104 if (exponent == 0) { 117 if (exponent == 0) {
105 // The double fits in a Smi or Mint. 118 // The double fits in a Smi or Mint.
106 return Integer::New(ival); 119 return Integer::New(ival);
107 } 120 }
siva 2017/07/07 23:11:20 Why not add your new code here if (Flag_...) { i
alexmarkov 2017/07/10 16:58:12 Although the code up to this points works fine in
108 Integer& result = Integer::Handle(); 121 Integer& result = Integer::Handle();
109 result = Bigint::NewFromShiftedInt64(ival, exponent); 122 result = Bigint::NewFromShiftedInt64(ival, exponent);
110 return result.AsValidInteger(); 123 return result.AsValidInteger();
111 } 124 }
112 125
113 126
114 DEFINE_NATIVE_ENTRY(Double_trunc_div, 2) { 127 DEFINE_NATIVE_ENTRY(Double_trunc_div, 2) {
115 double left = Double::CheckedHandle(arguments->NativeArgAt(0)).value(); 128 double left = Double::CheckedHandle(arguments->NativeArgAt(0)).value();
116 GET_NON_NULL_NATIVE_ARGUMENT(Double, right_object, arguments->NativeArgAt(1)); 129 GET_NON_NULL_NATIVE_ARGUMENT(Double, right_object, arguments->NativeArgAt(1));
117 double right = right_object.value(); 130 double right = right_object.value();
(...skipping 191 matching lines...) Expand 10 before | Expand all | Expand 10 after
309 DEFINE_NATIVE_ENTRY(Double_flipSignBit, 1) { 322 DEFINE_NATIVE_ENTRY(Double_flipSignBit, 1) {
310 const Double& arg = Double::CheckedHandle(arguments->NativeArgAt(0)); 323 const Double& arg = Double::CheckedHandle(arguments->NativeArgAt(0));
311 const double in_val = arg.value(); 324 const double in_val = arg.value();
312 const int64_t bits = bit_cast<int64_t, double>(in_val) ^ kSignBitDouble; 325 const int64_t bits = bit_cast<int64_t, double>(in_val) ^ kSignBitDouble;
313 return Double::New(bit_cast<double, int64_t>(bits)); 326 return Double::New(bit_cast<double, int64_t>(bits));
314 } 327 }
315 328
316 // Add here only functions using/referring to old-style casts. 329 // Add here only functions using/referring to old-style casts.
317 330
318 } // namespace dart 331 } // namespace dart
OLDNEW
« no previous file with comments | « no previous file | runtime/lib/integers.cc » ('j') | runtime/lib/integers.cc » ('J')

Powered by Google App Engine
This is Rietveld 408576698