1/* mpq_set_f -- set an mpq from an mpf.
2
3Copyright 2000, 2001, 2002 Free Software Foundation, Inc.
4
5This file is part of the GNU MP Library.
6
7The GNU MP Library is free software; you can redistribute it and/or modify
8it under the terms of the GNU Lesser General Public License as published by
9the Free Software Foundation; either version 3 of the License, or (at your
10option) any later version.
11
12The GNU MP Library is distributed in the hope that it will be useful, but
13WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
14or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
15License for more details.
16
17You should have received a copy of the GNU Lesser General Public License
18along with the GNU MP Library.  If not, see http://www.gnu.org/licenses/.  */
19
20#include "gmp.h"
21#include "gmp-impl.h"
22#include "longlong.h"
23
24
25void
26mpq_set_f (mpq_ptr q, mpf_srcptr f)
27{
28  mp_size_t  fexp = EXP(f);
29  mp_ptr     fptr = PTR(f);
30  mp_size_t  fsize = SIZ(f);
31  mp_size_t  abs_fsize = ABS(fsize);
32  mp_limb_t  flow;
33
34  if (fsize == 0)
35    {
36      /* set q=0 */
37      q->_mp_num._mp_size = 0;
38      q->_mp_den._mp_size = 1;
39      q->_mp_den._mp_d[0] = 1;
40      return;
41    }
42
43  /* strip low zero limbs from f */
44  flow = *fptr;
45  MPN_STRIP_LOW_ZEROS_NOT_ZERO (fptr, abs_fsize, flow);
46
47  if (fexp >= abs_fsize)
48    {
49      /* radix point is to the right of the limbs, no denominator */
50      mp_ptr  num_ptr;
51
52      MPZ_REALLOC (mpq_numref (q), fexp);
53      num_ptr = q->_mp_num._mp_d;
54      MPN_ZERO (num_ptr, fexp - abs_fsize);
55      MPN_COPY (num_ptr + fexp - abs_fsize, fptr, abs_fsize);
56
57      q->_mp_num._mp_size = fsize >= 0 ? fexp : -fexp;
58      q->_mp_den._mp_size = 1;
59      q->_mp_den._mp_d[0] = 1;
60    }
61  else
62    {
63      /* radix point is within or to the left of the limbs, use denominator */
64      mp_ptr     num_ptr, den_ptr;
65      mp_size_t  den_size;
66
67      den_size = abs_fsize - fexp;
68      MPZ_REALLOC (mpq_numref (q), abs_fsize);
69      MPZ_REALLOC (mpq_denref (q), den_size+1);
70      num_ptr = q->_mp_num._mp_d;
71      den_ptr = q->_mp_den._mp_d;
72
73      if (flow & 1)
74        {
75          /* no powers of two to strip from numerator */
76
77          MPN_COPY (num_ptr, fptr, abs_fsize);
78          MPN_ZERO (den_ptr, den_size);
79          den_ptr[den_size] = 1;
80        }
81      else
82        {
83          /* right shift numerator, adjust denominator accordingly */
84          int  shift;
85
86          den_size--;
87          count_trailing_zeros (shift, flow);
88
89          mpn_rshift (num_ptr, fptr, abs_fsize, shift);
90          abs_fsize -= (num_ptr[abs_fsize-1] == 0);
91
92          MPN_ZERO (den_ptr, den_size);
93          den_ptr[den_size] = GMP_LIMB_HIGHBIT >> (shift-1);
94        }
95
96      q->_mp_num._mp_size = fsize >= 0 ? abs_fsize : -abs_fsize;
97      q->_mp_den._mp_size = den_size + 1;
98    }
99}
100