[project @ 1998-11-26 09:17:22 by sof]
[ghc-hetmet.git] / ghc / runtime / gmp / mpz_mul.c
1 /* mpz_mul -- Multiply two integers.
2
3 Copyright (C) 1991 Free Software Foundation, Inc.
4
5 This file is part of the GNU MP Library.
6
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)
10 any later version.
11
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.
16
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.  */
20
21 #include "gmp.h"
22 #include "gmp-impl.h"
23
24 #ifndef BERKELEY_MP
25 void
26 #ifdef __STDC__
27 mpz_mul (MP_INT *w, const MP_INT *u, const MP_INT *v)
28 #else
29 mpz_mul (w, u, v)
30      MP_INT *w;
31      const MP_INT *u;
32      const MP_INT *v;
33 #endif
34 #else /* BERKELEY_MP */
35 void
36 #ifdef __STDC__
37 mult (const MP_INT *u, const MP_INT *v, MP_INT *w)
38 #else
39 mult (u, v, w)
40      const MP_INT *u;
41      const MP_INT *v;
42      MP_INT *w;
43 #endif
44 #endif /* BERKELEY_MP */
45 {
46   mp_size usize = u->size;
47   mp_size vsize = v->size;
48   mp_size wsize;
49   mp_size sign_product;
50   mp_ptr up, vp;
51   mp_ptr wp;
52   mp_ptr free_me = NULL;
53   size_t free_me_size;
54
55   sign_product = usize ^ vsize;
56   usize = ABS (usize);
57   vsize = ABS (vsize);
58
59   if (usize < vsize)
60     {
61       /* Swap U and V.  */
62       {const MP_INT *t = u; u = v; v = t;}
63       {mp_size t = usize; usize = vsize; vsize = t;}
64     }
65
66   up = u->d;
67   vp = v->d;
68   wp = w->d;
69
70   /* Ensure W has space enough to store the result.  */
71   wsize = usize + vsize;
72   if (w->alloc < wsize)
73     {
74       if (wp == up || wp == vp)
75         {
76           free_me = wp;
77           free_me_size = w->alloc;
78         }
79       else
80         (*_mp_free_func) (wp, w->alloc * BYTES_PER_MP_LIMB);
81
82       w->alloc = wsize;
83       wp = (mp_ptr) (*_mp_allocate_func) (wsize * BYTES_PER_MP_LIMB);
84       w->d = wp;
85     }
86   else
87     {
88       /* Make U and V not overlap with W.  */
89       if (wp == up)
90         {
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.  */
94           if (wp == vp)
95             vp = up;
96           /* Copy to the temporary space.  */
97           MPN_COPY (up, wp, usize);
98         }
99       else if (wp == vp)
100         {
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);
105         }
106     }
107
108   wsize = mpn_mul (wp, up, usize, vp, vsize);
109   w->size = sign_product < 0 ? -wsize : wsize;
110   if (free_me != NULL)
111     (*_mp_free_func) (free_me, free_me_size * BYTES_PER_MP_LIMB);
112
113   alloca (0);
114 }