From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-wr1-f52.google.com (mail-wr1-f52.google.com [209.85.221.52]) by mx.groups.io with SMTP id smtpd.web10.90842.1684161944287835759 for ; Mon, 15 May 2023 07:45:44 -0700 Authentication-Results: mx.groups.io; dkim=fail reason="signature has expired" header.i=@gmail.com header.s=20221208 header.b=gHcMXFs6; spf=pass (domain: gmail.com, ip: 209.85.221.52, mailfrom: pedro.falcato@gmail.com) Received: by mail-wr1-f52.google.com with SMTP id ffacd0b85a97d-3077d134028so7720671f8f.3 for ; Mon, 15 May 2023 07:45:44 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1684161942; x=1686753942; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=4bcp6ck3cpZadyFzca+AB3/qRWC8C/9hzjr2Wvc5PzA=; b=gHcMXFs6hsJcjX7UaV9gRCuccc/k2pcOC23YPFn2QLo6aHa/jeKjNhso1Ubd3Io+ph UxsYz+MQrLcZwgSEsCNKl0YeL7tVvvedJ2UA01r2Tr51PDUDpEgSuIEiWNIid8sExN4t 1th8oSYdSee3bC+l5SjdxXYCIUGKEz15Wwx8W1NuUJ6K2EZ+ih/YAwAnlL9/ACNBl1yf nXUn7lEd0Nt2KW4QLYEMi19XQJxN5qNR2kitfeH12W4vm2Arb+hHN/xZA7z1vNx1IdBi pXZNe9cTS7EGzVRplkDhgvQCS1ZbjUEPvAkDfhQPQF8Ecdy/S6p3IEBPiRSSKsL7k3Kq wvDQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1684161942; x=1686753942; 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=4bcp6ck3cpZadyFzca+AB3/qRWC8C/9hzjr2Wvc5PzA=; b=Y0ccp7+QKTJCbwYpG0Ru5SFnW3LKrv8IOKnbZ4IH7TVX60xMHgtEmr/HdXTnGaiQNY t5RfD/r1tcnJuCj91OGQV96pZCmCJ5kj3ErB8mGnfWqRR1rnvybM6ZJglaG3MbwNQ/Gh Ss8VcLDwS1scRJhztmCSWJ9HfK5ElhmCgXSoTfnJfgaFmetQqZIEeKfc45d/eOSpuVul l7wjNMeDIl6VyAp0Kihozd3PKRennYcT6xK0rRFzq9ZCb3Ylyzfqc4bRTDFHtzjXjs6R TUO6G/djbPbilpdURSpOIXV7RZlJyMlpAw4ssdWaCivvgZFcovfljwsaFUPl8sSTvNlo 447w== X-Gm-Message-State: AC+VfDyXePScYP9Pg8EGNvkSn99dn3orrrLGg8sAVGB79CQJ+S365w5z 8YK3jnrUWSvnOSzZCJV7+mUFnFB/T8g= X-Google-Smtp-Source: ACHHUZ5N2k2AVEnDCKo6X4Cv6JY6UrIXXmAyn9qaLPeObhUUhBG4XJshaFRrEyB39uuTT4lUyZa8ag== X-Received: by 2002:a05:6000:1372:b0:303:2583:9635 with SMTP id q18-20020a056000137200b0030325839635mr24464165wrz.20.1684161942184; Mon, 15 May 2023 07:45:42 -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 r10-20020a5d52ca000000b003079c402762sm12395wrv.19.2023.05.15.07.45.41 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 15 May 2023 07:45:41 -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 1/1] MdePkg/Base.h: Simply alignment expressions Date: Mon, 15 May 2023 15:45:39 +0100 Message-Id: <20230515144539.176967-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 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 Cc: Liming Gao Cc: Zhiguang Liu Cc: Marvin Häuser Signed-off-by: Pedro Falcato --- 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..eba1178e3497 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 - 1)) & ~(Alignment)) /** Adjust a pointer by adding the minimum offset required for it to be aligned on -- 2.40.1