// File: /workspace/github_c_makefile_star2_O1/archer1357[P]Offline-Raytracer/sobol.c
// Function: sobol_init
int sobol_init(void * state, unsigned int dimension)
{
  sobol_state_t * s_state = (sobol_state_t *) state;
  unsigned int i_dim;
  int j, k;
  int ell;

  if(dimension < 1 || dimension > SOBOL_MAX_DIMENSION) {
    return GSL_EINVAL;
  }

  /* Initialize direction table in dimension 0. */
  for(k=0; k<SOBOL_BIT_COUNT; k++) s_state->v_direction[k][0] = 1;

  /* Initialize in remaining dimensions. */
  for(i_dim=1; i_dim<dimension; i_dim++) {

    const int poly_index = i_dim;
    const int degree_i = degree_table[poly_index];
    int includ[8];

    /* Expand the polynomial bit pattern to separate
     * components of the logical array includ[].
     */
    int p_i = primitive_polynomials[poly_index];
    for(k = degree_i-1; k >= 0; k--) {
      includ[k] = ((p_i % 2) == 1);
      p_i /= 2;
    }

    /* Leading elements for dimension i come from v_init[][]. */
    for(j=0; j<degree_i; j++) s_state->v_direction[j][i_dim] = v_init[j][i_dim];

    /* Calculate remaining elements for this dimension,
     * as explained in Bratley+Fox, section 2.
     */
    for(j=degree_i; j<SOBOL_BIT_COUNT; j++) {
      int newv = s_state->v_direction[j-degree_i][i_dim];
      ell = 1;
      for(k=0; k<degree_i; k++) {
        ell *= 2;
        if(includ[k]) newv ^= (ell * s_state->v_direction[j-k-1][i_dim]);
      }
      s_state->v_direction[j][i_dim] = newv;
    }
  }

  /* Multiply columns of v by appropriate power of 2. */
  ell = 1;
  for(j=SOBOL_BIT_COUNT-1-1; j>=0; j--) {
    ell *= 2;
    for(i_dim=0; i_dim<dimension; i_dim++) {
      s_state->v_direction[j][i_dim] *= ell;
    }
  }

  /* 1/(common denominator of the elements in v_direction) */
  s_state->last_denominator_inv = 1.0 /(2.0 * ell);

  /* final setup */
  s_state->sequence_count = 0;
  for(i_dim=0; i_dim<dimension; i_dim++) s_state->last_numerator_vec[i_dim] = 0;

  return GSL_SUCCESS;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
leal -0x29(%rsi), %eax
movl $0x4, %ebx
cmpl $-0x28, %eax
jb 0x40a128
movl $0xb0, %eax
movl $0x1, (%rdi,%rax)
addq $0xa0, %rax
cmpq $0x1370, %rax           # imm = 0x1370
jne 0x409f30
movq %rdi, 0x8(%rsp)
movl %esi, %r11d
movq %rsi, 0x10(%rsp)
cmpl $0x2, %esi
jb 0x40a0b5
movabsq $-0x100000000, %rcx     # imm = 0xFFFFFFFF00000000
movq 0x8(%rsp), %rax
leaq 0xb0(%rax), %rsi
leaq 0xb4(%rax), %r8
movl $0x1, %r9d
leaq 0xcce2f(%rip), %r10     # 0x4d6db4
movq %r11, 0x18(%rsp)
leaq 0xcccdf(%rip), %rax     # 0x4d6c70
movslq (%rax,%r9,4), %rax
leaq 0xccd74(%rip), %rdx     # 0x4d6d10
movl (%rdx,%r9,4), %ebx
leal 0x1(%rax), %edx
leal -0x1(%rax), %edi
leaq (%rsp,%rdi,4), %rdi
addq $0x20, %rdi
movl %ebx, %r11d
movl %ebx, %ebp
andl $0x80000001, %ebp       # imm = 0x80000001
xorl %r15d, %r15d
cmpl $0x1, %ebp
sete %r15b
movl %r15d, (%rdi)
shrl $0x1f, %r11d
addl %ebx, %r11d
sarl %r11d
decl %edx
addq $-0x4, %rdi
movl %r11d, %ebx
cmpl $0x1, %edx
jg 0x409fb1
cmpl $0x2, %eax
movl $0x1, %r12d
cmovgel %eax, %r12d
movq %r12, %rdx
shlq $0x5, %rdx
leaq (%rdx,%rdx,4), %rdx
xorl %edi, %edi
movl (%r10,%rdi), %r11d
movl %r11d, (%r8,%rdi)
addq $0xa0, %rdi
cmpq %rdi, %rdx
jne 0x409ff8
cmpl $0x1e, %eax
movl $0x1d, %ebp
cmovgel %eax, %ebp
incl %ebp
movq %rax, %r14
shlq $0x20, %r14
addq %rcx, %r14
movq %rax, %r15
movq %r15, %rdx
subq %rax, %rdx
leaq (%rdx,%rdx,4), %rdx
shlq $0x5, %rdx
addq %rsi, %rdx
movl (%rdx,%r9,4), %ebx
movl $0x1, %edx
movq %r14, %r11
xorl %edi, %edi
addl %edx, %edx
cmpl $0x0, 0x20(%rsp,%rdi,4)
je 0x40a06d
movq %r11, %r13
sarq $0x20, %r13
leaq (%r13,%r13,4), %r13
shlq $0x5, %r13
addq %rsi, %r13
movl (%r13,%r9,4), %r13d
imull %edx, %r13d
xorl %r13d, %ebx
incq %rdi
addq %rcx, %r11
cmpq %rdi, %r12
jne 0x40a045
leaq (%r15,%r15,4), %rdx
shlq $0x5, %rdx
addq %rsi, %rdx
movl %ebx, (%rdx,%r9,4)
incq %r15
movabsq $0x100000000, %rdx      # imm = 0x100000000
addq %rdx, %r14
cmpl %r15d, %ebp
jne 0x40a026
incq %r9
addq $0x4, %r10
addq $0x4, %r8
movq 0x18(%rsp), %r11
cmpq %r11, %r9
jne 0x409f8a
movq 0x8(%rsp), %rdi
leaq 0x1230(%rdi), %rcx
movl $0x1, %r8d
movl $0x1c, %esi
movq 0x10(%rsp), %rdx
addl %r8d, %r8d
testl %edx, %edx
je 0x40a0ef
xorl %r9d, %r9d
movl (%rcx,%r9,4), %eax
imull %r8d, %eax
movl %eax, (%rcx,%r9,4)
incq %r9
cmpq %r9, %r11
jne 0x40a0db
addq $-0xa0, %rcx
subq $0x1, %rsi
jae 0x40a0d1
movabsq $0x3e10000000000000, %rax # imm = 0x3E10000000000000
movq %rax, 0x8(%rdi)
movl $0x0, (%rdi)
testl %edx, %edx
je 0x40a126
addq $0x10, %rdi
shll $0x2, %edx
xorl %ebx, %ebx
xorl %esi, %esi
callq 0x4010e0
jmp 0x40a128
xorl %ebx, %ebx
movl %ebx, %eax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq