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