github.com/chenzhuoyu/iasm@v0.9.1/expr/ops.go (about) 1 package expr 2 3 import ( 4 `fmt` 5 ) 6 7 func idiv(v int64, d int64) (int64, error) { 8 if d != 0 { 9 return v / d, nil 10 } else { 11 return 0, newRuntimeError("division by zero") 12 } 13 } 14 15 func imod(v int64, d int64) (int64, error) { 16 if d != 0 { 17 return v % d, nil 18 } else { 19 return 0, newRuntimeError("division by zero") 20 } 21 } 22 23 func ipow(v int64, e int64) (int64, error) { 24 mul := v 25 ret := int64(1) 26 27 /* value must be 0 or positive */ 28 if v < 0 { 29 return 0, newRuntimeError(fmt.Sprintf("negative base value: %d", v)) 30 } 31 32 /* exponent must be non-negative */ 33 if e < 0 { 34 return 0, newRuntimeError(fmt.Sprintf("negative exponent: %d", e)) 35 } 36 37 /* fast power first round */ 38 if (e & 1) != 0 { 39 ret *= mul 40 } 41 42 /* fast power remaining rounds */ 43 for e >>= 1; e != 0; e >>= 1 { 44 if mul *= mul; (e & 1) != 0 { 45 ret *= mul 46 } 47 } 48 49 /* all done */ 50 return ret, nil 51 }