Blame mpq/set_f.c

Packit 5c3484
/* mpq_set_f -- set an mpq from an mpf.
Packit 5c3484
Packit 5c3484
Copyright 2000-2002 Free Software Foundation, Inc.
Packit 5c3484
Packit 5c3484
This file is part of the GNU MP Library.
Packit 5c3484
Packit 5c3484
The GNU MP Library is free software; you can redistribute it and/or modify
Packit 5c3484
it under the terms of either:
Packit 5c3484
Packit 5c3484
  * the GNU Lesser General Public License as published by the Free
Packit 5c3484
    Software Foundation; either version 3 of the License, or (at your
Packit 5c3484
    option) any later version.
Packit 5c3484
Packit 5c3484
or
Packit 5c3484
Packit 5c3484
  * the GNU General Public License as published by the Free Software
Packit 5c3484
    Foundation; either version 2 of the License, or (at your option) any
Packit 5c3484
    later version.
Packit 5c3484
Packit 5c3484
or both in parallel, as here.
Packit 5c3484
Packit 5c3484
The GNU MP Library is distributed in the hope that it will be useful, but
Packit 5c3484
WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
Packit 5c3484
or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
Packit 5c3484
for more details.
Packit 5c3484
Packit 5c3484
You should have received copies of the GNU General Public License and the
Packit 5c3484
GNU Lesser General Public License along with the GNU MP Library.  If not,
Packit 5c3484
see https://www.gnu.org/licenses/.  */
Packit 5c3484
Packit 5c3484
#include "gmp.h"
Packit 5c3484
#include "gmp-impl.h"
Packit 5c3484
#include "longlong.h"
Packit 5c3484
Packit 5c3484
Packit 5c3484
void
Packit 5c3484
mpq_set_f (mpq_ptr q, mpf_srcptr f)
Packit 5c3484
{
Packit 5c3484
  mp_size_t  fexp = EXP(f);
Packit 5c3484
  mp_ptr     fptr = PTR(f);
Packit 5c3484
  mp_size_t  fsize = SIZ(f);
Packit 5c3484
  mp_size_t  abs_fsize = ABS(fsize);
Packit 5c3484
  mp_limb_t  flow;
Packit 5c3484
Packit 5c3484
  if (fsize == 0)
Packit 5c3484
    {
Packit 5c3484
      /* set q=0 */
Packit 5c3484
      SIZ(NUM(q)) = 0;
Packit 5c3484
      SIZ(DEN(q)) = 1;
Packit 5c3484
      PTR(DEN(q))[0] = 1;
Packit 5c3484
      return;
Packit 5c3484
    }
Packit 5c3484
Packit 5c3484
  /* strip low zero limbs from f */
Packit 5c3484
  flow = *fptr;
Packit 5c3484
  MPN_STRIP_LOW_ZEROS_NOT_ZERO (fptr, abs_fsize, flow);
Packit 5c3484
Packit 5c3484
  if (fexp >= abs_fsize)
Packit 5c3484
    {
Packit 5c3484
      /* radix point is to the right of the limbs, no denominator */
Packit 5c3484
      mp_ptr  num_ptr;
Packit 5c3484
Packit 5c3484
      num_ptr = MPZ_NEWALLOC (mpq_numref (q), fexp);
Packit 5c3484
      MPN_ZERO (num_ptr, fexp - abs_fsize);
Packit 5c3484
      MPN_COPY (num_ptr + fexp - abs_fsize, fptr, abs_fsize);
Packit 5c3484
Packit 5c3484
      SIZ(NUM(q)) = fsize >= 0 ? fexp : -fexp;
Packit 5c3484
      SIZ(DEN(q)) = 1;
Packit 5c3484
      PTR(DEN(q))[0] = 1;
Packit 5c3484
    }
Packit 5c3484
  else
Packit 5c3484
    {
Packit 5c3484
      /* radix point is within or to the left of the limbs, use denominator */
Packit 5c3484
      mp_ptr     num_ptr, den_ptr;
Packit 5c3484
      mp_size_t  den_size;
Packit 5c3484
Packit 5c3484
      den_size = abs_fsize - fexp;
Packit 5c3484
      num_ptr = MPZ_NEWALLOC (mpq_numref (q), abs_fsize);
Packit 5c3484
      den_ptr = MPZ_NEWALLOC (mpq_denref (q), den_size+1);
Packit 5c3484
Packit 5c3484
      if (flow & 1)
Packit 5c3484
        {
Packit 5c3484
          /* no powers of two to strip from numerator */
Packit 5c3484
Packit 5c3484
          MPN_COPY (num_ptr, fptr, abs_fsize);
Packit 5c3484
          MPN_ZERO (den_ptr, den_size);
Packit 5c3484
          den_ptr[den_size] = 1;
Packit 5c3484
        }
Packit 5c3484
      else
Packit 5c3484
        {
Packit 5c3484
          /* right shift numerator, adjust denominator accordingly */
Packit 5c3484
          int  shift;
Packit 5c3484
Packit 5c3484
          den_size--;
Packit 5c3484
          count_trailing_zeros (shift, flow);
Packit 5c3484
Packit 5c3484
          mpn_rshift (num_ptr, fptr, abs_fsize, shift);
Packit 5c3484
          abs_fsize -= (num_ptr[abs_fsize-1] == 0);
Packit 5c3484
Packit 5c3484
          MPN_ZERO (den_ptr, den_size);
Packit 5c3484
          den_ptr[den_size] = GMP_LIMB_HIGHBIT >> (shift-1);
Packit 5c3484
        }
Packit 5c3484
Packit 5c3484
      SIZ(NUM(q)) = fsize >= 0 ? abs_fsize : -abs_fsize;
Packit 5c3484
      SIZ(DEN(q)) = den_size + 1;
Packit 5c3484
    }
Packit 5c3484
}