xref: /openbmc/u-boot/arch/microblaze/lib/muldi3.c (revision fd1e959e)
1 /*
2  * U-Boot - muldi3.c contains routines for mult and div
3  *
4  *
5  * SPDX-License-Identifier:	GPL-2.0+
6  */
7 
8 /* Generic function got from GNU gcc package, libgcc2.c */
9 #ifndef SI_TYPE_SIZE
10 #define SI_TYPE_SIZE 32
11 #endif
12 #define __ll_B (1L << (SI_TYPE_SIZE / 2))
13 #define __ll_lowpart(t) ((USItype) (t) % __ll_B)
14 #define __ll_highpart(t) ((USItype) (t) / __ll_B)
15 #define BITS_PER_UNIT 8
16 
17 #if !defined(umul_ppmm)
18 #define umul_ppmm(w1, w0, u, v)						\
19 	do {								\
20 	USItype __x0, __x1, __x2, __x3;					\
21 	USItype __ul, __vl, __uh, __vh;					\
22 									\
23 		__ul = __ll_lowpart(u);					\
24 		__uh = __ll_highpart(u);				\
25 		__vl = __ll_lowpart(v);					\
26 		__vh = __ll_highpart(v);				\
27 									\
28 	__x0 = (USItype) __ul * __vl;					\
29 	__x1 = (USItype) __ul * __vh;					\
30 	__x2 = (USItype) __uh * __vl;					\
31 	__x3 = (USItype) __uh * __vh;					\
32 									\
33 		__x1 += __ll_highpart(__x0); /* this can't give carry */\
34 		__x1 += __x2; /* but this indeed can */			\
35 		if (__x1 < __x2) /* did we get it? */			\
36 		__x3 += __ll_B;	/* yes, add it in the proper pos. */	\
37 									\
38 		(w1) = __x3 + __ll_highpart(__x1);			\
39 		(w0) = __ll_lowpart(__x1) * __ll_B + __ll_lowpart(__x0);\
40 	} while (0)
41 #endif
42 
43 #if !defined(__umulsidi3)
44 #define __umulsidi3(u, v)						\
45 	({DIunion __w;							\
46 	umul_ppmm(__w.s.high, __w.s.low, u, v);		\
47 	__w.ll; })
48 #endif
49 
50 typedef unsigned int USItype __attribute__ ((mode(SI)));
51 typedef int SItype __attribute__ ((mode(SI)));
52 typedef int DItype __attribute__ ((mode(DI)));
53 typedef int word_type __attribute__ ((mode(__word__)));
54 
55 struct DIstruct {
56 	SItype low, high;
57 };
58 typedef union {
59 	struct DIstruct s;
60 	DItype ll;
61 } DIunion;
62 
63 DItype __muldi3(DItype u, DItype v)
64 {
65 	DIunion w;
66 	DIunion uu, vv;
67 
68 	uu.ll = u, vv.ll = v;
69 	/*  panic("kernel panic for __muldi3"); */
70 	w.ll = __umulsidi3(uu.s.low, vv.s.low);
71 	w.s.high += ((USItype) uu.s.low * (USItype) vv.s.high
72 		     + (USItype) uu.s.high * (USItype) vv.s.low);
73 
74 	return w.ll;
75 }
76