// File: /workspace/github_cpp_makefile_star2_O1/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, 0x18(%rsp)
movq %rdx, %r14
xorl %eax, %eax
movq %rax, (%rdx)
movq %rax, (%rcx)
testq %rsi, %rsi
je 0x1fe5e
movq %rsi, %r12
movq %rdi, %r13
movq %rcx, 0x30(%rsp)
xorpd %xmm1, %xmm1
movsd %xmm0, 0x10(%rsp)
callq 0x262ba
testb %al, %al
je 0x1fe6e
movq $0x0, (%r14)
movq 0x30(%rsp), %rcx
jmp 0x1fe65
movq $0x0, (%r14)
xorpd %xmm0, %xmm0
jmp 0x20195
xorl %edi, %edi
cmpq $0x20000000, %r12       # imm = 0x20000000
setl %dil
leaq 0x1719f(%rip), %rsi     # 0x37021
movq 0x18(%rsp), %rbx
movq %rbx, %rdx
callq 0x2581c
movsd 0x14549(%rip), %xmm0    # 0x343e0
movq %rbx, %rdi
callq 0x266d6
movsd %xmm0, 0x20(%rsp)
movsd 0x14a73(%rip), %xmm1    # 0x34920
movsd 0x10(%rsp), %xmm0
mulsd %xmm0, %xmm1
movq 0x30(%rsp), %rax
movsd %xmm1, (%rax)
movq %rbx, %rdi
callq 0x266d6
divsd 0x20(%rsp), %xmm0
movq %rbx, %rdi
callq 0x265be
negq %rax
movsd 0x144ff(%rip), %xmm0    # 0x343e0
movq %rax, %rdi
movq %rbx, %rsi
callq 0x25002
movsd %xmm0, 0x8(%rsp)
mulsd 0x10(%rsp), %xmm0
movsd 0x142a0(%rip), %xmm1    # 0x341a0
callq 0x2635a
testb %al, %al
je 0x1ff42
movsd 0x8(%rsp), %xmm0
mulsd 0x14551(%rip), %xmm0    # 0x34468
movsd %xmm0, 0x8(%rsp)
movsd 0x8(%rsp), %xmm0
mulsd 0x10(%rsp), %xmm0
movsd 0x1426f(%rip), %xmm1    # 0x341a0
callq 0x2635a
movsd 0x8(%rsp), %xmm0
testb %al, %al
jne 0x1ff0f
jmp 0x1ff54
movsd 0x8(%rsp), %xmm0
jmp 0x1ff54
movsd 0x8(%rsp), %xmm0
addsd %xmm0, %xmm0
movsd %xmm0, 0x8(%rsp)
mulsd 0x10(%rsp), %xmm0
movsd 0x14500(%rip), %xmm1    # 0x34468
callq 0x262fe
testb %al, %al
jne 0x1ff4a
movq 0x28(%r13), %rbx
leaq -0x1(%r12), %rbp
xorl %edi, %edi
movq %rbp, %rsi
callq 0x2923f
movl $0x1, %esi
movq %rbx, %rdi
movq %rax, %rdx
movsd 0x8(%rsp), %xmm0
callq 0x291fc
xorps %xmm1, %xmm1
cvtsi2sd %r12, %xmm1
movsd 0x16956(%rip), %xmm0    # 0x36900
movsd %xmm1, 0x40(%rsp)
divsd %xmm1, %xmm0
movq 0x18(%rsp), %rbx
movq %rbx, %rdi
callq 0x266d6
divsd 0x20(%rsp), %xmm0
movq %rbx, %rdi
callq 0x265e0
movsd 0x14409(%rip), %xmm0    # 0x343e0
movq %rax, %rdi
movq %rbx, %rsi
callq 0x25002
movsd %xmm0, 0x20(%rsp)
movsd 0x143f0(%rip), %xmm1    # 0x343e0
callq 0x262fe
movsd 0x143e3(%rip), %xmm0    # 0x343e0
testb %al, %al
jne 0x20007
movsd 0x20(%rsp), %xmm0
movsd %xmm0, 0x38(%rsp)
movsd 0x1418b(%rip), %xmm1    # 0x341a0
movapd %xmm1, %xmm2
divsd 0x8(%rsp), %xmm2
movsd %xmm2, 0x28(%rsp)
divsd %xmm0, %xmm1
movsd %xmm1, 0x50(%rsp)
movq $0x0, (%r14)
movq 0x28(%r13), %rbx
xorl %edi, %edi
movq %rbp, %rsi
callq 0x2923f
movl $0x1, %esi
movq %rbx, %rdi
movq %rax, %rdx
movsd 0x38(%rsp), %xmm0
callq 0x291fc
movq %r14, 0x48(%rsp)
movsd 0x28(%rsp), %xmm0
mulsd 0x50(%rsp), %xmm0
testq %r12, %r12
jle 0x2013b
movsd %xmm0, 0x28(%rsp)
movb $0x1, %bl
xorl %r14d, %r14d
xorl %r15d, %r15d
movq 0x28(%r13), %rax
movsd (%rax,%r14,8), %xmm0
movsd %xmm0, 0x8(%rsp)
movq 0x18(%rsp), %rdi
callq 0x265e0
movq %rax, %rbp
xorps %xmm1, %xmm1
cvtsi2sd %rax, %xmm1
movsd 0x8(%rsp), %xmm0
movsd %xmm1, 0x20(%rsp)
callq 0x262dc
xorl %ecx, %ecx
testb %al, %al
jne 0x200c1
movzbl %bl, %ecx
movl %ecx, %ebx
movsd 0x8(%rsp), %xmm0
subsd 0x20(%rsp), %xmm0
mulsd 0x38(%rsp), %xmm0
movq 0x28(%r13), %rax
movsd %xmm0, (%rax,%r14,8)
addq %rbp, %r15
incq %r14
cmpq %r14, %r12
jne 0x20082
xorps %xmm0, %xmm0
cvtsi2sd %r15, %xmm0
mulsd 0x28(%rsp), %xmm0
movq 0x48(%rsp), %r14
addsd (%r14), %xmm0
movsd %xmm0, (%r14)
movq 0x18(%rsp), %rdi
callq 0x26575
testb $0x1, %bl
jne 0x20157
movsd 0x28(%rsp), %xmm0
mulsd 0x40(%rsp), %xmm0
movsd 0x10(%rsp), %xmm1
addsd %xmm1, %xmm0
callq 0x262ba
testb %al, %al
je 0x2005f
jmp 0x20157
xorpd %xmm1, %xmm1
mulsd %xmm1, %xmm0
addsd (%r14), %xmm0
movsd %xmm0, (%r14)
movq 0x18(%rsp), %rdi
callq 0x26575
movq 0x30(%rsp), %rbx
movsd (%rbx), %xmm0
movsd %xmm0, 0x8(%rsp)
movsd (%r14), %xmm0
movq 0x18(%rsp), %r14
movq %r14, %rdi
callq 0x26575
movapd %xmm0, %xmm1
mulsd 0x1479c(%rip), %xmm1    # 0x34920
movsd 0x8(%rsp), %xmm0
movq %r14, %rdi
callq 0x26621
movq %rbx, %rcx
movsd %xmm0, (%rcx)
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq