From: "Pedro Falcato" <pedro.falcato@gmail.com>
To: devel@edk2.groups.io
Cc: "Pedro Falcato" <pedro.falcato@gmail.com>,
"Michael D Kinney" <michael.d.kinney@intel.com>,
"Liming Gao" <gaoliming@byosoft.com.cn>,
"Zhiguang Liu" <zhiguang.liu@intel.com>,
"Marvin Häuser" <mhaeuser@posteo.de>
Subject: [PATCH v2 1/1] MdePkg/Base.h: Simply alignment expressions
Date: Mon, 15 May 2023 16:14:37 +0100 [thread overview]
Message-ID: <20230515151437.179924-1-pedro.falcato@gmail.com> (raw)
Simplify ALIGN_VALUE and ALIGN_VALUE_ADDEND into simpler expressions.
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) = (15 + 15) & ~16 = 16
ALIGN_VALUE(16, 16) = (16 + 15) & ~16 = 16
Old codegen:
movq %rdi, %rax
negq %rax
andl $15, %eax
addq %rdi, %rax
New codegen:
leaq 15(%rdi), %rax
andq $-16, %rax
ALIGN_VALUE_ADDEND can simply use a bitwise NOT of Value to get the
addend for alignment, as, for instance:
~15 & (16 - 1) = 1
15 + 1 = 16
This change does not necessarily affect the end result, as the GCC and
clang compilers were already able to see through things and optimize
them into optimal instruction sequences, in the ALIGN_VALUE_ADDEND case.
Cc: Michael D Kinney <michael.d.kinney@intel.com>
Cc: Liming Gao <gaoliming@byosoft.com.cn>
Cc: Zhiguang Liu <zhiguang.liu@intel.com>
Cc: Marvin Häuser <mhaeuser@posteo.de>
Signed-off-by: Pedro Falcato <pedro.falcato@gmail.com>
---
v2:
Addressed concerns expressed on Discord by Marvin
- Added missing parens around Alignment in ALIGN_VALUE
- Replaced -1 with -1U, as in the other macros.
MdePkg/Include/Base.h | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/MdePkg/Include/Base.h b/MdePkg/Include/Base.h
index 6597e441a6e2..422f80aff53d 100644
--- a/MdePkg/Include/Base.h
+++ b/MdePkg/Include/Base.h
@@ -931,7 +931,7 @@ STATIC_ASSERT (ALIGNOF (__VERIFY_INT32_ENUM_SIZE) == sizeof (__VERIFY_INT32_ENUM
@return Addend to round Value up to alignment boundary Alignment.
**/
-#define ALIGN_VALUE_ADDEND(Value, Alignment) (((Alignment) - (Value)) & ((Alignment) - 1U))
+#define ALIGN_VALUE_ADDEND(Value, Alignment) ((~(Value)) & ((Alignment) - 1U))
/**
Rounds a value up to the next boundary using a specified alignment.
@@ -945,7 +945,7 @@ STATIC_ASSERT (ALIGNOF (__VERIFY_INT32_ENUM_SIZE) == sizeof (__VERIFY_INT32_ENUM
@return A value up to the next boundary.
**/
-#define ALIGN_VALUE(Value, Alignment) ((Value) + ALIGN_VALUE_ADDEND (Value, Alignment))
+#define ALIGN_VALUE(Value, Alignment) (((Value) + ((Alignment) - 1U)) & ~(Alignment))
/**
Adjust a pointer by adding the minimum offset required for it to be aligned on
--
2.40.1
next reply other threads:[~2023-05-15 15:14 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-05-15 15:14 Pedro Falcato [this message]
2023-05-16 1:46 ` 回复: [edk2-devel] [PATCH v2 1/1] MdePkg/Base.h: Simply alignment expressions gaoliming
2023-05-16 2:22 ` Pedro Falcato
2023-05-16 7:18 ` Marvin Häuser
2023-05-16 9:08 ` Marvin Häuser
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-list from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20230515151437.179924-1-pedro.falcato@gmail.com \
--to=devel@edk2.groups.io \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox