From nobody Wed Dec 17 01:32:13 2025 Received: from mailscanner04.zoner.fi (mailscanner04.zoner.fi [5.44.246.13]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id D1749161916 for ; Sun, 21 Jul 2024 13:37:16 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=5.44.246.13 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1721569039; cv=none; b=NLY5RJO6h5C6SXT+Ki2APyW4O5ZZNWlB9KcmNU8J3lpjh3JMZtj+O18KRwF1IUUJ5i7sTmJAiu5NTWOVpy73NoqT98vBsbI08QiiAyBmFo3iTkhmB4MEaDvGGenM9Y/uTJYCSMvj54MjDrzLSEWQAfHimYE7FCGHEKr3bGXRWco= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1721569039; c=relaxed/simple; bh=TDkuymGTHyPK5qlz75o1KXXkhPuoBm/rx9+sjJ+AAGI=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=ukZIcc3yzdyHx2FcbSn95GOOEbAoYndDoA1QDhtJXte2cXLr4TJmI3va0qTyWhCjN1Uqxu+Er75cG5sLhNdGQSd1VYhnUn89ZiFRgNINpXiaS8AG1s4rPZ+ymuBRow6soq6kzs8TsfgCp+X3PQ8j13B33sElqVsB2+LuP5Upduk= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=tukaani.org; spf=pass smtp.mailfrom=tukaani.org; arc=none smtp.client-ip=5.44.246.13 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=tukaani.org Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=tukaani.org Received: from www25.zoner.fi (www25.zoner.fi [84.34.147.45]) by mailscanner04.zoner.fi (Postfix) with ESMTPS id 0017E21345; Sun, 21 Jul 2024 16:37:15 +0300 (EEST) Received: from mail.zoner.fi ([84.34.147.244]) by www25.zoner.fi with esmtp (Exim 4.97.1) (envelope-from ) id 1sVWkR-00000001SmU-2J1D; Sun, 21 Jul 2024 16:37:14 +0300 From: Lasse Collin To: Andrew Morton Cc: Lasse Collin , Sam James , linux-kernel@vger.kernel.org Subject: [PATCH v2 10/16] xz: Optimize for-loop conditions in the BCJ decoders Date: Sun, 21 Jul 2024 16:36:25 +0300 Message-ID: <20240721133633.47721-11-lasse.collin@tukaani.org> X-Mailer: git-send-email 2.45.2 In-Reply-To: <20240721133633.47721-1-lasse.collin@tukaani.org> References: <20240721133633.47721-1-lasse.collin@tukaani.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Compilers cannot optimize the addition "i + 4" away since theoretically it could overflow. Reviewed-by: Sam James Signed-off-by: Lasse Collin --- lib/xz/xz_dec_bcj.c | 23 ++++++++++++++++++----- 1 file changed, 18 insertions(+), 5 deletions(-) diff --git a/lib/xz/xz_dec_bcj.c b/lib/xz/xz_dec_bcj.c index ab9237ed6db8..e0b4bf4999c0 100644 --- a/lib/xz/xz_dec_bcj.c +++ b/lib/xz/xz_dec_bcj.c @@ -161,7 +161,9 @@ static size_t bcj_powerpc(struct xz_dec_bcj *s, uint8_t= *buf, size_t size) size_t i; uint32_t instr; =20 - for (i =3D 0; i + 4 <=3D size; i +=3D 4) { + size &=3D ~(size_t)3; + + for (i =3D 0; i < size; i +=3D 4) { instr =3D get_unaligned_be32(buf + i); if ((instr & 0xFC000003) =3D=3D 0x48000001) { instr &=3D 0x03FFFFFC; @@ -218,7 +220,9 @@ static size_t bcj_ia64(struct xz_dec_bcj *s, uint8_t *b= uf, size_t size) /* Instruction normalized with bit_res for easier manipulation */ uint64_t norm; =20 - for (i =3D 0; i + 16 <=3D size; i +=3D 16) { + size &=3D ~(size_t)15; + + for (i =3D 0; i < size; i +=3D 16) { mask =3D branch_table[buf[i] & 0x1F]; for (slot =3D 0, bit_pos =3D 5; slot < 3; ++slot, bit_pos +=3D 41) { if (((mask >> slot) & 1) =3D=3D 0) @@ -266,7 +270,9 @@ static size_t bcj_arm(struct xz_dec_bcj *s, uint8_t *bu= f, size_t size) size_t i; uint32_t addr; =20 - for (i =3D 0; i + 4 <=3D size; i +=3D 4) { + size &=3D ~(size_t)3; + + for (i =3D 0; i < size; i +=3D 4) { if (buf[i + 3] =3D=3D 0xEB) { addr =3D (uint32_t)buf[i] | ((uint32_t)buf[i + 1] << 8) | ((uint32_t)buf[i + 2] << 16); @@ -289,7 +295,12 @@ static size_t bcj_armthumb(struct xz_dec_bcj *s, uint8= _t *buf, size_t size) size_t i; uint32_t addr; =20 - for (i =3D 0; i + 4 <=3D size; i +=3D 2) { + if (size < 4) + return 0; + + size -=3D 4; + + for (i =3D 0; i <=3D size; i +=3D 2) { if ((buf[i + 1] & 0xF8) =3D=3D 0xF0 && (buf[i + 3] & 0xF8) =3D=3D 0xF8) { addr =3D (((uint32_t)buf[i + 1] & 0x07) << 19) @@ -317,7 +328,9 @@ static size_t bcj_sparc(struct xz_dec_bcj *s, uint8_t *= buf, size_t size) size_t i; uint32_t instr; =20 - for (i =3D 0; i + 4 <=3D size; i +=3D 4) { + size &=3D ~(size_t)3; + + for (i =3D 0; i < size; i +=3D 4) { instr =3D get_unaligned_be32(buf + i); if ((instr >> 22) =3D=3D 0x100 || (instr >> 22) =3D=3D 0x1FF) { instr <<=3D 2; --=20 2.45.2