div64.h 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. #ifndef __ASM_ARM_DIV64
  3. #define __ASM_ARM_DIV64
  4. #include <linux/types.h>
  5. #include <asm/compiler.h>
  6. /*
  7. * The semantics of __div64_32() are:
  8. *
  9. * uint32_t __div64_32(uint64_t *n, uint32_t base)
  10. * {
  11. * uint32_t remainder = *n % base;
  12. * *n = *n / base;
  13. * return remainder;
  14. * }
  15. *
  16. * In other words, a 64-bit dividend with a 32-bit divisor producing
  17. * a 64-bit result and a 32-bit remainder. To accomplish this optimally
  18. * we override the generic version in lib/div64.c to call our __do_div64
  19. * assembly implementation with completely non standard calling convention
  20. * for arguments and results (beware).
  21. */
  22. static inline uint32_t __div64_32(uint64_t *n, uint32_t base)
  23. {
  24. register unsigned int __base asm("r4") = base;
  25. register unsigned long long __n asm("r0") = *n;
  26. register unsigned long long __res asm("r2");
  27. unsigned int __rem;
  28. asm( __asmeq("%0", "r0")
  29. __asmeq("%1", "r2")
  30. __asmeq("%2", "r4")
  31. "bl __do_div64"
  32. : "+r" (__n), "=r" (__res)
  33. : "r" (__base)
  34. : "ip", "lr", "cc");
  35. __rem = __n >> 32;
  36. *n = __res;
  37. return __rem;
  38. }
  39. #define __div64_32 __div64_32
  40. #if !defined(CONFIG_AEABI)
  41. /*
  42. * In OABI configurations, some uses of the do_div function
  43. * cause gcc to run out of registers. To work around that,
  44. * we can force the use of the out-of-line version for
  45. * configurations that build a OABI kernel.
  46. */
  47. #define do_div(n, base) __div64_32(&(n), base)
  48. #else
  49. static inline uint64_t __arch_xprod_64(uint64_t m, uint64_t n, bool bias)
  50. {
  51. unsigned long long res;
  52. register unsigned int tmp asm("ip") = 0;
  53. if (!bias) {
  54. asm ( "umull %Q0, %R0, %Q1, %Q2\n\t"
  55. "mov %Q0, #0"
  56. : "=&r" (res)
  57. : "r" (m), "r" (n)
  58. : "cc");
  59. } else if (!(m & ((1ULL << 63) | (1ULL << 31)))) {
  60. res = m;
  61. asm ( "umlal %Q0, %R0, %Q1, %Q2\n\t"
  62. "mov %Q0, #0"
  63. : "+&r" (res)
  64. : "r" (m), "r" (n)
  65. : "cc");
  66. } else {
  67. asm ( "umull %Q0, %R0, %Q2, %Q3\n\t"
  68. "cmn %Q0, %Q2\n\t"
  69. "adcs %R0, %R0, %R2\n\t"
  70. "adc %Q0, %1, #0"
  71. : "=&r" (res), "+&r" (tmp)
  72. : "r" (m), "r" (n)
  73. : "cc");
  74. }
  75. if (!(m & ((1ULL << 63) | (1ULL << 31)))) {
  76. asm ( "umlal %R0, %Q0, %R1, %Q2\n\t"
  77. "umlal %R0, %Q0, %Q1, %R2\n\t"
  78. "mov %R0, #0\n\t"
  79. "umlal %Q0, %R0, %R1, %R2"
  80. : "+&r" (res)
  81. : "r" (m), "r" (n)
  82. : "cc");
  83. } else {
  84. asm ( "umlal %R0, %Q0, %R2, %Q3\n\t"
  85. "umlal %R0, %1, %Q2, %R3\n\t"
  86. "mov %R0, #0\n\t"
  87. "adds %Q0, %1, %Q0\n\t"
  88. "adc %R0, %R0, #0\n\t"
  89. "umlal %Q0, %R0, %R2, %R3"
  90. : "+&r" (res), "+&r" (tmp)
  91. : "r" (m), "r" (n)
  92. : "cc");
  93. }
  94. return res;
  95. }
  96. #define __arch_xprod_64 __arch_xprod_64
  97. #include <asm-generic/div64.h>
  98. #endif
  99. #endif