// File: /workspace/github_cpp_makefile_star2_O2/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x58, %rsp
movq %r8, 0x10(%rsp)
movq %rcx, %rbx
movq %rdx, %r15
andq $0x0, (%rdx)
andq $0x0, (%rcx)
testq %rsi, %rsi
je 0x1e205
movq %rsi, %r13
movq %rdi, %r12
xorpd %xmm1, %xmm1
movsd %xmm0, 0x18(%rsp)
callq 0x241f5
testb %al, %al
je 0x1e212
andq $0x0, (%r15)
xorpd %xmm0, %xmm0
jmp 0x1e4d6
xorl %edi, %edi
cmpq $0x20000000, %r13       # imm = 0x20000000
setl %dil
leaq 0x14f73(%rip), %rsi     # 0x33199
movq 0x10(%rsp), %r14
movq %r14, %rdx
callq 0x238ca
movsd 0x121bd(%rip), %xmm0    # 0x303f8
movq %r14, %rdi
callq 0x2455f
movsd %xmm0, 0x8(%rsp)
movsd 0x1259f(%rip), %xmm1    # 0x307f0
movsd 0x18(%rsp), %xmm0
mulsd %xmm0, %xmm1
movsd %xmm1, (%rbx)
movq %r14, %rdi
callq 0x2455f
divsd 0x8(%rsp), %xmm0
movq %r14, %rdi
callq 0x24453
negq %rax
movsd 0x12178(%rip), %xmm0    # 0x303f8
movq %rax, %rdi
movq %r14, %rsi
callq 0x23100
movsd %xmm0, (%rsp)
mulsd 0x18(%rsp), %xmm0
movsd 0x11f5a(%rip), %xmm1    # 0x301f8
callq 0x24295
testb %al, %al
je 0x1e2b6
movsd (%rsp), %xmm0
mulsd 0x121d4(%rip), %xmm0    # 0x30488
jmp 0x1e28b
movsd (%rsp), %xmm0
movsd %xmm0, (%rsp)
mulsd 0x18(%rsp), %xmm0
movsd 0x121ba(%rip), %xmm1    # 0x30488
callq 0x24239
testb %al, %al
je 0x1e2e2
movsd (%rsp), %xmm0
addsd %xmm0, %xmm0
jmp 0x1e2bb
movq %rbx, 0x38(%rsp)
movq 0x28(%r12), %rbp
leaq -0x1(%r13), %rsi
xorl %ebx, %ebx
xorl %edi, %edi
movq %rsi, 0x30(%rsp)
callq 0x26b4d
pushq $0x1
popq %rsi
movq %rbp, %rdi
movq %rax, %rdx
movsd (%rsp), %xmm0
callq 0x26b08
cvtsi2sd %r13, %xmm1
movsd 0x14772(%rip), %xmm0    # 0x32a90
movsd %xmm1, 0x40(%rsp)
divsd %xmm1, %xmm0
movq 0x10(%rsp), %r14
movq %r14, %rdi
callq 0x2455f
divsd 0x8(%rsp), %xmm0
movq %r14, %rdi
callq 0x24475
movsd 0x120ad(%rip), %xmm0    # 0x303f8
movq %rax, %rdi
movq %r14, %rsi
callq 0x23100
movsd %xmm0, 0x8(%rsp)
movsd 0x12094(%rip), %xmm1    # 0x303f8
callq 0x24239
movsd 0x12087(%rip), %xmm0    # 0x303f8
testb %al, %al
jne 0x1e37b
movsd 0x8(%rsp), %xmm0
movsd %xmm0, 0x28(%rsp)
movsd 0x11e6f(%rip), %xmm1    # 0x301f8
movapd %xmm1, %xmm2
divsd (%rsp), %xmm2
movsd %xmm2, 0x20(%rsp)
divsd %xmm0, %xmm1
movsd %xmm1, 0x48(%rsp)
andq $0x0, (%r15)
movq 0x28(%r12), %rbp
xorl %edi, %edi
movq 0x30(%rsp), %rsi
callq 0x26b4d
movq %rbp, %rdi
pushq $0x1
popq %rsi
movq %rax, %rdx
movsd 0x28(%rsp), %xmm0
callq 0x26b08
testq %r13, %r13
cmovgq %r13, %rbx
movq %r15, 0x50(%rsp)
movb $0x1, %bpl
xorl %r15d, %r15d
xorl %r14d, %r14d
cmpq %r14, %rbx
je 0x1e440
movq 0x28(%r12), %rax
movsd (%rax,%r14,8), %xmm0
movsd %xmm0, (%rsp)
movq 0x10(%rsp), %rdi
callq 0x24475
movq %rax, %r13
cvtsi2sd %rax, %xmm1
movsd %xmm1, 0x8(%rsp)
movsd (%rsp), %xmm0
callq 0x24217
xorb $0x1, %al
andb %al, %bpl
movsd (%rsp), %xmm0
subsd 0x8(%rsp), %xmm0
mulsd 0x28(%rsp), %xmm0
movq 0x28(%r12), %rax
movsd %xmm0, (%rax,%r14,8)
addq %r13, %r15
incq %r14
jmp 0x1e3e0
movsd 0x20(%rsp), %xmm1
mulsd 0x48(%rsp), %xmm1
cvtsi2sd %r15, %xmm0
movsd %xmm1, 0x20(%rsp)
mulsd %xmm1, %xmm0
movq 0x50(%rsp), %r15
addsd (%r15), %xmm0
movsd %xmm0, (%r15)
movq 0x10(%rsp), %rdi
callq 0x2440c
testb $0x1, %bpl
jne 0x1e49d
movsd 0x20(%rsp), %xmm0
mulsd 0x40(%rsp), %xmm0
movsd 0x18(%rsp), %xmm1
addsd %xmm1, %xmm0
callq 0x241f5
testb %al, %al
je 0x1e3d7
movq 0x38(%rsp), %rbx
movsd (%rbx), %xmm0
movsd %xmm0, (%rsp)
movsd (%r15), %xmm0
movq 0x10(%rsp), %r14
movq %r14, %rdi
callq 0x2440c
movapd %xmm0, %xmm1
mulsd 0x12327(%rip), %xmm1    # 0x307f0
movsd (%rsp), %xmm0
movq %r14, %rdi
callq 0x244b6
movsd %xmm0, (%rbx)
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq