// File: mouda[P]LTE_random_access-simulate/alglibinternal.cpp
// Function: alglib_impl::xblas_xsum(alglib_impl::ae_vector*, double, long, double*, double*, alglib_impl::ae_state*)
static void xblas_xsum(/* Real    */ ae_vector* w,
     double mx,
     ae_int_t n,
     double* r,
     double* rerr,
     ae_state *_state)
{
    ae_int_t i;
    ae_int_t k;
    ae_int_t ks;
    double v;
    double s;
    double ln2;
    double chunk;
    double invchunk;
    ae_bool allzeros;

    *r = 0;
    *rerr = 0;

    
    /*
     * special cases:
     * * N=0
     * * N is too large to use integer arithmetics
     */
    if( n==0 )
    {
        *r = 0;
        *rerr = 0;
        return;
    }
    if( ae_fp_eq(mx,0) )
    {
        *r = 0;
        *rerr = 0;
        return;
    }
    ae_assert(n<536870912, "XDot: N is too large!", _state);
    
    /*
     * Prepare
     */
    ln2 = ae_log(2, _state);
    *rerr = mx*ae_machineepsilon;
    
    /*
     * 1. find S such that 0.5<=S*MX<1
     * 2. multiply W by S, so task is normalized in some sense
     * 3. S:=1/S so we can obtain original vector multiplying by S
     */
    k = ae_round(ae_log(mx, _state)/ln2, _state);
    s = xblas_xfastpow(2, -k, _state);
    while(ae_fp_greater_eq(s*mx,1))
    {
        s = 0.5*s;
    }
    while(ae_fp_less(s*mx,0.5))
    {
        s = 2*s;
    }
    ae_v_muld(&w->ptr.p_double[0], 1, ae_v_len(0,n-1), s);
    s = 1/s;
    
    /*
     * find Chunk=2^M such that N*Chunk<2^29
     *
     * we have chosen upper limit (2^29) with enough space left
     * to tolerate possible problems with rounding and N's close
     * to the limit, so we don't want to be very strict here.
     */
    k = ae_trunc(ae_log((double)536870912/(double)n, _state)/ln2, _state);
    chunk = xblas_xfastpow(2, k, _state);
    if( ae_fp_less(chunk,2) )
    {
        chunk = 2;
    }
    invchunk = 1/chunk;
    
    /*
     * calculate result
     */
    *r = 0;
    ae_v_muld(&w->ptr.p_double[0], 1, ae_v_len(0,n-1), chunk);
    for(;;)
    {
        s = s*invchunk;
        allzeros = ae_true;
        ks = 0;
        for(i=0; i<=n-1; i++)
        {
            v = w->ptr.p_double[i];
            k = ae_trunc(v, _state);
            if( ae_fp_neq(v,k) )
            {
                allzeros = ae_false;
            }
            w->ptr.p_double[i] = chunk*(v-k);
            ks = ks+k;
        }
        *r = *r+s*ks;
        v = ae_fabs(*r, _state);
        if( allzeros||ae_fp_eq(s*n+mx,mx) )
        {
            break;
        }
    }
    
    /*
     * correct error
     */
    *rerr = ae_maxreal(*rerr, ae_fabs(*r, _state)*ae_machineepsilon, _state);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x90, %rsp
movq %rdi, -0x8(%rbp)
movsd %xmm0, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq %rdx, -0x20(%rbp)
movq %rcx, -0x28(%rbp)
movq %r8, -0x30(%rbp)
movq -0x20(%rbp), %rax
xorps %xmm0, %xmm0
movsd %xmm0, (%rax)
movq -0x28(%rbp), %rax
xorps %xmm0, %xmm0
movsd %xmm0, (%rax)
cmpq $0x0, -0x18(%rbp)
jne 0x30d3c
movq -0x20(%rbp), %rax
xorps %xmm0, %xmm0
movsd %xmm0, (%rax)
movq -0x28(%rbp), %rax
xorps %xmm0, %xmm0
movsd %xmm0, (%rax)
jmp 0x310d6
movsd -0x10(%rbp), %xmm0
xorps %xmm1, %xmm1
callq 0x3c3d0
testb $0x1, %al
jne 0x30d4f
jmp 0x30d6a
movq -0x20(%rbp), %rax
xorps %xmm0, %xmm0
movsd %xmm0, (%rax)
movq -0x28(%rbp), %rax
xorps %xmm0, %xmm0
movsd %xmm0, (%rax)
jmp 0x310d6
cmpq $0x20000000, -0x18(%rbp) # imm = 0x20000000
setl %al
movq -0x30(%rbp), %rdx
movzbl %al, %edi
andl $0x1, %edi
leaq 0x2716f(%rip), %rsi     # 0x57ef5
callq 0x3b240
movq -0x30(%rbp), %rdi
movsd 0x24631(%rip), %xmm0    # 0x553c8
callq 0x3cd70
movsd %xmm0, -0x60(%rbp)
movsd 0x24b6f(%rip), %xmm0    # 0x55918
mulsd -0x10(%rbp), %xmm0
movq -0x28(%rbp), %rax
movsd %xmm0, (%rax)
movsd -0x10(%rbp), %xmm0
movq -0x30(%rbp), %rdi
callq 0x3cd70
divsd -0x60(%rbp), %xmm0
movq -0x30(%rbp), %rdi
callq 0x3c910
movq %rax, -0x40(%rbp)
xorl %eax, %eax
movl %eax, %edi
subq -0x40(%rbp), %rdi
movq -0x30(%rbp), %rsi
movsd 0x245de(%rip), %xmm0    # 0x553c8
callq 0x3a1a0
movsd %xmm0, -0x58(%rbp)
movsd -0x58(%rbp), %xmm0
mulsd -0x10(%rbp), %xmm0
movsd 0x24202(%rip), %xmm1    # 0x55008
callq 0x3c500
testb $0x1, %al
jne 0x30e11
jmp 0x30e25
movsd 0x24637(%rip), %xmm0    # 0x55450
mulsd -0x58(%rbp), %xmm0
movsd %xmm0, -0x58(%rbp)
jmp 0x30df4
jmp 0x30e27
movsd -0x58(%rbp), %xmm0
mulsd -0x10(%rbp), %xmm0
movsd 0x24617(%rip), %xmm1    # 0x55450
callq 0x3c440
testb $0x1, %al
jne 0x30e44
jmp 0x30e58
movsd 0x2457c(%rip), %xmm0    # 0x553c8
mulsd -0x58(%rbp), %xmm0
movsd %xmm0, -0x58(%rbp)
jmp 0x30e27
movq -0x8(%rbp), %rax
movq 0x28(%rax), %rax
movq %rax, -0x80(%rbp)
movq -0x18(%rbp), %rsi
subq $0x1, %rsi
xorl %eax, %eax
movl %eax, %edi
callq 0x42370
movq -0x80(%rbp), %rdi
movq %rax, %rdx
movsd -0x58(%rbp), %xmm0
movl $0x1, %esi
callq 0x422d0
movsd 0x24175(%rip), %xmm0    # 0x55008
divsd -0x58(%rbp), %xmm0
movsd %xmm0, -0x58(%rbp)
cvtsi2sdq -0x18(%rbp), %xmm1
movsd 0x269c5(%rip), %xmm0    # 0x57870
divsd %xmm1, %xmm0
movq -0x30(%rbp), %rdi
callq 0x3cd70
divsd -0x60(%rbp), %xmm0
movq -0x30(%rbp), %rdi
callq 0x3c970
movq %rax, -0x40(%rbp)
movq -0x40(%rbp), %rdi
movq -0x30(%rbp), %rsi
movsd 0x244ee(%rip), %xmm0    # 0x553c8
callq 0x3a1a0
movsd %xmm0, -0x68(%rbp)
movsd -0x68(%rbp), %xmm0
movsd 0x244d7(%rip), %xmm1    # 0x553c8
callq 0x3c440
testb $0x1, %al
jne 0x30efc
jmp 0x30f09
movsd 0x244c4(%rip), %xmm0    # 0x553c8
movsd %xmm0, -0x68(%rbp)
movsd 0x240f7(%rip), %xmm0    # 0x55008
divsd -0x68(%rbp), %xmm0
movsd %xmm0, -0x70(%rbp)
movq -0x20(%rbp), %rax
xorps %xmm0, %xmm0
movsd %xmm0, (%rax)
movq -0x8(%rbp), %rax
movq 0x28(%rax), %rax
movq %rax, -0x88(%rbp)
movq -0x18(%rbp), %rsi
subq $0x1, %rsi
xorl %eax, %eax
movl %eax, %edi
callq 0x42370
movq -0x88(%rbp), %rdi
movq %rax, %rdx
movsd -0x68(%rbp), %xmm0
movl $0x1, %esi
callq 0x422d0
movsd -0x58(%rbp), %xmm0
mulsd -0x70(%rbp), %xmm0
movsd %xmm0, -0x58(%rbp)
movb $0x1, -0x71(%rbp)
movq $0x0, -0x48(%rbp)
movq $0x0, -0x38(%rbp)
movq -0x38(%rbp), %rax
movq -0x18(%rbp), %rcx
subq $0x1, %rcx
cmpq %rcx, %rax
jg 0x3101f
movq -0x8(%rbp), %rax
movq 0x28(%rax), %rax
movq -0x38(%rbp), %rcx
movsd (%rax,%rcx,8), %xmm0
movsd %xmm0, -0x50(%rbp)
movsd -0x50(%rbp), %xmm0
movq -0x30(%rbp), %rdi
callq 0x3c970
movq %rax, -0x40(%rbp)
movsd -0x50(%rbp), %xmm0
cvtsi2sdq -0x40(%rbp), %xmm1
callq 0x3c410
testb $0x1, %al
jne 0x30fd5
jmp 0x30fd9
movb $0x0, -0x71(%rbp)
movsd -0x68(%rbp), %xmm0
movsd -0x50(%rbp), %xmm1
cvtsi2sdq -0x40(%rbp), %xmm2
subsd %xmm2, %xmm1
mulsd %xmm1, %xmm0
movq -0x8(%rbp), %rax
movq 0x28(%rax), %rax
movq -0x38(%rbp), %rcx
movsd %xmm0, (%rax,%rcx,8)
movq -0x48(%rbp), %rax
addq -0x40(%rbp), %rax
movq %rax, -0x48(%rbp)
movq -0x38(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x38(%rbp)
jmp 0x30f82
movq -0x20(%rbp), %rax
movsd (%rax), %xmm1
movsd -0x58(%rbp), %xmm0
cvtsi2sdq -0x48(%rbp), %xmm2
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movq -0x20(%rbp), %rax
movsd %xmm0, (%rax)
movq -0x20(%rbp), %rax
movsd (%rax), %xmm0
movq -0x30(%rbp), %rdi
callq 0x3c820
movsd %xmm0, -0x50(%rbp)
testb $0x1, -0x71(%rbp)
jne 0x31086
movsd -0x58(%rbp), %xmm0
cvtsi2sdq -0x18(%rbp), %xmm2
movsd -0x10(%rbp), %xmm1
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd -0x10(%rbp), %xmm1
callq 0x3c3d0
testb $0x1, %al
jne 0x31086
jmp 0x31088
jmp 0x3108d
jmp 0x30f5f
movq -0x28(%rbp), %rax
movsd (%rax), %xmm0
movsd %xmm0, -0x90(%rbp)
movq -0x20(%rbp), %rax
movsd (%rax), %xmm0
movq -0x30(%rbp), %rdi
callq 0x3c820
movaps %xmm0, %xmm1
movsd -0x90(%rbp), %xmm0
movsd 0x24857(%rip), %xmm2    # 0x55918
mulsd %xmm2, %xmm1
movq -0x30(%rbp), %rdi
callq 0x3ca70
movq -0x28(%rbp), %rax
movsd %xmm0, (%rax)
addq $0x90, %rsp
popq %rbp
retq
nop