0428133bd0be56f086c8238de6fa31016499b9dd
[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 #if defined(SDCC_MODEL_LARGE) || defined (SDCC_ds390)
36 #define bcast(x) ((union bil _xdata  *)&(x))
37 #elif defined(__z80) || defined(__gbz80)
38 #define bcast(x) ((union bil *)&(x))
39 #else
40 #define bcast(x) ((union bil _near *)&(x))
41 #endif
42
43 /*
44                      3   2   1   0
45        X             3   2   1   0
46        ----------------------------
47                    0.3 0.2 0.1 0.0 
48                1.3 1.2 1.1 1.0 
49            2.3 2.2 2.1 2.0 
50        3.3 3.2 3.1 3.0 
51        ----------------------------
52                   |3.3|1.3|0.2|0.0|   A
53                     |2.3|0.3|0.1|     B
54                     |3.2|1.2|1.0|     C
55                       |2.2|1.1|       D
56                       |3.1|2.0|       E
57                         |2.1|         F
58                         |3.0|         G
59                           |-------> only this side 32 x 32 -> 32
60 */
61
62 unsigned long _mululong (unsigned long a, unsigned long b) 
63 {
64         union bil t;
65
66         t.i.hi = bcast(a)->b.b0 * bcast(b)->b.b2;       // A
67         t.i.lo = bcast(a)->b.b0 * bcast(b)->b.b0;       // A
68         t.b.b3 += bcast(a)->b.b3 *
69                                   bcast(b)->b.b0;       // G
70         t.b.b3 += bcast(a)->b.b2 *
71                                   bcast(b)->b.b1;       // F
72         t.i.hi += bcast(a)->b.b2 * bcast(b)->b.b0;      // E <- b lost in .lst
73         // bcast(a)->i.hi is free !
74         t.i.hi += bcast(a)->b.b1 * bcast(b)->b.b1;      // D <- b lost in .lst
75
76         bcast(a)->bi.b3 = bcast(a)->b.b1 *
77                                           bcast(b)->b.b2;
78         bcast(a)->bi.i12 = bcast(a)->b.b1 *
79                            bcast(b)->b.b0;              // C
80
81         bcast(b)->bi.b3 = bcast(a)->b.b0 *
82                                           bcast(b)->b.b3;
83         bcast(b)->bi.i12 = bcast(a)->b.b0 *
84                            bcast(b)->b.b1;              // B
85         bcast(b)->bi.b0 = 0;                            // B
86         bcast(a)->bi.b0 = 0;                            // C
87         t.l += a;
88
89         return t.l + b;
90 }