// File: /workspace/github_cpp_makefile_star2_O3/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 %rdx, %r15
xorl %eax, %eax
movq %rax, (%rdx)
movq %rax, (%rcx)
testq %rsi, %rsi
je 0x1f817
movq %r8, %rbx
movq %rsi, %r12
movq %rdi, %r13
movq %rcx, 0x30(%rsp)
xorpd %xmm1, %xmm1
movsd %xmm0, 0x18(%rsp)
callq 0x25b75
testb %al, %al
je 0x1f826
movq $0x0, (%r15)
movq 0x30(%rsp), %rcx
jmp 0x1f81e
movq $0x0, (%r15)
xorps %xmm0, %xmm0
jmp 0x1fb6b
xorl %edi, %edi
cmpq $0x20000000, %r12       # imm = 0x20000000
setl %dil
leaq 0x1696f(%rip), %rsi     # 0x361a9
movq %rbx, %rdx
callq 0x2510e
movsd 0x13bb6(%rip), %xmm0    # 0x33400
movq %rbx, %rdi
callq 0x25f91
movsd %xmm0, 0x20(%rsp)
movsd 0x13f90(%rip), %xmm1    # 0x337f0
movsd 0x18(%rsp), %xmm0
mulsd %xmm0, %xmm1
movq 0x30(%rsp), %rax
movsd %xmm1, (%rax)
movq %rbx, %rdi
callq 0x25f91
divsd 0x20(%rsp), %xmm0
movq %rbx, %rdi
callq 0x25e79
negq %rax
movsd 0x13b6c(%rip), %xmm0    # 0x33400
movq %rax, %rdi
movq %rbx, %rsi
callq 0x2486a
movsd %xmm0, 0x8(%rsp)
mulsd 0x18(%rsp), %xmm0
movsd 0x1392d(%rip), %xmm1    # 0x331e0
movsd %xmm0, 0x10(%rsp)
callq 0x25c15
testb %al, %al
je 0x1f8fb
movsd 0x8(%rsp), %xmm0
mulsd 0x13bb8(%rip), %xmm0    # 0x33488
movsd %xmm0, 0x8(%rsp)
mulsd 0x18(%rsp), %xmm0
movsd %xmm0, 0x10(%rsp)
movsd 0x138f6(%rip), %xmm1    # 0x331e0
callq 0x25c15
movsd 0x8(%rsp), %xmm0
testb %al, %al
jne 0x1f8c8
jmp 0x1f901
movsd 0x8(%rsp), %xmm0
movsd %xmm0, 0x8(%rsp)
movsd 0x13b79(%rip), %xmm1    # 0x33488
movsd 0x10(%rsp), %xmm0
callq 0x25bb9
testb %al, %al
movq %rbx, 0x38(%rsp)
je 0x1f94a
movsd 0x8(%rsp), %xmm0
addsd %xmm0, %xmm0
movsd %xmm0, 0x8(%rsp)
mulsd 0x18(%rsp), %xmm0
movsd 0x13b47(%rip), %xmm1    # 0x33488
callq 0x25bb9
testb %al, %al
jne 0x1f923
movq 0x28(%r13), %rbx
leaq -0x1(%r12), %rbp
xorl %edi, %edi
movq %rbp, %rsi
callq 0x2897c
movl $0x1, %esi
movq %rbx, %rdi
movq %rax, %rdx
movsd 0x8(%rsp), %xmm0
callq 0x28939
xorps %xmm1, %xmm1
cvtsi2sd %r12, %xmm1
movsd 0x1611d(%rip), %xmm0    # 0x35aa0
movsd %xmm1, 0x40(%rsp)
divsd %xmm1, %xmm0
movq 0x38(%rsp), %rbx
movq %rbx, %rdi
callq 0x25f91
divsd 0x20(%rsp), %xmm0
movq %rbx, %rdi
callq 0x25e9b
movsd 0x13a50(%rip), %xmm0    # 0x33400
movq %rax, %rdi
movq %rbx, %rsi
callq 0x2486a
movsd %xmm0, 0x10(%rsp)
movsd 0x13a37(%rip), %xmm1    # 0x33400
callq 0x25bb9
movsd 0x13a2a(%rip), %xmm0    # 0x33400
testb %al, %al
jne 0x1f9e0
movsd 0x10(%rsp), %xmm0
movsd %xmm0, 0x20(%rsp)
movsd 0x137f2(%rip), %xmm1    # 0x331e0
movapd %xmm1, %xmm2
divsd 0x8(%rsp), %xmm2
movsd %xmm2, 0x28(%rsp)
divsd %xmm0, %xmm1
movsd %xmm1, 0x50(%rsp)
movq $0x0, (%r15)
movq 0x28(%r13), %rbx
xorl %edi, %edi
movq %rbp, %rsi
callq 0x2897c
movl $0x1, %esi
movq %rbx, %rdi
movq %rax, %rdx
movsd 0x20(%rsp), %xmm0
callq 0x28939
movq 0x38(%rsp), %rbx
movq %r15, 0x48(%rsp)
movsd 0x28(%rsp), %xmm0
mulsd 0x50(%rsp), %xmm0
testq %r12, %r12
jle 0x1fb15
movsd %xmm0, 0x28(%rsp)
movb $0x1, %bpl
xorl %r14d, %r14d
xorl %r15d, %r15d
movq 0x28(%r13), %rax
movsd (%rax,%r14,8), %xmm0
movsd %xmm0, 0x8(%rsp)
movq %rbx, %rdi
callq 0x25e9b
movq %rax, %rbx
xorps %xmm1, %xmm1
cvtsi2sd %rax, %xmm1
movsd %xmm1, 0x10(%rsp)
movsd 0x8(%rsp), %xmm0
callq 0x25b97
xorb $0x1, %al
andb %al, %bpl
movsd 0x8(%rsp), %xmm0
subsd 0x10(%rsp), %xmm0
mulsd 0x20(%rsp), %xmm0
movq 0x28(%r13), %rax
movsd %xmm0, (%rax,%r14,8)
addq %rbx, %r15
movq 0x38(%rsp), %rbx
incq %r14
cmpq %r14, %r12
jne 0x1fa61
xorps %xmm0, %xmm0
cvtsi2sd %r15, %xmm0
mulsd 0x28(%rsp), %xmm0
movq 0x48(%rsp), %r15
addsd (%r15), %xmm0
movsd %xmm0, (%r15)
movq %rbx, %rdi
callq 0x25e30
testb %bpl, %bpl
jne 0x1fb2f
movsd 0x28(%rsp), %xmm0
mulsd 0x40(%rsp), %xmm0
movsd 0x18(%rsp), %xmm1
addsd %xmm1, %xmm0
callq 0x25b75
testb %al, %al
je 0x1fa3d
jmp 0x1fb2f
xorpd %xmm1, %xmm1
mulsd %xmm1, %xmm0
addsd (%r15), %xmm0
movsd %xmm0, (%r15)
movq %rbx, %rdi
callq 0x25e30
movq %rbx, %r14
movq 0x30(%rsp), %rbx
movsd (%rbx), %xmm0
movsd %xmm0, 0x8(%rsp)
movsd (%r15), %xmm0
movq %r14, %rdi
callq 0x25e30
movapd %xmm0, %xmm1
mulsd 0x13c96(%rip), %xmm1    # 0x337f0
movsd 0x8(%rsp), %xmm0
movq %r14, %rdi
callq 0x25edc
movq %rbx, %rcx
movsd %xmm0, (%rcx)
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq