// File: ingowald[P]closestSurfacePointQueries/src/bvh.cpp
// Function: bvhlib::BVH::build(bvhlib::Geometry const*)
void BVH::build(const Geometry *geometry)
  {
    assert(geometry);
    size_t numPrimitives = geometry->numPrimitives();
    std::vector<BuildPrim> buildPrim;
    std::vector<size_t>    primID;
    buildPrim.resize(numPrimitives);
    primID.resize(numPrimitives);
    
    size_t blockSize = 1000;
    size_t numBlocks = (numPrimitives+blockSize-1) / blockSize;
    Mutex mutex;
    box3fa centBounds  = ospcommon::empty;

// #pragma omp for
    for (size_t blockID=0;blockID<numBlocks;blockID++) {
      size_t begin   = blockID * blockSize;
      size_t end     = std::min(begin+blockSize,numPrimitives);
      box3fa blockBounds  = ospcommon::empty;
      for (size_t i=begin;i<end;i++) {
        BuildPrim bp;
        geometry->initBuildPrim(bp,i);
        blockBounds.extend(bp.center());
        buildPrim[i] = bp;
        primID[i] = i;
      }

      LockGuard lock(mutex);
      centBounds.extend(blockBounds);
    }
    nodeList.push_back(Node());
    buildRec(0,centBounds,buildPrim.data(),primID);
  }

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x180, %rsp            # imm = 0x180
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0x168(%rbp)
cmpq $0x0, -0x10(%rbp)
je 0xdc47
jmp 0xdc66
leaq 0x3b66(%rip), %rdi      # 0x117b4
leaq 0x3b68(%rip), %rsi      # 0x117bd
movl $0x86, %edx
leaq 0x3b68(%rip), %rcx      # 0x117c9
callq 0x3190
movq -0x10(%rbp), %rdi
movq (%rdi), %rax
movq (%rax), %rax
callq *%rax
movq %rax, -0x18(%rbp)
leaq -0x30(%rbp), %rdi
movq %rdi, -0x170(%rbp)
callq 0xe9c0
leaq -0x48(%rbp), %rdi
callq 0xe060
movq -0x170(%rbp), %rdi
movq -0x18(%rbp), %rsi
callq 0xe9e0
jmp 0xdca1
movq -0x18(%rbp), %rsi
leaq -0x48(%rbp), %rdi
callq 0xe080
jmp 0xdcb0
movq $0x3e8, -0x60(%rbp)     # imm = 0x3E8
movq -0x18(%rbp), %rax
movq -0x60(%rbp), %rcx
leaq -0x1(%rax,%rcx), %rax
xorl %edx, %edx
divq %rcx
movq %rax, -0x68(%rbp)
leaq -0x90(%rbp), %rdi
callq 0x9fa0
jmp 0xdcdc
leaq -0xb0(%rbp), %rdi
callq 0xe110
jmp 0xdcea
movq $0x0, -0xc0(%rbp)
movq -0xc0(%rbp), %rax
cmpq -0x68(%rbp), %rax
jae 0xded2
movq -0xc0(%rbp), %rax
movq -0x60(%rbp), %rcx
imulq %rcx, %rax
movq %rax, -0xc8(%rbp)
movq -0xc8(%rbp), %rax
movq -0x60(%rbp), %rcx
addq %rcx, %rax
movq %rax, -0xd8(%rbp)
leaq -0xd8(%rbp), %rdi
leaq -0x18(%rbp), %rsi
callq 0x70b0
movq %rax, -0x178(%rbp)
jmp 0xdd4a
movq -0x178(%rbp), %rax
movq (%rax), %rax
movq %rax, -0xd0(%rbp)
leaq -0xf8(%rbp), %rdi
callq 0xe110
jmp 0xdd69
movq -0xc8(%rbp), %rax
movq %rax, -0x108(%rbp)
movq -0x108(%rbp), %rax
cmpq -0xd0(%rbp), %rax
jae 0xde6b
leaq -0x128(%rbp), %rdi
callq 0xea70
jmp 0xdd99
movq -0x10(%rbp), %rdi
movq -0x108(%rbp), %rdx
movq (%rdi), %rax
movq 0x8(%rax), %rax
leaq -0x128(%rbp), %rsi
callq *%rax
jmp 0xddb6
leaq -0x138(%rbp), %rdi
leaq -0x128(%rbp), %rsi
callq 0xe300
jmp 0xddcb
leaq -0xf8(%rbp), %rdi
leaq -0x138(%rbp), %rsi
callq 0xe3f0
jmp 0xdde0
movq -0x108(%rbp), %rsi
leaq -0x30(%rbp), %rdi
callq 0xea90
movq -0x128(%rbp), %rcx
movq %rcx, (%rax)
movq -0x120(%rbp), %rcx
movq %rcx, 0x8(%rax)
movq -0x118(%rbp), %rcx
movq %rcx, 0x10(%rax)
movq -0x110(%rbp), %rcx
movq %rcx, 0x18(%rax)
movq -0x108(%rbp), %rax
movq %rax, -0x180(%rbp)
movq -0x108(%rbp), %rsi
leaq -0x48(%rbp), %rdi
callq 0xdf80
movq -0x180(%rbp), %rcx
movq %rcx, (%rax)
movq -0x108(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x108(%rbp)
jmp 0xdd77
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x50(%rbp)
movl %eax, -0x54(%rbp)
jmp 0xdf3c
leaq -0x140(%rbp), %rdi
leaq -0x90(%rbp), %rsi
callq 0xdfa0
jmp 0xde80
leaq -0xb0(%rbp), %rdi
leaq -0xf8(%rbp), %rsi
callq 0xe520
jmp 0xde95
leaq -0x140(%rbp), %rdi
callq 0xdfd0
movq -0xc0(%rbp), %rax
addq $0x1, %rax
movq %rax, -0xc0(%rbp)
jmp 0xdcf5
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x50(%rbp)
movl %eax, -0x54(%rbp)
leaq -0x140(%rbp), %rdi
callq 0xdfd0
jmp 0xdf3c
movq -0x168(%rbp), %rdi
xorps %xmm0, %xmm0
movaps %xmm0, -0x150(%rbp)
movaps %xmm0, -0x160(%rbp)
leaq -0x160(%rbp), %rsi
callq 0xe6f0
jmp 0xdef8
leaq -0x30(%rbp), %rdi
callq 0xeab0
movq -0x168(%rbp), %rdi
movq %rax, %rcx
xorl %eax, %eax
movl %eax, %esi
leaq -0xb0(%rbp), %rdx
leaq -0x48(%rbp), %r8
callq 0xd0b0
jmp 0xdf21
leaq -0x48(%rbp), %rdi
callq 0xe960
leaq -0x30(%rbp), %rdi
callq 0xead0
addq $0x180, %rsp            # imm = 0x180
popq %rbp
retq
leaq -0x48(%rbp), %rdi
callq 0xe960
leaq -0x30(%rbp), %rdi
callq 0xead0
movq -0x50(%rbp), %rdi
callq 0x33e0
nopw (%rax,%rax)