12281ba51SDavid Holsgrove /*
2*a187559eSBin Meng * U-Boot - muldi3.c contains routines for mult and div
32281ba51SDavid Holsgrove *
42281ba51SDavid Holsgrove *
51a459660SWolfgang Denk * SPDX-License-Identifier: GPL-2.0+
62281ba51SDavid Holsgrove */
72281ba51SDavid Holsgrove
82281ba51SDavid Holsgrove /* Generic function got from GNU gcc package, libgcc2.c */
92281ba51SDavid Holsgrove #ifndef SI_TYPE_SIZE
102281ba51SDavid Holsgrove #define SI_TYPE_SIZE 32
112281ba51SDavid Holsgrove #endif
122281ba51SDavid Holsgrove #define __ll_B (1L << (SI_TYPE_SIZE / 2))
132281ba51SDavid Holsgrove #define __ll_lowpart(t) ((USItype) (t) % __ll_B)
142281ba51SDavid Holsgrove #define __ll_highpart(t) ((USItype) (t) / __ll_B)
152281ba51SDavid Holsgrove #define BITS_PER_UNIT 8
162281ba51SDavid Holsgrove
172281ba51SDavid Holsgrove #if !defined(umul_ppmm)
182281ba51SDavid Holsgrove #define umul_ppmm(w1, w0, u, v) \
192281ba51SDavid Holsgrove do { \
202281ba51SDavid Holsgrove USItype __x0, __x1, __x2, __x3; \
212281ba51SDavid Holsgrove USItype __ul, __vl, __uh, __vh; \
222281ba51SDavid Holsgrove \
232281ba51SDavid Holsgrove __ul = __ll_lowpart(u); \
242281ba51SDavid Holsgrove __uh = __ll_highpart(u); \
252281ba51SDavid Holsgrove __vl = __ll_lowpart(v); \
262281ba51SDavid Holsgrove __vh = __ll_highpart(v); \
272281ba51SDavid Holsgrove \
282281ba51SDavid Holsgrove __x0 = (USItype) __ul * __vl; \
292281ba51SDavid Holsgrove __x1 = (USItype) __ul * __vh; \
302281ba51SDavid Holsgrove __x2 = (USItype) __uh * __vl; \
312281ba51SDavid Holsgrove __x3 = (USItype) __uh * __vh; \
322281ba51SDavid Holsgrove \
332281ba51SDavid Holsgrove __x1 += __ll_highpart(__x0); /* this can't give carry */\
342281ba51SDavid Holsgrove __x1 += __x2; /* but this indeed can */ \
352281ba51SDavid Holsgrove if (__x1 < __x2) /* did we get it? */ \
362281ba51SDavid Holsgrove __x3 += __ll_B; /* yes, add it in the proper pos. */ \
372281ba51SDavid Holsgrove \
382281ba51SDavid Holsgrove (w1) = __x3 + __ll_highpart(__x1); \
392281ba51SDavid Holsgrove (w0) = __ll_lowpart(__x1) * __ll_B + __ll_lowpart(__x0);\
402281ba51SDavid Holsgrove } while (0)
412281ba51SDavid Holsgrove #endif
422281ba51SDavid Holsgrove
432281ba51SDavid Holsgrove #if !defined(__umulsidi3)
442281ba51SDavid Holsgrove #define __umulsidi3(u, v) \
452281ba51SDavid Holsgrove ({DIunion __w; \
462281ba51SDavid Holsgrove umul_ppmm(__w.s.high, __w.s.low, u, v); \
472281ba51SDavid Holsgrove __w.ll; })
482281ba51SDavid Holsgrove #endif
492281ba51SDavid Holsgrove
502281ba51SDavid Holsgrove typedef unsigned int USItype __attribute__ ((mode(SI)));
512281ba51SDavid Holsgrove typedef int SItype __attribute__ ((mode(SI)));
522281ba51SDavid Holsgrove typedef int DItype __attribute__ ((mode(DI)));
532281ba51SDavid Holsgrove typedef int word_type __attribute__ ((mode(__word__)));
542281ba51SDavid Holsgrove
552281ba51SDavid Holsgrove struct DIstruct {
562281ba51SDavid Holsgrove SItype low, high;
572281ba51SDavid Holsgrove };
582281ba51SDavid Holsgrove typedef union {
592281ba51SDavid Holsgrove struct DIstruct s;
602281ba51SDavid Holsgrove DItype ll;
612281ba51SDavid Holsgrove } DIunion;
622281ba51SDavid Holsgrove
__muldi3(DItype u,DItype v)632281ba51SDavid Holsgrove DItype __muldi3(DItype u, DItype v)
642281ba51SDavid Holsgrove {
652281ba51SDavid Holsgrove DIunion w;
662281ba51SDavid Holsgrove DIunion uu, vv;
672281ba51SDavid Holsgrove
682281ba51SDavid Holsgrove uu.ll = u, vv.ll = v;
692281ba51SDavid Holsgrove /* panic("kernel panic for __muldi3"); */
702281ba51SDavid Holsgrove w.ll = __umulsidi3(uu.s.low, vv.s.low);
712281ba51SDavid Holsgrove w.s.high += ((USItype) uu.s.low * (USItype) vv.s.high
722281ba51SDavid Holsgrove + (USItype) uu.s.high * (USItype) vv.s.low);
732281ba51SDavid Holsgrove
742281ba51SDavid Holsgrove return w.ll;
752281ba51SDavid Holsgrove }
76