Line  | Count  | Source (jump to first uncovered line)  | 
1  |  | /* mpz_mul -- Multiply two integers.  | 
2  |  |  | 
3  |  | Copyright 1991, 1993, 1994, 1996, 2000, 2001, 2005, 2009, 2011, 2012,  | 
4  |  | 2015 Free Software Foundation, 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 <stdio.h> /* for NULL */  | 
33  |  | #include "gmp-impl.h"  | 
34  |  |  | 
35  |  |  | 
36  |  | void  | 
37  |  | mpz_mul (mpz_ptr w, mpz_srcptr u, mpz_srcptr v)  | 
38  | 1.91k  | { | 
39  | 1.91k  |   mp_size_t usize;  | 
40  | 1.91k  |   mp_size_t vsize;  | 
41  | 1.91k  |   mp_size_t wsize;  | 
42  | 1.91k  |   mp_size_t sign_product;  | 
43  | 1.91k  |   mp_ptr up, vp;  | 
44  | 1.91k  |   mp_ptr wp;  | 
45  | 1.91k  |   mp_ptr free_me;  | 
46  | 1.91k  |   size_t free_me_size;  | 
47  | 1.91k  |   mp_limb_t cy_limb;  | 
48  | 1.91k  |   TMP_DECL;  | 
49  |  |  | 
50  | 1.91k  |   usize = SIZ (u);  | 
51  | 1.91k  |   vsize = SIZ (v);  | 
52  | 1.91k  |   sign_product = usize ^ vsize;  | 
53  | 1.91k  |   usize = ABS (usize);  | 
54  | 1.91k  |   vsize = ABS (vsize);  | 
55  |  |  | 
56  | 1.91k  |   if (usize < vsize)  | 
57  | 1.35k  |     { | 
58  | 1.35k  |       MPZ_SRCPTR_SWAP (u, v);  | 
59  | 1.35k  |       MP_SIZE_T_SWAP (usize, vsize);  | 
60  | 1.35k  |     }  | 
61  |  |  | 
62  | 1.91k  |   if (vsize == 0)  | 
63  | 28  |     { | 
64  | 28  |       SIZ (w) = 0;  | 
65  | 28  |       return;  | 
66  | 28  |     }  | 
67  |  |  | 
68  | 1.88k  | #if HAVE_NATIVE_mpn_mul_2  | 
69  | 1.88k  |   if (vsize <= 2)  | 
70  | 370  |     { | 
71  | 370  |       wp = MPZ_REALLOC (w, usize+vsize);  | 
72  | 370  |       if (vsize == 1)  | 
73  | 317  |   cy_limb = mpn_mul_1 (wp, PTR (u), usize, PTR (v)[0]);  | 
74  | 53  |       else  | 
75  | 53  |   { | 
76  | 53  |     cy_limb = mpn_mul_2 (wp, PTR (u), usize, PTR (v));  | 
77  | 53  |     usize++;  | 
78  | 53  |   }  | 
79  | 370  |       wp[usize] = cy_limb;  | 
80  | 370  |       usize += (cy_limb != 0);  | 
81  | 370  |       SIZ (w) = (sign_product >= 0 ? usize : -usize);  | 
82  | 370  |       return;  | 
83  | 370  |     }  | 
84  |  | #else  | 
85  |  |   if (vsize == 1)  | 
86  |  |     { | 
87  |  |       wp = MPZ_REALLOC (w, usize+1);  | 
88  |  |       cy_limb = mpn_mul_1 (wp, PTR (u), usize, PTR (v)[0]);  | 
89  |  |       wp[usize] = cy_limb;  | 
90  |  |       usize += (cy_limb != 0);  | 
91  |  |       SIZ (w) = (sign_product >= 0 ? usize : -usize);  | 
92  |  |       return;  | 
93  |  |     }  | 
94  |  | #endif  | 
95  |  |  | 
96  | 1.51k  |   TMP_MARK;  | 
97  | 1.51k  |   free_me = NULL;  | 
98  | 1.51k  |   up = PTR (u);  | 
99  | 1.51k  |   vp = PTR (v);  | 
100  | 1.51k  |   wp = PTR (w);  | 
101  |  |  | 
102  |  |   /* Ensure W has space enough to store the result.  */  | 
103  | 1.51k  |   wsize = usize + vsize;  | 
104  | 1.51k  |   if (ALLOC (w) < wsize)  | 
105  | 392  |     { | 
106  | 392  |       if (ALLOC (w) != 0)  | 
107  | 21  |   { | 
108  | 21  |     if (wp == up || wp == vp)  | 
109  | 4  |       { | 
110  | 4  |         free_me = wp;  | 
111  | 4  |         free_me_size = ALLOC (w);  | 
112  | 4  |       }  | 
113  | 17  |     else  | 
114  | 17  |       (*__gmp_free_func) (wp, (size_t) ALLOC (w) * GMP_LIMB_BYTES);  | 
115  | 21  |   }  | 
116  |  |  | 
117  | 392  |       ALLOC (w) = wsize;  | 
118  | 392  |       wp = __GMP_ALLOCATE_FUNC_LIMBS (wsize);  | 
119  | 392  |       PTR (w) = wp;  | 
120  | 392  |     }  | 
121  | 1.12k  |   else  | 
122  | 1.12k  |     { | 
123  |  |       /* Make U and V not overlap with W.  */  | 
124  | 1.12k  |       if (wp == up)  | 
125  | 50  |   { | 
126  |  |     /* W and U are identical.  Allocate temporary space for U.  */  | 
127  | 50  |     up = TMP_ALLOC_LIMBS (usize);  | 
128  |  |     /* Is V identical too?  Keep it identical with U.  */  | 
129  | 50  |     if (wp == vp)  | 
130  | 50  |       vp = up;  | 
131  |  |     /* Copy to the temporary space.  */  | 
132  | 50  |     MPN_COPY (up, wp, usize);  | 
133  | 50  |   }  | 
134  | 1.07k  |       else if (wp == vp)  | 
135  | 0  |   { | 
136  |  |     /* W and V are identical.  Allocate temporary space for V.  */  | 
137  | 0  |     vp = TMP_ALLOC_LIMBS (vsize);  | 
138  |  |     /* Copy to the temporary space.  */  | 
139  | 0  |     MPN_COPY (vp, wp, vsize);  | 
140  | 0  |   }  | 
141  | 1.12k  |     }  | 
142  |  |  | 
143  | 1.51k  |   if (up == vp)  | 
144  | 346  |     { | 
145  | 346  |       mpn_sqr (wp, up, usize);  | 
146  | 346  |       cy_limb = wp[wsize - 1];  | 
147  | 346  |     }  | 
148  | 1.16k  |   else  | 
149  | 1.16k  |     { | 
150  | 1.16k  |       cy_limb = mpn_mul (wp, up, usize, vp, vsize);  | 
151  | 1.16k  |     }  | 
152  |  |  | 
153  | 1.51k  |   wsize -= cy_limb == 0;  | 
154  |  |  | 
155  | 1.51k  |   SIZ (w) = sign_product < 0 ? -wsize : wsize;  | 
156  | 1.51k  |   if (free_me != NULL)  | 
157  | 4  |     (*__gmp_free_func) (free_me, free_me_size * GMP_LIMB_BYTES);  | 
158  | 1.51k  |   TMP_FREE;  | 
159  | 1.51k  | }  |