div64.h 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. /* MN10300 64-bit division
  2. *
  3. * Copyright (C) 2007 Red Hat, Inc. All Rights Reserved.
  4. * Written by David Howells (dhowells@redhat.com)
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public Licence
  8. * as published by the Free Software Foundation; either version
  9. * 2 of the Licence, or (at your option) any later version.
  10. */
  11. #ifndef _ASM_DIV64
  12. #define _ASM_DIV64
  13. #include <linux/types.h>
  14. extern void ____unhandled_size_in_do_div___(void);
  15. /*
  16. * divide n by base, leaving the result in n and returning the remainder
  17. * - we can do this quite efficiently on the MN10300 by cascading the divides
  18. * through the MDR register
  19. */
  20. #define do_div(n, base) \
  21. ({ \
  22. unsigned __rem = 0; \
  23. if (sizeof(n) <= 4) { \
  24. asm("mov %1,mdr \n" \
  25. "divu %2,%0 \n" \
  26. "mov mdr,%1 \n" \
  27. : "+r"(n), "=d"(__rem) \
  28. : "r"(base), "1"(__rem) \
  29. : "cc" \
  30. ); \
  31. } else if (sizeof(n) <= 8) { \
  32. union { \
  33. unsigned long long l; \
  34. u32 w[2]; \
  35. } __quot; \
  36. __quot.l = n; \
  37. asm("mov %0,mdr \n" /* MDR = 0 */ \
  38. "divu %3,%1 \n" \
  39. /* __quot.MSL = __div.MSL / base, */ \
  40. /* MDR = MDR:__div.MSL % base */ \
  41. "divu %3,%2 \n" \
  42. /* __quot.LSL = MDR:__div.LSL / base, */ \
  43. /* MDR = MDR:__div.LSL % base */ \
  44. "mov mdr,%0 \n" \
  45. : "=d"(__rem), "=r"(__quot.w[1]), "=r"(__quot.w[0]) \
  46. : "r"(base), "0"(__rem), "1"(__quot.w[1]), \
  47. "2"(__quot.w[0]) \
  48. : "cc" \
  49. ); \
  50. n = __quot.l; \
  51. } else { \
  52. ____unhandled_size_in_do_div___(); \
  53. } \
  54. __rem; \
  55. })
  56. /*
  57. * do an unsigned 32-bit multiply and divide with intermediate 64-bit product
  58. * so as not to lose accuracy
  59. * - we use the MDR register to hold the MSW of the product
  60. */
  61. static inline __attribute__((const))
  62. unsigned __muldiv64u(unsigned val, unsigned mult, unsigned div)
  63. {
  64. unsigned result;
  65. asm("mulu %2,%0 \n" /* MDR:val = val*mult */
  66. "divu %3,%0 \n" /* val = MDR:val/div;
  67. * MDR = MDR:val%div */
  68. : "=r"(result)
  69. : "0"(val), "ir"(mult), "r"(div)
  70. );
  71. return result;
  72. }
  73. /*
  74. * do a signed 32-bit multiply and divide with intermediate 64-bit product so
  75. * as not to lose accuracy
  76. * - we use the MDR register to hold the MSW of the product
  77. */
  78. static inline __attribute__((const))
  79. signed __muldiv64s(signed val, signed mult, signed div)
  80. {
  81. signed result;
  82. asm("mul %2,%0 \n" /* MDR:val = val*mult */
  83. "div %3,%0 \n" /* val = MDR:val/div;
  84. * MDR = MDR:val%div */
  85. : "=r"(result)
  86. : "0"(val), "ir"(mult), "r"(div)
  87. );
  88. return result;
  89. }
  90. #endif /* _ASM_DIV64 */