xref: /linux/arch/m68k/lib/muldi3.c (revision 93d90ad708b8da6efc0e487b66111aa9db7f70c7)
1 /* muldi3.c extracted from gcc-2.7.2.3/libgcc2.c and
2 			   gcc-2.7.2.3/longlong.h which is: */
3 /* Copyright (C) 1989, 1992, 1993, 1994, 1995 Free Software Foundation, Inc.
4 
5 This file is part of GNU CC.
6 
7 GNU CC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
10 any later version.
11 
12 GNU CC 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 General Public License for more details.
16 
17 You should have received a copy of the GNU General Public License
18 along with GNU CC; see the file COPYING.  If not, write to
19 the Free Software Foundation, 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA.  */
21 
22 #ifdef CONFIG_CPU_HAS_NO_MULDIV64
23 
24 #define SI_TYPE_SIZE 32
25 #define __BITS4 (SI_TYPE_SIZE / 4)
26 #define __ll_B (1L << (SI_TYPE_SIZE / 2))
27 #define __ll_lowpart(t) ((USItype) (t) % __ll_B)
28 #define __ll_highpart(t) ((USItype) (t) / __ll_B)
29 
30 #define umul_ppmm(w1, w0, u, v)						\
31   do {									\
32     USItype __x0, __x1, __x2, __x3;					\
33     USItype __ul, __vl, __uh, __vh;					\
34 									\
35     __ul = __ll_lowpart (u);						\
36     __uh = __ll_highpart (u);						\
37     __vl = __ll_lowpart (v);						\
38     __vh = __ll_highpart (v);						\
39 									\
40     __x0 = (USItype) __ul * __vl;					\
41     __x1 = (USItype) __ul * __vh;					\
42     __x2 = (USItype) __uh * __vl;					\
43     __x3 = (USItype) __uh * __vh;					\
44 									\
45     __x1 += __ll_highpart (__x0);/* this can't give carry */		\
46     __x1 += __x2;		/* but this indeed can */		\
47     if (__x1 < __x2)		/* did we get it? */			\
48       __x3 += __ll_B;		/* yes, add it in the proper pos. */	\
49 									\
50     (w1) = __x3 + __ll_highpart (__x1);					\
51     (w0) = __ll_lowpart (__x1) * __ll_B + __ll_lowpart (__x0);		\
52   } while (0)
53 
54 #else
55 
56 #define umul_ppmm(w1, w0, u, v) \
57   __asm__ ("mulu%.l %3,%1:%0"						\
58            : "=d" ((USItype)(w0)),					\
59              "=d" ((USItype)(w1))					\
60            : "%0" ((USItype)(u)),					\
61              "dmi" ((USItype)(v)))
62 
63 #endif
64 
65 #define __umulsidi3(u, v) \
66   ({DIunion __w;							\
67     umul_ppmm (__w.s.high, __w.s.low, u, v);				\
68     __w.ll; })
69 
70 typedef 	 int SItype	__attribute__ ((mode (SI)));
71 typedef unsigned int USItype	__attribute__ ((mode (SI)));
72 typedef		 int DItype	__attribute__ ((mode (DI)));
73 typedef int word_type __attribute__ ((mode (__word__)));
74 
75 struct DIstruct {SItype high, low;};
76 
77 typedef union
78 {
79   struct DIstruct s;
80   DItype ll;
81 } DIunion;
82 
83 DItype
84 __muldi3 (DItype u, DItype v)
85 {
86   DIunion w;
87   DIunion uu, vv;
88 
89   uu.ll = u,
90   vv.ll = v;
91 
92   w.ll = __umulsidi3 (uu.s.low, vv.s.low);
93   w.s.high += ((USItype) uu.s.low * (USItype) vv.s.high
94 	       + (USItype) uu.s.high * (USItype) vv.s.low);
95 
96   return w.ll;
97 }
98