aboutsummaryrefslogtreecommitdiff
path: root/src/long.js
diff options
context:
space:
mode:
authorAlon Zakai <alonzakai@gmail.com>2012-04-01 18:14:46 -0700
committerAlon Zakai <alonzakai@gmail.com>2012-04-01 18:14:46 -0700
commit609bf2ea07190fd87a875ce5fef7016ee2a0ee9b (patch)
tree2d2d7da9bfcad051fe2e1ed0d47eac8f9ed855bd /src/long.js
parenta08fe05250cd2ebd89dd832321d7d2f6c1711b9a (diff)
proper precise unsigned i64 modulo, and passing tests for i64 division and modulo
Diffstat (limited to 'src/long.js')
-rw-r--r--src/long.js26
1 files changed, 20 insertions, 6 deletions
diff --git a/src/long.js b/src/long.js
index 47a194ce..71cffa79 100644
--- a/src/long.js
+++ b/src/long.js
@@ -1588,12 +1588,26 @@ var Wrapper = {
Wrapper.result[1] = parseInt(h.toString()) | 0;
}
},
- modulo: function(xl, xh, yl, yh) {
- var x = new goog.math.Long(xl, xh);
- var y = new goog.math.Long(yl, yh);
- var ret = x.modulo(y);
- Wrapper.result[0] = ret.low_;
- Wrapper.result[1] = ret.high_;
+ modulo: function(xl, xh, yl, yh, unsigned) {
+ if (!Wrapper.two32) Wrapper.makeTwo32();
+ if (!unsigned) {
+ var x = new goog.math.Long(xl, xh);
+ var y = new goog.math.Long(yl, yh);
+ var ret = x.modulo(y);
+ Wrapper.result[0] = ret.low_;
+ Wrapper.result[1] = ret.high_;
+ } else {
+ // slow precise bignum division
+ var x = Wrapper.lh2bignum(xl >>> 0, xh >>> 0);
+ var y = Wrapper.lh2bignum(yl >>> 0, yh >>> 0);
+ var z = new BigInteger();
+ x.divRemTo(y, null, z);
+ var l = new BigInteger();
+ var h = new BigInteger();
+ z.divRemTo(Wrapper.two32, h, l);
+ Wrapper.result[0] = parseInt(l.toString()) | 0;
+ Wrapper.result[1] = parseInt(h.toString()) | 0;
+ }
},
stringify: function(l, h, unsigned) {
var ret = new goog.math.Long(l, h).toString();