1 /* mpz_mul -- Multiply two integers.
3 Copyright (C) 1991, 1993, 1994, 1996, 2000 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 Lesser General Public License as published by
9 the Free Software Foundation; either version 2.1 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 Lesser General Public
15 License for more details.
17 You should have received a copy of the GNU Lesser 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. */
22 #include <stdio.h> /* for NULL */
32 mpz_mul (mpz_ptr w, mpz_srcptr u, mpz_srcptr v)
39 #else /* BERKELEY_MP */
42 mult (mpz_srcptr u, mpz_srcptr v, mpz_ptr w)
49 #endif /* BERKELEY_MP */
51 mp_size_t usize = u->_mp_size;
52 mp_size_t vsize = v->_mp_size;
54 mp_size_t sign_product;
57 mp_ptr free_me = NULL;
63 sign_product = usize ^ vsize;
70 {const __mpz_struct *t = u; u = v; v = t;}
71 {mp_size_t t = usize; usize = vsize; vsize = t;}
78 /* Ensure W has space enough to store the result. */
79 wsize = usize + vsize;
80 if (w->_mp_alloc < wsize)
82 if (wp == up || wp == vp)
85 free_me_size = w->_mp_alloc;
88 (*_mp_free_func) (wp, w->_mp_alloc * BYTES_PER_MP_LIMB);
91 wp = (mp_ptr) (*_mp_allocate_func) (wsize * BYTES_PER_MP_LIMB);
96 /* Make U and V not overlap with W. */
99 /* W and U are identical. Allocate temporary space for U. */
100 up = (mp_ptr) TMP_ALLOC (usize * BYTES_PER_MP_LIMB);
101 /* Is V identical too? Keep it identical with U. */
104 /* Copy to the temporary space. */
105 MPN_COPY (up, wp, usize);
109 /* W and V are identical. Allocate temporary space for V. */
110 vp = (mp_ptr) TMP_ALLOC (vsize * BYTES_PER_MP_LIMB);
111 /* Copy to the temporary space. */
112 MPN_COPY (vp, wp, vsize);
122 cy_limb = mpn_mul (wp, up, usize, vp, vsize);
123 wsize = usize + vsize;
124 wsize -= cy_limb == 0;
127 w->_mp_size = sign_product < 0 ? -wsize : wsize;
129 (*_mp_free_func) (free_me, free_me_size * BYTES_PER_MP_LIMB);