lib/crypto/mpi/mpi-div.c

Source file repositories/reference/linux-study-clean/lib/crypto/mpi/mpi-div.c

File Facts

System
Linux kernel
Corpus path
lib/crypto/mpi/mpi-div.c
Extension
.c
Size
5693 bytes
Lines
231
Domain
Kernel Services
Bucket
lib
Inferred role
Kernel Services: implementation source
Status
source implementation candidate

Why This File Exists

Shared kernel service surface used by multiple subsystems, including helpers, cryptography, virtualization support, and async I/O infrastructure.

Dependency Surface

Detected Declarations

Annotated Snippet

if (num != rem) {
			rem->nlimbs = num->nlimbs;
			rem->sign = num->sign;
			MPN_COPY(rem->d, num->d, nsize);
		}
		if (quot) {
			/* This needs to follow the assignment to rem, in case the
			 * numerator and quotient are the same.
			 */
			quot->nlimbs = 0;
			quot->sign = 0;
		}
		return 0;
	}

	if (quot) {
		err = mpi_resize(quot, qsize);
		if (err)
			return err;
	}

	/* Read pointers here, when reallocation is finished.  */
	np = num->d;
	dp = den->d;
	rp = rem->d;

	/* Optimize division by a single-limb divisor.  */
	if (dsize == 1) {
		mpi_limb_t rlimb;
		if (quot) {
			qp = quot->d;
			rlimb = mpihelp_divmod_1(qp, np, nsize, dp[0]);
			qsize -= qp[qsize - 1] == 0;
			quot->nlimbs = qsize;
			quot->sign = sign_quotient;
		} else
			rlimb = mpihelp_mod_1(np, nsize, dp[0]);
		rp[0] = rlimb;
		rsize = rlimb != 0?1:0;
		rem->nlimbs = rsize;
		rem->sign = sign_remainder;
		return 0;
	}

	err = -ENOMEM;
	if (quot) {
		qp = quot->d;
		/* Make sure QP and NP point to different objects.  Otherwise the
		 * numerator would be gradually overwritten by the quotient limbs.
		 */
		if (qp == np) { /* Copy NP object to temporary space.  */
			np = marker[markidx++] = mpi_alloc_limb_space(nsize);
			if (!np)
				goto out_free_marker;
			MPN_COPY(np, qp, nsize);
		}
	} else /* Put quotient at top of remainder. */
		qp = rp + dsize;

	normalization_steps = count_leading_zeros(dp[dsize - 1]);

	/* Normalize the denominator, i.e. make its most significant bit set by
	 * shifting it NORMALIZATION_STEPS bits to the left.  Also shift the
	 * numerator the same number of steps (to keep the quotient the same!).
	 */
	if (normalization_steps) {
		mpi_ptr_t tp;
		mpi_limb_t nlimb;

		/* Shift up the denominator setting the most significant bit of
		 * the most significant word.  Use temporary storage not to clobber
		 * the original contents of the denominator.
		 */
		tp = marker[markidx++] = mpi_alloc_limb_space(dsize);
		if (!tp)
			goto out_free_marker;
		mpihelp_lshift(tp, dp, dsize, normalization_steps);
		dp = tp;

		/* Shift up the numerator, possibly introducing a new most
		 * significant word.  Move the shifted numerator in the remainder
		 * meanwhile.
		 */
		nlimb = mpihelp_lshift(rp, np, nsize, normalization_steps);
		if (nlimb) {
			rp[nsize] = nlimb;
			rsize = nsize + 1;
		} else
			rsize = nsize;
	} else {

Annotation

Implementation Notes