1 // Low-level arithmetic: operations on 16-bit and 32-bit words
8 // Determines the sign of a 16-bit number.
10 // > wert: eine 16-Bit-Zahl
11 // < sint16 ergebnis: 0 falls wert>=0, -1 falls wert<0.
12 inline sint16 sign_of (sint16 wert)
14 #if defined(__sparc64__)
15 return (sint64)wert >> 63;
16 #elif defined(__sparc__) || defined(__arm__)
17 return (sint32)wert >> 31;
19 return (wert >= 0 ? 0 : -1);
23 // Determines the sign of a 32-bit number.
25 // > wert: eine 32-Bit-Zahl
26 // < sint32 ergebnis: 0 falls wert>=0, -1 falls wert<0.
27 inline sint32 sign_of (sint32 wert)
29 #if defined(__sparc64__)
30 return (sint64)wert >> 63;
31 #elif defined(__sparc__) || defined(__arm__)
34 return (wert >= 0 ? 0 : -1);
38 #ifdef HAVE_FAST_LONGLONG
40 // Determines the sign of a 64-bit number.
42 // > wert: eine 64-Bit-Zahl
43 // < sint64 ergebnis: 0 falls wert>=0, -1 falls wert<0.
44 inline sint64 sign_of (sint64 wert)
49 #endif /* HAVE_FAST_LONGLONG */
52 // High-Word einer 32-Bit-Zahl bestimmen
54 inline uint16 high16 (uint32 wert)
59 // Low-Word einer 32-Bit-Zahl bestimmen
61 inline uint16 low16 (uint32 wert)
66 // Eine 32-Bit-Zahl aus ihrem High-Word und ihrem Low-Word bestimmen:
67 // highlow32(uint16 high, uint16 low)
68 inline uint32 highlow32 (uint16 high, uint16 low)
70 return ((uint32)high << 16) | (uint32)low;
73 // Eine 32-Bit-Zahl aus ihrem High-Word und ihrem Low-Word 0 bestimmen:
74 // highlow32_0(uint16 high)
75 inline uint32 highlow32_0 (uint16 high)
77 return (uint32)high << 16;
82 // High-Word einer 64-Bit-Zahl bestimmen
84 inline uint32 high32 (uint64 wert)
89 // Low-Word einer 64-Bit-Zahl bestimmen
91 inline uint32 low32 (uint64 wert)
96 // Eine 64-Bit-Zahl aus ihrem High-Word und ihrem Low-Word bestimmen:
97 // highlow64(uint32 high, uint32 low)
98 inline uint64 highlow64 (uint32 high, uint32 low)
100 return ((uint64)high << 32) | (uint64)low;
103 // Eine 64-Bit-Zahl aus ihrem High-Word und ihrem Low-Word 0 bestimmen:
104 // highlow64_0(uint32 high)
105 inline uint64 highlow64_0 (uint32 high)
107 return (uint64)high << 32;
110 #endif /* HAVE_LONGLONG */
113 // Multipliziert zwei 16-Bit-Zahlen miteinander und liefert eine 32-Bit-Zahl:
115 // > arg1, arg2 : zwei 16-Bit-Zahlen
116 // < ergebnis: eine 32-Bit-Zahl
117 #if defined(__GNUC__) && defined(__sparc__) && !defined(__sparc64__) && defined(FAST_DOUBLE)
118 // Ist das schneller als mulu16_ ??
119 inline uint32 mulu16 (uint16 arg1, uint16 arg2)
121 union { double f; uint32 i[2]; } __fi;
122 __fi.f = (double)(sint32)arg1 * (double)(sint32)arg2
123 + (double)(4503599627370496.0L); // + 2^52, zum Normalisieren
124 return __fi.i[1]; // untere 32 Bit herausholen (benutzt CL_CPU_BIG_ENDIAN_P !)
126 #elif defined(__GNUC__) && defined(__sparc64__) && !defined(NO_ASM)
127 inline uint32 mulu16 (uint16 arg1, uint16 arg2)
129 register uint64 _prod;
130 __asm__("umul %1,%2,%0"
132 : "r" (arg1), "r" (arg2)
136 #elif defined(__GNUC__) && (defined(__i386__) || defined(__x86_64__)) && !defined(NO_ASM)
137 inline uint32 mulu16 (uint16 arg1, uint16 arg2)
142 : "=d" /* %dx */ (_hi), "=a" /* %ax */ (_lo)
143 : "rm" (arg1), "1" /* %eax */ (arg2)
145 return highlow32(_hi,_lo);
147 #elif (defined(__sparc__) || defined(__sparc64__)) && !defined(NO_ASM)
148 extern "C" uint32 mulu16_ (uint16 arg1, uint16 arg2);
149 #define mulu16 mulu16_ // extern in Assembler
151 inline uint32 mulu16 (uint16 arg1, uint16 arg2)
157 // Multipliziert zwei 24-Bit-Zahlen zusammen und liefert eine 48-Bit-Zahl.
158 // mulu24(arg1,arg2,hi=,lo=);
159 // > arg1, arg2 : zwei 24-Bit-Zahlen
160 // < 2^32*hi+lo : eine 48-Bit-Zahl
161 #if defined(__sparc__) && !defined(__sparc64__) && defined(FAST_DOUBLE)
162 #define mulu24(x,y,hi_zuweisung,lo_zuweisung) \
163 { var uint32 _x = (x); \
164 var uint32 _y = (y); \
165 var union { double f; uint32 i[2]; uint16 s[4]; } __fi; \
166 __fi.f = (double)(sint32)(_x)*(double)(sint32)(_y) \
167 + (double)(4503599627370496.0L); /* + 2^52, zum Normalisieren */\
168 unused (hi_zuweisung __fi.s[1]); /* mittlere 16 Bit herausholen, (benutzt CL_CPU_BIG_ENDIAN_P !) */\
169 lo_zuweisung __fi.i[1]; /* untere 32 Bit herausholen (benutzt CL_CPU_BIG_ENDIAN_P !) */\
172 #define mulu24 mulu32
175 // Multipliziert zwei 32-Bit-Zahlen miteinander und liefert eine 32-Bit-Zahl:
176 // mulu32_unchecked(arg1,arg2)
177 // > arg1, arg2 : zwei 32-Bit-Zahlen
178 // < ergebnis : eine 32-Bit-Zahl
179 // Es wird vorausgesetzt, daß arg1*arg2 < 2^32.
180 #if defined(__GNUC__) && defined(__sparc64__) && !defined(NO_ASM)
181 inline uint32 mulu32_unchecked (uint32 arg1, uint32 arg2)
183 register uint64 _prod;
184 __asm__("umul %1,%2,%0"
186 : "r" (arg1), "r" (arg2)
190 #elif defined(__sparc__) && !defined(NO_ASM)
191 extern "C" uint32 mulu32_unchecked (uint32 x, uint32 y); // extern in Assembler
193 // Wir können dafür auch die Bibliotheksroutine des C-Compilers nehmen:
194 inline uint32 mulu32_unchecked (uint32 arg1, uint32 arg2)
200 // Multipliziert zwei 32-Bit-Zahlen miteinander und liefert eine 64-Bit-Zahl:
201 // mulu32(arg1,arg2,hi=,lo=);
202 // > arg1, arg2 : zwei 32-Bit-Zahlen
203 // < 2^32*hi+lo : eine 64-Bit-Zahl
204 extern "C" uint32 mulu32_ (uint32 arg1, uint32 arg2); // -> Low-Teil
206 // Workaround MSVC compiler bug: extern "C" results in wrong symbols, when
207 // declared inside a namespace!
208 } extern "C" uint32 mulu32_high; namespace cln { // -> High-Teil
210 extern "C" uint32 mulu32_high; // -> High-Teil
212 #if defined(__GNUC__) && defined(__m68k__) && !defined(NO_ASM)
213 #define mulu32(x,y,hi_zuweisung,lo_zuweisung) \
214 ({ var uint32 _x = (x); \
215 var uint32 _y = (y); \
218 __asm__("mulul %3,%0:%1" : "=d" (_hi), "=d"(_lo) : "1" (_x), "dm" (_y) ); \
219 unused (hi_zuweisung _hi); \
222 #elif defined(__GNUC__) && defined(__m68k__)
223 #define mulu32(x,y,hi_zuweisung,lo_zuweisung) \
224 ({ var uint32 _x = (x); \
225 var uint32 _y = (y); \
226 var uint16 _x1 = high16(_x); \
227 var uint16 _x0 = low16(_x); \
228 var uint16 _y1 = high16(_y); \
229 var uint16 _y0 = low16(_y); \
230 var uint32 _hi = mulu16(_x1,_y1); /* obere Portion */ \
231 var uint32 _lo = mulu16(_x0,_y0); /* untere Portion */ \
232 {var uint32 _mid = mulu16(_x0,_y1); /* 1. mittlere Portion */ \
233 _hi += high16(_mid); _mid = highlow32_0(low16(_mid)); \
234 _lo += _mid; if (_lo < _mid) { _hi += 1; } /* 64-Bit-Addition */\
236 {var uint32 _mid = mulu16(_x1,_y0); /* 2. mittlere Portion */ \
237 _hi += high16(_mid); _mid = highlow32_0(low16(_mid)); \
238 _lo += _mid; if (_lo < _mid) { _hi += 1; } /* 64-Bit-Addition */\
240 unused (hi_zuweisung _hi); \
243 #elif defined(__GNUC__) && defined(__sparc64__) && !defined(NO_ASM)
244 #define mulu32(x,y,hi_zuweisung,lo_zuweisung) \
245 ({ var register uint64 _prod; \
246 __asm__("umul %1,%2,%0" \
248 : "r" ((uint32)(x)), "r" ((uint32)(y)) \
250 unused (hi_zuweisung (uint32)(_prod>>32)); \
251 lo_zuweisung (uint32)(_prod); \
253 #elif defined(__GNUC__) && defined(__sparc__) && !defined(NO_ASM)
254 #define mulu32(x,y,hi_zuweisung,lo_zuweisung) \
255 ({ lo_zuweisung mulu32_(x,y); /* extern in Assembler */ \
256 {var register uint32 _hi __asm__("%g1"); \
257 unused (hi_zuweisung _hi); \
259 #elif defined(__GNUC__) && defined(__arm__) && 0 // see comment cl_asm_arm.cc
260 #define mulu32(x,y,hi_zuweisung,lo_zuweisung) \
261 ({ lo_zuweisung mulu32_(x,y); /* extern in Assembler */ \
262 {var register uint32 _hi __asm__("%r1"/*"%a2"*/); \
263 unused (hi_zuweisung _hi); \
265 #elif defined(__GNUC__) && (defined(__i386__) || defined(__x86_64__)) && !defined(NO_ASM)
266 #define mulu32(x,y,hi_zuweisung,lo_zuweisung) \
267 ({ var register uint32 _hi; \
268 var register uint32 _lo; \
270 : "=d" /* %edx */ (_hi), "=a" /* %eax */ (_lo) \
271 : "g" ((uint32)(x)), "1" /* %eax */ ((uint32)(y)) \
273 unused (hi_zuweisung _hi); lo_zuweisung _lo; \
275 #elif defined(__GNUC__) && defined(__mips__) && !defined(NO_ASM)
276 #define mulu32(x,y,hi_zuweisung,lo_zuweisung) \
277 ({ var register uint32 _hi; \
278 var register uint32 _lo; \
279 __asm__("multu %3,%2 ; mfhi %0 ; mflo %1" \
280 : "=r" (_hi), "=r" (_lo) \
281 : "r" ((uint32)(x)), "r" ((uint32)(y)) \
283 unused (hi_zuweisung _hi); lo_zuweisung _lo; \
285 #elif defined(__GNUC__) && defined(HAVE_LONGLONG) && !defined(__arm__)
286 #define mulu32(x,y,hi_zuweisung,lo_zuweisung) \
287 ({ var register uint64 _prod = (uint64)(uint32)(x) * (uint64)(uint32)(y); \
288 unused (hi_zuweisung (uint32)(_prod>>32)); \
289 lo_zuweisung (uint32)(_prod); \
291 #elif defined(WATCOM) && defined(__i386__) && !defined(NO_ASM)
292 #define mulu32(x,y,hi_zuweisung,lo_zuweisung) \
293 { var register uint32 _hi; \
294 var register uint32 _lo; \
295 _lo = mulu32_(x,y), _hi = mulu32_high_(); \
296 unused (hi_zuweisung _hi); lo_zuweisung _lo; \
298 extern "C" uint32 mulu32_high_ (void);
299 #pragma aux mulu32_ = 0xF7 0xE2 /* mull %edx */ parm [eax] [edx] value [eax] modify [eax edx];
300 #pragma aux mulu32_high_ = /* */ value [edx] modify [];
302 #define mulu32(x,y,hi_zuweisung,lo_zuweisung) \
303 { lo_zuweisung mulu32_(x,y); unused (hi_zuweisung mulu32_high); }
304 #if (defined(__m68k__) || defined(__sparc__) || defined(__sparc64__) || defined(__arm__) || (defined(__i386__) && !defined(WATCOM) && !defined(MICROSOFT)) || defined(__x86_64__) || defined(__mips__) || defined(__hppa__)) && !defined(NO_ASM)
305 // mulu32_ extern in Assembler
306 #if defined(__sparc__) || defined(__sparc64__)
307 extern "C" uint32 _get_g1 (void);
308 #define mulu32_high (_get_g1()) // Rückgabe im Register %g1
309 #elif !defined(__hppa__)
310 #define NEED_VAR_mulu32_high
313 #define NEED_FUNCTION_mulu32_
317 #ifdef HAVE_FAST_LONGLONG
319 // Multipliziert zwei 32-Bit-Zahlen miteinander und liefert eine 64-Bit-Zahl:
320 // mulu32_w(arg1,arg2)
321 // > arg1, arg2 : zwei 32-Bit-Zahlen
322 // < result : eine 64-Bit-Zahl
323 #if defined(__GNUC__) && defined(__sparc64__) && !defined(NO_ASM)
324 // Prefer the umul instruction over the mulx instruction (overkill).
325 #define mulu32_w(x,y) \
326 ({ var register uint64 _prod; \
327 __asm__("umul %1,%2,%0" \
329 : "r" ((uint32)(x)), "r" ((uint32)(y)) \
333 #elif defined(__GNUC__)
334 #define mulu32_w(x,y) ((uint64)(uint32)(x) * (uint64)(uint32)(y))
336 extern "C" uint64 mulu32_w (uint32 arg1, uint32 arg2);
337 #define NEED_FUNCTION_mulu32_w
340 // Multipliziert zwei 64-Bit-Zahlen miteinander und liefert eine 128-Bit-Zahl:
341 // mulu64(arg1,arg2,hi=,lo=);
342 // > arg1, arg2 : zwei 64-Bit-Zahlen
343 // < 2^64*hi+lo : eine 128-Bit-Zahl
344 extern "C" uint64 mulu64_ (uint64 arg1, uint64 arg2); // -> Low-Teil
346 // Workaround MSVC compiler bug.
347 } extern "C" uint64 mulu64_high; namespace cln { // -> High-Teil
349 extern "C" uint64 mulu64_high; // -> High-Teil
351 #if defined(__GNUC__) && defined(__alpha__) && !defined(NO_ASM)
352 #define mulu64(x,y,hi_zuweisung,lo_zuweisung) \
353 ({ var register uint64 _x = (x); \
354 var register uint64 _y = (y); \
355 var register uint64 _hi; \
356 var register uint64 _lo; \
357 __asm__("mulq %1,%2,%0" \
359 : "r" (_x), "r" (_y) \
361 __asm__("umulh %1,%2,%0" \
363 : "r" (_x), "r" (_y) \
368 #elif defined(__GNUC__) && defined(__sparc64__) && !defined(NO_ASM)
369 #define mulu64(x,y,hi_zuweisung,lo_zuweisung) \
370 ({ lo_zuweisung mulu64_(x,y); /* extern in Assembler */ \
371 {var register uint64 _hi __asm__("%g2"); \
374 #elif defined(__GNUC__) && defined(__x86_64__) && !defined(NO_ASM)
375 #define mulu64(x,y,hi_zuweisung,lo_zuweisung) \
376 ({ var register uint64 _hi; \
377 var register uint64 _lo; \
379 : "=d" /* %rdx */ (_hi), "=a" /* %rax */ (_lo) \
380 : "rm" ((uint64)(x)), "1" /* %rax */ ((uint64)(y)) \
382 hi_zuweisung _hi; lo_zuweisung _lo; \
384 #elif defined(__GNUC__) && defined(__ia64__) && !defined(NO_ASM)
385 #define mulu64(x,y,hi_zuweisung,lo_zuweisung) \
386 ({ var register uint64 _x = (x); \
387 var register uint64 _y = (y); \
388 var register uint64 _hi; \
389 __asm__("xma.hu %0 = %1, %2, f0" \
391 : "f" ((uint64)(_x)), "f" ((uint64)(_y)) \
393 hi_zuweisung _hi; lo_zuweisung ((uint64)(_x)*(uint64)(_y));\
396 #define mulu64(x,y,hi_zuweisung,lo_zuweisung) \
397 { lo_zuweisung mulu64_(x,y); hi_zuweisung mulu64_high; }
398 #if defined(__sparc64__) && !defined(NO_ASM)
399 // mulu64_ extern in Assembler
400 extern "C" uint64 _get_g2 (void);
401 #define mulu64_high (_get_g2()) // Rückgabe im Register %g2
403 #define NEED_FUNCTION_mulu64_
407 #endif /* HAVE_FAST_LONGLONG */
410 // Dividiert eine 16-Bit-Zahl durch eine 16-Bit-Zahl und
411 // liefert einen 16-Bit-Quotienten und einen 16-Bit-Rest.
412 // divu_1616_1616(x,y,q=,r=);
413 // > uint16 x: Zähler
414 // > uint16 y: Nenner
415 // < uint16 q: floor(x/y)
416 // < uint16 r: x mod y
418 #define divu_1616_1616(x,y,q_zuweisung,r_zuweisung) \
419 { var uint16 __x = (x); \
420 var uint16 __y = (y); \
421 q_zuweisung floor(__x,__y); \
422 r_zuweisung (__x % __y); \
425 // Dividiert eine 32-Bit-Zahl durch eine 16-Bit-Zahl und
426 // liefert einen 16-Bit-Quotienten und einen 16-Bit-Rest.
427 // divu_3216_1616(x,y,q=,r=);
428 // > uint32 x: Zähler
429 // > uint16 y: Nenner
430 // > Es sei bekannt, daß 0 <= x < 2^16*y .
431 // < uint16 q: floor(x/y)
432 // < uint16 r: x mod y
434 #if defined(__sparc__)
435 extern "C" uint32 divu_3216_1616_ (uint32 x, uint16 y); // -> Quotient q, Rest r
437 extern "C" uint16 divu_3216_1616_ (uint32 x, uint16 y); // -> Quotient q
439 // Workaround MSVC compiler bug.
440 } extern "C" uint16 divu_16_rest; namespace cln { // -> Rest r
442 extern "C" uint16 divu_16_rest; // -> Rest r
445 #if defined(__GNUC__) && defined(__sparc64__) && !defined(NO_ASM)
446 #define divu_3216_1616(x,y,q_zuweisung,r_zuweisung) \
447 ({var uint32 __x = (x); \
448 var uint16 __y = (y); \
451 __asm__ __volatile__ ( \
452 "wr %%g0,%%g0,%%y\n\t" \
453 "udiv %2,%3,%0\n\t" \
454 "umul %0,%3,%1\n\t" \
456 : "=&r" (__q), "=&r" (__r) \
457 : "r" (__x), "r" (__y)); \
458 q_zuweisung (uint16)__q; \
459 r_zuweisung (uint16)__r; \
461 #elif defined(__GNUC__) && (defined(__sparc__) || defined(__sparc64__)) && !defined(NO_ASM)
462 #define divu_3216_1616(x,y,q_zuweisung,r_zuweisung) \
463 ({ var uint32 __qr = divu_3216_1616_(x,y); /* extern in Assembler */\
464 q_zuweisung low16(__qr); \
465 r_zuweisung high16(__qr); \
467 #elif defined(__GNUC__) && defined(__m68k__) && !defined(NO_ASM)
468 #define divu_3216_1616(x,y,q_zuweisung,r_zuweisung) \
469 ({var uint32 __x = (x); \
470 var uint16 __y = (y); \
472 __asm__ __volatile__ (" \
474 " : "=d" (__qr) : "0" (__x), "dm" (__y)); \
475 q_zuweisung low16(__qr); \
476 r_zuweisung high16(__qr); \
478 #elif defined(__GNUC__) && (defined(__i386__) || defined(__x86_64__)) && !defined(NO_ASM)
479 #define divu_3216_1616(x,y,q_zuweisung,r_zuweisung) \
480 ({var uint32 __x = (x); \
481 var uint16 __y = (y); \
485 : "=a" /* %ax */ (__q), "=d" /* %dx */ (__r) \
486 : "1" /* %dx */ ((uint16)(high16(__x))), "0" /* %ax */ ((uint16)(low16(__x))), "rm" (__y) \
491 #elif defined(__GNUC__) && defined(__arm__) && 0 // see comment cl_asm_arm.cc
492 #define divu_3216_1616(x,y,q_zuweisung,r_zuweisung) \
493 { var uint32 __q = divu_3216_1616_(x,y); /* extern in Assembler */ \
494 var register uint32 __r __asm__("%r1"/*"%a2"*/); \
495 q_zuweisung __q; r_zuweisung __r; \
497 #elif defined(__GNUC__) && !defined(__arm__)
498 #define divu_3216_1616(x,y,q_zuweisung,r_zuweisung) \
499 ({var uint32 __x = (x); \
500 var uint16 __y = (y); \
501 var uint16 __q = floor(__x,__y); \
503 r_zuweisung (__x - __q * __y); \
505 #elif (defined(__sparc__) || defined(__sparc64__)) && !defined(NO_ASM)
506 #define divu_3216_1616(x,y,q_zuweisung,r_zuweisung) \
507 { var uint32 __qr = divu_3216_1616_(x,y); /* extern in Assembler */ \
508 q_zuweisung low16(__qr); \
509 r_zuweisung high16(__qr); \
511 #elif defined(__arm__) && !defined(NO_ASM)
512 #define divu_3216_1616(x,y,q_zuweisung,r_zuweisung) \
513 { q_zuweisung divu_3216_1616_(x,y); /* extern in Assembler */ \
514 r_zuweisung divu_16_rest; \
516 #define NEED_VAR_divu_16_rest
518 #define divu_3216_1616(x,y,q_zuweisung,r_zuweisung) \
519 { q_zuweisung divu_3216_1616_(x,y); r_zuweisung divu_16_rest; }
520 #define NEED_FUNCTION_divu_3216_1616_
523 // Dividiert eine 32-Bit-Zahl durch eine 16-Bit-Zahl und
524 // liefert einen 32-Bit-Quotienten und einen 16-Bit-Rest.
525 // divu_3216_3216(x,y,q=,r=);
526 // > uint32 x: Zähler
527 // > uint16 y: Nenner
528 // Es sei bekannt, daß y>0.
529 // < uint32 q: floor(x/y)
530 // < uint16 r: x mod y
532 extern "C" uint32 divu_3216_3216_ (uint32 x, uint16 y); // -> Quotient q
534 // Workaround MSVC compiler bug.
535 } extern "C" uint16 divu_16_rest; namespace cln { // -> Rest r
537 extern "C" uint16 divu_16_rest; // -> Rest r
539 #if defined(__GNUC__) && defined(__sparc64__) && !defined(NO_ASM)
540 #define divu_3216_3216(x,y,q_zuweisung,r_zuweisung) \
541 ({var uint32 __x = (x); \
542 var uint16 __y = (y); \
545 __asm__ __volatile__ ( \
546 "wr %%g0,%%g0,%%y\n\t" \
547 "udiv %2,%3,%0\n\t" \
548 "umul %0,%3,%1\n\t" \
550 : "=&r" (__q), "=&r" (__r) \
551 : "r" (__x), "r" (__y)); \
552 q_zuweisung (uint32)__q; \
553 r_zuweisung (uint16)__r; \
555 #elif defined(__sparc__) || defined(__sparc64__) || defined(__i386__) || defined(__x86_64__)
556 #define divu_3216_3216 divu_3232_3232
558 // Methode: (beta = 2^16)
559 // x = x1*beta+x0 schreiben.
560 // Division mit Rest: x1 = q1*y + r1, wobei 0 <= x1 < beta <= beta*y.
561 // Also 0 <= q1 < beta, 0 <= r1 < y.
562 // Division mit Rest: (r1*beta+x0) = q0*y + r0, wobei 0 <= r1*beta+x0 < beta*y.
563 // Also 0 <= q0 < beta, 0 <= r0 < y
564 // und x = x1*beta+x0 = (q1*beta+q0)*y + r0.
565 // Setze q := q1*beta+q0 und r := r0.
566 #define divu_3216_3216(x,y,q_zuweisung,r_zuweisung) \
567 { var uint32 _x = (x); \
568 var uint16 _y = (y); \
572 divu_3216_1616(high16(_x),_y, _q1 = , _r1 = ); \
573 divu_3216_1616(highlow32(_r1,low16(_x)),_y, _q0 = , r_zuweisung); \
574 q_zuweisung highlow32(_q1,_q0); \
578 // Dividiert eine 32-Bit-Zahl durch eine 32-Bit-Zahl und
579 // liefert einen 32-Bit-Quotienten und einen 32-Bit-Rest.
580 // divu_3232_3232(x,y,q=,r=);
581 // > uint32 x: Zähler
582 // > uint32 y: Nenner
583 // Es sei bekannt, daß y>0.
584 // < uint32 q: floor(x/y)
585 // < uint32 r: x mod y
587 extern "C" uint32 divu_3232_3232_ (uint32 x, uint32 y); // -> Quotient q
589 // Workaround MSVC compiler bug.
590 } extern "C" uint32 divu_32_rest; namespace cln { // -> Rest r
592 extern "C" uint32 divu_32_rest; // -> Rest r
594 #if defined(__GNUC__) && defined(__sparc64__) && !defined(NO_ASM)
595 #define divu_3232_3232(x,y,q_zuweisung,r_zuweisung) \
596 ({var uint32 __x = (x); \
597 var uint32 __y = (y); \
600 __asm__ __volatile__ ( \
601 "wr %%g0,%%g0,%%y\n\t" \
602 "udiv %2,%3,%0\n\t" \
603 "umul %0,%3,%1\n\t" \
605 : "=&r" (__q), "=&r" (__r) \
606 : "r" (__x), "r" (__y)); \
607 q_zuweisung (uint32)__q; \
608 r_zuweisung (uint32)__r; \
610 #define divu_3232_3232_(x,y) divu_6432_3232_(0,x,y)
611 #elif defined(__sparc__) || defined(__sparc64__) || defined(__i386__) || defined(__x86_64__)
612 #define divu_3232_3232(x,y,q_zuweisung,r_zuweisung) \
613 divu_6432_3232(0,x,y,q_zuweisung,r_zuweisung)
614 #define divu_3232_3232_(x,y) divu_6432_3232_(0,x,y)
616 // Methode: (beta = 2^n = 2^16, n = 16)
617 // Falls y < beta, handelt es sich um eine 32-durch-16-Bit-Division.
619 // Quotient q = floor(x/y) < beta (da 0 <= x < beta^2, y >= beta).
620 // y habe genau n+k Bits (1 <= k <= n), d.h. 2^(n+k-1) <= y < 2^(n+k).
621 // Schreibe x = 2^k*x1 + x0 mit x1 := floor(x/2^k)
622 // und y = 2^k*y1 + y0 mit y1 := floor(y/2^k)
623 // und bilde den Näherungs-Quotienten floor(x1/y1)
624 // oder (noch besser) floor(x1/(y1+1)).
625 // Wegen 0 <= x1 < 2^(2n) und 0 < 2^(n-1) <= y1 < 2^n
626 // und x1/(y1+1) <= x/y < x1/(y1+1) + 2
627 // (denn x1/(y1+1) = (x1*2^k)/((y1+1)*2^k) <= (x1*2^k)/y <= x/y
628 // und x/y - x1/(y1+1) = (x+x*y1-x1*y)/(y*(y1+1))
629 // = (x+x0*y1-x1*y0)/(y*(y1+1)) <= (x+x0*y1)/(y*(y1+1))
630 // <= x/(y*(y1+1)) + x0/y
631 // <= 2^(2n)/(2^(n+k-1)*(2^(n-1)+1)) + 2^k/2^(n+k-1)
632 // = 2^(n-k+1)/(2^(n-1)+1) + 2^(1-n) <= 2^n/(2^(n-1)+1) + 2^(1-n) < 2 )
633 // gilt floor(x1/(y1+1)) <= floor(x/y) <= floor(x1/(y1+1)) + 2 .
634 // Man bildet also q:=floor(x1/(y1+1)) (ein Shift um n Bit oder
635 // eine (2n)-durch-n-Bit-Division, mit Ergebnis q <= floor(x/y) < beta)
636 // und x-q*y und muß hiervon noch höchstens 2 mal y abziehen und q
637 // incrementieren, um den Quotienten q = floor(x/y) und den Rest
638 // x-floor(x/y)*y der Division zu bekommen.
639 #define divu_3232_3232(x,y,q_zuweisung,r_zuweisung) \
640 { var uint32 _x = (x); \
641 var uint32 _y = (y); \
642 if (_y <= (uint32)(bit(16)-1)) \
646 divu_3216_1616(high16(_x),_y, _q1 = , _r1 = ); \
647 divu_3216_1616(highlow32(_r1,low16(_x)),_y, _q0 = , r_zuweisung); \
648 q_zuweisung highlow32(_q1,_q0); \
651 { var uint32 _x1 = _x; /* x1 := x */ \
652 var uint32 _y1 = _y; /* y1 := y */ \
654 do { _x1 = floor(_x1,2); _y1 = floor(_y1,2); } /* k erhöhen */\
655 until (_y1 <= (uint32)(bit(16)-1)); /* bis y1 < beta */ \
656 { var uint16 _y2 = low16(_y1)+1; /* y1+1 bilden */ \
658 { _q = high16(_x1); } /* y1+1=beta -> ein Shift */ \
660 { divu_3216_1616(_x1,_y2,_q=,); } /* Division von x1 durch y1+1 */\
662 /* _q = q = floor(x1/(y1+1)) */ \
663 /* x-q*y bilden (eine 16-mal-32-Bit-Multiplikation ohne Überlauf): */\
664 _x -= highlow32_0(mulu16(_q,high16(_y))); /* q * high16(y) * beta */\
665 /* gefahrlos, da q*high16(y) <= q*y/beta <= x/beta < beta */ \
666 _x -= mulu16(_q,low16(_y)); /* q * low16(y) */ \
667 /* gefahrlos, da q*high16(y)*beta + q*low16(y) = q*y <= x */ \
668 /* Noch höchstens 2 mal y abziehen: */ \
670 { _q += 1; _x -= _y; \
672 { _q += 1; _x -= _y; } \
675 q_zuweisung (uint32)(_q); \
677 #define NEED_FUNCTION_divu_3232_3232_
680 // Dividiert eine 64-Bit-Zahl durch eine 32-Bit-Zahl und
681 // liefert einen 32-Bit-Quotienten und einen 32-Bit-Rest.
682 // divu_6432_3232(xhi,xlo,y,q=,r=);
683 // > uint32 xhi,xlo: x = 2^32*xhi+xlo = Zähler
684 // > uint32 y: Nenner
685 // > Es sei bekannt, daß 0 <= x < 2^32*y .
686 // < uint32 q: floor(x/y)
687 // < uint32 r: x mod y
689 extern "C" uint32 divu_6432_3232_ (uint32 xhi, uint32 xlo, uint32 y); // -> Quotient q
691 // Workaround MSVC compiler bug.
692 } extern "C" uint32 divu_32_rest; namespace cln { // -> Rest r
694 extern "C" uint32 divu_32_rest; // -> Rest r
696 #if defined(__GNUC__) && defined(__m68k__) && !defined(NO_ASM)
697 #define divu_6432_3232(xhi,xlo,y,q_zuweisung,r_zuweisung) \
698 ({var uint32 __xhi = (xhi); \
699 var uint32 __xlo = (xlo); \
700 var uint32 __y = (y); \
703 __asm__ __volatile__ (" \
705 " : "=d" (__q), "=d" (__r) : "1" (__xhi), "0" (__xlo), "dm" (__y)); \
709 #define divu_6432_3232_(xhi,xlo,y) \
710 ({var uint32 ___q; divu_6432_3232(xhi,xlo,y,___q=,); ___q; })
711 #elif defined(__GNUC__) && defined(__sparc64__) && !defined(NO_ASM)
712 #define divu_6432_3232(xhi,xlo,y,q_zuweisung,r_zuweisung) \
713 ({var uint32 __xhi = (xhi); \
714 var uint32 __xlo = (xlo); \
715 var uint32 __y = (y); \
718 __asm__ __volatile__ ( \
719 "wr %2,%%g0,%%y\n\t" \
720 "udiv %3,%4,%0\n\t" \
721 "umul %0,%4,%1\n\t" \
723 : "=&r" (__q), "=&r" (__r) \
724 : "r" (__xhi), "r" (__xlo), "r" (__y)); \
725 q_zuweisung (uint32)__q; \
726 r_zuweisung (uint32)__r; \
728 #elif defined(__GNUC__) && (defined(__sparc__) || defined(__sparc64__)) && !defined(NO_ASM)
729 #define divu_6432_3232(xhi,xlo,y,q_zuweisung,r_zuweisung) \
730 ({ var uint32 _q = divu_6432_3232_(xhi,xlo,y); /* extern in Assembler */\
731 var register uint32 _r __asm__("%g1"); \
732 q_zuweisung _q; r_zuweisung _r; \
734 #elif defined(__GNUC__) && defined(__arm__) && 0 // see comment cl_asm_arm.cc
735 #define divu_6432_3232(xhi,xlo,y,q_zuweisung,r_zuweisung) \
736 ({ var uint32 _q = divu_6432_3232_(xhi,xlo,y); /* extern in Assembler */\
737 var register uint32 _r __asm__("%r1"/*"%a2"*/); \
738 q_zuweisung _q; r_zuweisung _r; \
740 #elif defined(__GNUC__) && (defined(__i386__) || defined(__x86_64__)) && !defined(NO_ASM)
741 #define divu_6432_3232(xhi,xlo,y,q_zuweisung,r_zuweisung) \
742 ({var uint32 __xhi = (xhi); \
743 var uint32 __xlo = (xlo); \
744 var uint32 __y = (y); \
747 __asm__ __volatile__ ( \
749 : "=a" /* %eax */ (__q), "=d" /* %edx */ (__r) \
750 : "1" /* %edx */ (__xhi), "0" /* %eax */ (__xlo), "rm" (__y) \
755 #define divu_6432_3232_(xhi,xlo,y) \
756 ({var uint32 ___q; divu_6432_3232(xhi,xlo,y,___q=,); ___q; })
757 #elif defined(__GNUC__) && defined(HAVE_LONGLONG) && !defined(__arm__)
758 #define divu_6432_3232(xhi,xlo,y,q_zuweisung,r_zuweisung) \
759 ({var uint32 __xhi = (xhi); \
760 var uint32 __xlo = (xlo); \
761 var uint64 __x = ((uint64)__xhi << 32) | (uint64)__xlo; \
762 var uint32 __y = (y); \
763 var uint32 __q = floor(__x,(uint64)__y); \
764 q_zuweisung __q; r_zuweisung __xlo - __q * __y; \
766 #define divu_6432_3232_(xhi,xlo,y) \
767 ({var uint32 ___q; divu_6432_3232(xhi,xlo,y,___q=,); ___q; })
768 #elif defined(WATCOM) && defined(__i386__) && !defined(NO_ASM)
769 #define divu_6432_3232(xhi,xlo,y,q_zuweisung,r_zuweisung) \
770 { var uint32 __xhi = (xhi); \
771 var uint32 __xlo = (xlo); \
772 var uint32 __y = (y); \
775 __q = divu_6432_3232_(__xhi,__xlo,__y); __r = divu_6432_3232_rest(); \
779 extern "C" uint32 divu_6432_3232_rest (void);
780 #pragma aux divu_6432_3232_ = 0xF7 0xF1 /* divl %ecx */ parm [edx] [eax] [ecx] value [eax] modify [eax edx];
781 #pragma aux divu_6432_3232_rest = /* */ value [edx] modify [];
783 #define divu_6432_3232(xhi,xlo,y,q_zuweisung,r_zuweisung) \
784 { q_zuweisung divu_6432_3232_(xhi,xlo,y); r_zuweisung divu_32_rest; }
785 #if (defined(__m68k__) || defined(__sparc__) || defined(__sparc64__) || defined(__arm__) || (defined(__i386__) && !defined(WATCOM) && !defined(MICROSOFT)) || defined(__x86_64__) || defined(__hppa__)) && !defined(NO_ASM)
786 // divu_6432_3232_ extern in Assembler
787 #if defined(__sparc__) || defined(__sparc64__)
788 extern "C" uint32 _get_g1 (void);
789 #define divu_32_rest (_get_g1()) // Rückgabe im Register %g1
791 #define NEED_VAR_divu_32_rest
794 #define NEED_FUNCTION_divu_6432_3232_
798 #ifdef HAVE_FAST_LONGLONG
800 // Dividiert eine 64-Bit-Zahl durch eine 32-Bit-Zahl und
801 // liefert einen 32-Bit-Quotienten und einen 32-Bit-Rest.
802 // divu_6432_3232_w(x,y,q=,r=);
803 // > uint64 x: Zähler
804 // > uint32 y: Nenner
805 // > Es sei bekannt, daß 0 <= x < 2^32*y .
806 // < uint32 q: floor(x/y)
807 // < uint32 r: x mod y
809 #if defined(__GNUC__) && defined(__sparc64__) && !defined(NO_ASM)
810 // Prefer the udiv and umul instructions over the udivx and mulx instructions
812 #define divu_6432_3232_w(x,y,q_zuweisung,r_zuweisung) \
813 ({var uint64 __x = (x); \
814 var uint32 __xhi = high32(__x); \
815 var uint32 __xlo = low32(__x); \
816 var uint32 __y = (y); \
819 __asm__ __volatile__ ( \
820 "wr %2,%%g0,%%y\n\t" \
821 "udiv %3,%4,%0\n\t" \
822 "umul %0,%4,%1\n\t" \
824 : "=&r" (__q), "=&r" (__r) \
825 : "r" (__xhi), "r" (__xlo), "r" (__y)); \
826 q_zuweisung (uint32)__q; \
827 r_zuweisung (uint32)__r; \
829 #elif defined(__GNUC__) && (defined(__alpha__) || defined(__ia64__) || defined(__mips64__) || defined(__sparc64__))
830 // On __alpha__, computing the remainder by multiplication is just two
831 // instructions, compared to the __remqu (libc) function call for the %
833 // On __ia64__, computing the remainder by multiplication is just four
834 // instructions, compared to the __umoddi3 (libgcc) function call for the %
836 // On __mips64__, computing the remainder by multiplication is just two
837 // instructions, compared to the __umoddi3 (libgcc) function call for the %
839 // On __sparc64__, computing the remainder by multiplication uses a 32-bit
840 // multiplication instruction, compared to a 64-bit multiplication when the %
842 #define divu_6432_3232_w(x,y,q_zuweisung,r_zuweisung) \
843 ({var uint64 __x = (x); \
844 var uint32 __y = (y); \
845 var uint32 __q = floor(__x,(uint64)__y); \
846 q_zuweisung __q; r_zuweisung (uint32)__x - __q * __y; \
848 #elif defined(__GNUC__) && defined(__x86_64__)
849 // On __x86_64__, gcc 4.0 performs both quotient and remainder computation
850 // in a single instruction.
851 #define divu_6432_3232_w(x,y,q_zuweisung,r_zuweisung) \
852 ({var uint64 __x = (x); \
853 var uint32 __y = (y); \
854 var uint32 __q = floor(__x,(uint64)__y); \
855 q_zuweisung __q; r_zuweisung __x % (uint64)__y; \
858 #define divu_6432_3232_w(x,y,q_zuweisung,r_zuweisung) \
859 { var uint64 __x = (x); \
860 divu_6432_3232(high32(__x),low32(__x),(y),q_zuweisung,r_zuweisung); \
864 // Dividiert eine 64-Bit-Zahl durch eine 32-Bit-Zahl und
865 // liefert einen 64-Bit-Quotienten und einen 32-Bit-Rest.
866 // divu_6432_6432(x,y,q=,r=);
867 // > uint64 x: Zähler
868 // > uint32 y: Nenner
869 // > Es sei bekannt, daß y>0.
870 // < uint64 q: floor(x/y)
871 // < uint32 r: x mod y
873 #if defined(__GNUC__) && (defined(__alpha__) || defined(__ia64__) || defined(__mips64__) || defined(__sparc64__))
874 // On __alpha__, computing the remainder by multiplication is just two
875 // instructions, compared to the __remqu (libc) function call for the %
877 // On __ia64__, computing the remainder by multiplication is just four
878 // instructions, compared to the __umoddi3 (libgcc) function call for the %
880 // On __mips64__, computing the remainder by multiplication is just two
881 // instructions, compared to the __umoddi3 (libgcc) function call for the %
883 // On __sparc64__, computing the remainder by multiplication uses a 32-bit
884 // multiplication instruction, compared to a 64-bit multiplication when the %
886 #define divu_6432_6432(x,y,q_zuweisung,r_zuweisung) \
887 ({var uint64 _x = (x); \
888 var uint32 _y = (y); \
890 q_zuweisung _q = floor(_x,(uint64)_y); \
891 r_zuweisung low32(_x) - low32(_q) * _y; \
893 #elif defined(__GNUC__) && defined(__x86_64__)
894 // On __x86_64__, gcc 4.0 performs both quotient and remainder computation
895 // in a single instruction.
896 #define divu_6432_6432(x,y,q_zuweisung,r_zuweisung) \
897 ({var uint64 _x = (x); \
898 var uint32 _y = (y); \
899 q_zuweisung floor(_x,(uint64)_y); \
900 r_zuweisung _x % (uint64)_y; \
903 // Methode: (beta = 2^32)
904 // x = x1*beta+x0 schreiben.
905 // Division mit Rest: x1 = q1*y + r1, wobei 0 <= x1 < beta <= beta*y.
906 // Also 0 <= q1 < beta, 0 <= r1 < y.
907 // Division mit Rest: (r1*beta+x0) = q0*y + r0, wobei 0 <= r1*beta+x0 < beta*y.
908 // Also 0 <= q0 < beta, 0 <= r0 < y
909 // und x = x1*beta+x0 = (q1*beta+q0)*y + r0.
910 // Setze q := q1*beta+q0 und r := r0.
911 #if defined(__GNUC__)
912 #define divu_6432_6432(x,y,q_zuweisung,r_zuweisung) \
913 ({var uint64 _x = (x); \
914 var uint32 _y = (y); \
918 divu_6432_3232(0,high32(_x),_y, _q1 = , _r1 = ); \
919 divu_6432_3232(_r1,low32(_x),_y, _q0 = , r_zuweisung); \
920 q_zuweisung highlow64(_q1,_q0); \
923 #define divu_6432_6432(x,y,q_zuweisung,r_zuweisung) \
924 {var uint64 _x = (x); \
925 var uint32 _y = (y); \
929 divu_6432_3232(0,high32(_x),_y, _q1 = , _r1 = ); \
930 divu_6432_3232(_r1,low32(_x),_y, _q0 = , r_zuweisung); \
931 q_zuweisung highlow64(_q1,_q0); \
936 // Dividiert eine 64-Bit-Zahl durch eine 64-Bit-Zahl und
937 // liefert einen 64-Bit-Quotienten und einen 64-Bit-Rest.
938 // divu_6464_6464(x,y,q=,r=);
939 // > uint64 x: Zähler
940 // > uint64 y: Nenner
941 // > Es sei bekannt, daß y>0.
942 // < uint64 q: floor(x/y)
943 // < uint64 r: x mod y
945 #if defined(__GNUC__) && (defined(__alpha__) || defined(__ia64__) || defined(__mips64__) || defined(__sparc64__))
946 // On __alpha__, computing the remainder by multiplication is just two
947 // instructions, compared to the __remqu (libc) function call for the %
949 // On __ia64__, computing the remainder by multiplication is just four
950 // instructions, compared to the __umoddi3 (libgcc) function call for the %
952 // On __mips64__, computing the remainder by multiplication is just two
953 // instructions, compared to the __umoddi3 (libgcc) function call for the %
955 // On __sparc64__, it doesn't matter.
956 #define divu_6464_6464(x,y,q_zuweisung,r_zuweisung) \
957 ({var uint64 _x = (x); \
958 var uint64 _y = (y); \
960 q_zuweisung _q = floor(_x,_y); \
961 r_zuweisung _x - _q * _y; \
963 #elif defined(__GNUC__) && (defined(__sparc64__) || defined(__x86_64__))
964 // On __sparc64__, it doesn't matter.
965 // On __x86_64__, gcc 4.0 performs both quotient and remainder computation
966 // in a single instruction.
967 #define divu_6464_6464(x,y,q_zuweisung,r_zuweisung) \
968 ({var uint64 _x = (x); \
969 var uint64 _y = (y); \
970 q_zuweisung floor(_x,_y); \
971 r_zuweisung _x % _y; \
974 // For unknown CPUs, we don't know whether gcc's __udivdi3 function plus a
975 // multiplication is slower or faster than our own divu_6464_6464_ routine.
976 // Anyway, call our own routine.
977 extern "C" uint64 divu_6464_6464_ (uint64 x, uint64 y); // -> Quotient q
979 // Workaround MSVC compiler bug.
980 } extern "C" uint64 divu_64_rest; namespace cln { // -> Rest r
982 extern "C" uint64 divu_64_rest; // -> Rest r
984 #define divu_6464_6464(x,y,q_zuweisung,r_zuweisung) \
985 { q_zuweisung divu_6464_6464_(x,y); r_zuweisung divu_64_rest; }
986 #define NEED_VAR_divu_64_rest
987 #define NEED_FUNCTION_divu_6464_6464_
990 // Dividiert eine 128-Bit-Zahl durch eine 64-Bit-Zahl und
991 // liefert einen 64-Bit-Quotienten und einen 64-Bit-Rest.
992 // divu_12864_6464(xhi,xlo,y,q=,r=);
993 // > uint64 xhi,xlo: x = 2^64*xhi+xlo = Zähler
994 // > uint64 y: Nenner
995 // > Es sei bekannt, daß 0 <= x < 2^64*y .
996 // < uint64 q: floor(x/y)
997 // < uint64 r: x mod y
999 extern "C" uint64 divu_12864_6464_ (uint64 xhi, uint64 xlo, uint64 y); // -> Quotient q
1001 // Workaround MSVC compiler bug.
1002 } extern "C" uint64 divu_64_rest; namespace cln { // -> Rest r
1004 extern "C" uint64 divu_64_rest; // -> Rest r
1006 #if defined(__GNUC__) && defined(__x86_64__) && !defined(NO_ASM)
1007 #define divu_12864_6464(xhi,xlo,y,q_zuweisung,r_zuweisung) \
1008 ({var uint64 __xhi = (xhi); \
1009 var uint64 __xlo = (xlo); \
1010 var uint64 __y = (y); \
1013 __asm__ __volatile__ ( \
1015 : "=a" /* %rax */ (__q), "=d" /* %rdx */ (__r) \
1016 : "1" /* %rdx */ (__xhi), "0" /* %rax */ (__xlo), "rm" (__y) \
1021 #define divu_12864_64364_(xhi,xlo,y) \
1022 ({var uint64 ___q; divu_12864_6464(xhi,xlo,y,___q=,); ___q; })
1024 #define divu_12864_6464(xhi,xlo,y,q_zuweisung,r_zuweisung) \
1025 { q_zuweisung divu_12864_6464_(xhi,xlo,y); r_zuweisung divu_64_rest; }
1026 #define NEED_VAR_divu_64_rest
1027 #define NEED_FUNCTION_divu_12864_6464_
1030 #endif /* HAVE_FAST_LONGLONG */
1033 // Zieht die Ganzzahl-Wurzel aus einer 32-Bit-Zahl und
1034 // liefert eine 16-Bit-Wurzel und einen Rest.
1035 // isqrt_32_16(x,y=,sqrtp=);
1036 // > uint32 x: Radikand, >= 2^30, < 2^32
1037 // < uint16 y: floor(sqrt(x)), >= 2^15, < 2^16
1038 // < boolean sqrtp: /=0, falls x=y^2
1040 // y := 2^16 als Anfangswert,
1041 // y := floor((y + floor(x/y))/2) als nächster Wert,
1042 // solange z := floor(x/y) < y, setze y := floor((y+z)/2).
1043 // y ist fertig; x=y^2 genau dann, wenn z=y und die letzte Division aufging.
1045 // 1. Die Folge der y ist streng monoton fallend.
1046 // 2. Stets gilt y >= floor(sqrt(x)) (denn für alle y>0 ist
1047 // y + x/y >= 2*sqrt(x) und daher floor((y + floor(x/y))/2) =
1048 // floor(y/2 + x/(2*y)) >= floor(sqrt(x)) ).
1049 // 3. Am Schluß gilt x >= y^2.
1051 #define isqrt_32_16(x,y_zuweisung,sqrtp_zuweisung) \
1052 { var uint32 _x = (x); \
1053 var uint16 _x1 = high16(_x); \
1054 var uint16 _y = floor(_x1,2) | bit(16-1); \
1058 if (_x1 >= _y) /* Division _x/_y ergäbe Überlauf -> _z > _y */\
1059 { unused (sqrtp_zuweisung FALSE); break; } \
1060 divu_3216_1616(_x,_y, _z=,_r=); /* Dividiere _x/_y */ \
1062 { unused (sqrtp_zuweisung (_z == _y) && (_r == 0)); break; } \
1063 _y = floor((uint16)(_z+_y),2) | bit(16-1); /* _y muß >= 2^15 bleiben */\
1068 // Zieht die Ganzzahl-Wurzel aus einer 64-Bit-Zahl und
1069 // liefert eine 32-Bit-Wurzel und einen Rest.
1070 // isqrt_64_32(xhi,xlo,y=,sqrtp=);
1071 // > uint32 xhi,xlo: Radikand x = 2^32*xhi+xlo, >= 2^62, < 2^64
1072 // < uint32 y: floor(sqrt(x)), >= 2^31, < 2^32
1073 // < boolean sqrtp: /=0, falls x=y^2
1074 #if defined(__sparc__) || defined(__sparc64__) || defined(__m68k__) || defined(__hppa__)
1076 // y := 2^32 als Anfangswert,
1077 // y := floor((y + floor(x/y))/2) als nächster Wert,
1078 // solange z := floor(x/y) < y, setze y := floor((y+z)/2).
1079 // y ist fertig; x=y^2 genau dann, wenn z=y und die letzte Division aufging.
1081 // 1. Die Folge der y ist streng monoton fallend.
1082 // 2. Stets gilt y >= floor(sqrt(x)) (denn für alle y>0 ist
1083 // y + x/y >= 2*sqrt(x) und daher floor((y + floor(x/y))/2) =
1084 // floor(y/2 + x/(2*y)) >= floor(sqrt(x)) ).
1085 // 3. Am Schluß gilt x >= y^2.
1087 #define isqrt_64_32(xhi,xlo,y_zuweisung,sqrtp_zuweisung) \
1088 { var uint32 _xhi = (xhi); \
1089 var uint32 _xlo = (xlo); \
1090 var uint32 _y = floor(_xhi,2) | bit(32-1); \
1094 if (_xhi >= _y) /* Division _x/_y ergäbe Überlauf -> _z > _y */\
1095 { sqrtp_zuweisung FALSE; break; } \
1096 divu_6432_3232(_xhi,_xlo,_y, _z=,_rest=); /* Dividiere _x/_y */\
1098 { sqrtp_zuweisung (_z == _y) && (_rest == 0); break; } \
1099 _y = floor(_z+_y,2) | bit(32-1); /* _y muß >= 2^31 bleiben */ \
1105 // Wie bei UDS_sqrt mit n=2.
1106 // y = 2^16*yhi + ylo ansetzen.
1108 // yhi = floor(y/2^16) = floor(floor(sqrt(x))/2^16)
1109 // = floor(sqrt(x)/2^16) = floor(sqrt(x/2^32)) = isqrt(xhi)
1110 // sein. Es folgt yhi >= 2^15.
1111 // Danach sucht man das größte ylo >=0 mit
1112 // x - 2^32*yhi^2 >= 2*2^16*yhi*ylo + ylo^2.
1113 // Dazu setzen wir xhi*2^32+xlo := x - 2^32*yhi^2
1114 // (also xhi := xhi - yhi^2, das ist >=0, <=2*yhi).
1115 // Die Schätzung für die zweite Ziffer
1116 // ylo' := min(2^16-1,floor((xhi*2^32+xlo)/(2*2^16*yhi)))
1117 // erfüllt ylo'-1 <= ylo <= ylo', ist also um höchstens 1 zu groß.
1118 // (Beweis: Rechte Ungleichung klar, da ylo < 2^16 und
1119 // xhi*2^32+xlo >= 2*2^16*yhi*ylo + ylo^2 >= 2*2^16*yhi*ylo
1120 // ==> (xhi*2^32+xlo)/(2*2^16*yhi) >= ylo gelten muß.
1121 // Linke Ungleichung: Falls floor(...)>=2^16, ist
1122 // xhi*2^32+xlo >= 2*2^16*2^16*yhi >= 2*2^16*yhi*(2^16-1) + 2^32
1123 // >= 2*2^16*yhi*(2^16-1) + (2^16-1)^2
1124 // und xhi*2^32+xlo < 2*2^16*2^16*yhi + (2^16)^2, also
1125 // ylo = 2^16-1 = ylo'.
1126 // Sonst ist ylo' = floor((xhi*2^32+xlo)/(2*2^16*yhi)), also
1127 // xhi*2^32+xlo >= 2*2^16*yhi*ylo' >= 2*2^16*yhi*(ylo'-1) + 2^32
1128 // >= 2*2^16*yhi*(ylo'-1) + (ylo'-1)^2,
1129 // also ylo >= ylo'-1 nach Definition von ylo.)
1130 #define isqrt_64_32(xhi,xlo,y_zuweisung,sqrtp_zuweisung) \
1131 { var uint32 _xhi = (xhi); \
1132 var uint32 _xlo = (xlo); \
1135 /* erste Ziffer berechnen: */ \
1136 isqrt_32_16(_xhi,_yhi=,); /* yhi := isqrt(xhi) */ \
1137 _xhi -= mulu16(_yhi,_yhi); /* jetzt 0 <= xhi <= 2*yhi */ \
1138 /* x = 2^32*yhi^2 + 2^32*xhi + xlo */ \
1139 /* Schätzung für die zweite Ziffer berechnen: */ \
1140 /* ylo := min(2^16-1,floor((xhi*2^32+xlo)/(2*2^16*yhi))) bilden: */\
1141 {var uint32 _z = (_xhi << 15) | (_xlo >> 17); /* < 2^15*(2*yhi+1) */\
1142 var uint32 _r = highlow32_0(_yhi); \
1144 { _ylo = bit(16)-1; _r = _z - _r + (uint32)_yhi; } \
1146 { divu_3216_1616(_z,_yhi, _ylo=,_r=); } \
1147 /* x = 2^32*yhi^2 + 2*2^16*yhi*ylo + 2^17*r + (xlo mod 2^17), */ \
1148 /* 0 <= r < yhi + 2^15 */ \
1149 _xlo = (_r << 17) | (_xlo & (bit(17)-1)); \
1150 /* x = 2^32*yhi^2 + 2*2^16*yhi*ylo + 2^32*floor(r/2^15) + xlo */ \
1151 _z = mulu16(_ylo,_ylo); /* z = ylo^2 */ \
1152 /* Versuche vom Rest 2^32*floor(r/2^15) + xlo z zu subtrahieren. */\
1153 /* Falls Rest >= z (d.h. r>=2^15 oder xlo>=z), ist ylo fertig, */ \
1154 /* und es gilt x=y^2 genau dann, wenn r<2^15 und xlo=z. */ \
1155 /* Sonst (d.h. r<2^15 und xlo<z), muß man ylo erniedrigen. Dazu */\
1156 /* setzt man ylo := ylo-1, z := z-(2*ylo+1), */ \
1157 /* Rest := Rest + 2^17*yhi = xlo + 2^17*yhi >= 2^32 > z, also x>y^2. */\
1160 { _ylo -= 1; sqrtp_zuweisung FALSE; } \
1162 { sqrtp_zuweisung (_xlo == _z); } \
1165 { sqrtp_zuweisung FALSE; } \
1166 y_zuweisung highlow32(_yhi,_ylo); \
1170 #ifdef HAVE_FAST_LONGLONG
1172 // Zieht die Ganzzahl-Wurzel aus einer 128-Bit-Zahl und
1173 // liefert eine 64-Bit-Wurzel und einen Rest.
1174 // isqrt_128_64(xhi,xlo,y=,sqrtp=);
1175 // > uint64 xhi,xlo: Radikand x = 2^64*xhi+xlo, >= 2^126, < 2^128
1176 // < uint64 y: floor(sqrt(x)), >= 2^63, < 2^64
1177 // < boolean sqrtp: /=0, falls x=y^2
1179 // Wie bei UDS_sqrt mit n=2.
1180 // y = 2^32*yhi + ylo ansetzen.
1182 // yhi = floor(y/2^32) = floor(floor(sqrt(x))/2^32)
1183 // = floor(sqrt(x)/2^32) = floor(sqrt(x/2^64)) = isqrt(xhi)
1184 // sein. Es folgt yhi >= 2^31.
1185 // Danach sucht man das größte ylo >=0 mit
1186 // x - 2^64*yhi^2 >= 2*2^32*yhi*ylo + ylo^2.
1187 // Dazu setzen wir xhi*2^64+xlo := x - 2^64*yhi^2
1188 // (also xhi := xhi - yhi^2, das ist >=0, <=2*yhi).
1189 // Die Schätzung für die zweite Ziffer
1190 // ylo' := min(2^32-1,floor((xhi*2^64+xlo)/(2*2^32*yhi)))
1191 // erfüllt ylo'-1 <= ylo <= ylo', ist also um höchstens 1 zu groß.
1192 // (Beweis: Rechte Ungleichung klar, da ylo < 2^32 und
1193 // xhi*2^64+xlo >= 2*2^32*yhi*ylo + ylo^2 >= 2*2^32*yhi*ylo
1194 // ==> (xhi*2^64+xlo)/(2*2^32*yhi) >= ylo gelten muß.
1195 // Linke Ungleichung: Falls floor(...)>=2^32, ist
1196 // xhi*2^64+xlo >= 2*2^32*2^32*yhi >= 2*2^32*yhi*(2^32-1) + 2^64
1197 // >= 2*2^32*yhi*(2^32-1) + (2^32-1)^2
1198 // und xhi*2^64+xlo < 2*2^32*2^32*yhi + (2^32)^2, also
1199 // ylo = 2^32-1 = ylo'.
1200 // Sonst ist ylo' = floor((xhi*2^64+xlo)/(2*2^32*yhi)), also
1201 // xhi*2^64+xlo >= 2*2^32*yhi*ylo' >= 2*2^32*yhi*(ylo'-1) + 2^64
1202 // >= 2*2^32*yhi*(ylo'-1) + (ylo'-1)^2,
1203 // also ylo >= ylo'-1 nach Definition von ylo.)
1204 #define isqrt_128_64(x_hi,x_lo,y_zuweisung,sqrtp_zuweisung) \
1205 { var uint64 xhi = (x_hi); \
1206 var uint64 xlo = (x_lo); \
1209 /* erste Ziffer berechnen: */ \
1210 isqrt_64_32(high32(xhi),low32(xhi),yhi=,); /* yhi := isqrt(xhi) */\
1211 xhi -= mulu32_w(yhi,yhi); /* jetzt 0 <= xhi <= 2*yhi */ \
1212 /* x = 2^64*yhi^2 + 2^64*xhi + xlo */ \
1213 /* Schätzung für die zweite Ziffer berechnen: */ \
1214 /* ylo := min(2^32-1,floor((xhi*2^64+xlo)/(2*2^32*yhi))) bilden: */\
1215 {var uint64 z = (xhi << 31) | (xlo >> 33); /* < 2^31*(2*yhi+1) */ \
1216 var uint64 r = highlow64_0(yhi); \
1218 { ylo = bit(32)-1; r = z - r + (uint64)yhi; } \
1220 { divu_6432_3232_w(z,yhi, ylo=,r=); } \
1221 /* x = 2^64*yhi^2 + 2*2^32*yhi*ylo + 2^33*r + (xlo mod 2^33), */ \
1222 /* 0 <= r < yhi + 2^31 */ \
1223 xlo = (r << 33) | (xlo & (bit(33)-1)); \
1224 /* x = 2^64*yhi^2 + 2*2^32*yhi*ylo + 2^64*floor(r/2^31) + xlo */ \
1225 z = mulu32_w(ylo,ylo); /* z = ylo^2 */ \
1226 /* Versuche vom Rest 2^64*floor(r/2^31) + xlo z zu subtrahieren. */\
1227 /* Falls Rest >= z (d.h. r>=2^31 oder xlo>=z), ist ylo fertig, */ \
1228 /* und es gilt x=y^2 genau dann, wenn r<2^31 und xlo=z. */ \
1229 /* Sonst (d.h. r<2^31 und xlo<z), muß man ylo erniedrigen. Dazu */\
1230 /* setzt man ylo := ylo-1, z := z-(2*ylo+1), */ \
1231 /* Rest := Rest + 2^33*yhi = xlo + 2^33*yhi >= 2^64 > z, also x>y^2. */\
1234 { ylo -= 1; sqrtp_zuweisung FALSE; } \
1236 { sqrtp_zuweisung (xlo == z); } \
1239 { sqrtp_zuweisung FALSE; } \
1240 y_zuweisung highlow64(yhi,ylo); \
1243 #endif /* HAVE_FAST_LONGLONG */
1245 // Zieht die Ganzzahl-Wurzel aus einer 32-Bit-Zahl und
1246 // liefert eine 16-Bit-Wurzel.
1248 // > uintL x : Radikand, >=0, <2^32
1249 // < uintL ergebnis : Wurzel, >=0, <2^16
1250 extern uintL isqrt (uintL x);
1252 #ifdef HAVE_LONGLONG
1253 // Extracts integer root of a 64-bit number and returns a 32-bit number.
1255 // > uintQ x : radicand, >=0, <2^64
1256 // < uintL result : square root, >=0, <2^32
1257 extern uintL isqrt (uintQ x);
1260 // Sorry for this. We need an isqrt function taking uintC arguments but we
1261 // cannot use overloading since this would lead to ambiguities with any of the
1262 // two signatures above.
1263 inline uintL isqrtC (uintC x)
1266 return isqrt((uintL)x);
1268 return isqrt((uintQ)x);
1273 // Zieht die Ganzzahl-Wurzel aus einer 64-Bit-Zahl und
1274 // liefert eine 32-Bit-Wurzel.
1276 // > uintL2 x = x1*2^32+x0 : Radikand, >=0, <2^64
1277 // < uintL ergebnis : Wurzel, >=0, <2^32
1278 extern uintL isqrt (uintL x1, uintL x0);
1281 // Bits einer 8-Bit-Zahl zählen:
1282 // integerlength8(digit,size=);
1283 // setzt size auf die höchste in digit vorkommende Bitnummer.
1284 // > digit: ein uint8 >0
1285 // < size: >0, <=8, mit 2^(size-1) <= digit < 2^size
1286 #if defined(__GNUC__) && defined(__m68k__) && !defined(NO_ASM)
1287 #define integerlength8(digit,size_zuweisung) \
1288 { var uintL _zero_counter; /* zählt die führenden Nullbits in digit */\
1289 __asm__("bfffo %1{#0:#8},%0" : "=d" (_zero_counter) : "dm" ((uint8)(digit)) ); \
1290 size_zuweisung (8-_zero_counter); \
1292 #elif defined(__sparc__) && !defined(__sparc64__)
1293 #define integerlength8(digit,size_zuweisung) \
1294 integerlength32((uint32)(digit),size_zuweisung) // siehe unten
1295 #elif defined(__GNUC__) && defined(__i386__) && !defined(NO_ASM)
1296 #define integerlength8(digit,size_zuweisung) \
1297 integerlength16((uint16)(digit),size_zuweisung)
1299 #define integerlength8(digit,size_zuweisung) \
1300 { var uintC _bitsize = 1; \
1301 var uintL _x8 = (uint8)(digit); \
1302 /* _x8 hat höchstens 8 Bits. */\
1303 if (_x8 >= bit(4)) { _x8 = _x8>>4; _bitsize += 4; } \
1304 /* _x8 hat höchstens 4 Bits. */\
1305 if (_x8 >= bit(2)) { _x8 = _x8>>2; _bitsize += 2; } \
1306 /* _x8 hat höchstens 2 Bits. */\
1307 if (_x8 >= bit(1)) { /* _x8 = _x8>>1; */ _bitsize += 1; } \
1308 /* _x8 hat höchstens 1 Bit. Dieses Bit muß gesetzt sein. */\
1309 size_zuweisung _bitsize; \
1313 // Bits einer 16-Bit-Zahl zählen:
1314 // integerlength16(digit,size=);
1315 // setzt size auf die höchste in digit vorkommende Bitnummer.
1316 // > digit: ein uint16 >0
1317 // < size: >0, <=16, mit 2^(size-1) <= digit < 2^size
1318 #if defined(__GNUC__) && defined(__m68k__) && !defined(NO_ASM)
1319 #define integerlength16(digit,size_zuweisung) \
1320 { var uintL _zero_counter; /* zählt die führenden Nullbits in digit */\
1321 __asm__("bfffo %1{#0:#16},%0" : "=d" (_zero_counter) : "dm" ((uint16)(digit)) ); \
1322 size_zuweisung (16-_zero_counter); \
1324 #elif defined(__sparc__) && !defined(__sparc64__)
1325 #define integerlength16(digit,size_zuweisung) \
1326 integerlength32((uint32)(digit),size_zuweisung) // siehe unten
1327 #elif defined(__GNUC__) && defined(__i386__) && !defined(NO_ASM)
1328 #define integerlength16(digit,size_zuweisung) \
1329 { var uintW _one_position; /* Position der führenden 1 */\
1330 __asm__("bsrw %1,%0" : "=r" (_one_position) : "r" ((uint16)(digit)) ); \
1331 size_zuweisung (1+_one_position); \
1333 // Die weiteren kommen von gcc/longlong.h :
1334 #elif defined(__GNUC__) && defined(__ibm032__) && !defined(NO_ASM) // RT/ROMP
1335 #define integerlength16(digit,size_zuweisung) \
1336 { var uintL _zero_counter; /* zählt die führenden Nullbits in digit */\
1337 __asm__("clz %0,%1" : "=r" (_zero_counter) : "r" ((uint32)(digit)) ); \
1338 size_zuweisung (16-_zero_counter); \
1341 #define integerlength16(digit,size_zuweisung) \
1342 { var uintC _bitsize = 1; \
1343 var uintWL _x16 = (uint16)(digit); \
1344 /* _x16 hat höchstens 16 Bits. */\
1345 if (_x16 >= bit(8)) { _x16 = _x16>>8; _bitsize += 8; } \
1346 /* _x16 hat höchstens 8 Bits. */\
1347 if (_x16 >= bit(4)) { _x16 = _x16>>4; _bitsize += 4; } \
1348 /* _x16 hat höchstens 4 Bits. */\
1349 if (_x16 >= bit(2)) { _x16 = _x16>>2; _bitsize += 2; } \
1350 /* _x16 hat höchstens 2 Bits. */\
1351 if (_x16 >= bit(1)) { /* _x16 = _x16>>1; */ _bitsize += 1; } \
1352 /* _x16 hat höchstens 1 Bit. Dieses Bit muß gesetzt sein. */\
1353 size_zuweisung _bitsize; \
1357 // Bits einer 32-Bit-Zahl zählen:
1358 // integerlength32(digit,size=);
1359 // setzt size auf die höchste in digit vorkommende Bitnummer.
1360 // > digit: ein uint32 >0
1361 // < size: >0, <=32, mit 2^(size-1) <= digit < 2^size
1362 #if defined(__GNUC__) && defined(__m68k__) && !defined(NO_ASM)
1363 #define integerlength32(digit,size_zuweisung) \
1364 { var uintL _zero_counter; /* zählt die führenden Nullbits in digit */\
1365 __asm__("bfffo %1{#0:#32},%0" : "=d" (_zero_counter) : "dm" ((uint32)(digit)) ); \
1366 size_zuweisung (32-_zero_counter); \
1368 #elif defined(__sparc__) && !defined(__sparc64__) && defined(FAST_DOUBLE)
1369 #define integerlength32(digit,size_zuweisung) \
1370 {var union { double f; uint32 i[2]; } __fi; \
1371 const int df_mant_len = 52; /* mantissa bits (excl. hidden bit) */\
1372 const int df_exp_mid = 1022; /* exponent bias */ \
1373 /* Bilde 2^52 + digit: */\
1374 __fi.i[0] = (uint32)(df_mant_len+1+df_exp_mid) << (df_mant_len-32); /* Vorzeichen 0, Exponent 53 */\
1375 __fi.i[1] = (digit); /* untere 32 Bits setzen (benutzt CL_CPU_BIG_ENDIAN_P !) */\
1376 /* subtrahiere 2^52: */\
1377 __fi.f = __fi.f - (double)(4503599627370496.0L); \
1378 /* Hole davon den Exponenten: */\
1379 size_zuweisung ((__fi.i[0] >> (df_mant_len-32)) - df_exp_mid); \
1381 #elif defined(__GNUC__) && defined(__i386__) && !defined(NO_ASM)
1382 #define integerlength32(digit,size_zuweisung) \
1383 { var uintL _one_position; /* Position der führenden 1 */\
1384 __asm__("bsrl %1,%0" : "=r" (_one_position) : "rm" ((uint32)(digit)) ); \
1385 size_zuweisung (1+_one_position); \
1387 #elif defined(__hppa__) && !defined(NO_ASM)
1388 #define integerlength32(digit,size_zuweisung) \
1389 size_zuweisung length32(digit);
1390 extern "C" uintL length32 (uintL digit); // extern in Assembler
1391 // Die weiteren kommen von gcc/longlong.h :
1392 #elif defined(__GNUC__) && (defined(__a29k__) || defined(___AM29K__)) && !defined(NO_ASM)
1393 #define integerlength32(digit,size_zuweisung) \
1394 { var uintL _zero_counter; /* zählt die führenden Nullbits in digit */\
1395 __asm__("clz %0,%1" : "=r" (_zero_counter) : "r" ((uint32)(digit)) ); \
1396 size_zuweisung (32-_zero_counter); \
1398 #elif defined(__GNUC__) && defined(__gmicro__) && !defined(NO_ASM)
1399 #define integerlength32(digit,size_zuweisung) \
1400 { var uintL _zero_counter; /* zählt die führenden Nullbits in digit */\
1401 __asm__("bsch/1 %1,%0" : "=g" (_zero_counter) : "g" ((uint32)(digit)) ); \
1402 size_zuweisung (32-_zero_counter); \
1404 #elif defined(__GNUC__) && defined(__rs6000__) && !defined(NO_ASM)
1406 // old assembler syntax
1407 #define integerlength32(digit,size_zuweisung) \
1408 { var uintL _zero_counter; /* zählt die führenden Nullbits in digit */\
1409 __asm__("cntlz %0,%1" : "=r" (_zero_counter) : "r" ((uint32)(digit)) ); \
1410 size_zuweisung (32-_zero_counter); \
1413 // new assembler syntax
1414 #define integerlength32(digit,size_zuweisung) \
1415 { var uintL _zero_counter; /* zählt die führenden Nullbits in digit */\
1416 __asm__("cntlzw %0,%1" : "=r" (_zero_counter) : "r" ((uint32)(digit)) ); \
1417 size_zuweisung (32-_zero_counter); \
1420 #elif defined(__GNUC__) && defined(__m88k__) && !defined(NO_ASM)
1421 #define integerlength32(digit,size_zuweisung) \
1422 { var uintL _one_position; /* Position der führenden 1 */\
1423 __asm__("ff1 %0,%1" : "=r" (_one_position) : "r" ((uint32)(digit)) ); \
1424 size_zuweisung (1+_one_position); \
1426 #elif defined(__GNUC__) && defined(__ibm032__) && !defined(NO_ASM) // RT/ROMP
1427 #define integerlength32(digit,size_zuweisung) \
1428 { var uintL _x32 = (uint32)(digit); \
1429 if (_x32 >= bit(16)) \
1430 { integerlength16(_x32>>16,size_zuweisung 16 + ); } \
1432 { integerlength16(_x32,size_zuweisung); } \
1435 #define integerlength32(digit,size_zuweisung) \
1436 { var uintC _bitsize = 1; \
1437 var uintL _x32 = (uint32)(digit); \
1438 /* _x32 hat höchstens 32 Bits. */\
1439 if (_x32 >= bit(16)) { _x32 = _x32>>16; _bitsize += 16; } \
1440 /* _x32 hat höchstens 16 Bits. */\
1441 if (_x32 >= bit(8)) { _x32 = _x32>>8; _bitsize += 8; } \
1442 /* _x32 hat höchstens 8 Bits. */\
1443 if (_x32 >= bit(4)) { _x32 = _x32>>4; _bitsize += 4; } \
1444 /* _x32 hat höchstens 4 Bits. */\
1445 if (_x32 >= bit(2)) { _x32 = _x32>>2; _bitsize += 2; } \
1446 /* _x32 hat höchstens 2 Bits. */\
1447 if (_x32 >= bit(1)) { /* _x32 = _x32>>1; */ _bitsize += 1; } \
1448 /* _x32 hat höchstens 1 Bit. Dieses Bit muß gesetzt sein. */\
1449 size_zuweisung _bitsize; \
1451 #define GENERIC_INTEGERLENGTH32
1454 // Bits einer 64-Bit-Zahl zählen:
1455 // integerlength64(digit,size=);
1456 // setzt size auf die höchste in digit vorkommende Bitnummer.
1457 // > digit: ein uint64 >0
1458 // < size: >0, <=64, mit 2^(size-1) <= digit < 2^size
1459 #ifdef GENERIC_INTEGERLENGTH32
1460 #define integerlength64(digit,size_zuweisung) \
1461 { var uintC _bitsize = 1; \
1462 var uint64 _x64 = (uint64)(digit); \
1463 /* _x64 hat höchstens 64 Bits. */\
1464 if (_x64 >= bit(32)) { _x64 = _x64>>32; _bitsize += 32; } \
1465 /* _x64 hat höchstens 32 Bits. */\
1466 if (_x64 >= bit(16)) { _x64 = _x64>>16; _bitsize += 16; } \
1467 /* _x64 hat höchstens 16 Bits. */\
1468 if (_x64 >= bit(8)) { _x64 = _x64>>8; _bitsize += 8; } \
1469 /* _x64 hat höchstens 8 Bits. */\
1470 if (_x64 >= bit(4)) { _x64 = _x64>>4; _bitsize += 4; } \
1471 /* _x64 hat höchstens 4 Bits. */\
1472 if (_x64 >= bit(2)) { _x64 = _x64>>2; _bitsize += 2; } \
1473 /* _x64 hat höchstens 2 Bits. */\
1474 if (_x64 >= bit(1)) { /* _x64 = _x64>>1; */ _bitsize += 1; } \
1475 /* _x64 hat höchstens 1 Bit. Dieses Bit muß gesetzt sein. */\
1476 size_zuweisung _bitsize; \
1479 #define integerlength64(digit,size_zuweisung) \
1480 { var uint64 _x64 = (digit); \
1481 var uintC _bitsize64 = 0; \
1482 var uint32 _x32_from_integerlength64; \
1483 if (_x64 >= (1ULL << 32)) { \
1484 _x32_from_integerlength64 = _x64>>32; _bitsize64 += 32; \
1486 _x32_from_integerlength64 = _x64; \
1488 integerlength32(_x32_from_integerlength64, size_zuweisung _bitsize64 + ); \
1492 // Bits einer uintC-Zahl zählen:
1493 // integerlengthC(digit,size=);
1494 // setzt size auf die höchste in digit vorkommende Bitnummer.
1495 // > digit: ein uintC >0
1496 // < size: >0, <=intCsize, mit 2^(size-1) <= digit < 2^size
1498 #define integerlengthC integerlength32
1501 #define integerlengthC integerlength64
1504 // Hintere Nullbits eines 32-Bit-Wortes zählen:
1505 // ord2_32(digit,count=);
1506 // setzt size auf die kleinste in digit vorkommende Bitnummer.
1507 // > digit: ein uint32 >0
1508 // < count: >=0, <32, mit 2^count | digit, digit/2^count ungerade
1509 #if defined(__GNUC__) && defined(__i386__) && !defined(NO_ASM)
1510 #define ord2_32(digit,count_zuweisung) \
1511 { var uintL _one_position; /* Position der letzten 1 */\
1512 __asm__("bsfl %1,%0" : "=r" (_one_position) : "rm" ((uint32)(digit)) ); \
1513 count_zuweisung _one_position; \
1516 #elif defined(__sparc__) && !defined(__sparc64__)
1517 #define ord2_32(digit,count_zuweisung) \
1518 { var uint32 n = (digit); \
1522 n = n - (n<<16); /* or n = n ^ (n<<16); or n = n &~ (n<<16); */ \
1523 /* static const char ord2_tab [64] = {-1,0,1,12,2,6,-1,13,3,-1,7,-1,-1,-1,-1,14,10,4,-1,-1,8,-1,-1,25,-1,-1,-1,-1,-1,21,27,15,31,11,5,-1,-1,-1,-1,-1,9,-1,-1,24,-1,-1,20,26,30,-1,-1,-1,-1,23,-1,19,29,-1,22,18,28,17,16,-1}; */ \
1524 /* count_zuweisung ord2_tab[n>>26]; */ \
1525 count_zuweisung "\377\000\001\014\002\006\377\015\003\377\007\377\377\377\377\016\012\004\377\377\010\377\377\031\377\377\377\377\377\025\033\017\037\013\005\377\377\377\377\377\011\377\377\030\377\377\024\032\036\377\377\377\377\027\377\023\035\377\026\022\034\021\020"[n>>26]; \
1529 // Sei n = ord2(x). Dann ist logxor(x,x-1) = 2^n + (2^n-1) = 2^(n+1)-1.
1530 // Also (ord2 x) = (1- (integer-length (logxor x (1- x)))) .
1531 #define ord2_32(digit,count_zuweisung) \
1532 { var uint32 _digit = (digit) ^ ((digit) - 1); \
1533 integerlength32(_digit,count_zuweisung -1 + ) \
1537 // Hintere Nullbits eines 64-Bit-Wortes zählen:
1538 // ord2_64(digit,count=);
1539 // setzt size auf die kleinste in digit vorkommende Bitnummer.
1540 // > digit: ein uint64 >0
1541 // < count: >=0, <64, mit 2^count | digit, digit/2^count ungerade
1542 // Sei n = ord2(x). Dann ist logxor(x,x-1) = 2^n + (2^n-1) = 2^(n+1)-1.
1543 // Also (ord2 x) = (1- (integer-length (logxor x (1- x)))) .
1544 #define ord2_64(digit,count_zuweisung) \
1545 { var uint64 _digit = (digit) ^ ((digit) - 1); \
1546 integerlength64(_digit,count_zuweisung -1 + ) \
1550 // Bits eines Wortes zählen.
1552 // > xNN: ein uintNN
1553 // < xNN: Anzahl der darin gesetzten Bits
1554 // Bits von x8 zählen: (Input x8, Output x8)
1555 #define logcount_8() \
1556 ( /* x8 besteht aus 8 1-Bit-Zählern (0,1). */\
1557 x8 = (x8 & 0x55U) + ((x8 & 0xAAU) >> 1), \
1558 /* x8 besteht aus 4 2-Bit-Zählern (0,1,2). */\
1559 x8 = (x8 & 0x33U) + ((x8 & 0xCCU) >> 2), \
1560 /* x8 besteht aus 2 4-Bit-Zählern (0,1,2,3,4). */\
1561 x8 = (x8 & 0x0FU) + (x8 >> 4) \
1562 /* x8 besteht aus 1 8-Bit-Zähler (0,...,8). */\
1564 // Bits von x16 zählen: (Input x16, Output x16)
1565 #define logcount_16() \
1566 ( /* x16 besteht aus 16 1-Bit-Zählern (0,1). */\
1567 x16 = (x16 & 0x5555U) + ((x16 & 0xAAAAU) >> 1), \
1568 /* x16 besteht aus 8 2-Bit-Zählern (0,1,2). */\
1569 x16 = (x16 & 0x3333U) + ((x16 & 0xCCCCU) >> 2), \
1570 /* x16 besteht aus 4 4-Bit-Zählern (0,1,2,3,4). */\
1571 x16 = (x16 & 0x0F0FU) + ((x16 & 0xF0F0U) >> 4), \
1572 /* x16 besteht aus 2 8-Bit-Zählern (0,...,8). */\
1573 x16 = (x16 & 0x00FFU) + (x16 >> 8) \
1574 /* x16 besteht aus 1 16-Bit-Zähler (0,...,16). */\
1576 // Bits von x32 zählen: (Input x32, Output x32)
1577 #define logcount_32() \
1578 ( /* x32 besteht aus 32 1-Bit-Zählern (0,1). */\
1579 x32 = (x32 & 0x55555555UL) + ((x32 & 0xAAAAAAAAUL) >> 1), \
1580 /* x32 besteht aus 16 2-Bit-Zählern (0,1,2). */\
1581 x32 = (x32 & 0x33333333UL) + ((x32 & 0xCCCCCCCCUL) >> 2), \
1582 /* x32 besteht aus 8 4-Bit-Zählern (0,1,2,3,4). */\
1583 x32 = high16(x32)+low16(x32), \
1584 /* x32 besteht aus 4 4-Bit-Zählern (0,...,8). */\
1585 x32 = (x32 & 0x0F0FU) + ((x32 & 0xF0F0U) >> 4), \
1586 /* x32 besteht aus 2 8-Bit-Zählern (0,...,16). */\
1587 x32 = (x32 & 0x00FFU) + (x32 >> 8) \
1588 /* x32 besteht aus 1 16-Bit-Zähler (0,...,32). */\
1590 // Bits von x64 zählen: (Input x64, Output x64)
1591 #define logcount_64() \
1592 ( /* x64 besteht aus 64 1-Bit-Zählern (0,1). */\
1593 x64 = (x64 & 0x5555555555555555ULL) + ((x64 & 0xAAAAAAAAAAAAAAAAULL) >> 1),\
1594 /* x64 besteht aus 32 2-Bit-Zählern (0,1,2). */\
1595 x64 = (x64 & 0x3333333333333333ULL) + ((x64 & 0xCCCCCCCCCCCCCCCCULL) >> 2),\
1596 /* x64 besteht aus 16 4-Bit-Zählern (0,1,2,3,4). */\
1597 x64 = (uint32)(x64 + (x64 >> 32)), \
1598 /* x64 besteht aus 8 4-Bit-Zählern (0,...,8). */\
1599 x64 = (x64 & 0x0F0F0F0FUL) + ((x64 & 0xF0F0F0F0UL) >> 4), \
1600 /* x64 besteht aus 4 8-Bit-Zählern (0,...,16). */\
1601 x64 = (x64 & 0x00FF00FFU) + ((x64 & 0xFF00FF00U) >> 8), \
1602 /* x64 besteht aus 2 16-Bit-Zählern (0,...,32). */\
1603 x64 = (x64 & 0x0000FFFFU) + (x64 >> 16) \
1604 /* x64 besteht aus 1 16-Bit-Zähler (0,...,64). */\
1609 #endif /* _CL_LOW_H */