How this for loop could be translated efficiently in vhdl using for generate statements

vhdl

Hi guys i have this pseudo-code nested for loop (very simple to translate in C)

Input : x; //is an unsigned
Output : y;

val = 0;
for i = 0:n-1
  for j = 0:n-1
    if( ((x >> i) & 0x1) && ((x >> j) & 0x1) )
        val = val + x << (i + j);

y = val;

Is there a way to translate this using for-generate statments in VHDL? I'm aware of the fact that i could implement basically the same code using process and sequential for loop, but it is possible to do something similar using the for generate instead?

(my main issue is that there's no variable in concurrent statements).

Best Answer

If you use for loops within a process, you can use a variable. This (translated into valid VHDL) should synthesise fine (assuming n is a constant or generic).

The resulting hardware may be large or slow, but the same would be true if you used generate statements. If it doesn't meet your size or speed goals, you get to play with clocked processes, and pipelining.

The obvious way to do it using if ... generate would be to make Val a 2-D array signal, which makes the "unrolling" into parallel hardware absolutely explicit. (or an n * n sized 1-D array) But that is more complex and error-prone than the simple process approach.