Imported Upstream version 2.9.0
[debian/cc1111] / device / lib / hc08 / _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
7    This library is free software; you can redistribute it and/or modify it
8    under the terms of the GNU Library General Public License as published by the
9    Free Software Foundation; either version 2, or (at your option) any
10    later version.
11
12    This library is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU Library General Public License for more details.
16
17    You should have received a copy of the GNU Library General Public License
18    along with this program; if not, write to the Free Software
19    Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20
21    In other words, you are welcome to use, share and improve this program.
22    You are forbidden to forbid anyone else to use, share and improve
23    what you give them.   Help stamp out software-hoarding!
24 -------------------------------------------------------------------------*/
25
26 /* Signed and unsigned multiplication are the same - as long as the output
27    has the same precision as the input.
28
29    Assembler-functions are provided for:
30      mcs51 small
31      mcs51 small stack-auto
32 */
33
34
35 struct some_struct {
36         short a ;
37         char b;
38         long c ;};
39 union bil {
40         struct {unsigned char b3,b2,b1,b0 ;} b;
41         struct {unsigned short hi,lo ;} i;
42         unsigned long l;
43         struct { unsigned char b3; unsigned short i12; unsigned char b0;} bi;
44 } ;
45
46 #  define bcast(x) ((union bil *)&(x))
47
48 /*
49                      3   2   1   0
50        X             3   2   1   0
51        ----------------------------
52                    0.3 0.2 0.1 0.0
53                1.3 1.2 1.1 1.0
54            2.3 2.2 2.1 2.0
55        3.3 3.2 3.1 3.0
56        ----------------------------
57                   |3.3|1.3|0.2|0.0|   A
58                     |2.3|0.3|0.1|     B
59                     |3.2|1.2|1.0|     C
60                       |2.2|1.1|       D
61                       |3.1|2.0|       E
62                         |2.1|         F
63                         |3.0|         G
64                           |-------> only this side 32 x 32 -> 32
65 */
66 long
67 _mullong (long a, long b)
68 {
69         union bil t;
70
71         t.i.hi = bcast(a)->b.b0 * bcast(b)->b.b2;       // A
72         t.i.lo = bcast(a)->b.b0 * bcast(b)->b.b0;       // A
73         t.b.b3 += bcast(a)->b.b3 *
74                                   bcast(b)->b.b0;       // G
75         t.b.b3 += bcast(a)->b.b2 *
76                                   bcast(b)->b.b1;       // F
77         t.i.hi += bcast(a)->b.b2 * bcast(b)->b.b0;      // E <- b lost in .lst
78         // bcast(a)->i.hi is free !
79         t.i.hi += bcast(a)->b.b1 * bcast(b)->b.b1;      // D <- b lost in .lst
80
81         bcast(a)->bi.b3 = bcast(a)->b.b1 *
82                                           bcast(b)->b.b2;
83         bcast(a)->bi.i12 = bcast(a)->b.b1 *
84                            bcast(b)->b.b0;              // C
85
86         bcast(b)->bi.b3 = bcast(a)->b.b0 *
87                                           bcast(b)->b.b3;
88         bcast(b)->bi.i12 = bcast(a)->b.b0 *
89                            bcast(b)->b.b1;              // B
90         bcast(b)->bi.b0 = 0;                            // B
91         bcast(a)->bi.b0 = 0;                            // C
92         t.l += a;
93
94         return t.l + b;
95 }
96