// File: /workspace/github_cpp_makefile_star2_O2/GUI-Graphics[P]cpuraytracing/sphere.cpp
// Function: gui::Sphere::intersect(gui::Ray const&, float, float, gui::Intersection&) const
bool Sphere::intersect(
		const Ray& ray,
		float tMin,
		float tMax,
		Intersection& intersection) const {
			
			++Renderer::nIntersects;

			Vector3 co = ray.origin - center;
			float a = ray.direction.lengthSquared();
			float b = ray.direction.dot(co);
			float c = co.lengthSquared() - radius * radius;
			float delta = b * b - a * c;

			if(delta < 0) return false;

			delta = sqrt(delta);
			float t = (-b - delta) / a;

			if(t > tMin && t < tMax) {
				intersection.t = t;
				intersection.position = ray.origin + ray.direction * t;
				intersection.normal = (intersection.position - center) / radius;
				intersection.material = material;
				return true;
			}

			t = (-b + delta) / a;
			if(t > tMin && t < tMax) {
				intersection.t = t;
				intersection.position = ray.origin + ray.direction * t;
				intersection.normal = (intersection.position - center) / radius;
				intersection.material = material;
				return true;
			}

			return false;
	}

// Assembly:
pushq %r15
pushq %r14
pushq %r12
pushq %rbx
subq $0x38, %rsp
movq %rdx, %rbx
movss %xmm1, 0x8(%rsp)
movss %xmm0, 0x4(%rsp)
movq %rsi, %r12
movq %rdi, %r14
leaq 0x71cd(%rip), %rax      # 0xa868
incq (%rax)
leaq 0x10(%rdi), %r15
movq %rsi, %rdi
movq %r15, %rsi
callq 0x35c8
movss 0xc(%r12), %xmm2
movss 0x10(%r12), %xmm3
movaps %xmm3, %xmm4
mulss %xmm3, %xmm4
movaps %xmm2, %xmm5
mulss %xmm2, %xmm5
addss %xmm4, %xmm5
movss 0x14(%r12), %xmm7
movaps %xmm7, %xmm6
mulss %xmm7, %xmm6
addss %xmm5, %xmm6
movaps %xmm0, %xmm4
shufps $0x55, %xmm0, %xmm4     # xmm4 = xmm4[1,1],xmm0[1,1]
mulss %xmm4, %xmm3
mulss %xmm0, %xmm2
addss %xmm3, %xmm2
mulss %xmm1, %xmm7
addss %xmm2, %xmm7
mulss %xmm4, %xmm4
mulss %xmm0, %xmm0
addss %xmm4, %xmm0
mulss %xmm1, %xmm1
addss %xmm0, %xmm1
movss 0x1c(%r14), %xmm0
mulss %xmm0, %xmm0
subss %xmm0, %xmm1
mulss %xmm6, %xmm1
movaps %xmm7, %xmm0
mulss %xmm7, %xmm0
subss %xmm1, %xmm0
xorps %xmm1, %xmm1
ucomiss %xmm0, %xmm1
jbe 0x373a
xorl %eax, %eax
jmp 0x380b
ucomiss %xmm1, %xmm0
jb 0x3745
sqrtss %xmm0, %xmm0
jmp 0x3760
movss %xmm6, 0xc(%rsp)
movaps %xmm7, 0x20(%rsp)
callq 0x2250
movaps 0x20(%rsp), %xmm7
movss 0xc(%rsp), %xmm6
movss 0x8(%rsp), %xmm2
movss 0x4(%rsp), %xmm3
leaq 0xc(%r12), %rdi
movaps 0x1a28(%rip), %xmm1     # 0x51a0
xorps %xmm7, %xmm1
subss %xmm0, %xmm1
divss %xmm6, %xmm1
ucomiss %xmm3, %xmm1
jbe 0x3796
ucomiss %xmm1, %xmm2
jbe 0x3796
movss %xmm1, (%rbx)
movaps %xmm1, %xmm0
jmp 0x37ae
subss %xmm7, %xmm0
divss %xmm6, %xmm0
xorl %eax, %eax
ucomiss %xmm3, %xmm0
jbe 0x380b
ucomiss %xmm0, %xmm2
jbe 0x380b
movss %xmm0, (%rbx)
callq 0x359e
leaq 0x10(%rsp), %rsi
movlps %xmm0, (%rsi)
movss %xmm1, 0x8(%rsi)
movq %r12, %rdi
callq 0x35b2
leaq 0x4(%rbx), %rdi
movlps %xmm0, 0x4(%rbx)
movss %xmm1, 0xc(%rbx)
movq %r15, %rsi
callq 0x35c8
movss 0x182b(%rip), %xmm2     # 0x5010
divss 0x1c(%r14), %xmm2
movaps %xmm2, %xmm3
unpcklps %xmm2, %xmm3            # xmm3 = xmm3[0],xmm2[0],xmm3[1],xmm2[1]
mulps %xmm0, %xmm3
mulss %xmm1, %xmm2
movlps %xmm3, 0x10(%rbx)
movss %xmm2, 0x18(%rbx)
movq 0x8(%r14), %rax
movq %rax, 0x20(%rbx)
movb $0x1, %al
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
retq
nop