blob: a3f9a03acdcda454a99e780b898bfcef4ae7e046 [file] [log] [blame]
Michal Simekd64af912013-02-01 13:10:35 +01001#include <linux/export.h>
Michal Simek3370d822010-12-07 11:55:06 +01002
3#include "libgcc.h"
4
Michal Simek3370d822010-12-07 11:55:06 +01005#define W_TYPE_SIZE 32
6
Michal Simeka671de02012-12-13 17:46:31 +01007#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))
Michal Simek3370d822010-12-07 11:55:06 +010010
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 { \
Michal Simeka671de02012-12-13 17:46:31 +010015 unsigned long __x0, __x1, __x2, __x3; \
16 unsigned short __ul, __vl, __uh, __vh; \
Michal Simek3370d822010-12-07 11:55:06 +010017 \
18 __ul = __ll_lowpart(u); \
19 __uh = __ll_highpart(u); \
20 __vl = __ll_lowpart(v); \
21 __vh = __ll_highpart(v); \
22 \
Michal Simeka671de02012-12-13 17:46:31 +010023 __x0 = (unsigned long) __ul * __vl; \
24 __x1 = (unsigned long) __ul * __vh; \
25 __x2 = (unsigned long) __uh * __vl; \
26 __x3 = (unsigned long) __uh * __vh; \
Michal Simek3370d822010-12-07 11:55:06 +010027 \
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
Michal Simeka671de02012-12-13 17:46:31 +010046long long __muldi3(long long u, long long v)
Michal Simek3370d822010-12-07 11:55:06 +010047{
48 const DWunion uu = {.ll = u};
49 const DWunion vv = {.ll = v};
50 DWunion w = {.ll = __umulsidi3(uu.s.low, vv.s.low)};
51
Michal Simeka671de02012-12-13 17:46:31 +010052 w.s.high += ((unsigned long) uu.s.low * (unsigned long) vv.s.high
53 + (unsigned long) uu.s.high * (unsigned long) vv.s.low);
Michal Simek3370d822010-12-07 11:55:06 +010054
55 return w.ll;
56}
Michal Simekd50c3032011-02-24 15:36:49 +010057EXPORT_SYMBOL(__muldi3);