// File: /workspace/github_cpp_makefile_star2_O2/btbujiangjun[P]abcdl/include/utils/MnistHelper.h
// Function: abcdl::utils::MnistHelper<float>::read_image(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>> const&, abcdl::algebra::Matrix<float>*, int, unsigned long)
bool MnistHelper<T>::read_image(const std::string& image_file,
                                abcdl::algebra::Matrix<T>* out_mat,
                                const int limit,
                                const size_t threshold){

    size_t count = 0;
    auto image_buffer = read_mnist_file(image_file, 0x803, &count);

    if(!image_buffer || count == 0){
        return false;
    }

    if( limit > 0 && limit < (int)count){
        count = limit;
    }

    //read image data
    auto rows = read_header(image_buffer, 2);
    auto cols = read_header(image_buffer, 3);

    auto image_data_buffer = reinterpret_cast<unsigned char*>(image_buffer.get() + 16);

    size_t size = count * rows * cols;
    T* data = new T[size];
    for(size_t i = 0; i < size; i++){
        if(threshold > 0){
            data[i] = static_cast<T>(*image_data_buffer++) > threshold ? 1 : 0;
        }else{
            data[i] = static_cast<T>(*image_data_buffer++) / 255;
        }
    }

    out_mat->set_shallow_data(data, count, rows * cols);

    return true;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movq %r8, %r14
movl %ecx, %r12d
movq %rdx, 0x18(%rsp)
movq %rsi, %rdx
movq %rdi, %rsi
leaq 0x20(%rsp), %r15
andq $0x0, (%r15)
leaq 0x10(%rsp), %r13
movq %r13, %rdi
movl $0x803, %ecx            # imm = 0x803
movq %r15, %r8
callq 0x10f68
movq (%r13), %r13
testq %r13, %r13
setne %al
movq (%r15), %rdx
testq %rdx, %rdx
setne %bpl
andb %al, %bpl
cmpb $0x1, %bpl
jne 0x13736
movb %bpl, 0xf(%rsp)
testl %r12d, %r12d
setg %al
cmpl %r12d, %edx
setg %cl
andb %al, %cl
cmpb $0x1, %cl
jne 0x13660
movl %r12d, %edx
movq %rdx, 0x20(%rsp)
movl 0x8(%r13), %r12d
movl 0xc(%r13), %ebx
movl $0xffffff, %eax         # imm = 0xFFFFFF
movl %r12d, %ecx
andl %eax, %ecx
bswapl %ecx
sarl $0x18, %r12d
orl %ecx, %r12d
andl %ebx, %eax
bswapl %eax
sarl $0x18, %ebx
orl %eax, %ebx
movslq %r12d, %rax
imulq %rdx, %rax
movslq %ebx, %r15
imulq %rax, %r15
movq %r15, %rax
shrq $0x3e, %rax
leaq (,%r15,4), %rcx
xorl %edi, %edi
negq %rax
sbbq %rdi, %rdi
orq %rcx, %rdi
movq %rdx, %rbp
callq 0x9030
movq %rbp, %rdx
movb 0xf(%rsp), %bpl
testq %r14, %r14
js 0x136c8
cvtsi2ss %r14, %xmm0
jmp 0x136e0
movq %r14, %rcx
shrq %rcx
movl %r14d, %esi
andl $0x1, %esi
orq %rcx, %rsi
cvtsi2ss %rsi, %xmm0
addss %xmm0, %xmm0
xorl %ecx, %ecx
movss 0x1c91e(%rip), %xmm1    # 0x30008
movss 0x1c91a(%rip), %xmm2    # 0x3000c
cmpq %rcx, %r15
je 0x13722
movzbl 0x10(%r13,%rcx), %esi
cvtsi2ss %esi, %xmm4
movaps %xmm4, %xmm3
divss %xmm1, %xmm3
testq %r14, %r14
je 0x13718
movaps %xmm0, %xmm3
cmpltss %xmm4, %xmm3
andps %xmm2, %xmm3
movss %xmm3, (%rax,%rcx,4)
incq %rcx
jmp 0x136f2
imull %r12d, %ebx
movslq %ebx, %rcx
movq 0x18(%rsp), %rdi
movq %rax, %rsi
callq 0x1823e
leaq 0x10(%rsp), %rdi
callq 0x111be
movl %ebp, %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
leaq 0x10(%rsp), %rdi
callq 0x111be
movq %rbx, %rdi
callq 0x95e0