1 /* hgcd_step.c. 2 3 THE FUNCTIONS IN THIS FILE ARE INTERNAL WITH MUTABLE INTERFACES. IT IS ONLY 4 SAFE TO REACH THEM THROUGH DOCUMENTED INTERFACES. IN FACT, IT IS ALMOST 5 GUARANTEED THAT THEY'LL CHANGE OR DISAPPEAR IN A FUTURE GNU MP RELEASE. 6 7 Copyright 2003, 2004, 2005, 2008, 2011, 2012 Free Software Foundation, Inc. 8 9 This file is part of the GNU MP Library. 10 11 The GNU MP Library is free software; you can redistribute it and/or modify 12 it under the terms of the GNU Lesser General Public License as published by 13 the Free Software Foundation; either version 3 of the License, or (at your 14 option) any later version. 15 16 The GNU MP Library is distributed in the hope that it will be useful, but 17 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 18 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public 19 License for more details. 20 21 You should have received a copy of the GNU Lesser General Public License 22 along with the GNU MP Library. If not, see http://www.gnu.org/licenses/. */ 23 24 #include "gmp.h" 25 #include "gmp-impl.h" 26 #include "longlong.h" 27 28 29 static void 30 hgcd_hook (void *p, mp_srcptr gp, mp_size_t gn, 31 mp_srcptr qp, mp_size_t qn, int d) 32 { 33 ASSERT (!gp); 34 ASSERT (d >= 0); 35 ASSERT (d <= 1); 36 37 MPN_NORMALIZE (qp, qn); 38 if (qn > 0) 39 { 40 struct hgcd_matrix *M = (struct hgcd_matrix *) p; 41 /* NOTES: This is a bit ugly. A tp area is passed to 42 gcd_subdiv_step, which stores q at the start of that area. We 43 now use the rest. */ 44 mp_ptr tp = (mp_ptr) qp + qn; 45 mpn_hgcd_matrix_update_q (M, qp, qn, d, tp); 46 } 47 } 48 49 /* Perform a few steps, using some of mpn_hgcd2, subtraction and 50 division. Reduces the size by almost one limb or more, but never 51 below the given size s. Return new size for a and b, or 0 if no 52 more steps are possible. 53 54 If hgcd2 succeds, needs temporary space for hgcd_matrix_mul_1, M->n 55 limbs, and hgcd_mul_matrix1_inverse_vector, n limbs. If hgcd2 56 fails, needs space for the quotient, qn <= n - s limbs, for and 57 hgcd_matrix_update_q, qn + (size of the appropriate column of M) <= 58 (resulting size of M) + 1. 59 60 If N is the input size to the calling hgcd, then s = floor(N/2) + 61 1, M->n < N, qn + product size <= n - s + n - s + 1 = 2 (n - s) + 1 62 <= N. 63 */ 64 65 mp_size_t 66 mpn_hgcd_step (mp_size_t n, mp_ptr ap, mp_ptr bp, mp_size_t s, 67 struct hgcd_matrix *M, mp_ptr tp) 68 { 69 struct hgcd_matrix1 M1; 70 mp_limb_t mask; 71 mp_limb_t ah, al, bh, bl; 72 73 ASSERT (n > s); 74 75 mask = ap[n-1] | bp[n-1]; 76 ASSERT (mask > 0); 77 78 if (n == s + 1) 79 { 80 if (mask < 4) 81 goto subtract; 82 83 ah = ap[n-1]; al = ap[n-2]; 84 bh = bp[n-1]; bl = bp[n-2]; 85 } 86 else if (mask & GMP_NUMB_HIGHBIT) 87 { 88 ah = ap[n-1]; al = ap[n-2]; 89 bh = bp[n-1]; bl = bp[n-2]; 90 } 91 else 92 { 93 int shift; 94 95 count_leading_zeros (shift, mask); 96 ah = MPN_EXTRACT_NUMB (shift, ap[n-1], ap[n-2]); 97 al = MPN_EXTRACT_NUMB (shift, ap[n-2], ap[n-3]); 98 bh = MPN_EXTRACT_NUMB (shift, bp[n-1], bp[n-2]); 99 bl = MPN_EXTRACT_NUMB (shift, bp[n-2], bp[n-3]); 100 } 101 102 /* Try an mpn_hgcd2 step */ 103 if (mpn_hgcd2 (ah, al, bh, bl, &M1)) 104 { 105 /* Multiply M <- M * M1 */ 106 mpn_hgcd_matrix_mul_1 (M, &M1, tp); 107 108 /* Can't swap inputs, so we need to copy. */ 109 MPN_COPY (tp, ap, n); 110 /* Multiply M1^{-1} (a;b) */ 111 return mpn_matrix22_mul1_inverse_vector (&M1, ap, tp, bp, n); 112 } 113 114 subtract: 115 116 return mpn_gcd_subdiv_step (ap, bp, n, s, hgcd_hook, M, tp); 117 } 118