From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-wm1-f41.google.com (mail-wm1-f41.google.com [209.85.128.41]) by mx.groups.io with SMTP id smtpd.web11.11612.1684203567762998661 for ; Mon, 15 May 2023 19:19:28 -0700 Authentication-Results: mx.groups.io; dkim=fail reason="signature has expired" header.i=@gmail.com header.s=20221208 header.b=rv1xLWVh; spf=pass (domain: gmail.com, ip: 209.85.128.41, mailfrom: pedro.falcato@gmail.com) Received: by mail-wm1-f41.google.com with SMTP id 5b1f17b1804b1-3f42bcf5df1so78023565e9.3 for ; Mon, 15 May 2023 19:19:27 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1684203566; x=1686795566; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=Nr7csmX1MMfN9xvbO9aXnw6ZrrlfJcpq1IbtHuSdtM4=; b=rv1xLWVhl5K9wBWOdtBBXDTI6Po3Q70kEtx8KgPw+kaP9J3c73gYd3XiDYQHwcqh8A kKYif/GI9n++QC3zMxaX8PhL8kbSxy7kTIuDfezhbe/oE/v+ZHcpflkPuYwJbTox82RF i9vqG9HIkXcdMMoPpXEQYYkK4NBkQjA1LM9a/Yt7kgAMLMPMSf/Y+XUsjwgxs72oZx8t 6n9tNUZiG3ITezLfzJd0jtKQzRnJhDkKCibloRarJuCT70yfI/MXkVLgiBXSlT2xc0gz /OUGnUl83KUCLJs+3eTN+WgUvPNZBVN121P+5LDyl/q3NUwLAadhr6BmuPIoEmwjU5Bi nxmA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1684203566; x=1686795566; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=Nr7csmX1MMfN9xvbO9aXnw6ZrrlfJcpq1IbtHuSdtM4=; b=f6pqjELsfeuPd7BA0l2HWLVRR4Dk/SVarsO8lVi5iMsI4Eps/CG7fnwfAaZ8eouhJd mRl5qz0UooLo8Mm7UEqVIPhWtmr2wQqZbb4jpFcfJP3/pi2IbilNX6H+LxDspkjOaWvR uvWNkENO/P/Kqn+eyYP5aEDiXBSHXLc0RQBZXBY7gqYHs4AXqmKzeRadIS6FXzwa4QWV aR7kDCIW9Xq9dy0lmc4HerVzqFu+4o4+CjV1sIrLIgabYDGiPi4bslPXyRg3WyxocNde gjFiWeQJ3M9eQYE2f187E1212AvSBtO4kN2OKZZ7y+9hmb62uMS2PKZEXpdBZZ+fhTRE FcPw== X-Gm-Message-State: AC+VfDyFtMHoJ1EFSnwtvc1qCqVmmE2rr9fwiNr94yckvC7OmeRnoDqi k9ngW2/KUA8uQvmJwQ2Q0qBars+QEYI= X-Google-Smtp-Source: ACHHUZ6eviuMTH2JixFIT/jGRUFXPY7zuhevDDi6hfzm/oU1mXljaD3rOg38FcH5ZSuE9oD/1J7TWg== X-Received: by 2002:a7b:cd04:0:b0:3f1:9527:8e8a with SMTP id f4-20020a7bcd04000000b003f195278e8amr22963070wmj.21.1684203565572; Mon, 15 May 2023 19:19:25 -0700 (PDT) Return-Path: Received: from PC-PEDRO-ARCH.lan ([2001:8a0:7280:5801:9441:3dce:686c:bfc7]) by smtp.gmail.com with ESMTPSA id u2-20020a05600c00c200b003f423f5b659sm640772wmm.10.2023.05.15.19.19.23 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 15 May 2023 19:19:24 -0700 (PDT) From: "Pedro Falcato" To: devel@edk2.groups.io Cc: Pedro Falcato , Michael D Kinney , Liming Gao , Zhiguang Liu , =?UTF-8?q?Marvin=20H=C3=A4user?= Subject: [PATCH v3 1/1] MdePkg/Base.h: Simplify alignment expressions Date: Tue, 16 May 2023 03:19:21 +0100 Message-Id: <20230516021921.411852-1-pedro.falcato@gmail.com> X-Mailer: git-send-email 2.40.1 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 the negation 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 Cc: Liming Gao Cc: Zhiguang Liu Cc: Marvin Häuser Signed-off-by: Pedro Falcato --- Changes: - Correct the ADDEND macro to use negation and not a binary NOT (thanks Liming!) 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..a070593a360d 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