* device/lib/Makefile.in: removed comment line with model-pic16,
[fw/sdcc] / 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 #define MSB_SET(x) ((x >> (8*sizeof(x)-1)) & 1)
29
30 unsigned char _divuchar (unsigned char a, unsigned char b)
31 {
32   unsigned char reste = 0;
33   unsigned char count = 8;
34     char c;
35
36   do
37   {
38     // reste: a <- 0;
39     c = MSB_SET(a);
40     a <<= 1;
41     reste <<= 1;
42     if (c)
43       reste |= 1;
44
45     if (reste >= b)
46     {
47       reste -= b;
48       // a <- (result = 1)
49       a |= 1;
50     }
51   }
52   while (--count);
53   return a;
54 }