// File: /workspace/github_cpp_makefile_star2_O2/jc-kim[P]swaptions/HJM_Swaption_Blocking.cpp
// Function: HJM_Swaption_Blocking(double*, double, double, double, double, double, int, int, double, double*, double*, long, long, int, int, double*, double*, double*, double*, double*, double*, double*, double*, double*, double*, double*, double*)
int HJM_Swaption_Blocking(FTYPE *pdSwaptionPrice, //Output vector that will store simulation results in the form:
    //Swaption Price
    //Swaption Standard Error
    //Swaption Parameters
    FTYPE dStrike,
    FTYPE dCompounding,     //Compounding convention used for quoting the strike (0 => continuous,
    //0.5 => semi-annual, 1 => annual).
    FTYPE dMaturity,	      //Maturity of the swaption (time to expiration)
    FTYPE dTenor,	      //Tenor of the swap
    FTYPE dPaymentInterval, //frequency of swap payments e.g. dPaymentInterval = 0.5 implies a swap payment every half
    //year
    //HJM Framework Parameters (please refer HJM.cpp for explanation of variables and functions)
    int iN,
    int iFactors,
    FTYPE dYears,
    FTYPE *pdYield,
    FTYPE *ppdFactors,
    //Simulation Parameters
    long iRndSeed,
    long lTrials,
    int BLOCKSIZE, int tid,
		FTYPE *ppdHJMPath, FTYPE *pdForward, FTYPE *ppdDrifts, FTYPE *pdTotalDrift,
		FTYPE *pdDiscountingRatePath, FTYPE *pdPayoffDiscountFactors, FTYPE *pdSwapRatePath, FTYPE *pdSwapDiscountFactors, FTYPE *pdSwapPayoffs,
		FTYPE *pdZ, FTYPE *randZ,
		FTYPE *pdexpRes)
{
  int iSuccess = 0;
  int i;
  int b; //block looping variable
  long l; //looping variables

  FTYPE ddelt = (FTYPE)(dYears/iN);
  int iFreqRatio = (int)(dPaymentInterval/ddelt + 0.5);
  FTYPE dStrikeCont;
	dStrikeCont = dCompounding == 0 ? dStrike: (1 / dCompounding) * log(1 + dStrike * dCompounding);  

	int iSwapVectorLength = (int)(iN - dMaturity / ddelt + 0.5);

  int iSwapStartTimeIndex;
  int iSwapTimePoints;
  FTYPE dSwapVectorYears;

  FTYPE dSwaptionPayoff;
  FTYPE dDiscSwaptionPayoff;
  FTYPE dFixedLegValue;

  // Accumulators
  FTYPE dSumSimSwaptionPrice; 
  FTYPE dSumSquareSimSwaptionPrice;

  // Final returned results
  FTYPE dSimSwaptionMeanPrice;
  FTYPE dSimSwaptionStdError;

  iSwapStartTimeIndex = (int)(dMaturity/ddelt + 0.5);	//Swap starts at swaption maturity
  iSwapTimePoints = (int)(dTenor/ddelt + 0.5);			//Total HJM time points corresponding to the swap's tenor
  dSwapVectorYears = (FTYPE)(iSwapVectorLength*ddelt);

  for (i = 0; i < iSwapVectorLength; i++) pdSwapPayoffs[i] = 0.0;
  for (i = iFreqRatio; i <= iSwapTimePoints; i += iFreqRatio) {
    if(i != iSwapTimePoints)
      pdSwapPayoffs[i] = exp(dStrikeCont * dPaymentInterval) - 1;
    else
      pdSwapPayoffs[i] = exp(dStrikeCont * dPaymentInterval);
  }

  //generating forward curve at t=0 from supplied yield curve
  iSuccess = HJM_Yield_to_Forward(pdForward, iN, pdYield);
  if(iSuccess != 1) return iSuccess;

  //computation of drifts from factor volatilities
  iSuccess = HJM_Drifts(pdTotalDrift, ppdDrifts, iN, iFactors, dYears, ppdFactors);
  if(iSuccess != 1) return iSuccess;

  dSumSimSwaptionPrice = 0.0;
  dSumSquareSimSwaptionPrice = 0.0;

  //Simulations begin:
  for(l = 0; l < lTrials; l += BLOCKSIZE) {
    //For each trial a new HJM Path is generated
    /* GC: 51% of the time goes here */
    iSuccess = HJM_SimPath_Forward_Blocking(ppdHJMPath, iN, iFactors, dYears, pdForward, pdTotalDrift, ppdFactors, &iRndSeed, BLOCKSIZE, pdZ, randZ);
    if(iSuccess != 1) return iSuccess;

    //now we compute the discount factor vector
    for(i = 0; i < iN; i++){
      for(b = 0; b < BLOCKSIZE; b++){
        pdDiscountingRatePath[BLOCKSIZE*i + b] = ppdHJMPath[i * iN * BLOCKSIZE + b];
      }
    }

    /* 15% of the time goes here */
    iSuccess = Discount_Factors_Blocking(pdPayoffDiscountFactors, iN, dYears, pdDiscountingRatePath, BLOCKSIZE, pdexpRes);
    if(iSuccess != 1) return iSuccess;

    //now we compute discount factors along the swap path
    for(i = 0; i < iSwapVectorLength; i++){
      for(b = 0; b < BLOCKSIZE; b++){
        pdSwapRatePath[i * BLOCKSIZE + b] = ppdHJMPath[iSwapStartTimeIndex * iN * BLOCKSIZE + i * BLOCKSIZE + b];
      }
    }

    iSuccess = Discount_Factors_Blocking(pdSwapDiscountFactors, iSwapVectorLength, dSwapVectorYears, pdSwapRatePath, BLOCKSIZE, pdexpRes);
    if(iSuccess != 1) return iSuccess;

    // Simulation
    for(b = 0; b < BLOCKSIZE; b++){
      dFixedLegValue = 0.0;

      for(i = 0; i < iSwapVectorLength; i++) dFixedLegValue += pdSwapPayoffs[i] * pdSwapDiscountFactors[i*BLOCKSIZE + b];
      dSwaptionPayoff = dMax(dFixedLegValue - 1.0, 0);

      dDiscSwaptionPayoff = dSwaptionPayoff * pdPayoffDiscountFactors[iSwapStartTimeIndex*BLOCKSIZE + b];
      // end simulation

      // accumulate into the aggregating variables
      dSumSimSwaptionPrice += dDiscSwaptionPayoff;
      dSumSquareSimSwaptionPrice += dDiscSwaptionPayoff*dDiscSwaptionPayoff;
    }
  }

  // Simulation Results Stored
  dSimSwaptionMeanPrice = dSumSimSwaptionPrice / lTrials;
  dSimSwaptionStdError = sqrt((dSumSquareSimSwaptionPrice-dSumSimSwaptionPrice*dSumSimSwaptionPrice/lTrials)/
      (lTrials-1.0))/sqrt((FTYPE)lTrials);

  //results returned
  pdSwaptionPrice[0] = dSimSwaptionMeanPrice;
  pdSwaptionPrice[1] = dSimSwaptionStdError;

  return 1;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xb8, %rsp
movq %r8, 0x88(%rsp)
movl %edx, 0xc(%rsp)
movl %esi, %r13d
movsd %xmm2, 0x30(%rsp)
cvtsi2sd %esi, %xmm7
movsd %xmm5, 0x48(%rsp)
divsd %xmm7, %xmm5
movapd %xmm4, %xmm6
movsd %xmm5, 0x28(%rsp)
divsd %xmm5, %xmm6
addsd 0x1502(%rip), %xmm6     # 0x3188
movq %r9, 0xb0(%rsp)
xorpd %xmm2, %xmm2
ucomisd %xmm2, %xmm1
movq %rdi, 0x80(%rsp)
movq %rcx, 0x70(%rsp)
jne 0x1ca7
jnp 0x1d02
movsd 0x1369(%rip), %xmm2     # 0x3018
mulsd %xmm1, %xmm0
addsd %xmm2, %xmm0
divsd %xmm1, %xmm2
movsd %xmm2, 0x40(%rsp)
movsd %xmm4, 0x10(%rsp)
movsd %xmm3, 0x60(%rsp)
movsd %xmm6, 0x50(%rsp)
movapd %xmm7, 0x90(%rsp)
callq 0x1090
movapd 0x90(%rsp), %xmm7
movsd 0x50(%rsp), %xmm6
movsd 0x60(%rsp), %xmm3
movsd 0x10(%rsp), %xmm4
mulsd 0x40(%rsp), %xmm0
movsd 0x28(%rsp), %xmm1
movsd 0x30(%rsp), %xmm2
divsd %xmm1, %xmm2
subsd %xmm2, %xmm7
movsd 0x146a(%rip), %xmm5     # 0x3188
addsd %xmm5, %xmm7
addsd %xmm5, %xmm2
movsd %xmm2, 0x30(%rsp)
divsd %xmm1, %xmm3
addsd %xmm5, %xmm3
movq 0x148(%rsp), %rbp
cvttsd2si %xmm7, %ecx
cvttpd2dq %xmm7, %xmm1
cvtdq2pd %xmm1, %xmm1
movapd %xmm1, 0x60(%rsp)
xorl %eax, %eax
testl %ecx, %ecx
movl $0x0, %r14d
movl %ecx, 0x40(%rsp)
cmovgl %ecx, %r14d
cvttsd2si %xmm6, %ecx
cmpq %rax, %r14
je 0x1d74
andq $0x0, (%rbp,%rax,8)
incq %rax
jmp 0x1d64
cvttsd2si %xmm3, %eax
mulsd %xmm4, %xmm0
movslq %ecx, %rbx
movslq %eax, %r15
movq %rbx, %r12
movsd %xmm0, 0x10(%rsp)
cmpq %r15, %r12
jg 0x1dbc
callq 0x1150
movapd %xmm0, %xmm1
addsd 0x13ef(%rip), %xmm1     # 0x3190
cmpq %r12, %r15
je 0x1daa
movapd %xmm1, %xmm0
movsd %xmm0, (%rbp,%r12,8)
addq %rbx, %r12
movsd 0x10(%rsp), %xmm0
jmp 0x1d8b
movq 0x110(%rsp), %rdi
movl %r13d, %esi
movq 0x70(%rsp), %rdx
callq 0x19b8
cmpl $0x1, %eax
jne 0x2152
movq 0x120(%rsp), %rdi
movq 0x118(%rsp), %rsi
movl %r13d, %edx
movl 0xc(%rsp), %ecx
movsd 0x48(%rsp), %xmm0
movq 0x88(%rsp), %r8
callq 0x19f9
cmpl $0x1, %eax
jne 0x2152
cvttsd2si 0x30(%rsp), %eax
movsd 0x28(%rsp), %xmm0
mulsd 0x60(%rsp), %xmm0
movsd %xmm0, 0x28(%rsp)
movl 0xf8(%rsp), %ecx
imull %ecx, %eax
movslq %ecx, %rdx
xorl %esi, %esi
testl %edx, %edx
movl $0x0, %r15d
cmovgl %ecx, %r15d
testl %r13d, %r13d
movl $0x0, %edi
cmovgl %r13d, %edi
movq %rdi, 0x70(%rsp)
movslq %eax, %rdi
movq 0x130(%rsp), %rax
leaq (%rax,%rdi,8), %rax
movq %rax, 0x50(%rsp)
movq %rdx, 0xa0(%rsp)
leaq (,%rdx,8), %rbx
movl %ecx, %eax
imull %r13d, %eax
movl %eax, 0x60(%rsp)
imull %r13d, %edi
movq %rdi, 0xa8(%rsp)
xorpd %xmm0, %xmm0
movapd %xmm0, 0x30(%rsp)
xorpd %xmm1, %xmm1
movl %r13d, 0x5c(%rsp)
movq %rsi, 0x90(%rsp)
cmpq 0xf0(%rsp), %rsi
movq 0x140(%rsp), %r12
jge 0x20b1
movapd %xmm1, 0x10(%rsp)
movq 0x108(%rsp), %rdi
movl %r13d, %esi
movl 0xc(%rsp), %edx
movsd 0x48(%rsp), %xmm0
movq 0x110(%rsp), %rcx
movq 0x120(%rsp), %r8
movq 0x88(%rsp), %r9
pushq 0x158(%rsp)
pushq 0x158(%rsp)
movl 0x108(%rsp), %eax
pushq %rax
leaq 0xc8(%rsp), %rax
pushq %rax
callq 0x16fd
addq $0x20, %rsp
cmpl $0x1, %eax
jne 0x2152
xorl %eax, %eax
movq 0x128(%rsp), %rcx
xorl %edx, %edx
cmpq 0x70(%rsp), %rdx
je 0x1f64
movslq %eax, %rsi
movq 0x108(%rsp), %rdi
leaq (%rdi,%rsi,8), %rsi
xorl %edi, %edi
cmpq %rdi, %r15
je 0x1f58
movsd (%rsi,%rdi,8), %xmm0
movsd %xmm0, (%rcx,%rdi,8)
incq %rdi
jmp 0x1f44
incq %rdx
addq %rbx, %rcx
addl 0x60(%rsp), %eax
jmp 0x1f2c
movq 0x130(%rsp), %rdi
movl %r13d, %esi
movsd 0x48(%rsp), %xmm0
movq 0x128(%rsp), %rdx
movl 0xf8(%rsp), %ecx
movq 0x160(%rsp), %r8
callq 0x1b30
cmpl $0x1, %eax
jne 0x2152
movq 0xa8(%rsp), %rax
movq 0x138(%rsp), %rcx
xorl %edx, %edx
cmpq %r14, %rdx
je 0x1fe7
movslq %eax, %rsi
movq 0x108(%rsp), %rdi
leaq (%rdi,%rsi,8), %rsi
xorl %edi, %edi
cmpq %rdi, %r15
je 0x1fd6
movsd (%rsi,%rdi,8), %xmm0
movsd %xmm0, (%rcx,%rdi,8)
incq %rdi
jmp 0x1fc2
incq %rdx
addq %rbx, %rcx
movl 0xf8(%rsp), %esi
addl %esi, %eax
jmp 0x1fac
movq %r12, %rdi
movl 0x40(%rsp), %esi
movsd 0x28(%rsp), %xmm0
movq 0x138(%rsp), %rdx
movl 0xf8(%rsp), %ecx
movq 0x160(%rsp), %r8
callq 0x1b30
cmpl $0x1, %eax
jne 0x2152
movq %r12, %r13
xorl %r12d, %r12d
movapd 0x10(%rsp), %xmm1
cmpq %r15, %r12
je 0x2097
movapd %xmm1, 0x10(%rsp)
xorpd %xmm0, %xmm0
movq %r13, %rax
xorl %ecx, %ecx
cmpq %rcx, %r14
je 0x2054
movsd (%rbp,%rcx,8), %xmm1
mulsd (%rax), %xmm1
addsd %xmm1, %xmm0
incq %rcx
addq %rbx, %rax
jmp 0x2039
addsd 0x1134(%rip), %xmm0     # 0x3190
xorpd %xmm1, %xmm1
callq 0x1424
movq 0x50(%rsp), %rax
mulsd (%rax,%r12,8), %xmm0
movapd 0x30(%rsp), %xmm1
addsd %xmm0, %xmm1
movapd %xmm1, 0x30(%rsp)
mulsd %xmm0, %xmm0
movapd 0x10(%rsp), %xmm1
addsd %xmm0, %xmm1
incq %r12
addq $0x8, %r13
jmp 0x2025
movq 0x90(%rsp), %rsi
addq 0xa0(%rsp), %rsi
movl 0x5c(%rsp), %r13d
jmp 0x1e9e
cvtsi2sdq 0xf0(%rsp), %xmm2
movapd 0x30(%rsp), %xmm0
mulsd %xmm0, %xmm0
divsd %xmm2, %xmm0
subsd %xmm0, %xmm1
movsd 0x10bb(%rip), %xmm0     # 0x3190
addsd %xmm2, %xmm0
divsd %xmm0, %xmm1
xorpd %xmm0, %xmm0
ucomisd %xmm0, %xmm1
movapd %xmm2, 0x10(%rsp)
jb 0x20f3
sqrtsd %xmm1, %xmm3
jmp 0x210a
movapd %xmm1, %xmm0
callq 0x1080
movapd 0x10(%rsp), %xmm2
movapd %xmm0, %xmm3
xorpd %xmm0, %xmm0
movq 0x80(%rsp), %rbx
ucomisd %xmm0, %xmm2
jb 0x211e
sqrtsd %xmm2, %xmm0
jmp 0x2139
movapd %xmm2, %xmm0
movapd %xmm3, 0x70(%rsp)
callq 0x1080
movapd 0x70(%rsp), %xmm3
movapd 0x10(%rsp), %xmm2
movapd 0x30(%rsp), %xmm1
unpcklpd %xmm3, %xmm1            # xmm1 = xmm1[0],xmm3[0]
unpcklpd %xmm0, %xmm2            # xmm2 = xmm2[0],xmm0[0]
divpd %xmm2, %xmm1
movupd %xmm1, (%rbx)
pushq $0x1
popq %rax
addq $0xb8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq