PIPS
divide.c
Go to the documentation of this file.
1 /*
2 
3  $Id: divide.c 1669 2019-06-26 17:24:57Z coelho $
4 
5  Copyright 1989-2016 MINES ParisTech
6 
7  This file is part of Linear/C3 Library.
8 
9  Linear/C3 Library is free software: you can redistribute it and/or modify it
10  under the terms of the GNU Lesser General Public License as published by
11  the Free Software Foundation, either version 3 of the License, or
12  any later version.
13 
14  Linear/C3 Library is distributed in the hope that it will be useful, but WITHOUT ANY
15  WARRANTY; without even the implied warranty of MERCHANTABILITY or
16  FITNESS FOR A PARTICULAR PURPOSE.
17 
18  See the GNU Lesser General Public License for more details.
19 
20  You should have received a copy of the GNU Lesser General Public License
21  along with Linear/C3 Library. If not, see <http://www.gnu.org/licenses/>.
22 
23 */
24 
25 /* package arithmetique
26  */
27 
28 /*LINTLIBRARY*/
29 #ifdef HAVE_CONFIG_H
30  #include "config.h"
31 #endif
32 #include <stdlib.h>
33 #include <stdio.h>
34 
35 #include "linear_assert.h"
36 #include "arithmetique.h"
37 
38 /* int divide(int a, int b): calcul du divide de a par b;
39  * le reste (qui n'est pas retourne) est toujours positif; il
40  * est fourni par la fonction modulo()
41  *
42  * Il y a quatre configuration de signe a traiter:
43  * 1. a>0 && b>0: a / b
44  * 2. a<0 && b>0: (a-b+1) / b
45  * 3. a>0 && b<0: cf. 1. apres changement de signe de b, puis changement
46  * de signe du resultat
47  * 4. a<0 && b<0: cf. 2. apres changement de signe de b, puis changement
48  * de signe du resultat
49  * 5. a==0: 0
50  */
52 {
53  /* definition d'une look-up table pour les valeurs de a appartenant
54  a [-DIVIDE_MAX_A..DIVIDE_MAX_A] et pour les valeurs de b
55  appartenant a [1..DIVIDE_MAX_B] (en fait [-DIVIDE_MAX_B..DIVIDE_MAX_B]
56  a cause du changement de signe)
57 
58  Serait-il utile d'ajouter une test b==1 pour supprimer une colonne?
59 
60  Serait-il utile de tester b > a pour renvoyer 0 ou -1 tout de suite?
61  */
62 
63 #define DIVIDE_MAX_A 7
64 #define DIVIDE_MAX_B 8
65 
66  static Value
67  divide_look_up[2*DIVIDE_MAX_A+1][DIVIDE_MAX_B]={
68  /* b == 1 2 3 4 5 6 7 8 */
69  {/* a == - 7 */ -7, -4, -3, -2, -2, -2, -1, -1},
70  {/* a == - 6 */ -6, -3, -2, -2, -2, -1, -1, -1},
71  {/* a == - 5 */ -5, -3, -2, -2, -1, -1, -1, -1},
72  {/* a == - 4 */ -4, -2, -2, -2, -1, -1, -1, -1},
73  {/* a == - 3 */ -3, -2, -1, -1, -1, -1, -1, -1},
74  {/* a == - 2 */ -2, -1, -1, -1, -1, -1, -1, -1},
75  {/* a == - 1 */ -1, -1, -1, -1, -1, -1, -1, -1},
76  {/* a == 0 */ 0, 0, 0, 0, 0, 0, 0, 0},
77  {/* a == 1 */ 1, 0, 0, 0, 0, 0, 0, 0},
78  {/* a == 2 */ 2, 1, 0, 0, 0, 0, 0, 0},
79  {/* a == 3 */ 3, 1, 1, 0, 0, 0, 0, 0},
80  {/* a == 4 */ 4, 2, 1, 1, 0, 0, 0, 0},
81  {/* a == 5 */ 5, 2, 1, 1, 1, 0, 0, 0},
82  {/* a == 6 */ 6, 3, 2, 1, 1, 1, 0, 0},
83  {/* a == 7 */ 7, 3, 2, 1, 1, 1, 1, 0}
84  };
85  /* translation de a pour acces a la look-up table par indice positif:
86  la == a + DIVIDE_MAX_A >= 0 */
87 
88  Value quotient; /* valeur du quotient C */
89 
91 
92  /* serait-il utile d'optimiser la division de a=0 par b? Ou bien
93  cette routine n'est-elle jamais appelee avec a=0 par le package vecteur?
94  */
95 
100  {
101  /* direct table look up */
102  int bint = VALUE_TO_INT(b),
103  la = VALUE_TO_INT(a)+DIVIDE_MAX_A; /* shift a for the table */
104  quotient = (bint>0)?
105  divide_look_up[la][bint-1]:
106  value_uminus(divide_look_up[la][(-bint)-1]);
107  }
108  else
109  quotient = value_pdiv(a,b); /* this is just divide_slow */
110 
111  return quotient;
112 }
113 
115 {
116  return value_pdiv(a, b);
117 }
#define int_to_value(i)
end LINEAR_VALUE_IS_INT
#define VALUE_TO_INT(val)
#define value_pdiv(v1, v2)
#define value_le(v1, v2)
#define value_notzero_p(val)
#define value_uminus(val)
unary operators on values
int Value
#define value_ge(v1, v2)
Value divide_slow(Value a, Value b)
Definition: divide.c:114
#define DIVIDE_MAX_B
Value divide_fast(Value a, Value b)
package arithmetique
Definition: divide.c:51
#define DIVIDE_MAX_A
#define assert(ex)
Definition: newgen_assert.h:41