// File: jc-kim[P]swaptions/HJM_SimPath_Forward_Blocking.cpp
// Function: HJM_SimPath_Forward_Blocking(double*, int, int, double, double*, double*, double*, long*, int, double*, double*)
int HJM_SimPath_Forward_Blocking(FTYPE *ppdHJMPath,	//Matrix that stores generated HJM path (Output)
    int iN,					//Number of time-steps
    int iFactors,			//Number of factors in the HJM framework
    FTYPE dYears,			//Number of years
    FTYPE *pdForward,		//t=0 Forward curve
    FTYPE *pdTotalDrift,	//Vector containing total drift corrections for different maturities
    FTYPE *ppdFactors,	//Factor volatilities
    long *lRndSeed,			//Random number seed
    int BLOCKSIZE,
		FTYPE *pdZ, FTYPE *randZ)
{	
  //This function computes and stores an HJM Path for given inputs

  int iSuccess = 0;
  int i, j, l; //looping variables
  int b;
  FTYPE dTotalShock; //total shock by which the forward curve is hit at (t, T-t)
  FTYPE ddelt, sqrt_ddelt; //length of time steps	

  ddelt = (FTYPE)(dYears/iN);
  sqrt_ddelt = sqrt(ddelt);

  // t=0 forward curve stored iN first row of ppdHJMPath
  // At time step 0: insert expected drift 
  // rest reset to 0
  for(b = 0; b < BLOCKSIZE; b++){
    for(j = 0; j < iN; j++){
      ppdHJMPath[BLOCKSIZE*j + b] = pdForward[j]; 

      //initializing HJMPath to zero
      for(i = 1; i < iN; i++) ppdHJMPath[i * iN * BLOCKSIZE + BLOCKSIZE*j + b] = 0;
    }
  }

  // sequentially generating random numbers
  //compute random number in exact same sequence
  /* 10% of the total executition time */
  for(b = 0; b < BLOCKSIZE; b++){
    for(j = 1; j < iN; j++){
      for(l = 0; l < iFactors; l++){
        randZ[l * iN * BLOCKSIZE + BLOCKSIZE * j + b] = RanUnif(lRndSeed);
      }
    }
  }

  // shocks to hit various factors for forward curve at t
  /* 18% of the total executition time */
  serialB(pdZ, randZ, BLOCKSIZE, iN, iFactors);

  // Generation of HJM Path1
  for(b = 0; b < BLOCKSIZE; b++){ // b is the blocks
    for(j = 1; j < iN; j++) { // j is the timestep
      for(l = 0; l < iN - j; l++){ // l is the future steps
        dTotalShock = 0;

        // i steps through the stochastic factors
        for (i = 0; i < iFactors; i++) dTotalShock += ppdFactors[i * (iN - 1) + l] * pdZ[i * iN * BLOCKSIZE + BLOCKSIZE * j + b];

        ppdHJMPath[j * iN * BLOCKSIZE + BLOCKSIZE * l + b] = ppdHJMPath[(j - 1) * iN * BLOCKSIZE + BLOCKSIZE * (l + 1) + b] + pdTotalDrift[l] * ddelt + sqrt_ddelt * dTotalShock;
        //as per formula
      }
    }
  }

  return 1;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x60, %rsp
movq 0x28(%rbp), %rax
movq 0x20(%rbp), %rax
movl 0x18(%rbp), %eax
movq 0x10(%rbp), %rax
movq %rdi, -0x8(%rbp)
movl %esi, -0xc(%rbp)
movl %edx, -0x10(%rbp)
movsd %xmm0, -0x18(%rbp)
movq %rcx, -0x20(%rbp)
movq %r8, -0x28(%rbp)
movq %r9, -0x30(%rbp)
movl $0x0, -0x34(%rbp)
movsd -0x18(%rbp), %xmm0
cvtsi2sdl -0xc(%rbp), %xmm1
divsd %xmm1, %xmm0
movsd %xmm0, -0x58(%rbp)
movsd -0x58(%rbp), %xmm0
callq 0x20e0
movsd %xmm0, -0x60(%rbp)
movl $0x0, -0x44(%rbp)
movl -0x44(%rbp), %eax
cmpl 0x18(%rbp), %eax
jge 0x2e6d
movl $0x0, -0x3c(%rbp)
movl -0x3c(%rbp), %eax
cmpl -0xc(%rbp), %eax
jge 0x2e5d
movq -0x20(%rbp), %rax
movslq -0x3c(%rbp), %rcx
movsd (%rax,%rcx,8), %xmm0
movq -0x8(%rbp), %rax
movl 0x18(%rbp), %ecx
imull -0x3c(%rbp), %ecx
addl -0x44(%rbp), %ecx
movslq %ecx, %rcx
movsd %xmm0, (%rax,%rcx,8)
movl $0x1, -0x38(%rbp)
movl -0x38(%rbp), %eax
cmpl -0xc(%rbp), %eax
jge 0x2e50
movq -0x8(%rbp), %rax
movl -0x38(%rbp), %ecx
imull -0xc(%rbp), %ecx
imull 0x18(%rbp), %ecx
movl 0x18(%rbp), %edx
imull -0x3c(%rbp), %edx
addl %edx, %ecx
addl -0x44(%rbp), %ecx
movslq %ecx, %rcx
xorps %xmm0, %xmm0
movsd %xmm0, (%rax,%rcx,8)
movl -0x38(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x38(%rbp)
jmp 0x2e17
jmp 0x2e52
movl -0x3c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x3c(%rbp)
jmp 0x2de5
jmp 0x2e5f
movl -0x44(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x44(%rbp)
jmp 0x2dd2
movl $0x0, -0x44(%rbp)
movl -0x44(%rbp), %eax
cmpl 0x18(%rbp), %eax
jge 0x2eeb
movl $0x1, -0x3c(%rbp)
movl -0x3c(%rbp), %eax
cmpl -0xc(%rbp), %eax
jge 0x2ede
movl $0x0, -0x40(%rbp)
movl -0x40(%rbp), %eax
cmpl -0x10(%rbp), %eax
jge 0x2ed1
movq 0x10(%rbp), %rdi
callq 0x2680
movq 0x28(%rbp), %rax
movl -0x40(%rbp), %ecx
imull -0xc(%rbp), %ecx
imull 0x18(%rbp), %ecx
movl 0x18(%rbp), %edx
imull -0x3c(%rbp), %edx
addl %edx, %ecx
addl -0x44(%rbp), %ecx
movslq %ecx, %rcx
movsd %xmm0, (%rax,%rcx,8)
movl -0x40(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x40(%rbp)
jmp 0x2e92
jmp 0x2ed3
movl -0x3c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x3c(%rbp)
jmp 0x2e83
jmp 0x2ee0
movl -0x44(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x44(%rbp)
jmp 0x2e74
movq 0x20(%rbp), %rdi
movq 0x28(%rbp), %rsi
movl 0x18(%rbp), %edx
movl -0xc(%rbp), %ecx
movl -0x10(%rbp), %r8d
callq 0x2ca0
movl $0x0, -0x44(%rbp)
movl -0x44(%rbp), %eax
cmpl 0x18(%rbp), %eax
jge 0x305a
movl $0x1, -0x3c(%rbp)
movl -0x3c(%rbp), %eax
cmpl -0xc(%rbp), %eax
jge 0x304a
movl $0x0, -0x40(%rbp)
movl -0x40(%rbp), %eax
movl -0xc(%rbp), %ecx
subl -0x3c(%rbp), %ecx
cmpl %ecx, %eax
jge 0x303a
xorps %xmm0, %xmm0
movsd %xmm0, -0x50(%rbp)
movl $0x0, -0x38(%rbp)
movl -0x38(%rbp), %eax
cmpl -0x10(%rbp), %eax
jge 0x2fb2
movq -0x30(%rbp), %rax
movl -0x38(%rbp), %ecx
movl -0xc(%rbp), %edx
subl $0x1, %edx
imull %edx, %ecx
addl -0x40(%rbp), %ecx
movslq %ecx, %rcx
movsd (%rax,%rcx,8), %xmm0
movq 0x20(%rbp), %rax
movl -0x38(%rbp), %ecx
imull -0xc(%rbp), %ecx
imull 0x18(%rbp), %ecx
movl 0x18(%rbp), %edx
imull -0x3c(%rbp), %edx
addl %edx, %ecx
addl -0x44(%rbp), %ecx
movslq %ecx, %rcx
movsd (%rax,%rcx,8), %xmm2
movsd -0x50(%rbp), %xmm1
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, -0x50(%rbp)
movl -0x38(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x38(%rbp)
jmp 0x2f4f
movq -0x8(%rbp), %rax
movl -0x3c(%rbp), %ecx
subl $0x1, %ecx
imull -0xc(%rbp), %ecx
imull 0x18(%rbp), %ecx
movl 0x18(%rbp), %edx
movl -0x40(%rbp), %esi
addl $0x1, %esi
imull %esi, %edx
addl %edx, %ecx
addl -0x44(%rbp), %ecx
movslq %ecx, %rcx
movsd (%rax,%rcx,8), %xmm0
movq -0x28(%rbp), %rax
movslq -0x40(%rbp), %rcx
movsd (%rax,%rcx,8), %xmm1
movsd -0x58(%rbp), %xmm2
mulsd %xmm2, %xmm1
addsd %xmm0, %xmm1
movsd -0x60(%rbp), %xmm0
movsd -0x50(%rbp), %xmm2
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movq -0x8(%rbp), %rax
movl -0x3c(%rbp), %ecx
imull -0xc(%rbp), %ecx
imull 0x18(%rbp), %ecx
movl 0x18(%rbp), %edx
imull -0x40(%rbp), %edx
addl %edx, %ecx
addl -0x44(%rbp), %ecx
movslq %ecx, %rcx
movsd %xmm0, (%rax,%rcx,8)
movl -0x40(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x40(%rbp)
jmp 0x2f2f
jmp 0x303c
movl -0x3c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x3c(%rbp)
jmp 0x2f1c
jmp 0x304c
movl -0x44(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x44(%rbp)
jmp 0x2f09
movl $0x1, %eax
addq $0x60, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)
nop