1/* mpfr_get_sj -- convert a MPFR number to a huge machine signed integer
2
3Copyright 2004, 2006-2023 Free Software Foundation, Inc.
4Contributed by the AriC and Caramba projects, INRIA.
5
6This file is part of the GNU MPFR Library.
7
8The GNU MPFR Library is free software; you can redistribute it and/or modify
9it under the terms of the GNU Lesser General Public License as published by
10the Free Software Foundation; either version 3 of the License, or (at your
11option) any later version.
12
13The GNU MPFR Library is distributed in the hope that it will be useful, but
14WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
15or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
16License for more details.
17
18You should have received a copy of the GNU Lesser General Public License
19along with the GNU MPFR Library; see the file COPYING.LESSER.  If not, see
20https://www.gnu.org/licenses/ or write to the Free Software Foundation, Inc.,
2151 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. */
22
23#define MPFR_NEED_INTMAX_H
24#include "mpfr-impl.h"
25
26#ifdef _MPFR_H_HAVE_INTMAX_T
27
28intmax_t
29mpfr_get_sj (mpfr_srcptr f, mpfr_rnd_t rnd)
30{
31  intmax_t r;
32  mpfr_prec_t prec;
33  mpfr_t x;
34  MPFR_SAVE_EXPO_DECL (expo);
35
36  if (MPFR_UNLIKELY (!mpfr_fits_intmax_p (f, rnd)))
37    {
38      MPFR_SET_ERANGEFLAG ();
39      return MPFR_IS_NAN (f) ? 0 :
40        MPFR_IS_NEG (f) ? INTMAX_MIN : INTMAX_MAX;
41    }
42
43  if (MPFR_IS_ZERO (f))
44     return (intmax_t) 0;
45
46  /* Determine the precision of intmax_t. |INTMAX_MIN| may have one
47     more bit as an integer, but in this case, this is a power of 2,
48     thus fits in a precision-prec floating-point number. */
49  for (r = INTMAX_MAX, prec = 0; r != 0; r /= 2, prec++)
50    { }
51
52  MPFR_ASSERTD (r == 0);
53
54  MPFR_SAVE_EXPO_MARK (expo);
55
56  mpfr_init2 (x, prec);
57  mpfr_rint (x, f, rnd);
58  MPFR_ASSERTN (MPFR_IS_FP (x));
59
60  /* The flags from mpfr_rint are the wanted ones. In particular,
61     it sets the inexact flag when necessary. */
62  MPFR_SAVE_EXPO_UPDATE_FLAGS (expo, __gmpfr_flags);
63
64  if (MPFR_NOTZERO (x))
65    {
66      mp_limb_t *xp;
67      int sh;        /* An int should be sufficient in this context. */
68
69      xp = MPFR_MANT (x);
70      sh = MPFR_GET_EXP (x);
71      MPFR_ASSERTN ((mpfr_prec_t) sh <= prec + 1);
72      if (INTMAX_MIN + INTMAX_MAX != 0
73          && MPFR_UNLIKELY ((mpfr_prec_t) sh > prec))
74        {
75          /* 2's complement and x <= INTMAX_MIN: in the case mp_limb_t
76             has the same size as intmax_t, we cannot use the code in
77             the for loop since the operations would be performed in
78             unsigned arithmetic. */
79          MPFR_ASSERTD (MPFR_IS_NEG (x) && mpfr_powerof2_raw (x));
80          r = INTMAX_MIN;
81        }
82      /* sh is the number of bits that remain to be considered in {xp, xn} */
83      else
84        {
85#ifdef MPFR_INTMAX_WITHIN_LIMB
86          MPFR_ASSERTD (sh > 0 && sh < GMP_NUMB_BITS);
87          r = xp[0] >> (GMP_NUMB_BITS - sh);
88#else
89          int n;
90
91          /* Note: testing the condition sh > 0 is necessary to avoid
92             an undefined behavior on xp[n] >> S when S >= GMP_NUMB_BITS
93             (even though xp[n] == 0 in such a case). This can happen if
94             sizeof(mp_limb_t) < sizeof(intmax_t) and |x| is small enough
95             because of the trailing bits due to its normalization. */
96          for (n = MPFR_LIMB_SIZE (x) - 1; n >= 0 && sh > 0; n--)
97            {
98              sh -= GMP_NUMB_BITS;
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              MPFR_ASSERTD (-sh < GMP_NUMB_BITS);
107              /* each limb should be shifted by sh bits to the left if sh>=0,
108                 and by sh bits to the right if sh < 0 */
109              r += sh >= 0
110                ? (intmax_t) xp[n] << sh
111                : (intmax_t) (xp[n] >> (-sh));
112            }
113#endif
114          if (MPFR_IS_NEG(x))
115            r = -r;
116        }
117    }
118
119  mpfr_clear (x);
120
121  MPFR_SAVE_EXPO_FREE (expo);
122
123  return r;
124}
125
126#endif
127