Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3450688
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T09:04:31+00:00 2026-05-18T09:04:31+00:00

Let me preface this with.. I have extremely limited experience with ASM, and even

  • 0

Let me preface this with.. I have extremely limited experience with ASM, and even less with SIMD.

But it happens that I have the following MMX/SSE optimised code, that I would like to port across to AltiVec instructions for use on PPC/Cell processors.

This is probably a big ask.. Even though it’s only a few lines of code, I’ve had no end of trouble trying to work out what’s going on here.

The original function:

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;
}

Any tips on how I might rewrite this to use AltiVec instructions?

My first attempt (a very wrong attempt) looks something like this.. But it’s not entirely (or even remotely) correct.

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;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T09:04:31+00:00Added an answer on May 18, 2026 at 9:04 am

    You’re not far off – I fixed a few minor problems, cleaned up the code a little, added a test harness, and it seems to work OK now:

    #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;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let me preface this by saying that I'm pretty new to Java. I have
First off, let me preface this question by stating that I'm really a pretty
Let me preface this with the disclaimer that I am very new to multithreading
So let me preface this by saying that I'm not an SQL wizard by
Let me preface this question by saying I use TextMate on Mac OSX for
Let me preface this by saying I'm a complete amateur when it comes to
Let's say you have a class called Customer, which contains the following fields: UserName
Let me try to explain what I need. I have a server that is
Let me preface this by saying I now realize how stupid I am and
Let me preface this post with a single caution. I am a total beginner

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.