beta-0.89.2
[luatex.git] / source / libs / gmp / gmp-src / mpf / cmp.c
blob0c066721827171326003c2e0ff5d1933b23d4fda
1 /* mpf_cmp -- Compare two floats.
3 Copyright 1993, 1994, 1996, 2001, 2015 Free Software Foundation, Inc.
5 This file is part of the GNU MP Library.
7 The GNU MP Library is free software; you can redistribute it and/or modify
8 it under the terms of either:
10 * the GNU Lesser General Public License as published by the Free
11 Software Foundation; either version 3 of the License, or (at your
12 option) any later version.
16 * the GNU General Public License as published by the Free Software
17 Foundation; either version 2 of the License, or (at your option) any
18 later version.
20 or both in parallel, as here.
22 The GNU MP Library is distributed in the hope that it will be useful, but
23 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
24 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
25 for more details.
27 You should have received copies of the GNU General Public License and the
28 GNU Lesser General Public License along with the GNU MP Library. If not,
29 see https://www.gnu.org/licenses/. */
31 #include "gmp.h"
32 #include "gmp-impl.h"
34 int
35 mpf_cmp (mpf_srcptr u, mpf_srcptr v) __GMP_NOTHROW
37 mp_srcptr up, vp;
38 mp_size_t usize, vsize;
39 mp_exp_t uexp, vexp;
40 int cmp;
41 int usign;
43 usize = SIZ(u);
44 vsize = SIZ(v);
45 usign = usize >= 0 ? 1 : -1;
47 /* 1. Are the signs different? */
48 if ((usize ^ vsize) >= 0)
50 /* U and V are both non-negative or both negative. */
51 if (usize == 0)
52 /* vsize >= 0 */
53 return -(vsize != 0);
54 if (vsize == 0)
55 /* usize >= 0 */
56 return usize != 0;
57 /* Fall out. */
59 else
61 /* Either U or V is negative, but not both. */
62 return usign;
65 /* U and V have the same sign and are both non-zero. */
67 uexp = EXP(u);
68 vexp = EXP(v);
70 /* 2. Are the exponents different? */
71 if (uexp > vexp)
72 return usign;
73 if (uexp < vexp)
74 return -usign;
76 usize = ABS (usize);
77 vsize = ABS (vsize);
79 up = PTR (u);
80 vp = PTR (v);
82 #define STRICT_MPF_NORMALIZATION 0
83 #if ! STRICT_MPF_NORMALIZATION
84 /* Ignore zeroes at the low end of U and V. */
85 do {
86 mp_limb_t tl;
87 tl = up[0];
88 MPN_STRIP_LOW_ZEROS_NOT_ZERO (up, usize, tl);
89 tl = vp[0];
90 MPN_STRIP_LOW_ZEROS_NOT_ZERO (vp, vsize, tl);
91 } while (0);
92 #endif
94 if (usize > vsize)
96 cmp = mpn_cmp (up + usize - vsize, vp, vsize);
97 /* if (cmp == 0) */
98 /* return usign; */
99 ++cmp;
101 else if (vsize > usize)
103 cmp = mpn_cmp (up, vp + vsize - usize, usize);
104 /* if (cmp == 0) */
105 /* return -usign; */
107 else
109 cmp = mpn_cmp (up, vp, usize);
110 if (cmp == 0)
111 return 0;
113 return cmp > 0 ? usign : -usign;