1 /* mpfr_add -- add two floating-point numbers
2
3 Copyright 1999-2004, 2006-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 MPFR_HOT_FUNCTION_ATTR int
mpfr_add(mpfr_ptr a,mpfr_srcptr b,mpfr_srcptr c,mpfr_rnd_t rnd_mode)26 mpfr_add (mpfr_ptr a, mpfr_srcptr b, mpfr_srcptr c, mpfr_rnd_t rnd_mode)
27 {
28 MPFR_LOG_FUNC
29 (("b[%Pd]=%.*Rg c[%Pd]=%.*Rg rnd=%d",
30 mpfr_get_prec (b), mpfr_log_prec, b,
31 mpfr_get_prec (c), mpfr_log_prec, c, rnd_mode),
32 ("a[%Pd]=%.*Rg", mpfr_get_prec (a), mpfr_log_prec, a));
33
34 if (MPFR_ARE_SINGULAR_OR_UBF (b, c))
35 {
36 if (MPFR_IS_NAN(b) || MPFR_IS_NAN(c))
37 {
38 MPFR_SET_NAN(a);
39 MPFR_RET_NAN;
40 }
41 /* neither b nor c is NaN here */
42 else if (MPFR_IS_INF(b))
43 {
44 if (!MPFR_IS_INF(c) || MPFR_SIGN(b) == MPFR_SIGN(c))
45 {
46 MPFR_SET_INF(a);
47 MPFR_SET_SAME_SIGN(a, b);
48 MPFR_RET(0); /* exact */
49 }
50 else
51 {
52 MPFR_SET_NAN(a);
53 MPFR_RET_NAN;
54 }
55 }
56 else if (MPFR_IS_INF(c))
57 {
58 MPFR_SET_INF(a);
59 MPFR_SET_SAME_SIGN(a, c);
60 MPFR_RET(0); /* exact */
61 }
62 /* now both b and c are finite numbers */
63 else if (MPFR_IS_ZERO(b))
64 {
65 if (MPFR_IS_ZERO(c))
66 {
67 /* for round away, we take the same convention for 0 + 0
68 as for round to zero or to nearest: it always gives +0,
69 except (-0) + (-0) = -0. */
70 MPFR_SET_SIGN(a,
71 (rnd_mode != MPFR_RNDD ?
72 (MPFR_IS_NEG(b) && MPFR_IS_NEG(c) ?
73 MPFR_SIGN_NEG : MPFR_SIGN_POS) :
74 (MPFR_IS_POS(b) && MPFR_IS_POS(c) ?
75 MPFR_SIGN_POS : MPFR_SIGN_NEG)));
76 MPFR_SET_ZERO(a);
77 MPFR_RET(0); /* 0 + 0 is exact */
78 }
79 return mpfr_set (a, c, rnd_mode);
80 }
81 else if (MPFR_IS_ZERO(c))
82 {
83 return mpfr_set (a, b, rnd_mode);
84 }
85 else
86 {
87 MPFR_ASSERTD (MPFR_IS_PURE_UBF (b));
88 MPFR_ASSERTD (MPFR_IS_PURE_UBF (c));
89 /* mpfr_sub1sp and mpfr_add1sp are not intended to support UBF,
90 for which optimization is less important. */
91 if (MPFR_SIGN(b) != MPFR_SIGN(c))
92 return mpfr_sub1 (a, b, c, rnd_mode);
93 else if (MPFR_UBF_EXP_LESS_P (b, c))
94 return mpfr_add1 (a, c, b, rnd_mode);
95 else
96 return mpfr_add1 (a, b, c, rnd_mode);
97 }
98 }
99
100 MPFR_ASSERTD (MPFR_IS_PURE_FP (b));
101 MPFR_ASSERTD (MPFR_IS_PURE_FP (c));
102
103 if (MPFR_UNLIKELY(MPFR_SIGN(b) != MPFR_SIGN(c)))
104 { /* signs differ, it is a subtraction */
105 if (MPFR_LIKELY(MPFR_PREC(a) == MPFR_PREC(b)
106 && MPFR_PREC(b) == MPFR_PREC(c)))
107 return mpfr_sub1sp(a, b, c, rnd_mode);
108 else
109 return mpfr_sub1(a, b, c, rnd_mode);
110 }
111 else
112 { /* signs are equal, it's an addition */
113 if (MPFR_LIKELY(MPFR_PREC(a) == MPFR_PREC(b)
114 && MPFR_PREC(b) == MPFR_PREC(c)))
115 return mpfr_add1sp(a, b, c, rnd_mode);
116 else
117 if (MPFR_GET_EXP(b) < MPFR_GET_EXP(c))
118 return mpfr_add1(a, c, b, rnd_mode);
119 else
120 return mpfr_add1(a, b, c, rnd_mode);
121 }
122 }
123