ae9764c483f0e554d2004793e8588ae9c474cbdf
[fw/sdcc] / device / lib / pic16 / libsdcc / long / mullong.c
1 /*-------------------------------------------------------------------------
2    _mullong.c - routine for multiplication of 32 bit (unsigned) long
3
4              Written By -  Jean Louis VERN jlvern@writeme.com (1999)
5              Written By -  Sandeep Dutta . sandeep.dutta@usa.net (1999)
6              Modified for pic16 port by - Vangelis Rokas, vrokas@otenet.gr (2004)
7
8    This library is free software; you can redistribute it and/or modify it
9    under the terms of the GNU Library General Public License as published by the
10    Free Software Foundation; either version 2, or (at your option) any
11    later version.
12
13    This library is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16    GNU Library General Public License for more details.
17
18    You should have received a copy of the GNU Library General Public License
19    along with this program; if not, write to the Free Software
20    Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
21
22    In other words, you are welcome to use, share and improve this program.
23    You are forbidden to forbid anyone else to use, share and improve
24    what you give them.   Help stamp out software-hoarding!
25 -------------------------------------------------------------------------*/
26
27 union bil {
28         struct {unsigned char b0,b1,b2,b3 ;} b;
29         struct {unsigned short lo,hi ;} i;
30         unsigned long l;
31         struct { unsigned char b0; unsigned short i12; unsigned char b3;} bi;
32 };
33
34 #define bcast(x) ((union bil near  *)&(x))
35
36 /*
37                      3   2   1   0
38        X             3   2   1   0
39        ----------------------------
40                    0.3 0.2 0.1 0.0
41                1.3 1.2 1.1 1.0
42            2.3 2.2 2.1 2.0
43        3.3 3.2 3.1 3.0
44        ----------------------------
45                   |3.3|1.3|0.2|0.0|   A
46                     |2.3|0.3|0.1|     B
47                     |3.2|1.2|1.0|     C
48                       |2.2|1.1|       D
49                       |3.1|2.0|       E
50                         |2.1|         F
51                         |3.0|         G
52                           |-------> only this side 32 x 32 -> 32
53 */
54
55 long _mullong (long a, long b)
56 {
57 #if 0
58         union bil t;
59
60         t.i.hi = bcast(a)->b.b0 * bcast(b)->b.b2;       // A
61         t.i.lo = bcast(a)->b.b0 * bcast(b)->b.b0;       // A
62         t.b.b3 += bcast(a)->b.b3 *
63                                   bcast(b)->b.b0;       // G
64         t.b.b3 += bcast(a)->b.b2 *
65                                   bcast(b)->b.b1;       // F
66         t.i.hi += bcast(a)->b.b2 * bcast(b)->b.b0;      // E <- b lost in .lst
67         // bcast(a)->i.hi is free !
68         t.i.hi += bcast(a)->b.b1 * bcast(b)->b.b1;      // D <- b lost in .lst
69
70         bcast(a)->bi.b3 = bcast(a)->b.b1 *
71                                           bcast(b)->b.b2;
72         bcast(a)->bi.i12 = bcast(a)->b.b1 *
73                            bcast(b)->b.b0;              // C
74
75         bcast(b)->bi.b3 = bcast(a)->b.b0 *
76                                           bcast(b)->b.b3;
77         bcast(b)->bi.i12 = bcast(a)->b.b0 *
78                            bcast(b)->b.b1;              // B
79         bcast(b)->bi.b0 = 0;                            // B
80         bcast(a)->bi.b0 = 0;                            // C
81         t.l += a;
82
83         return t.l + b;
84 #else
85
86   union bil x;
87   union bil y;
88   union bil t;
89   union bil t1, t2;
90   
91         x.l = a;
92         y.l = b;
93         
94         t.i.hi = x.b.b0 * y.b.b2;
95         t.i.lo = x.b.b0 * y.b.b0;
96         
97         t.b.b3 += x.b.b3 * y.b.b0;
98         t.b.b3 += x.b.b2 * y.b.b1;
99         
100         t.i.hi += x.b.b2 * y.b.b0;
101         t.i.hi += x.b.b1 * y.b.b1;
102         
103         t1.bi.b3 = x.b.b1 * y.b.b2;
104         t1.bi.i12 = x.b.b1 * y.b.b0;
105         
106         t2.bi.b3 = x.b.b0 * y.b.b3;
107         t2.bi.i12 = x.b.b0 * y.b.b1;
108         
109         t1.bi.b0 = 0;
110         t2.bi.b0 = 0;
111         t.l += t1.l;
112
113   return (t.l + t2.l);
114
115 #endif
116 }