1 /* mpfr_fmma, mpfr_fmms -- Compute a*b +/- c*d
2
3 Copyright 2014-2023 Free Software Foundation, Inc.
4 Contributed by the AriC and Caramba projects, INRIA.
5
6 This file is part of the GNU MPFR Library.
7
8 The GNU MPFR Library is free software; you can redistribute it and/or modify
9 it under the terms of the GNU Lesser General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or (at your
11 option) any later version.
12
13 The GNU MPFR Library is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
15 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
16 License for more details.
17
18 You should have received a copy of the GNU Lesser General Public License
19 along with the GNU MPFR Library; see the file COPYING.LESSER. If not, see
20 https://www.gnu.org/licenses/ or write to the Free Software Foundation, Inc.,
21 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. */
22
23 #include "mpfr-impl.h"
24
25 /* compute a*b+c*d if neg=0 (fmma), a*b-c*d otherwise (fmms) */
26 static int
mpfr_fmma_aux(mpfr_ptr z,mpfr_srcptr a,mpfr_srcptr b,mpfr_srcptr c,mpfr_srcptr d,mpfr_rnd_t rnd,int neg)27 mpfr_fmma_aux (mpfr_ptr z, mpfr_srcptr a, mpfr_srcptr b, mpfr_srcptr c,
28 mpfr_srcptr d, mpfr_rnd_t rnd, int neg)
29 {
30 mpfr_ubf_t u, v;
31 mpfr_t zz;
32 mpfr_prec_t prec_z = MPFR_PREC(z);
33 mp_size_t un, vn;
34 mpfr_limb_ptr up, vp, zp;
35 int inex;
36 MPFR_TMP_DECL(marker);
37
38 MPFR_LOG_FUNC
39 (("a[%Pd]=%.*Rg b[%Pd]=%.*Rg c[%Pd]=%.*Rg d[%Pd]=%.*Rg rnd=%d neg=%d",
40 mpfr_get_prec (a), mpfr_log_prec, a,
41 mpfr_get_prec (b), mpfr_log_prec, b,
42 mpfr_get_prec (c), mpfr_log_prec, c,
43 mpfr_get_prec (d), mpfr_log_prec, d, rnd, neg),
44 ("z[%Pd]=%.*Rg inex=%d",
45 mpfr_get_prec (z), mpfr_log_prec, z, inex));
46
47 MPFR_TMP_MARK (marker);
48
49 un = MPFR_LIMB_SIZE (a) + MPFR_LIMB_SIZE (b);
50 vn = MPFR_LIMB_SIZE (c) + MPFR_LIMB_SIZE (d);
51 MPFR_TMP_INIT (up, u, (mpfr_prec_t) un * GMP_NUMB_BITS, un);
52 MPFR_TMP_INIT (vp, v, (mpfr_prec_t) vn * GMP_NUMB_BITS, vn);
53
54 mpfr_ubf_mul_exact (u, a, b);
55 mpfr_ubf_mul_exact (v, c, d);
56 if (prec_z == MPFR_PREC(a) && prec_z == MPFR_PREC(b) &&
57 prec_z == MPFR_PREC(c) && prec_z == MPFR_PREC(d) &&
58 un == MPFR_PREC2LIMBS(2 * prec_z))
59 {
60 MPFR_TMP_INIT (zp, zz, 2 * prec_z, un);
61 MPFR_PREC(u) = MPFR_PREC(v) = 2 * prec_z;
62 inex = (neg == 0) ? mpfr_add (zz, (mpfr_srcptr) u, (mpfr_srcptr) v, rnd)
63 : mpfr_sub (zz, (mpfr_srcptr) u, (mpfr_srcptr) v, rnd);
64 inex = mpfr_set_1_2 (z, zz, rnd, inex);
65 }
66 else
67 inex = (neg == 0) ? mpfr_add (z, (mpfr_srcptr) u, (mpfr_srcptr) v, rnd)
68 : mpfr_sub (z, (mpfr_srcptr) u, (mpfr_srcptr) v, rnd);
69
70 MPFR_UBF_CLEAR_EXP (u);
71 MPFR_UBF_CLEAR_EXP (v);
72
73 MPFR_TMP_FREE (marker);
74
75 return inex;
76 }
77
78 /* z <- a*b + c*d */
79 int
mpfr_fmma(mpfr_ptr z,mpfr_srcptr a,mpfr_srcptr b,mpfr_srcptr c,mpfr_srcptr d,mpfr_rnd_t rnd)80 mpfr_fmma (mpfr_ptr z, mpfr_srcptr a, mpfr_srcptr b, mpfr_srcptr c,
81 mpfr_srcptr d, mpfr_rnd_t rnd)
82 {
83 return mpfr_fmma_aux (z, a, b, c, d, rnd, 0);
84 }
85
86 /* z <- a*b - c*d */
87 int
mpfr_fmms(mpfr_ptr z,mpfr_srcptr a,mpfr_srcptr b,mpfr_srcptr c,mpfr_srcptr d,mpfr_rnd_t rnd)88 mpfr_fmms (mpfr_ptr z, mpfr_srcptr a, mpfr_srcptr b, mpfr_srcptr c,
89 mpfr_srcptr d, mpfr_rnd_t rnd)
90 {
91 return mpfr_fmma_aux (z, a, b, c, d, rnd, 1);
92 }
93