Imported Upstream version 2.9.0
[debian/cc1111] / device / lib / pic / libsdcc / _mulint.c
1 /* ---------------------------------------------------------------------------
2    _mulint.c : routine for 16 bit multiplication
3
4         Written By      Raphael Neider <rneider AT web.de> (2005)
5
6    This library is free software; you can redistribute it and/or modify it
7    under the terms of the GNU Library General Public License as published by
8    the Free Software Foundation; either version 2, or (at your option) any
9    later version.
10
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
14    GNU Library General Public License for more details.
15
16    You should have received a copy of the GNU Library General Public License
17    along with this program; if not, write to the Free Software
18    Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19
20    In other words, you are welcome to use, share and improve this program.
21    You are forbidden to forbid anyone else to use, share and improve
22    what you give them.   Help stamp out software-hoarding!
23
24    $Id: _mulint.c 4148 2006-05-01 20:47:12Z tecodev $
25    ------------------------------------------------------------------------ */
26
27 #pragma save
28 #pragma disable_warning 126 /* unreachable code */
29 #pragma disable_warning 116 /* left shifting more than size of object */
30 int
31 _mulint (int a, int b)
32 {
33   int result = 0;
34   unsigned char i;
35
36   /* check all bits in a byte */
37   for (i = 0; i < 8u; i++) {
38     /* check all bytes in operand (generic code, optimized by the compiler) */
39     if (a & 0x0001u) result += b;
40     if (sizeof (a) > 1 && (a & 0x00000100ul)) result += (b << 8u);
41     if (sizeof (a) > 2 && (a & 0x00010000ul)) result += (b << 16u);
42     if (sizeof (a) > 3 && (a & 0x01000000ul)) result += (b << 24u);
43     a = ((unsigned int)a) >> 1u;
44     b <<= 1u;
45   } // for i
46
47   return result;
48 }
49 #pragma restore