Imported Upstream version 2.9.0
[debian/cc1111] / 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 #include <sdcc-lib.h>
28
29 union bil {
30         struct {unsigned char b0,b1,b2,b3 ;} b;
31         struct {unsigned short lo,hi ;} i;
32         unsigned long l;
33         struct { unsigned char b0; unsigned short i12; unsigned char b3;} bi;
34 };
35
36 #define bcast(x) ((union bil near  *)&(x))
37
38 /*
39                      3   2   1   0
40        X             3   2   1   0
41        ----------------------------
42                    0.3 0.2 0.1 0.0
43                1.3 1.2 1.1 1.0
44            2.3 2.2 2.1 2.0
45        3.3 3.2 3.1 3.0
46        ----------------------------
47                   |3.3|1.3|0.2|0.0|   A
48                     |2.3|0.3|0.1|     B
49                     |3.2|1.2|1.0|     C
50                       |2.2|1.1|       D
51                       |3.1|2.0|       E
52                         |2.1|         F
53                         |3.0|         G
54                           |-------> only this side 32 x 32 -> 32
55 */
56
57 long _mullong (long a, long b) _IL_REENTRANT
58 {
59 #if 0
60         union bil t;
61
62         t.i.hi = bcast(a)->b.b0 * bcast(b)->b.b2;       // A
63         t.i.lo = bcast(a)->b.b0 * bcast(b)->b.b0;       // A
64         t.b.b3 += bcast(a)->b.b3 *
65                                   bcast(b)->b.b0;       // G
66         t.b.b3 += bcast(a)->b.b2 *
67                                   bcast(b)->b.b1;       // F
68         t.i.hi += bcast(a)->b.b2 * bcast(b)->b.b0;      // E <- b lost in .lst
69         // bcast(a)->i.hi is free !
70         t.i.hi += bcast(a)->b.b1 * bcast(b)->b.b1;      // D <- b lost in .lst
71
72         bcast(a)->bi.b3 = bcast(a)->b.b1 *
73                                           bcast(b)->b.b2;
74         bcast(a)->bi.i12 = bcast(a)->b.b1 *
75                            bcast(b)->b.b0;              // C
76
77         bcast(b)->bi.b3 = bcast(a)->b.b0 *
78                                           bcast(b)->b.b3;
79         bcast(b)->bi.i12 = bcast(a)->b.b0 *
80                            bcast(b)->b.b1;              // B
81         bcast(b)->bi.b0 = 0;                            // B
82         bcast(a)->bi.b0 = 0;                            // C
83         t.l += a;
84
85         return t.l + b;
86 #else
87
88   union bil x;
89   union bil y;
90   union bil t;
91   union bil t1, t2;
92   
93         x.l = a;
94         y.l = b;
95         
96         t.i.hi = x.b.b0 * y.b.b2;
97         t.i.lo = x.b.b0 * y.b.b0;
98         
99         t.b.b3 += x.b.b3 * y.b.b0;
100         t.b.b3 += x.b.b2 * y.b.b1;
101         
102         t.i.hi += x.b.b2 * y.b.b0;
103         t.i.hi += x.b.b1 * y.b.b1;
104         
105         t1.bi.b3 = x.b.b1 * y.b.b2;
106         t1.bi.i12 = x.b.b1 * y.b.b0;
107         
108         t2.bi.b3 = x.b.b0 * y.b.b3;
109         t2.bi.i12 = x.b.b0 * y.b.b1;
110         
111         t1.bi.b0 = 0;
112         t2.bi.b0 = 0;
113         t.l += t1.l;
114
115   return (t.l + t2.l);
116
117 #endif
118 }