First pass at DS80C390 flat mode support
[fw/sdcc] / device / lib / _mululong.c
1 /*-------------------------------------------------------------------------
2    _mululong.c - routine for multiplication of 32 bit unsigned long
3
4              Written By -  Jean Louis VERN jlvern@writeme.com (1999)
5
6    This program is free software; you can redistribute it and/or modify it
7    under the terms of the GNU General Public License as published by the
8    Free Software Foundation; either version 2, or (at your option) any
9    later version.
10
11    This program 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 General Public License for more details.
15
16    You should have received a copy of the GNU 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
25 struct some_struct {
26         int a ;
27         char b;
28         long c ;};
29 union bil {
30         struct {unsigned char b0,b1,b2,b3 ;} b;
31         struct {unsigned int lo,hi ;} i;
32         unsigned long l;
33         struct { unsigned char b0; unsigned int i12; unsigned char b3;} bi;
34 } ;
35 #ifdef SDCC_MODEL_LARGE
36 #define bcast(x) ((union bil _xdata  *)&(x))
37 #else
38 #define bcast(x) ((union bil _near *)&(x))
39 #endif
40
41 /*
42                      3   2   1   0
43        X             3   2   1   0
44        ----------------------------
45                    0.3 0.2 0.1 0.0 
46                1.3 1.2 1.1 1.0 
47            2.3 2.2 2.1 2.0 
48        3.3 3.2 3.1 3.0 
49        ----------------------------
50                   |3.3|1.3|0.2|0.0|   A
51                     |2.3|0.3|0.1|     B
52                     |3.2|1.2|1.0|     C
53                       |2.2|1.1|       D
54                       |3.1|2.0|       E
55                         |2.1|         F
56                         |3.0|         G
57                           |-------> only this side 32 x 32 -> 32
58 */
59 unsigned long _mululong (unsigned long a, unsigned long b) 
60 {
61         union bil t;
62
63         t.i.hi = bcast(a)->b.b0 * bcast(b)->b.b2;       // A
64         t.i.lo = bcast(a)->b.b0 * bcast(b)->b.b0;       // A
65         t.b.b3 += (unsigned char)(bcast(a)->b.b3 *
66                                   bcast(b)->b.b0);      // G
67         t.b.b3 += (unsigned char)(bcast(a)->b.b2 *
68                                   bcast(b)->b.b1);      // F
69         t.i.hi += (unsigned int)(bcast(a)->b.b2 * bcast(b)->b.b0);      // E <- b lost in .lst
70         // bcast(a)->i.hi is free !
71         t.i.hi += (unsigned int)(bcast(a)->b.b1 * bcast(b)->b.b1);      // D <- b lost in .lst
72
73         bcast(a)->bi.b3 = (unsigned char)(bcast(a)->b.b1 *
74                                           bcast(b)->b.b2);
75         bcast(a)->bi.i12 = bcast(a)->b.b1 *
76                            bcast(b)->b.b0;              // C
77
78         bcast(b)->bi.b3 = (unsigned char)(bcast(a)->b.b0 *
79                                           bcast(b)->b.b3);
80         bcast(b)->bi.i12 = bcast(a)->b.b0 *
81                            bcast(b)->b.b1;              // B
82         bcast(b)->bi.b0 = 0;                            // B
83         bcast(a)->bi.b0 = 0;                            // C
84         t.l += a;
85
86         return t.l + b;
87 }