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