From nobody Mon May 20 16:32:56 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; spf=pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=linux.intel.com ARC-Seal: i=1; a=rsa-sha256; t=1560136341; cv=none; d=zoho.com; s=zohoarc; b=G0uE4vyGCj7JEbce/D0GiVt8IhxstdXwqCKxUHCqHoWXbVMeAczv39m9IBEH5sBr9rTsDLxg8a94ylLEHQRSNImLV9SlZE/VPhIVI98yL5WH2CfA6eXZ3FQKzCQN8bGPrhvSxyWPXUsS0ahNRX3OsU2EzYHgYZ0AG6qI9ZeXG5o= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zoho.com; s=zohoarc; t=1560136341; h=Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To:ARC-Authentication-Results; bh=fqVitF6A0oDyot7DwGv0vEwZ+hhZBPY34Nwtwsv7/nA=; b=hrqA+UFVCLubWvXLAFXhegryPP2UBU+i9KaKLkQU8GIaQB7VmNFa6hVBWp8t2fvots61SYw5xu+H6xI/pysozoI+dSqauD945xJKQis40jLPcTgRjhV94aupJwzKU43ERzCogCsAivDuRcJLoQzEWk/2EvZYfPuPJaN4h+mLcxc= ARC-Authentication-Results: i=1; mx.zoho.com; spf=pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail header.from= (p=none dis=none) header.from= Return-Path: Received: from lists.gnu.org (209.51.188.17 [209.51.188.17]) by mx.zohomail.com with SMTPS id 1560136341774484.6061265455744; Sun, 9 Jun 2019 20:12:21 -0700 (PDT) Received: from localhost ([::1]:40156 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.86_2) (envelope-from ) id 1haAim-0006O5-Qd for importer@patchew.org; Sun, 09 Jun 2019 23:11:44 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:39021) by lists.gnu.org with esmtp (Exim 4.86_2) (envelope-from ) id 1haAgu-0004aG-R4 for qemu-devel@nongnu.org; Sun, 09 Jun 2019 23:09:49 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1haAgt-0005sd-VE for qemu-devel@nongnu.org; Sun, 09 Jun 2019 23:09:48 -0400 Received: from mga02.intel.com ([134.134.136.20]:49683) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1haAgt-0005rt-O3 for qemu-devel@nongnu.org; Sun, 09 Jun 2019 23:09:47 -0400 Received: from fmsmga006.fm.intel.com ([10.253.24.20]) by orsmga101.jf.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 09 Jun 2019 20:09:45 -0700 Received: from richard.sh.intel.com (HELO localhost) ([10.239.159.54]) by fmsmga006.fm.intel.com with ESMTP; 09 Jun 2019 20:09:44 -0700 X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False X-ExtLoop1: 1 From: Wei Yang To: qemu-devel@nongnu.org Date: Mon, 10 Jun 2019 11:08:51 +0800 Message-Id: <20190610030852.16039-2-richardw.yang@linux.intel.com> X-Mailer: git-send-email 2.19.1 In-Reply-To: <20190610030852.16039-1-richardw.yang@linux.intel.com> References: <20190610030852.16039-1-richardw.yang@linux.intel.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 134.134.136.20 Subject: [Qemu-devel] [PATCH 1/2] cutils: remove one unnecessary pointer operation X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Wei Yang , dgilbert@redhat.com, quintela@redhat.com Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" Content-Type: text/plain; charset="utf-8" Since we will not operate on the next address pointed by out, it is not necessary to do addition on it. After removing the operation, the function size reduced 16/18 bytes. Signed-off-by: Wei Yang Reviewed-by: Dr. David Alan Gilbert Reviewed-by: Juan Quintela --- util/cutils.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/util/cutils.c b/util/cutils.c index 9aacc422ca..1933a68da5 100644 --- a/util/cutils.c +++ b/util/cutils.c @@ -754,11 +754,11 @@ int uleb128_encode_small(uint8_t *out, uint32_t n) { g_assert(n <=3D 0x3fff); if (n < 0x80) { - *out++ =3D n; + *out =3D n; return 1; } else { *out++ =3D (n & 0x7f) | 0x80; - *out++ =3D n >> 7; + *out =3D n >> 7; return 2; } } @@ -766,7 +766,7 @@ int uleb128_encode_small(uint8_t *out, uint32_t n) int uleb128_decode_small(const uint8_t *in, uint32_t *n) { if (!(*in & 0x80)) { - *n =3D *in++; + *n =3D *in; return 1; } else { *n =3D *in++ & 0x7f; @@ -774,7 +774,7 @@ int uleb128_decode_small(const uint8_t *in, uint32_t *n) if (*in & 0x80) { return -1; } - *n |=3D *in++ << 7; + *n |=3D *in << 7; return 2; } } --=20 2.19.1 From nobody Mon May 20 16:32:56 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; spf=pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=linux.intel.com ARC-Seal: i=1; a=rsa-sha256; t=1560136428; cv=none; d=zoho.com; s=zohoarc; b=Cu/6re2FKl2waPI/oYVgVf/NmJLSosIUzYskaxC7vOwHOrdxhVtabkyxHp4jTs6TQT3AvbKHAx257ZBsx7wxJgzs0KhskLbz3KbUWXPMOyj6nMQDo0/p2vSMraA1PeC0UtDdgX013hMD7O8FjCP02uGwiASXR+cIL2NSvy9s60E= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zoho.com; s=zohoarc; t=1560136428; h=Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To:ARC-Authentication-Results; bh=1d5hMdHCFUu+NIueueXomqbKsSeurpQ4GbhesXsChFk=; b=PyKrXVQjyedOIedarHjo2WqirIPrC0VxzqJ/4GMpg5v8/vn1vgIXe9gbrIf6WnA5fXHxlfj5wE7lYISpNI3ICe6fi6w3XyA+CD5SZX81qP8zsZLftQRHpeierWcVJL+xuAxNPN7Ed2pRIaDPVKb9gbpj6lBpY7UgJOH9bmBPb+k= ARC-Authentication-Results: i=1; mx.zoho.com; spf=pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail header.from= (p=none dis=none) header.from= Return-Path: Received: from lists.gnu.org (209.51.188.17 [209.51.188.17]) by mx.zohomail.com with SMTPS id 1560136428511585.3839663201691; Sun, 9 Jun 2019 20:13:48 -0700 (PDT) Received: from localhost ([::1]:40164 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.86_2) (envelope-from ) id 1haAkO-0007kQ-3Y for importer@patchew.org; Sun, 09 Jun 2019 23:13:24 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:39037) by lists.gnu.org with esmtp (Exim 4.86_2) (envelope-from ) id 1haAgv-0004aJ-Kn for qemu-devel@nongnu.org; Sun, 09 Jun 2019 23:09:50 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1haAgu-0005sr-DJ for qemu-devel@nongnu.org; Sun, 09 Jun 2019 23:09:49 -0400 Received: from mga02.intel.com ([134.134.136.20]:49683) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1haAgu-0005rt-5p for qemu-devel@nongnu.org; Sun, 09 Jun 2019 23:09:48 -0400 Received: from fmsmga006.fm.intel.com ([10.253.24.20]) by orsmga101.jf.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 09 Jun 2019 20:09:47 -0700 Received: from richard.sh.intel.com (HELO localhost) ([10.239.159.54]) by fmsmga006.fm.intel.com with ESMTP; 09 Jun 2019 20:09:46 -0700 X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False X-ExtLoop1: 1 From: Wei Yang To: qemu-devel@nongnu.org Date: Mon, 10 Jun 2019 11:08:52 +0800 Message-Id: <20190610030852.16039-3-richardw.yang@linux.intel.com> X-Mailer: git-send-email 2.19.1 In-Reply-To: <20190610030852.16039-1-richardw.yang@linux.intel.com> References: <20190610030852.16039-1-richardw.yang@linux.intel.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 134.134.136.20 Subject: [Qemu-devel] [PATCH 2/2] migration/xbzrle: make xbzrle_encode_buffer little easier to read X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Wei Yang , dgilbert@redhat.com, quintela@redhat.com Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" Content-Type: text/plain; charset="utf-8" The encoding process could be described below: for [content] get length of a run encode this run By refactoring the code with this logic, it maybe a little easier to understand. Signed-off-by: Wei Yang --- migration/xbzrle.c | 153 +++++++++++++++++++++------------------------ 1 file changed, 73 insertions(+), 80 deletions(-) diff --git a/migration/xbzrle.c b/migration/xbzrle.c index 1ba482ded9..25c69708ec 100644 --- a/migration/xbzrle.c +++ b/migration/xbzrle.c @@ -14,6 +14,59 @@ #include "qemu/cutils.h" #include "xbzrle.h" =20 +static int next_run(uint8_t *old_buf, uint8_t *new_buf, int off, int slen, + bool zrun) +{ + uint32_t len =3D 0; + long res; + + res =3D (slen - off) % sizeof(long); + + /* first unaligned bytes */ + while (res) { + uint8_t xor =3D old_buf[off + len] ^ new_buf[off + len]; + + if (!(zrun ^ !!xor)) { + break; + } + len++; + res--; + } + + if (res) { + return len; + } + + /* word at a time for speed, use of 32-bit long okay */ + while (off + len < slen) { + /* truncation to 32-bit long okay */ + unsigned long mask =3D (unsigned long)0x0101010101010101ULL; + long xor =3D (*(long *)(old_buf + off + len)) ^ + (*(long *)(new_buf + off + len)); + + if (zrun && !(zrun ^ !!xor)) { + break; + } else if (!zrun && ((xor - mask) & ~xor & (mask << 7))) { + break; + } + + len +=3D sizeof(long); + } + + /* go over the rest */ + while (off + len < slen) { + uint8_t xor =3D old_buf[off + len] ^ new_buf[off + len]; + + if (!(zrun ^ !!xor)) { + break; + } + + len++; + } + + return len; +} + /* page =3D zrun nzrun | zrun nzrun page @@ -27,103 +80,43 @@ int xbzrle_encode_buffer(uint8_t *old_buf, uint8_t *new_buf, int slen, uint8_t *dst, int dlen) { - uint32_t zrun_len =3D 0, nzrun_len =3D 0; - int d =3D 0, i =3D 0; - long res; - uint8_t *nzrun_start =3D NULL; + bool zrun =3D true; + int len, src_off =3D 0, dst_off =3D 0; =20 g_assert(!(((uintptr_t)old_buf | (uintptr_t)new_buf | slen) % sizeof(long))); =20 - while (i < slen) { + for (; src_off < slen; src_off +=3D len, zrun =3D !zrun) { /* overflow */ - if (d + 2 > dlen) { + if (dst_off + 2 > dlen) { return -1; } =20 - /* not aligned to sizeof(long) */ - res =3D (slen - i) % sizeof(long); - while (res && old_buf[i] =3D=3D new_buf[i]) { - zrun_len++; - i++; - res--; - } + len =3D next_run(old_buf, new_buf, src_off, slen, zrun); =20 - /* word at a time for speed */ - if (!res) { - while (i < slen && - (*(long *)(old_buf + i)) =3D=3D (*(long *)(new_buf + i)= )) { - i +=3D sizeof(long); - zrun_len +=3D sizeof(long); + if (zrun) { + /* buffer unchanged */ + if (len =3D=3D slen) { + return 0; } - - /* go over the rest */ - while (i < slen && old_buf[i] =3D=3D new_buf[i]) { - zrun_len++; - i++; + /* skip last zero run */ + if (src_off + len =3D=3D slen) { + return dst_off; } } =20 - /* buffer unchanged */ - if (zrun_len =3D=3D slen) { - return 0; - } - - /* skip last zero run */ - if (i =3D=3D slen) { - return d; - } - - d +=3D uleb128_encode_small(dst + d, zrun_len); - - zrun_len =3D 0; - nzrun_start =3D new_buf + i; - - /* overflow */ - if (d + 2 > dlen) { - return -1; - } - /* not aligned to sizeof(long) */ - res =3D (slen - i) % sizeof(long); - while (res && old_buf[i] !=3D new_buf[i]) { - i++; - nzrun_len++; - res--; - } - - /* word at a time for speed, use of 32-bit long okay */ - if (!res) { - /* truncation to 32-bit long okay */ - unsigned long mask =3D (unsigned long)0x0101010101010101ULL; - while (i < slen) { - unsigned long xor; - xor =3D *(unsigned long *)(old_buf + i) - ^ *(unsigned long *)(new_buf + i); - if ((xor - mask) & ~xor & (mask << 7)) { - /* found the end of an nzrun within the current long */ - while (old_buf[i] !=3D new_buf[i]) { - nzrun_len++; - i++; - } - break; - } else { - i +=3D sizeof(long); - nzrun_len +=3D sizeof(long); - } + dst_off +=3D uleb128_encode_small(dst + dst_off, len); + if (!zrun) { + /* overflow */ + if (dst_off + len > dlen) { + return -1; } + memcpy(dst + dst_off, new_buf + src_off, len); + dst_off +=3D len; } - - d +=3D uleb128_encode_small(dst + d, nzrun_len); - /* overflow */ - if (d + nzrun_len > dlen) { - return -1; - } - memcpy(dst + d, nzrun_start, nzrun_len); - d +=3D nzrun_len; - nzrun_len =3D 0; } =20 - return d; + return dst_off; } =20 int xbzrle_decode_buffer(uint8_t *src, int slen, uint8_t *dst, int dlen) --=20 2.19.1