^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) #include <linux/export.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) #include "libgcc.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) * GCC 7 & older can suboptimally generate __multi3 calls for mips64r6, so for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) * that specific case only we implement that intrinsic here.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) * See https://gcc.gnu.org/bugzilla/show_bug.cgi?id=82981
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #if defined(CONFIG_64BIT) && defined(CONFIG_CPU_MIPSR6) && (__GNUC__ < 8)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) /* multiply 64-bit values, low 64-bits returned */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) static inline long long notrace dmulu(long long a, long long b)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) long long res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) asm ("dmulu %0,%1,%2" : "=r" (res) : "r" (a), "r" (b));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) return res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) /* multiply 64-bit unsigned values, high 64-bits of 128-bit result returned */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) static inline long long notrace dmuhu(long long a, long long b)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) long long res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) asm ("dmuhu %0,%1,%2" : "=r" (res) : "r" (a), "r" (b));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) return res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) /* multiply 128-bit values, low 128-bits returned */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) ti_type notrace __multi3(ti_type a, ti_type b)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) TWunion res, aa, bb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) aa.ti = a;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) bb.ti = b;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) * a * b = (a.lo * b.lo)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) * + 2^64 * (a.hi * b.lo + a.lo * b.hi)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) * [+ 2^128 * (a.hi * b.hi)]
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) res.s.low = dmulu(aa.s.low, bb.s.low);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) res.s.high = dmuhu(aa.s.low, bb.s.low);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) res.s.high += dmulu(aa.s.high, bb.s.low);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) res.s.high += dmulu(aa.s.low, bb.s.high);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) return res.ti;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) EXPORT_SYMBOL(__multi3);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) #endif /* 64BIT && CPU_MIPSR6 && GCC7 */