4b2d11c7a0e1d4e4a47b47ca8f4116c88b35478b
[fw/sdcc] / device / lib / _muluint.c
1 /*-------------------------------------------------------------------------
2
3   _muluint.c :- routine for unsigned int (16 bit) multiplication               
4
5              Written By -  Sandeep Dutta . sandeep.dutta@usa.net (1999)
6
7    This library is free software; you can redistribute it and/or modify it
8    under the terms of the GNU Library General Public License as published by the
9    Free Software Foundation; either version 2, or (at your option) any
10    later version.
11    
12    This library is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU Library General Public License for more details.
16    
17    You should have received a copy of the GNU Library General Public License
18    along with this program; if not, write to the Free Software
19    Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20    
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!  
24 -------------------------------------------------------------------------*/
25
26 #ifdef SDCC_MODEL_FLAT24
27
28 unsigned int _muluint (unsigned int a, unsigned int b) 
29 {
30   a*b; // hush the compiler
31
32   /* muluint=
33       (int)(lsb_a*lsb_b) +
34       (char)(msb_a*lsb_b)<<8 +
35       (char)(lsb_a*msb_b)<<8
36   */
37
38   _asm 
39     mov r2,dph ; msb_a
40     mov r3,dpl ; lsb_a
41
42     mov b,r3 ; lsb_a
43     mov dptr,#__muluint_PARM_2
44     movx a,@dptr ; lsb_b
45     mul ab ; lsb_a*lsb_b
46     mov r0,a
47     mov r1,b
48
49     mov b,r2 ; msb_a
50     movx a,@dptr ; lsb_b
51     mul ab ; msb_a*lsb_b
52     add a,r1
53     mov r1,a
54
55     mov b,r3 ; lsb_a
56     inc dptr
57     movx a,@dptr ; msb_b
58     mul ab ; lsb_a*msb_b
59     add a,r1
60
61     mov dph,a
62     mov dpl,r0
63     ret
64   _endasm;
65 }
66
67 #else
68
69 union uu {
70         struct { unsigned short lo,hi ;} s;
71         unsigned int t;
72 } ;
73
74 unsigned int _muluint (unsigned int a, unsigned int b) 
75 {
76 #ifdef SDCC_MODEL_LARGE
77         union uu _xdata *x;
78         union uu _xdata *y; 
79         union uu t;
80         x = (union uu _xdata *)&a;
81         y = (union uu _xdata *)&b;
82 #else
83         register union uu _near *x;
84         register union uu _near *y; 
85         union uu t;
86         x = (union uu _near *)&a;
87         y = (union uu _near *)&b;
88 #endif
89
90         t.t = x->s.lo * y->s.lo;
91         t.s.hi += (x->s.lo * y->s.hi) + (x->s.hi * y->s.lo);
92
93        return t.t;
94
95
96 #endif