File: burnikel.c

package info (click to toggle)
numerix 0.22-3
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 4,380 kB
  • ctags: 4,165
  • sloc: asm: 26,210; ansic: 12,168; ml: 4,912; sh: 3,899; pascal: 414; makefile: 179
file content (132 lines) | stat: -rw-r--r-- 4,576 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
// file kernel/n/c/burnikel.c: Burnikel-Ziegler division
/*-----------------------------------------------------------------------+
 |  Copyright 2005-2006, Michel Quercia (michel.quercia@prepas.org)      |
 |                                                                       |
 |  This file is part of Numerix. Numerix is free software; you can      |
 |  redistribute it and/or modify it under the terms of the GNU Lesser   |
 |  General Public License as published by the Free Software Foundation; |
 |  either version 2.1 of the License, or (at your option) any later     |
 |  version.                                                             |
 |                                                                       |
 |  The Numerix Library is distributed in the hope that it will be       |
 |  useful, but WITHOUT ANY WARRANTY; without even the implied warranty  |
 |  of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU  |
 |  Lesser General Public License for more details.                      |
 |                                                                       |
 |  You should have received a copy of the GNU Lesser General Public     |
 |  License along with the GNU MP Library; see the file COPYING. If not, |
 |  write to the Free Software Foundation, Inc., 59 Temple Place -       |
 |  Suite 330, Boston, MA 02111-1307, USA.                               |
 +-----------------------------------------------------------------------+
 |                                                                       |
 |                     Division de Burnikel et Ziegler                   |
 |                                                                       |
 +-----------------------------------------------------------------------*/


                              /* +------------+
                                 |  Division  |
                                 +------------+ */

/*
  entre :
  a = naturel de longueur lc+lb
  b = naturel de longueur lb
  c = naturel de longueur lc

  contraintes :
  lb >= 2, lc > 0, le bit de poids fort de b est non nul,
  a < BASE^lc*b
  a,b,c non confondus

  sortie :
  a <- a mod b
  c <- floor(a/b)
*/


#ifndef assembly_sn_burnidiv
#ifdef debug_burnidiv
void xn(burnidiv_buggy)
#else
void xn(burnidiv)
#endif
(chiffre *a, long lc, chiffre *b, long lb, chiffre *c) {
  long p,q,r;
  chiffre *x;

  /* petite division => div_n2 */
  if ((lb <= burnidiv_lim) || (lc <= div_small_c_lim)) {xn(div_n2)(a,lc,b,lb,c); return;}

  /* cas rcursif, dcoupe b en deux et divise par tranches de q chiffres */
  p = lb/2; q = lb-p;
  r = lc%q; if (r == 0) r = q;
  x = xn(alloc_tmp)(lb);

  lc -= r; a += lc; c += lc;
  while(lc>=0) {

    /* divise a1 par b1 en tronquant le quotient  BASE^r - 1 */
    if (xn(cmp)(a+p+r,q,b+p,q)) xn(burnidiv)(a+p,r, b+p,q, c);
    else {
      xn(clear)(a+p+r,q);
      xn(inc)(a+p,q+r,b+p,q);
      xn(fill)(c,r);
    }

    /* a0 + BASE^p*r1 -= c*b0, et corrige si < 0 */
    if (r < p) xn(toommul)(b,p,c,r,x); else xn(toommul)(c,r,b,p,x);
    if (xn(dec)(a,lb+1,x,p+r)) {
      do xn(dec1)(c,r); while (xn(inc)(a,lb+1,b,lb) == 0);
    }

    /* tranche de q chiffres suivante */
    a -= q; c -= q; lc -= q; r = q;

  }

  xn(free_tmp)(x);
}
#endif /* assembly_sn_burnidiv */

                         /* +------------+
                            |  Contrle  |
                            +------------+ */

#ifdef debug_burnidiv
void xn(burnidiv_buggy)(chiffre *a, long la, chiffre *b, long lb, chiffre *c);
void xn(burnidiv)(chiffre *a, long lc, chiffre *b, long lb, chiffre *c) {
  long la = lc+lb;
  chiffre *x, *y;

  x = xn(alloc_tmp)(2*la); y = x+la;

  /* validit des longueurs ? */
  if ((lb < 2) || (la < lb))
    xn(internal_error)("error, burnidiv is called with lb < 2 or la < lb",0);

  /* le bit de poids fort de b est non nul ? */
  if ((b[lb-1] & BASE_2) == 0)
    xn(internal_error)("error, burnidiv is called with msb(b) = 0",0);

  /* a < BASE^lc*b ? */
  if (xn(cmp)(a+lc,lb,b,lb) >= 0)
    xn(internal_error)("error, burnidiv is called with a >= BASE^lc*b",2,a,la,b,lb);

  /* effectue la division douteuse */
  xn(move)(a,la,x);
  xn(burnidiv_buggy)(a,lc,b,lb,c);

  /* a_entre = a_sortie + b*c et a_sortie < b ?*/
  if (lc < lb) xn(toommul)(b,lb,c,lc,y); else xn(toommul)(c,lc,b,lb,y);
  if (     (xn(inc)(y,la,a,lb) != 0)
        || (xn(cmp)(x,la,y,la) != 0)
        || (xn(cmp)(a,la,b,lb) >= 0)) {
    xn(internal_error)("error in burnidiv", 4,x,la,b,lb,a,la,c,lc);
  }

  xn(free_tmp)(x);

}
#endif /* debug_burnidiv */