From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mout02.posteo.de (mout02.posteo.de [185.67.36.66]) by mx.groups.io with SMTP id smtpd.web11.15452.1684221507531132859 for ; Tue, 16 May 2023 00:18:28 -0700 Authentication-Results: mx.groups.io; dkim=pass header.i=@posteo.de header.s=2017 header.b=C89+UGOg; spf=pass (domain: posteo.de, ip: 185.67.36.66, mailfrom: mhaeuser@posteo.de) Received: from submission (posteo.de [185.67.36.169]) by mout02.posteo.de (Postfix) with ESMTPS id ABECB240108 for ; Tue, 16 May 2023 09:18:25 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=posteo.de; s=2017; t=1684221505; bh=l+iFRbQUD4f2q9VB4+Y/EB4T+i5rTeizM9oOthA6bic=; h=Content-Transfer-Encoding:From:Mime-Version:Subject:Date: Message-Id:Cc:To:From; b=C89+UGOgGHKfjtoFeXjwpPnP2hzmovgQuKnHPIKkgyP8dBCGybg5t8L7+PXOePI0p vUcW1774FgAdc9pAAVirLjc6fW0wDnsHI2X1hskYdJZLkJfi/OO/u/e+JnyCHxQWgV Lj1lHjRmkkimK8/i0UkhscqY0X3SyALD+D+HJOPtQnRiK6heXgwT1VjLOTrKoCWOKV q4uWVjuJjQF1u+rq1VppffIDED7rgUzhpR+jTaXmf7pNV4jexDJXRDglIzJ7D5xvkd kagEM2I76Wk2WUM8/elscdTlCmd9FexnpjdagAGhwSgewiErdHcffUIzJNeA9gU7AA biHVExlokeQrw== Received: from customer (localhost [127.0.0.1]) by submission (posteo.de) with ESMTPSA id 4QL6z34G7mz9rxS; Tue, 16 May 2023 09:18:23 +0200 (CEST) From: =?UTF-8?B?TWFydmluIEjDpHVzZXI=?= Mime-Version: 1.0 (1.0) Subject: Re: [edk2-devel] [PATCH v2 1/1] MdePkg/Base.h: Simply alignment expressions Date: Tue, 16 May 2023 07:18:22 +0000 Message-Id: <30BAE3A6-AA5B-4092-9736-A9E9D9408C4B@posteo.de> References: Cc: devel@edk2.groups.io, gaoliming@byosoft.com.cn, Michael D Kinney , Zhiguang Liu In-Reply-To: To: Pedro Falcato Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable > On 16. May 2023, at 04:22, Pedro Falcato wrote: >=20 > =EF=BB=BFOn Tue, May 16, 2023 at 2:46=E2=80=AFAM gaoliming via groups.io > wrote: >>=20 >> Pedro: >>=20 >>> -----=E9=82=AE=E4=BB=B6=E5=8E=9F=E4=BB=B6----- >>> =E5=8F=91=E4=BB=B6=E4=BA=BA: devel@edk2.groups.io = =E4=BB=A3=E8=A1=A8 Pedro Falcato >>> =E5=8F=91=E9=80=81=E6=97=B6=E9=97=B4: 2023=E5=B9=B45=E6=9C=8815=E6=97=A5= 23:15 >>> =E6=94=B6=E4=BB=B6=E4=BA=BA: devel@edk2.groups.io >>> =E6=8A=84=E9=80=81: Pedro Falcato ; Michael D K= inney >>> ; Liming Gao ; >>> Zhiguang Liu ; Marvin H=C3=A4user >>> >>> =E4=B8=BB=E9=A2=98: [edk2-devel] [PATCH v2 1/1] MdePkg/Base.h: Simply al= ignment >>> expressions >>>=20 >>> Simplify ALIGN_VALUE and ALIGN_VALUE_ADDEND into simpler expressions. >>>=20 >>> ALIGN_VALUE can simply be a (value + (align - 1)) & ~align >>> expression, which works for any power of 2 alignment and generates >>> smaller code sequences. For instance: >>> ALIGN_VALUE(15, 16) =3D (15 + 15) & ~16 =3D 16 >>> ALIGN_VALUE(16, 16) =3D (16 + 15) & ~16 =3D 16 >>>=20 >>> Old codegen: >>> movq %rdi, %rax >>> negq %rax >>> andl $15, %eax >>> addq %rdi, %rax >>>=20 >>> New codegen: >>> leaq 15(%rdi), %rax >>> andq $-16, %rax >>>=20 >>> ALIGN_VALUE_ADDEND can simply use a bitwise NOT of Value to get the >>> addend for alignment, as, for instance: >>> ~15 & (16 - 1) =3D 1 >>> 15 + 1 =3D 16 >>>=20 >>=20 >>> ~15 & (16 - 1) =3D 1 >> Its value should be zero, not 1. I also verify the updated ALIGN_VALUE_AD= DEND. >> Its value is incorrect. Please double check. >=20 > Hi Liming, you're 100% right. There was a mixup when we were > discussing this optimization, and I got the mental calculations wrong > there. > Two's complement is definitely what we want, as one's complement is > always off by one (from what we want). >=20 > So negation (-) works beautifully, as seen in the old codegen (we > figured this out from the compiler's output). To be clear on the maths side of things: =E2=80=9C& (Alignment - 1U)=E2=80=9D is equivalent to =E2=80=9Cmod Alignment= =E2=80=9D for powers of two. =E2=80=9C-Value=E2=80=9D is equivalent to =E2=80=9C2^N - Value=E2=80=9D once= the expression is promoted to an unsigned type, where N is the precision of= said type. So, the old expression basically was =E2=80=9C(Alignment - Value) mod Alignm= ent=E2=80=9D and the new expression is =E2=80=9C(2^N - Value) mod Alignment=E2= =80=9D. By modulo laws, we can apply the mod to the operands, which for the l= eft ones gives us =E2=80=9CAlignment mod Alignment =3D 0=E2=80=9D and =E2=80= =9C2^N mod Alignment =3D 0=E2=80=9D, obviously for Alignment being a power o= f two. They=E2=80=99re trivially equivalent. If you want a more technical explanation - previously, only the lower =E2=80= =9CAlignment - 1=E2=80=9D Bits of the result were considered. As they are 0 f= or Alignment, the left operand, basically you get: Result =3D (Alignment - Value) & (Alignment - 1) =3D (Alignment - Value)[0 := Alignment - 1] =3D (Alignment[0 : Alignment - 1] - Value[0 : Alignment - 1]= )[=E2=80=A6] =3D (0 - Value[=E2=80=A6])[=E2=80=A6] As you can see, only the lower Alignment -1 Bits of both operands matter and= they are always equal for Alignment and 0. Best regards, Marvin >=20 > Sent a v3. >=20 > --=20 > Pedro