18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci */
48c2ecf20Sopenharmony_ci
58c2ecf20Sopenharmony_ci#include <linux/export.h>
68c2ecf20Sopenharmony_ci#include <linux/libgcc.h>
78c2ecf20Sopenharmony_ci
88c2ecf20Sopenharmony_ci#define W_TYPE_SIZE 32
98c2ecf20Sopenharmony_ci
108c2ecf20Sopenharmony_ci#define __ll_B ((unsigned long) 1 << (W_TYPE_SIZE / 2))
118c2ecf20Sopenharmony_ci#define __ll_lowpart(t) ((unsigned long) (t) & (__ll_B - 1))
128c2ecf20Sopenharmony_ci#define __ll_highpart(t) ((unsigned long) (t) >> (W_TYPE_SIZE / 2))
138c2ecf20Sopenharmony_ci
148c2ecf20Sopenharmony_ci/* If we still don't have umul_ppmm, define it using plain C.  */
158c2ecf20Sopenharmony_ci#if !defined(umul_ppmm)
168c2ecf20Sopenharmony_ci#define umul_ppmm(w1, w0, u, v)						\
178c2ecf20Sopenharmony_ci	do {								\
188c2ecf20Sopenharmony_ci		unsigned long __x0, __x1, __x2, __x3;			\
198c2ecf20Sopenharmony_ci		unsigned short __ul, __vl, __uh, __vh;			\
208c2ecf20Sopenharmony_ci									\
218c2ecf20Sopenharmony_ci		__ul = __ll_lowpart(u);					\
228c2ecf20Sopenharmony_ci		__uh = __ll_highpart(u);				\
238c2ecf20Sopenharmony_ci		__vl = __ll_lowpart(v);					\
248c2ecf20Sopenharmony_ci		__vh = __ll_highpart(v);				\
258c2ecf20Sopenharmony_ci									\
268c2ecf20Sopenharmony_ci		__x0 = (unsigned long) __ul * __vl;			\
278c2ecf20Sopenharmony_ci		__x1 = (unsigned long) __ul * __vh;			\
288c2ecf20Sopenharmony_ci		__x2 = (unsigned long) __uh * __vl;			\
298c2ecf20Sopenharmony_ci		__x3 = (unsigned long) __uh * __vh;			\
308c2ecf20Sopenharmony_ci									\
318c2ecf20Sopenharmony_ci		__x1 += __ll_highpart(__x0); /* this can't give carry */\
328c2ecf20Sopenharmony_ci		__x1 += __x2; /* but this indeed can */			\
338c2ecf20Sopenharmony_ci		if (__x1 < __x2) /* did we get it? */			\
348c2ecf20Sopenharmony_ci		__x3 += __ll_B; /* yes, add it in the proper pos */	\
358c2ecf20Sopenharmony_ci									\
368c2ecf20Sopenharmony_ci		(w1) = __x3 + __ll_highpart(__x1);			\
378c2ecf20Sopenharmony_ci		(w0) = __ll_lowpart(__x1) * __ll_B + __ll_lowpart(__x0);\
388c2ecf20Sopenharmony_ci	} while (0)
398c2ecf20Sopenharmony_ci#endif
408c2ecf20Sopenharmony_ci
418c2ecf20Sopenharmony_ci#if !defined(__umulsidi3)
428c2ecf20Sopenharmony_ci#define __umulsidi3(u, v) ({				\
438c2ecf20Sopenharmony_ci	DWunion __w;					\
448c2ecf20Sopenharmony_ci	umul_ppmm(__w.s.high, __w.s.low, u, v);		\
458c2ecf20Sopenharmony_ci	__w.ll;						\
468c2ecf20Sopenharmony_ci	})
478c2ecf20Sopenharmony_ci#endif
488c2ecf20Sopenharmony_ci
498c2ecf20Sopenharmony_cilong long notrace __muldi3(long long u, long long v)
508c2ecf20Sopenharmony_ci{
518c2ecf20Sopenharmony_ci	const DWunion uu = {.ll = u};
528c2ecf20Sopenharmony_ci	const DWunion vv = {.ll = v};
538c2ecf20Sopenharmony_ci	DWunion w = {.ll = __umulsidi3(uu.s.low, vv.s.low)};
548c2ecf20Sopenharmony_ci
558c2ecf20Sopenharmony_ci	w.s.high += ((unsigned long) uu.s.low * (unsigned long) vv.s.high
568c2ecf20Sopenharmony_ci		+ (unsigned long) uu.s.high * (unsigned long) vv.s.low);
578c2ecf20Sopenharmony_ci
588c2ecf20Sopenharmony_ci	return w.ll;
598c2ecf20Sopenharmony_ci}
608c2ecf20Sopenharmony_ciEXPORT_SYMBOL(__muldi3);
61