blob: 5c1a1541c8c4305b41b1b94e46fac1ab5c2f7cad [file] [log] [blame]
David Holsgrove387162782012-11-06 23:01:24 +10001/*
2 * U-boot - muldi3.c contains routines for mult and div
3 *
4 *
Wolfgang Denkd79de1d2013-07-08 09:37:19 +02005 * SPDX-License-Identifier: GPL-2.0+
David Holsgrove387162782012-11-06 23:01:24 +10006 */
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
50typedef unsigned int USItype __attribute__ ((mode(SI)));
51typedef int SItype __attribute__ ((mode(SI)));
52typedef int DItype __attribute__ ((mode(DI)));
53typedef int word_type __attribute__ ((mode(__word__)));
54
55struct DIstruct {
56 SItype low, high;
57};
58typedef union {
59 struct DIstruct s;
60 DItype ll;
61} DIunion;
62
63DItype __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}