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