Imported Upstream version 2.9.0
[debian/cc1111] / device / lib / pic16 / libsdcc / char / divuchar.c
1 /*-------------------------------------------------------------------------
2   _divuchar.c :- routine for unsigned char (8 bit) division
3
4              Ecrit par -  Jean-Louis Vern . jlvern@writeme.com (1999)
5              Adopted for char (8-bit) and pic16 port by
6                         - Vangelis Rokas, vrokas@otenet.gr (2004)
7                                       
8    This library is free software; you can redistribute it and/or modify it
9    under the terms of the GNU Library General Public License as published by the
10    Free Software Foundation; either version 2, or (at your option) any
11    later version.
12
13    This library is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16    GNU Library General Public License for more details.
17
18    You should have received a copy of the GNU Library General Public License
19    along with this program; if not, write to the Free Software
20    Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
21
22    In other words, you are welcome to use, share and improve this program.
23    You are forbidden to forbid anyone else to use, share and improve
24    what you give them.   Help stamp out software-hoarding!
25 -------------------------------------------------------------------------*/
26
27 /*
28 ** $Id: divuchar.c 3711 2005-03-31 16:25:17Z vrokas $
29 */
30
31 #include <sdcc-lib.h>
32
33 //#define MSB_SET(x)    ((x >> (8*sizeof(x)-1)) & 1)
34 #define MSB_SET(x)      (x & 0x80)
35
36 unsigned char _divuchar (unsigned char a, unsigned char b) _IL_REENTRANT
37 {
38   unsigned char reste = 0;
39   unsigned char count = 8;
40   char c;
41
42   do
43   {
44     // reste: a <- 0;
45     c = MSB_SET(a);
46     a <<= 1;
47     reste <<= 1;
48     if (c)
49       reste |= 1;
50
51     if (reste >= b)
52     {
53       reste -= b;
54
55       // a <- (result = 1)
56       a |= 1;
57     }
58   }
59   while (--count);
60
61   return a;
62 }