On 4/20/20 8:10 AM, Stephen Long wrote:
> +#define DO_FP_MATRIX_MUL(NAME, TYPE, H)                                     \
> +void HELPER(NAME)(void *vd, void *va, void *vn, void *vm,                   \
> +                     void *status, uint32_t desc)                           \
> +{                                                                           \
> +    intptr_t s, i, j;                                                       \
> +    intptr_t opr_sz = simd_oprsz(desc) / (sizeof(TYPE) >> 2);               \
> +                                                                            \
> +    for (s = 0; s < opr_sz; ++s) {                                          \
> +        TYPE *n = vn + s * (sizeof(TYPE) >> 2);                             \
> +        TYPE *m = vm + s * (sizeof(TYPE) >> 2);                             \
> +        TYPE *a = va + s * (sizeof(TYPE) >> 2);                             \
> +        TYPE *d = vd + s * (sizeof(TYPE) >> 2);                             \
> +                                                                            \
> +        for (i = 0; i < 1; ++i) {                                           \
> +            for (j = 0; j < 1; ++j) {                                       \
> +                TYPE addend = a[H(2*i + j)];                                \
> +                                                                            \
> +                TYPE nn0 = n[H(2*i)];                                       \
> +                TYPE mm0 = m[H(2*j)];                                       \
> +                TYPE prod0 = TYPE##_mul(nn0, mm0, status);                  \
> +                                                                            \
> +                TYPE nn1 = n[H4(2*i + 1)];                                  \
> +                TYPE mm1 = m[H4(2*j + 1)];                                  \
> +                TYPE prod1 = TYPE##_mul(nn1, mm1, status);                  \
> +                                                                            \
> +                TYPE sum = TYPE##_add(prod0, prod1, status);                \
> +                d[H(2*i + j)] = TYPE##_add(sum, addend, status);            \
> +            }                                                               \
> +        }                                                                   \

This has a read-after-write problem, when D overlaps any of the inputs.  You
need to read all of the inputs before writing anything.

It might be easiest to just unroll these two inner loops:

    TYPE n00 = n[0], n01 = n[1], n10 = n[2], n11 = n[3];
    TYPE m00 = m[0], m01 = m[1], m10 = m[2], m11 = m[3];
    TYPE p0, p1;

    // i = 0, j = 0
    p0 = mul(n00, m00, status);
    p1 = mul(n01, m01, status);
    a[0] = add(a[0], add(p0, p1, status), status);

    // i = 0, j = 1
    p0 = mul(n00, m10, status);
    p1 = mul(n01, m11, status);
    a[1] = add(a[1], add(p0, p1, status), status);
    ...



r~

Reply via email to