1 /* mpf_mul -- Multiply two floats.
2
3 Copyright 1993, 1994, 1996, 2001, 2005, 2019, 2020 Free Software
4 Foundation, Inc.
5
6 This file is part of the GNU MP Library.
7
8 The GNU MP Library is free software; you can redistribute it and/or modify
9 it under the terms of either:
10
11 * the GNU Lesser General Public License as published by the Free
12 Software Foundation; either version 3 of the License, or (at your
13 option) any later version.
14
15 or
16
17 * the GNU General Public License as published by the Free Software
18 Foundation; either version 2 of the License, or (at your option) any
19 later version.
20
21 or both in parallel, as here.
22
23 The GNU MP Library is distributed in the hope that it will be useful, but
24 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
25 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
26 for more details.
27
28 You should have received copies of the GNU General Public License and the
29 GNU Lesser General Public License along with the GNU MP Library. If not,
30 see https://www.gnu.org/licenses/. */
31
32 #include "gmp-impl.h"
33
34 void
35 mpf_mul (mpf_ptr r, mpf_srcptr u, mpf_srcptr v)
36 {
37 mp_size_t sign_product;
38 mp_size_t prec = PREC (r);
39 mp_size_t rsize;
40 mp_limb_t cy_limb;
41 mp_ptr rp, tp;
42 mp_size_t adj;
43 TMP_DECL;
44
45 if (u == v)
46 {
47 mp_srcptr up;
48 mp_size_t usize;
49
50 sign_product = 0;
51
52 usize = ABSIZ (u);
53
54 up = PTR (u);
55 if (usize > prec)
56 {
57 up += usize - prec;
58 usize = prec;
59 }
60
61 if (usize == 0)
62 {
63 SIZ (r) = 0;
64 EXP (r) = 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 = SIZ (u);
83 vsize = SIZ (v);
84 sign_product = usize ^ vsize;
85
86 usize = ABS (usize);
87 vsize = ABS (vsize);
88
89 up = PTR (u);
90 vp = PTR (v);
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 SIZ (r) = 0;
105 EXP (r) = 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 = PTR (r);
129 MPN_COPY (rp, tp, rsize);
130 EXP (r) = EXP (u) + EXP (v) - adj;
131 SIZ (r) = sign_product >= 0 ? rsize : -rsize;
132
133 TMP_FREE;
134 }