1 /* ---------------------------------------------------------------------------
2 _divuchar.c : 8 bit division routines for pic14 devices
4 Written By Raphael Neider <rneider AT web.de> (2005)
6 This library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Library General Public
8 License as published by the Free Software Foundation; either
9 version 2 of the License, or (at your option) any later version.
11 This library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Library General Public License for more details.
16 You should have received a copy of the GNU Library General Public
17 License along with this library; if not, write to the
18 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307 USA.
21 In other words, you are welcome to use, share and improve this program.
22 You are forbidden to forbid anyone else to use, share and improve
23 what you give them. Help stamp out software-hoarding!
26 ------------------------------------------------------------------------ */
29 _divuchar (unsigned char a, unsigned char b)
31 unsigned char result = 0;
32 unsigned char mask = 0x01;
34 /* prevent endless loop (division by zero exception?!?) */
35 if (!b) return (unsigned char)-1;
37 /* it would suffice to make b >= a, but that test is
38 * more complex and will fail if a has its MSB set */
39 while (!(b & (1UL << (8*sizeof(unsigned char)-1)))) {
44 /* now add up the powers of two (of b) that "fit" into a */
45 /* we might stop if (a == 0), but that's an additional test in every iteration... */