让我用它来置换..我在ASM方面的经验极为有限,而SIMD的经验甚至更少。

但是碰巧我有以下MMX/SSE优化代码,我想将其移植到Altivec指令中,以便在PPC/Cell处理器上使用。

这可能是一个很大的问题。.即使只是几行代码,我也没有任何麻烦尝试解决这里发生的事情。

原始功能:

static inline int convolve(const short *a, const short *b, int n)
{
    int out = 0;
    union {
        __m64 m64;
        int i32[2];
    } tmp;
    tmp.i32[0] = 0;
    tmp.i32[1] = 0;
    while (n >= 4) {
        tmp.m64 = _mm_add_pi32(tmp.m64,
                               _mm_madd_pi16(*((__m64 *)a),
                                             *((__m64 *)b)));
        a += 4;
        b += 4;
        n -= 4;
    }
    out = tmp.i32[0] + tmp.i32[1];
    _mm_empty();

    while (n --)
        out += (*(a++)) * (*(b++));
    return out;
}

关于我如何将其重写以使用Altivec说明的任何提示?

我的第一次尝试(一个非常错误的尝试)看起来像这样..但是这并不完全(甚至是遥不可及)。

static inline int convolve_altivec(const short *a, const short *b, int n)
{
    int out = 0;
    union {
        vector unsigned int m128;
        int i64[2];
    } tmp;

    vector unsigned int zero = {0, 0, 0, 0};

    tmp.i64[0] = 0;
    tmp.i64[1] = 0;
    while (n >= 8) {
        tmp.m128 = vec_add(tmp.m128,
                               vec_msum(*((vector unsigned short *)a),
                                             *((vector unsigned short *)b), zero));

        a += 8;
        b += 8;
        n -= 8;
    }
    out = tmp.i64[0] + tmp.i64[1];
#endif
    while (n --)
        out += (*(a++)) * (*(b++));
    return out;
}
有帮助吗?

解决方案

您不远 - 我解决了一些小问题,对代码进行了一些清理,添加了测试安全带,现在似乎可以正常工作:

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

static int convolve_ref(const short *a, const short *b, int n)
{
    int out = 0;
    int i;

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

    return out;
}

static inline int convolve_altivec(const short *a, const short *b, int n)
{
    int out = 0;
    union {
        vector signed int m128;
        int i32[4];
    } tmp;

    const vector signed int zero = {0, 0, 0, 0};

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

    tmp.m128 = zero;

    while (n >= 8)
    {
        tmp.m128 = vec_msum(*((vector signed short *)a),
                            *((vector signed short *)b), tmp.m128);

        a += 8;
        b += 8;
        n -= 8;
    }

    out = tmp.i32[0] + tmp.i32[1] + tmp.i32[2] + tmp.i32[3];

    while (n --)
        out += (*(a++)) * (*(b++));

    return out;
}

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

    vector signed short _a[n / 8 + 1];
    vector signed short _b[n / 8 + 1];

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

    int sum_ref, sum_test;

    int i;

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

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

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

    printf("%s\n", sum_ref == sum_test ? "PASS" : "FAIL");

    return 0;
}

其他提示

(警告:我所有的Altivec经验都来自在Xbox360/PS3上工作 - 我不确定它们与其他Altivec平台有何不同)。

首先,您应该检查指针对齐。大多数向量负载(和商店)操作预计将来自16字节的对齐地址。如果不是这样,通常会在没有警告的情况下继续前进,但是您不会得到所期望的数据。

可以(但较慢)进行不规则的负载,但是基本上您必须在数据之前和之后阅读一点并将其组合在一起。看 苹果的Altivec页面. 。我也在使用 lvlxlvrx 加载说明,然后将它们一起演奏。


接下来,我不确定您的成倍增加和添加是否相同。我从来没有使用过_mm_madd_pi16或vec_msum,所以我不是正等同的。您应该在调试器中进行逐步完成,并确保它们为相同的输入数据提供相同的输出。另一个可能的区别是它们可能以不同的方式对待溢出(例如,模块化与饱和)。


最后但并非最不重要的一点是,您一次计算4个INT,而不是2。因此,您的联合应该容纳4个INT,您应该在最后总结所有4个INT。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top