[aarch64-port-dev ] Fix string_compare for chars that have the top bit set

Edward Nevill edward.nevill at linaro.org
Fri Jul 4 10:10:32 UTC 2014


Hi,

string_compare generates incorrect results in cases where one of the characters has the top bit set.

Consider the case

String1: aaaa bbbb cccc dddd 0001 1110 2222 3333
String2: aaaa bbbb cccc dddd fff0 1111 2222 3333

Here String1 is < than String2 so it should return a negative result.

In the existing code

// At this point
//   result    = 3333 2222 1110 0001
//   cnt1      = 3333 2222 1111 fff0
//   rscratch2 = 0000 0000 0001 fff1

     rev(rscratch2, rscratch2);        // rscratch2 <- f1ff 0100 0000 0000
     clz(rscratch2, rscratch2);        // rscratch2 <- 0
     andr(rscratch2, rscratch2, -16);  // rscratch2 <- 0
     lsrv(result, result, rscratch2);  // result <- 3333 2222 1110 0001
     lsrv(cnt1, cnt1, rscratch2);      // cnt1 <- 3333 2222 1111 fff0
     sub(result, result, cnt1);        // result <- ffff ffff fffe 0011
     sxthw(result, result);            // result <- 0000 0000 0000 0011

which is positive instead of negative.

I believe the following generate the required result in all cases.

    rev(rscratch2, rscratch2);
    clz(rscratch2, rscratch2);
    andr(rscratch2, rscratch2, -16);
    lsrv(result, result, rscratch2);
    uxth(result, result);
    lsrv(cnt1, cnt1, rscratch2);
    uxth(cnt1, cnt1);
    subw(result, result, cnt1);

IE. we zero extend the characters and then do the subtraction.

All the best,
Ed.

--- CUT HERE ---
# HG changeset patch
# User Edward Nevill edward.nevill at linaro.org
# Date 1404467207 -3600
#      Fri Jul 04 10:46:47 2014 +0100
# Node ID a824f2b2f34634fac79628bb47cbd5e0b13ae28c
# Parent  c3d17e4167b4109d878bd85b50c1f7fa5758aec7
Fix string_compare for chars that have the top bit set

diff -r c3d17e4167b4 -r a824f2b2f346 src/cpu/aarch64/vm/macroAssembler_aarch64.cpp
--- a/src/cpu/aarch64/vm/macroAssembler_aarch64.cpp	Thu Jul 03 16:07:33 2014 +0100
+++ b/src/cpu/aarch64/vm/macroAssembler_aarch64.cpp	Fri Jul 04 10:46:47 2014 +0100
@@ -3409,9 +3409,10 @@
     clz(rscratch2, rscratch2);
     andr(rscratch2, rscratch2, -16);
     lsrv(result, result, rscratch2);
+    uxth(result, result);
     lsrv(cnt1, cnt1, rscratch2);
-    sub(result, result, cnt1);
-    sxthw(result, result);
+    uxth(cnt1, cnt1);
+    subw(result, result, cnt1);
     b(DONE);
   }
 
--- CUT HERE ---




More information about the aarch64-port-dev mailing list