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 8063933
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T11:12:31+00:00 2026-06-05T11:12:31+00:00

This is my very first time working with SSE intrinsics. I am trying to

  • 0

This is my very first time working with SSE intrinsics. I am trying to convert a simple piece of code into a faster version using Intel SSE intrinsic (up to SSE4.2). I seem to encounter a number of errors.

The scalar version of the code is: (simple matrix multiplication)

     void mm(int n, double *A, double *B, double *C)
     {
        int i,j,k;
        double tmp;

        for(i = 0; i < n; i++)
            for(j = 0; j < n; j++) {
                    tmp = 0.0;
                    for(k = 0; k < n; k++)
                            tmp += A[n*i+k] *
                                   B[n*k+j];
                    C[n*i+j] = tmp;

              }
            }

This is my version: I have included #include <ia32intrin.h>

      void mm_sse(int n, double *A, double *B, double *C)
      {
        int i,j,k;
        double tmp;
        __m128d a_i, b_i, c_i;

        for(i = 0; i < n; i++)
            for(j = 0; j < n; j++) {
                    tmp = 0.0;
                    for(k = 0; k < n; k+=4)
                            a_i = __mm_load_ps(&A[n*i+k]);
                            b_i = __mm_load_ps(&B[n*k+j]);
                            c_i = __mm_load_ps(&C[n*i+j]);

                            __m128d tmp1 = __mm_mul_ps(a_i,b_i);
                            __m128d tmp2 = __mm_hadd_ps(tmp1,tmp1);
                            __m128d tmp3 = __mm_add_ps(tmp2,tmp3);
                            __mm_store_ps(&C[n*i+j], tmp3);

            }
         }

Where am I going wrong with this? I am getting several errors like this:

mm_vec.c(84): error: a value of type “int” cannot be assigned to an entity of type “__m128d”
a_i = __mm_load_ps(&A[n*i+k]);

This is how I am compiling: icc -O2 mm_vec.c -o vec

Can someone please assist me converting this code accurately. Thanks!

UPDATE:

According to your suggestions, I have made the following changes:

       void mm_sse(int n, float *A, float *B, float *C)
       {
         int i,j,k;
         float tmp;
         __m128 a_i, b_i, c_i;

         for(i = 0; i < n; i++)
            for(j = 0; j < n; j++) {
                    tmp = 0.0;
                    for(k = 0; k < n; k+=4)
                            a_i = _mm_load_ps(&A[n*i+k]);
                            b_i = _mm_load_ps(&B[n*k+j]);
                            c_i = _mm_load_ps(&C[n*i+j]);

                            __m128 tmp1 = _mm_mul_ps(a_i,b_i);
                            __m128 tmp2 = _mm_hadd_ps(tmp1,tmp1);
                            __m128 tmp3 = _mm_add_ps(tmp2,tmp3);
                            _mm_store_ps(&C[n*i+j], tmp3);


            }
        }

But now I seem to be getting a Segmentation fault. I know this perhaps because I am not accessing the array subscripts properly for array A,B,C. I am very new to this and not sure how to proceed with this.

Please help me determine the correct approach towards handling this code.

  • 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-06-05T11:12:33+00:00Added an answer on June 5, 2026 at 11:12 am

    The error you’re seeing is because you have too many underscores in the function names, e.g.:

    __mm_mul_ps
    

    should be:

    _mm_mul_ps // Just one underscore up front
    

    so the C compiler is assuming they return int since it hasn’t seen a declaration.

    Beyond this though there’s further problems – you seem to be mixing calls to double and single float variants of the same instruction.

    For example you have:

    __m128d a_i, b_i, c_i;
    

    but you call:

    __mm_load_ps(&A[n*i+k]);
    

    which returns a __m128 not a __m128d – you wanted to call:

    _mm_load_pd
    

    instead. Likewise for the other instructions if you want them to work on pairs of doubles.


    If you’re seeing unexplained segmentation faults and in SSE code I’d be inclined to guess that you’ve got memory alignment problems – pointers passed to SSE intrinsics (mostly1) need to be 16 byte aligned. You can check this with a simple assert in your code, or check it in a debugger (you expect the last digit of the pointer to be 0 if it’s aligned properly).

    If it isn’t aligned right you need to make sure it is. For things not allocated with new/malloc() you can do this with a compiler extension (e.g. with gcc):

    float a[16] __attribute__ ((aligned (16)));
    

    Provided your version of gcc has a max alignment large enough to support this and a few other caveats about stack alignment. For dynamically allocated storage you’ll want to use a platform specific extension, e.g. posix_memalign to allocate suitable storage:

    float *a=NULL;
    posix_memalign(&a, __alignof__(__m128), sizeof(float)*16);
    

    (I think there might be nicer, portable ways of doing this with C++11 but I’m not 100% sure on that yet).

    1 There are some instructions which allow you do to unaligned loads and stores, but they’re terribly slow compared to aligned loads and worth avoiding if at all possible.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to write my very first python script. This was working but
I'm very new to trigger function. Actually this is the first time I'm using
i'm trying to upload my app engine project for the very first time and
My data from getmovies.php is working correctly and loading into #moviesPage the FIRST time
This is the first time I came across this thing, and I've been working
Long time reader, first time asker. Anyway, Here's the code I'm working with: class
This is my first time working with a JSON and I am clearly getting
I'm working on a library (C++) that will be integrated into clients code. This
This is my VERY FIRST Makefile and so I have cut and paste junk
I'm going a little mad I think. This is the VERY first thing I

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.