编辑代码

#include <stdio.h>
#define INPUT_EXTEND_SCALE 8

void InputRearrange(char* din,
                    char* dout,
                    const int c,
                    const int h,
                    const int w,
                    const int pad) {
  char* dout_array[INPUT_EXTEND_SCALE];
  int idx_fpga_idata = 0;
  for (int i = 0; i < (c - 1) / INPUT_EXTEND_SCALE + 1; i++) {
    dout_array[0] =
        din + i * ((h + 2 * pad) * (w + 2 * pad) * INPUT_EXTEND_SCALE);
    for (int n = 1; n < INPUT_EXTEND_SCALE; n++) {
      dout_array[n] = dout_array[n - 1] + (h + 2 * pad) * (w + 2 * pad);
    }
    for (int r = 0; r < (h + 2 * pad); r++) {
      for (int cc = 0; cc < (w + 2 * pad); cc++) {
        for (int k = 0; k < INPUT_EXTEND_SCALE; k++) {
          if (k < c)
            dout[idx_fpga_idata++] =
                *(dout_array[k]++);  //*(dout_array[k] + r * w + cc);  //
          else
            dout[idx_fpga_idata++] = 0;
        }
      }
    }
  }
                    }

void input_reorganized(char* src, char* dst, int in_c, int in_h, int in_w) {
  int input_c = (in_c - 1) / INPUT_EXTEND_SCALE + 1;
  for (int i = 0; i < input_c; i++) {
    for (int r = 0; r < in_h; r++) {
      for (int c = 0; c < in_w; c++) {
        for (int k = 0; k < INPUT_EXTEND_SCALE; k++) {
          if (i * INPUT_EXTEND_SCALE + k < in_c) {
            char temp =
                src[((i * INPUT_EXTEND_SCALE + k) * in_h + r) * in_w + c];
            dst[((i * in_h + r) * in_w + c) * INPUT_EXTEND_SCALE + k] = temp;
          } else {
            dst[((i * in_h + r) * in_w + c) * INPUT_EXTEND_SCALE + k] = 0;
          }
        }
      }
    }
  }
}

void OutputRearrange(char* din,
                     char* dout,
                     const int c,
                     const int h,
                     const int w) {
  char* dout_array[INPUT_EXTEND_SCALE];
  int idx_fpga_idata = 0;
  for (int i = 0; i < i < (c - 1) / INPUT_EXTEND_SCALE + 1; i++) {
    dout_array[0] = dout + i * h * w * INPUT_EXTEND_SCALE;
    for (int n = 1; n < INPUT_EXTEND_SCALE; n++) {
      dout_array[n] = dout_array[n - 1] + h * w;
    }
    for (int r = 0; r < h; r++) {
      for (int cc = 0; cc < w; cc++) {
        for (int k = 0; k < INPUT_EXTEND_SCALE; k++) {
          *(dout_array[k]++) = din[idx_fpga_idata++];
        }
      }
    }
  }
                     }

void output_reorganize(char* src,
                       char* dst,
                       int out_c,
                       int out_h,
                       int out_w) {
  int output_channel_block = (out_c - 1) / INPUT_EXTEND_SCALE + 1;
  for (int i = 0; i < output_channel_block; i++) {
    for (int r = 0; r < out_h; r++) {
      for (int c = 0; c < out_w; c++) {
        for (int k = 0; k < INPUT_EXTEND_SCALE; k++) {
          if (i * INPUT_EXTEND_SCALE + k < out_c) {
            int sw_index =
                (i * INPUT_EXTEND_SCALE + k) * out_h * out_w + r * out_w + c;
            int hw_index =
                ((i * out_h + r) * out_w + c) * INPUT_EXTEND_SCALE + k;
            dst[sw_index] = src[hw_index];
          }
        }
      }
    }
  }
}

void reorganize(char*src,
						  char*dst,
						  //hls_avalon_slave_register_argument int32 src_reg,
						  //hls_avalon_slave_register_argument int32 dst_reg, 
						   int mode,
						   int en, 
						   int c, 
						   int h, 
						   int w
							) {

  int io_c = (c - 1) / INPUT_EXTEND_SCALE + 1;
  //src=ihc::mm_master<int>(reinterpret_cast<int *>(0x0)) ;
  //dst=ihc::mm_master<int>(reinterpret_cast<int *>(0x0)) ;
  // check the ctrl and reorganize the data accordingly
  if (en == 1) {
    for (int i = 0; i < io_c; i++) {
      for (int r = 0; r < h; r++) {
        for (int cc = 0; cc < w; cc++) {
          for (int k = 0; k < INPUT_EXTEND_SCALE; k++) {
//#pragma HLS PIPELINE II=1
            int sw_index = (i * INPUT_EXTEND_SCALE + k) * h * w + r * w + cc;
            int hw_index = ((i * h + r) * w + cc) * INPUT_EXTEND_SCALE + k;
            if (mode == 0) {
              if (i * INPUT_EXTEND_SCALE + k < c) {
                dst[hw_index] = src[sw_index];
              } else {
                dst[hw_index] = 0;
              }
            } else if (mode == 1) {
              if (i * INPUT_EXTEND_SCALE + k < c) {
                dst[sw_index] = src[hw_index];
              } else {
                dst[sw_index] = 0;
              }
            }
          }
        }
      }
	  
    }
    en = 0;
	return;
  } 
	else 
  { // invalid ctrl
    return;
  }
}

int main() {
	printf("out");
  // allocate memory for input and output data
  char src[500];
  char dst0[500];
  char dst1[500];
  char dst2[500];
    // create a control register with some values
    int	src_reg = 0;
	int	dst_reg = 0;
	int	mode = 1; // mode is channel shuffle
	int	en = 1; // enable signal is high
	int	c = 16; // number of channels is 16
	int	h = 5; // height of input is 5
	int	w = 5; // width of input is 5

  // initialize the input data with values from 1 to 400
  for (int i = 0; i < 400; i++) {
    src[i] = i ;
  }

  // call the reorganize function with the input, output and control register
  //reorganize(ctrl);
	//InputRearrange(src,dst0,c,h,w,0);
    //OutputRearrange(src,dst0,c,h,w);
    //reorganize(src,dst0,1,1,c,h,w);
    output_reorganize(src,dst0,c,h,w);
  // print the output data for manual verification
  for (int i = 0; i < 400; i++) {
    printf("out[%d] = %d\n", i,(dst0[i]));

  }

  // free the memory for input and output data

  getchar();
  
}