/src/gmp-6.2.1/mpz/tdiv_r.c
Line  | Count  | Source (jump to first uncovered line)  | 
1  |  | /* mpz_tdiv_r(rem, dividend, divisor) -- Set REM to DIVIDEND mod DIVISOR.  | 
2  |  |  | 
3  |  | Copyright 1991, 1993, 1994, 2000, 2001, 2005, 2012 Free Software Foundation,  | 
4  |  | Inc.  | 
5  |  |  | 
6  |  | This file is part of the GNU MP Library.  | 
7  |  |  | 
8  |  | The GNU MP Library is free software; you can redistribute it and/or modify  | 
9  |  | it under the terms of either:  | 
10  |  |  | 
11  |  |   * the GNU Lesser General Public License as published by the Free  | 
12  |  |     Software Foundation; either version 3 of the License, or (at your  | 
13  |  |     option) any later version.  | 
14  |  |  | 
15  |  | or  | 
16  |  |  | 
17  |  |   * the GNU General Public License as published by the Free Software  | 
18  |  |     Foundation; either version 2 of the License, or (at your option) any  | 
19  |  |     later version.  | 
20  |  |  | 
21  |  | or both in parallel, as here.  | 
22  |  |  | 
23  |  | The GNU MP Library is distributed in the hope that it will be useful, but  | 
24  |  | WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY  | 
25  |  | or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License  | 
26  |  | for more details.  | 
27  |  |  | 
28  |  | You should have received copies of the GNU General Public License and the  | 
29  |  | GNU Lesser General Public License along with the GNU MP Library.  If not,  | 
30  |  | see https://www.gnu.org/licenses/.  */  | 
31  |  |  | 
32  |  | #include "gmp-impl.h"  | 
33  |  | #include "longlong.h"  | 
34  |  |  | 
35  |  | void  | 
36  |  | mpz_tdiv_r (mpz_ptr rem, mpz_srcptr num, mpz_srcptr den)  | 
37  | 2.10k  | { | 
38  | 2.10k  |   mp_size_t ql;  | 
39  | 2.10k  |   mp_size_t ns, nl, dl;  | 
40  | 2.10k  |   mp_ptr np, dp, qp, rp;  | 
41  | 2.10k  |   TMP_DECL;  | 
42  |  |  | 
43  | 2.10k  |   ns = SIZ (num);  | 
44  | 2.10k  |   nl = ABS (ns);  | 
45  | 2.10k  |   dl = ABSIZ (den);  | 
46  | 2.10k  |   ql = nl - dl + 1;  | 
47  |  |  | 
48  | 2.10k  |   if (UNLIKELY (dl == 0))  | 
49  | 0  |     DIVIDE_BY_ZERO;  | 
50  |  |  | 
51  | 2.10k  |   if (ql <= 0)  | 
52  | 227  |     { | 
53  | 227  |       if (num != rem)  | 
54  | 138  |   { | 
55  | 138  |     SIZ (rem) = ns;  | 
56  | 138  |     rp = MPZ_NEWALLOC (rem, nl);  | 
57  | 138  |     np = PTR (num);  | 
58  | 138  |     MPN_COPY (rp, np, nl);  | 
59  | 138  |   }  | 
60  | 227  |       return;  | 
61  | 227  |     }  | 
62  |  |  | 
63  | 1.87k  |   rp = MPZ_REALLOC (rem, dl);  | 
64  |  |  | 
65  | 1.87k  |   TMP_MARK;  | 
66  | 1.87k  |   qp = TMP_ALLOC_LIMBS (ql);  | 
67  | 1.87k  |   np = PTR (num);  | 
68  | 1.87k  |   dp = PTR (den);  | 
69  |  |  | 
70  |  |   /* FIXME: We should think about how to handle the temporary allocation.  | 
71  |  |      Perhaps mpn_tdiv_qr should handle it, since it anyway often needs to  | 
72  |  |      allocate temp space.  */  | 
73  |  |  | 
74  |  |   /* Copy denominator to temporary space if it overlaps with the remainder.  */  | 
75  | 1.87k  |   if (dp == rp)  | 
76  | 0  |     { | 
77  | 0  |       mp_ptr tp;  | 
78  | 0  |       tp = TMP_ALLOC_LIMBS (dl);  | 
79  | 0  |       MPN_COPY (tp, dp, dl);  | 
80  | 0  |       dp = tp;  | 
81  | 0  |     }  | 
82  |  |   /* Copy numerator to temporary space if it overlaps with the remainder.  */  | 
83  | 1.87k  |   if (np == rp)  | 
84  | 472  |     { | 
85  | 472  |       mp_ptr tp;  | 
86  | 472  |       tp = TMP_ALLOC_LIMBS (nl);  | 
87  | 472  |       MPN_COPY (tp, np, nl);  | 
88  | 472  |       np = tp;  | 
89  | 472  |     }  | 
90  |  |  | 
91  | 1.87k  |   mpn_tdiv_qr (qp, rp, 0L, np, nl, dp, dl);  | 
92  |  |  | 
93  | 1.87k  |   MPN_NORMALIZE (rp, dl);  | 
94  |  |  | 
95  | 1.87k  |   SIZ (rem) = ns >= 0 ? dl : -dl;  | 
96  | 1.87k  |   TMP_FREE;  | 
97  | 1.87k  | }  |