1 /* mpz_mul -- Multiply two integers.
3 Copyright (C) 1991, 1993, 1994, 1996 Free Software Foundation, Inc.
5 This file is part of the GNU MP Library.
7 The GNU MP Library is free software; you can redistribute it and/or modify
8 it under the terms of the GNU Library General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or (at your
10 option) any later version.
12 The GNU MP Library is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
14 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
15 License for more details.
17 You should have received a copy of the GNU Library General Public License
18 along with the GNU MP Library; see the file COPYING.LIB. If not, write to
19 the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
20 MA 02111-1307, USA. */
28 mpz_mul (mpz_ptr w, mpz_srcptr u, mpz_srcptr v)
35 #else /* BERKELEY_MP */
38 mult (mpz_srcptr u, mpz_srcptr v, mpz_ptr w)
45 #endif /* BERKELEY_MP */
47 mp_size_t usize = u->_mp_size;
48 mp_size_t vsize = v->_mp_size;
50 mp_size_t sign_product;
53 mp_ptr free_me = NULL;
59 sign_product = usize ^ vsize;
66 {const __mpz_struct *t = u; u = v; v = t;}
67 {mp_size_t t = usize; usize = vsize; vsize = t;}
74 /* Ensure W has space enough to store the result. */
75 wsize = usize + vsize;
76 if (w->_mp_alloc < wsize)
78 if (wp == up || wp == vp)
81 free_me_size = w->_mp_alloc;
84 (*_mp_free_func) (wp, w->_mp_alloc * BYTES_PER_MP_LIMB);
87 wp = (mp_ptr) (*_mp_allocate_func) (wsize * BYTES_PER_MP_LIMB);
92 /* Make U and V not overlap with W. */
95 /* W and U are identical. Allocate temporary space for U. */
96 up = (mp_ptr) TMP_ALLOC (usize * BYTES_PER_MP_LIMB);
97 /* Is V identical too? Keep it identical with U. */
100 /* Copy to the temporary space. */
101 MPN_COPY (up, wp, usize);
105 /* W and V are identical. Allocate temporary space for V. */
106 vp = (mp_ptr) TMP_ALLOC (vsize * BYTES_PER_MP_LIMB);
107 /* Copy to the temporary space. */
108 MPN_COPY (vp, wp, vsize);
118 cy_limb = mpn_mul (wp, up, usize, vp, vsize);
119 wsize = usize + vsize;
120 wsize -= cy_limb == 0;
123 w->_mp_size = sign_product < 0 ? -wsize : wsize;
125 (*_mp_free_func) (free_me, free_me_size * BYTES_PER_MP_LIMB);