Skip to content

Commit

Permalink
feat(long): replace long implementatin with long.js
Browse files Browse the repository at this point in the history
There have been improvements on the original implementation since
it was vendored in, including a wasm implementation. We will now
track the community module.
  • Loading branch information
mbroadst committed Aug 2, 2018
1 parent 3c922ad commit 545900d
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 938 deletions.
48 changes: 24 additions & 24 deletions lib/decimal128.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ function divideu128(value) {
_rem = _rem.shiftLeft(32);
// Add the divided to _rem
_rem = _rem.add(new Long(value.parts[i], 0));
value.parts[i] = _rem.div(DIVISOR).low_;
value.parts[i] = _rem.div(DIVISOR).low;
_rem = _rem.modulo(DIVISOR);
}

Expand Down Expand Up @@ -126,15 +126,15 @@ function multiply64x2(left, right) {

function lessThan(left, right) {
// Make values unsigned
const uhleft = left.high_ >>> 0;
const uhright = right.high_ >>> 0;
const uhleft = left.high >>> 0;
const uhright = right.high >>> 0;

// Compare high bits first
if (uhleft < uhright) {
return true;
} else if (uhleft === uhright) {
const ulleft = left.low_ >>> 0;
const ulright = right.low_ >>> 0;
const ulleft = left.low >>> 0;
const ulright = right.low >>> 0;
if (ulleft < ulright) return true;
}

Expand All @@ -153,7 +153,6 @@ function invalidErr(string, message) {
* @return {Double}
*/
function Decimal128(bytes) {
this._bsontype = 'Decimal128';
this.bytes = bytes;
}

Expand Down Expand Up @@ -499,7 +498,7 @@ Decimal128.fromString = function(string) {
significand.high
.shiftRightUnsigned(49)
.and(Long.fromNumber(1))
.equals(Long.fromNumber)
.equals(Long.fromNumber(1))
) {
// Encode '11' into bits 1 to 3
dec.high = dec.high.or(Long.fromNumber(0x3).shiftLeft(61));
Expand All @@ -525,27 +524,27 @@ Decimal128.fromString = function(string) {

// Encode the low 64 bits of the decimal
// Encode low bits
buffer[index++] = dec.low.low_ & 0xff;
buffer[index++] = (dec.low.low_ >> 8) & 0xff;
buffer[index++] = (dec.low.low_ >> 16) & 0xff;
buffer[index++] = (dec.low.low_ >> 24) & 0xff;
buffer[index++] = dec.low.low & 0xff;
buffer[index++] = (dec.low.low >> 8) & 0xff;
buffer[index++] = (dec.low.low >> 16) & 0xff;
buffer[index++] = (dec.low.low >> 24) & 0xff;
// Encode high bits
buffer[index++] = dec.low.high_ & 0xff;
buffer[index++] = (dec.low.high_ >> 8) & 0xff;
buffer[index++] = (dec.low.high_ >> 16) & 0xff;
buffer[index++] = (dec.low.high_ >> 24) & 0xff;
buffer[index++] = dec.low.high & 0xff;
buffer[index++] = (dec.low.high >> 8) & 0xff;
buffer[index++] = (dec.low.high >> 16) & 0xff;
buffer[index++] = (dec.low.high >> 24) & 0xff;

// Encode the high 64 bits of the decimal
// Encode low bits
buffer[index++] = dec.high.low_ & 0xff;
buffer[index++] = (dec.high.low_ >> 8) & 0xff;
buffer[index++] = (dec.high.low_ >> 16) & 0xff;
buffer[index++] = (dec.high.low_ >> 24) & 0xff;
buffer[index++] = dec.high.low & 0xff;
buffer[index++] = (dec.high.low >> 8) & 0xff;
buffer[index++] = (dec.high.low >> 16) & 0xff;
buffer[index++] = (dec.high.low >> 24) & 0xff;
// Encode high bits
buffer[index++] = dec.high.high_ & 0xff;
buffer[index++] = (dec.high.high_ >> 8) & 0xff;
buffer[index++] = (dec.high.high_ >> 16) & 0xff;
buffer[index++] = (dec.high.high_ >> 24) & 0xff;
buffer[index++] = dec.high.high & 0xff;
buffer[index++] = (dec.high.high >> 8) & 0xff;
buffer[index++] = (dec.high.high >> 16) & 0xff;
buffer[index++] = (dec.high.high >> 24) & 0xff;

// Return the new Decimal128
return new Decimal128(buffer);
Expand Down Expand Up @@ -682,7 +681,7 @@ Decimal128.prototype.toString = function() {
// Peform the divide
let result = divideu128(significand128);
significand128 = result.quotient;
least_digits = result.rem.low_;
least_digits = result.rem.low;

// We now have the 9 least significant digits (in base 2).
// Convert and output to string.
Expand Down Expand Up @@ -789,4 +788,5 @@ Decimal128.prototype.toJSON = function() {
return { $numberDecimal: this.toString() };
};

Object.defineProperty(Decimal128.prototype, '_bsontype', { value: 'Decimal128' });
module.exports = Decimal128;
Loading

0 comments on commit 545900d

Please sign in to comment.