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

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

Issue 23645003: Esoteric bit operations. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 7 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 | runtime/lib/integers.dart » ('j') | runtime/lib/integers.dart » ('J')
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 #include "vm/bootstrap_natives.h" 5 #include "vm/bootstrap_natives.h"
6 6
7 #include "vm/bigint_operations.h" 7 #include "vm/bigint_operations.h"
8 #include "vm/dart_entry.h" 8 #include "vm/dart_entry.h"
9 #include "vm/exceptions.h" 9 #include "vm/exceptions.h"
10 #include "vm/native_entry.h" 10 #include "vm/native_entry.h"
(...skipping 17 matching lines...) Expand all
28 !BigintOperations::FitsIntoInt64(bigint); 28 !BigintOperations::FitsIntoInt64(bigint);
29 } 29 }
30 if (i.IsMint()) { 30 if (i.IsMint()) {
31 const Mint& mint = Mint::Cast(i); 31 const Mint& mint = Mint::Cast(i);
32 return !Smi::IsValid64(mint.value()); 32 return !Smi::IsValid64(mint.value());
33 } 33 }
34 return true; 34 return true;
35 } 35 }
36 36
37 37
38 static int bitLength(uint64_t value) {
srdjan 2013/09/06 15:35:34 s/bitLength/BitLength/ (Dart vs C++ style)
sra1 2013/09/06 22:11:57 Done.
39 // TODO(sra): Use (64 - __builtin_clzll(value));
40 int result = 0;
41 uint32_t value32;
42 if (value >= (static_cast<uint64_t>(1) << 32)) {
srdjan 2013/09/06 15:35:34 You can use Utils::IsUint(32, value), also for the
sra1 2013/09/06 22:11:57 Done.
43 result += 32;
44 value32 = static_cast<uint32_t>(value >> 32);
45 } else {
46 value32 = static_cast<uint32_t>(value);
47 }
48 if (value32 >= (1 << 16)) {
49 result += 16;
50 value32 >>= 16;
51 }
52 if (value32 >= (1 << 8)) {
53 result += 8;
54 value32 >>= 8;
55 }
56 if (value32 >= (1 << 4)) {
57 result += 4;
58 value32 >>= 4;
59 }
60 if (value32 >= (1 << 2)) {
61 result += 2;
62 value32 >>= 2;
63 }
64 if (value32 >= (1 << 1)) {
65 result += 1;
66 value32 >>= 1;
67 }
68 if (value32 >= 1) {
69 result += 1;
70 }
71 return result;
72 }
73
74
38 DEFINE_NATIVE_ENTRY(Integer_bitAndFromInteger, 2) { 75 DEFINE_NATIVE_ENTRY(Integer_bitAndFromInteger, 2) {
39 const Integer& right = Integer::CheckedHandle(arguments->NativeArgAt(0)); 76 const Integer& right = Integer::CheckedHandle(arguments->NativeArgAt(0));
40 GET_NON_NULL_NATIVE_ARGUMENT(Integer, left, arguments->NativeArgAt(1)); 77 GET_NON_NULL_NATIVE_ARGUMENT(Integer, left, arguments->NativeArgAt(1));
41 ASSERT(CheckInteger(right)); 78 ASSERT(CheckInteger(right));
42 ASSERT(CheckInteger(left)); 79 ASSERT(CheckInteger(left));
43 if (FLAG_trace_intrinsified_natives) { 80 if (FLAG_trace_intrinsified_natives) {
44 OS::Print("Integer_bitAndFromInteger %s & %s\n", 81 OS::Print("Integer_bitAndFromInteger %s & %s\n",
45 right.ToCString(), left.ToCString()); 82 right.ToCString(), left.ToCString());
46 } 83 }
47 const Integer& result = 84 const Integer& result =
(...skipping 272 matching lines...) Expand 10 before | Expand all | Expand 10 after
320 const Smi& operand = Smi::CheckedHandle(arguments->NativeArgAt(0)); 357 const Smi& operand = Smi::CheckedHandle(arguments->NativeArgAt(0));
321 if (FLAG_trace_intrinsified_natives) { 358 if (FLAG_trace_intrinsified_natives) {
322 OS::Print("Smi_bitNegate: %s\n", operand.ToCString()); 359 OS::Print("Smi_bitNegate: %s\n", operand.ToCString());
323 } 360 }
324 intptr_t result = ~operand.Value(); 361 intptr_t result = ~operand.Value();
325 ASSERT(Smi::IsValid(result)); 362 ASSERT(Smi::IsValid(result));
326 return Smi::New(result); 363 return Smi::New(result);
327 } 364 }
328 365
329 366
367 DEFINE_NATIVE_ENTRY(Smi_bitLength, 1) {
368 const Smi& operand = Smi::CheckedHandle(arguments->NativeArgAt(0));
369 if (FLAG_trace_intrinsified_natives) {
370 OS::Print("Smi_bitLength: %s\n", operand.ToCString());
371 }
372 int64_t value = operand.AsInt64Value();
373 value ^= value >> (8 * sizeof(value) - 1); // flip bits if negative.
374
375 intptr_t result = bitLength(value);
376
377 ASSERT(Smi::IsValid(result));
378 return Smi::New(result);
379 }
380
381
330 // Mint natives. 382 // Mint natives.
331 383
332 DEFINE_NATIVE_ENTRY(Mint_bitNegate, 1) { 384 DEFINE_NATIVE_ENTRY(Mint_bitNegate, 1) {
333 const Mint& operand = Mint::CheckedHandle(arguments->NativeArgAt(0)); 385 const Mint& operand = Mint::CheckedHandle(arguments->NativeArgAt(0));
334 ASSERT(CheckInteger(operand)); 386 ASSERT(CheckInteger(operand));
335 if (FLAG_trace_intrinsified_natives) { 387 if (FLAG_trace_intrinsified_natives) {
336 OS::Print("Mint_bitNegate: %s\n", operand.ToCString()); 388 OS::Print("Mint_bitNegate: %s\n", operand.ToCString());
337 } 389 }
338 int64_t result = ~operand.value(); 390 int64_t result = ~operand.value();
339 return Integer::New(result); 391 return Integer::New(result);
340 } 392 }
341 393
342 394
395 DEFINE_NATIVE_ENTRY(Mint_bitLength, 1) {
396 const Mint& operand = Mint::CheckedHandle(arguments->NativeArgAt(0));
397 ASSERT(CheckInteger(operand));
398 if (FLAG_trace_intrinsified_natives) {
399 OS::Print("Mint_bitLength: %s\n", operand.ToCString());
400 }
401 int64_t value = operand.AsInt64Value();
402 value ^= value >> (8 * sizeof(value) - 1); // flip bits if negative.
403
404 intptr_t result = bitLength(value);
405
406 ASSERT(Smi::IsValid(result));
407 return Smi::New(result);
408 }
409
410
343 DEFINE_NATIVE_ENTRY(Mint_shlFromInt, 2) { 411 DEFINE_NATIVE_ENTRY(Mint_shlFromInt, 2) {
344 // Use the preallocated out of memory exception to avoid calling 412 // Use the preallocated out of memory exception to avoid calling
345 // into dart code or allocating any code. 413 // into dart code or allocating any code.
346 const Instance& exception = 414 const Instance& exception =
347 Instance::Handle(isolate->object_store()->out_of_memory()); 415 Instance::Handle(isolate->object_store()->out_of_memory());
348 Exceptions::Throw(exception); 416 Exceptions::Throw(exception);
349 UNREACHABLE(); 417 UNREACHABLE();
350 return 0; 418 return 0;
351 } 419 }
352 420
353 421
354 // Bigint natives. 422 // Bigint natives.
355 423
356 DEFINE_NATIVE_ENTRY(Bigint_bitNegate, 1) { 424 DEFINE_NATIVE_ENTRY(Bigint_bitNegate, 1) {
357 const Bigint& value = Bigint::CheckedHandle(arguments->NativeArgAt(0)); 425 const Bigint& value = Bigint::CheckedHandle(arguments->NativeArgAt(0));
358 const Bigint& result = Bigint::Handle(BigintOperations::BitNot(value)); 426 const Bigint& result = Bigint::Handle(BigintOperations::BitNot(value));
359 ASSERT(CheckInteger(value)); 427 ASSERT(CheckInteger(value));
360 ASSERT(CheckInteger(result)); 428 ASSERT(CheckInteger(result));
361 return result.AsValidInteger(); 429 return result.AsValidInteger();
362 } 430 }
363 431
364 432
433 DEFINE_NATIVE_ENTRY(Bigint_bitLength, 1) {
434 const Bigint& value = Bigint::CheckedHandle(arguments->NativeArgAt(0));
435 return Integer::New(BigintOperations::BitLength(value));
436 }
437
438
365 DEFINE_NATIVE_ENTRY(Bigint_shlFromInt, 2) { 439 DEFINE_NATIVE_ENTRY(Bigint_shlFromInt, 2) {
366 // Use the preallocated out of memory exception to avoid calling 440 // Use the preallocated out of memory exception to avoid calling
367 // into dart code or allocating any code. 441 // into dart code or allocating any code.
368 const Instance& exception = 442 const Instance& exception =
369 Instance::Handle(isolate->object_store()->out_of_memory()); 443 Instance::Handle(isolate->object_store()->out_of_memory());
370 Exceptions::Throw(exception); 444 Exceptions::Throw(exception);
371 UNREACHABLE(); 445 UNREACHABLE();
372 return 0; 446 return 0;
373 } 447 }
374 448
375 } // namespace dart 449 } // namespace dart
OLDNEW
« no previous file with comments | « no previous file | runtime/lib/integers.dart » ('j') | runtime/lib/integers.dart » ('J')

Powered by Google App Engine
This is Rietveld 408576698