1 /* mpz_mul -- Multiply two integers.
3 Copyright (C) 1991 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 General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
12 The GNU MP Library is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with the GNU MP Library; see the file COPYING. If not, write to
19 the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
27 mpz_mul (MP_INT *w, const MP_INT *u, const MP_INT *v)
34 #else /* BERKELEY_MP */
37 mult (const MP_INT *u, const MP_INT *v, MP_INT *w)
44 #endif /* BERKELEY_MP */
46 mp_size usize = u->size;
47 mp_size vsize = v->size;
52 mp_ptr free_me = NULL;
55 sign_product = usize ^ vsize;
62 {const MP_INT *t = u; u = v; v = t;}
63 {mp_size t = usize; usize = vsize; vsize = t;}
70 /* Ensure W has space enough to store the result. */
71 wsize = usize + vsize;
74 if (wp == up || wp == vp)
77 free_me_size = w->alloc;
80 (*_mp_free_func) (wp, w->alloc * BYTES_PER_MP_LIMB);
83 wp = (mp_ptr) (*_mp_allocate_func) (wsize * BYTES_PER_MP_LIMB);
88 /* Make U and V not overlap with W. */
91 /* W and U are identical. Allocate temporary space for U. */
92 up = (mp_ptr) alloca (usize * BYTES_PER_MP_LIMB);
93 /* Is V identical too? Keep it identical with U. */
96 /* Copy to the temporary space. */
97 MPN_COPY (up, wp, usize);
101 /* W and V are identical. Allocate temporary space for V. */
102 vp = (mp_ptr) alloca (vsize * BYTES_PER_MP_LIMB);
103 /* Copy to the temporary space. */
104 MPN_COPY (vp, wp, vsize);
108 wsize = mpn_mul (wp, up, usize, vp, vsize);
109 w->size = sign_product < 0 ? -wsize : wsize;
111 (*_mp_free_func) (free_me, free_me_size * BYTES_PER_MP_LIMB);