// File: /workspace/github_cpp_makefile_star2_O3/redpony[P]cpyp/crp_test.cc
// Function: test_mh1()
void test_mh1() {
  cpyp::MT19937 eng;
  const vector<double> ref = {0, 0, 0, 0.00466121, 0.0233846, 0.0647365, 0.125693, 0.183448, 0.204806, 0.177036, 0.119629, 0.0627523, 0.02507, 0.00725451, 0.0013911};
  cpyp::crp<int> crp(0.5, 1.0);
  vector<int> hist(15, 0);
  double c = 0;
  double ac = 0;
  double tmh = 0;
  for (int s = 0; s < 200000; ++s) {
    for (int i = 0; i < 15; ++i) {
      int y_i = i % 3;
      cpyp::crp<int> prev = crp;  // save old state
      bool wasrem = false;
      if (s > 0)
        wasrem = crp.decrement(y_i, eng);

      bool wasnew = crp.increment(y_i, q0[y_i], eng);
      if (s > 0) {
        double p_new = 1, q_new = 1;
        if (wasnew) { p_new = p0[y_i]; q_new = q0[y_i]; }
        double p_old = 1, q_old = 1;
        if (wasrem) { p_old = p0[y_i]; q_old = q0[y_i]; }
        double a = p_new / p_old * q_old / q_new;
        ++tmh;
        if (a >= 1.0 || cpyp::sample_uniform01<double>(eng) < a) { // mh accept
          ++ac;
        } else { // mh reject
          std::swap(crp, prev);  // swap is faster than =
        }
      }
    }
    if (s > 300 && s % 4 == 3) { ++c; hist[crp.num_tables()]++; }
  }
  ac /= tmh;
  cerr << "ACCEPTANCE: " << ac << endl;
  int j =0;
  double te = 0;
  double me = 0;
  for (auto i : hist) {
    double err = (i / c - ref[j++]);
    cerr << err << "\t" << i/c << endl;
    te += fabs(err);
    if (fabs(err) > me) { me = fabs(err); }
  }
  te /= 12;
  cerr << "Average error: " << te;
  if (te > 0.01) { cerr << "  ** TOO HIGH **"; }
  cerr << endl << "    Max error: " << me;
  if (me > 0.01) { cerr << "  ** TOO HIGH **"; }
  cerr << endl;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x1548, %rsp           # imm = 0x1548
leaq 0x1c0(%rsp), %rdi
callq 0x4bf4
leaq 0x6c0c(%rip), %rsi      # 0x9178
leaq 0x148(%rsp), %rax
movl $0xf, %ecx
movq %rax, %rdi
rep  movsq (%rsi), %es:(%rdi)
leaq 0x40(%rsp), %rdi
leaq 0x58(%rsp), %rcx
movl $0xf, %edx
movq %rax, %rsi
callq 0x4c72
leaq 0x58(%rsp), %rdi
movsd 0x6a65(%rip), %xmm0     # 0x9008
movsd 0x6a65(%rip), %xmm1     # 0x9010
callq 0x4cb0
movl $0x3c, %edi
callq 0x21b0
movq %rax, %rbp
xorpd %xmm0, %xmm0
movupd %xmm0, 0x2c(%rax)
movupd %xmm0, 0x20(%rax)
movupd %xmm0, 0x10(%rax)
movupd %xmm0, (%rax)
leaq 0xd8(%rsp), %r15
leaq 0x60(%rsp), %r14
xorl %ebx, %ebx
xorpd %xmm0, %xmm0
movsd %xmm0, 0x30(%rsp)
movsd %xmm0, 0x10(%rsp)
movsd %xmm0, 0x20(%rsp)
movl $0x0, (%rsp)
movq %rax, 0x18(%rsp)
movl $0xf, %r12d
xorl %r13d, %r13d
movl %r13d, %eax
movl $0xaaaaaaab, %ecx       # imm = 0xAAAAAAAB
imulq %rcx, %rax
shrq $0x21, %rax
leal (%rax,%rax,2), %eax
movl %r13d, %ecx
subl %eax, %ecx
movl %ecx, 0xc(%rsp)
movq 0x58(%rsp), %rax
movq 0x68(%rsp), %rcx
movq %rax, 0xd0(%rsp)
movq %rbx, 0xd8(%rsp)
movq %rcx, 0xe0(%rsp)
movq %rbx, 0xe8(%rsp)
movq 0x78(%rsp), %rax
movq %rax, 0xf0(%rsp)
movups 0x20(%r14), %xmm0
movups %xmm0, 0x20(%r15)
movq %rbx, 0x108(%rsp)
movq %r15, 0x148(%rsp)
movq %r15, %rdi
movq %r14, %rsi
leaq 0x148(%rsp), %rdx
callq 0x5a68
movq 0x68(%r14), %rax
movq %rax, 0x68(%r15)
movupd 0x38(%r14), %xmm0
movupd 0x48(%r14), %xmm1
movupd 0x58(%r14), %xmm2
movupd %xmm2, 0x58(%r15)
movupd %xmm1, 0x48(%r15)
movupd %xmm0, 0x38(%r15)
cmpl $0x0, (%rsp)
je 0x26e1
leaq 0x58(%rsp), %rdi
leaq 0xc(%rsp), %rsi
leaq 0x1c0(%rsp), %rdx
xorl %ecx, %ecx
callq 0x4d1a
testl %eax, %eax
setne %bpl
jmp 0x26e3
xorl %ebp, %ebp
movslq 0xc(%rsp), %rax
leaq 0x6a51(%rip), %rcx      # 0x9140
leaq (%rcx,%rax,8), %rdx
leaq 0x58(%rsp), %rdi
leaq 0xc(%rsp), %rsi
leaq 0x1c0(%rsp), %rcx
callq 0x4e70
cmpl $0x0, (%rsp)
je 0x27d6
movsd 0x68f4(%rip), %xmm0     # 0x9010
movapd %xmm0, %xmm3
testl %eax, %eax
je 0x2741
movslq 0xc(%rsp), %rax
leaq 0x6a30(%rip), %rcx      # 0x9160
movsd (%rcx,%rax,8), %xmm3
leaq 0x6a04(%rip), %rcx      # 0x9140
movsd (%rcx,%rax,8), %xmm0
movsd 0x68c7(%rip), %xmm2     # 0x9010
movapd %xmm2, %xmm1
testb %bpl, %bpl
je 0x276f
movslq 0xc(%rsp), %rax
leaq 0x6a02(%rip), %rcx      # 0x9160
movsd (%rcx,%rax,8), %xmm1
leaq 0x69d6(%rip), %rcx      # 0x9140
movsd (%rcx,%rax,8), %xmm2
divsd %xmm1, %xmm3
mulsd %xmm2, %xmm3
divsd %xmm0, %xmm3
movsd 0x688d(%rip), %xmm0     # 0x9010
movsd 0x20(%rsp), %xmm1
addsd %xmm0, %xmm1
movsd %xmm1, 0x20(%rsp)
movsd %xmm3, 0x38(%rsp)
ucomisd %xmm0, %xmm3
movq 0x18(%rsp), %rbp
jae 0x27bd
leaq 0x1c0(%rsp), %rdi
callq 0x770d
movsd 0x38(%rsp), %xmm1
ucomisd %xmm0, %xmm1
jbe 0x27e0
movsd 0x10(%rsp), %xmm0
addsd 0x6845(%rip), %xmm0     # 0x9010
movsd %xmm0, 0x10(%rsp)
jmp 0x28bd
movq 0x18(%rsp), %rbp
jmp 0x28bd
movq 0x58(%rsp), %rax
movq %rax, 0x148(%rsp)
leaq 0x150(%rsp), %rbp
movq %rbp, %rdi
movq %r14, %rsi
movq %r14, %rdx
callq 0x799e
movq 0x68(%r14), %rax
movq %rax, 0x68(%rbp)
movups 0x38(%r14), %xmm0
movups 0x48(%r14), %xmm1
movups 0x58(%r14), %xmm2
movups %xmm2, 0x58(%rbp)
movups %xmm1, 0x48(%rbp)
movups %xmm0, 0x38(%rbp)
movq 0xd0(%rsp), %rax
movq %rax, 0x58(%rsp)
movq %r14, %rdi
movq %r15, %rsi
callq 0x22c0
movq 0x68(%r15), %rax
movq %rax, 0x68(%r14)
movups 0x38(%r15), %xmm0
movups 0x48(%r15), %xmm1
movups 0x58(%r15), %xmm2
movups %xmm2, 0x58(%r14)
movups %xmm1, 0x48(%r14)
movq %r14, %rbx
movups %xmm0, 0x38(%r14)
movq 0x148(%rsp), %rax
movq %rax, 0xd0(%rsp)
movq %r15, %rdi
movq %rbp, %rsi
callq 0x22c0
movq 0x68(%rbp), %rax
movq %rax, 0x68(%r15)
movupd 0x38(%rbp), %xmm0
movupd 0x48(%rbp), %xmm1
movupd 0x58(%rbp), %xmm2
movupd %xmm2, 0x58(%r15)
movupd %xmm1, 0x48(%r15)
movupd %xmm0, 0x38(%r15)
movq %rbp, %rdi
callq 0x5c7a
movq 0x18(%rsp), %rbp
movq %rbx, %r14
xorl %ebx, %ebx
movq %r15, %rdi
callq 0x5c7a
incl %r13d
decl %r12d
jne 0x260e
movl (%rsp), %ecx
cmpl $0x12d, %ecx            # imm = 0x12D
setae %al
notl %ecx
testb $0x3, %cl
sete %cl
testb %cl, %al
je 0x2905
movsd 0x30(%rsp), %xmm0
addsd 0x6719(%rip), %xmm0     # 0x9010
movsd %xmm0, 0x30(%rsp)
movl 0x58(%rsp), %eax
incl (%rbp,%rax,4)
movl (%rsp), %eax
incl %eax
movl %eax, (%rsp)
cmpl $0x30d40, %eax          # imm = 0x30D40
jne 0x2605
movq 0xa6d1(%rip), %rdi      # 0xcff0
leaq 0x6ab2(%rip), %rsi      # 0x93d8
movl $0xc, %edx
callq 0x21c0
movsd 0x10(%rsp), %xmm0
divsd 0x20(%rsp), %xmm0
movq 0xa6ad(%rip), %rdi      # 0xcff0
callq 0x2230
movq %rax, %rbx
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x2190
movsbl %al, %esi
movq %rbx, %rdi
callq 0x2030
movq %rax, %rdi
callq 0x2100
xorpd %xmm1, %xmm1
xorl %r13d, %r13d
movq 0xa670(%rip), %rbx      # 0xcff0
leaq 0x6a5e(%rip), %r15      # 0x93e5
xorpd %xmm0, %xmm0
movsd %xmm0, (%rsp)
movsd %xmm1, 0x38(%rsp)
xorps %xmm0, %xmm0
cvtsi2sdl (%rbp,%r13), %xmm0
divsd 0x30(%rsp), %xmm0
movq 0x40(%rsp), %rax
movsd %xmm0, 0x10(%rsp)
subsd (%rax,%r13,2), %xmm0
movq %rbx, %rdi
movapd %xmm0, 0x20(%rsp)
callq 0x2230
movq %rax, %r12
movl $0x1, %edx
movq %rax, %rdi
movq %r15, %rsi
callq 0x21c0
movq %r12, %rdi
movsd 0x10(%rsp), %xmm0
callq 0x2230
movq %rax, %r12
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %r12, %rdi
movl $0xa, %esi
callq 0x2190
movsbl %al, %esi
movq %r12, %rdi
callq 0x2030
movq %rax, %rdi
callq 0x2100
movapd 0x20(%rsp), %xmm0
andpd 0x6682(%rip), %xmm0     # 0x90a0
movsd (%rsp), %xmm1
addsd %xmm0, %xmm1
movsd %xmm1, (%rsp)
maxsd 0x38(%rsp), %xmm0
addq $0x4, %r13
movapd %xmm0, %xmm1
cmpq $0x3c, %r13
jne 0x2990
movapd %xmm0, 0x20(%rsp)
movq 0xa59f(%rip), %rdi      # 0xcff0
leaq 0x698f(%rip), %rsi      # 0x93e7
movl $0xf, %edx
callq 0x21c0
movsd (%rsp), %xmm0
divsd 0x65a9(%rip), %xmm0     # 0x9018
movsd %xmm0, (%rsp)
movq 0xa575(%rip), %rbx      # 0xcff0
movq %rbx, %rdi
movsd (%rsp), %xmm0
callq 0x2230
movsd (%rsp), %xmm0
ucomisd 0x658b(%rip), %xmm0     # 0x9020
jbe 0x2aaf
movq 0xa552(%rip), %rdi      # 0xcff0
leaq 0x6952(%rip), %rsi      # 0x93f7
movl $0x10, %edx
callq 0x21c0
movq (%rbx), %rax
addq -0x18(%rax), %rbx
movq %rbx, %rdi
movl $0xa, %esi
callq 0x2190
movsbl %al, %esi
movq 0xa523(%rip), %rbx      # 0xcff0
movq %rbx, %rdi
callq 0x2030
movq %rax, %rdi
callq 0x2100
movq %rax, %r15
leaq 0x6921(%rip), %rsi      # 0x9408
movl $0xf, %edx
movq %rax, %rdi
callq 0x21c0
movq %r15, %rdi
movapd 0x20(%rsp), %xmm0
callq 0x2230
movapd 0x20(%rsp), %xmm0
ucomisd 0x6510(%rip), %xmm0     # 0x9020
jbe 0x2b2a
movq 0xa4d7(%rip), %rdi      # 0xcff0
leaq 0x68d7(%rip), %rsi      # 0x93f7
movl $0x10, %edx
callq 0x21c0
movq (%rbx), %rax
addq -0x18(%rax), %rbx
movq %rbx, %rdi
movl $0xa, %esi
callq 0x2190
movsbl %al, %esi
movq 0xa4a8(%rip), %rdi      # 0xcff0
callq 0x2030
movq %rax, %rdi
callq 0x2100
movq %rbp, %rdi
callq 0x2180
movq %r14, %rdi
callq 0x5c7a
movq 0x40(%rsp), %rdi
testq %rdi, %rdi
je 0x2b74
callq 0x2180
addq $0x1548, %rsp           # imm = 0x1548
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
jmp 0x2bc4
movq %rax, %rbx
jmp 0x2bce
jmp 0x2bb9
jmp 0x2bb9
jmp 0x2b96
movq %rax, %rdi
callq 0x58fe
jmp 0x2bb9
jmp 0x2ba2
movq %rax, %rbx
jmp 0x2baf
movq %rax, %rbx
movq 0x18(%rsp), %rbp
movq %r15, %rdi
callq 0x5c7a
jmp 0x2bbc
movq %rax, %rbx
movq %rbp, %rdi
callq 0x2180
leaq 0x60(%rsp), %rdi
callq 0x5c7a
movq 0x40(%rsp), %rdi
testq %rdi, %rdi
je 0x2bdd
callq 0x2180
movq %rbx, %rdi
callq 0x2270