1 /*-------------------------------------------------------------------------
2 _mulint.c :- routine for (unsigned) int (16 bit) multiplication
4 Written By - Sandeep Dutta . sandeep.dutta@usa.net (1999)
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 the
8 Free Software Foundation; either version 2, or (at your option) any
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.
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.
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 -------------------------------------------------------------------------*/
25 /* Signed and unsigned multiplication are the same - as long as the output
26 has the same precision as the input.
28 To do: _muluint and _mulsint should be replaced by _mulint.
30 bernhard@bernhardheld.de
32 Assembler-functions are provided for:
35 mcs51 small stack-auto
39 #if !defined(SDCC_USE_XSTACK) && !defined(_SDCC_NO_ASM_LIB_FUNCS)
40 # if defined(SDCC_ds390)
41 # if !defined(SDCC_STACK_AUTO)
42 # define _MULINT_ASM_LARGE
44 # elif defined(SDCC_mcs51)
45 # if defined(SDCC_MODEL_SMALL)
46 # if defined(SDCC_STACK_AUTO) && !defined(SDCC_PARMS_IN_BANK1)
47 # define _MULINT_ASM_SMALL_AUTO
49 # define _MULINT_ASM_SMALL
51 # else // must be SDCC_MODEL_LARGE
52 # if !defined(SDCC_STACK_AUTO)
53 # define _MULINT_ASM_LARGE
59 #ifdef _MULINT_ASM_LARGE
62 _muluint (unsigned int a, unsigned int b) // in future: _mulint
64 a*b; // hush the compiler
68 (char)(msb_a*lsb_b)<<8 +
69 (char)(lsb_a*msb_b)<<8
77 #if defined(SDCC_PARMS_IN_BANK1)
80 mov dptr,#__muluint_PARM_2
88 #if defined(SDCC_PARMS_IN_BANK1)
98 #if defined(SDCC_PARMS_IN_BANK1)
114 _mulsint (int a, int b) // obsolete
116 return _muluint (a, b);
119 #elif defined _MULINT_ASM_SMALL || defined _MULINT_ASM_SMALL_AUTO
122 _mulint_dummy (void) _naked
127 __muluint: ; obsolete
128 __mulsint: ; obsolete
131 .globl __muluint ; obsolete
132 .globl __mulsint ; obsolete
134 #if !defined(SDCC_STACK_AUTO) || defined(SDCC_PARMS_IN_BANK1)
136 #if defined(SDCC_NOOVERLAY)
139 .area OSEG (OVR,DATA)
141 #if defined(SDCC_PARMS_IN_BANK1)
145 #define bl (__mulint_PARM_2)
146 #define bh (__mulint_PARM_2 + 1)
148 __muluint_PARM_2: ; obsolete
149 __mulsint_PARM_2: ; obsolete
151 .globl __mulint_PARM_2
152 .globl __muluint_PARM_2 ; obsolete
153 .globl __mulsint_PARM_2 ; obsolete
160 ; globbered registers none
165 xch a,dpl ; 1 store low-byte of return value, fetch al
172 xch a,dph ; 1 ah -> acc
181 #else // SDCC_STACK_AUTO
183 ; globbered registers r0
185 mov a,#-2 ; 1 return address 2 bytes
187 mov r0,a ; 1 r0 points to bh
199 mov dpl,a ; 1 low-byte of return-value
203 xch a,dph ; 1 ah -> acc
212 #endif // SDCC_STACK_AUTO
220 struct { unsigned char lo,hi ;} s;
225 _muluint (unsigned int a, unsigned int b) // in future: _mulint
227 #if !defined(SDCC_STACK_AUTO) && (defined(SDCC_MODEL_LARGE) || defined(SDCC_ds390)) // still needed for large
231 x = (union uu xdata *)&a;
232 y = (union uu xdata *)&b;
234 register union uu *x;
235 register union uu *y;
241 t.t = x->s.lo * y->s.lo;
242 t.s.hi += (x->s.lo * y->s.hi) + (x->s.hi * y->s.lo);
248 _mulsint (int a, int b) // obsolete
250 return _muluint (a, b);