Add APIC_ID to extract apic id from local apic id field
[dragonfly.git] / contrib / mpfr / tan.c
1 /* mpfr_tan -- tangent 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 /* computes tan(x) = sign(x)*sqrt(1/cos(x)^2-1) */
27 int
28 mpfr_tan (mpfr_ptr y, mpfr_srcptr x, mp_rnd_t rnd_mode)
29 {
30   mp_prec_t precy, m;
31   int inexact;
32   mpfr_t s, c;
33   MPFR_ZIV_DECL (loop);
34   MPFR_SAVE_EXPO_DECL (expo);
35   MPFR_GROUP_DECL (group);
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       else /* x is zero */
48         {
49           MPFR_ASSERTD(MPFR_IS_ZERO(x));
50           MPFR_SET_ZERO(y);
51           MPFR_SET_SAME_SIGN(y, x);
52           MPFR_RET(0);
53         }
54     }
55
56   /* tan(x) = x + x^3/3 + ... so the error is < 2^(3*EXP(x)-1) */
57   MPFR_FAST_COMPUTE_IF_SMALL_INPUT (y, x, -2 * MPFR_GET_EXP (x), 1, 1,
58                                     rnd_mode, {});
59
60   MPFR_SAVE_EXPO_MARK (expo);
61
62   /* Compute initial precision */
63   precy = MPFR_PREC (y);
64   m = precy + MPFR_INT_CEIL_LOG2 (precy) + 13;
65   MPFR_ASSERTD (m >= 2); /* needed for the error analysis in algorithms.tex */
66
67   MPFR_GROUP_INIT_2 (group, m, s, c);
68   MPFR_ZIV_INIT (loop, m);
69   for (;;)
70     {
71       /* The only way to get an overflow is to get ~ Pi/2
72          But the result will be ~ 2^Prec(y). */
73       mpfr_sin_cos (s, c, x, GMP_RNDN); /* err <= 1/2 ulp on s and c */
74       mpfr_div (c, s, c, GMP_RNDN);     /* err <= 4 ulps */
75       MPFR_ASSERTD (!MPFR_IS_SINGULAR (c));
76       if (MPFR_LIKELY (MPFR_CAN_ROUND (c, m - 2, precy, rnd_mode)))
77         break;
78       MPFR_ZIV_NEXT (loop, m);
79       MPFR_GROUP_REPREC_2 (group, m, s, c);
80     }
81   MPFR_ZIV_FREE (loop);
82   inexact = mpfr_set (y, c, rnd_mode);
83   MPFR_GROUP_CLEAR (group);
84
85   MPFR_SAVE_EXPO_FREE (expo);
86   return mpfr_check_range (y, inexact, rnd_mode);
87 }