]> www.ginac.de Git - cln.git/blob - src/integer/elem/cl_I_compare.cc
* All Files have been modified for inclusion of namespace cln;
[cln.git] / src / integer / elem / cl_I_compare.cc
1 // compare().
2
3 // General includes.
4 #include "cl_sysdep.h"
5
6 // Specification.
7 #include "cln/integer.h"
8
9
10 // Implementation.
11
12 #include "cl_I.h"
13 #include "cl_DS.h"
14
15 namespace cln {
16
17 cl_signean compare (const cl_I& x, const cl_I& y)
18 {
19       // Methode:
20       // x und y haben verschiedenes Vorzeichen ->
21       //    x < 0 -> x < y
22       //    x >= 0 -> x > y
23       // x und y haben gleiches Vorzeichen ->
24       // x Fixnum ->
25       //    y Fixnum -> direkt vergleichen.
26       //    y Bignum ->
27       //       y > 0 -> x < y
28       //       y < 0 -> x > y
29       // x Bignum ->
30       //    y Fixnum ->
31       //       x < 0 -> x < y
32       //       x > 0 -> x > y
33       //    y Bignum ->
34       //       falls beide gleich lang -> wortweise vergleichen
35       //       x kürzer als y -> bei x,y > 0 : x < y, bei x,y < 0 : x > y
36       //       y kürzer als x -> bei x,y > 0 : x > y, bei x,y > 0 : x < y
37       var uintC xlen;
38       var uintC ylen;
39       if (fixnump(x))
40         // x Fixnum
41         if (fixnump(y))
42           // x Fixnum, y Fixnum
43           { // This assumes cl_value_shift + cl_value_len == cl_pointer_size.
44             if ((cl_sint)x.word == (cl_sint)y.word) return signean_null;
45             else if ((cl_sint)x.word > (cl_sint)y.word) return signean_plus;
46             else return signean_minus;
47           }
48           else
49           // x Fixnum, y Bignum
50           if ((sintD)mspref(BN_MSDptr(y),0) >= 0)
51             // x Fixnum, y Bignum >0
52             return signean_minus; // x<y
53             else
54             // x Fixnum, y Bignum <0
55             return signean_plus; // x>y
56         else
57         // x Bignum
58         if (fixnump(y))
59           // x Bignum, y Fixnum
60           if ((sintD)mspref(BN_MSDptr(x),0) >= 0)
61             // x Bignum >0, y Fixnum
62             return signean_plus; // x>y
63             else
64             // x Bignum <0, y Fixnum
65             return signean_minus; // x<y
66           else
67           // x Bignum, y Bignum
68           if ((sintD)mspref(BN_MSDptr(x),0) >= 0)
69             // x Bignum >0, y Bignum
70             if ((sintD)mspref(BN_MSDptr(y),0) >= 0)
71               // x und y Bignums >0
72               if (x.pointer == y.pointer)
73                 return signean_null; // gleiche Pointer -> selbe Zahl
74                 else
75                 { xlen = TheBignum(x)->length;
76                   ylen = TheBignum(y)->length;
77                   if (xlen==ylen)
78                     samelength:
79                     // gleiche Länge -> digitweise vergleichen
80                     return compare_loop_msp(BN_MSDptr(x),BN_MSDptr(y),xlen);
81                     else
82                     return (xlen > ylen ? signean_plus : signean_minus);
83                 }
84               else
85               // x Bignum >0, y Bignum <0
86               return signean_plus; // x>y
87             else
88             // x Bignum <0, y Bignum
89             if ((sintD)mspref(BN_MSDptr(y),0) >= 0)
90               // x Bignum <0, y Bignum >0
91               return signean_minus; // x<y
92               else
93               // x und y Bignums <0
94               if (x.pointer == y.pointer)
95                 return signean_null; // gleiche Pointer -> selbe Zahl
96                 else
97                 { xlen = TheBignum(x)->length;
98                   ylen = TheBignum(y)->length;
99                   if (xlen==ylen)
100                     // gleiche Länge -> wortweise vergleichen
101                     goto samelength; // wie oben
102                     else
103                     return (xlen > ylen ? signean_minus : signean_plus);
104                 }
105 }
106
107 }  // namespace cln