6 "github.com/bytom/bytom/math/checked"
9 func op1Add(vm *virtualMachine) error {
10 err := vm.applyCost(2)
15 n, err := vm.popBigInt(true)
24 num, ok := checked.NewUInt256("1")
29 if num.Add(n, num); num.Sign() < 0 {
33 return vm.pushBigInt(num, true)
36 func op1Sub(vm *virtualMachine) error {
37 err := vm.applyCost(2)
41 n, err := vm.popInt64(true)
45 res, ok := checked.SubInt64(n, 1)
49 return vm.pushInt64(res, true)
52 func op2Mul(vm *virtualMachine) error {
53 err := vm.applyCost(2)
57 n, err := vm.popInt64(true)
61 res, ok := checked.MulInt64(n, 2)
65 return vm.pushInt64(res, true)
68 func op2Div(vm *virtualMachine) error {
69 err := vm.applyCost(2)
73 n, err := vm.popInt64(true)
77 return vm.pushInt64(n>>1, true)
80 func opNegate(vm *virtualMachine) error {
81 err := vm.applyCost(2)
85 n, err := vm.popInt64(true)
89 res, ok := checked.NegateInt64(n)
93 return vm.pushInt64(res, true)
96 func opAbs(vm *virtualMachine) error {
97 err := vm.applyCost(2)
101 n, err := vm.popInt64(true)
105 if n == math.MinInt64 {
111 return vm.pushInt64(n, true)
114 func opNot(vm *virtualMachine) error {
115 err := vm.applyCost(2)
119 n, err := vm.popInt64(true)
123 return vm.pushBool(n == 0, true)
126 func op0NotEqual(vm *virtualMachine) error {
127 err := vm.applyCost(2)
131 n, err := vm.popInt64(true)
135 return vm.pushBool(n != 0, true)
138 func opAdd(vm *virtualMachine) error {
139 err := vm.applyCost(2)
143 y, err := vm.popInt64(true)
147 x, err := vm.popInt64(true)
151 res, ok := checked.AddInt64(x, y)
155 return vm.pushInt64(res, true)
158 func opSub(vm *virtualMachine) error {
159 err := vm.applyCost(2)
163 y, err := vm.popInt64(true)
167 x, err := vm.popInt64(true)
171 res, ok := checked.SubInt64(x, y)
175 return vm.pushInt64(res, true)
178 func opMul(vm *virtualMachine) error {
179 err := vm.applyCost(8)
183 y, err := vm.popInt64(true)
187 x, err := vm.popInt64(true)
191 res, ok := checked.MulInt64(x, y)
195 return vm.pushInt64(res, true)
198 func opDiv(vm *virtualMachine) error {
199 err := vm.applyCost(8)
203 y, err := vm.popInt64(true)
207 x, err := vm.popInt64(true)
214 res, ok := checked.DivInt64(x, y)
218 return vm.pushInt64(res, true)
221 func opMod(vm *virtualMachine) error {
222 err := vm.applyCost(8)
226 y, err := vm.popInt64(true)
230 x, err := vm.popInt64(true)
238 res, ok := checked.ModInt64(x, y)
243 // Go's modulus operator produces the wrong result for mixed-sign
245 if res != 0 && (x >= 0) != (y >= 0) {
249 return vm.pushInt64(res, true)
252 func opLshift(vm *virtualMachine) error {
253 err := vm.applyCost(8)
257 y, err := vm.popInt64(true)
264 x, err := vm.popInt64(true)
268 if x == 0 || y == 0 {
269 return vm.pushInt64(x, true)
272 res, ok := checked.LshiftInt64(x, y)
277 return vm.pushInt64(res, true)
280 func opRshift(vm *virtualMachine) error {
281 err := vm.applyCost(8)
285 y, err := vm.popInt64(true)
289 x, err := vm.popInt64(true)
296 return vm.pushInt64(x>>uint64(y), true)
299 func opBoolAnd(vm *virtualMachine) error {
300 err := vm.applyCost(2)
304 b, err := vm.pop(true)
308 a, err := vm.pop(true)
312 return vm.pushBool(AsBool(a) && AsBool(b), true)
315 func opBoolOr(vm *virtualMachine) error {
316 err := vm.applyCost(2)
320 b, err := vm.pop(true)
324 a, err := vm.pop(true)
328 return vm.pushBool(AsBool(a) || AsBool(b), true)
340 func opNumEqual(vm *virtualMachine) error {
341 return doNumCompare(vm, cmpEqual)
344 func opNumEqualVerify(vm *virtualMachine) error {
345 err := vm.applyCost(2)
349 y, err := vm.popInt64(true)
353 x, err := vm.popInt64(true)
360 return ErrVerifyFailed
363 func opNumNotEqual(vm *virtualMachine) error {
364 return doNumCompare(vm, cmpNotEqual)
367 func opLessThan(vm *virtualMachine) error {
368 return doNumCompare(vm, cmpLess)
371 func opGreaterThan(vm *virtualMachine) error {
372 return doNumCompare(vm, cmpGreater)
375 func opLessThanOrEqual(vm *virtualMachine) error {
376 return doNumCompare(vm, cmpLessEqual)
379 func opGreaterThanOrEqual(vm *virtualMachine) error {
380 return doNumCompare(vm, cmpGreaterEqual)
383 func doNumCompare(vm *virtualMachine, op int) error {
384 err := vm.applyCost(2)
388 y, err := vm.popInt64(true)
392 x, err := vm.popInt64(true)
404 case cmpGreaterEqual:
411 return vm.pushBool(res, true)
414 func opMin(vm *virtualMachine) error {
415 err := vm.applyCost(2)
419 y, err := vm.popInt64(true)
423 x, err := vm.popInt64(true)
430 return vm.pushInt64(x, true)
433 func opMax(vm *virtualMachine) error {
434 err := vm.applyCost(2)
438 y, err := vm.popInt64(true)
442 x, err := vm.popInt64(true)
449 return vm.pushInt64(x, true)
452 func opWithin(vm *virtualMachine) error {
453 err := vm.applyCost(4)
457 max, err := vm.popInt64(true)
461 min, err := vm.popInt64(true)
465 x, err := vm.popInt64(true)
469 return vm.pushBool(x >= min && x < max, true)