Fix rc script order.
[dragonfly.git] / contrib / mpfr / get_sj.c
1 /* mpfr_get_sj -- convert a MPFR number to a huge machine signed integer
2
3 Copyright 2004, 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 #ifdef HAVE_CONFIG_H
24 # include "config.h"            /* for a build within gmp */
25 #endif
26
27 /* The ISO C99 standard specifies that in C++ implementations the
28    INTMAX_MAX, ... macros should only be defined if explicitly requested.  */
29 #if defined __cplusplus
30 # define __STDC_LIMIT_MACROS
31 # define __STDC_CONSTANT_MACROS
32 #endif
33
34 #if HAVE_INTTYPES_H
35 # include <inttypes.h> /* for intmax_t */
36 #else
37 # if HAVE_STDINT_H
38 #  include <stdint.h>
39 # endif
40 #endif
41
42 #include "mpfr-impl.h"
43
44 #ifdef _MPFR_H_HAVE_INTMAX_T
45
46 intmax_t
47 mpfr_get_sj (mpfr_srcptr f, mpfr_rnd_t rnd)
48 {
49   intmax_t r;
50   mp_prec_t prec;
51   mpfr_t x;
52
53   if (!mpfr_fits_intmax_p (f, rnd))
54     {
55       MPFR_SET_ERANGE ();
56       return MPFR_IS_NEG (f) ? INTMAX_MIN : INTMAX_MAX;
57     }
58   if (MPFR_IS_ZERO (f))
59      return (intmax_t) 0;
60
61   /* determine the precision of intmax_t */
62   for (r = INTMAX_MIN, prec = 0; r != 0; r /= 2, prec++)
63     {
64     }
65   /* Note: though INTMAX_MAX would have been sufficient for the conversion,
66      we chose INTMAX_MIN so that INTMAX_MIN - 1 is always representable in
67      precision prec; this is useful to detect overflows in GMP_RNDZ (will
68      be needed later). */
69
70   /* Now, r = 0. */
71
72   mpfr_init2 (x, prec);
73   mpfr_rint (x, f, rnd);
74   MPFR_ASSERTN (MPFR_IS_FP (x));
75
76   if (MPFR_NOTZERO (x))
77     {
78       mp_limb_t *xp;
79       int sh, n;        /* An int should be sufficient in this context. */
80
81       xp = MPFR_MANT (x);
82       sh = MPFR_GET_EXP (x);
83       MPFR_ASSERTN ((mp_prec_t) sh <= prec);
84       if (INTMAX_MIN + INTMAX_MAX != 0
85           && MPFR_UNLIKELY ((mp_prec_t) sh == prec))
86         {
87           /* 2's complement and x <= INTMAX_MIN: in the case mp_limb_t
88              has the same size as intmax_t, we cannot use the code in
89              the for loop since the operations would be performed in
90              unsigned arithmetic. */
91           MPFR_ASSERTN (MPFR_IS_NEG (x) && (mpfr_powerof2_raw (x)));
92           r = INTMAX_MIN;
93         }
94       else if (MPFR_IS_POS (x))
95         {
96           for (n = MPFR_LIMB_SIZE (x) - 1; n >= 0; n--)
97             {
98               sh -= BITS_PER_MP_LIMB;
99               /* Note the concerning the casts below:
100                  When sh >= 0, the cast must be performed before the shift
101                  for the case sizeof(intmax_t) > sizeof(mp_limb_t).
102                  When sh < 0, the cast must be performed after the shift
103                  for the case sizeof(intmax_t) == sizeof(mp_limb_t), as
104                  mp_limb_t is unsigned, therefore not representable as an
105                  intmax_t when the MSB is 1 (this is the case here). */
106               r += (sh >= 0
107                     ? (intmax_t) xp[n] << sh
108                     : (intmax_t) (xp[n] >> (-sh)));
109             }
110         }
111       else
112         {
113           for (n = MPFR_LIMB_SIZE (x) - 1; n >= 0; n--)
114             {
115               sh -= BITS_PER_MP_LIMB;
116               /* See above for the note concerning the casts. */
117               r -= (sh >= 0
118                     ? (intmax_t) xp[n] << sh
119                     : (intmax_t) (xp[n] >> (-sh)));
120             }
121         }
122     }
123
124   mpfr_clear (x);
125
126   return r;
127 }
128
129 #endif