source: trunk/third/gmp/mpz/cmpabs_d.c @ 18191

Revision 18191, 2.9 KB checked in by ghudson, 22 years ago (diff)
This commit was generated by cvs2svn to compensate for changes in r18190, which included commits to RCS files with non-trunk default branches.
Line 
1/* mpz_cmpabs_d -- compare absolute values of mpz and double.
2
3Copyright 2001, 2002 Free Software Foundation, Inc.
4
5This file is part of the GNU MP Library.
6
7The GNU MP Library is free software; you can redistribute it and/or modify
8it under the terms of the GNU Lesser General Public License as published by
9the Free Software Foundation; either version 2.1 of the License, or (at your
10option) any later version.
11
12The GNU MP Library is distributed in the hope that it will be useful, but
13WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
14or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
15License for more details.
16
17You should have received a copy of the GNU Lesser General Public License
18along with the GNU MP Library; see the file COPYING.LIB.  If not, write to
19the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
20MA 02111-1307, USA.
21*/
22
23#include "gmp.h"
24#include "gmp-impl.h"
25
26
27#define RETURN_CMP(zl, dl)              \
28  do {                                  \
29    zlimb = (zl);                       \
30    dlimb = (dl);                       \
31    if (zlimb != dlimb)                 \
32      return (zlimb >= dlimb ? 1 : -1); \
33  } while (0)
34
35#define RETURN_NONZERO(ptr, size, val)          \
36  do {                                          \
37    mp_size_t __i;                              \
38    for (__i = (size)-1; __i >= 0; __i--)       \
39      if ((ptr)[__i] != 0)                      \
40        return val;                             \
41    return 0;                                   \
42  } while (0)
43
44
45int
46mpz_cmpabs_d (mpz_srcptr z, double d)
47{
48  mp_limb_t  darray[LIMBS_PER_DOUBLE], zlimb, dlimb;
49  mp_srcptr  zp;
50  mp_size_t  zsize;
51  int        dexp;
52
53  /* 1. Check for either operand zero. */
54  zsize = SIZ(z);
55  if (d == 0.0)
56    return (zsize != 0);
57  if (zsize == 0)
58    return (d != 0 ? -1 : 0);
59
60  /* 2. Ignore signs. */
61  zsize = ABS(zsize);
62  d = ABS(d);
63
64  /* 3. Small d, knowing abs(z) >= 1. */
65  if (d < 1.0)
66    return 1;
67
68  dexp = __gmp_extract_double (darray, d);
69  ASSERT (dexp >= 1);
70
71  /* 4. Check for different high limb positions. */
72  if (zsize != dexp)
73    return (zsize >= dexp ? 1 : -1);
74
75  /* 5. Limb data. */
76  zp = PTR(z);
77
78#if LIMBS_PER_DOUBLE == 2
79  RETURN_CMP (zp[zsize-1], darray[1]);
80  if (zsize == 1)
81    return (darray[0] != 0 ? -1 : 0);
82
83  RETURN_CMP (zp[zsize-2], darray[0]);
84  RETURN_NONZERO (zp, zsize-2, 1);
85
86#else
87#if LIMBS_PER_DOUBLE == 3
88  RETURN_CMP (zp[zsize-1], darray[2]);
89  if (zsize == 1)
90    return ((darray[0] | darray[1]) != 0 ? -1 : 0);
91
92  RETURN_CMP (zp[zsize-2], darray[1]);
93  if (zsize == 2)
94    return (darray[0] != 0 ? -1 : 0);
95
96  RETURN_CMP (zp[zsize-3], darray[0]);
97  RETURN_NONZERO (zp, zsize-3, 1);
98
99#else
100  for (i = 1; i <= LIMBS_PER_DOUBLE; i++)
101    {
102      RETURN_CMP (zp[zsize-i], darray[LIMBS_PER_DOUBLE-i]);
103      if (i >= zsize)
104        RETURN_NONZERO (darray, LIMBS_PER_DOUBLE-i, -1);
105    }
106  RETURN_NONZERO (zp, zsize-LIMBS_PER_DOUBLE, 1);
107#endif
108#endif
109}
Note: See TracBrowser for help on using the repository browser.