github.com/aergoio/aergo@v1.3.1/libtool/src/gmp-6.1.2/mpn/generic/dive_1.c (about) 1 /* mpn_divexact_1 -- mpn by limb exact division. 2 3 THE FUNCTIONS IN THIS FILE ARE FOR INTERNAL USE ONLY. THEY'RE ALMOST 4 CERTAIN TO BE SUBJECT TO INCOMPATIBLE CHANGES OR DISAPPEAR COMPLETELY IN 5 FUTURE GNU MP RELEASES. 6 7 Copyright 2000-2003, 2005, 2013 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 either: 13 14 * the GNU Lesser General Public License as published by the Free 15 Software Foundation; either version 3 of the License, or (at your 16 option) any later version. 17 18 or 19 20 * the GNU General Public License as published by the Free Software 21 Foundation; either version 2 of the License, or (at your option) any 22 later version. 23 24 or both in parallel, as here. 25 26 The GNU MP Library is distributed in the hope that it will be useful, but 27 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 28 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 29 for more details. 30 31 You should have received copies of the GNU General Public License and the 32 GNU Lesser General Public License along with the GNU MP Library. If not, 33 see https://www.gnu.org/licenses/. */ 34 35 #include "gmp.h" 36 #include "gmp-impl.h" 37 #include "longlong.h" 38 39 40 41 /* Divide a={src,size} by d=divisor and store the quotient in q={dst,size}. 42 q will only be correct if d divides a exactly. 43 44 A separate loop is used for shift==0 because n<<GMP_LIMB_BITS doesn't 45 give zero on all CPUs (for instance it doesn't on the x86s). This 46 separate loop might run faster too, helping odd divisors. 47 48 Possibilities: 49 50 mpn_divexact_1c could be created, accepting and returning c. This would 51 let a long calculation be done piece by piece. Currently there's no 52 particular need for that, and not returning c means that a final umul can 53 be skipped. 54 55 Another use for returning c would be letting the caller know whether the 56 division was in fact exact. It would work just to return the carry bit 57 "c=(l>s)" and let the caller do a final umul if interested. 58 59 When the divisor is even, the factors of two could be handled with a 60 separate mpn_rshift, instead of shifting on the fly. That might be 61 faster on some CPUs and would mean just the shift==0 style loop would be 62 needed. 63 64 If n<<GMP_LIMB_BITS gives zero on a particular CPU then the separate 65 shift==0 loop is unnecessary, and could be eliminated if there's no great 66 speed difference. 67 68 It's not clear whether "/" is the best way to handle size==1. Alpha gcc 69 2.95 for instance has a poor "/" and might prefer the modular method. 70 Perhaps a tuned parameter should control this. 71 72 If src[size-1] < divisor then dst[size-1] will be zero, and one divide 73 step could be skipped. A test at last step for s<divisor (or ls in the 74 even case) might be a good way to do that. But if this code is often 75 used with small divisors then it might not be worth bothering */ 76 77 void 78 mpn_divexact_1 (mp_ptr dst, mp_srcptr src, mp_size_t size, mp_limb_t divisor) 79 { 80 mp_size_t i; 81 mp_limb_t c, h, l, ls, s, s_next, inverse, dummy; 82 unsigned shift; 83 84 ASSERT (size >= 1); 85 ASSERT (divisor != 0); 86 ASSERT (MPN_SAME_OR_SEPARATE_P (dst, src, size)); 87 ASSERT_MPN (src, size); 88 ASSERT_LIMB (divisor); 89 90 if ((divisor & 1) == 0) 91 { 92 count_trailing_zeros (shift, divisor); 93 divisor >>= shift; 94 } 95 else 96 shift = 0; 97 98 binvert_limb (inverse, divisor); 99 divisor <<= GMP_NAIL_BITS; 100 101 if (shift != 0) 102 { 103 c = 0; 104 105 s = src[0]; 106 107 for (i = 1; i < size; i++) 108 { 109 s_next = src[i]; 110 ls = ((s >> shift) | (s_next << (GMP_NUMB_BITS-shift))) & GMP_NUMB_MASK; 111 s = s_next; 112 113 SUBC_LIMB (c, l, ls, c); 114 115 l = (l * inverse) & GMP_NUMB_MASK; 116 dst[i - 1] = l; 117 118 umul_ppmm (h, dummy, l, divisor); 119 c += h; 120 } 121 122 ls = s >> shift; 123 l = ls - c; 124 l = (l * inverse) & GMP_NUMB_MASK; 125 dst[size - 1] = l; 126 } 127 else 128 { 129 s = src[0]; 130 131 l = (s * inverse) & GMP_NUMB_MASK; 132 dst[0] = l; 133 c = 0; 134 135 for (i = 1; i < size; i++) 136 { 137 umul_ppmm (h, dummy, l, divisor); 138 c += h; 139 140 s = src[i]; 141 SUBC_LIMB (c, l, s, c); 142 143 l = (l * inverse) & GMP_NUMB_MASK; 144 dst[i] = l; 145 } 146 } 147 }