// File: /workspace/github_cpp_makefile_star2_O1/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 $0x98, %rsp
movq %r8, 0x70(%rsp)
movq %rcx, %rbx
movl %edx, 0x30(%rsp)
movl %esi, 0x34(%rsp)
movq %rdi, 0x68(%rsp)
cvtsi2sd %esi, %xmm6
movsd %xmm5, 0x40(%rsp)
divsd %xmm6, %xmm5
movsd %xmm4, 0x10(%rsp)
movapd %xmm4, %xmm7
movsd %xmm5, 0x38(%rsp)
divsd %xmm5, %xmm7
addsd 0x1394(%rip), %xmm7     # 0x3188
movq %r9, 0x90(%rsp)
xorpd %xmm4, %xmm4
ucomisd %xmm4, %xmm1
jne 0x1e08
jnp 0x1e5d
movsd 0x1208(%rip), %xmm4     # 0x3018
mulsd %xmm1, %xmm0
addsd %xmm4, %xmm0
divsd %xmm1, %xmm4
movsd %xmm4, 0x28(%rsp)
movsd %xmm2, 0x20(%rsp)
movapd %xmm6, 0x50(%rsp)
movsd %xmm3, 0x8(%rsp)
movsd %xmm7, 0x18(%rsp)
callq 0x1090
movsd 0x18(%rsp), %xmm7
movsd 0x8(%rsp), %xmm3
movapd 0x50(%rsp), %xmm6
movsd 0x20(%rsp), %xmm2
mulsd 0x28(%rsp), %xmm0
cvttsd2si %xmm7, %ebp
movq 0x128(%rsp), %r12
movsd 0x38(%rsp), %xmm1
divsd %xmm1, %xmm2
movsd %xmm2, 0x20(%rsp)
subsd %xmm2, %xmm6
movsd 0x1303(%rip), %xmm2     # 0x3188
addsd %xmm2, %xmm6
movapd %xmm6, 0x50(%rsp)
cvttsd2si %xmm6, %r14d
divsd %xmm1, %xmm3
addsd %xmm2, %xmm3
cvttsd2si %xmm3, %r15d
testl %r14d, %r14d
jle 0x1ec3
movl %r14d, %edx
shlq $0x3, %rdx
movq %r12, %rdi
xorl %esi, %esi
movsd %xmm0, 0x8(%rsp)
callq 0x1040
movsd 0x8(%rsp), %xmm0
movl %r14d, 0x28(%rsp)
cmpl %r15d, %ebp
jg 0x1f0f
mulsd 0x10(%rsp), %xmm0
movslq %ebp, %r14
movslq %r15d, %r15
movq %r14, %r13
movsd %xmm0, 0x8(%rsp)
cmpq %r13, %r15
jne 0x1eee
callq 0x1150
jmp 0x1efb
callq 0x1150
addsd 0x1295(%rip), %xmm0     # 0x3190
movsd %xmm0, (%r12,%r13,8)
addq %r14, %r13
cmpq %r15, %r13
movsd 0x8(%rsp), %xmm0
jle 0x1ee2
movq 0xf0(%rsp), %rdi
movl 0x34(%rsp), %ebp
movl %ebp, %esi
movq %rbx, %rdx
callq 0x1ae4
cmpl $0x1, %eax
jne 0x2307
movq 0x100(%rsp), %rdi
movq 0xf8(%rsp), %rsi
movl %ebp, %edx
movl 0x30(%rsp), %ecx
movsd 0x40(%rsp), %xmm0
movq 0x70(%rsp), %r8
callq 0x1b33
cmpl $0x1, %eax
movl 0x28(%rsp), %r14d
jne 0x2307
xorpd %xmm0, %xmm0
movsd %xmm0, 0x10(%rsp)
movsd %xmm0, 0x18(%rsp)
cmpq $0x0, 0xd0(%rsp)
jle 0x2251
cvttpd2dq 0x50(%rsp), %xmm0
movsd 0x20(%rsp), %xmm1
addsd 0x11f3(%rip), %xmm1     # 0x3188
cvtdq2pd %xmm0, %xmm0
movq 0x110(%rsp), %rcx
movq 0xe8(%rsp), %rdi
movl 0xd8(%rsp), %r10d
cvttsd2si %xmm1, %eax
movsd 0x38(%rsp), %xmm1
mulsd %xmm0, %xmm1
movsd %xmm1, 0x38(%rsp)
imull %r10d, %eax
movslq %r10d, %rdx
cltq
movl %ebp, %esi
movq %rsi, 0x88(%rsp)
movl %edx, %r15d
leaq (%rcx,%rax,8), %rcx
movq %rcx, 0x20(%rsp)
movl %r10d, %ecx
imull %ebp, %ecx
movl %ecx, 0x4c(%rsp)
imull %ebp, %eax
movq %rax, 0x78(%rsp)
xorpd %xmm0, %xmm0
movsd %xmm0, 0x18(%rsp)
movsd %xmm0, 0x10(%rsp)
movl %r14d, %ebx
movq %rdx, 0x80(%rsp)
leaq (,%rdx,8), %rax
movq %rax, 0x8(%rsp)
leaq (,%r15,8), %r13
xorl %eax, %eax
movq %rax, 0x50(%rsp)
movl %ebp, %esi
movl 0x30(%rsp), %edx
movsd 0x40(%rsp), %xmm0
movq 0xf0(%rsp), %rcx
movq 0x100(%rsp), %r8
movq 0x70(%rsp), %r9
pushq 0x138(%rsp)
pushq 0x138(%rsp)
pushq %r10
leaq 0xa8(%rsp), %rax
pushq %rax
callq 0x17ca
addq $0x20, %rsp
cmpl $0x1, %eax
jne 0x2307
testl %ebp, %ebp
movq 0xe8(%rsp), %r8
movl 0xd8(%rsp), %r9d
movq 0x88(%rsp), %r10
movl 0x4c(%rsp), %r11d
jle 0x20d6
xorl %eax, %eax
movq 0x108(%rsp), %rcx
xorl %edx, %edx
testl %r9d, %r9d
jle 0x20c6
movslq %eax, %rsi
leaq (%r8,%rsi,8), %rsi
xorl %edi, %edi
movsd (%rsi,%rdi,8), %xmm0
movsd %xmm0, (%rcx,%rdi,8)
incq %rdi
cmpq %rdi, %r15
jne 0x20b4
incq %rdx
addq 0x8(%rsp), %rcx
addl %r11d, %eax
cmpq %r10, %rdx
jne 0x20a6
movq 0x110(%rsp), %rdi
movl %ebp, %esi
movsd 0x40(%rsp), %xmm0
movq 0x108(%rsp), %rdx
movl %r9d, %ecx
movq 0x140(%rsp), %rbp
movq %rbp, %r8
callq 0x1c98
cmpl $0x1, %eax
jne 0x2307
testl %r14d, %r14d
movq 0xe8(%rsp), %r8
movl 0xd8(%rsp), %r9d
jle 0x215e
movq 0x78(%rsp), %rax
movq 0x118(%rsp), %rcx
xorl %edx, %edx
testl %r9d, %r9d
jle 0x214e
movslq %eax, %rsi
leaq (%r8,%rsi,8), %rsi
xorl %edi, %edi
movsd (%rsi,%rdi,8), %xmm0
movsd %xmm0, (%rcx,%rdi,8)
incq %rdi
cmpq %rdi, %r15
jne 0x213c
incq %rdx
addq 0x8(%rsp), %rcx
addl %r9d, %eax
cmpq %rbx, %rdx
jne 0x212e
movq 0x120(%rsp), %rdi
movl %r14d, %esi
movsd 0x38(%rsp), %xmm0
movq 0x118(%rsp), %rdx
movl %r9d, %ecx
movq %rbp, %r8
movq %r9, %r14
callq 0x1c98
cmpl $0x1, %eax
jne 0x2307
testl %r14d, %r14d
jle 0x221d
movq 0x120(%rsp), %r14
xorl %ebp, %ebp
movsd 0xfe7(%rip), %xmm0      # 0x3190
cmpl $0x0, 0x28(%rsp)
jle 0x21da
xorpd %xmm0, %xmm0
movq %r14, %rax
xorl %ecx, %ecx
movsd (%r12,%rcx,8), %xmm1
mulsd (%rax), %xmm1
addsd %xmm1, %xmm0
incq %rcx
addq %r13, %rax
cmpq %rcx, %rbx
jne 0x21b9
addsd 0xfb6(%rip), %xmm0      # 0x3190
xorpd %xmm1, %xmm1
callq 0x14e4
movq 0x20(%rsp), %rax
mulsd (%rax,%rbp,8), %xmm0
movsd 0x10(%rsp), %xmm1
addsd %xmm0, %xmm1
movsd %xmm1, 0x10(%rsp)
mulsd %xmm0, %xmm0
movsd 0x18(%rsp), %xmm1
addsd %xmm0, %xmm1
movsd %xmm1, 0x18(%rsp)
incq %rbp
addq $0x8, %r14
cmpq %r15, %rbp
jne 0x21a1
movq 0x50(%rsp), %rax
addq 0x80(%rsp), %rax
cmpq 0xd0(%rsp), %rax
movl 0x34(%rsp), %ebp
movl 0x28(%rsp), %r14d
movq 0xe8(%rsp), %rdi
movl 0xd8(%rsp), %r10d
jl 0x2028
xorps %xmm2, %xmm2
cvtsi2sdq 0xd0(%rsp), %xmm2
movsd 0x10(%rsp), %xmm0
mulsd %xmm0, %xmm0
divsd %xmm2, %xmm0
movsd 0x18(%rsp), %xmm1
subsd %xmm0, %xmm1
movsd 0xf12(%rip), %xmm0      # 0x3190
addsd %xmm2, %xmm0
divsd %xmm0, %xmm1
movapd %xmm1, %xmm0
ucomisd 0xe16(%rip), %xmm1      # 0x30a8
jb 0x229d
xorps %xmm1, %xmm1
sqrtsd %xmm0, %xmm1
jmp 0x22b2
movsd %xmm2, 0x8(%rsp)
callq 0x1080
movsd 0x8(%rsp), %xmm2
movapd %xmm0, %xmm1
movsd 0x10(%rsp), %xmm0
divsd %xmm2, %xmm0
movsd %xmm0, 0x10(%rsp)
xorpd %xmm0, %xmm0
ucomisd %xmm0, %xmm2
jb 0x22d5
xorps %xmm0, %xmm0
sqrtsd %xmm2, %xmm0
jmp 0x22ea
movapd %xmm2, %xmm0
movsd %xmm1, 0x8(%rsp)
callq 0x1080
movsd 0x8(%rsp), %xmm1
movq 0x68(%rsp), %rax
divsd %xmm0, %xmm1
movsd 0x10(%rsp), %xmm0
movsd %xmm0, (%rax)
movsd %xmm1, 0x8(%rax)
movl $0x1, %eax
addq $0x98, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nopl (%rax)