1 #line 2 "../src/kernel/none/cmp.c"
2 /* $Id: cmp.c 6490 2005-01-13 21:50:15Z kb $
3
4 Copyright (C) 2002-2003 The PARI group.
5
6 This file is part of the PARI/GP package.
7
8 PARI/GP is free software; you can redistribute it and/or modify it under the
9 terms of the GNU General Public License as published by the Free Software
10 Foundation. It is distributed in the hope that it will be useful, but WITHOUT
11 ANY WARRANTY WHATSOEVER.
12
13 Check the License for details. You should have received a copy of it, along
14 with the package; see the file 'COPYING'. If not, write to the Free Software
15 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
16
17
18 /********************************************************************/
19 /** **/
20 /** Comparison routines **/
21 /** **/
22 /********************************************************************/
23
24 /*They depend on absi_cmp_lg and absi_equal_lg in mp.c*/
25
26 #define MASK(x) (((ulong)(x)) & (LGBITS | SIGNBITS))
27 int
equalii(GEN x,GEN y)28 equalii(GEN x, GEN y)
29 {
30 if (MASK(x[1]) != MASK(y[1])) return 0;
31 return absi_equal_lg(x, y, lgefint(x));
32 }
33 #undef MASK
34
35 /* x == y ? */
36 int
equalsi(long x,GEN y)37 equalsi(long x, GEN y)
38 {
39 if (!x) return !signe(y);
40 if (x > 0)
41 {
42 if (signe(y) <= 0 || lgefint(y) != 3) return 0;
43 return ((ulong)y[2] == (ulong)x);
44 }
45 if (signe(y) >= 0 || lgefint(y) != 3) return 0;
46 return ((ulong)y[2] == (ulong)-x);
47 }
48 /* x == |y| ? */
49 int
equalui(ulong x,GEN y)50 equalui(ulong x, GEN y)
51 {
52 if (!x) return !signe(y);
53 return (lgefint(y) == 3 && (ulong)y[2] == x);
54 }
55
56 int
cmpsi(long x,GEN y)57 cmpsi(long x, GEN y)
58 {
59 ulong p;
60
61 if (!x) return -signe(y);
62
63 if (x>0)
64 {
65 if (signe(y)<=0) return 1;
66 if (lgefint(y)>3) return -1;
67 p=y[2]; if (p == (ulong)x) return 0;
68 return p < (ulong)x ? 1 : -1;
69 }
70
71 if (signe(y)>=0) return -1;
72 if (lgefint(y)>3) return 1;
73 p=y[2]; if (p == (ulong)-x) return 0;
74 return p < (ulong)(-x) ? -1 : 1;
75 }
76
77 /* compare x and |y| */
78 int
cmpui(ulong x,GEN y)79 cmpui(ulong x, GEN y)
80 {
81 long l = lgefint(y);
82 ulong p;
83
84 if (!x) return (l > 2)? -1: 0;
85 if (l == 2) return 1;
86 if (l > 3) return -1;
87 p = y[2]; if (p == x) return 0;
88 return p < x ? 1 : -1;
89 }
90
91 int
cmpii(GEN x,GEN y)92 cmpii(GEN x, GEN y)
93 {
94 const long sx = signe(x), sy = signe(y);
95 long lx,ly;
96
97 if (sx<sy) return -1;
98 if (sx>sy) return 1;
99 if (!sx) return 0;
100
101 lx=lgefint(x); ly=lgefint(y);
102 if (lx>ly) return sx;
103 if (lx<ly) return -sx;
104 if (sx>0)
105 return absi_cmp_lg(x, y, lx);
106 else
107 return -absi_cmp_lg(x, y, lx);
108 }
109
110 int
cmprr(GEN x,GEN y)111 cmprr(GEN x, GEN y)
112 {
113 const long sx = signe(x), sy = signe(y);
114 long ex,ey,lx,ly,lz,i;
115
116 if (sx<sy) return -1;
117 if (sx>sy) return 1;
118 if (!sx) return 0;
119
120 ex=expo(x); ey=expo(y);
121 if (ex>ey) return sx;
122 if (ex<ey) return -sx;
123
124 lx=lg(x); ly=lg(y); lz = (lx<ly)?lx:ly;
125 i=2; while (i<lz && x[i]==y[i]) i++;
126 if (i<lz) return ((ulong)x[i] > (ulong)y[i]) ? sx : -sx;
127 if (lx>=ly)
128 {
129 while (i<lx && !x[i]) i++;
130 return (i==lx) ? 0 : sx;
131 }
132 while (i<ly && !y[i]) i++;
133 return (i==ly) ? 0 : -sx;
134 }
135
136 /* x and y are integers. Return 1 if |x| == |y|, 0 otherwise */
137 int
absi_equal(GEN x,GEN y)138 absi_equal(GEN x, GEN y)
139 {
140 long lx;
141
142 if (!signe(x)) return !signe(y);
143 if (!signe(y)) return 0;
144
145 lx=lgefint(x); if (lx != lgefint(y)) return 0;
146 return absi_equal_lg(x, y, lx);
147 }
148
149 /* x and y are integers. Return sign(|x| - |y|) */
150 int
absi_cmp(GEN x,GEN y)151 absi_cmp(GEN x, GEN y)
152 {
153 long lx,ly;
154
155 if (!signe(x)) return signe(y)? -1: 0;
156 if (!signe(y)) return 1;
157
158 lx=lgefint(x); ly=lgefint(y);
159 if (lx>ly) return 1;
160 if (lx<ly) return -1;
161 return absi_cmp_lg(x, y, lx);
162 }
163
164 /* x and y are reals. Return sign(|x| - |y|) */
165 int
absr_cmp(GEN x,GEN y)166 absr_cmp(GEN x, GEN y)
167 {
168 long ex,ey,lx,ly,lz,i;
169
170 if (!signe(x)) return signe(y)? -1: 0;
171 if (!signe(y)) return 1;
172
173 ex=expo(x); ey=expo(y);
174 if (ex>ey) return 1;
175 if (ex<ey) return -1;
176
177 lx=lg(x); ly=lg(y); lz = (lx<ly)?lx:ly;
178 i=2; while (i<lz && x[i]==y[i]) i++;
179 if (i<lz) return ((ulong)x[i] > (ulong)y[i])? 1: -1;
180 if (lx>=ly)
181 {
182 while (i<lx && !x[i]) i++;
183 return (i==lx)? 0: 1;
184 }
185 while (i<ly && !y[i]) i++;
186 return (i==ly)? 0: -1;
187 }
188
189