1
0

mips64r6-multi3.patch 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. MIPS: Implement __multi3 for GCC7 MIPS64r6 builds
  2. Reported-by: Thomas Petazzoni <thomas.petazzoni@free-electrons.com>
  3. Signed-off-by: James Hogan <jhogan@kernel.org>
  4. diff -Nur linux-4.14.8.orig/arch/mips/lib/libgcc.h linux-4.14.8/arch/mips/lib/libgcc.h
  5. --- linux-4.14.8.orig/arch/mips/lib/libgcc.h 2017-12-20 10:10:38.000000000 +0100
  6. +++ linux-4.14.8/arch/mips/lib/libgcc.h 2017-12-25 16:58:38.258470502 +0100
  7. @@ -10,10 +10,18 @@
  8. struct DWstruct {
  9. int high, low;
  10. };
  11. +
  12. +struct TWstruct {
  13. + long long high, low;
  14. +};
  15. #elif defined(__LITTLE_ENDIAN)
  16. struct DWstruct {
  17. int low, high;
  18. };
  19. +
  20. +struct TWstruct {
  21. + long long low, high;
  22. +};
  23. #else
  24. #error I feel sick.
  25. #endif
  26. @@ -23,4 +31,13 @@
  27. long long ll;
  28. } DWunion;
  29. +#if defined(CONFIG_64BIT) && defined(CONFIG_CPU_MIPSR6)
  30. +typedef int ti_type __attribute__((mode(TI)));
  31. +
  32. +typedef union {
  33. + struct TWstruct s;
  34. + ti_type ti;
  35. +} TWunion;
  36. +#endif
  37. +
  38. #endif /* __ASM_LIBGCC_H */
  39. diff -Nur linux-4.14.8.orig/arch/mips/lib/Makefile linux-4.14.8/arch/mips/lib/Makefile
  40. --- linux-4.14.8.orig/arch/mips/lib/Makefile 2017-12-20 10:10:38.000000000 +0100
  41. +++ linux-4.14.8/arch/mips/lib/Makefile 2017-12-25 16:58:38.258470502 +0100
  42. @@ -16,4 +16,5 @@
  43. obj-$(CONFIG_CPU_TX39XX) += r3k_dump_tlb.o
  44. # libgcc-style stuff needed in the kernel
  45. -obj-y += ashldi3.o ashrdi3.o bswapsi.o bswapdi.o cmpdi2.o lshrdi3.o ucmpdi2.o
  46. +obj-y += ashldi3.o ashrdi3.o bswapsi.o bswapdi.o cmpdi2.o lshrdi3.o multi3.o \
  47. + ucmpdi2.o
  48. diff -Nur linux-4.14.8.orig/arch/mips/lib/multi3.c linux-4.14.8/arch/mips/lib/multi3.c
  49. --- linux-4.14.8.orig/arch/mips/lib/multi3.c 1970-01-01 01:00:00.000000000 +0100
  50. +++ linux-4.14.8/arch/mips/lib/multi3.c 2017-12-25 16:58:38.258470502 +0100
  51. @@ -0,0 +1,52 @@
  52. +// SPDX-License-Identifier: GPL-2.0
  53. +#include <linux/export.h>
  54. +
  55. +#include "libgcc.h"
  56. +
  57. +/*
  58. + * GCC 7 suboptimally generates __multi3 calls for mips64r6, so for that
  59. + * specific case only we'll implement it here.
  60. + *
  61. + * See https://gcc.gnu.org/bugzilla/show_bug.cgi?id=82981
  62. + */
  63. +#if defined(CONFIG_64BIT) && defined(CONFIG_CPU_MIPSR6) && (__GNUC__ == 7)
  64. +
  65. +/* multiply 64-bit values, low 64-bits returned */
  66. +static inline long long notrace dmulu(long long a, long long b)
  67. +{
  68. + long long res;
  69. + asm ("dmulu %0,%1,%2" : "=r" (res) : "r" (a), "r" (b));
  70. + return res;
  71. +}
  72. +
  73. +/* multiply 64-bit unsigned values, high 64-bits of 128-bit result returned */
  74. +static inline long long notrace dmuhu(long long a, long long b)
  75. +{
  76. + long long res;
  77. + asm ("dmuhu %0,%1,%2" : "=r" (res) : "r" (a), "r" (b));
  78. + return res;
  79. +}
  80. +
  81. +/* multiply 128-bit values, low 128-bits returned */
  82. +ti_type notrace __multi3(ti_type a, ti_type b)
  83. +{
  84. + TWunion res, aa, bb;
  85. +
  86. + aa.ti = a;
  87. + bb.ti = b;
  88. +
  89. + /*
  90. + * a * b = (a.lo * b.lo)
  91. + * + 2^64 * (a.hi * b.lo + a.lo * b.hi)
  92. + * [+ 2^128 * (a.hi * b.hi)]
  93. + */
  94. + res.s.low = dmulu(aa.s.low, bb.s.low);
  95. + res.s.high = dmuhu(aa.s.low, bb.s.low);
  96. + res.s.high += dmulu(aa.s.high, bb.s.low);
  97. + res.s.high += dmulu(aa.s.low, bb.s.high);
  98. +
  99. + return res.ti;
  100. +}
  101. +EXPORT_SYMBOL(__multi3);
  102. +
  103. +#endif /* 64BIT && CPU_MIPSR6 && GCC7 */