Skip to content

Commit

Permalink
[rom_ctrl,util] Make collision check faster in scramble_image.py
Browse files Browse the repository at this point in the history
Update the collision check implementation using a dict to traverse the
chunks only once.

Signed-off-by: Emmanuel Blot <[email protected]>
  • Loading branch information
rivos-eblot committed Aug 28, 2024
1 parent ab4a4de commit a68eac4
Showing 1 changed file with 13 additions and 13 deletions.
26 changes: 13 additions & 13 deletions hw/ip/rom_ctrl/util/mem.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import re
import subprocess
import tempfile
from itertools import combinations
from typing import Any, BinaryIO, Dict, IO, List, Optional, TextIO, Tuple

from elftools.elf.elffile import ELFFile # type: ignore
Expand Down Expand Up @@ -331,16 +332,15 @@ def collisions(self) -> List[Tuple[int, int]]:
such addresses where addr0 < addr1 and in ascending order of addr0.
'''
ret = []
for idx0, chunk0 in enumerate(self.chunks):
for off0, word0 in enumerate(chunk0.words):
for diff_idx, chunk1 in enumerate(self.chunks[idx0:]):
first_off1 = 0 if diff_idx else off0 + 1
for diff_off, word1 in enumerate(chunk1.words[first_off1:]):
off1 = first_off1 + diff_off

if word0 == word1:
addr0 = chunk0.base_addr + off0
addr1 = chunk1.base_addr + off1
ret.append((addr0, addr1))
return ret
data = {} # type: Dict[int, List[int]]
for chunk in self.chunks:
addr = chunk.base_addr
for off, word in enumerate(chunk.words):
if word not in data:
data[word] = []
data[word].append(addr + off)
ret = [] # type: List[Tuple[int, int]]
for addrs in data.values():
if len(addrs) > 1:
ret.extend(combinations(addrs, 2))
return list(sorted(ret))

0 comments on commit a68eac4

Please sign in to comment.