// File: /workspace/github_c_makefile_star2_O2/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 0x1237
movl $0x40000, %edi          # imm = 0x40000
callq 0x10c0
movq %rax, 0x8(%rsp)
xorl %ecx, %ecx
cmpq $0x100, %rcx            # imm = 0x100
je 0x127b
xorl %edx, %edx
cmpq $0x100, %rdx            # imm = 0x100
je 0x122c
movl %edx, %esi
xorl %ecx, %esi
andl $0x8, %esi
shll $0x4, %esi
cvtsi2ss %esi, %xmm0
movss %xmm0, (%rax,%rdx,4)
incq %rdx
jmp 0x120b
incq %rcx
addq $0x400, %rax            # imm = 0x400
jmp 0x1200
movq 0x8(%rsi), %rdi
leaq 0x18(%rsp), %rsi
leaq 0x8(%rsp), %rdx
callq 0x10b0
testl %eax, %eax
je 0x1288
callq 0x1050
movq %rax, %rbx
callq 0x1030
movq %rax, %r14
callq 0x1090
leaq 0xd9a(%rip), %rdi       # 0x2004
movq %r14, %rsi
movl %eax, %edx
xorl %eax, %eax
callq *%rbx
pushq $-0x1
popq %rax
jmp 0x1417
movl $0x100, %ebx            # imm = 0x100
movl $0x100, %r14d           # imm = 0x100
jmp 0x1291
movl 0x18(%rsp), %r14d
movl 0x20(%rsp), %ebx
callq 0x1050
movq %rax, %rcx
leaq 0xd80(%rip), %rdi       # 0x2020
xorl %ebp, %ebp
movl %r14d, %esi
movl %ebx, %edx
xorl %eax, %eax
callq *%rcx
movl %r14d, 0x10(%rsp)
movslq %r14d, %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
pushq $0x3c
popq %rdi
callq 0x10c0
movq %rax, %r12
cmpq $0xf, %rbp
je 0x12f9
movl $0x3d888889, (%r12,%rbp,4) # imm = 0x3D888889
incq %rbp
jmp 0x12e6
xorl %edi, %edi
callq 0x10d0
callq 0x1040
movl $0x3e8, %ebp            # imm = 0x3E8
subl $0x1, %ebp
jb 0x133c
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
jmp 0x130a
callq 0x1050
movq %rax, %rbp
callq 0x1080
leaq 0xcf3(%rip), %rdi       # 0x2043
movb $0x1, %al
callq *%rbp
pushq $0x1
popq %rdi
callq 0x10d0
callq 0x1040
movl $0x3e8, %ebp            # imm = 0x3E8
subl $0x1, %ebp
jb 0x1398
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
jmp 0x1366
callq 0x1050
movq %rax, %rbx
callq 0x1080
leaq 0xcb5(%rip), %rdi       # 0x2061
movb $0x1, %al
callq *%rbx
movq 0x8(%rsp), %rsi
leaq 0xcc5(%rip), %rdi       # 0x2081
movl 0x10(%rsp), %ebp
movl %ebp, %edx
movl 0x14(%rsp), %ebx
movl %ebx, %ecx
callq 0x1060
leaq 0xcba(%rip), %rdi       # 0x208e
movq %r14, %rsi
movl %ebx, %edx
movl %ebp, %ecx
callq 0x1060
leaq 0xcb5(%rip), %rdi       # 0x209c
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