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

            Bug ID: 129033
           Summary: missed matrix vectorization: complicated access
                    pattern
           Product: gcc
           Version: 17.0
            Status: UNCONFIRMED
          Severity: enhancement
          Priority: P3
         Component: middle-end
          Assignee: unassigned at gcc dot gnu.org
          Reporter: manu at gcc dot gnu.org
  Target Milestone: ---

This may be impossible without a constant 'dim', but you may have some great
ideas:

#include <math.h>
#include <stdint.h>
#include <stdlib.h>
typedef uint8_t dimension_t;

double *
memmove_minmax(double * restrict dst, const double * restrict src,
               dimension_t dim, const double * restrict minmax)
{
    // GCC/Clang convert this to vectorized and+xor without multiplication.
    for (dimension_t d = 0; d < dim; d++) {
        dst[d] = src[d] * copysign(1.0, minmax[d]);
    }
    return dst;
}

/**
   minmax is -1 (maximization) or 1 (minimization) and length dim.
*/
void
matrix_minmax(double * restrict y, const double * restrict x, size_t n,
dimension_t dim, const double * restrict minmax)
{
    for (size_t i = 0; i < n; i++) {
        memmove_minmax(y + i*dim, x + i*dim, minmax, dim);
    }
}


gcc -O3 -fno-trapping-math -march=x86-64-v4 -ffast-math -fopt-info-vec-all

vectorizes the loop in memmove_minmax() but not the outer loop in
matrix_minmax(). As suggested by the types of the indexes n is typically larger
than dim.

If dim is a compile time constant, it works. That is, with dim=3, it processes
two rows at a time.

https://godbolt.org/z/7G76ardoh

Reply via email to