From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from [87.239.111.99] (localhost [127.0.0.1]) by dev.tarantool.org (Postfix) with ESMTP id E33636BD34; Tue, 23 Mar 2021 12:44:48 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org E33636BD34 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=tarantool.org; s=dev; t=1616492688; bh=pAoOTqwSUYQJZL5iKcEve6rUVOxcr5T9uI1opATc8JA=; h=To:Cc:Date:In-Reply-To:References:Subject:List-Id: List-Unsubscribe:List-Archive:List-Post:List-Help:List-Subscribe: From:Reply-To:From; b=y8CpwFum+tQ9iWdKDXUxkNhukdUlsNrgHaQC4S9kyu6ifaCYgP2cVlXscRbOiPwV9 c+I36ZdxF+Yu87+NO+RGkKrZ1qnVVxIrtkDXn5sjTWjVU+qLgD6V9SM/t5xRMDg759 uDvlxjVYcfA00r//ZB2oNMQ7UySJK7+3DGIkZIlI= Received: from smtp32.i.mail.ru (smtp32.i.mail.ru [94.100.177.92]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dev.tarantool.org (Postfix) with ESMTPS id 131B868F49 for ; Tue, 23 Mar 2021 12:35:38 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org 131B868F49 Received: by smtp32.i.mail.ru with esmtpa (envelope-from ) id 1lOdRp-0000bo-AF; Tue, 23 Mar 2021 12:35:37 +0300 To: v.shpilevoy@tarantool.org, tsafin@tarantool.org Cc: tarantool-patches@dev.tarantool.org Date: Tue, 23 Mar 2021 12:35:37 +0300 Message-Id: <70ffab024b9c7066d833028ee4bd2b8e6db14c6c.1616491731.git.imeevma@gmail.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-7564579A: B8F34718100C35BD X-77F55803: 4F1203BC0FB41BD95D6E7CC48CB1F5F10D3016C09B407F8B88411E9FEB481E8E182A05F53808504040E5BC5014A78F6FAF319749044C1B6C67435FABEE73E098B6BF1A3C03F0F3BA X-7FA49CB5: FF5795518A3D127A4AD6D5ED66289B5278DA827A17800CE79E25BE5045FD62C0EA1F7E6F0F101C67BD4B6F7A4D31EC0BCC500DACC3FED6E28638F802B75D45FF8AA50765F7900637B0052F7941B60F658638F802B75D45FF914D58D5BE9E6BC131B5C99E7648C95C686AF563A045C75EFF5A96CBF9A58840F68DF3ACE6A042E3A471835C12D1D9774AD6D5ED66289B5278DA827A17800CE73AFA331E307B52169FA2833FD35BB23D2EF20D2F80756B5F868A13BD56FB6657A471835C12D1D977725E5C173C3A84C311471C67CF6D96D5117882F4460429728AD0CFFFB425014E868A13BD56FB6657D81D268191BDAD3DC09775C1D3CA48CFE9B4B92903337A35BA3038C0950A5D36C8A9BA7A39EFB766EC990983EF5C0329BA3038C0950A5D36D5E8D9A59859A8B613302629575AB2B876E601842F6C81A1F004C906525384307823802FF610243DF43C7A68FF6260569E8FC8737B5C2249EC8D19AE6D49635B3BBE47FD9DD3FB59A8DF7F3B2552694A57739F23D657EF2B13377AFFFEAFD26923F8577A6DFFEA7CDDB9BF3B882869D593EC92FD9297F6715571747095F342E857739F23D657EF2BD5E8D9A59859A8B6B67FC62909E22F84089D37D7C0E48F6C5571747095F342E857739F23D657EF2B6825BDBE14D8E7024847893F9AA87235E5BFE6E7EFDEDCD789D4C264860C145E X-C1DE0DAB: C20DE7B7AB408E4181F030C43753B8186998911F362727C414F749A5E30D975C686AF563A045C75EFF5A96CBF9A58840F68DF3ACE6A042E39C2B6934AE262D3EE7EAB7254005DCED7532B743992DF240BDC6A1CF3F042BAD6DF99611D93F60EF0417BEADF48D1460699F904B3F4130E343918A1A30D5E7FCCB5012B2E24CD356 X-C8649E89: 4E36BF7865823D7055A7F0CF078B5EC49A30900B95165D345CB6DE26F1654667730C7AFF057538AFF22D2FFA6DE2B823046D3765F759112D8AD724B9F8A7E5681D7E09C32AA3244C30ABFDF4A1F08D930CBD8E6E02C7EA2435DA7DC5AF9B58C0FACE5A9C96DEB163 X-D57D3AED: 3ZO7eAau8CL7WIMRKs4sN3D3tLDjz0dLbV79QFUyzQ2Ujvy7cMT6pYYqY16iZVKkSc3dCLJ7zSJH7+u4VD18S7Vl4ZUrpaVfd2+vE6kuoey4m4VkSEu530nj6fImhcD4MUrOEAnl0W826KZ9Q+tr5ycPtXkTV4k65bRjmOUUP8cvGozZ33TWg5HZplvhhXbhDGzqmQDTd6OAevLeAnq3Ra9uf7zvY2zzsIhlcp/Y7m53TZgf2aB4JOg4gkr2biojWJZv20R+6Ug6NcJ5kXBEXg== X-Mailru-Sender: 5C3750E245F362008BC1685FEC6306EDDBDC7B48590FFFCEAF319749044C1B6C6565FCDE7E403FA15105BD0848736F9966FEC6BF5C9C28D97E07721503EA2E00ED97202A5A4E92BF7402F9BA4338D657ED14614B50AE0675 X-Mras: Ok Subject: [Tarantool-patches] [PATCH v4 20/53] sql: introduce mem_bitwise() X-BeenThere: tarantool-patches@dev.tarantool.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: Tarantool development patches List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , From: Mergen Imeev via Tarantool-patches Reply-To: imeevma@tarantool.org Errors-To: tarantool-patches-bounces@dev.tarantool.org Sender: "Tarantool-patches" 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