gmp_libmpn_sec_sub_1 Method |
Namespace: Math.Gmp.Native
public static mp_limb_t mpn_sec_sub_1( mp_ptr rp, mp_ptr ap, mp_size_t n, mp_limb_t b, mp_ptr tp )
This function takes O(N) time, unlike the leaky functions mpn_sub_1(mp_ptr, mp_ptr, mp_size_t, mp_limb_t) which is O(1) on average. It requires scratch space of mpn_sec_sub_1_itch(mp_size_t)(n) limbs, to be passed in the tp parameter. The scratch space requirements are guaranteed to be at most n limbs, and increase monotonously in the operand size.
// Create multi-precision operands, and expected result. mp_ptr ap = new mp_ptr(new uint[] { 0xffffffff, 0xffffffff }); mp_ptr result = new mp_ptr(new uint[] { 0xfffffffe, 0xffffffff }); mp_ptr rp = new mp_ptr(result.Size); // Create scratch space. mp_size_t size = gmp_lib.mpn_sec_sub_1_itch(ap.Size); mp_ptr tp = new mp_ptr(size); // Set rp = ap - 1. mp_limb_t borrow = gmp_lib.mpn_sec_sub_1(rp, ap, ap.Size, 1, tp); // Assert result of operation. Assert.IsTrue(borrow == 0); Assert.IsTrue(rp.SequenceEqual(result)); // Release unmanaged memory. gmp_lib.free(rp, ap, tp, result);