Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003  */
0004 
0005 #include <linux/export.h>
0006 #include <linux/libgcc.h>
0007 
0008 #define W_TYPE_SIZE 32
0009 
0010 #define __ll_B ((unsigned long) 1 << (W_TYPE_SIZE / 2))
0011 #define __ll_lowpart(t) ((unsigned long) (t) & (__ll_B - 1))
0012 #define __ll_highpart(t) ((unsigned long) (t) >> (W_TYPE_SIZE / 2))
0013 
0014 /* If we still don't have umul_ppmm, define it using plain C.  */
0015 #if !defined(umul_ppmm)
0016 #define umul_ppmm(w1, w0, u, v)                     \
0017     do {                                \
0018         unsigned long __x0, __x1, __x2, __x3;           \
0019         unsigned short __ul, __vl, __uh, __vh;          \
0020                                     \
0021         __ul = __ll_lowpart(u);                 \
0022         __uh = __ll_highpart(u);                \
0023         __vl = __ll_lowpart(v);                 \
0024         __vh = __ll_highpart(v);                \
0025                                     \
0026         __x0 = (unsigned long) __ul * __vl;         \
0027         __x1 = (unsigned long) __ul * __vh;         \
0028         __x2 = (unsigned long) __uh * __vl;         \
0029         __x3 = (unsigned long) __uh * __vh;         \
0030                                     \
0031         __x1 += __ll_highpart(__x0); /* this can't give carry */\
0032         __x1 += __x2; /* but this indeed can */         \
0033         if (__x1 < __x2) /* did we get it? */           \
0034         __x3 += __ll_B; /* yes, add it in the proper pos */ \
0035                                     \
0036         (w1) = __x3 + __ll_highpart(__x1);          \
0037         (w0) = __ll_lowpart(__x1) * __ll_B + __ll_lowpart(__x0);\
0038     } while (0)
0039 #endif
0040 
0041 #if !defined(__umulsidi3)
0042 #define __umulsidi3(u, v) ({                \
0043     DWunion __w;                    \
0044     umul_ppmm(__w.s.high, __w.s.low, u, v);     \
0045     __w.ll;                     \
0046     })
0047 #endif
0048 
0049 long long notrace __muldi3(long long u, long long v)
0050 {
0051     const DWunion uu = {.ll = u};
0052     const DWunion vv = {.ll = v};
0053     DWunion w = {.ll = __umulsidi3(uu.s.low, vv.s.low)};
0054 
0055     w.s.high += ((unsigned long) uu.s.low * (unsigned long) vv.s.high
0056         + (unsigned long) uu.s.high * (unsigned long) vv.s.low);
0057 
0058     return w.ll;
0059 }
0060 EXPORT_SYMBOL(__muldi3);