summaryrefslogtreecommitdiff
path: root/set_z.c
blob: 0367a866917d5beebff771923a4ac37378c7a634 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
/* mpfr_set_z -- set a floating-point number from a multiple-precision integer

Copyright (C) 1999 Free Software Foundation.

This file is part of the MPFR Library.

The MPFR Library is free software; you can redistribute it and/or modify
it under the terms of the GNU Library General Public License as published by
the Free Software Foundation; either version 2 of the License, or (at your
option) any later version.

The MPFR Library is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Library General Public
License for more details.

You should have received a copy of the GNU Library General Public License
along with the MPFR Library; see the file COPYING.LIB.  If not, write to
the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
MA 02111-1307, USA. */

#include "gmp.h"
#include "mpfr.h"
#include "gmp-impl.h"
#include "longlong.h"

/* set f to the integer z */
int 
#if __STDC__
mpfr_set_z (mpfr_ptr f, mpz_srcptr z, mp_rnd_t rnd)
#else
mpfr_set_z (f, z, rnd) 
     mpfr_ptr f;
     mpz_srcptr z;
     mp_rnd_t rnd;
#endif
{
  int fn, zn, k, dif, sign_z, sh; mp_limb_t *fp = MPFR_MANT(f), *zp, cc, c2;

  sign_z = mpz_cmp_ui(z,0);
  if (sign_z==0) {
    MPFR_SET_ZERO(f);
    return 0;
  }
  fn = 1 + (MPFR_PREC(f)-1)/BITS_PER_MP_LIMB;
  zn = ABS(SIZ(z));
  dif = zn-fn;
  zp = PTR(z);
  count_leading_zeros(k, zp[zn-1]);
  MPFR_EXP(f) = zn*BITS_PER_MP_LIMB-k;
  if (MPFR_SIGN(f)*sign_z<0) MPFR_CHANGE_SIGN(f);
  if (dif>=0) { /* number has to be truncated */
    if (k) {
      mpn_lshift(fp, zp + dif, fn, k);
      if (dif) *fp += zp[dif-1] >> (BITS_PER_MP_LIMB-k);
    }
    else MPN_COPY(fp, zp + dif, fn);
    sh = fn*BITS_PER_MP_LIMB-MPFR_PREC(f);
    cc = *fp & (((mp_limb_t)1 << sh) - 1);
    *fp = *fp & ~cc;
    if (rnd==GMP_RNDN) {
      if (sh) c2 = (mp_limb_t)1 << (sh-1);
      else { /* sh=0 */
	c2 = ((mp_limb_t)1) << (BITS_PER_MP_LIMB-1);
	dif--;
	cc = (dif>=0) ? ((zp[dif])<<k) : 0;
	if (dif>0 && k) cc += zp[dif-1] >> (BITS_PER_MP_LIMB-k);
      }
      /* now compares cc to c2 */
      if (cc>c2) { mpfr_add_one_ulp(f); return cc; }
      else if (cc<c2) goto towards_zero;
      else {
	cc=0;
	while (dif>0 && (cc=zp[dif-1])==0) dif--;
	if (cc) { mpfr_add_one_ulp(f); return cc; }
	else /* exactly in middle: inexact in both cases */
	  if (*fp & ((mp_limb_t)1<<sh)) { mpfr_add_one_ulp(f); return 1; }
	  else return 1;
      }
    }
    else if ((sign_z>0 && rnd==GMP_RNDU) || (sign_z<0 && rnd==GMP_RNDD)) {
      /* round towards infinity */
      /* result is exact iff all remaining bits are zero */
      if (dif>0 && cc==0) cc=zp[--dif]<<k;
      while (cc==0 && dif>0) cc=zp[--dif];
      if (cc) { mpfr_add_one_ulp(f); return 1; }
      else return 0;
    }
    else { /* round towards zero */
      /* result is exact iff all remaining bits are zero */
    towards_zero:
      if (cc==0 && dif>0) cc=zp[--dif]<<k;
      while (cc==0 && dif>0) cc=zp[--dif];
      return cc;
    }
  }
  else {
    if (k) mpn_lshift(fp-dif, zp, zn, k);
    else MPN_COPY(fp-dif, zp, zn);
    /* fill with zeroes */
    MPN_ZERO(fp, -dif);
    return 0; /* result is exact */
  }
}