[Tarantool-patches] [PATCH v4 20/53] sql: introduce mem_bitwise()

imeevma at tarantool.org imeevma at tarantool.org
Tue Mar 23 12:35:37 MSK 2021


This patch introduces mem_bitwise(). Function mem_bitwise() executes
bitwise operations with two operands and writes the result to the third
MEM.

Part of #5818
---
 src/box/sql/mem.c  | 51 +++++++++++++++++++++++++++++++++++++++++++
 src/box/sql/mem.h  |  3 +++
 src/box/sql/vdbe.c | 54 +++-------------------------------------------
 3 files changed, 57 insertions(+), 51 deletions(-)

diff --git a/src/box/sql/mem.c b/src/box/sql/mem.c
index 8119644ed..2b455e39f 100644
--- a/src/box/sql/mem.c
+++ b/src/box/sql/mem.c
@@ -556,6 +556,57 @@ mem_arithmetic(const struct Mem *left, const struct Mem *right,
 	return 0;
 }
 
+int
+mem_bitwise(struct Mem *left, struct Mem *right, struct Mem *result, int op)
+{
+	sqlVdbeMemSetNull(result);
+	result->field_type = FIELD_TYPE_INTEGER;
+	if (((left->flags | right->flags) & MEM_Null) != 0)
+		return 0;
+	int64_t l;
+	int64_t r;
+	bool unused;
+	if (sqlVdbeIntValue(left, &l, &unused) != 0) {
+		diag_set(ClientError, ER_SQL_TYPE_MISMATCH,
+			 mem_str(left), "integer");
+		return -1;
+	}
+	if (sqlVdbeIntValue(right, &r, &unused) != 0) {
+		diag_set(ClientError, ER_SQL_TYPE_MISMATCH,
+			 mem_str(right), "integer");
+		return -1;
+	}
+	if (op == OP_BitAnd) {
+		result->u.i = l & r;
+		result->flags = result->u.i < 0 ? MEM_Int : MEM_UInt;
+		return 0;
+	}
+	if (op == OP_BitOr) {
+		result->u.i = l | r;
+		result->flags = result->u.i < 0 ? MEM_Int : MEM_UInt;
+		return 0;
+	}
+	assert(op == OP_ShiftRight || op == OP_ShiftLeft);
+	bool is_left_shift = op == OP_ShiftLeft;
+	if (r < 0) {
+		is_left_shift = !is_left_shift;
+		r = r < -64 ? 64 : -r;
+	}
+	if (r >= 64) {
+		result->u.i = is_left_shift || l >= 0 ? 0 : -1;
+		result->flags = result->u.i < 0 ? MEM_Int : MEM_UInt;
+		return 0;
+	}
+	if (is_left_shift) {
+		result->u.i = l << r;
+		result->flags = result->u.i < 0 ? MEM_Int : MEM_UInt;
+		return 0;
+	}
+	result->u.i = l >> r;
+	result->flags = result->u.i < 0 ? MEM_Int : MEM_UInt;
+	return 0;
+}
+
 static int
 compare_blobs(const struct Mem *left, const struct Mem *right, int *result)
 {
diff --git a/src/box/sql/mem.h b/src/box/sql/mem.h
index 096ce6533..8971e3d59 100644
--- a/src/box/sql/mem.h
+++ b/src/box/sql/mem.h
@@ -196,6 +196,9 @@ int
 mem_arithmetic(const struct Mem *left, const struct Mem *right,
 	       struct Mem *result, int op);
 
+int
+mem_bitwise(struct Mem *left, struct Mem *right, struct Mem *result, int op);
+
 int
 mem_compare(const struct Mem *left, const struct Mem *right, int *result,
 	    enum field_type type, struct coll *coll);
diff --git a/src/box/sql/vdbe.c b/src/box/sql/vdbe.c
index 726066c17..cdcf7b2e8 100644
--- a/src/box/sql/vdbe.c
+++ b/src/box/sql/vdbe.c
@@ -1304,60 +1304,12 @@ case OP_BitAnd:                 /* same as TK_BITAND, in1, in2, out3 */
 case OP_BitOr:                  /* same as TK_BITOR, in1, in2, out3 */
 case OP_ShiftLeft:              /* same as TK_LSHIFT, in1, in2, out3 */
 case OP_ShiftRight: {           /* same as TK_RSHIFT, in1, in2, out3 */
-	i64 iA;
-	u64 uA;
-	i64 iB;
-	u8 op;
-
 	pIn1 = &aMem[pOp->p1];
 	pIn2 = &aMem[pOp->p2];
-	pOut = vdbe_prepare_null_out(p, pOp->p3);
-	if (mem_is_null(pIn1) || mem_is_null(pIn2)) {
-		/* Force NULL be of type INTEGER. */
-		pOut->field_type = FIELD_TYPE_INTEGER;
-		break;
-	}
-	bool unused;
-	if (sqlVdbeIntValue(pIn2, (int64_t *) &iA, &unused) != 0) {
-		diag_set(ClientError, ER_SQL_TYPE_MISMATCH,
-			 mem_str(pIn2), "integer");
-		goto abort_due_to_error;
-	}
-	if (sqlVdbeIntValue(pIn1, (int64_t *) &iB, &unused) != 0) {
-		diag_set(ClientError, ER_SQL_TYPE_MISMATCH,
-			 mem_str(pIn1), "integer");
+	pOut = &aMem[pOp->p3];
+	if (mem_bitwise(pIn2, pIn1, pOut, pOp->opcode) != 0)
 		goto abort_due_to_error;
-	}
-	op = pOp->opcode;
-	if (op==OP_BitAnd) {
-		iA &= iB;
-	} else if (op==OP_BitOr) {
-		iA |= iB;
-	} else if (iB!=0) {
-		assert(op==OP_ShiftRight || op==OP_ShiftLeft);
-
-		/* If shifting by a negative amount, shift in the other direction */
-		if (iB<0) {
-			assert(OP_ShiftRight==OP_ShiftLeft+1);
-			op = 2*OP_ShiftLeft + 1 - op;
-			iB = iB>(-64) ? -iB : 64;
-		}
-
-		if (iB>=64) {
-			iA = (iA>=0 || op==OP_ShiftLeft) ? 0 : -1;
-		} else {
-			memcpy(&uA, &iA, sizeof(uA));
-			if (op==OP_ShiftLeft) {
-				uA <<= iB;
-			} else {
-				uA >>= iB;
-				/* Sign-extend on a right shift of a negative number */
-				if (iA<0) uA |= ((((u64)0xffffffff)<<32)|0xffffffff) << (64-iB);
-			}
-			memcpy(&iA, &uA, sizeof(iA));
-		}
-	}
-	mem_set_i64(pOut, iA);
+	assert(pOut->field_type == FIELD_TYPE_INTEGER);
 	break;
 }
 
-- 
2.25.1



More information about the Tarantool-patches mailing list