xref: /dflybsd-src/contrib/mpfr/src/gen_inverse.h (revision 2786097444a0124b5d33763854de247e230c6629)
14a238c70SJohn Marino /* generic inverse of a function.
24a238c70SJohn Marino 
3*ab6d115fSJohn Marino Copyright 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013 Free Software Foundation, Inc.
4*ab6d115fSJohn Marino Contributed by the AriC and Caramel projects, INRIA.
54a238c70SJohn Marino 
64a238c70SJohn Marino This file is part of the GNU MPFR Library.
74a238c70SJohn Marino 
84a238c70SJohn Marino The GNU MPFR Library is free software; you can redistribute it and/or modify
94a238c70SJohn Marino it under the terms of the GNU Lesser General Public License as published by
104a238c70SJohn Marino the Free Software Foundation; either version 3 of the License, or (at your
114a238c70SJohn Marino option) any later version.
124a238c70SJohn Marino 
134a238c70SJohn Marino The GNU MPFR Library is distributed in the hope that it will be useful, but
144a238c70SJohn Marino WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
154a238c70SJohn Marino or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
164a238c70SJohn Marino License for more details.
174a238c70SJohn Marino 
184a238c70SJohn Marino You should have received a copy of the GNU Lesser General Public License
194a238c70SJohn Marino along with the GNU MPFR Library; see the file COPYING.LESSER.  If not, see
204a238c70SJohn Marino http://www.gnu.org/licenses/ or write to the Free Software Foundation, Inc.,
214a238c70SJohn Marino 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. */
224a238c70SJohn Marino 
234a238c70SJohn Marino #define MPFR_NEED_LONGLONG_H
244a238c70SJohn Marino #include "mpfr-impl.h"
254a238c70SJohn Marino 
264a238c70SJohn Marino #ifndef ACTION_SPECIAL
274a238c70SJohn Marino #define ACTION_SPECIAL
284a238c70SJohn Marino #endif
294a238c70SJohn Marino 
304a238c70SJohn Marino #ifndef ACTION_TINY
314a238c70SJohn Marino #define ACTION_TINY
324a238c70SJohn Marino #endif
334a238c70SJohn Marino 
344a238c70SJohn Marino /* example of use:
354a238c70SJohn Marino #define FUNCTION mpfr_sec
364a238c70SJohn Marino #define INVERSE  mpfr_cos
374a238c70SJohn Marino #define ACTION_NAN(y) do { MPFR_SET_NAN(y); MPFR_RET_NAN; } while (1)
384a238c70SJohn Marino #define ACTION_INF(y) do { MPFR_SET_NAN(y); MPFR_RET_NAN; } while (1)
394a238c70SJohn Marino #define ACTION_ZERO(y) return mpfr_set_ui (y, 1, MPFR_RNDN)
404a238c70SJohn Marino #include "gen_inverse.h"
414a238c70SJohn Marino */
424a238c70SJohn Marino 
434a238c70SJohn Marino int
FUNCTION(mpfr_ptr y,mpfr_srcptr x,mpfr_rnd_t rnd_mode)444a238c70SJohn Marino FUNCTION (mpfr_ptr y, mpfr_srcptr x, mpfr_rnd_t rnd_mode)
454a238c70SJohn Marino {
464a238c70SJohn Marino   mpfr_prec_t precy; /* target precision */
474a238c70SJohn Marino   mpfr_prec_t m;     /* working precision */
484a238c70SJohn Marino   mpfr_t z;        /* temporary variable to store INVERSE(x) */
494a238c70SJohn Marino   int inexact;     /* inexact flag */
504a238c70SJohn Marino   MPFR_ZIV_DECL (loop);
514a238c70SJohn Marino   MPFR_SAVE_EXPO_DECL (expo);
524a238c70SJohn Marino 
534a238c70SJohn Marino   if (MPFR_UNLIKELY(MPFR_IS_SINGULAR(x)))
544a238c70SJohn Marino     {
554a238c70SJohn Marino       if (MPFR_IS_NAN(x))
564a238c70SJohn Marino         ACTION_NAN(y);
574a238c70SJohn Marino       else if (MPFR_IS_INF(x))
584a238c70SJohn Marino         ACTION_INF(y);
594a238c70SJohn Marino       else /* x = 0 */
604a238c70SJohn Marino         ACTION_ZERO(y,x);
614a238c70SJohn Marino     }
624a238c70SJohn Marino 
634a238c70SJohn Marino   /* x is neither NaN, Inf nor zero */
644a238c70SJohn Marino   MPFR_SAVE_EXPO_MARK (expo);
654a238c70SJohn Marino   ACTION_TINY (y, x, rnd_mode); /* special case for very small input x */
664a238c70SJohn Marino   precy = MPFR_PREC(y);
674a238c70SJohn Marino   m = precy + MPFR_INT_CEIL_LOG2 (precy) + 3;
684a238c70SJohn Marino   mpfr_init2 (z, m);
694a238c70SJohn Marino 
704a238c70SJohn Marino   MPFR_ZIV_INIT (loop, m);
714a238c70SJohn Marino   for(;;)
724a238c70SJohn Marino     {
734a238c70SJohn Marino       MPFR_BLOCK_DECL (flags);
744a238c70SJohn Marino 
754a238c70SJohn Marino       MPFR_BLOCK (flags, INVERSE (z, x, MPFR_RNDZ)); /* error k_u < 1 ulp */
764a238c70SJohn Marino       /* FIXME: the following assumes that if an overflow happens with
774a238c70SJohn Marino          MPFR_EMAX_MAX, then necessarily an underflow happens with
784a238c70SJohn Marino          __gmpfr_emin */
794a238c70SJohn Marino       if (MPFR_OVERFLOW (flags))
804a238c70SJohn Marino         {
814a238c70SJohn Marino           int s = MPFR_SIGN(z);
824a238c70SJohn Marino           MPFR_ZIV_FREE (loop);
834a238c70SJohn Marino           mpfr_clear (z);
844a238c70SJohn Marino           MPFR_SAVE_EXPO_FREE (expo);
854a238c70SJohn Marino           return mpfr_underflow (y, (rnd_mode == MPFR_RNDN) ?
864a238c70SJohn Marino                                  MPFR_RNDZ : rnd_mode, s);
874a238c70SJohn Marino         }
884a238c70SJohn Marino       mpfr_ui_div (z, 1, z, MPFR_RNDN);
894a238c70SJohn Marino       /* the error is less than c_w + 2*c_u*k_u (see algorithms.tex),
904a238c70SJohn Marino          where c_w = 1/2, c_u = 1 since z was rounded toward zero,
914a238c70SJohn Marino          thus 1/2 + 2 < 4 */
924a238c70SJohn Marino       if (MPFR_LIKELY (MPFR_CAN_ROUND (z, m - 2, precy, rnd_mode)))
934a238c70SJohn Marino         break;
944a238c70SJohn Marino       ACTION_SPECIAL;
954a238c70SJohn Marino       MPFR_ZIV_NEXT (loop, m);
964a238c70SJohn Marino       mpfr_set_prec (z, m);
974a238c70SJohn Marino     }
984a238c70SJohn Marino   MPFR_ZIV_FREE (loop);
994a238c70SJohn Marino 
1004a238c70SJohn Marino   inexact = mpfr_set (y, z, rnd_mode);
1014a238c70SJohn Marino   mpfr_clear (z);
1024a238c70SJohn Marino 
1034a238c70SJohn Marino  end:
1044a238c70SJohn Marino   MPFR_SAVE_EXPO_FREE (expo);
1054a238c70SJohn Marino   return mpfr_check_range (y, inexact, rnd_mode);
1064a238c70SJohn Marino }
107