// File: /workspace/github_cpp_makefile_star2_O2/marxn[P]pupp/3rdparty/gmp/mpn/invertappr.c
// Function: _gmpn_ni_invertappr
mp_limb_t
mpn_ni_invertappr (mp_ptr ip, mp_srcptr dp, mp_size_t n, mp_ptr scratch)
{
  mp_limb_t cy;
  mp_size_t rn, mn;
  mp_size_t sizes[NPOWS], *sizp;
  mp_ptr tp;
  TMP_DECL;
#define xp scratch

  ASSERT (n > 4);
  ASSERT (dp[n-1] & GMP_NUMB_HIGHBIT);
  ASSERT (! MPN_OVERLAP_P (ip, n, dp, n));
  ASSERT (! MPN_OVERLAP_P (ip, n, scratch, mpn_invertappr_itch(n)));
  ASSERT (! MPN_OVERLAP_P (dp, n, scratch, mpn_invertappr_itch(n)));

  /* Compute the computation precisions from highest to lowest, leaving the
     base case size in 'rn'.  */
  sizp = sizes;
  rn = n;
  do {
    *sizp = rn;
    rn = (rn >> 1) + 1;
    ++sizp;
  } while (ABOVE_THRESHOLD (rn, INV_NEWTON_THRESHOLD));

  /* We search the inverse of 0.{dp,n}, we compute it as 1.{ip,n} */
  dp += n;
  ip += n;

  /* Compute a base value of rn limbs. */
  mpn_bc_invertappr (ip - rn, dp - rn, rn, scratch);

  TMP_MARK;

  if (ABOVE_THRESHOLD (n, INV_MULMOD_BNM1_THRESHOLD))
    {
      mn = mpn_mulmod_bnm1_next_size (n + 1);
      tp = TMP_ALLOC_LIMBS (mpn_mulmod_bnm1_itch (mn, n, (n >> 1) + 1));
    }
  /* Use Newton's iterations to get the desired precision.*/

  while (1) {
    n = *--sizp;
    /*
      v    n  v
      +----+--+
      ^ rn ^
    */

    /* Compute i_jd . */
    if (BELOW_THRESHOLD (n, INV_MULMOD_BNM1_THRESHOLD)
	|| ((mn = mpn_mulmod_bnm1_next_size (n + 1)) > (n + rn))) {
      /* FIXME: We do only need {xp,n+1}*/
      mpn_mul (xp, dp - n, n, ip - rn, rn);
      mpn_add_n (xp + rn, xp + rn, dp - n, n - rn + 1);
      cy = CNST_LIMB(1); /* Remember we truncated, Mod B^(n+1) */
      /* We computed (truncated) {xp,n+1} <- 1.{ip,rn} * 0.{dp,n} */
    } else { /* Use B^mn-1 wraparound */
      mpn_mulmod_bnm1 (xp, mn, dp - n, n, ip - rn, rn, tp);
      /* We computed {xp,mn} <- {ip,rn} * {dp,n} mod (B^mn-1) */
      /* We know that 2*|ip*dp + dp*B^rn - B^{rn+n}| < B^mn-1 */
      /* Add dp*B^rn mod (B^mn-1) */
      ASSERT (n >= mn - rn);
      cy = mpn_add_n (xp + rn, xp + rn, dp - n, mn - rn);
      cy = mpn_add_nc (xp, xp, dp - (n - (mn - rn)), n - (mn - rn), cy);
      /* Subtract B^{rn+n}, maybe only compensate the carry*/
      xp[mn] = CNST_LIMB (1); /* set a limit for DECR_U */
      MPN_DECR_U (xp + rn + n - mn, 2 * mn + 1 - rn - n, CNST_LIMB (1) - cy);
      MPN_DECR_U (xp, mn, CNST_LIMB (1) - xp[mn]); /* if DECR_U eroded xp[mn] */
      cy = CNST_LIMB(0); /* Remember we are working Mod B^mn-1 */
    }

    if (xp[n] < CNST_LIMB (2)) { /* "positive" residue class */
      cy = xp[n]; /* 0 <= cy <= 1 here. */
#if HAVE_NATIVE_mpn_sublsh1_n
      if (cy++) {
	if (mpn_cmp (xp, dp - n, n) > 0) {
	  mp_limb_t chk;
	  chk = mpn_sublsh1_n (xp, xp, dp - n, n);
	  ASSERT (chk == xp[n]);
	  ++ cy;
	} else
	  ASSERT_CARRY (mpn_sub_n (xp, xp, dp - n, n));
      }
#else /* no mpn_sublsh1_n*/
      if (cy++ && !mpn_sub_n (xp, xp, dp - n, n)) {
	ASSERT_CARRY (mpn_sub_n (xp, xp, dp - n, n));
	++cy;
      }
#endif
      /* 1 <= cy <= 3 here. */
#if HAVE_NATIVE_mpn_rsblsh1_n
      if (mpn_cmp (xp, dp - n, n) > 0) {
	ASSERT_NOCARRY (mpn_rsblsh1_n (xp + n, xp, dp - n, n));
	++cy;
      } else
	ASSERT_NOCARRY (mpn_sub_nc (xp + 2 * n - rn, dp - rn, xp + n - rn, rn, mpn_cmp (xp, dp - n, n - rn) > 0));
#else /* no mpn_rsblsh1_n*/
      if (mpn_cmp (xp, dp - n, n) > 0) {
	ASSERT_NOCARRY (mpn_sub_n (xp, xp, dp - n, n));
	++cy;
      }
      ASSERT_NOCARRY (mpn_sub_nc (xp + 2 * n - rn, dp - rn, xp + n - rn, rn, mpn_cmp (xp, dp - n, n - rn) > 0));
#endif
      MPN_DECR_U(ip - rn, rn, cy); /* 1 <= cy <= 4 here. */
    } else { /* "negative" residue class */
      ASSERT (xp[n] >= GMP_NUMB_MAX - CNST_LIMB(1));
      MPN_DECR_U(xp, n + 1, cy);
      if (xp[n] != GMP_NUMB_MAX) {
	MPN_INCR_U(ip - rn, rn, CNST_LIMB (1));
	ASSERT_CARRY (mpn_add_n (xp, xp, dp - n, n));
      }
      mpn_com (xp + 2 * n - rn, xp + n - rn, rn);
    }

    /* Compute x_ju_j. FIXME:We need {xp+rn,rn}, mulhi? */
    mpn_mul_n (xp, xp + 2 * n - rn, ip - rn, rn);
    cy = mpn_add_n (xp + rn, xp + rn, xp + 2 * n - rn, 2 * rn - n);
    cy = mpn_add_nc (ip - n, xp + 3 * rn - n, xp + n + rn, n - rn, cy);
    MPN_INCR_U (ip - rn, rn, cy);
    if (sizp == sizes) { /* Get out of the cycle */
      /* Check for possible carry propagation from below. */
      cy = xp[3 * rn - n - 1] > GMP_NUMB_MAX - CNST_LIMB (7); /* Be conservative. */
      /*    cy = mpn_add_1 (xp + rn, xp + rn, 2*rn - n, 4); */
      break;
    }
    rn = n;
  }
  TMP_FREE;

  return cy;
#undef xp
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x1a8, %rsp            # imm = 0x1A8
movq %rcx, %rbx
movq %rdx, %r15
xorl %r13d, %r13d
movq %rdx, %r12
movq %r12, -0x1d0(%rbp,%r13)
movq %r12, %rax
sarq %rax
leaq 0x1(%rax), %r12
addq $0x8, %r13
cmpq $0xf4, %rax
jg 0x2b988
leaq (%rsi,%r15,8), %rcx
leaq (%rdi,%r15,8), %rdx
movq %rdx, -0x38(%rbp)
notq %rax
leaq (%rdx,%rax,8), %rdi
movq %rcx, -0x30(%rbp)
leaq (%rcx,%rax,8), %rsi
movq %r12, %rdx
movq %rbx, %rcx
callq 0x2be12
andq $0x0, -0x70(%rbp)
leaq 0x1(%r15), %rdi
callq 0x253ca
movq %r15, %rcx
sarq %rcx
movq %rax, %rdx
sarq %rdx
cmpq %rcx, %rdx
movq %rax, %rcx
cmovgq %rdx, %rcx
xorl %esi, %esi
cmpq %r15, %rdx
cmovlq %rcx, %rsi
addq %rax, %rsi
leaq 0x20(,%rsi,8), %rsi
cmpq $0x7f00, %rsi           # imm = 0x7F00
movq %rbx, -0x50(%rbp)
ja 0x2bdf9
movq %rsp, %rax
addq $0xf, %rsi
andq $-0x10, %rsi
subq %rsi, %rax
movq %rax, -0x60(%rbp)
movq %rax, %rsp
movq -0x50(%rbp), %r15
movq -0x1d8(%rbp,%r13), %r14
leaq 0x1(%r14), %rdi
callq 0x253ca
movq %rax, %rbx
leaq (%r14,%r12), %rax
leaq (%r15,%r12,8), %rcx
movq %rcx, -0x58(%rbp)
cmpq %rax, %rbx
movq %r14, -0x40(%rbp)
movq %r13, -0x88(%rbp)
jle 0x2babc
leaq (,%r14,8), %rax
movq -0x30(%rbp), %r13
movq %r15, %rbx
movq %r13, %r15
subq %rax, %r15
leaq (,%r12,8), %rax
movq -0x38(%rbp), %rcx
subq %rax, %rcx
movq %rbx, %rdi
movq %r15, %rsi
movq -0x40(%rbp), %rdx
movq %r12, %r8
callq 0x18a58
movq -0x40(%rbp), %rcx
subq %r12, %rcx
incq %rcx
movq -0x58(%rbp), %r14
movq %r14, %rdi
movq %r14, %rsi
movq %r15, %rdx
movq -0x40(%rbp), %r15
callq 0x17cf0
pushq $0x1
popq %rax
jmp 0x2bb8b
leaq (,%r14,8), %rax
movq -0x30(%rbp), %r13
subq %rax, %r13
leaq (,%r12,8), %rax
movq -0x38(%rbp), %r8
subq %rax, %r8
subq $0x8, %rsp
movq %r15, %rdi
movq %rbx, %rsi
movq %r13, %rdx
movq %r14, %rcx
movq %r12, %r9
pushq -0x60(%rbp)
callq 0x24f3e
popq %rax
popq %rcx
movq %r15, %r14
movq %rbx, %r15
subq %r12, %r15
movq -0x58(%rbp), %rdi
movq %rdi, %rsi
movq %r13, %rdx
movq %r15, %rcx
callq 0x17cf0
movq -0x40(%rbp), %rcx
subq %r15, %rcx
movq -0x40(%rbp), %r15
leaq (,%rcx,8), %rsi
movq -0x30(%rbp), %r13
movq %r13, %rdx
subq %rsi, %rdx
movq %r14, %rdi
movq %r14, %rsi
movq %rax, %r8
callq 0x17cd0
movq %rbx, %rcx
shlq $0x3, %rcx
pushq $0x1
popq %rsi
movq %rsi, (%r14,%rbx,8)
movq %rsi, %rdx
subq %rax, %rdx
movq -0x58(%rbp), %rdi
leaq (%rdi,%r15,8), %rax
subq %rcx, %rax
subq %rdx, (%rax)
jae 0x2bb6c
subq $0x1, 0x8(%rax)
leaq 0x8(%rax), %rax
jb 0x2bb61
movq %rsi, %rax
subq (%r14,%rbx,8), %rax
movq %r14, %rcx
subq %rax, (%rcx)
jae 0x2bb86
subq $0x1, 0x8(%rcx)
leaq 0x8(%rcx), %rcx
jb 0x2bb7b
xorl %eax, %eax
movq %r14, %rbx
movq %r12, %r14
leaq (%rbx,%r15,8), %r12
movq (%r12), %rcx
cmpq $0x1, %rcx
movq %r12, -0x80(%rbp)
ja 0x2bbe9
movq %r15, %rdx
negq %rdx
pushq $0x1
popq %rax
testq %rcx, %rcx
movq %rdx, -0x48(%rbp)
je 0x2bc86
leaq (%r13,%rdx,8), %r13
movq %rbx, %rdi
movq %r13, %rsi
movq %r15, %rdx
callq 0x1ac5c
movq %rbx, %rdi
movq %rbx, %rsi
movq %r13, %rdx
movq %r15, %rcx
testl %eax, %eax
jle 0x2bc76
callq 0x292c0
pushq $0x3
jmp 0x2bc7d
movq %r13, %rdx
movq %rbx, %rcx
subq %rax, (%rcx)
jae 0x2bbff
subq $0x1, 0x8(%rcx)
leaq 0x8(%rcx), %rcx
jb 0x2bbf4
movq %r14, %r13
negq %r13
cmpq $-0x1, (%r12)
je 0x2bc43
movq -0x38(%rbp), %rax
leaq (%rax,%r13,8), %rax
addq $0x1, (%rax)
leaq 0x8(%rax), %rax
jb 0x2bc14
movq %r15, %rax
negq %rax
movq %rax, -0x48(%rbp)
leaq (,%r15,8), %rax
subq %rax, %rdx
movq %rbx, %rdi
movq %rbx, %rsi
movq %r15, %rcx
callq 0x17cf0
jmp 0x2bc4d
movq %r15, %rax
negq %rax
movq %rax, -0x48(%rbp)
movq %r15, %rax
shlq $0x4, %rax
addq %rbx, %rax
leaq (%rax,%r13,8), %rdi
leaq (%r12,%r13,8), %rsi
movq %r14, %rdx
callq 0x18000
movq %r14, %r12
movq %r15, %r14
movq -0x38(%rbp), %rsi
jmp 0x2bd43
callq 0x17ed0
pushq $0x2
popq %rax
movq -0x48(%rbp), %rdx
movq -0x30(%rbp), %r13
movq %rax, -0x68(%rbp)
leaq (%r13,%rdx,8), %r13
movq %rbx, %rdi
movq %r13, %rsi
movq %r15, %rdx
callq 0x1ac5c
testl %eax, %eax
jle 0x2bcbb
movq %r12, %rdi
movq %rbx, %rsi
movq %r13, %rdx
movq %r15, %rcx
callq 0x293e0
movq -0x68(%rbp), %rdx
incq %rdx
jmp 0x2bd14
movq %r15, %rbx
shlq $0x4, %rbx
addq -0x50(%rbp), %rbx
leaq (,%r14,8), %rax
subq %rax, %rbx
movq -0x30(%rbp), %rcx
subq %rax, %rcx
movq %rcx, -0x78(%rbp)
subq %rax, %r12
subq %r14, %r15
movq -0x50(%rbp), %rdi
movq %r13, %rsi
movq %r15, %rdx
callq 0x1ac5c
xorl %r8d, %r8d
testl %eax, %eax
setg %r8b
movq %rbx, %rdi
movq -0x50(%rbp), %rbx
movq -0x78(%rbp), %rsi
movq %r12, %rdx
movq %r14, %rcx
callq 0x17eb0
movq -0x68(%rbp), %rdx
movq %r14, %r13
negq %r13
leaq (,%r14,8), %rax
movq -0x38(%rbp), %rsi
movq %rsi, %rcx
subq %rax, %rcx
subq %rdx, (%rcx)
jae 0x2bd3c
subq $0x1, 0x8(%rcx)
leaq 0x8(%rcx), %rcx
jb 0x2bd31
movq %r14, %r12
movq -0x40(%rbp), %r14
movq %r14, %rax
shlq $0x4, %rax
addq %rbx, %rax
movq %rbx, %r15
leaq (%rax,%r13,8), %rbx
leaq (%rsi,%r13,8), %r13
movq %r15, %rdi
movq %rbx, %rsi
movq %r13, %rdx
movq %r12, %rcx
callq 0x19500
leaq (%r12,%r12), %rcx
subq %r14, %rcx
movq -0x58(%rbp), %rdi
movq %rdi, %rsi
movq %rbx, %rdx
callq 0x17cf0
movq -0x48(%rbp), %rcx
movq -0x38(%rbp), %rdx
leaq (%rdx,%rcx,8), %rdi
imulq $0x18, %r12, %rbx
addq %r15, %rbx
leaq (%rbx,%rcx,8), %rsi
movq -0x80(%rbp), %rcx
leaq (%rcx,%r12,8), %rdx
movq %r14, %rcx
subq %r12, %rcx
movq %rax, %r8
callq 0x17cd0
addq %rax, (%r13)
jae 0x2bdbd
addq $0x1, 0x8(%r13)
leaq 0x8(%r13), %r13
jb 0x2bdb2
movq %r14, %r12
movq -0x88(%rbp), %r13
addq $-0x8, %r13
jne 0x2ba2e
notq %r14
movq (%rbx,%r14,8), %rbx
movq -0x70(%rbp), %rdi
testq %rdi, %rdi
jne 0x2be0b
xorl %eax, %eax
cmpq $-0x7, %rbx
setae %al
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq -0x70(%rbp), %rdi
callq 0x29dc0
movq %rax, -0x60(%rbp)
jmp 0x2ba2a
callq 0x29df3
jmp 0x2bde1