]> www.ginac.de Git - cln.git/blob - include/cln/integer.h
Make @exec_prefix@ usable in shell scripts.
[cln.git] / include / cln / integer.h
1 // Public integer operations.
2
3 #ifndef _CL_INTEGER_H
4 #define _CL_INTEGER_H
5
6 #include "cln/number.h"
7 #include "cln/integer_class.h"
8 #include "cln/random.h"
9
10 namespace cln {
11
12 CL_DEFINE_AS_CONVERSION(cl_I)
13
14
15 // Konversion Integer >=0, <2^32 nach uintL.
16 // Wandelt Integer >=0 in Unsigned Longword um.
17 // cl_I_to_UL(obj)
18 // > obj: Integer, sollte >=0, <2^32 sein
19 // < ergebnis: der Wert des Integer als 32-Bit-Zahl.
20   extern uint32 cl_I_to_UL (const cl_I& obj);
21
22 // Konversion Integer >=-2^31, <2^31 nach sintL.
23 // Wandelt Integer in Signed Longword um.
24 // cl_I_to_L(obj)
25 // > obj: Integer, sollte >=-2^31, <2^31 sein
26 // < ergebnis: der Wert des Integer als 32-Bit-Zahl.
27   extern sint32 cl_I_to_L (const cl_I& obj);
28
29 // Convert an integer to a C `int' or `unsigned int'.
30 #if (int_bitsize==32)
31   inline int          cl_I_to_int  (const cl_I& x) { return cl_I_to_L(x);  }
32   inline unsigned int cl_I_to_uint (const cl_I& x) { return cl_I_to_UL(x); }
33 #endif
34
35 // Convert an integer to a C `long' or `unsigned long'.
36 #if (long_bitsize==32)
37   inline long          cl_I_to_long  (const cl_I& x) { return cl_I_to_L(x);  }
38   inline unsigned long cl_I_to_ulong (const cl_I& x) { return cl_I_to_UL(x); }
39 #elif (long_bitsize==64)
40   extern uint64 cl_I_to_UQ (const cl_I& obj);
41   extern sint64 cl_I_to_Q (const cl_I& obj);
42   inline long          cl_I_to_long  (const cl_I& x) { return cl_I_to_Q(x);  }
43   inline unsigned long cl_I_to_ulong (const cl_I& x) { return cl_I_to_UQ(x); }
44 #endif
45
46
47 // Logische Operationen auf Integers:
48
49 // (LOGIOR x y), wenn x, y Integers sind.
50 // Ergebnis Integer.
51 extern const cl_I logior (const cl_I& x, const cl_I& y);
52
53 // (LOGXOR x y), wenn x, y Integers sind.
54 // Ergebnis Integer.
55 extern const cl_I logxor (const cl_I& x, const cl_I& y);
56
57 // (LOGAND x y), wenn x, y Integers sind.
58 // Ergebnis Integer.
59 extern const cl_I logand (const cl_I& x, const cl_I& y);
60
61 // (LOGEQV x y), wenn x, y Integers sind.
62 // Ergebnis Integer.
63 extern const cl_I logeqv (const cl_I& x, const cl_I& y);
64
65 // (LOGNAND x y), wenn x, y Integers sind.
66 // Ergebnis Integer.
67 extern const cl_I lognand (const cl_I& x, const cl_I& y);
68
69 // (LOGNOR x y), wenn x, y Integers sind.
70 // Ergebnis Integer.
71 extern const cl_I lognor (const cl_I& x, const cl_I& y);
72
73 // (LOGANDC2 x y), wenn x, y Integers sind.
74 // Ergebnis Integer.
75 extern const cl_I logandc2 (const cl_I& x, const cl_I& y);
76
77 // (LOGANDC1 x y), wenn x, y Integers sind.
78 // Ergebnis Integer.
79 inline const cl_I logandc1 (const cl_I& x, const cl_I& y)
80 {
81         return logandc2(y,x);
82 }
83
84 // (LOGORC2 x y), wenn x, y Integers sind.
85 // Ergebnis Integer.
86 extern const cl_I logorc2 (const cl_I& x, const cl_I& y);
87
88 // (LOGORC1 x y), wenn x, y Integers sind.
89 // Ergebnis Integer.
90 inline const cl_I logorc1 (const cl_I& x, const cl_I& y)
91 {
92         return logorc2(y,x);
93 }
94
95 // (LOGNOT x), wenn x ein Integer sind.
96 // Ergebnis Integer.
97 extern const cl_I lognot (const cl_I& x);
98
99 // Konstanten für BOOLE:
100 typedef enum {
101         boole_clr,
102         boole_set,
103         boole_1,
104         boole_2,
105         boole_c1,
106         boole_c2,
107         boole_and,
108         boole_ior,
109         boole_xor,
110         boole_eqv,
111         boole_nand,
112         boole_nor,
113         boole_andc1,
114         boole_andc2,
115         boole_orc1,
116         boole_orc2
117 } cl_boole;
118
119 // (BOOLE op x y), wenn x und y Integers und op ein Objekt sind.
120 // Ergebnis Integer.
121 extern const cl_I boole (cl_boole op, const cl_I& x, const cl_I& y);
122
123 // Prüft, ob (LOGTEST x y), wo x und y Integers sind.
124 // (LOGTEST x y) = (NOT (ZEROP (LOGAND x y))).
125 // < ergebnis: /=0, falls ja; =0, falls nein.
126 extern cl_boolean logtest (const cl_I& x, const cl_I& y);
127
128 // Prüft, ob (LOGBITP x y), wo x und y Integers sind.
129 // Ergebnis: /=0, wenn ja; =0, wenn nein.
130 extern cl_boolean logbitp (uintL x, const cl_I& y);
131 extern cl_boolean logbitp (const cl_I& x, const cl_I& y);
132
133 // Prüft, ob (ODDP x), wo x ein Integer ist.
134 // Ergebnis: /=0, falls ja; =0, falls nein.
135 extern cl_boolean oddp (const cl_I& x);
136
137 // Prüft, ob (EVENP x), wo x ein Integer ist.
138 // Ergebnis: /=0, falls ja; =0, falls nein.
139 inline cl_boolean evenp (const cl_I& x)
140         { return (cl_boolean) (!oddp(x)); }
141
142 // (ASH x y), wo x und y Integers sind. Ergebnis Integer.
143 extern const cl_I ash (const cl_I& x, sintL y);
144 extern const cl_I ash (const cl_I& x, const cl_I& y);
145
146 // (LOGCOUNT x), wo x ein Integer ist. Ergebnis uintL.
147 extern uintL logcount (const cl_I& x);
148
149 // (INTEGER-LENGTH x), wo x ein Integer ist. Ergebnis uintL.
150 extern uintL integer_length (const cl_I& x);
151
152 // (ORD2 x) = max{n>=0: 2^n | x }, wo x ein Integer /=0 ist. Ergebnis uintL.
153 extern uintL ord2 (const cl_I& x);
154
155 // power2p(x) stellt fest, ob ein Integer x>0 eine Zweierpotenz ist.
156 // Ergebnis: n>0, wenn x=2^(n-1), 0 sonst.
157 extern uintL power2p (const cl_I& x);
158
159 inline const cl_I operator| (const cl_I& x, const cl_I& y)
160         { return logior(x,y); }
161 inline const cl_I operator^ (const cl_I& x, const cl_I& y)
162         { return logxor(x,y); }
163 inline const cl_I operator& (const cl_I& x, const cl_I& y)
164         { return logand(x,y); }
165 inline const cl_I operator~ (const cl_I& x)
166         { return lognot(x); }
167 #ifdef WANT_OBFUSCATING_OPERATORS
168 // This could be optimized to use in-place operations.
169 inline cl_I& operator|= (cl_I& x, const cl_I& y) { return x = x | y; }
170 inline cl_I& operator^= (cl_I& x, const cl_I& y) { return x = x ^ y; }
171 inline cl_I& operator&= (cl_I& x, const cl_I& y) { return x = x & y; }
172 #endif
173
174
175 // Addition/Subtraktion von Integers
176
177 // (1+ x), wo x ein Integer ist. Ergebnis Integer.
178 extern const cl_I plus1 (const cl_I& x);
179
180 // (1- x), wo x ein Integer ist. Ergebnis Integer.
181 extern const cl_I minus1 (const cl_I& x);
182
183 // (+ x y), wo x und y Integers sind. Ergebnis Integer.
184 extern const cl_I operator+ (const cl_I& x, const cl_I& y);
185 // Dem C++-Compiler muß man auch das Folgende sagen:
186 inline const cl_I operator+ (const int x, const cl_I& y)
187         { return cl_I(x) + y; }
188 inline const cl_I operator+ (const unsigned int x, const cl_I& y)
189         { return cl_I(x) + y; }
190 inline const cl_I operator+ (const long x, const cl_I& y)
191         { return cl_I(x) + y; }
192 inline const cl_I operator+ (const unsigned long x, const cl_I& y)
193         { return cl_I(x) + y; }
194 inline const cl_I operator+ (const cl_I& x, const int y)
195         { return x + cl_I(y); }
196 inline const cl_I operator+ (const cl_I& x, const unsigned int y)
197         { return x + cl_I(y); }
198 inline const cl_I operator+ (const cl_I& x, const long y)
199         { return x + cl_I(y); }
200 inline const cl_I operator+ (const cl_I& x, const unsigned long y)
201         { return x + cl_I(y); }
202
203 // (- x), wenn x ein Integer ist. Ergebnis Integer.
204 extern const cl_I operator- (const cl_I& x);
205
206 // (- x y), wo x und y Integers sind. Ergebnis Integer.
207 extern const cl_I operator- (const cl_I& x, const cl_I& y);
208 // Dem C++-Compiler muß man auch das Folgende sagen:
209 inline const cl_I operator- (const int x, const cl_I& y)
210         { return cl_I(x) - y; }
211 inline const cl_I operator- (const unsigned int x, const cl_I& y)
212         { return cl_I(x) - y; }
213 inline const cl_I operator- (const long x, const cl_I& y)
214         { return cl_I(x) - y; }
215 inline const cl_I operator- (const unsigned long x, const cl_I& y)
216         { return cl_I(x) - y; }
217 inline const cl_I operator- (const cl_I& x, const int y)
218         { return x - cl_I(y); }
219 inline const cl_I operator- (const cl_I& x, const unsigned int y)
220         { return x - cl_I(y); }
221 inline const cl_I operator- (const cl_I& x, const long y)
222         { return x - cl_I(y); }
223 inline const cl_I operator- (const cl_I& x, const unsigned long y)
224         { return x - cl_I(y); }
225
226 // (abs x), wenn x ein Integer ist. Ergebnis Integer.
227 extern const cl_I abs (const cl_I& x);
228
229 // Shifts.
230 inline const cl_I operator<< (const cl_I& x, sintL y) // assume 0 <= y < 2^31
231         { return ash(x,y); }
232 inline const cl_I operator<< (const cl_I& x, const cl_I& y) // assume y >= 0
233         { return ash(x,y); }
234 inline const cl_I operator>> (const cl_I& x, sintL y) // assume 0 <= y < 2^31
235         { return ash(x,-y); }
236 inline const cl_I operator>> (const cl_I& x, const cl_I& y) // assume y >= 0
237         { return ash(x,-y); }
238
239
240 // Vergleich von Integers
241
242 // equal(x,y) vergleicht zwei Integers x und y auf Gleichheit.
243 extern cl_boolean equal (const cl_I& x, const cl_I& y);
244 // equal_hashcode(x) liefert einen equal-invarianten Hashcode für x.
245 extern uint32 equal_hashcode (const cl_I& x);
246
247 // compare(x,y) vergleicht zwei Integers x und y.
248 // Ergebnis: 0 falls x=y, +1 falls x>y, -1 falls x<y.
249 extern cl_signean compare (const cl_I& x, const cl_I& y);
250
251 inline bool operator== (const cl_I& x, const cl_I& y)
252         { return equal(x,y); }
253 inline bool operator!= (const cl_I& x, const cl_I& y)
254         { return !equal(x,y); }
255 inline bool operator<= (const cl_I& x, const cl_I& y)
256         { return compare(x,y)<=0; }
257 inline bool operator< (const cl_I& x, const cl_I& y)
258         { return compare(x,y)<0; }
259 inline bool operator>= (const cl_I& x, const cl_I& y)
260         { return compare(x,y)>=0; }
261 inline bool operator> (const cl_I& x, const cl_I& y)
262         { return compare(x,y)>0; }
263
264 // minusp(x) == (< x 0)
265 extern cl_boolean minusp (const cl_I& x);
266
267 // plusp(x) == (> x 0)
268 extern cl_boolean plusp (const cl_I& x);
269
270 // zerop(x) stellt fest, ob ein Integer = 0 ist.
271 extern cl_boolean zerop (const cl_I& x);
272
273
274 // BYTE-Operationen auf Integers
275
276 struct cl_byte {
277         uintL size;
278         uintL position;
279 // Konstruktor:
280         cl_byte (unsigned int s, unsigned int p) : size (s), position (p) {}
281 };
282
283 // (LDB byte n), wo n ein Integer ist.
284 extern const cl_I ldb (const cl_I& n, const cl_byte& b);
285
286 // ldb_test(n,byte) führt (LDB-TEST byte n) aus, wobei n ein Integer ist.
287 // Ergebnis: cl_false wenn nein (also alle fraglichen Bits =0), cl_true wenn ja.
288 extern cl_boolean ldb_test (const cl_I& n, const cl_byte& b);
289
290 // (MASK-FIELD byte n), wo n ein Integer ist.
291 extern const cl_I mask_field (const cl_I& n, const cl_byte& b);
292
293 // (DEPOSIT-FIELD newbyte byte n), wo n und newbyte Integers sind.
294 extern const cl_I deposit_field (const cl_I& newbyte, const cl_I& n, const cl_byte& b);
295
296 // (DPB newbyte byte n), wo n und newbyte Integers sind.
297 extern const cl_I dpb (const cl_I& newbyte, const cl_I& n, const cl_byte& b);
298
299
300 // Multiplikation ganzer Zahlen
301
302 // (* x y), wo x und y Integers sind. Ergebnis Integer.
303 extern const cl_I operator* (const cl_I& x, const cl_I& y);
304 // Dem C++-Compiler muß man auch das Folgende sagen:
305 inline const cl_I operator* (const int x, const cl_I& y)
306         { return cl_I(x) * y; }
307 inline const cl_I operator* (const unsigned int x, const cl_I& y)
308         { return cl_I(x) * y; }
309 inline const cl_I operator* (const long x, const cl_I& y)
310         { return cl_I(x) * y; }
311 inline const cl_I operator* (const unsigned long x, const cl_I& y)
312         { return cl_I(x) * y; }
313 inline const cl_I operator* (const cl_I& x, const int y)
314         { return x * cl_I(y); }
315 inline const cl_I operator* (const cl_I& x, const unsigned int y)
316         { return x * cl_I(y); }
317 inline const cl_I operator* (const cl_I& x, const long y)
318         { return x * cl_I(y); }
319 inline const cl_I operator* (const cl_I& x, const unsigned long y)
320         { return x * cl_I(y); }
321
322 // (EXPT x 2), wo x Integer ist.
323 extern const cl_I square (const cl_I& x);
324
325 // (EXPT x y), wo x Integer, y Integer >0 ist.
326 extern const cl_I expt_pos (const cl_I& x, uintL y);
327 extern const cl_I expt_pos (const cl_I& x, const cl_I& y);
328
329 // Fakultät (! n), wo n Fixnum >=0 ist. Ergebnis Integer.
330 extern const cl_I factorial (uintL n);
331 //CL_REQUIRE(cl_I_factorial)
332
333 // Double factorial (!! n), with n Fixnum >=0.  Returns integer.
334 extern const cl_I doublefactorial (uintL n);
335
336 // Binomialkoeffizient (n \choose k) = n! / k! (n-k)!, wo n,k >= 0 sind.
337 extern const cl_I binomial (uintL n, uintL k);
338
339
340 // Division ganzer Zahlen
341
342 // Return type for division operators.
343 // x / y  --> (q,r) with x = y*q+r.
344 struct cl_I_div_t {
345         cl_I quotient;
346         cl_I remainder;
347 // Constructor.
348         cl_I_div_t () {}
349         cl_I_div_t (const cl_I& q, const cl_I& r) : quotient(q), remainder(r) {}
350 };
351
352 // Dividiert zwei Integers x,y >=0 und liefert den Quotienten x/y >=0.
353 // Bei y=0 Error. Die Division muß aufgehen, sonst Error.
354 // exquopos(x,y)
355 // > x,y: Integers >=0
356 // < ergebnis: Quotient x/y, ein Integer >=0
357   extern const cl_I exquopos (const cl_I& x, const cl_I& y);
358
359 // Dividiert zwei Integers x,y und liefert den Quotienten x/y.
360 // Bei y=0 Error. Die Division muß aufgehen, sonst Error.
361 // exquo(x,y)
362 // > x,y: Integers
363 // < ergebnis: Quotient x/y, ein Integer
364   extern const cl_I exquo (const cl_I& x, const cl_I& y);
365
366 // mod(x,y) = (mod x y), wo x,y Integers sind.
367   extern const cl_I mod (const cl_I& x, const cl_I& y);
368
369 // rem(x,y) = (rem x y), wo x,y Integers sind.
370   extern const cl_I rem (const cl_I& x, const cl_I& y);
371
372 // Dividiert zwei Integers x,y und liefert Quotient und Rest
373 // (q,r) := (floor x y)
374 // floor2(x,y)
375 // > x,y: Integers
376 // < q,r: Quotient q, Rest r
377   extern const cl_I_div_t floor2 (const cl_I& x, const cl_I& y);
378   extern const cl_I floor1 (const cl_I& x, const cl_I& y);
379
380 // Dividiert zwei Integers x,y und liefert Quotient und Rest
381 // (q,r) := (ceiling x y)
382 // ceiling2(x,y)
383 // > x,y: Integers
384 // < q,r: Quotient q, Rest r
385   extern const cl_I_div_t ceiling2 (const cl_I& x, const cl_I& y);
386   extern const cl_I ceiling1 (const cl_I& x, const cl_I& y);
387
388 // Dividiert zwei Integers x,y und liefert Quotient und Rest
389 // (q,r) := (truncate x y)
390 // truncate2(x,y)
391 // > x,y: Integers
392 // < q,r: Quotient q, Rest r
393   extern const cl_I_div_t truncate2 (const cl_I& x, const cl_I& y);
394   extern const cl_I truncate1 (const cl_I& x, const cl_I& y);
395
396 // Dividiert zwei Integers x,y und liefert Quotient und Rest
397 // (q,r) := (round x y)
398 // round2(x,y)
399 // > x,y: Integers
400 // < q,r: Quotient q, Rest r
401   extern const cl_I_div_t round2 (const cl_I& x, const cl_I& y);
402   extern const cl_I round1 (const cl_I& x, const cl_I& y);
403
404
405 // ggT und kgV von Integers
406
407 // Liefert den ggT zweier Integers.
408 // gcd(a,b)
409 // > a,b: zwei Integers
410 // < ergebnis: (gcd a b), ein Integer >=0
411   extern const cl_I gcd (const cl_I& a, const cl_I& b);
412   extern uint32 gcd (uint32 a, uint32 b);
413
414 // Liefert den ggT zweier Integers samt Beifaktoren.
415 // g = xgcd(a,b,&u,&v)
416 // > a,b: zwei Integers
417 // < u, v, g: Integers mit u*a+v*b = g >= 0
418   extern const cl_I xgcd (const cl_I& a, const cl_I& b, cl_I* u, cl_I* v);
419 // Im Fall A/=0, B/=0 genügt das Ergebnis (g,u,v) den Ungleichungen:
420 //   Falls |A| = |B| : g = |A|, u = (signum A), v = 0.
421 //   Falls |B| | |A|, |B| < |A| : g = |B|, u = 0, v = (signum B).
422 //   Falls |A| | |B|, |A| < |B| : g = |A|, u = (signum A), v = 0.
423 //   Sonst: |u| <= |B| / (2*g), |v| <= |A| / (2*g).
424 //   In jedem Fall |u| <= |B|/g, |v| < |A|/g.
425 // (Beweis: Im Prinzip macht man ja mehrere Euklid-Schritte auf einmal. Im
426 // letzten Fall - oBdA |A| > |B| - braucht man mindestens zwei Euklid-Schritte,
427 // also gilt im Euklid-Tableau
428 //                 i         |A|            |B|         Erg.
429 //                --------------------------------------------
430 //                 0          1              0          |A|
431 //                 1          0              1          |B|
432 //                ...        ...            ...         ...
433 //                n-1  -(-1)^n*x[n-1]  (-1)^n*y[n-1]   z[n-1]
434 //                 n    (-1)^n*x[n]    -(-1)^n*y[n]     z[n]
435 //                n+1  -(-1)^n*x[n+1]  (-1)^n*y[n+1]   z[n+1] = 0
436 //                --------------------------------------------
437 //                       g = z[n], |u|=x[n], |v|=y[n]
438 // n>=2, z[0] > ... > z[n-1] > z[n] = g, g | z[n-1], also z[n-1] >= 2*g.
439 // Da aber mit  (-1)^i*x[i]*|A| - (-1)^i*y[i]*|B| = z[i]  für i=0..n+1
440 // und            x[i]*y[i+1] - x[i+1]*y[i] = (-1)^i  für i=0..n,
441 //                x[i]*z[i+1] - x[i+1]*z[i] = (-1)^i*|B|  für i=0..n,
442 //                y[i]*z[i+1] - y[i+1]*z[i] = -(-1)^i*|A|  für i=0..n
443 // auch |A| = y[i+1]*z[i] + y[i]*z[i+1], |B| = x[i+1]*z[i] + x[i]*z[i+1]
444 // für i=0..n (Cramersche Regel), folgt
445 // |A| = y[n]*z[n-1] + y[n-1]*z[n] >= y[n]*2*g + 0 = |v|*2*g,
446 // |B| = x[n]*z[n-1] + x[n-1]*z[n] >= x[n]*2*g + 0 = |u|*2*g.)
447
448 // Liefert den kgV zweier Integers.
449 // lcm(a,b)
450 // > a,b: zwei Integers
451 // < ergebnis: (lcm a b), ein Integer >=0
452   extern const cl_I lcm (const cl_I& a, const cl_I& b);
453
454
455 // Wurzel aus ganzen Zahlen
456
457 // Zieht die Wurzel (ISQRT x) aus einem Integer.
458 // isqrt(x,&w)
459 // > x: Integer (sollte >=0 sein)
460 // < w: (isqrt x)
461 // < ergebnis: cl_true falls x Quadratzahl, cl_false sonst
462   extern cl_boolean isqrt (const cl_I& x, cl_I* w);
463 // Wenn das boolesche Ergebnis uninteressant ist:
464   inline const cl_I isqrt (const cl_I& x) { cl_I w; isqrt(x,&w); return w; }
465
466 // Stellt fest, ob ein Integer >=0 eine Quadratzahl ist.
467 // sqrtp(x,&w)
468 // > x: ein Integer >=0
469 // < w: Integer (sqrt x) falls x Quadratzahl
470 // < ergebnis: cl_true   ..................., cl_false sonst
471   extern cl_boolean sqrtp (const cl_I& x, cl_I* w);
472
473 // Stellt fest, ob ein Integer >=0 eine n-te Potenz ist.
474 // rootp(x,n,&w)
475 // > x: ein Integer >=0
476 // > n: ein Integer >0
477 // < w: Integer (expt x (/ n)) falls x eine n-te Potenz
478 // < ergebnis: cl_true         ........................, cl_false sonst
479   extern cl_boolean rootp (const cl_I& x, uintL n, cl_I* w);
480   extern cl_boolean rootp (const cl_I& x, const cl_I& n, cl_I* w);
481
482
483 // max(x,y) liefert (max x y), wo x und y ganze Zahlen sind.
484 extern const cl_I max (const cl_I& x, const cl_I& y);
485
486 // min(x,y) liefert (min x y), wo x und y ganze Zahlen sind.
487 extern const cl_I min (const cl_I& x, const cl_I& y);
488
489 // signum(x) liefert (signum x), wo x eine ganze Zahl ist.
490 extern const cl_I signum (const cl_I& x);
491
492
493 // Multipliziert ein Integer mit 10 und addiert eine weitere Ziffer.
494 // mul_10_plus_x(y,x)
495 // > y: Integer Y (>=0)
496 // > x: Ziffernwert X (>=0,<10)
497 // < ergebnis: Integer Y*10+X (>=0)
498 extern const cl_I mul_10_plus_x (const cl_I& y, unsigned char x);
499
500
501 // 2-adische Inverse.
502 // cl_recip2adic(n,x)
503 // > n: >0
504 // > x: Integer, ungerade
505 // < ergebnis: n-Bit-Zahl y == (x mod 2^n)^-1, d.h. y*x == 1 mod 2^n
506 extern const cl_I cl_recip2adic (uintL n, const cl_I& x);
507
508 // 2-adische Division.
509 // cl_div2adic(n,x,y)
510 // > n: >0
511 // > x: Integer
512 // > y: Integer, ungerade
513 // < ergebnis: n-Bit-Zahl z == (x mod 2^n)/(y mod 2^n), d.h. z*y == x mod 2^n
514 extern const cl_I cl_div2adic (uintL n, const cl_I& x, const cl_I& y);
515
516
517 // numerator(r) liefert den Zähler des Integer r.
518 inline const cl_I numerator (const cl_I& r)
519         { return r; }
520 // denominator(r) liefert den Nenner (> 0) des Integer r.
521 inline const cl_I denominator (const cl_I& r)
522         { (void)r; return 1; }
523
524
525 // Konversion zu einem C "float".
526 extern float float_approx (const cl_I& x);
527
528 // Konversion zu einem C "double".
529 extern double double_approx (const cl_I& x);
530
531
532 // random_I(randomstate,n) liefert zu einem Integer n>0 ein zufälliges
533 // Integer x mit 0 <= x < n.
534 // > randomstate: ein Random-State, wird verändert
535 extern const cl_I random_I (random_state& randomstate, const cl_I& n);
536
537 inline const cl_I random_I (const cl_I& n)
538         { return random_I(default_random_state,n); }
539
540 // testrandom_I(randomstate) liefert ein zufälliges Integer zum Testen.
541 // > randomstate: ein Random-State, wird verändert
542 extern const cl_I testrandom_I (random_state& randomstate);
543
544 inline const cl_I testrandom_I ()
545         { return testrandom_I(default_random_state); }
546
547
548 #ifdef WANT_OBFUSCATING_OPERATORS
549 // This could be optimized to use in-place operations.
550 inline cl_I& operator+= (cl_I& x, const cl_I& y) { return x = x + y; }
551 inline cl_I& operator+= (cl_I& x, const int y) { return x = x + y; }
552 inline cl_I& operator+= (cl_I& x, const unsigned int y) { return x = x + y; }
553 inline cl_I& operator+= (cl_I& x, const long y) { return x = x + y; }
554 inline cl_I& operator+= (cl_I& x, const unsigned long y) { return x = x + y; }
555 inline cl_I& operator++ /* prefix */ (cl_I& x) { return x = plus1(x); }
556 inline void operator++ /* postfix */ (cl_I& x, int dummy) { (void)dummy; x = plus1(x); }
557 inline cl_I& operator-= (cl_I& x, const cl_I& y) { return x = x - y; }
558 inline cl_I& operator-= (cl_I& x, const int y) { return x = x - y; }
559 inline cl_I& operator-= (cl_I& x, const unsigned int y) { return x = x - y; }
560 inline cl_I& operator-= (cl_I& x, const long y) { return x = x - y; }
561 inline cl_I& operator-= (cl_I& x, const unsigned long y) { return x = x - y; }
562 inline cl_I& operator-- /* prefix */ (cl_I& x) { return x = minus1(x); }
563 inline void operator-- /* postfix */ (cl_I& x, int dummy) { (void)dummy; x = minus1(x); }
564 inline cl_I& operator*= (cl_I& x, const cl_I& y) { return x = x * y; }
565 inline cl_I& operator<<= (cl_I& x, sintL y) // assume 0 <= y < 2^31
566         { return x = x << y; }
567 inline cl_I& operator<<= (cl_I& x, const cl_I& y) // assume y >= 0
568         { return x = x << y; }
569 inline cl_I& operator>>= (cl_I& x, sintL y) // assume 0 <= y < 2^31
570         { return x = x >> y; }
571 inline cl_I& operator>>= (cl_I& x, const cl_I& y) // assume y >= 0
572         { return x = x >> y; }
573 #if 0 // Defining operator/ collides with the operator/ (cl_RA, cl_RA).
574 // operator/ should perform exquo(x,y), but people believe in the C semantics.
575 // And it would be wiser to use floor1 and mod instead of truncate1 and rem,
576 // but again, many C compilers implement / and % like this and people believe
577 // in it.
578 inline const cl_I operator/ (const cl_I& x, const cl_I& y) { return truncate1(x,y); }
579 inline const cl_I operator% (const cl_I& x, const cl_I& y) { return rem(x,y); }
580 inline cl_I& operator/= (cl_I& x, const cl_I& y) { return x = x / y; }
581 inline cl_I& operator%= (cl_I& x, const cl_I& y) { return x = x % y; }
582 #endif
583 #endif
584
585
586 // Runtime typing support.
587 extern cl_class cl_class_fixnum;
588 extern cl_class cl_class_bignum;
589 static const void* const cl_I_classes_dummy[] = { &cl_I_classes_dummy,
590         &cl_class_fixnum
591 };
592
593
594 // Debugging support.
595 #ifdef CL_DEBUG
596 extern int cl_I_debug_module;
597 static void* const cl_I_debug_dummy[] = { &cl_I_debug_dummy,
598         &cl_I_debug_module
599 };
600 #endif
601
602 }  // namespace cln
603
604 #endif /* _CL_INTEGER_H */