xref: /OK3568_Linux_fs/kernel/arch/m68k/lib/muldi3.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun /* muldi3.c extracted from gcc-2.7.2.3/libgcc2.c and
2*4882a593Smuzhiyun 			   gcc-2.7.2.3/longlong.h which is: */
3*4882a593Smuzhiyun /* Copyright (C) 1989, 1992, 1993, 1994, 1995 Free Software Foundation, Inc.
4*4882a593Smuzhiyun 
5*4882a593Smuzhiyun This file is part of GNU CC.
6*4882a593Smuzhiyun 
7*4882a593Smuzhiyun GNU CC is free software; you can redistribute it and/or modify
8*4882a593Smuzhiyun it under the terms of the GNU General Public License as published by
9*4882a593Smuzhiyun the Free Software Foundation; either version 2, or (at your option)
10*4882a593Smuzhiyun any later version.
11*4882a593Smuzhiyun 
12*4882a593Smuzhiyun GNU CC is distributed in the hope that it will be useful,
13*4882a593Smuzhiyun but WITHOUT ANY WARRANTY; without even the implied warranty of
14*4882a593Smuzhiyun MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15*4882a593Smuzhiyun GNU General Public License for more details. */
16*4882a593Smuzhiyun 
17*4882a593Smuzhiyun #include <linux/compiler.h>
18*4882a593Smuzhiyun #include <linux/export.h>
19*4882a593Smuzhiyun 
20*4882a593Smuzhiyun #ifdef CONFIG_CPU_HAS_NO_MULDIV64
21*4882a593Smuzhiyun 
22*4882a593Smuzhiyun #define SI_TYPE_SIZE 32
23*4882a593Smuzhiyun #define __BITS4 (SI_TYPE_SIZE / 4)
24*4882a593Smuzhiyun #define __ll_B (1L << (SI_TYPE_SIZE / 2))
25*4882a593Smuzhiyun #define __ll_lowpart(t) ((USItype) (t) % __ll_B)
26*4882a593Smuzhiyun #define __ll_highpart(t) ((USItype) (t) / __ll_B)
27*4882a593Smuzhiyun 
28*4882a593Smuzhiyun #define umul_ppmm(w1, w0, u, v)						\
29*4882a593Smuzhiyun   do {									\
30*4882a593Smuzhiyun     USItype __x0, __x1, __x2, __x3;					\
31*4882a593Smuzhiyun     USItype __ul, __vl, __uh, __vh;					\
32*4882a593Smuzhiyun 									\
33*4882a593Smuzhiyun     __ul = __ll_lowpart (u);						\
34*4882a593Smuzhiyun     __uh = __ll_highpart (u);						\
35*4882a593Smuzhiyun     __vl = __ll_lowpart (v);						\
36*4882a593Smuzhiyun     __vh = __ll_highpart (v);						\
37*4882a593Smuzhiyun 									\
38*4882a593Smuzhiyun     __x0 = (USItype) __ul * __vl;					\
39*4882a593Smuzhiyun     __x1 = (USItype) __ul * __vh;					\
40*4882a593Smuzhiyun     __x2 = (USItype) __uh * __vl;					\
41*4882a593Smuzhiyun     __x3 = (USItype) __uh * __vh;					\
42*4882a593Smuzhiyun 									\
43*4882a593Smuzhiyun     __x1 += __ll_highpart (__x0);/* this can't give carry */		\
44*4882a593Smuzhiyun     __x1 += __x2;		/* but this indeed can */		\
45*4882a593Smuzhiyun     if (__x1 < __x2)		/* did we get it? */			\
46*4882a593Smuzhiyun       __x3 += __ll_B;		/* yes, add it in the proper pos. */	\
47*4882a593Smuzhiyun 									\
48*4882a593Smuzhiyun     (w1) = __x3 + __ll_highpart (__x1);					\
49*4882a593Smuzhiyun     (w0) = __ll_lowpart (__x1) * __ll_B + __ll_lowpart (__x0);		\
50*4882a593Smuzhiyun   } while (0)
51*4882a593Smuzhiyun 
52*4882a593Smuzhiyun #else
53*4882a593Smuzhiyun 
54*4882a593Smuzhiyun #define umul_ppmm(w1, w0, u, v) \
55*4882a593Smuzhiyun   __asm__ ("mulu%.l %3,%1:%0"						\
56*4882a593Smuzhiyun            : "=d" ((USItype)(w0)),					\
57*4882a593Smuzhiyun              "=d" ((USItype)(w1))					\
58*4882a593Smuzhiyun            : "%0" ((USItype)(u)),					\
59*4882a593Smuzhiyun              "dmi" ((USItype)(v)))
60*4882a593Smuzhiyun 
61*4882a593Smuzhiyun #endif
62*4882a593Smuzhiyun 
63*4882a593Smuzhiyun #define __umulsidi3(u, v) \
64*4882a593Smuzhiyun   ({DIunion __w;							\
65*4882a593Smuzhiyun     umul_ppmm (__w.s.high, __w.s.low, u, v);				\
66*4882a593Smuzhiyun     __w.ll; })
67*4882a593Smuzhiyun 
68*4882a593Smuzhiyun typedef 	 int SItype	__mode(SI);
69*4882a593Smuzhiyun typedef unsigned int USItype	__mode(SI);
70*4882a593Smuzhiyun typedef		 int DItype	__mode(DI);
71*4882a593Smuzhiyun typedef int word_type           __mode(__word__);
72*4882a593Smuzhiyun 
73*4882a593Smuzhiyun struct DIstruct {SItype high, low;};
74*4882a593Smuzhiyun 
75*4882a593Smuzhiyun typedef union
76*4882a593Smuzhiyun {
77*4882a593Smuzhiyun   struct DIstruct s;
78*4882a593Smuzhiyun   DItype ll;
79*4882a593Smuzhiyun } DIunion;
80*4882a593Smuzhiyun 
81*4882a593Smuzhiyun DItype
__muldi3(DItype u,DItype v)82*4882a593Smuzhiyun __muldi3 (DItype u, DItype v)
83*4882a593Smuzhiyun {
84*4882a593Smuzhiyun   DIunion w;
85*4882a593Smuzhiyun   DIunion uu, vv;
86*4882a593Smuzhiyun 
87*4882a593Smuzhiyun   uu.ll = u,
88*4882a593Smuzhiyun   vv.ll = v;
89*4882a593Smuzhiyun 
90*4882a593Smuzhiyun   w.ll = __umulsidi3 (uu.s.low, vv.s.low);
91*4882a593Smuzhiyun   w.s.high += ((USItype) uu.s.low * (USItype) vv.s.high
92*4882a593Smuzhiyun 	       + (USItype) uu.s.high * (USItype) vv.s.low);
93*4882a593Smuzhiyun 
94*4882a593Smuzhiyun   return w.ll;
95*4882a593Smuzhiyun }
96*4882a593Smuzhiyun EXPORT_SYMBOL(__muldi3);
97