// File: ashlall[P]StreamStats/ChiSquareExperiments/./../SourceCode/ChiSquare/ChiSquareContinuous.cpp
// Function: ChiSquareContinuous::calculate_statistic(int, double (ChiSquareContinuous::*)(double, double, double), double, double)
double ChiSquareContinuous::calculate_statistic(int num_buckets, double(ChiSquareContinuous::*distribution)(double, double, double), double location, double scale)
{		
  int stream_size = quantile_sketch->get_stream_size();
  double expected_frequency = (double)stream_size/num_buckets;

  // Reallocates the memory for upper_bins and lower_bins
  delete upper_bins;
  delete lower_bins;
  upper_bins = new double[num_buckets+1];
  lower_bins = new double[num_buckets+1];

  for (int i = 1; i <= num_buckets; i++)
  {
    // Finds the upper and lower values from distribution that mark the bin.
    double lower_interval = (this->*distribution)(((double)i-1)/num_buckets, location, scale);
    double upper_interval = (this->*distribution)((double)i/num_buckets, location, scale);

    upper_bins[i] = upper_interval;
    lower_bins[i] = lower_interval;

    // Finds the upper and lower quantiles from quantile_sketch that correspond
    // to lower_interval and upper_interval.
    double lower_value, upper_value;
    lower_value = (quantile_sketch->reverseQuantile(lower_interval, memory))/memory;
    upper_value = (quantile_sketch->reverseQuantile(upper_interval, memory))/memory;
		
    double observed_frequency = stream_size * (upper_value - lower_value);
    double lambda = fabs(observed_frequency - expected_frequency);
    chi_squared=chi_squared+ ((lambda*lambda)/ expected_frequency);
  }		
  return chi_squared;	
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0xd0, %rsp
movq %rdx, -0x10(%rbp)
movq %rcx, -0x8(%rbp)
movq -0x10(%rbp), %rax
movq -0x8(%rbp), %rcx
movq %rdi, -0x18(%rbp)
movl %esi, -0x1c(%rbp)
movq %rcx, -0x28(%rbp)
movq %rax, -0x30(%rbp)
movsd %xmm0, -0x38(%rbp)
movsd %xmm1, -0x40(%rbp)
movq -0x18(%rbp), %rax
movq %rax, -0x98(%rbp)
movq 0x20(%rax), %rdi
movq (%rdi), %rax
callq *0x10(%rax)
movq %rax, %rcx
movq -0x98(%rbp), %rax
movl %ecx, -0x44(%rbp)
cvtsi2sdl -0x44(%rbp), %xmm0
cvtsi2sdl -0x1c(%rbp), %xmm1
divsd %xmm1, %xmm0
movsd %xmm0, -0x50(%rbp)
movq 0x10(%rax), %rax
movq %rax, -0x90(%rbp)
cmpq $0x0, %rax
je 0x5fc6
movq -0x90(%rbp), %rdi
callq 0x21a0
movq -0x98(%rbp), %rax
movq 0x18(%rax), %rax
movq %rax, -0xa0(%rbp)
cmpq $0x0, %rax
je 0x5fea
movq -0xa0(%rbp), %rdi
callq 0x21a0
movl -0x1c(%rbp), %eax
addl $0x1, %eax
cltq
movl $0x8, %ecx
mulq %rcx
movq %rax, %rdi
seto %cl
movq $-0x1, %rax
testb $0x1, %cl
cmovneq %rax, %rdi
callq 0x2030
movq %rax, %rcx
movq -0x98(%rbp), %rax
movq %rcx, 0x10(%rax)
movl -0x1c(%rbp), %eax
addl $0x1, %eax
cltq
movl $0x8, %ecx
mulq %rcx
movq %rax, %rdi
seto %cl
movq $-0x1, %rax
testb $0x1, %cl
cmovneq %rax, %rdi
callq 0x2030
movq %rax, %rcx
movq -0x98(%rbp), %rax
movq %rcx, 0x18(%rax)
movl $0x1, -0x54(%rbp)
movl -0x54(%rbp), %eax
cmpl -0x1c(%rbp), %eax
jg 0x625c
movq -0x98(%rbp), %rcx
movq -0x30(%rbp), %rax
movq %rax, -0xb0(%rbp)
movq -0x28(%rbp), %rdx
addq %rdx, %rcx
movq %rcx, -0xa8(%rbp)
andq $0x1, %rax
cmpq $0x0, %rax
je 0x60b7
movq -0xb0(%rbp), %rcx
movq -0xa8(%rbp), %rax
movq (%rax), %rax
subq $0x1, %rcx
movq (%rax,%rcx), %rax
movq %rax, -0xb8(%rbp)
jmp 0x60c5
movq -0xb0(%rbp), %rax
movq %rax, -0xb8(%rbp)
movq -0xa8(%rbp), %rdi
movq -0xb8(%rbp), %rax
cvtsi2sdl -0x54(%rbp), %xmm0
movsd 0xc068(%rip), %xmm1     # 0x12148
addsd %xmm1, %xmm0
cvtsi2sdl -0x1c(%rbp), %xmm1
divsd %xmm1, %xmm0
movsd -0x38(%rbp), %xmm1
movsd -0x40(%rbp), %xmm2
callq *%rax
movq -0x98(%rbp), %rcx
movsd %xmm0, -0x60(%rbp)
movq -0x30(%rbp), %rax
movq %rax, -0xc8(%rbp)
movq -0x28(%rbp), %rdx
addq %rdx, %rcx
movq %rcx, -0xc0(%rbp)
andq $0x1, %rax
cmpq $0x0, %rax
je 0x614a
movq -0xc8(%rbp), %rcx
movq -0xc0(%rbp), %rax
movq (%rax), %rax
subq $0x1, %rcx
movq (%rax,%rcx), %rax
movq %rax, -0xd0(%rbp)
jmp 0x6158
movq -0xc8(%rbp), %rax
movq %rax, -0xd0(%rbp)
movq -0xc0(%rbp), %rdi
movq -0xd0(%rbp), %rax
cvtsi2sdl -0x54(%rbp), %xmm0
cvtsi2sdl -0x1c(%rbp), %xmm1
divsd %xmm1, %xmm0
movsd -0x38(%rbp), %xmm1
movsd -0x40(%rbp), %xmm2
callq *%rax
movq -0x98(%rbp), %rax
movsd %xmm0, -0x68(%rbp)
movsd -0x68(%rbp), %xmm0
movq 0x10(%rax), %rcx
movslq -0x54(%rbp), %rdx
movsd %xmm0, (%rcx,%rdx,8)
movsd -0x60(%rbp), %xmm0
movq 0x18(%rax), %rcx
movslq -0x54(%rbp), %rdx
movsd %xmm0, (%rcx,%rdx,8)
movq 0x20(%rax), %rdi
movsd -0x60(%rbp), %xmm0
cvttsd2si 0x8(%rax), %esi
callq 0x2b60
movq -0x98(%rbp), %rax
divsd 0x8(%rax), %xmm0
movsd %xmm0, -0x70(%rbp)
movq 0x20(%rax), %rdi
movsd -0x68(%rbp), %xmm0
cvttsd2si 0x8(%rax), %esi
callq 0x2b60
movq -0x98(%rbp), %rax
divsd 0x8(%rax), %xmm0
movsd %xmm0, -0x78(%rbp)
cvtsi2sdl -0x44(%rbp), %xmm0
movsd -0x78(%rbp), %xmm1
subsd -0x70(%rbp), %xmm1
mulsd %xmm1, %xmm0
movsd %xmm0, -0x80(%rbp)
movsd -0x80(%rbp), %xmm0
subsd -0x50(%rbp), %xmm0
movaps 0xbf6f(%rip), %xmm1     # 0x12190
pand %xmm1, %xmm0
movsd %xmm0, -0x88(%rbp)
movsd (%rax), %xmm0
movsd -0x88(%rbp), %xmm1
mulsd -0x88(%rbp), %xmm1
divsd -0x50(%rbp), %xmm1
addsd %xmm1, %xmm0
movsd %xmm0, (%rax)
movl -0x54(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x54(%rbp)
jmp 0x605f
movq -0x98(%rbp), %rax
movsd (%rax), %xmm0
addq $0xd0, %rsp
popq %rbp
retq