iscontrol(8): Fix synopsis, sync usage() & improve markup
[dragonfly.git] / contrib / mpfr / add.c
blobca50a2d17c242dd08f855f6d4b1f7d7a2a732cbf
1 /* mpfr_add -- add two floating-point numbers
3 Copyright 1999, 2000, 2001, 2002, 2003, 2004, 2006, 2007, 2008, 2009 Free Software Foundation, Inc.
4 Contributed by the Arenaire and Cacao projects, INRIA.
6 This file is part of the GNU MPFR Library.
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 2.1 of the License, or (at your
11 option) any later version.
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.
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.LIB. If not, write to
20 the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
21 MA 02110-1301, USA. */
23 #include "mpfr-impl.h"
25 int
26 mpfr_add (mpfr_ptr a, mpfr_srcptr b, mpfr_srcptr c, mp_rnd_t rnd_mode)
28 MPFR_LOG_FUNC (("b[%#R]=%R c[%#R]=%R rnd=%d", b, b, c, c, rnd_mode),
29 ("a[%#R]=%R", a, a));
31 if (MPFR_ARE_SINGULAR(b,c))
33 if (MPFR_IS_NAN(b) || MPFR_IS_NAN(c))
35 MPFR_SET_NAN(a);
36 MPFR_RET_NAN;
38 /* neither b nor c is NaN here */
39 else if (MPFR_IS_INF(b))
41 if (!MPFR_IS_INF(c) || MPFR_SIGN(b) == MPFR_SIGN(c))
43 MPFR_SET_INF(a);
44 MPFR_SET_SAME_SIGN(a, b);
45 MPFR_RET(0); /* exact */
47 else
49 MPFR_SET_NAN(a);
50 MPFR_RET_NAN;
53 else if (MPFR_IS_INF(c))
55 MPFR_SET_INF(a);
56 MPFR_SET_SAME_SIGN(a, c);
57 MPFR_RET(0); /* exact */
59 /* now either b or c is zero */
60 else if (MPFR_IS_ZERO(b))
62 if (MPFR_IS_ZERO(c))
64 MPFR_SET_SIGN(a,
65 (rnd_mode != GMP_RNDD ?
66 ((MPFR_IS_NEG(b) && MPFR_IS_NEG(c)) ? -1 : 1) :
67 ((MPFR_IS_POS(b) && MPFR_IS_POS(c)) ? 1 : -1)));
68 MPFR_SET_ZERO(a);
69 MPFR_RET(0); /* 0 + 0 is exact */
71 return mpfr_set (a, c, rnd_mode);
73 else
75 MPFR_ASSERTD(MPFR_IS_ZERO(c));
76 return mpfr_set (a, b, rnd_mode);
80 MPFR_ASSERTD(MPFR_IS_PURE_FP(b) && MPFR_IS_PURE_FP(c));
81 MPFR_CLEAR_FLAGS(a); /* clear flags */
83 if (MPFR_UNLIKELY(MPFR_SIGN(b) != MPFR_SIGN(c)))
84 { /* signs differ, it's a subtraction */
85 if (MPFR_LIKELY(MPFR_PREC(a) == MPFR_PREC(b)
86 && MPFR_PREC(b) == MPFR_PREC(c)))
87 return mpfr_sub1sp(a,b,c,rnd_mode);
88 else
89 return mpfr_sub1(a, b, c, rnd_mode);
91 else
92 { /* signs are equal, it's an addition */
93 if (MPFR_LIKELY(MPFR_PREC(a) == MPFR_PREC(b)
94 && MPFR_PREC(b) == MPFR_PREC(c)))
95 if (MPFR_GET_EXP(b) < MPFR_GET_EXP(c))
96 return mpfr_add1sp(a, c, b, rnd_mode);
97 else
98 return mpfr_add1sp(a, b, c, rnd_mode);
99 else
100 if (MPFR_GET_EXP(b) < MPFR_GET_EXP(c))
101 return mpfr_add1(a, c, b, rnd_mode);
102 else
103 return mpfr_add1(a, b, c, rnd_mode);