xref: /netbsd-src/external/lgpl3/gmp/dist/mpf/mul.c (revision 72c7faa4dbb41dbb0238d6b4a109da0d4b236dd4)
1 /* mpf_mul -- Multiply two floats.
2 
3 Copyright 1993, 1994, 1996, 2001, 2005, 2019 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 either:
9 
10   * the GNU Lesser General Public License as published by the Free
11     Software Foundation; either version 3 of the License, or (at your
12     option) any later version.
13 
14 or
15 
16   * the GNU General Public License as published by the Free Software
17     Foundation; either version 2 of the License, or (at your option) any
18     later version.
19 
20 or both in parallel, as here.
21 
22 The GNU MP Library is distributed in the hope that it will be useful, but
23 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
24 or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
25 for more details.
26 
27 You should have received copies of the GNU General Public License and the
28 GNU Lesser General Public License along with the GNU MP Library.  If not,
29 see https://www.gnu.org/licenses/.  */
30 
31 #include "gmp-impl.h"
32 
33 void
mpf_mul(mpf_ptr r,mpf_srcptr u,mpf_srcptr v)34 mpf_mul (mpf_ptr r, mpf_srcptr u, mpf_srcptr v)
35 {
36   mp_size_t sign_product;
37   mp_size_t prec = r->_mp_prec;
38   mp_size_t rsize;
39   mp_limb_t cy_limb;
40   mp_ptr rp, tp;
41   mp_size_t adj;
42   TMP_DECL;
43 
44   if (u == v)
45     {
46       mp_srcptr up;
47       mp_size_t usize;
48 
49       usize = u->_mp_size;
50       sign_product = 0;
51 
52       usize = ABS (usize);
53 
54       up = u->_mp_d;
55       if (usize > prec)
56 	{
57 	  up += usize - prec;
58 	  usize = prec;
59 	}
60 
61       if (usize == 0)
62 	{
63 	  r->_mp_size = 0;
64 	  r->_mp_exp = 0;		/* ??? */
65 	  return;
66 	}
67       else
68 	{
69 	  TMP_MARK;
70 	  rsize = 2 * usize;
71 	  tp = TMP_ALLOC_LIMBS (rsize);
72 
73 	  mpn_sqr (tp, up, usize);
74 	  cy_limb = tp[rsize - 1];
75 	}
76     }
77   else
78     {
79       mp_srcptr up, vp;
80       mp_size_t usize, vsize;
81 
82       usize = u->_mp_size;
83       vsize = v->_mp_size;
84       sign_product = usize ^ vsize;
85 
86       usize = ABS (usize);
87       vsize = ABS (vsize);
88 
89       up = u->_mp_d;
90       vp = v->_mp_d;
91       if (usize > prec)
92 	{
93 	  up += usize - prec;
94 	  usize = prec;
95 	}
96       if (vsize > prec)
97 	{
98 	  vp += vsize - prec;
99 	  vsize = prec;
100 	}
101 
102       if (usize == 0 || vsize == 0)
103 	{
104 	  r->_mp_size = 0;
105 	  r->_mp_exp = 0;
106 	  return;
107 	}
108       else
109 	{
110 	  TMP_MARK;
111 	  rsize = usize + vsize;
112 	  tp = TMP_ALLOC_LIMBS (rsize);
113 	  cy_limb = (usize >= vsize
114 		     ? mpn_mul (tp, up, usize, vp, vsize)
115 		     : mpn_mul (tp, vp, vsize, up, usize));
116 
117 	}
118     }
119 
120   adj = cy_limb == 0;
121   rsize -= adj;
122   prec++;
123   if (rsize > prec)
124     {
125       tp += rsize - prec;
126       rsize = prec;
127     }
128   rp = r->_mp_d;
129   MPN_COPY (rp, tp, rsize);
130   r->_mp_exp = u->_mp_exp + v->_mp_exp - adj;
131   r->_mp_size = sign_product >= 0 ? rsize : -rsize;
132 
133   TMP_FREE;
134 }
135