multi3.c 1.3 KB

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