xref: /linux/arch/microblaze/lib/muldi3.c (revision c4ee0af3fa0dc65f690fc908f02b8355f9576ea0)
1 #include <linux/export.h>
2 
3 #include "libgcc.h"
4 
5 #define W_TYPE_SIZE 32
6 
7 #define __ll_B ((unsigned long) 1 << (W_TYPE_SIZE / 2))
8 #define __ll_lowpart(t) ((unsigned long) (t) & (__ll_B - 1))
9 #define __ll_highpart(t) ((unsigned long) (t) >> (W_TYPE_SIZE / 2))
10 
11 /* If we still don't have umul_ppmm, define it using plain C.  */
12 #if !defined(umul_ppmm)
13 #define umul_ppmm(w1, w0, u, v)						\
14 	do {								\
15 		unsigned long __x0, __x1, __x2, __x3;			\
16 		unsigned short __ul, __vl, __uh, __vh;			\
17 									\
18 		__ul = __ll_lowpart(u);					\
19 		__uh = __ll_highpart(u);				\
20 		__vl = __ll_lowpart(v);					\
21 		__vh = __ll_highpart(v);				\
22 									\
23 		__x0 = (unsigned long) __ul * __vl;			\
24 		__x1 = (unsigned long) __ul * __vh;			\
25 		__x2 = (unsigned long) __uh * __vl;			\
26 		__x3 = (unsigned long) __uh * __vh;			\
27 									\
28 		__x1 += __ll_highpart(__x0); /* this can't give carry */\
29 		__x1 += __x2; /* but this indeed can */			\
30 		if (__x1 < __x2) /* did we get it? */			\
31 		__x3 += __ll_B; /* yes, add it in the proper pos */	\
32 									\
33 		(w1) = __x3 + __ll_highpart(__x1);			\
34 		(w0) = __ll_lowpart(__x1) * __ll_B + __ll_lowpart(__x0);\
35 	} while (0)
36 #endif
37 
38 #if !defined(__umulsidi3)
39 #define __umulsidi3(u, v) ({				\
40 	DWunion __w;					\
41 	umul_ppmm(__w.s.high, __w.s.low, u, v);		\
42 	__w.ll;						\
43 	})
44 #endif
45 
46 long long __muldi3(long long u, long long v)
47 {
48 	const DWunion uu = {.ll = u};
49 	const DWunion vv = {.ll = v};
50 	DWunion w = {.ll = __umulsidi3(uu.s.low, vv.s.low)};
51 
52 	w.s.high += ((unsigned long) uu.s.low * (unsigned long) vv.s.high
53 		+ (unsigned long) uu.s.high * (unsigned long) vv.s.low);
54 
55 	return w.ll;
56 }
57 EXPORT_SYMBOL(__muldi3);
58