summaryrefslogtreecommitdiff
path: root/trunc.c
blob: c2bd5cd22202fe2240813fc9c6bab9638fd44a25 (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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
/* mpf_trunc, mpf_floor, mpf_ceil -- Assign a float from another float while
   rounding it to an integer.

Copyright (C) 1997, 1998 Free Software Foundation, Inc.

This file is part of the GNU MP Library.

The GNU MP 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 GNU MP 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 GNU MP 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 <stdio.h>
#include "gmp.h"
#include "gmp-impl.h"
#include "mpfr.h"
#include "mpfr-impl.h"

#if MPFR_FLOOR
#define _MPFR_FLOOR_OR_CEIL
#define FUNC_NAME mpfr_floor
#undef MPFR_FLOOR
#define MPFR_FLOOR 1
#define MPFR_CEIL 0
#endif

#if MPFR_CEIL
#define _MPFR_FLOOR_OR_CEIL
#define FUNC_NAME mpfr_ceil
#undef MPFR_CEIL
#define MPFR_CEIL 1
#define MPFR_FLOOR 0
#endif

#if MPFR_TRUNC
#undef FUNC_NAME
#define FUNC_NAME mpfr_trunc
#endif

#ifdef _MPFR_FLOOR_OR_CEIL
static int
mpn_zero_p (p, n)
     mp_ptr p;
     mp_size_t n;
{
  mp_size_t i;

  for (i = 0; i < n; i++)
    {
      if (p[i] != 0)
	return 0;
    }

  return 1;
}
#endif

void
#if __STDC__
FUNC_NAME (mpfr_ptr r, mpfr_srcptr u)
#else
FUNC_NAME (r, u)
     mpfr_ptr r;
     mpfr_srcptr u;
#endif
{
  mp_ptr rp, up;
  mp_size_t usize;
  mp_size_t rsize, rw;
#ifdef _MPFR_FLOOR_OR_CEIL
  mp_size_t ignored_n;
#endif
  mp_exp_t exp;
  int signu; long diff;

  if (MPFR_IS_NAN(u)) {
    MPFR_SET_NAN(r);
    return;
  }

  MPFR_CLEAR_NAN(r);

  if (MPFR_IS_INF(u)) {
    MPFR_SET_INF(r);
    if (MPFR_SIGN(r) != MPFR_SIGN(u)) MPFR_CHANGE_SIGN(r);
    return;
  }

  MPFR_CLEAR_INF(r);

  if (!MPFR_NOTZERO(u)) {
    MPFR_SET_ZERO(r);
    return;
  }

  signu = MPFR_SIZE(u);
  rp = MPFR_MANT(r);
  exp = MPFR_EXP(u);
  rsize = (MPFR_PREC(r) - 1)/BITS_PER_MP_LIMB + 1;

  /* Single out the case where |u| < 1.  */
  if (exp <= 0)
    {
#ifdef _MPFR_FLOOR_OR_CEIL
      if ((MPFR_FLOOR && signu < 0) || (MPFR_CEIL && signu >= 0))
	{
	  rp[rsize-1] = (mp_limb_t) 1 << (BITS_PER_MP_LIMB-1);
	  MPN_ZERO(rp, rsize-1);
	  /* sign of result is that of u */
	  if (MPFR_SIGN(r) * signu < 0) MPFR_CHANGE_SIGN(r);
	  MPFR_EXP(r) = 1;
	  return;
	}
#endif
      MPFR_SET_ZERO(r);
      return;
    }

  usize = (MPFR_PREC(u) - 1)/BITS_PER_MP_LIMB + 1;

#ifdef _MPFR_FLOOR_OR_CEIL
  ignored_n = 0;
#endif
  up = MPFR_MANT(u);

  if (usize > rsize)
    {
#ifdef _MPFR_FLOOR_OR_CEIL
      ignored_n = usize - rsize;
#endif
      up += usize - rsize;
      usize = rsize;
    }

  diff = BITS_PER_MP_LIMB * usize - exp;
  if (diff > 0)
    {
      diff = diff/BITS_PER_MP_LIMB;
#ifdef _MPFR_FLOOR_OR_CEIL
      ignored_n += diff;
#endif
      up += diff;
      usize -= diff;
    }

  /* number of non significant bits in low limb of r */
  rw = usize * BITS_PER_MP_LIMB - exp;
  MPN_ZERO(rp, rsize-usize);
  rp += rsize-usize;

#ifdef _MPFR_FLOOR_OR_CEIL
  if (((MPFR_FLOOR && signu < 0) || (MPFR_CEIL && signu >= 0))
      && (!mpn_zero_p (up - ignored_n, ignored_n)
	  || (rw && (up[0] << (BITS_PER_MP_LIMB-rw)))))
    {
      mp_limb_t cy;
      cy = mpn_add_1 (rp, up, usize, (mp_limb_t) 1 << rw);
      if (cy != 0)
	{
	  /* all the bits from "1<<rw" upwards are zero */
	  rp[usize-1] = (mp_limb_t) 1 << (BITS_PER_MP_LIMB - 1);
	  exp++;
	}
    }
  else
#endif
    {
      if (rp != up)
	MPN_COPY (rp, up, usize);
    }

  /* Put to 0 the remaining bits */
  if (rw) rp[0] &=
	    ~((((mp_limb_t)1)<<rw) - (mp_limb_t)1);

  MPFR_EXP(r) = exp;
  if (MPFR_SIGN(r) * signu < 0) MPFR_CHANGE_SIGN(r);
}