// File: /workspace/github_c_makefile_star2_O3/zpole[P]IICV/src/test_imopv.c
// Function: main
int
main (int argc, char** argv)
{
  int width = 256 ;
  int height = 256 ;
  int const W = 7 ;

  float * image ;
  float * dest  ;
  float * dest2 ;
  float * filt ;

  int x, y ;

  if (argc < 2) {
    image = vl_malloc (sizeof(float) * width * height) ;
    for (y = 0 ; y < height ; ++y) {
      for (x = 0  ; x < width ; ++x) {
        image [x + width * y] =
        128.0f * (((x % 16) < 8) ^ ((y % 16) < 8)) ;
      }
    }
  } else {
    VlPgmImage im ;
    int err ;
    err = vl_pgm_read_new_f (argv[1], &im, &image) ;
    if (err) {
      VL_PRINTF("test_imopv: error: %s (%d)\n",
                vl_get_last_error_message(),
                vl_get_last_error()) ;
      return -1 ;
    }
    width = im.width ;
    height = im.height ;
  }

  VL_PRINTF("test_imopv: width: %d, height: %d\n", width, height);

  dest  = vl_malloc (sizeof(float) * width * height) ;
  dest2 = vl_malloc (sizeof(float) * width * height) ;
  filt  = vl_malloc (sizeof(float) * (2*W+1)) ;

  for (x = 0 ; x < 2*W+1 ; ++ x) {
    filt [x] = 1.0f / (2*W+1) ;
  }

#if 1
  vl_set_simd_enabled (0) ;
  vl_tic() ;
  {
    int trial = 0 ;
    for (trial = 0 ; trial < 1000 ; ++trial)
      vl_imconvcol_vf (dest, height,
                       image, width, height, width,
                       filt, -W, W, 1,
                       VL_TRANSPOSE|VL_PAD_BY_CONTINUITY) ;
  }
  VL_PRINTF ("Elapsed time no SIMD: %f [s]\n", vl_toc()) ;

  vl_set_simd_enabled (1) ;
  vl_tic() ;
  {
    int trial = 0 ;
    for (trial = 0 ; trial < 1000 ; ++trial)
      vl_imconvcol_vf (dest2, height,
                       image, width, height, width,
                       filt, -W, W, 1,
                       VL_TRANSPOSE|VL_PAD_BY_CONTINUITY) ;
  }
  VL_PRINTF ("Elapsed time with SIMD: %f [s]\n", vl_toc()) ;

#else

  vl_imconvcoltri_vf (dest, height,
                      image, width, height, width,
                      5, 1, VL_TRANSPOSE|VL_PAD_BY_CONTINUITY) ;

  vl_imconvcoltri_vf (dest2, height,
                      image, width, height, width,
                      5, 1, VL_TRANSPOSE|VL_PAD_BY_ZERO) ;
#endif

  vl_pgm_write_f("/tmp/src.pgm", image, width, height) ;
  vl_pgm_write_f("/tmp/test.pgm", dest, height, width) ;
  vl_pgm_write_f("/tmp/test2.pgm", dest2, height, width) ;


  vl_free(image) ;
  vl_free(filt) ;
  vl_free(dest) ;
  vl_free(dest2) ;

  return 0 ;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
cmpl $0x1, %edi
jg 0x1292
movl $0x40000, %edi          # imm = 0x40000
callq 0x10c0
movq %rax, 0x8(%rsp)
xorl %ecx, %ecx
movdqa 0xe04(%rip), %xmm0      # 0x2010
movdqa 0xe0c(%rip), %xmm1      # 0x2020
movdqa 0xe14(%rip), %xmm2      # 0x2030
movdqa 0xe1c(%rip), %xmm3      # 0x2040
movq %rcx, %xmm4
pshufd $0x44, %xmm4, %xmm4     # xmm4 = xmm4[0,1,0,1]
xorl %edx, %edx
movdqa %xmm1, %xmm5
movdqa %xmm0, %xmm6
movdqa %xmm5, %xmm7
pxor %xmm4, %xmm7
movdqa %xmm6, %xmm8
pxor %xmm4, %xmm8
shufps $0x88, %xmm8, %xmm7     # xmm7 = xmm7[0,2],xmm8[0,2]
pslld $0x4, %xmm7
pand %xmm2, %xmm7
cvtdq2ps %xmm7, %xmm7
movups %xmm7, (%rax,%rdx,4)
addq $0x4, %rdx
paddq %xmm3, %xmm5
paddq %xmm3, %xmm6
cmpq $0x100, %rdx            # imm = 0x100
jne 0x1238
incq %rcx
addq $0x400, %rax            # imm = 0x400
cmpq $0x100, %rcx            # imm = 0x100
jne 0x1224
movl $0x100, %ebx            # imm = 0x100
movl $0x100, %ebp            # imm = 0x100
jmp 0x12e0
movq 0x8(%rsi), %rdi
leaq 0x18(%rsp), %rsi
leaq 0x8(%rsp), %rdx
callq 0x10b0
testl %eax, %eax
je 0x12d8
callq 0x1050
movq %rax, %rbx
callq 0x1030
movq %rax, %r14
callq 0x1090
leaq 0xdab(%rip), %rdi       # 0x2070
movq %r14, %rsi
movl %eax, %edx
xorl %eax, %eax
callq *%rbx
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
jmp 0x1519
movl 0x18(%rsp), %ebp
movl 0x20(%rsp), %ebx
callq 0x1050
movq %rax, %rcx
leaq 0xd9d(%rip), %rdi       # 0x208c
movl %ebp, %esi
movl %ebx, %edx
xorl %eax, %eax
callq *%rcx
movslq %ebp, %r13
movl %ebx, 0x14(%rsp)
movslq %ebx, %rbx
movq %r13, %r15
imulq %rbx, %r15
shlq $0x2, %r15
movq %r15, %rdi
callq 0x10c0
movq %rax, %r14
movq %r15, %rdi
callq 0x10c0
movq %rax, %r15
movl $0x3c, %edi
callq 0x10c0
movq %rax, %r12
movdqa 0xcd9(%rip), %xmm0      # 0x2010
movdqa 0xce1(%rip), %xmm1      # 0x2020
movl $0x3, %eax
movdqa 0xd04(%rip), %xmm2      # 0x2050
movdqa 0xd0c(%rip), %xmm3      # 0x2060
pshufd $0xf5, %xmm3, %xmm4     # xmm4 = xmm3[1,1,3,3]
movdqa 0xcdf(%rip), %xmm5      # 0x2040
movdqa %xmm1, %xmm6
pxor %xmm2, %xmm6
movdqa %xmm3, %xmm7
pcmpgtd %xmm6, %xmm7
pshufd $0xf5, %xmm6, %xmm6     # xmm6 = xmm6[1,1,3,3]
pcmpeqd %xmm4, %xmm6
pand %xmm7, %xmm6
movd %xmm6, %ecx
testb $0x1, %cl
je 0x1390
movl $0x3d888889, -0xc(%r12,%rax,4) # imm = 0x3D888889
pextrw $0x4, %xmm6, %ecx
testb $0x1, %cl
je 0x13a3
movl $0x3d888889, -0x8(%r12,%rax,4) # imm = 0x3D888889
movdqa %xmm0, %xmm6
pxor %xmm2, %xmm6
movdqa %xmm3, %xmm7
pcmpgtd %xmm6, %xmm7
pshufd $0xf5, %xmm6, %xmm6     # xmm6 = xmm6[1,1,3,3]
pcmpeqd %xmm4, %xmm6
pand %xmm7, %xmm6
pextrw $0x0, %xmm6, %ecx
testb $0x1, %cl
je 0x13d3
movl $0x3d888889, -0x4(%r12,%rax,4) # imm = 0x3D888889
pextrw $0x4, %xmm6, %ecx
testb $0x1, %cl
je 0x13e5
movl $0x3d888889, (%r12,%rax,4) # imm = 0x3D888889
paddq %xmm5, %xmm1
paddq %xmm5, %xmm0
addq $0x4, %rax
cmpq $0x13, %rax
jne 0x1361
movl %ebp, 0x10(%rsp)
xorl %edi, %edi
callq 0x10d0
callq 0x1040
movl $0x3e8, %ebp            # imm = 0x3E8
movq 0x8(%rsp), %rdx
subq $0x8, %rsp
movq %r14, %rdi
movq %rbx, %rsi
movq %r13, %rcx
movq %rbx, %r8
movq %r13, %r9
pushq $0x5
pushq $0x1
pushq $0x7
pushq $-0x7
pushq %r12
callq 0x1070
addq $0x30, %rsp
decl %ebp
jne 0x1410
callq 0x1050
movq %rax, %rbp
callq 0x1080
leaq 0xc5c(%rip), %rdi       # 0x20af
movb $0x1, %al
callq *%rbp
movl $0x1, %edi
callq 0x10d0
callq 0x1040
movl $0x3e8, %ebp            # imm = 0x3E8
movq 0x8(%rsp), %rdx
subq $0x8, %rsp
movq %r15, %rdi
movq %rbx, %rsi
movq %r13, %rcx
movq %rbx, %r8
movq %r13, %r9
pushq $0x5
pushq $0x1
pushq $0x7
pushq $-0x7
pushq %r12
callq 0x1070
addq $0x30, %rsp
decl %ebp
jne 0x146b
callq 0x1050
movq %rax, %rbx
callq 0x1080
leaq 0xc1f(%rip), %rdi       # 0x20cd
movb $0x1, %al
callq *%rbx
movq 0x8(%rsp), %rsi
leaq 0xc2f(%rip), %rdi       # 0x20ed
movl 0x10(%rsp), %ebp
movl %ebp, %edx
movl 0x14(%rsp), %ebx
movl %ebx, %ecx
callq 0x1060
leaq 0xc24(%rip), %rdi       # 0x20fa
movq %r14, %rsi
movl %ebx, %edx
movl %ebp, %ecx
callq 0x1060
leaq 0xc1f(%rip), %rdi       # 0x2108
movq %r15, %rsi
movl %ebx, %edx
movl %ebp, %ecx
callq 0x1060
movq 0x8(%rsp), %rdi
callq 0x10a0
movq %r12, %rdi
callq 0x10a0
movq %r14, %rdi
callq 0x10a0
movq %r15, %rdi
callq 0x10a0
xorl %eax, %eax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq