Click or drag to resize
gmp_libmpn_sec_div_qr Method
Set Q to the truncated quotient N / D and R to N modulo D, where N = {np, nn}, D = {dp, dn}, Q’s most significant limb is the function return value and the remaining limbs are {qp, nn - dn}, and R = {np, dn}.

Namespace:  Math.Gmp.Native
Assembly:  Math.Gmp.Native (in Math.Gmp.Native.dll) Version: 1.0.0.0 (1.0.0.0)
Syntax
public static mp_limb_t mpn_sec_div_qr(
	mp_ptr qp,
	mp_ptr np,
	mp_size_t nn,
	mp_ptr dp,
	mp_size_t dn,
	mp_ptr tp
)

Parameters

qp
Type: Math.Gmp.Nativemp_ptr
The quotient result operand.
np
Type: Math.Gmp.Nativemp_ptr
The first operand and remainder result integer.
nn
Type: Math.Gmp.Nativemp_size_t
The number of limbs of np.
dp
Type: Math.Gmp.Nativemp_ptr
The second operand integer.
dn
Type: Math.Gmp.Nativemp_size_t
The number of limbs of dp.
tp
Type: Math.Gmp.Nativemp_ptr
The scratch operand integer.

Return Value

Type: mp_limb_t
Q’s most significant limb.
Remarks

It is required that nndn ≥ 1, and that dp[dn - 1] ≠ 0. This does not imply that N ≥ D since N might be zero-padded.

Note the overlapping between N and R. No other operand overlapping is allowed. The entire space occupied by N is overwritten.

This function requires scratch space of mpn_sec_div_qr_itch(nn, dn) limbs to be passed in the tp parameter.

Examples
// Create multi-precision operands, and expected result.
mp_ptr np = new mp_ptr(new uint[] { 0xffffffff, 0x0000ffff });
mp_ptr dp = new mp_ptr(new uint[] { 0x00000003 });
mp_ptr remainder = new mp_ptr(new uint[] { 0x00000000 });
mp_ptr qp = new mp_ptr(new uint[np.Size]);

// Create scratch space.
mp_size_t size = gmp_lib.mpn_sec_div_qr_itch(np.Size, dp.Size);
mp_ptr tp = new mp_ptr(size);

// Set qp = floor(np / dp) and rp = np mod dp.
mp_limb_t mslimb = gmp_lib.mpn_sec_div_qr(qp, np, np.Size, dp, dp.Size, tp);

// Assert result of operation.
Assert.IsTrue(mslimb == (ulong)(gmp_lib.mp_bytes_per_limb == 4 ? 0x00005555 : 0x0000555555555555));
Assert.IsTrue(qp[0] == (ulong)(gmp_lib.mp_bytes_per_limb == 4 ? 0x55555555 : 0x0000000000000000));
Assert.IsTrue(np[0] == remainder[0]);

// Release unmanaged memory.
gmp_lib.free(qp, np, dp, remainder, tp);
See Also