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.web08.23903.1629119411563675420 for ; Mon, 16 Aug 2021 06:10:12 -0700 Authentication-Results: mx.groups.io; dkim=fail reason="body hash did not verify" header.i=@posteo.de header.s=2017 header.b=orOik1kA; spf=pass (domain: posteo.de, ip: 185.67.36.66, mailfrom: mhaeuser@posteo.de) Received: from submission (posteo.de [89.146.220.130]) by mout02.posteo.de (Postfix) with ESMTPS id BD492240104 for ; Mon, 16 Aug 2021 15:10:08 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=posteo.de; s=2017; t=1629119408; bh=eaIVLWayoOArpQcJurdxpJ8bm3OAfWQn9VbsAI8e3VU=; h=Subject:To:Cc:From:Date:From; b=orOik1kAHOWbWfsynT3dHyyRU9SLR/q510JwBAh1iZxcDM6JpMUIU4AYof2LrnDKr ARWt10scg5V6sxXrev04jAt/6FtKSc6AmXWG43okSEKiBPfeb5v2/pNHrKyIe/e5AL uvEosy4l+ocnMaIp8bt793e/TWZdy0y4cvVetpMOTXPO4RZ5V5Cs6iD5IX1MWoF/Yi 90OoZj9uNQTnzyP0NjdA3mF85PqQ6jZpiRCGyQ/3e5coP4bL/PybI00HGgnBxf4p4K X95xfk7tXGCrMO2rCncPJlWkawop/6J6r2fRcnsD/+HS3BvPQkuhBKEMmud3NqhspO mFZsrorl8d9Xg== Received: from customer (localhost [127.0.0.1]) by submission (posteo.de) with ESMTPSA id 4GpDzL5cjbz9rxF; Mon, 16 Aug 2021 15:10:06 +0200 (CEST) Subject: Re: [edk2-devel] [PATCH V2 2/3] MdePkg/Base.h: Introduce various alignment-related macros To: devel@edk2.groups.io, ray.ni@intel.com Cc: "Kinney, Michael D" , Liming Gao , "Liu, Zhiguang" , Vitaly Cheptsov References: <69c6e14c4fe944d380d38dcdb851a88f51631f86.1629057790.git.mhaeuser@posteo.de> From: =?UTF-8?B?TWFydmluIEjDpHVzZXI=?= Message-ID: <6b4798af-92e4-3411-1586-f9e1d922e0ab@posteo.de> Date: Mon, 16 Aug 2021 13:10:06 +0000 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: en-GB Content-Transfer-Encoding: quoted-printable Hey Ray, On 16/08/2021 11:42, Ni, Ray wrote: > Marvin, > So lucky to have you in the edk2 project looking into these fundamentals! Thank you. :) > + #define ALIGNOF(TYPE) OFFSET_OF (struct { CHAR8 C; TYPE A; }, A) > > 1. Does struct{} inside a macro conform to C standard? How is the compati= bility with different compilers? This should work, yes. The C standard defines offsetof as such: "The macros are [...] =C2=A0=C2=A0=C2=A0 =C2=A0=C2=A0=C2=A0 offsetof(type, member-designator) which expands to an integer constant expression that has type size_t,=20 the value of which is the offset in bytes, to the structure member (designated by=20 member-designator), from the beginning of its structure (designated by type). The type and=20 member designator shall be such that given =C2=A0=C2=A0=C2=A0 =C2=A0=C2=A0=C2=A0 static type t; then the expression &(t.member-designator) evaluates to an address=20 constant. [...]" [1] If we plug in t: =C2=A0=C2=A0=C2=A0 =C2=A0=C2=A0=C2=A0 static struct { CHAR8 C; TYPE A; } t= ; we get a valid static storage duration variable declaration that=20 satisfies the the last condition because: "An address constant is [...], a pointer to an lvalue designating an=20 object of static storage duration, or [...]" [2] It worked with all compilers I tinkered with at https://godbolt.org/ I sadly do not have access to any of the compilers where this may be=20 used effectively (RVCT, EBC). > +#define IS_POW2(Value) ((Value) !=3D 0U && ((Value) & ((Value) - 1U)) = =3D=3D > +0U) > > 2. Good to me. I learned this trick when implementing the MtrrLib. > > +#define ALIGN_VALUE_ADDEND(Value, Alignment) (((Alignment) - (Value)) > +& ((Alignment) - 1U)) > > 3. Is any other open source project using the same macro for the addend? > This is actually a general question to all new macros. > I would like the macros look familiar to developers from other open sourc= e projects. Good question, I never really saw it. I only came up with it because for=20 the new PE loader, we may align the PE memory within an underaligned=20 buffer, and for that we need the addend. I initially used to align up=20 and then subtract, but I saw this could be simplified with=20 ALIGN_VALUE_ADDEND, which was used in ALIGN_VALUE anyway. If you have a=20 better name, I'll change it. Best regards, Marvin [1] ISO/IEC 9899:2011, 7.19, 3. [2] ISO/IEC 9899:2011, 6.6, 9. > > >=20 > >