// File: /workspace/github_cpp_makefile_star2_O3/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 $0xa8, %rsp
movq %r8, 0x80(%rsp)
movq %rcx, %rbx
movl %edx, 0x20(%rsp)
movl %esi, %ebp
movq %rdi, 0x78(%rsp)
cvtsi2sd %esi, %xmm6
movsd %xmm5, 0x50(%rsp)
divsd %xmm6, %xmm5
movsd %xmm4, 0x40(%rsp)
movapd %xmm4, %xmm7
movsd %xmm5, 0x28(%rsp)
divsd %xmm5, %xmm7
addsd 0x1313(%rip), %xmm7     # 0x3188
movq %r9, 0xa0(%rsp)
xorpd %xmm4, %xmm4
ucomisd %xmm4, %xmm1
jne 0x1e89
jnp 0x1edc
movsd 0x1187(%rip), %xmm4     # 0x3018
mulsd %xmm1, %xmm0
addsd %xmm4, %xmm0
divsd %xmm1, %xmm4
movsd %xmm4, 0x18(%rsp)
movsd %xmm2, 0x10(%rsp)
movapd %xmm6, 0x60(%rsp)
movsd %xmm3, (%rsp)
movsd %xmm7, 0x30(%rsp)
callq 0x1090
movsd 0x30(%rsp), %xmm7
movsd (%rsp), %xmm3
movapd 0x60(%rsp), %xmm6
movsd 0x10(%rsp), %xmm2
mulsd 0x18(%rsp), %xmm0
movl %ebp, 0x24(%rsp)
cvttsd2si %xmm7, %ebp
movq 0x138(%rsp), %r12
movsd 0x28(%rsp), %xmm1
divsd %xmm1, %xmm2
movsd %xmm2, 0x10(%rsp)
subsd %xmm2, %xmm6
movsd 0x1280(%rip), %xmm2     # 0x3188
addsd %xmm2, %xmm6
movapd %xmm6, 0x60(%rsp)
cvttsd2si %xmm6, %r14d
divsd %xmm1, %xmm3
addsd %xmm2, %xmm3
cvttsd2si %xmm3, %r15d
testl %r14d, %r14d
jle 0x1f44
movl %r14d, %edx
shlq $0x3, %rdx
movq %r12, %rdi
xorl %esi, %esi
movsd %xmm0, (%rsp)
callq 0x1040
movsd (%rsp), %xmm0
movl %r14d, 0x18(%rsp)
cmpl %r15d, %ebp
jg 0x1f8f
mulsd 0x40(%rsp), %xmm0
movslq %ebp, %r14
movslq %r15d, %r15
movq %r14, %r13
movsd %xmm0, (%rsp)
callq 0x1150
movapd %xmm0, %xmm1
addsd 0x124d(%rip), %xmm1     # 0x31c0
cmpq %r13, %r15
je 0x1f7c
movapd %xmm1, %xmm0
movsd %xmm0, (%r12,%r13,8)
addq %r14, %r13
cmpq %r15, %r13
movsd (%rsp), %xmm0
jle 0x1f62
movq 0x100(%rsp), %rdi
movl 0x24(%rsp), %r14d
movl %r14d, %esi
movq %rbx, %rdx
callq 0x1af8
cmpl $0x1, %eax
jne 0x2391
movq 0x110(%rsp), %rdi
movq 0x108(%rsp), %rsi
movl %r14d, %edx
movl 0x20(%rsp), %ecx
movsd 0x50(%rsp), %xmm0
movq 0x80(%rsp), %r8
callq 0x1b47
cmpl $0x1, %eax
jne 0x2391
xorpd %xmm0, %xmm0
movapd %xmm0, 0x40(%rsp)
movsd %xmm0, 0x30(%rsp)
cmpq $0x0, 0xe0(%rsp)
jle 0x22e5
cvttpd2dq 0x60(%rsp), %xmm0
movsd 0x10(%rsp), %xmm1
addsd 0x1172(%rip), %xmm1     # 0x3188
cvtdq2pd %xmm0, %xmm0
movq 0x150(%rsp), %r15
movq 0x120(%rsp), %rcx
movq 0xf8(%rsp), %rdi
movl 0xe8(%rsp), %r10d
cvttsd2si %xmm1, %eax
movsd 0x28(%rsp), %xmm1
mulsd %xmm0, %xmm1
movsd %xmm1, 0x28(%rsp)
imull %r10d, %eax
movslq %r10d, %rdx
movl %edx, %ebp
cltq
movl %r14d, %esi
movq %rsi, 0x98(%rsp)
leaq (%rcx,%rax,8), %rcx
movq %rcx, 0x10(%rsp)
movl %r10d, %ecx
imull %r14d, %ecx
movl %ecx, 0x5c(%rsp)
imull %r14d, %eax
movq %rax, 0x88(%rsp)
xorpd %xmm0, %xmm0
movsd %xmm0, 0x30(%rsp)
movapd %xmm0, 0x40(%rsp)
movl 0x18(%rsp), %ebx
movq %rdx, 0x90(%rsp)
leaq (,%rdx,8), %rax
movq %rax, (%rsp)
leaq (,%rbp,8), %r13
xorl %eax, %eax
movq %rax, 0x60(%rsp)
movl %r14d, %esi
movl 0x20(%rsp), %edx
movsd 0x50(%rsp), %xmm0
movq 0x100(%rsp), %rcx
movq 0x110(%rsp), %r8
movq 0x80(%rsp), %r9
pushq 0x148(%rsp)
pushq 0x148(%rsp)
pushq %r10
leaq 0xb8(%rsp), %rax
pushq %rax
callq 0x17ce
addq $0x20, %rsp
cmpl $0x1, %eax
jne 0x2391
testl %r14d, %r14d
movq 0xf8(%rsp), %r8
movl 0xe8(%rsp), %r9d
movq 0x98(%rsp), %r10
movl 0x5c(%rsp), %r11d
jle 0x2168
xorl %eax, %eax
movq 0x118(%rsp), %rcx
xorl %edx, %edx
testl %r9d, %r9d
jle 0x2159
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, %rbp
jne 0x2147
incq %rdx
addq (%rsp), %rcx
addl %r11d, %eax
cmpq %r10, %rdx
jne 0x2139
movq 0x120(%rsp), %rdi
movl %r14d, %esi
movsd 0x50(%rsp), %xmm0
movq 0x118(%rsp), %rdx
movl %r9d, %ecx
movq %r15, %r8
callq 0x1c9a
cmpl $0x1, %eax
jne 0x2391
movl 0x18(%rsp), %esi
testl %esi, %esi
movq 0xf8(%rsp), %r9
movl 0xe8(%rsp), %r10d
jle 0x21ef
movq 0x88(%rsp), %rax
movq 0x128(%rsp), %rcx
xorl %edx, %edx
testl %r10d, %r10d
jle 0x21e0
movslq %eax, %rdi
leaq (%r9,%rdi,8), %r8
xorl %edi, %edi
movsd (%r8,%rdi,8), %xmm0
movsd %xmm0, (%rcx,%rdi,8)
incq %rdi
cmpq %rdi, %rbp
jne 0x21cd
incq %rdx
addq (%rsp), %rcx
addl %r10d, %eax
cmpq %rbx, %rdx
jne 0x21bf
movq 0x130(%rsp), %rdi
movsd 0x28(%rsp), %xmm0
movq 0x128(%rsp), %rdx
movl %r10d, %ecx
movq %r15, %r8
movq %r10, %r14
callq 0x1c9a
cmpl $0x1, %eax
jne 0x2391
testl %r14d, %r14d
jle 0x22ad
movq 0x130(%rsp), %r14
xorl %r15d, %r15d
movsd 0xf88(%rip), %xmm0      # 0x31c0
cmpl $0x0, 0x18(%rsp)
jle 0x2269
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 0x2248
addsd 0xf57(%rip), %xmm0      # 0x31c0
xorpd %xmm1, %xmm1
callq 0x14e4
movq 0x10(%rsp), %rax
mulsd (%rax,%r15,8), %xmm0
movapd 0x40(%rsp), %xmm1
addsd %xmm0, %xmm1
movapd %xmm1, 0x40(%rsp)
mulsd %xmm0, %xmm0
movsd 0x30(%rsp), %xmm1
addsd %xmm0, %xmm1
movsd %xmm1, 0x30(%rsp)
incq %r15
addq $0x8, %r14
cmpq %rbp, %r15
jne 0x2230
movq 0x60(%rsp), %rax
addq 0x90(%rsp), %rax
cmpq 0xe0(%rsp), %rax
movl 0x24(%rsp), %r14d
movq 0x150(%rsp), %r15
movq 0xf8(%rsp), %rdi
movl 0xe8(%rsp), %r10d
jl 0x20b6
xorps %xmm2, %xmm2
cvtsi2sdq 0xe0(%rsp), %xmm2
movapd 0x40(%rsp), %xmm0
mulsd %xmm0, %xmm0
divsd %xmm2, %xmm0
movsd 0x30(%rsp), %xmm1
subsd %xmm0, %xmm1
movsd 0xeae(%rip), %xmm0      # 0x31c0
addsd %xmm2, %xmm0
divsd %xmm0, %xmm1
movapd %xmm1, %xmm0
ucomisd 0xd82(%rip), %xmm1      # 0x30a8
movapd %xmm2, (%rsp)
jb 0x2336
xorps %xmm3, %xmm3
sqrtsd %xmm0, %xmm3
jmp 0x2344
callq 0x1080
movapd (%rsp), %xmm2
movapd %xmm0, %xmm3
xorpd %xmm0, %xmm0
ucomisd %xmm0, %xmm2
jb 0x2357
xorps %xmm0, %xmm0
sqrtsd %xmm2, %xmm0
jmp 0x2371
movapd %xmm2, %xmm0
movapd %xmm3, 0x30(%rsp)
callq 0x1080
movapd 0x30(%rsp), %xmm3
movapd (%rsp), %xmm2
movq 0x78(%rsp), %rax
movapd 0x40(%rsp), %xmm1
unpcklpd %xmm3, %xmm1            # xmm1 = xmm1[0],xmm3[0]
unpcklpd %xmm0, %xmm2            # xmm2 = xmm2[0],xmm0[0]
divpd %xmm2, %xmm1
movupd %xmm1, (%rax)
movl $0x1, %eax
addq $0xa8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop