HAMMER VFS - Minor bug (caught by assertion panic)
[dragonfly.git] / contrib / mpfr / sub.c
blobb1454ec17f243705e21e05f307c3f080875bb2ff
1 /* mpfr_sub -- subtract two floating-point numbers
3 Copyright 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_sub (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 else if (MPFR_IS_INF (b))
40 if (!MPFR_IS_INF (c) || MPFR_SIGN (b) != MPFR_SIGN(c))
42 MPFR_SET_INF (a);
43 MPFR_SET_SAME_SIGN (a, b);
44 MPFR_RET (0); /* exact */
46 else
48 MPFR_SET_NAN (a); /* Inf - Inf */
49 MPFR_RET_NAN;
52 else if (MPFR_IS_INF (c))
54 MPFR_SET_INF (a);
55 MPFR_SET_OPPOSITE_SIGN (a, c);
56 MPFR_RET (0); /* exact */
58 else if (MPFR_IS_ZERO (b))
60 if (MPFR_IS_ZERO (c))
62 int sign = rnd_mode != GMP_RNDD
63 ? ((MPFR_IS_NEG(b) && MPFR_IS_POS(c)) ? -1 : 1)
64 : ((MPFR_IS_POS(b) && MPFR_IS_NEG(c)) ? 1 : -1);
65 MPFR_SET_SIGN (a, sign);
66 MPFR_SET_ZERO (a);
67 MPFR_RET(0); /* 0 - 0 is exact */
69 else
70 return mpfr_neg (a, c, rnd_mode);
72 else
74 MPFR_ASSERTD (MPFR_IS_ZERO (c));
75 return mpfr_set (a, b, rnd_mode);
78 MPFR_CLEAR_FLAGS (a);
79 MPFR_ASSERTD (MPFR_IS_PURE_FP (b) && MPFR_IS_PURE_FP (c));
81 if (MPFR_LIKELY (MPFR_SIGN (b) == MPFR_SIGN (c)))
82 { /* signs are equal, it's a real subtraction */
83 if (MPFR_LIKELY (MPFR_PREC (a) == MPFR_PREC (b)
84 && MPFR_PREC (b) == MPFR_PREC (c)))
85 return mpfr_sub1sp (a, b, c, rnd_mode);
86 else
87 return mpfr_sub1 (a, b, c, rnd_mode);
89 else
90 { /* signs differ, it's an addition */
91 if (MPFR_GET_EXP (b) < MPFR_GET_EXP (c))
92 { /* exchange rounding modes towards +/- infinity */
93 int inexact;
94 rnd_mode = MPFR_INVERT_RND (rnd_mode);
95 if (MPFR_LIKELY (MPFR_PREC (a) == MPFR_PREC (b)
96 && MPFR_PREC (b) == MPFR_PREC (c)))
97 inexact = mpfr_add1sp (a, c, b, rnd_mode);
98 else
99 inexact = mpfr_add1 (a, c, b, rnd_mode);
100 MPFR_CHANGE_SIGN (a);
101 return -inexact;
103 else
105 if (MPFR_LIKELY (MPFR_PREC (a) == MPFR_PREC (b)
106 && MPFR_PREC (b) == MPFR_PREC (c)))
107 return mpfr_add1sp (a, b, c, rnd_mode);
108 else
109 return mpfr_add1 (a, b, c, rnd_mode);