github.com/ontio/ontology@v1.14.4/vm/neovm/common.go (about) 1 /* 2 * Copyright (C) 2018 The ontology Authors 3 * This file is part of The ontology library. 4 * 5 * The ontology is free software: you can redistribute it and/or modify 6 * it under the terms of the GNU Lesser General Public License as published by 7 * the Free Software Foundation, either version 3 of the License, or 8 * (at your option) any later version. 9 * 10 * The ontology is distributed in the hope that it will be useful, 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 * GNU Lesser General Public License for more details. 14 * 15 * You should have received a copy of the GNU Lesser General Public License 16 * along with The ontology. If not, see <http://www.gnu.org/licenses/>. 17 */ 18 19 package neovm 20 21 import ( 22 "math/big" 23 ) 24 25 func ToBigInt(data interface{}) *big.Int { 26 var bi big.Int 27 switch t := data.(type) { 28 case int64: 29 bi.SetInt64(int64(t)) 30 case int32: 31 bi.SetInt64(int64(t)) 32 case int16: 33 bi.SetInt64(int64(t)) 34 case int8: 35 bi.SetInt64(int64(t)) 36 case int: 37 bi.SetInt64(int64(t)) 38 case uint64: 39 bi.SetUint64(uint64(t)) 40 case uint32: 41 bi.SetUint64(uint64(t)) 42 case uint16: 43 bi.SetUint64(uint64(t)) 44 case uint8: 45 bi.SetUint64(uint64(t)) 46 case uint: 47 bi.SetUint64(uint64(t)) 48 case big.Int: 49 bi = t 50 case *big.Int: 51 bi = *t 52 } 53 return &bi 54 } 55 56 func BigIntZip(ints1 *big.Int, ints2 *big.Int, op OpCode) *big.Int { 57 nb := new(big.Int) 58 switch op { 59 case AND: 60 nb.And(ints1, ints2) 61 case OR: 62 nb.Or(ints1, ints2) 63 case XOR: 64 nb.Xor(ints1, ints2) 65 case ADD: 66 nb.Add(ints1, ints2) 67 case SUB: 68 nb.Sub(ints1, ints2) 69 case MUL: 70 nb.Mul(ints1, ints2) 71 case DIV: 72 nb.Quo(ints1, ints2) 73 case MOD: 74 nb.Rem(ints1, ints2) 75 case SHL: 76 nb.Lsh(ints1, uint(ints2.Int64())) 77 case SHR: 78 nb.Rsh(ints1, uint(ints2.Int64())) 79 case MIN: 80 c := ints1.Cmp(ints2) 81 if c <= 0 { 82 nb.Set(ints1) 83 } else { 84 nb.Set(ints2) 85 } 86 case MAX: 87 c := ints1.Cmp(ints2) 88 if c <= 0 { 89 nb.Set(ints2) 90 } else { 91 nb.Set(ints1) 92 } 93 } 94 return nb 95 }