idiv32.S 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. /*
  3. * Copyright (C) 2000 Hewlett-Packard Co
  4. * Copyright (C) 2000 David Mosberger-Tang <davidm@hpl.hp.com>
  5. *
  6. * 32-bit integer division.
  7. *
  8. * This code is based on the application note entitled "Divide, Square Root
  9. * and Remainder Algorithms for the IA-64 Architecture". This document
  10. * is available as Intel document number 248725-002 or via the web at
  11. * http://developer.intel.com/software/opensource/numerics/
  12. *
  13. * For more details on the theory behind these algorithms, see "IA-64
  14. * and Elementary Functions" by Peter Markstein; HP Professional Books
  15. * (http://www.goodreads.com/book/show/2019887.Ia_64_and_Elementary_Functions)
  16. */
  17. #include <asm/asmmacro.h>
  18. #include <asm/export.h>
  19. #ifdef MODULO
  20. # define OP mod
  21. #else
  22. # define OP div
  23. #endif
  24. #ifdef UNSIGNED
  25. # define SGN u
  26. # define EXTEND zxt4
  27. # define INT_TO_FP(a,b) fcvt.xuf.s1 a=b
  28. # define FP_TO_INT(a,b) fcvt.fxu.trunc.s1 a=b
  29. #else
  30. # define SGN
  31. # define EXTEND sxt4
  32. # define INT_TO_FP(a,b) fcvt.xf a=b
  33. # define FP_TO_INT(a,b) fcvt.fx.trunc.s1 a=b
  34. #endif
  35. #define PASTE1(a,b) a##b
  36. #define PASTE(a,b) PASTE1(a,b)
  37. #define NAME PASTE(PASTE(__,SGN),PASTE(OP,si3))
  38. GLOBAL_ENTRY(NAME)
  39. .regstk 2,0,0,0
  40. // Transfer inputs to FP registers.
  41. mov r2 = 0xffdd // r2 = -34 + 65535 (fp reg format bias)
  42. EXTEND in0 = in0 // in0 = a
  43. EXTEND in1 = in1 // in1 = b
  44. ;;
  45. setf.sig f8 = in0
  46. setf.sig f9 = in1
  47. #ifdef MODULO
  48. sub in1 = r0, in1 // in1 = -b
  49. #endif
  50. ;;
  51. // Convert the inputs to FP, to avoid FP software-assist faults.
  52. INT_TO_FP(f8, f8)
  53. INT_TO_FP(f9, f9)
  54. ;;
  55. setf.exp f7 = r2 // f7 = 2^-34
  56. frcpa.s1 f6, p6 = f8, f9 // y0 = frcpa(b)
  57. ;;
  58. (p6) fmpy.s1 f8 = f8, f6 // q0 = a*y0
  59. (p6) fnma.s1 f6 = f9, f6, f1 // e0 = -b*y0 + 1
  60. ;;
  61. #ifdef MODULO
  62. setf.sig f9 = in1 // f9 = -b
  63. #endif
  64. (p6) fma.s1 f8 = f6, f8, f8 // q1 = e0*q0 + q0
  65. (p6) fma.s1 f6 = f6, f6, f7 // e1 = e0*e0 + 2^-34
  66. ;;
  67. #ifdef MODULO
  68. setf.sig f7 = in0
  69. #endif
  70. (p6) fma.s1 f6 = f6, f8, f8 // q2 = e1*q1 + q1
  71. ;;
  72. FP_TO_INT(f6, f6) // q = trunc(q2)
  73. ;;
  74. #ifdef MODULO
  75. xma.l f6 = f6, f9, f7 // r = q*(-b) + a
  76. ;;
  77. #endif
  78. getf.sig r8 = f6 // transfer result to result register
  79. br.ret.sptk.many rp
  80. END(NAME)
  81. EXPORT_SYMBOL(NAME)