~ubuntu-branches/ubuntu/intrepid/ecl/intrepid

« back to all changes in this revision

Viewing changes to src/gmp/mpfr/set_ui.c

  • Committer: Bazaar Package Importer
  • Author(s): Peter Van Eynde
  • Date: 2006-05-17 02:46:26 UTC
  • Revision ID: james.westby@ubuntu.com-20060517024626-lljr08ftv9g9vefl
Tags: upstream-0.9h-20060510
ImportĀ upstreamĀ versionĀ 0.9h-20060510

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* mpfr_set_ui -- set a MPFR number from a machine unsigned integer
 
2
 
 
3
Copyright 1999, 2000, 2001 Free Software Foundation.
 
4
 
 
5
This file is part of the MPFR Library.
 
6
 
 
7
The MPFR Library is free software; you can redistribute it and/or modify
 
8
it under the terms of the GNU Lesser General Public License as published by
 
9
the Free Software Foundation; either version 2.1 of the License, or (at your
 
10
option) any later version.
 
11
 
 
12
The MPFR Library is distributed in the hope that it will be useful, but
 
13
WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 
14
or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
 
15
License for more details.
 
16
 
 
17
You should have received a copy of the GNU Lesser General Public License
 
18
along with the MPFR Library; see the file COPYING.LIB.  If not, write to
 
19
the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
 
20
MA 02111-1307, USA. */
 
21
 
 
22
#include "gmp.h"
 
23
#include "gmp-impl.h"
 
24
#include "longlong.h"
 
25
#include "mpfr.h"
 
26
#include "mpfr-impl.h"
 
27
 
 
28
int
 
29
mpfr_set_ui (mpfr_ptr x, unsigned long i, mp_rnd_t rnd_mode)
 
30
{
 
31
  int inex = 0;
 
32
 
 
33
  MPFR_CLEAR_FLAGS(x);
 
34
  if (i == 0)
 
35
    MPFR_SET_ZERO(x);  /* the sign will be set later */
 
36
  else
 
37
    {
 
38
      mp_size_t xn;
 
39
      unsigned int cnt, nbits;
 
40
      mp_limb_t *xp;
 
41
 
 
42
      xn = (MPFR_PREC(x)-1)/BITS_PER_MP_LIMB;
 
43
      count_leading_zeros(cnt, (mp_limb_t) i);
 
44
 
 
45
      xp = MPFR_MANT(x);
 
46
      xp[xn] = ((mp_limb_t) i) << cnt;
 
47
      /* don't forget to put zero in lower limbs */
 
48
      MPN_ZERO(xp, xn);
 
49
 
 
50
      MPFR_EXP(x) = nbits = BITS_PER_MP_LIMB - cnt;
 
51
      inex = mpfr_check_range(x, rnd_mode);
 
52
      if (inex)
 
53
        return inex; /* underflow or overflow */
 
54
 
 
55
      /* round if MPFR_PREC(x) smaller than length of i */
 
56
      if (MPFR_PREC(x) < nbits)
 
57
        {
 
58
          int carry;
 
59
          carry = mpfr_round_raw(xp+xn, xp+xn, nbits, 0, MPFR_PREC(x),
 
60
                                 rnd_mode, &inex);
 
61
          if (carry)
 
62
            {
 
63
              mp_exp_t exp = MPFR_EXP(x);
 
64
 
 
65
              if (exp == __mpfr_emax)
 
66
                return mpfr_set_overflow(x, rnd_mode, 1);
 
67
 
 
68
              MPFR_EXP(x)++;
 
69
              xp[xn] = GMP_LIMB_HIGHBIT;
 
70
            }
 
71
        }
 
72
    }
 
73
 
 
74
  MPFR_SET_POS(x);
 
75
 
 
76
  MPFR_RET(inex);
 
77
}