diff options
author | Meng-Ying Yang <garfield@dexon.org> | 2019-04-16 17:04:50 +0800 |
---|---|---|
committer | Meng-Ying Yang <garfield@dexon.org> | 2019-05-06 15:50:03 +0800 |
commit | fa9da43982e46a39e9e140bdb85fee53aa09b18d (patch) | |
tree | 3641149411f4f427f41a714c093c939b03496ed5 | |
parent | 01e130a74e957a2415edbcd99612c6c21b1f64ad (diff) | |
download | dexon-fa9da43982e46a39e9e140bdb85fee53aa09b18d.tar.gz dexon-fa9da43982e46a39e9e140bdb85fee53aa09b18d.tar.zst dexon-fa9da43982e46a39e9e140bdb85fee53aa09b18d.zip |
core: vm: sqlvm: add built-in function BITOR()
-rw-r--r-- | core/vm/sqlvm/runtime/functions.go | 20 | ||||
-rw-r--r-- | core/vm/sqlvm/runtime/instructions_op_test.go | 62 | ||||
-rw-r--r-- | core/vm/sqlvm/runtime/instructions_tmpl_data.go | 62 |
3 files changed, 144 insertions, 0 deletions
diff --git a/core/vm/sqlvm/runtime/functions.go b/core/vm/sqlvm/runtime/functions.go index 6cdceb7fb..26e2d0d91 100644 --- a/core/vm/sqlvm/runtime/functions.go +++ b/core/vm/sqlvm/runtime/functions.go @@ -27,6 +27,7 @@ const ( NOW RAND BITAND + BITOR ) type fn func(*common.Context, []*Operand, uint64) (*Operand, error) @@ -44,6 +45,7 @@ var ( TXORIGIN: fnTxOrigin, RAND: fnRand, BITAND: fnBitAnd, + BITOR: fnBitOr, } ) @@ -316,3 +318,21 @@ func (r *Raw) bitBinOp(r2 *Raw, dType ast.DataType, bFn bitBinFunc) (r3 *Raw) { r3.fromBytes(bytes3, dType) return } + +func fnBitOr(ctx *common.Context, ops []*Operand, length uint64) (result *Operand, err error) { + n, op1, op2, err := extractOps(ops) + if err != nil { + return + } + + result = op1.clone(true) + result.Data = make([]Tuple, n) + for i := 0; i < n; i++ { + result.Data[i] = op1.Data[i].bitBinOp( + op2.Data[i], + op1.Meta, + func(b1, b2 byte) byte { return b1 | b2 }, + ) + } + return +} diff --git a/core/vm/sqlvm/runtime/instructions_op_test.go b/core/vm/sqlvm/runtime/instructions_op_test.go index b2fec064e..14c8d853d 100644 --- a/core/vm/sqlvm/runtime/instructions_op_test.go +++ b/core/vm/sqlvm/runtime/instructions_op_test.go @@ -3755,3 +3755,65 @@ func (s *instructionSuite) TestOpFuncBitAnd() { s.run(testcases, opFunc) } + +func (s *instructionSuite) TestOpFuncBitOr() { + testcases := []opTestcase{ + { + "Func BitOr", + Instruction{ + Op: FUNC, + Input: []*Operand{ + makeOperand( + true, + []ast.DataType{ + ast.ComposeDataType(ast.DataTypeMajorUint, 0), + }, + []Tuple{ + {&Raw{Value: decimal.NewFromFloat(2)}}, + }, + ), + makeOperand( + true, + []ast.DataType{ + ast.ComposeDataType(ast.DataTypeMajorUint, 1), + }, + []Tuple{ + {&Raw{Value: decimal.NewFromFloat(11)}}, + }, + ), + makeOperand( + false, + []ast.DataType{ + ast.ComposeDataType(ast.DataTypeMajorUint, 0), ast.ComposeDataType(ast.DataTypeMajorUint, 0), ast.ComposeDataType(ast.DataTypeMajorInt, 0), ast.ComposeDataType(ast.DataTypeMajorInt, 0), ast.ComposeDataType(ast.DataTypeMajorFixedBytes, 0), ast.ComposeDataType(ast.DataTypeMajorFixedBytes, 0), + }, + []Tuple{ + {&Raw{Value: decimal.NewFromFloat(1)}, &Raw{Value: decimal.NewFromFloat(2)}, &Raw{Value: decimal.NewFromFloat(-1)}, &Raw{Value: decimal.NewFromFloat(-128)}, &Raw{Bytes: []byte{0x12, 0x34}}, &Raw{Bytes: []byte{0x56, 0x78}}}, + }, + ), + makeOperand( + false, + []ast.DataType{ + ast.ComposeDataType(ast.DataTypeMajorUint, 0), ast.ComposeDataType(ast.DataTypeMajorUint, 0), ast.ComposeDataType(ast.DataTypeMajorInt, 0), ast.ComposeDataType(ast.DataTypeMajorInt, 0), ast.ComposeDataType(ast.DataTypeMajorFixedBytes, 0), ast.ComposeDataType(ast.DataTypeMajorFixedBytes, 0), + }, + []Tuple{ + {&Raw{Value: decimal.NewFromFloat(5)}, &Raw{Value: decimal.NewFromFloat(6)}, &Raw{Value: decimal.NewFromFloat(-2)}, &Raw{Value: decimal.NewFromFloat(-2)}, &Raw{Bytes: []byte{0xff, 0xff}}, &Raw{Bytes: []byte{0x00, 0x00}}}, + }, + ), + }, + Output: 0, + }, + makeOperand( + false, + []ast.DataType{ + ast.ComposeDataType(ast.DataTypeMajorUint, 0), ast.ComposeDataType(ast.DataTypeMajorUint, 0), ast.ComposeDataType(ast.DataTypeMajorInt, 0), ast.ComposeDataType(ast.DataTypeMajorInt, 0), ast.ComposeDataType(ast.DataTypeMajorFixedBytes, 0), ast.ComposeDataType(ast.DataTypeMajorFixedBytes, 0), + }, + []Tuple{ + {&Raw{Value: decimal.NewFromFloat(5)}, &Raw{Value: decimal.NewFromFloat(6)}, &Raw{Value: decimal.NewFromFloat(-1)}, &Raw{Value: decimal.NewFromFloat(-2)}, &Raw{Bytes: []byte{0xff, 0xff}}, &Raw{Bytes: []byte{0x56, 0x78}}}, + }, + ), + nil, + }, + } + + s.run(testcases, opFunc) +} diff --git a/core/vm/sqlvm/runtime/instructions_tmpl_data.go b/core/vm/sqlvm/runtime/instructions_tmpl_data.go index c7a718cbf..3d92346f9 100644 --- a/core/vm/sqlvm/runtime/instructions_tmpl_data.go +++ b/core/vm/sqlvm/runtime/instructions_tmpl_data.go @@ -2660,5 +2660,67 @@ var testData = &tmplData{ }, }, // -- end of FUNC BITAND + { + TestName: "OpFuncBitOr", OpFunc: "opFunc", + Cases: []*tmplTestCase{ + { + Name: "Func BitOr", + Error: "nil", OpCode: "FUNC", + Inputs: []*tmplOp{ + { + Im: true, + Metas: []*tmplOpMeta{ + {Major: "Uint", Minor: 0}, + }, + Data: []string{`{V: 2}`}, + }, + { + Im: true, + Metas: []*tmplOpMeta{ + {Major: "Uint", Minor: 1}, + }, + Data: []string{`{V: 11}`}, + }, + { + Im: false, + Metas: []*tmplOpMeta{ + {Major: "Uint", Minor: 0}, + {Major: "Uint", Minor: 0}, + {Major: "Int", Minor: 0}, + {Major: "Int", Minor: 0}, + {Major: "FixedBytes", Minor: 0}, + {Major: "FixedBytes", Minor: 0}, + }, + Data: []string{"{V: 1, V: 2, V: -1, V: -128, B: {0x12, 0x34}, B: {0x56, 0x78}}"}, + }, + { + Im: false, + Metas: []*tmplOpMeta{ + {Major: "Uint", Minor: 0}, + {Major: "Uint", Minor: 0}, + {Major: "Int", Minor: 0}, + {Major: "Int", Minor: 0}, + {Major: "FixedBytes", Minor: 0}, + {Major: "FixedBytes", Minor: 0}, + }, + Data: []string{"{V: 5, V: 6, V: -2, V: -2, B: {0xff, 0xff}, B:{0x00, 0x00}}"}, + }, + }, + Output: &tmplOp{ + Im: false, + Metas: []*tmplOpMeta{ + {Major: "Uint", Minor: 0}, + {Major: "Uint", Minor: 0}, + {Major: "Int", Minor: 0}, + {Major: "Int", Minor: 0}, + {Major: "FixedBytes", Minor: 0}, + {Major: "FixedBytes", Minor: 0}, + }, + Data: []string{`{V: 5, V: 6, V: -1, V: -2, B: {0xff, 0xff}, B: {0x56, 0x78}}`}, + }, + }, + }, + }, + // -- end of FUNC BITOR }, } |