Blame mpf/get_ui.c

Packit 5c3484
/* mpf_get_ui -- mpf to ulong conversion
Packit 5c3484
Packit 5c3484
Copyright 2001, 2002, 2004 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
Packit 5c3484
Packit 5c3484
/* Any fraction bits are truncated, meaning simply discarded.
Packit 5c3484
Packit 5c3484
   For values bigger than a ulong, the low bits are returned (the low
Packit 5c3484
   absolute value bits actually), like mpz_get_ui, but this isn't
Packit 5c3484
   documented.
Packit 5c3484
Packit 5c3484
   Notice this is equivalent to mpz_set_f + mpz_get_ui.
Packit 5c3484
Packit 5c3484
Packit 5c3484
   Implementation:
Packit 5c3484
Packit 5c3484
   The limb just above the radix point for us to extract is ptr[size-exp].
Packit 5c3484
Packit 5c3484
   We need to check that the size-exp index falls in our available data
Packit 5c3484
   range, 0 to size-1 inclusive.  We test this without risk of an overflow
Packit 5c3484
   involving exp by requiring size>=exp (giving size-exp >= 0) and exp>0
Packit 5c3484
   (giving size-exp <= size-1).
Packit 5c3484
Packit 5c3484
   Notice if size==0 there's no fetch, since of course size>=exp and exp>0
Packit 5c3484
   can only be true if size>0.  So there's no special handling for size==0,
Packit 5c3484
   it comes out as 0 the same as any other time we have no data at our
Packit 5c3484
   target index.
Packit 5c3484
Packit 5c3484
   For nails, the second limb above the radix point is also required, this
Packit 5c3484
   is ptr[size-exp+1].
Packit 5c3484
Packit 5c3484
   Again we need to check that size-exp+1 falls in our data range, 0 to
Packit 5c3484
   size-1 inclusive.  We test without risk of overflow by requiring
Packit 5c3484
   size+1>=exp (giving size-exp+1 >= 0) and exp>1 (giving size-exp+1 <=
Packit 5c3484
   size-1).
Packit 5c3484
Packit 5c3484
   And again if size==0 these second fetch conditions are not satisfied
Packit 5c3484
   either since size+1>=exp and exp>1 are only true if size>0.
Packit 5c3484
Packit 5c3484
   The code is arranged with exp>0 wrapping the exp>1 test since exp>1 is
Packit 5c3484
   mis-compiled by alpha gcc prior to version 3.4.  It re-writes it as
Packit 5c3484
   exp-1>0, which is incorrect when exp==MP_EXP_T_MIN.  By having exp>0
Packit 5c3484
   tested first we ensure MP_EXP_T_MIN doesn't reach exp>1.  */
Packit 5c3484
Packit 5c3484
unsigned long
Packit 5c3484
mpf_get_ui (mpf_srcptr f) __GMP_NOTHROW
Packit 5c3484
{
Packit 5c3484
  mp_size_t size;
Packit 5c3484
  mp_exp_t exp;
Packit 5c3484
  mp_srcptr fp;
Packit 5c3484
  mp_limb_t fl;
Packit 5c3484
Packit 5c3484
  exp = EXP (f);
Packit 5c3484
  size = SIZ (f);
Packit 5c3484
  fp = PTR (f);
Packit 5c3484
Packit 5c3484
  fl = 0;
Packit 5c3484
  if (exp > 0)
Packit 5c3484
    {
Packit 5c3484
      /* there are some limbs above the radix point */
Packit 5c3484
Packit 5c3484
      size = ABS (size);
Packit 5c3484
      if (size >= exp)
Packit 5c3484
        fl = fp[size-exp];
Packit 5c3484
Packit 5c3484
#if BITS_PER_ULONG > GMP_NUMB_BITS
Packit 5c3484
      if (exp > 1 && size+1 >= exp)
Packit 5c3484
        fl += (fp[size-exp+1] << GMP_NUMB_BITS);
Packit 5c3484
#endif
Packit 5c3484
    }
Packit 5c3484
Packit 5c3484
  return (unsigned long) fl;
Packit 5c3484
}