https://gcc.gnu.org/bugzilla/show_bug.cgi?id=102124

--- Comment #1 from Tomas Chang <changyp6 at gmail dot com> ---
Test Program is as below:
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>

typedef uint8_t byte;
int buf_eq_const(const void *_a, const void *_b, size_t len)
 {
   const byte *a = _a; 
   const byte *b = _b; 
   int ab, ba; 
   size_t i;

   /* Constant-time compare. */
   for (i = 0, ab = 0, ba = 0; i < len; i++)
     {
       /* If a[i] != b[i], either ab or ba will be negative. */
       ab |= a[i] - b[i];
       ba |= b[i] - a[i];
     }   

   /* 'ab | ba' is negative when buffers are not equal. */
   printf("(ab | ba) = %d(%08x), ab = %d(%08x), ba = %d(%08x)\n", (ab | ba),
(ab | ba), ab, ab, ba, ba);
   return (ab | ba) >= 0;
 }

void print_array(const char *name, uint8_t *a, size_t len)
{
    printf("%s[%lu] = {", name, len);
    for (size_t i = 0; i < len; ++ i) {
        printf("\'%c\', ", a[i]);
    }
    printf("}\n");
}

int main(int argc, char *argv[])
{
    uint8_t a[32] = {'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a',
'a', 'a', 'a', 'a', 'a'};
    uint8_t b[32] = {'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a',
'a', 'a', 'a', 'a', 'a'};
    uint8_t c[32] = {'b', 'b', 'b', 'b', 'b', 'b', 'b', 'b', 'b', 'b', 'b',
'b', 'b', 'b', 'b', 'b'};

    printf("Comparing array a and array b:\n");
    print_array("a", a, 16);
    print_array("b", b, 16);
    if (buf_eq_const(a, b, 16)) {
        printf("a == b\n");
    } else {
        printf("a != b\n");
    }

    printf("Comparing array a and array c:\n");
    print_array("a", a, 16);
    print_array("c", c, 16);
    if (buf_eq_const(a, c, 16)) {
        printf("a == c\n");
    } else {
        printf("a != c\n");
    }

    return 0;
}

Compile this file with the following command:
gcc -Wall -O2 -march=armv8.2-a+crypto+fp16+rcpc+dotprod -mtune=cortex-a76 -o $@
$<

Running Results:
./test-gcc-O2
Comparing array a and array b:
a[16] = {'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a',
'a', 'a', }
b[16] = {'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a',
'a', 'a', }
(ab | ba) = 0(00000000), ab = 0(00000000), ba = 0(00000000)
a == b
Comparing array a and array c:
a[16] = {'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a',
'a', 'a', }
c[16] = {'b', 'b', 'b', 'b', 'b', 'b', 'b', 'b', 'b', 'b', 'b', 'b', 'b', 'b',
'b', 'b', }
(ab | ba) = -1(ffffffff), ab = -1(ffffffff), ba = 1(00000001)
a != c


Compile this file with the following command:
test-gcc-O2-tree-loop-vectorize: test-gcc.c
        gcc -Wall -O2 -march=armv8.2-a+crypto+fp16+rcpc+dotprod
-mtune=cortex-a76 -ftree-loop-vectorize -o $@ $<

Running Results:
./test-gcc-O2-tree-loop-vectorize 
Comparing array a and array b:
a[16] = {'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a',
'a', 'a', }
b[16] = {'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a',
'a', 'a', }
(ab | ba) = 0(00000000), ab = 0(00000000), ba = 0(00000000)
a == b
Comparing array a and array c:
a[16] = {'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a',
'a', 'a', }
c[16] = {'b', 'b', 'b', 'b', 'b', 'b', 'b', 'b', 'b', 'b', 'b', 'b', 'b', 'b',
'b', 'b', }
(ab | ba) = 65535(0000ffff), ab = 65535(0000ffff), ba = 1(00000001)
a == c

Reply via email to