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

« back to all changes in this revision

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

  • Committer: Bazaar Package Importer
  • Author(s): Peter Van Eynde
  • Date: 2007-04-09 11:51:51 UTC
  • mfrom: (1.1.3 upstream)
  • Revision ID: james.westby@ubuntu.com-20070409115151-ql8cr0kalzx1jmla
Tags: 0.9i-20070324-2
Upload to unstable. 

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* mpfr_cmp_ui -- compare a floating-point number with a machine integer
2
 
 
3
 
Copyright 1999, 2001, 2002 Free Software Foundation, Inc.
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 <stdio.h>
23
 
#include "gmp.h"
24
 
#include "gmp-impl.h"
25
 
#include "longlong.h"
26
 
#include "mpfr.h"
27
 
#include "mpfr-impl.h"
28
 
 
29
 
/* returns a positive value if b>i*2^f,
30
 
           a negative value if b<i*2^f,
31
 
           zero if b=i*2^f
32
 
*/
33
 
 
34
 
int 
35
 
mpfr_cmp_ui_2exp (mpfr_srcptr b, unsigned long int i, int f)
36
 
{
37
 
  int e, k, bn;
38
 
  mp_limb_t c, *bp;
39
 
 
40
 
  MPFR_ASSERTN(!MPFR_IS_NAN(b));
41
 
 
42
 
  if (MPFR_IS_INF(b))
43
 
    return MPFR_SIGN(b);
44
 
 
45
 
  /* now b is neither NaN nor +/-Infinity */
46
 
  if (MPFR_IS_ZERO(b))
47
 
    return i ? -1 : 0;
48
 
  else if (MPFR_SIGN(b) < 0)
49
 
    return -1;
50
 
  /* now b>0 */
51
 
  else if (i == 0)
52
 
    return 1;
53
 
  else
54
 
    { /* b>0, i>0 */
55
 
      e = MPFR_EXP(b); /* 2^(e-1) <= b < 2^e */
56
 
      if (e > f + BITS_PER_MP_LIMB)
57
 
        return 1;
58
 
 
59
 
    c = (mp_limb_t) i;
60
 
    count_leading_zeros(k, c);
61
 
    k = f + BITS_PER_MP_LIMB - k; /* 2^(k-1) <= i*2^f < 2^k */
62
 
    if (k != e)
63
 
      return e - k;
64
 
 
65
 
    /* now k=e */
66
 
    c <<= f + BITS_PER_MP_LIMB - k;
67
 
    bn = (MPFR_PREC(b) - 1) / BITS_PER_MP_LIMB;
68
 
    bp = MPFR_MANT(b) + bn;
69
 
    if (*bp > c)
70
 
      return 1;
71
 
    if (*bp < c)
72
 
      return -1;
73
 
 
74
 
    /* most significant limbs agree, check remaining limbs from b */
75
 
    while (--bn >= 0)
76
 
      if (*--bp)
77
 
        return 1;
78
 
    return 0;
79
 
    }
80
 
}
81
 
 
82
 
/* returns a positive value if b>i*2^f,
83
 
           a negative value if b<i*2^f,
84
 
           zero if b=i*2^f 
85
 
*/
86
 
 
87
 
int 
88
 
mpfr_cmp_si_2exp (mpfr_srcptr b, long int i, int f)
89
 
{
90
 
  int e, k, bn, si;
91
 
  mp_limb_t c, *bp;
92
 
 
93
 
  MPFR_ASSERTN(!MPFR_IS_NAN(b));
94
 
 
95
 
  si = i < 0 ? -1 : 1; /* sign of i */
96
 
  if (MPFR_IS_INF(b) || (MPFR_NOTZERO(b) && MPFR_SIGN(b) != si))
97
 
    return MPFR_SIGN(b);
98
 
  /* both signs differ */
99
 
  else if (MPFR_IS_ZERO(b) || i == 0)
100
 
    return i == 0 ? (MPFR_IS_ZERO(b) ? 0 : MPFR_SIGN(b)) : -si;
101
 
  else
102
 
    { /* b and i are of same sign */
103
 
      e = MPFR_EXP(b); /* 2^(e-1) <= b < 2^e */
104
 
      if (e > f + BITS_PER_MP_LIMB)
105
 
        return si;
106
 
 
107
 
      c = i < 0 ? - (mp_limb_t) i : (mp_limb_t) i;
108
 
      count_leading_zeros(k, c);
109
 
      k = f + BITS_PER_MP_LIMB - k; /* 2^(k-1) <= i*2^f < 2^k */
110
 
      if (k != e)
111
 
        return si * (e - k);
112
 
 
113
 
      /* now k = e */
114
 
      c <<= f + BITS_PER_MP_LIMB - k;
115
 
      bn = (MPFR_PREC(b) - 1) / BITS_PER_MP_LIMB;
116
 
      bp = MPFR_MANT(b) + bn;
117
 
      if (*bp > c)
118
 
        return si;
119
 
      if (*bp < c)
120
 
        return -si;
121
 
 
122
 
      /* most significant limbs agree, check remaining limbs from b */
123
 
      while (--bn >= 0)
124
 
        if (*--bp)
125
 
          return si;
126
 
      return 0;
127
 
    }
128
 
}