1 | /* mpz_tdiv_r_ui(rem, dividend, divisor_limb) |
---|
2 | -- Set REM to DIVDEND mod DIVISOR_LIMB. |
---|
3 | |
---|
4 | Copyright 1991, 1993, 1994, 1996, 1998, 2001, 2002 Free Software Foundation, |
---|
5 | Inc. |
---|
6 | |
---|
7 | This file is part of the GNU MP Library. |
---|
8 | |
---|
9 | The GNU MP Library is free software; you can redistribute it and/or modify |
---|
10 | it under the terms of the GNU Lesser General Public License as published by |
---|
11 | the Free Software Foundation; either version 2.1 of the License, or (at your |
---|
12 | option) any later version. |
---|
13 | |
---|
14 | The GNU MP Library is distributed in the hope that it will be useful, but |
---|
15 | WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY |
---|
16 | or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public |
---|
17 | License for more details. |
---|
18 | |
---|
19 | You should have received a copy of the GNU Lesser General Public License |
---|
20 | along with the GNU MP Library; see the file COPYING.LIB. If not, write to |
---|
21 | the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, |
---|
22 | MA 02111-1307, USA. */ |
---|
23 | |
---|
24 | #include "gmp.h" |
---|
25 | #include "gmp-impl.h" |
---|
26 | |
---|
27 | unsigned long int |
---|
28 | mpz_tdiv_r_ui (mpz_ptr rem, mpz_srcptr dividend, unsigned long int divisor) |
---|
29 | { |
---|
30 | mp_size_t ns, nn; |
---|
31 | mp_ptr np; |
---|
32 | mp_limb_t rl; |
---|
33 | |
---|
34 | if (divisor == 0) |
---|
35 | DIVIDE_BY_ZERO; |
---|
36 | |
---|
37 | ns = SIZ(dividend); |
---|
38 | if (ns == 0) |
---|
39 | { |
---|
40 | SIZ(rem) = 0; |
---|
41 | return 0; |
---|
42 | } |
---|
43 | |
---|
44 | nn = ABS(ns); |
---|
45 | np = PTR(dividend); |
---|
46 | #if GMP_NAIL_BITS != 0 |
---|
47 | if (divisor > GMP_NUMB_MAX) |
---|
48 | { |
---|
49 | mp_limb_t dp[2]; |
---|
50 | mp_ptr rp, qp; |
---|
51 | mp_size_t rn; |
---|
52 | TMP_DECL (mark); |
---|
53 | |
---|
54 | if (nn == 1) /* tdiv_qr requirements; tested above for 0 */ |
---|
55 | { |
---|
56 | rl = np[0]; |
---|
57 | SIZ(rem) = ns >= 0 ? 1 : -1; |
---|
58 | PTR(rem)[0] = rl; |
---|
59 | return rl; |
---|
60 | } |
---|
61 | |
---|
62 | MPZ_REALLOC (rem, 2); |
---|
63 | rp = PTR(rem); |
---|
64 | |
---|
65 | TMP_MARK (mark); |
---|
66 | dp[0] = divisor & GMP_NUMB_MASK; |
---|
67 | dp[1] = divisor >> GMP_NUMB_BITS; |
---|
68 | qp = TMP_ALLOC_LIMBS (nn - 2 + 1); |
---|
69 | mpn_tdiv_qr (qp, rp, (mp_size_t) 0, np, nn, dp, (mp_size_t) 2); |
---|
70 | TMP_FREE (mark); |
---|
71 | rl = rp[0] + (rp[1] << GMP_NUMB_BITS); |
---|
72 | rn = 2 - (rp[1] == 0); rn -= (rp[rn - 1] == 0); |
---|
73 | SIZ(rem) = ns >= 0 ? rn : -rn; |
---|
74 | } |
---|
75 | else |
---|
76 | #endif |
---|
77 | { |
---|
78 | rl = mpn_mod_1 (np, nn, (mp_limb_t) divisor); |
---|
79 | if (rl == 0) |
---|
80 | SIZ(rem) = 0; |
---|
81 | else |
---|
82 | { |
---|
83 | /* Store the single-limb remainder. We don't check if there's space |
---|
84 | for just one limb, since no function ever makes zero space. */ |
---|
85 | SIZ(rem) = ns >= 0 ? 1 : -1; |
---|
86 | PTR(rem)[0] = rl; |
---|
87 | } |
---|
88 | } |
---|
89 | |
---|
90 | return rl; |
---|
91 | } |
---|