Swap array dimensions

Consider following example for swapping array dimensions:


type1_t [DIM_X1_MAX-1:0][[DIM_Y1_MAX-1:0] vec1;
type1_t [DIM_Y1_MAX-1:0][[DIM_X1_MAX-1:0] swapped_vec1;

function type1_t [DIM_Y1_MAX-1:0][[DIM_X1_MAX-1:0] swap_vec1;
  type1_t [DIM_X1_MAX-1:0][[DIM_Y1_MAX-1:0] in_vec1;
  for(int x1=0;x1<DIM_X1_MAX;x1++) begin
    for(int y1=0;y1<DIM_Y1_MAX;y1++) begin
      swap_vec1[y1][x1]=in_vec1[x1][y1];
    end
  end
endfunction
assign swapped_vec1 = swap_vec1(vec1);

While this works, it is not very efficient to scale it when there are N 2d arrays having different data types and width parameters. Any alternatives?

In reply to stupidkris1010:

It depends on what kind of efficiency you are looking for. If it RTL coding flexibility, then you can write

always_comb foreach(vec1[i,j]) swapped_vec1[j][i] = vec1[i][j];

In reply to dave_59:
Cheers @dave_59, I guess code flexibility was the right term that I was looking for (and not efficiency). I have got this one for N 2d arrays with different data types. Note that I have used generic names here in code snippet. In my actual use-case there is no patterns in data-type/signal names.
If you spot any further code flexibility, please let me know. Cheers.


type1_t [DIM_X1_MAX-1:0][[DIM_Y1_MAX-1:0] vec1;
type1_t [DIM_Y1_MAX-1:0][[DIM_X1_MAX-1:0] swapped_vec1;
type2_t [DIM_X2_MAX-1:0][[DIM_Y2_MAX-1:0] vec2;
type2_t [DIM_Y2_MAX-1:0][[DIM_X2_MAX-1:0] swapped_vec2;
// ...
typen_t [DIM_Xn_MAX-1:0][[DIM_Yn_MAX-1:0] vecn;
typen_t [DIM_Yn_MAX-1:0][[DIM_Xn_MAX-1:0] swapped_vecn;

`define F_SWAP_VECn(DTYPE,XPARAM,YPARAM,FUNC_NAME)    \
  function DTYPE [YPARAM-1:0][[XPARAM-1:0] FUNC_NAME; \
  DTYPE [XPARAM-1:0][[YPARAM-1:0] in;                 \
  foreach(in[i,j]) FUNC_NAME[j][i]=in[i][j];          \
endfunction

`F_SWAP_VECn(type1_t,DIM_X1_MAX,DIM_Y1_MAX,swap_vec1)
`F_SWAP_VECn(type2_t,DIM_X2_MAX,DIM_Y2_MAX,swap_vec2)
// ...
`F_SWAP_VECn(typen_t,DIM_Xn_MAX,DIM_Yn_MAX,swap_vecn)

assign swapped_vec1 = swap_vec1(vec1);
assign swapped_vec2 = swap_vec2(vec2);
// ...
assign swapped_vecn = swap_vecn(vecn);

In reply to stupidkris1010:

Why do you need to define a function?

`define INVERSE(in,out) always_comb foreach(out[i,j]) out[j][i] = int[i][j];

`INVERSE(swapped_vec1,vec1);
`INVERSE(swapped_vec2,vec2);