// File: alan0201tw[P]RayTracer/src/aabb.cpp
// Function: aabb::hit(ray const&, float, float) const
bool aabb::hit(const ray& _ray, float t_min, float t_max) const
{
    for (int a = 0; a < 3; a++)
    {
        float t0 = ffmin((min[a] - _ray.origin()[a]) / _ray.direction()[a],
                        (max[a] - _ray.origin()[a]) / _ray.direction()[a]);
        float t1 = ffmax((min[a] - _ray.origin()[a]) / _ray.direction()[a],
                        (max[a] - _ray.origin()[a]) / _ray.direction()[a]);
        t_min = ffmax(t0, t_min);
        t_max = ffmin(t1, t_max);
        if (t_max <= t_min)
        {
            return false;
        }
    }
    return true;

    // Reference : Andrew Kensler at Pixar ( mentioned in Ray Tracing: The Next Week, page 11 )

    // for (int a = 0; a < 3; a++)
    // {
    //     float invD = 1.0f / _ray.direction()[a];
    //     float t0 = (min[a] - _ray.origin()[a]) * invD;
    //     float t1 = (max[a] - _ray.origin()[a]) * invD;

    //     if(invD < 0.0f)
    //     {
    //         std::swap(t0, t1);
    //     }

    //     t_min = t0 > t_min ? t0 : t_min;
    //     t_max = t1 > t_max ? t1 : t_max;
        
    //     if(t_max <= t_min)
    //     {
    //         return false;
    //     }
    // }
    // return true;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x170, %rsp            # imm = 0x170
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movss %xmm0, -0x1c(%rbp)
movss %xmm1, -0x20(%rbp)
movq -0x10(%rbp), %rax
movq %rax, -0x130(%rbp)
movl $0x0, -0x24(%rbp)
cmpl $0x3, -0x24(%rbp)
jge 0x1af7a
movq -0x130(%rbp), %rdi
movl -0x24(%rbp), %esi
callq 0x1b3e0
movss %xmm0, -0x164(%rbp)
movq -0x18(%rbp), %rdi
callq 0xf870
movss %xmm1, -0x40(%rbp)
movlpd %xmm0, -0x48(%rbp)
movl -0x40(%rbp), %eax
movl %eax, -0x30(%rbp)
movq -0x48(%rbp), %rax
movq %rax, -0x38(%rbp)
movl -0x24(%rbp), %esi
leaq -0x38(%rbp), %rdi
callq 0x1b400
movss -0x164(%rbp), %xmm0
movss (%rax), %xmm1
subss %xmm1, %xmm0
movss %xmm0, -0x160(%rbp)
movq -0x18(%rbp), %rdi
callq 0xf8b0
movss %xmm1, -0x60(%rbp)
movlpd %xmm0, -0x68(%rbp)
movl -0x60(%rbp), %eax
movl %eax, -0x50(%rbp)
movq -0x68(%rbp), %rax
movq %rax, -0x58(%rbp)
movl -0x24(%rbp), %esi
leaq -0x58(%rbp), %rdi
callq 0x1b400
movss -0x160(%rbp), %xmm0
movq -0x130(%rbp), %rdi
movss (%rax), %xmm1
divss %xmm1, %xmm0
movss %xmm0, -0x154(%rbp)
addq $0xc, %rdi
movq %rdi, -0x148(%rbp)
movl -0x24(%rbp), %esi
callq 0x1b3e0
movss %xmm0, -0x15c(%rbp)
movq -0x18(%rbp), %rdi
callq 0xf870
movss %xmm1, -0x80(%rbp)
movlpd %xmm0, -0x88(%rbp)
movl -0x80(%rbp), %eax
movl %eax, -0x70(%rbp)
movq -0x88(%rbp), %rax
movq %rax, -0x78(%rbp)
movl -0x24(%rbp), %esi
leaq -0x78(%rbp), %rdi
callq 0x1b400
movss -0x15c(%rbp), %xmm0
movss (%rax), %xmm1
subss %xmm1, %xmm0
movss %xmm0, -0x158(%rbp)
movq -0x18(%rbp), %rdi
callq 0xf8b0
movss %xmm1, -0xa0(%rbp)
movlpd %xmm0, -0xa8(%rbp)
movl -0xa0(%rbp), %eax
movl %eax, -0x90(%rbp)
movq -0xa8(%rbp), %rax
movq %rax, -0x98(%rbp)
movl -0x24(%rbp), %esi
leaq -0x98(%rbp), %rdi
callq 0x1b400
movss -0x158(%rbp), %xmm1
movss -0x154(%rbp), %xmm0
movss (%rax), %xmm2
divss %xmm2, %xmm1
callq 0x1b3a0
movq -0x130(%rbp), %rdi
movss %xmm0, -0x28(%rbp)
movl -0x24(%rbp), %esi
callq 0x1b3e0
movss %xmm0, -0x150(%rbp)
movq -0x18(%rbp), %rdi
callq 0xf870
movss %xmm1, -0xc0(%rbp)
movlpd %xmm0, -0xc8(%rbp)
movl -0xc0(%rbp), %eax
movl %eax, -0xb0(%rbp)
movq -0xc8(%rbp), %rax
movq %rax, -0xb8(%rbp)
movl -0x24(%rbp), %esi
leaq -0xb8(%rbp), %rdi
callq 0x1b400
movss -0x150(%rbp), %xmm0
movss (%rax), %xmm1
subss %xmm1, %xmm0
movss %xmm0, -0x14c(%rbp)
movq -0x18(%rbp), %rdi
callq 0xf8b0
movss %xmm1, -0xe0(%rbp)
movlpd %xmm0, -0xe8(%rbp)
movl -0xe0(%rbp), %eax
movl %eax, -0xd0(%rbp)
movq -0xe8(%rbp), %rax
movq %rax, -0xd8(%rbp)
movl -0x24(%rbp), %esi
leaq -0xd8(%rbp), %rdi
callq 0x1b400
movss -0x14c(%rbp), %xmm0
movq -0x148(%rbp), %rdi
movss (%rax), %xmm1
divss %xmm1, %xmm0
movss %xmm0, -0x134(%rbp)
movl -0x24(%rbp), %esi
callq 0x1b3e0
movss %xmm0, -0x13c(%rbp)
movq -0x18(%rbp), %rdi
callq 0xf870
movss %xmm1, -0x100(%rbp)
movlpd %xmm0, -0x108(%rbp)
movl -0x100(%rbp), %eax
movl %eax, -0xf0(%rbp)
movq -0x108(%rbp), %rax
movq %rax, -0xf8(%rbp)
movl -0x24(%rbp), %esi
leaq -0xf8(%rbp), %rdi
callq 0x1b400
movss -0x13c(%rbp), %xmm0
movss (%rax), %xmm1
subss %xmm1, %xmm0
movss %xmm0, -0x138(%rbp)
movq -0x18(%rbp), %rdi
callq 0xf8b0
movss %xmm1, -0x120(%rbp)
movlpd %xmm0, -0x128(%rbp)
movq -0x128(%rbp), %rax
movq %rax, -0x114(%rbp)
movl -0x120(%rbp), %eax
movl %eax, -0x10c(%rbp)
movl -0x24(%rbp), %esi
leaq -0x114(%rbp), %rdi
callq 0x1b400
movss -0x138(%rbp), %xmm1
movss -0x134(%rbp), %xmm0
divss (%rax), %xmm1
callq 0x1b420
movss %xmm0, -0xac(%rbp)
movss -0x28(%rbp), %xmm0
movss -0x1c(%rbp), %xmm1
callq 0x1b420
movss %xmm0, -0x1c(%rbp)
movss -0xac(%rbp), %xmm0
movss -0x20(%rbp), %xmm1
callq 0x1b3a0
movss %xmm0, -0x20(%rbp)
movss -0x20(%rbp), %xmm1
movss -0x1c(%rbp), %xmm0
ucomiss %xmm1, %xmm0
jb 0x1af6a
movb $0x0, -0x1(%rbp)
jmp 0x1af7e
jmp 0x1af6c
movl -0x24(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x24(%rbp)
jmp 0x1ac0f
movb $0x1, -0x1(%rbp)
movb -0x1(%rbp), %al
andb $0x1, %al
addq $0x170, %rsp            # imm = 0x170
popq %rbp
retq
nopl (%rax)