mpfr: Remove a local modification we no longer need.
[dragonfly.git] / contrib / mpfr / sin.c
1 /* mpfr_sin -- sine of a floating-point number
2
3 Copyright 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009 Free Software Foundation, Inc.
4 Contributed by the Arenaire and Cacao 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 2.1 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.LIB.  If not, write to
20 the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
21 MA 02110-1301, USA. */
22
23 #define MPFR_NEED_LONGLONG_H
24 #include "mpfr-impl.h"
25
26 int
27 mpfr_sin (mpfr_ptr y, mpfr_srcptr x, mp_rnd_t rnd_mode)
28 {
29   mpfr_t c, xr;
30   mpfr_srcptr xx;
31   mp_exp_t expx, err;
32   mp_prec_t precy, m;
33   int inexact, sign, reduce;
34   MPFR_ZIV_DECL (loop);
35   MPFR_SAVE_EXPO_DECL (expo);
36
37   MPFR_LOG_FUNC (("x[%#R]=%R rnd=%d", x, x, rnd_mode),
38                   ("y[%#R]=%R inexact=%d", y, y, inexact));
39
40   if (MPFR_UNLIKELY (MPFR_IS_SINGULAR (x)))
41     {
42       if (MPFR_IS_NAN (x) || MPFR_IS_INF (x))
43         {
44           MPFR_SET_NAN (y);
45           MPFR_RET_NAN;
46
47         }
48       else /* x is zero */
49         {
50           MPFR_ASSERTD (MPFR_IS_ZERO (x));
51           MPFR_SET_ZERO (y);
52           MPFR_SET_SAME_SIGN (y, x);
53           MPFR_RET (0);
54         }
55     }
56
57   /* sin(x) = x - x^3/6 + ... so the error is < 2^(3*EXP(x)-2) */
58   MPFR_FAST_COMPUTE_IF_SMALL_INPUT (y, x, -2 * MPFR_GET_EXP (x), 2, 0,
59                                     rnd_mode, {});
60
61   MPFR_SAVE_EXPO_MARK (expo);
62
63   /* Compute initial precision */
64   precy = MPFR_PREC (y);
65   m = precy + MPFR_INT_CEIL_LOG2 (precy) + 13;
66   expx = MPFR_GET_EXP (x);
67
68   mpfr_init (c);
69   mpfr_init (xr);
70
71   MPFR_ZIV_INIT (loop, m);
72   for (;;)
73     {
74       /* first perform argument reduction modulo 2*Pi (if needed),
75          also helps to determine the sign of sin(x) */
76       if (expx >= 2) /* If Pi < x < 4, we need to reduce too, to determine
77                         the sign of sin(x). For 2 <= |x| < Pi, we could avoid
78                         the reduction. */
79         {
80           reduce = 1;
81           /* As expx + m - 1 will silently be converted into mpfr_prec_t
82              in the mpfr_set_prec call, the assert below may be useful to
83              avoid undefined behavior. */
84           MPFR_ASSERTN (expx + m - 1 <= MPFR_PREC_MAX);
85           mpfr_set_prec (c, expx + m - 1);
86           mpfr_set_prec (xr, m);
87           mpfr_const_pi (c, GMP_RNDN);
88           mpfr_mul_2ui (c, c, 1, GMP_RNDN);
89           mpfr_remainder (xr, x, c, GMP_RNDN);
90           /* The analysis is similar to that of cos.c:
91              |xr - x - 2kPi| <= 2^(2-m). Thus we can decide the sign
92              of sin(x) if xr is at distance at least 2^(2-m) of both
93              0 and +/-Pi. */
94           mpfr_div_2ui (c, c, 1, GMP_RNDN);
95           /* Since c approximates Pi with an error <= 2^(2-expx-m) <= 2^(-m),
96              it suffices to check that c - |xr| >= 2^(2-m). */
97           if (MPFR_SIGN (xr) > 0)
98             mpfr_sub (c, c, xr, GMP_RNDZ);
99           else
100             mpfr_add (c, c, xr, GMP_RNDZ);
101           if (MPFR_IS_ZERO(xr) || MPFR_EXP(xr) < (mp_exp_t) 3 - (mp_exp_t) m
102               || MPFR_EXP(c) < (mp_exp_t) 3 - (mp_exp_t) m)
103             goto ziv_next;
104
105           /* |xr - x - 2kPi| <= 2^(2-m), thus |sin(xr) - sin(x)| <= 2^(2-m) */
106           xx = xr;
107         }
108       else /* the input argument is already reduced */
109         {
110           reduce = 0;
111           xx = x;
112         }
113
114       sign = MPFR_SIGN(xx);
115       /* now that the argument is reduced, precision m is enough */
116       mpfr_set_prec (c, m);
117       mpfr_cos (c, xx, GMP_RNDZ);    /* can't be exact */
118       mpfr_nexttoinf (c);           /* now c = cos(x) rounded away */
119       mpfr_mul (c, c, c, GMP_RNDU); /* away */
120       mpfr_ui_sub (c, 1, c, GMP_RNDZ);
121       mpfr_sqrt (c, c, GMP_RNDZ);
122       if (MPFR_IS_NEG_SIGN(sign))
123         MPFR_CHANGE_SIGN(c);
124
125       /* Warning: c may be 0! */
126       if (MPFR_UNLIKELY (MPFR_IS_ZERO (c)))
127         {
128           /* Huge cancellation: increase prec a lot! */
129           m = MAX (m, MPFR_PREC (x));
130           m = 2 * m;
131         }
132       else
133         {
134           /* the absolute error on c is at most 2^(3-m-EXP(c)),
135              plus 2^(2-m) if there was an argument reduction.
136              Since EXP(c) <= 1, 3-m-EXP(c) >= 2-m, thus the error
137              is at most 2^(3-m-EXP(c)) in case of argument reduction. */
138           err = 2 * MPFR_GET_EXP (c) + (mp_exp_t) m - 3 - (reduce != 0);
139           if (MPFR_CAN_ROUND (c, err, precy, rnd_mode))
140             break;
141
142           /* check for huge cancellation (Near 0) */
143           if (err < (mp_exp_t) MPFR_PREC (y))
144             m += MPFR_PREC (y) - err;
145           /* Check if near 1 */
146           if (MPFR_GET_EXP (c) == 1)
147             m += m;
148         }
149
150     ziv_next:
151       /* Else generic increase */
152       MPFR_ZIV_NEXT (loop, m);
153     }
154   MPFR_ZIV_FREE (loop);
155
156   inexact = mpfr_set (y, c, rnd_mode);
157   /* inexact cannot be 0, since this would mean that c was representable
158      within the target precision, but in that case mpfr_can_round will fail */
159
160   mpfr_clear (c);
161   mpfr_clear (xr);
162
163   MPFR_SAVE_EXPO_FREE (expo);
164   return mpfr_check_range (y, inexact, rnd_mode);
165 }