Skip to content

Commit

Permalink
Improved BigInt
Browse files Browse the repository at this point in the history
  • Loading branch information
shiroyk committed Aug 18, 2024
1 parent f6b3b46 commit 53edc48
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 14 deletions.
10 changes: 5 additions & 5 deletions builtin_bigint.go
Original file line number Diff line number Diff line change
Expand Up @@ -246,10 +246,10 @@ func (r *Runtime) bigint_asIntN(call FunctionCall) Value {
}
bigint := toBigInt(call.Argument(1))

lsh := big.NewInt(2).Lsh(big.NewInt(1), uint(bits))
mod := new(big.Int).Mod(bigint.i, lsh)
if bits > 0 && mod.Cmp(big.NewInt(2).Lsh(big.NewInt(1), uint(bits-1))) >= 0 {
return valueBigInt{mod.Sub(mod, lsh)}
twoToBits := new(big.Int).Lsh(big.NewInt(1), uint(bits))
mod := new(big.Int).Mod(bigint.i, twoToBits)
if bits > 0 && mod.Cmp(new(big.Int).Lsh(big.NewInt(1), uint(bits-1))) >= 0 {
return valueBigInt{mod.Sub(mod, twoToBits)}
} else {
return valueBigInt{mod}
}
Expand All @@ -264,7 +264,7 @@ func (r *Runtime) bigint_asUintN(call FunctionCall) Value {
panic(r.NewTypeError("Invalid value: not (convertible to) a safe integer"))
}
bigint := toBigInt(call.Argument(1))
return valueBigInt{new(big.Int).Mod(bigint.i, big.NewInt(2).Lsh(big.NewInt(1), uint(bits)))}
return valueBigInt{new(big.Int).Mod(bigint.i, new(big.Int).Lsh(big.NewInt(1), uint(bits)))}
}

var bigintTemplate *objectTemplate
Expand Down
4 changes: 2 additions & 2 deletions builtin_typedarrays.go
Original file line number Diff line number Diff line change
Expand Up @@ -396,7 +396,7 @@ func (r *Runtime) dataViewProto_setUint32(call FunctionCall) Value {
func (r *Runtime) dataViewProto_setBigInt64(call FunctionCall) Value {
if dv, ok := r.toObject(call.This).self.(*dataViewObject); ok {
idxVal := r.toIndex(call.Argument(0))
val := toBigInt(call.Argument(1)).i
val := toBigInt64(call.Argument(1))
idx, bo := dv.getIdxAndByteOrder(idxVal, call.Argument(2), 8)
dv.viewedArrayBuf.setBigInt64(idx, val, bo)
return _undefined
Expand All @@ -407,7 +407,7 @@ func (r *Runtime) dataViewProto_setBigInt64(call FunctionCall) Value {
func (r *Runtime) dataViewProto_setBigUint64(call FunctionCall) Value {
if dv, ok := r.toObject(call.This).self.(*dataViewObject); ok {
idxVal := r.toIndex(call.Argument(0))
val := toBigInt(call.Argument(1)).i
val := toBigUint64(call.Argument(1))
idx, bo := dv.getIdxAndByteOrder(idxVal, call.Argument(2), 8)
dv.viewedArrayBuf.setBigUint64(idx, val, bo)
return _undefined
Expand Down
12 changes: 8 additions & 4 deletions typedarrays.go
Original file line number Diff line number Diff line change
Expand Up @@ -648,9 +648,13 @@ func (a *bigInt64Array) get(idx int) Value {

func toBigInt64(v Value) *big.Int {
n := toBigInt(v).i
int64bit := new(big.Int).Mod(n, big.NewInt(2).Exp(big.NewInt(2), big.NewInt(64), nil))
if int64bit.Cmp(big.NewInt(2).Exp(big.NewInt(2), big.NewInt(63), nil)) >= 0 {
return int64bit.Sub(int64bit, big.NewInt(2).Exp(big.NewInt(2), big.NewInt(64), nil))

twoTo64 := new(big.Int).Lsh(big.NewInt(1), 64)
twoTo63 := new(big.Int).Lsh(big.NewInt(1), 63)

int64bit := new(big.Int).Mod(n, twoTo64)
if int64bit.Cmp(twoTo63) >= 0 {
return int64bit.Sub(int64bit, twoTo64)
}
return int64bit
}
Expand Down Expand Up @@ -717,7 +721,7 @@ func (a *bigUint64Array) get(idx int) Value {

func toBigUint64(v Value) *big.Int {
n := toBigInt(v).i
return new(big.Int).Mod(n, big.NewInt(2).Exp(big.NewInt(2), big.NewInt(64), nil))
return new(big.Int).Mod(n, new(big.Int).Lsh(big.NewInt(1), 64))
}

func (a *bigUint64Array) set(idx int, value Value) {
Expand Down
6 changes: 3 additions & 3 deletions vm.go
Original file line number Diff line number Diff line change
Expand Up @@ -1542,7 +1542,7 @@ func (_mod) exec(vm *vm) {
case right.i.Cmp(big.NewInt(0)) == 0:
panic(vm.r.newError(vm.r.getRangeError(), "Division by zero"))
case left.i.Cmp(big.NewInt(0)) < 0:
abs := left.i.Abs(left.i)
abs := new(big.Int).Abs(left.i)
v := new(big.Int).Mod(abs, right.i)
result = valueBigInt{v.Neg(v)}
default:
Expand Down Expand Up @@ -4463,7 +4463,7 @@ func cmp(px, py Value) Value {
nx, _ := nx.Int(nil)
ret = nx.Cmp(ny.i) < 0
} else {
ret = nx.Cmp(big.NewFloat(0).SetInt(ny.i)) < 0
ret = nx.Cmp(new(big.Float).SetInt(ny.i)) < 0
}
goto end
}
Expand All @@ -4484,7 +4484,7 @@ func cmp(px, py Value) Value {
ny, _ := ny.Int(nil)
ret = nx.i.Cmp(ny) < 0
} else {
ret = big.NewFloat(0).SetInt(nx.i).Cmp(ny) < 0
ret = new(big.Float).SetInt(nx.i).Cmp(ny) < 0
}
goto end
case valueBigInt:
Expand Down

0 comments on commit 53edc48

Please sign in to comment.