OLD | NEW |
1 // Copyright 2012 Google Inc. All Rights Reserved. | 1 // Copyright 2012 Google Inc. All Rights Reserved. |
2 | 2 |
3 #ifndef VM_BIGINT_OPERATIONS_H_ | 3 #ifndef VM_BIGINT_OPERATIONS_H_ |
4 #define VM_BIGINT_OPERATIONS_H_ | 4 #define VM_BIGINT_OPERATIONS_H_ |
5 | 5 |
6 #include "platform/utils.h" | 6 #include "platform/utils.h" |
7 | 7 |
8 #include "vm/object.h" | 8 #include "vm/object.h" |
9 | 9 |
10 namespace dart { | 10 namespace dart { |
11 | 11 |
12 class BigintOperations : public AllStatic { | 12 class BigintOperations : public AllStatic { |
13 public: | 13 public: |
14 static RawBigint* NewFromSmi(const Smi& smi, Heap::Space space = Heap::kNew); | 14 static RawBigint* NewFromSmi(const Smi& smi, Heap::Space space = Heap::kNew); |
15 static RawBigint* NewFromInt64(int64_t value, Heap::Space space = Heap::kNew); | 15 static RawBigint* NewFromInt64(int64_t value, Heap::Space space = Heap::kNew); |
16 static RawBigint* NewFromUint64(uint64_t value, | 16 static RawBigint* NewFromUint64(uint64_t value, |
17 Heap::Space space = Heap::kNew); | 17 Heap::Space space = Heap::kNew); |
18 // The given string must be a valid integer representation. It may be | 18 // The given string must be a valid integer representation. It may be |
19 // prefixed by a minus and/or "0x". | 19 // prefixed by a minus and/or "0x". |
20 // Returns Bigint::null() if the string cannot be parsed. | 20 // Returns Bigint::null() if the string cannot be parsed. |
21 static RawBigint* NewFromCString(const char* str, | 21 static RawBigint* NewFromCString(const char* str, |
22 Heap::Space space = Heap::kNew); | 22 Heap::Space space = Heap::kNew); |
23 static RawBigint* NewFromDouble(double d, Heap::Space space = Heap::kNew); | 23 static RawBigint* NewFromDouble(double d, Heap::Space space = Heap::kNew); |
24 | 24 |
25 // The given string must be a nul-terminated string of hex-digits. It must | 25 // Compute chunk length of the bigint instance created for the |
26 // only contain hex-digits. No sign or leading "0x" is allowed. | 26 // specified hex string. The specified hex string must be a |
27 static RawBigint* FromHexCString(const char* str, | 27 // nul-terminated string of hex-digits. It must only contain |
| 28 // hex-digits. Leading "0x" is not allowed. |
| 29 static intptr_t ComputeChunkLength(const char* hex_string); |
| 30 |
| 31 // Create a bigint instance from the specified hex string. The given string |
| 32 // must be a nul-terminated string of hex-digits. It must only contain |
| 33 // hex-digits. Leading "0x" is not allowed. |
| 34 static RawBigint* FromHexCString(const char* hex_string, |
28 Heap::Space space = Heap::kNew); | 35 Heap::Space space = Heap::kNew); |
29 | 36 |
| 37 // Helper method to initialize a bigint instance object with the bigint value |
| 38 // in the specified string. The given string must be a nul-terminated string |
| 39 // of hex-digits. It must only contain hex-digits, leading "0x" is not |
| 40 // allowed. |
| 41 static void FromHexCString(const char* hex_string, const Bigint& value); |
| 42 |
30 // The given string must be a nul-terminated string of decimal digits. It | 43 // The given string must be a nul-terminated string of decimal digits. It |
31 // must only contain decimal digits (0-9). No sign is allowed. Leading | 44 // must only contain decimal digits (0-9). No sign is allowed. Leading |
32 // zeroes are ignored. | 45 // zeroes are ignored. |
33 static RawBigint* FromDecimalCString(const char* str, | 46 static RawBigint* FromDecimalCString(const char* str, |
34 Heap::Space space = Heap::kNew); | 47 Heap::Space space = Heap::kNew); |
35 | 48 |
36 // Converts the bigint to a HEX string. The returned string is prepended by | 49 // Converts the bigint to a HEX string. The returned string is prepended by |
37 // a "0x" (after the optional minus-sign). | 50 // a "0x" (after the optional minus-sign). |
38 static const char* ToHexCString(intptr_t length, | 51 static const char* ToHexCString(intptr_t length, |
39 bool is_negative, | 52 bool is_negative, |
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
88 | 101 |
89 private: | 102 private: |
90 typedef Bigint::Chunk Chunk; | 103 typedef Bigint::Chunk Chunk; |
91 typedef Bigint::DoubleChunk DoubleChunk; | 104 typedef Bigint::DoubleChunk DoubleChunk; |
92 | 105 |
93 static const int kDigitBitSize = 28; | 106 static const int kDigitBitSize = 28; |
94 static const Chunk kDigitMask = (static_cast<Chunk>(1) << kDigitBitSize) - 1; | 107 static const Chunk kDigitMask = (static_cast<Chunk>(1) << kDigitBitSize) - 1; |
95 static const Chunk kDigitMaxValue = kDigitMask; | 108 static const Chunk kDigitMaxValue = kDigitMask; |
96 static const int kChunkSize = sizeof(Chunk); | 109 static const int kChunkSize = sizeof(Chunk); |
97 static const int kChunkBitSize = kChunkSize * kBitsPerByte; | 110 static const int kChunkBitSize = kChunkSize * kBitsPerByte; |
| 111 static const int kHexCharsPerDigit = kDigitBitSize / 4; |
98 | 112 |
99 static RawBigint* Zero() { return Bigint::Allocate(0); } | 113 static RawBigint* Zero() { return Bigint::Allocate(0); } |
100 static RawBigint* One() { | 114 static RawBigint* One() { |
101 Bigint& result = Bigint::Handle(Bigint::Allocate(1)); | 115 Bigint& result = Bigint::Handle(Bigint::Allocate(1)); |
102 result.SetChunkAt(0, 1); | 116 result.SetChunkAt(0, 1); |
103 return result.raw(); | 117 return result.raw(); |
104 } | 118 } |
105 static RawBigint* MinusOne() { | 119 static RawBigint* MinusOne() { |
106 Bigint& result = Bigint::Handle(One()); | 120 Bigint& result = Bigint::Handle(One()); |
107 result.ToggleSign(); | 121 result.ToggleSign(); |
(...skipping 23 matching lines...) Expand all Loading... |
131 static RawBigint* Copy(const Bigint& bigint); | 145 static RawBigint* Copy(const Bigint& bigint); |
132 | 146 |
133 static int CountBits(Chunk digit); | 147 static int CountBits(Chunk digit); |
134 | 148 |
135 DISALLOW_IMPLICIT_CONSTRUCTORS(BigintOperations); | 149 DISALLOW_IMPLICIT_CONSTRUCTORS(BigintOperations); |
136 }; | 150 }; |
137 | 151 |
138 } // namespace dart | 152 } // namespace dart |
139 | 153 |
140 #endif // VM_BIGINT_OPERATIONS_H_ | 154 #endif // VM_BIGINT_OPERATIONS_H_ |
OLD | NEW |