Skip to content

Sorted isn’t the same as non-overlapping – unless you check the right pair

write-from-scratch / multi-constraint / array

Hand out N address ranges that don’t overlap. Ordering the starts (regions[0].base < regions[1].base < ...) looks like enough, but says nothing about where each range ends: a large region reaches straight into the next one.

Once the array is sorted by base_addr, each region only needs checking against its immediate predecessor. That’s the point of keeping it sorted: an all-pairs check becomes an adjacent-pairs check.

regions[i-1] only makes sense for i > 0, and a foreach index is unsigned: guard it with a real if/else, or index -1 wraps to the largest value instead of going negative.

Fix the class below.

Requirements

  1. regions.size() is between 4 and 8, inclusive.
  2. regions[] is in strictly ascending order by base_addr.
  3. Every region’s base_addr is strictly less than its limit_addr.
  4. Every base_addr and limit_addr is within [32’h0000_0000 : 32’h7FFF_FFFF].
  5. Every region’s size (limit_addr - base_addr + 1; limit_addr is the last byte, inclusive) is a multiple of 4KB (32’h1000).
  6. Every base_addr is 4KB-aligned.
  7. No two regions overlap: with requirement 2 in place, check each base_addr against the previous region’s limit_addr.

Source on GitHub

nonoverlapping_address_ranges.sv