/* Merge 2 ranges (normal and prefetchable) into a single aperture comprehending the addresses encompassed by both of them. If both ranges are not empty they must be contiguous for correctness. */
STATIC EFI_STATUS MergeHostBridgeApertures(IN UINT64 Start, IN UINT64 Size, IN UINT64 PStart, IN UINT64 PSize, IN UINT64 MaxValue, OUT PCI_ROOT_BRIDGE_APERTURE *Aperture)
/* Merge 2 ranges (normal and prefetchable) into a single aperture comprehending the addresses encompassed by both of them. If both ranges are not empty they must be contiguous for correctness. */
STATIC EFI_STATUS MergeHostBridgeApertures(IN UINT64 Start, IN UINT64 Size, IN UINT64 PStart, IN UINT64 PSize, IN UINT64 MaxValue, OUT PCI_ROOT_BRIDGE_APERTURE *Aperture)
{
  UINT64  PEnd;
  if (Aperture == NULL) {
    if (!IS_RANGE_INVALID (Start, Size, MaxValue) ||
        !IS_RANGE_INVALID (PStart, PSize, MaxValue))
    {
      return EFI_INVALID_PARAMETER;
    }
    return EFI_WARN_BUFFER_TOO_SMALL;
  }
  InvalidateRootBridgeAperture (Aperture);
  if (!IS_RANGE_INVALID (Start, Size, MaxValue)) {
    Aperture->Base  = Start;
    Aperture->Limit = Start + Size - 1;
  }
  if (!IS_RANGE_INVALID (PStart, PSize, MaxValue)) {
    PEnd = PStart + PSize - 1;
    if (PStart < Aperture->Base) {
      Aperture->Base = PStart;
    }
    if (PEnd > Aperture->Limit) {
      Aperture->Limit = PEnd;
    }
  }
  return EFI_SUCCESS;
}