]> git.gag.com Git - fw/sdcc/blob - device/lib/_muluint.c
Temporary (?) fix for char calculation in int space
[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 program is free software; you can redistribute it and/or modify it
8    under the terms of the GNU 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 program 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 General Public License for more details.
16    
17    You should have received a copy of the GNU 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=(int)(lsb_a*lsb_b) + (char)(msb_a*msb_b)<<8
33   //          ^^^
34
35   _asm 
36     push dph ; msb_a
37     mov b,dpl ; lsb_a
38     mov dptr,#__muluint_PARM_2
39     movx a,@dptr ; lsb_b
40     mul ab
41     mov r0,a
42     mov r1,b
43     pop b ; msb_a
44     inc dptr
45     movx a,@dptr ; msb_b
46     mul ab
47     add a,r1
48     mov dph,a
49     mov dpl,r0
50     ret
51   _endasm;
52 }
53
54 #else
55
56 union uu {
57         struct { unsigned short lo,hi ;} s;
58         unsigned int t;
59 } ;
60
61 unsigned int _muluint (unsigned int a, unsigned int b) 
62 {
63 #ifdef SDCC_MODEL_LARGE
64         union uu _xdata *x;
65         union uu _xdata *y; 
66         union uu t;
67         x = (union uu _xdata *)&a;
68         y = (union uu _xdata *)&b;
69 #else
70         register union uu _near *x;
71         register union uu _near *y; 
72         union uu t;
73         x = (union uu _near *)&a;
74         y = (union uu _near *)&b;
75 #endif
76
77         t.t = x->s.lo * y->s.lo;
78         t.s.hi += (x->s.lo * y->s.hi) + (x->s.hi * y->s.lo);
79
80        return t.t;
81
82
83 #endif