Question

Quelqu'un est-il au courant d'une méthode pour obtenir une fonctionnalité de vec_msum contre un vecteur de valeurs flottantes?

Je suis tout à fait nouveau pour SIMD, et bien que je penser Je commence à donner un sens -. Il y a encore quelques casse-têtes

Mon objectif final est de réécrire la fonction « convolve_altivec » (que l'on trouve dans la réponse acceptée pour

Était-ce utile?

La solution

Pour une version flottante dont vous avez besoin vec_madd.

Voici une version flottante de 16 bits précédente version int et le faisceau de test que je posté en réponse à votre question précédente:

#include <assert.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <altivec.h>

static float convolve_ref(const float *a, const float *b, int n)
{
    float sum = 0.0f;
    int i;

    for (i = 0; i < n; ++i)
    {
        sum += a[i] * b[i];
    }

    return sum;
}

static inline float convolve_altivec(const float *a, const float *b, int n)
{
    float sum = 0.0f;
    vector float vsum = { 0.0f, 0.0f, 0.0f, 0.0f };
    union {
        vector float v;
        float a[4];
    } usum;

    vector float *pa = (vector float *)a;
    vector float *pb = (vector float *)b;

    assert(((unsigned long)a & 15) == 0);
    assert(((unsigned long)b & 15) == 0);

    while (n >= 4)
    {
        vsum = vec_madd(*pa, *pb, vsum);
        pa++;
        pb++;
        n -= 4;
    }

    usum.v = vsum;

    sum = usum.a[0] + usum.a[1] + usum.a[2] + usum.a[3];

    a = (float *)pa;
    b = (float *)pb;

    while (n --)
    {
        sum += (*a++ * *b++);
    }

    return sum;
}

int main(void)
{
    const int n = 1002;

    vector float _a[n / 4 + 1];
    vector float _b[n / 4 + 1];

    float *a = (float *)_a;
    float *b = (float *)_b;

    float sum_ref, sum_test;

    int i;

    for (i = 0; i < n; ++i)
    {
        a[i] = (float)rand();
        b[i] = (float)rand();
    }

    sum_ref = convolve_ref(a, b, n);
    sum_test = convolve_altivec(a, b, n);

    printf("sum_ref = %g\n", sum_ref);
    printf("sum_test = %g\n", sum_test);

    printf("%s\n", fabsf((sum_ref - sum_test) / sum_ref) < 1.0e-6 ? "PASS" : "FAIL");

    return 0;
}
scroll top