div64.h 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. #ifndef __ASM_ARM_DIV64
  2. #define __ASM_ARM_DIV64
  3. #include <linux/types.h>
  4. #include <asm/compiler.h>
  5. /*
  6. * The semantics of __div64_32() are:
  7. *
  8. * uint32_t __div64_32(uint64_t *n, uint32_t base)
  9. * {
  10. * uint32_t remainder = *n % base;
  11. * *n = *n / base;
  12. * return remainder;
  13. * }
  14. *
  15. * In other words, a 64-bit dividend with a 32-bit divisor producing
  16. * a 64-bit result and a 32-bit remainder. To accomplish this optimally
  17. * we override the generic version in lib/div64.c to call our __do_div64
  18. * assembly implementation with completely non standard calling convention
  19. * for arguments and results (beware).
  20. */
  21. #ifdef __ARMEB__
  22. #define __xh "r0"
  23. #define __xl "r1"
  24. #else
  25. #define __xl "r0"
  26. #define __xh "r1"
  27. #endif
  28. static inline uint32_t __div64_32(uint64_t *n, uint32_t base)
  29. {
  30. register unsigned int __base asm("r4") = base;
  31. register unsigned long long __n asm("r0") = *n;
  32. register unsigned long long __res asm("r2");
  33. register unsigned int __rem asm(__xh);
  34. asm( __asmeq("%0", __xh)
  35. __asmeq("%1", "r2")
  36. __asmeq("%2", "r0")
  37. __asmeq("%3", "r4")
  38. "bl __do_div64"
  39. : "=r" (__rem), "=r" (__res)
  40. : "r" (__n), "r" (__base)
  41. : "ip", "lr", "cc");
  42. *n = __res;
  43. return __rem;
  44. }
  45. #define __div64_32 __div64_32
  46. #if !defined(CONFIG_AEABI)
  47. /*
  48. * In OABI configurations, some uses of the do_div function
  49. * cause gcc to run out of registers. To work around that,
  50. * we can force the use of the out-of-line version for
  51. * configurations that build a OABI kernel.
  52. */
  53. #define do_div(n, base) __div64_32(&(n), base)
  54. #else
  55. /*
  56. * gcc versions earlier than 4.0 are simply too problematic for the
  57. * __div64_const32() code in asm-generic/div64.h. First there is
  58. * gcc PR 15089 that tend to trig on more complex constructs, spurious
  59. * .global __udivsi3 are inserted even if none of those symbols are
  60. * referenced in the generated code, and those gcc versions are not able
  61. * to do constant propagation on long long values anyway.
  62. */
  63. #define __div64_const32_is_OK (__GNUC__ >= 4)
  64. static inline uint64_t __arch_xprod_64(uint64_t m, uint64_t n, bool bias)
  65. {
  66. unsigned long long res;
  67. register unsigned int tmp asm("ip") = 0;
  68. if (!bias) {
  69. asm ( "umull %Q0, %R0, %Q1, %Q2\n\t"
  70. "mov %Q0, #0"
  71. : "=&r" (res)
  72. : "r" (m), "r" (n)
  73. : "cc");
  74. } else if (!(m & ((1ULL << 63) | (1ULL << 31)))) {
  75. res = m;
  76. asm ( "umlal %Q0, %R0, %Q1, %Q2\n\t"
  77. "mov %Q0, #0"
  78. : "+&r" (res)
  79. : "r" (m), "r" (n)
  80. : "cc");
  81. } else {
  82. asm ( "umull %Q0, %R0, %Q2, %Q3\n\t"
  83. "cmn %Q0, %Q2\n\t"
  84. "adcs %R0, %R0, %R2\n\t"
  85. "adc %Q0, %1, #0"
  86. : "=&r" (res), "+&r" (tmp)
  87. : "r" (m), "r" (n)
  88. : "cc");
  89. }
  90. if (!(m & ((1ULL << 63) | (1ULL << 31)))) {
  91. asm ( "umlal %R0, %Q0, %R1, %Q2\n\t"
  92. "umlal %R0, %Q0, %Q1, %R2\n\t"
  93. "mov %R0, #0\n\t"
  94. "umlal %Q0, %R0, %R1, %R2"
  95. : "+&r" (res)
  96. : "r" (m), "r" (n)
  97. : "cc");
  98. } else {
  99. asm ( "umlal %R0, %Q0, %R2, %Q3\n\t"
  100. "umlal %R0, %1, %Q2, %R3\n\t"
  101. "mov %R0, #0\n\t"
  102. "adds %Q0, %1, %Q0\n\t"
  103. "adc %R0, %R0, #0\n\t"
  104. "umlal %Q0, %R0, %R2, %R3"
  105. : "+&r" (res), "+&r" (tmp)
  106. : "r" (m), "r" (n)
  107. : "cc");
  108. }
  109. return res;
  110. }
  111. #define __arch_xprod_64 __arch_xprod_64
  112. #include <asm-generic/div64.h>
  113. #endif
  114. #endif