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.

211 lines
4.6 KiB

  1. <% function mulS1S2() { %>
  2. xor rax, rax
  3. mov eax, r8d
  4. imul r9d
  5. jo mul_manageOverflow ; rsi already is the 64bits result
  6. mov [rdi], rax ; not necessary to adjust so just save and return
  7. mul_manageOverflow: ; Do the operation in 64 bits
  8. push rsi
  9. movsx rax, r8d
  10. movsx rcx, r9d
  11. imul rcx
  12. mov rsi, rax
  13. call rawCopyS2L
  14. pop rsi
  15. <% } %>
  16. <% function mulL1S2(t) { %>
  17. push rsi
  18. add rsi, 8
  19. movsx rdx, r9d
  20. add rdi, 8
  21. cmp rdx, 0
  22. <% const rawPositiveLabel = global.tmpLabel() %>
  23. jns <%= rawPositiveLabel %>
  24. neg rdx
  25. call rawMontgomeryMul1
  26. mov rsi, rdi
  27. call rawNegL
  28. sub rdi, 8
  29. pop rsi
  30. <% const done = global.tmpLabel() %>
  31. jmp <%= done %>
  32. <%= rawPositiveLabel %>:
  33. call rawMontgomeryMul1
  34. sub rdi, 8
  35. pop rsi
  36. <%= done %>:
  37. <% } %>
  38. <% function mulS1L2() { %>
  39. push rsi
  40. lea rsi, [rdx + 8]
  41. movsx rdx, r8d
  42. add rdi, 8
  43. cmp rdx, 0
  44. <% const rawPositiveLabel = global.tmpLabel() %>
  45. jns <%= rawPositiveLabel %>
  46. neg rdx
  47. call rawMontgomeryMul1
  48. mov rsi, rdi
  49. call rawNegL
  50. sub rdi, 8
  51. pop rsi
  52. <% const done = global.tmpLabel() %>
  53. jmp <%= done %>
  54. <%= rawPositiveLabel %>:
  55. call rawMontgomeryMul1
  56. sub rdi, 8
  57. pop rsi
  58. <%= done %>:
  59. <% } %>
  60. <% function mulL1L2() { %>
  61. add rdi, 8
  62. add rsi, 8
  63. add rdx, 8
  64. call rawMontgomeryMul
  65. sub rdi, 8
  66. sub rsi, 8
  67. <% } %>
  68. <% function mulR3() { %>
  69. push rsi
  70. add rdi, 8
  71. mov rsi, rdi
  72. lea rdx, [R3]
  73. call rawMontgomeryMul
  74. sub rdi, 8
  75. pop rsi
  76. <% } %>
  77. ;;;;;;;;;;;;;;;;;;;;;;
  78. ; mul
  79. ;;;;;;;;;;;;;;;;;;;;;;
  80. ; Multiplies two elements of any kind
  81. ; Params:
  82. ; rsi <= Pointer to element 1
  83. ; rdx <= Pointer to element 2
  84. ; rdi <= Pointer to result
  85. ; [rdi] = [rsi] * [rdi]
  86. ; Modified Registers:
  87. ; r8, r9, 10, r11, rax, rcx
  88. ;;;;;;;;;;;;;;;;;;;;;;
  89. <%=name%>_mul:
  90. mov r8, [rsi]
  91. mov r9, [rdx]
  92. bt r8, 63 ; Check if is short first operand
  93. jc mul_l1
  94. bt r9, 63 ; Check if is short second operand
  95. jc mul_s1l2
  96. mul_s1s2: ; Both operands are short
  97. <%= mulS1S2() %>
  98. ret
  99. mul_l1:
  100. bt r9, 63 ; Check if is short second operand
  101. jc mul_l1l2
  102. ;;;;;;;;
  103. mul_l1s2:
  104. bt r8, 62 ; check if montgomery first
  105. jc mul_l1ms2
  106. mul_l1ns2:
  107. bt r9, 62 ; check if montgomery first
  108. jc mul_l1ns2m
  109. mul_l1ns2n:
  110. <%= global.setTypeDest("0xC0"); %>
  111. <%= mulL1S2() %>
  112. <%= mulR3() %>
  113. ret
  114. mul_l1ns2m:
  115. <%= global.setTypeDest("0x80"); %>
  116. <%= mulL1L2() %>
  117. ret
  118. mul_l1ms2:
  119. bt r9, 62 ; check if montgomery second
  120. jc mul_l1ms2m
  121. mul_l1ms2n:
  122. <%= global.setTypeDest("0x80"); %>
  123. <%= mulL1S2() %>
  124. ret
  125. mul_l1ms2m:
  126. <%= global.setTypeDest("0xC0"); %>
  127. <%= mulL1L2() %>
  128. ret
  129. ;;;;;;;;
  130. mul_s1l2:
  131. bt r8, 62 ; check if montgomery first
  132. jc mul_s1ml2
  133. mul_s1nl2:
  134. bt r9, 62 ; check if montgomery first
  135. jc mul_s1nl2m
  136. mul_s1nl2n:
  137. <%= global.setTypeDest("0xC0"); %>
  138. <%= mulS1L2() %>
  139. <%= mulR3() %>
  140. ret
  141. mul_s1nl2m:
  142. <%= global.setTypeDest("0x80"); %>
  143. <%= mulS1L2(); %>
  144. ret
  145. mul_s1ml2:
  146. bt r9, 62 ; check if montgomery first
  147. jc mul_s1ml2m
  148. mul_s1ml2n:
  149. <%= global.setTypeDest("0x80"); %>
  150. <%= mulL1L2() %>
  151. ret
  152. mul_s1ml2m:
  153. <%= global.setTypeDest("0xC0"); %>
  154. <%= mulL1L2() %>
  155. ret
  156. ;;;;
  157. mul_l1l2:
  158. bt r8, 62 ; check if montgomery first
  159. jc mul_l1ml2
  160. mul_l1nl2:
  161. bt r9, 62 ; check if montgomery second
  162. jc mul_l1nl2m
  163. mul_l1nl2n:
  164. <%= global.setTypeDest("0xC0"); %>
  165. <%= mulL1L2() %>
  166. <%= mulR3() %>
  167. ret
  168. mul_l1nl2m:
  169. <%= global.setTypeDest("0x80"); %>
  170. <%= mulL1L2() %>
  171. ret
  172. mul_l1ml2:
  173. bt r9, 62 ; check if montgomery seconf
  174. jc mul_l1ml2m
  175. mul_l1ml2n:
  176. <%= global.setTypeDest("0x80"); %>
  177. <%= mulL1L2() %>
  178. ret
  179. mul_l1ml2m:
  180. <%= global.setTypeDest("0xC0"); %>
  181. <%= mulL1L2() %>
  182. ret