edk2/MdePkg/Library/BaseMemoryLibOptPei
Mingjie Shen f57a4f1216 MdePkg: Replace manual alignment checks with helper macros
Replace manual alignment checks with IS_ALIGNED() and
ADDRESS_IS_ALIGNED().

Convert the following bitmask and modulo forms:

- ((E & ((PowOf2Expr) - ONE)) == ZERO)
- ((E & ((PowOf2Expr) - ONE)) != ZERO)
- ((E % (PowOf2Expr)) == ZERO)
- ((E % (PowOf2Expr)) != ZERO)

to the corresponding helper macro forms:

+ IS_ALIGNED (E, PowOf2Expr)
+ !IS_ALIGNED (E, PowOf2Expr)

PowOf2Expr is limited to known power-of-two expressions, including
SIZE_* and BASE_* macros, EFI_PAGE_SIZE, CPU_STACK_ALIGNMENT,
RUNTIME_PAGE_ALLOCATION_GRANULARITY, sizeof() of UEFI integer types
(e.g. BOOLEAN, CHAR16, UINT32, UINTN) and pointer types, and 1 << E1
expressions.

Address checks that cast the checked value to UINTN are written with
ADDRESS_IS_ALIGNED().

The change was generated with the Coccinelle semantic patch below.

```smpl
@power_of_2_expr@
expression PowOf2Expr;
expression E1;
typedef BOOLEAN, CHAR8, CHAR16, INT8, UINT8, INT16, UINT16, INT32, UINT32, INT64, UINT64, INTN, UINTN;
type ScalarType = { BOOLEAN, CHAR8, CHAR16, INT8, UINT8, INT16, UINT16, INT32, UINT32, INT64, UINT64, INTN, UINTN };
type AnyType;
type PointerType = AnyType *;
idexpression ScalarType ScalarValue;
idexpression PointerType PointerValue;
constant SizeBase =~ "^(SIZE|BASE)_(1|2|4|8|16|32|64|128|256|512)[KMGTPE]B$";
constant NamedPowerOf2 =~ "^(EFI_PAGE_SIZE|CPU_STACK_ALIGNMENT|RUNTIME_PAGE_ALLOCATION_GRANULARITY)$";
constant ONE = {1, 1U, 1u};
@@
(
(
  SizeBase
|
  NamedPowerOf2
|
  ONE << E1
|
  sizeof (ScalarType)
|
  sizeof (PointerType)
|
  sizeof (ScalarValue)
|
  sizeof (PointerValue)
)
&
PowOf2Expr
)

@aligned depends on power_of_2_expr disable is_zero,isnt_zero@
expression E;
expression power_of_2_expr.PowOf2Expr;
constant ONE = {1, 1U, 1u};
constant ZERO = {0, 0U, 0u};
@@
(
  ((E & (E - ONE)) == ZERO)
|
- ((E & ((PowOf2Expr) - ONE)) == ZERO)
+ IS_ALIGNED (E, PowOf2Expr)
|
  ((E & (E - ONE)) != ZERO)
|
- ((E & ((PowOf2Expr) - ONE)) != ZERO)
+ !IS_ALIGNED (E, PowOf2Expr)
|
- ((E % (PowOf2Expr)) == ZERO)
+ IS_ALIGNED (E, PowOf2Expr)
|
- ((E % (PowOf2Expr)) != ZERO)
+ !IS_ALIGNED (E, PowOf2Expr)
)

@address_is_aligned@
typedef UINTN;
expression *Address;
expression Alignment;
@@
- IS_ALIGNED ((UINTN) Address, Alignment)
+ ADDRESS_IS_ALIGNED (Address, Alignment)

@normalize_aligned disable paren expression@
expression E, SZ;
@@
(
- (IS_ALIGNED (E, SZ))
+ IS_ALIGNED (E, SZ)
|
- (!IS_ALIGNED (E, SZ))
+ !IS_ALIGNED (E, SZ)
)

@normalize_macro_args disable paren expression@
expression E, SZ;
@@
(
- IS_ALIGNED ((E), SZ)
+ IS_ALIGNED (E, SZ)
|
- IS_ALIGNED (E, (SZ))
+ IS_ALIGNED (E, SZ)
)
```

Signed-off-by: Mingjie Shen <shen497@purdue.edu>
2026-06-09 07:20:10 +00:00
..
Ia32 MdePkg: Replace BSD License with BSD+Patent License 2019-04-09 10:58:13 -07:00
X64 MdePkg: Replace BSD License with BSD+Patent License 2019-04-09 10:58:13 -07:00
BaseMemoryLibOptPei.inf Refactor SetMemWrapper to reduce binary size 2024-09-11 21:50:20 +00:00
BaseMemoryLibOptPei.uni MdePkg: Replace BSD License with BSD+Patent License 2019-04-09 10:58:13 -07:00
CompareMemWrapper.c MdePkg: Apply uncrustify changes 2021-12-07 17:24:28 +00:00
CopyMemWrapper.c MdePkg: Apply uncrustify changes 2021-12-07 17:24:28 +00:00
IsZeroBufferWrapper.c MdePkg: Replace BSD License with BSD+Patent License 2019-04-09 10:58:13 -07:00
MemLibGuid.c MdePkg: Apply uncrustify changes 2021-12-07 17:24:28 +00:00
MemLibInternals.h MdePkg: Replace include guards with #pragma once 2026-02-23 21:01:28 +00:00
ScanMem8Wrapper.c MdePkg: Apply uncrustify changes 2021-12-07 17:24:28 +00:00
ScanMem16Wrapper.c MdePkg: Replace manual alignment checks with helper macros 2026-06-09 07:20:10 +00:00
ScanMem32Wrapper.c MdePkg: Replace manual alignment checks with helper macros 2026-06-09 07:20:10 +00:00
ScanMem64Wrapper.c MdePkg: Replace manual alignment checks with helper macros 2026-06-09 07:20:10 +00:00
SetMem16Wrapper.c MdePkg: Replace manual alignment checks with helper macros 2026-06-09 07:20:10 +00:00
SetMem32Wrapper.c MdePkg: Replace manual alignment checks with helper macros 2026-06-09 07:20:10 +00:00
SetMem64Wrapper.c MdePkg: Replace manual alignment checks with helper macros 2026-06-09 07:20:10 +00:00
SetMemNWrapper.c Refactor SetMemWrapper to reduce binary size 2024-09-11 21:50:20 +00:00
SetMemWrapper.c Refactor SetMemWrapper to reduce binary size 2024-09-11 21:50:20 +00:00
ZeroMemWrapper.c MdePkg: Replace BSD License with BSD+Patent License 2019-04-09 10:58:13 -07:00