public inbox for devel@edk2.groups.io
 help / color / mirror / Atom feed
* [PATCH 1/1] MdePkg/Base.h: Simply alignment expressions
@ 2023-05-15 14:45 Pedro Falcato
  2023-05-15 15:15 ` [edk2-devel] " Rebecca Cran
  0 siblings, 1 reply; 5+ messages in thread
From: Pedro Falcato @ 2023-05-15 14:45 UTC (permalink / raw)
  To: devel
  Cc: Pedro Falcato, Michael D Kinney, Liming Gao, Zhiguang Liu,
	Marvin Häuser

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>
---
 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


^ permalink raw reply related	[flat|nested] 5+ messages in thread

* Re: [edk2-devel] [PATCH 1/1] MdePkg/Base.h: Simply alignment expressions
  2023-05-15 14:45 [PATCH 1/1] MdePkg/Base.h: Simply alignment expressions Pedro Falcato
@ 2023-05-15 15:15 ` Rebecca Cran
  2023-05-15 15:20   ` Marvin Häuser
  0 siblings, 1 reply; 5+ messages in thread
From: Rebecca Cran @ 2023-05-15 15:15 UTC (permalink / raw)
  To: devel, pedro.falcato
  Cc: Michael D Kinney, Liming Gao, Zhiguang Liu, Marvin Häuser

On 5/15/23 08:45, Pedro Falcato wrote:
> -#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))

Since ALIGN_VALUE_ADDEND is only used in ALIGN_VALUE, it should probably 
be deleted instead of updated.


-- 

Rebecca Cran


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [edk2-devel] [PATCH 1/1] MdePkg/Base.h: Simply alignment expressions
  2023-05-15 15:15 ` [edk2-devel] " Rebecca Cran
@ 2023-05-15 15:20   ` Marvin Häuser
  2023-05-15 15:29     ` Pedro Falcato
  0 siblings, 1 reply; 5+ messages in thread
From: Marvin Häuser @ 2023-05-15 15:20 UTC (permalink / raw)
  To: Rebecca Cran
  Cc: edk2-devel-groups-io, Pedro Falcato, Michael D Kinney, Liming Gao,
	Zhiguang Liu, Gerd Hoffmann

Well, I explicitly added this macro as a prerequisite to code used in our new PE library (remember this patch was initially sent in 2021). We still require it downstream, but obviously upstream is not interested in the related contributions that were to follow at the time.

Gerd picked it up because he wanted to attempt to re-try contributing the new PE library, but I haven't heard from him in weeks.

Design-wise, I agree it could be removed again. However, there first was a downstream burden when adding it (as we needed to rewrite our history to drop our downstream patch in favour of the upstream solution). Now introducing another downstream burden *again* to remove the macro that was added only a few weeks back would be a sign of poor management and planning.

Best regards,
Marvin

> On 15. May 2023, at 17:15, Rebecca Cran <rebecca@bsdio.com> wrote:
> 
> On 5/15/23 08:45, Pedro Falcato wrote:
>> -#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))
> 
> Since ALIGN_VALUE_ADDEND is only used in ALIGN_VALUE, it should probably be deleted instead of updated.
> 
> 
> -- 
> 
> Rebecca Cran
> 


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [edk2-devel] [PATCH 1/1] MdePkg/Base.h: Simply alignment expressions
  2023-05-15 15:20   ` Marvin Häuser
@ 2023-05-15 15:29     ` Pedro Falcato
  2023-05-15 15:33       ` Rebecca Cran
  0 siblings, 1 reply; 5+ messages in thread
From: Pedro Falcato @ 2023-05-15 15:29 UTC (permalink / raw)
  To: Marvin Häuser
  Cc: Rebecca Cran, edk2-devel-groups-io, Michael D Kinney, Liming Gao,
	Zhiguang Liu, Gerd Hoffmann

On Mon, May 15, 2023 at 4:20 PM Marvin Häuser <mhaeuser@posteo.de> wrote:
>
> Well, I explicitly added this macro as a prerequisite to code used in our new PE library (remember this patch was initially sent in 2021). We still require it downstream, but obviously upstream is not interested in the related contributions that were to follow at the time.
>
> Gerd picked it up because he wanted to attempt to re-try contributing the new PE library, but I haven't heard from him in weeks.
>
> Design-wise, I agree it could be removed again. However, there first was a downstream burden when adding it (as we needed to rewrite our history to drop our downstream patch in favour of the upstream solution). Now introducing another downstream burden *again* to remove the macro that was added only a few weeks back would be a sign of poor management and planning.

I don't agree with the removal of the macro. It's useful enough to
consumers of Base.h, and clearly there's code that is indeed actively
using it.

It's also a single line of code.

-- 
Pedro

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [edk2-devel] [PATCH 1/1] MdePkg/Base.h: Simply alignment expressions
  2023-05-15 15:29     ` Pedro Falcato
@ 2023-05-15 15:33       ` Rebecca Cran
  0 siblings, 0 replies; 5+ messages in thread
From: Rebecca Cran @ 2023-05-15 15:33 UTC (permalink / raw)
  To: devel, pedro.falcato, Marvin Häuser
  Cc: Kinney, Michael D, gaoliming, 'Zhiguang Liu',
	Gerd Hoffmann

I wasn’t aware of the history, so I agree with keeping it.

— 
Rebecca Cran

On Mon, May 15, 2023, at 9:29 AM, Pedro Falcato wrote:
> On Mon, May 15, 2023 at 4:20 PM Marvin Häuser <mhaeuser@posteo.de> wrote:
>>
>> Well, I explicitly added this macro as a prerequisite to code used in our new PE library (remember this patch was initially sent in 2021). We still require it downstream, but obviously upstream is not interested in the related contributions that were to follow at the time.
>>
>> Gerd picked it up because he wanted to attempt to re-try contributing the new PE library, but I haven't heard from him in weeks.
>>
>> Design-wise, I agree it could be removed again. However, there first was a downstream burden when adding it (as we needed to rewrite our history to drop our downstream patch in favour of the upstream solution). Now introducing another downstream burden *again* to remove the macro that was added only a few weeks back would be a sign of poor management and planning.
>
> I don't agree with the removal of the macro. It's useful enough to
> consumers of Base.h, and clearly there's code that is indeed actively
> using it.
>
> It's also a single line of code.
>
> -- 
> Pedro
>
>
> 

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2023-05-15 15:33 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2023-05-15 14:45 [PATCH 1/1] MdePkg/Base.h: Simply alignment expressions Pedro Falcato
2023-05-15 15:15 ` [edk2-devel] " Rebecca Cran
2023-05-15 15:20   ` Marvin Häuser
2023-05-15 15:29     ` Pedro Falcato
2023-05-15 15:33       ` Rebecca Cran

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox