add native COMPARE primitive for performance

This commit is contained in:
Jesse D. McDonald 2020-11-07 11:02:21 -06:00
parent b427711830
commit 11b5b3e7ed
2 changed files with 24 additions and 10 deletions

View File

@ -942,6 +942,30 @@ defcode CMOVE_UP,"CMOVE>"
mov %edx,%esi mov %edx,%esi
NEXT NEXT
/* ( c-addr1 u1 c-addr2 u2 -- -1 | 0 | 1 ) Lexically compare two strings */
defcode COMPARE
mov %esi,%edx
mov (%esp),%ecx
mov 8(%esp),%ebx
xor %eax,%eax
cmp %ebx,%ecx
jbe 1f
mov %ebx,%ecx
1: mov 12(%esp),%esi
mov 4(%esp),%edi
repe cmpsb
je 3f
2: seta %al
shl $1,%eax
dec %eax
jmp 4f
3: cmp (%esp),%ebx
jne 2b
4: add $16,%esp
push %eax
mov %edx,%esi
NEXT
/* ( a -- ) ( R: -- a ) */ /* ( a -- ) ( R: -- a ) */
defcode TOR,">R" defcode TOR,">R"
pop %eax pop %eax

View File

@ -1067,16 +1067,6 @@ CREATE DISPLAY-ITEM-LIMIT 6 ,
: -TRAILING ( c-addr u1 -- c-addr u2 ) : -TRAILING ( c-addr u1 -- c-addr u2 )
BEGIN DUP AND-THEN 2DUP 1- + C@ SPACE? THEN WHILE 1- REPEAT ; BEGIN DUP AND-THEN 2DUP 1- + C@ SPACE? THEN WHILE 1- REPEAT ;
\ Return -1, 0, or 1 if the left string is respectively
\ less than, equal to, or greater than the right string
: COMPARE ( c-addr1 u1 c-addr2 u2 -- -1 | 0 | 1 )
ROT SWAP ▪ 2DUP U<=> -ROT 2>R -ROT 2R> ▪ UMIN 0 ?DO
( S: u1-u2 c-addr1 c-addr2 R: loop-sys )
OVER I + C@ OVER I + C@
( S: u1-u2 c-addr1 c-addr2 ch1 ch2 )
U<=> ?DUP IF -ROT 2>R NIP 2R> LEAVE THEN
LOOP ▪ 2DROP ;
\ Convert a character to lowercase or uppercase, respectively \ Convert a character to lowercase or uppercase, respectively
: TO-LOWER ( ch1 -- ch2 ) : TO-LOWER ( ch1 -- ch2 )
DUP [[ CHAR A ]] [[ CHAR Z 1+ ]] WITHIN IF [[ CHAR a CHAR A - ]] + THEN ; DUP [[ CHAR A ]] [[ CHAR Z 1+ ]] WITHIN IF [[ CHAR a CHAR A - ]] + THEN ;