1 // logand().
2 
3 // General includes.
4 #include "base/cl_sysdep.h"
5 
6 // Specification.
7 #include "cln/integer.h"
8 
9 
10 // Implementation.
11 
12 #include "integer/cl_I.h"
13 #include "base/digitseq/cl_DS.h"
14 #include "integer/bitwise/cl_I_log.h"
15 
16 namespace cln {
17 
18 // Logische Operationen auf Integers:
19 // Methode: aus den Längen der beiden Argumente eine obere Schranke für
20 // die Länge des Ergebnisses berechnen (das Maximum der beiden Längen und
21 // FN_maxlength), so daß das MSD für unendlich viele Bits steht.
22 // Dann beide Argumente in gleichgroße Digit sequences umwandeln, Operation
23 // mit einer einfachen Schleife durchführen.
24 
logand(const cl_I & x,const cl_I & y)25 const cl_I logand (const cl_I& x, const cl_I& y)
26     { if (fixnump(x) && fixnump(y)) // Beides Fixnums -> ganz einfach:
27         { // bitweise als Fixnum zurück
28           return cl_I_from_word(x.word & y.word);
29         }
30       if (fixnump(x))
31         { DeclareType(cl_FN,x);
32           if (!minusp(x))
33             // PosFixnum AND Bignum -> PosFixnum
34             { return cl_I_from_word(x.word & cl_combine(cl_FN_tag,pFN_maxlength_digits_at(BN_LSDptr(y)))); }
35         }
36       if (fixnump(y))
37         { DeclareType(cl_FN,y);
38           if (!minusp(y))
39             // Bignum AND PosFixnum -> PosFixnum
40             { return cl_I_from_word(cl_combine(cl_FN_tag,pFN_maxlength_digits_at(BN_LSDptr(x))) & y.word); }
41         }
42       { CL_ALLOCA_STACK;
43         var uintC n; // Anzahl der Digits
44        {var uintC nx = I_to_DS_need(x);
45         var uintC ny = I_to_DS_need(y);
46         n = (nx>=ny ? nx : ny);
47        }
48        {var uintD* xptr; I_to_DS_n(x,n,xptr=); // Pointer in DS zu x
49         var uintD* yptr; I_to_DS_n(y,n,yptr=); // Pointer in DS zu y
50         var uintD* zptr = xptr; // Pointer aufs Ergebnis
51         and_loop_msp(xptr,yptr,n); // mit AND verknüpfen
52         return DS_to_I(zptr,n); // Ergebnis als Integer
53     } }}
54 
55 }  // namespace cln
56