]> www.ginac.de Git - cln.git/blob - src/base/digit/cl_2D_div.cc
Initial revision
[cln.git] / src / base / digit / cl_2D_div.cc
1 // div2adic().
2
3 // General includes.
4 #include "cl_sysdep.h"
5
6 // Specification.
7 #include "cl_2D.h"
8
9
10 // Implementation.
11
12 uintD div2adic (uintD a, uintD b)
13 {
14 // Methode:
15 // Konstruiere c Bit für Bit.
16 // c := 0, d := a.
17 // Für j=0,...,intDsize:
18 //   [Hier b*c == a mod 2^j und d = (a-b*c)/2^j.] j=intDsize -> fertig.
19 //   Falls d ungerade, setze c:=c+2^j und d:=(d-b)/2, sonst d:=d/2.
20 // Ergebnis c.
21       ASSERT(!((b % 2) ==0))
22 #if 1
23      {var uintD c = 0;
24       var uintD bit_j = 1; // 2^j
25       loop // Verwende a als Variable d
26         { if (a & bit(0)) { c = c+bit_j; a = a-b; }
27           a = a>>1;
28           bit_j = bit_j << 1;
29           if (bit_j == 0) break; // j=intDsize -> fertig
30         }
31       return c;
32      }
33 #else
34      {var uintD bit_j = 1; // 2^j
35       var uintD b_j = b-1; // (b-1)*2^j
36       loop // Verwende a als Variable d*2^j+c
37         { if (a & bit_j) { a = a - b_j; }
38           b_j = b_j << 1; bit_j = bit_j << 1;
39           if (bit_j == 0) break; // j=intDsize -> fertig
40         }
41       return a;
42      }
43 #endif
44 }