kernel - Reenable acpi sleep states
[dragonfly.git] / contrib / mpfr / factorial.c
blob4cfb3238d465ae22eae32335b3e154760a10df28
1 /* mpfr_fac_ui -- factorial of a non-negative integer
3 Copyright 2001, 2004, 2005, 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 #define MPFR_NEED_LONGLONG_H
24 #include "mpfr-impl.h"
26 /* The computation of n! is done by
28 n!=prod^{n}_{i=1}i
31 /* FIXME: efficient problems with large arguments; see comments in gamma.c. */
33 int
34 mpfr_fac_ui (mpfr_ptr y, unsigned long int x, mp_rnd_t rnd_mode)
36 mpfr_t t; /* Variable of Intermediary Calculation*/
37 unsigned long i;
38 int round, inexact;
40 mp_prec_t Ny; /* Precision of output variable */
41 mp_prec_t Nt; /* Precision of Intermediary Calculation variable */
42 mp_prec_t err; /* Precision of error */
44 mp_rnd_t rnd;
45 MPFR_SAVE_EXPO_DECL (expo);
46 MPFR_ZIV_DECL (loop);
48 /***** test x = 0 and x == 1******/
49 if (MPFR_UNLIKELY (x <= 1))
50 return mpfr_set_ui (y, 1, rnd_mode); /* 0! = 1 and 1! = 1 */
52 MPFR_SAVE_EXPO_MARK (expo);
54 /* Initialisation of the Precision */
55 Ny = MPFR_PREC (y);
57 /* compute the size of intermediary variable */
58 Nt = Ny + 2 * MPFR_INT_CEIL_LOG2 (x) + 7;
60 mpfr_init2 (t, Nt); /* initialise of intermediary variable */
62 rnd = GMP_RNDZ;
63 MPFR_ZIV_INIT (loop, Nt);
64 for (;;)
66 /* compute factorial */
67 inexact = mpfr_set_ui (t, 1, rnd);
68 for (i = 2 ; i <= x ; i++)
70 round = mpfr_mul_ui (t, t, i, rnd);
71 /* assume the first inexact product gives the sign
72 of difference: is that always correct? */
73 if (inexact == 0)
74 inexact = round;
77 err = Nt - 1 - MPFR_INT_CEIL_LOG2 (Nt);
79 round = !inexact || mpfr_can_round (t, err, rnd, GMP_RNDZ,
80 Ny + (rnd_mode == GMP_RNDN));
82 if (MPFR_LIKELY (round))
84 /* If inexact = 0, then t is exactly x!, so round is the
85 correct inexact flag.
86 Otherwise, t != x! since we rounded to zero or away. */
87 round = mpfr_set (y, t, rnd_mode);
88 if (inexact == 0)
90 inexact = round;
91 break;
93 else if ((inexact < 0 && round <= 0)
94 || (inexact > 0 && round >= 0))
95 break;
96 else /* inexact and round have opposite signs: we cannot
97 compute the inexact flag. Restart using the
98 symmetric rounding. */
99 rnd = (rnd == GMP_RNDZ) ? GMP_RNDU : GMP_RNDZ;
101 MPFR_ZIV_NEXT (loop, Nt);
102 mpfr_set_prec (t, Nt);
104 MPFR_ZIV_FREE (loop);
106 mpfr_clear (t);
107 MPFR_SAVE_EXPO_FREE (expo);
108 return mpfr_check_range (y, inexact, rnd_mode);