You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

108 lines
2.8 KiB

  1. <% function retOne() { %>
  2. mov qword [rdi], 1
  3. add rsp, <%= (n64+1)*8 %>
  4. ret
  5. <% } %>
  6. <% function retZero() { %>
  7. mov qword [rdi], 0
  8. add rsp, <%= (n64+1)*8 %>
  9. ret
  10. <% } %>
  11. <% function cmpLong(op, eq) { %>
  12. <%
  13. if (eq==true) {
  14. if (["leq","geq"].indexOf(op) >= 0) retOne();
  15. if (["lt","gt"].indexOf(op) >= 0) retZero();
  16. }
  17. %>
  18. <% const label_gt = global.tmpLabel() %>
  19. <% const label_lt = global.tmpLabel() %>
  20. <% for (let i=n64-1; i>=0; i--) { %>
  21. mov rax, [rsp + <%= 8+(i*8) %>]
  22. cmp [half + <%= (i*8) %>], rax ; comare with (q-1)/2
  23. jc <%=label_lt%> ; half<rax => e1-e2 is neg => e1 < e2
  24. jnz <%=label_gt%> ; half>rax => e1 -e2 is pos => e1 > e2
  25. <% } %>
  26. ; half == rax => e1-e2 is pos => e1 > e2
  27. <%=label_gt%>:
  28. <% if (["geq","gt"].indexOf(op) >= 0) retOne(); else retZero(); %>
  29. <%=label_lt%>:
  30. <% if (["leq","lt"].indexOf(op) >= 0) retOne(); else retZero(); %>
  31. <% } // cmpLong%>
  32. <% function cmpOp(op) { %>
  33. ;;;;;;;;;;;;;;;;;;;;;;
  34. ; <%= op %>
  35. ;;;;;;;;;;;;;;;;;;;;;;
  36. ; Adds two elements of any kind
  37. ; Params:
  38. ; rsi <= Pointer to element 1
  39. ; rdx <= Pointer to element 2
  40. ; rdi <= Pointer to result can be zero or one.
  41. ; Modified Registers:
  42. ; r8, r9, 10, r11, rax, rcx
  43. ;;;;;;;;;;;;;;;;;;;;;;
  44. <%=name%>_<%=op%>:
  45. sub rsp, <%= (n64+1)*8 %> ; Save space for the result of the substraction
  46. push rdi ; Save rdi
  47. lea rdi, [rsp+8] ; We pushed rdi so we need to add 8
  48. call <%=name%>_sub ; Do a substraction
  49. call <%=name%>_toNormal ; Convert it to normal
  50. pop rdi
  51. mov rax, [rsp] ; We already poped do no need to add 8
  52. bt rax, 63 ; check is result is long
  53. jc <%=op%>_longCmp
  54. <%=op%>_shortCmp:
  55. cmp eax, 0
  56. je <%=op%>_s_eq
  57. js <%=op%>_s_lt
  58. <%=op%>_s_gt:
  59. <% if (["geq","gt", "neq"].indexOf(op) >= 0) retOne(); else retZero(); %>
  60. <%=op%>_s_lt:
  61. <% if (["leq","lt", "neq"].indexOf(op) >= 0) retOne(); else retZero(); %>
  62. <%=op%>_s_eq:
  63. <% if (["eq","geq", "leq"].indexOf(op) >= 0) retOne(); else retZero(); %>
  64. <%=op%>_longCmp:
  65. <% for (let i=n64-1; i>=0; i--) { %>
  66. cmp qword [rsp + <%= 8+(i*8) %>], 0
  67. jnz <%=op%>_neq
  68. <% } %>
  69. <%=op%>_eq:
  70. <% if (op == "eq") {
  71. retOne();
  72. } else if (op == "neq") {
  73. retZero();
  74. } else {
  75. cmpLong(op, true);
  76. }
  77. %>
  78. <%=op%>_neq:
  79. <% if (op == "neq") {
  80. retOne();
  81. } else if (op == "eq") {
  82. retZero();
  83. } else {
  84. cmpLong(op, false);
  85. }
  86. %>
  87. <% } %>
  88. <%= cmpOp("eq") %>
  89. <%= cmpOp("neq") %>
  90. <%= cmpOp("lt") %>
  91. <%= cmpOp("gt") %>
  92. <%= cmpOp("leq") %>
  93. <%= cmpOp("geq") %>