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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T07:47:35+00:00 2026-06-15T07:47:35+00:00

Sorry for the previous non-descriptive question. Please allow me to rephrase the question again:

  • 0

Sorry for the previous non-descriptive question. Please allow me to rephrase the question again:

The setup:

I need to do ADD and some bit wise operations of 4 32-bit values from 4 arrays at the same time using SSE. All the element in these 4 arrays are integer size (32 bit). The result goes to the 5th array.

So my question is:

  1. What header files and compiler flags do I need to include such that I can run the SSE using C?
  2. Does the example code provide by Paul still work?

Another question, if I need to read last bit from integer A, and first bit from integer B, and replace the last bit and first bit in integer C by the values I just read, can I use SSE here? Or is there any fast way to do it? Instead of 3 access in normal case?

Code provided by Paul

#include <stdint.h>
#include <emmintrin.h>

const size_t N = 4096;  // size of input/output arrays

int32_t array0[N];      // 4 x input arrays
int32_t array1[N];
int32_t array2[N];
int32_t array3[N];
int32_t array_sum[N];   // output array

for (size_t i = 0; i < N; i += 4)
{
    __m128i v0 = _mm_load_si128(&array0[i]); // load 4 x vectors of 4 x int
    __m128i v1 = _mm_load_si128(&array1[i]);
    __m128i v2 = _mm_load_si128(&array2[i]);
    __m128i v3 = _mm_load_si128(&array3[i]);
    __m128i vsum = _mm_add_epi32(v0, v1);    // sum vectors
    __m128i vsum = _mm_add_epi32(vsum, v2);
    __m128i vsum = _mm_add_epi32(vsum, v3);
    _mm_store_si128(&array_out[i], vsum);    // store sum
}
  • 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-15T07:47:36+00:00Added an answer on June 15, 2026 at 7:47 am

    The code you have will indeed work, with some slight changes.

    #include <stdint.h>
    #include <emmintrin.h>
    
    const size_t N = 4096;  // size of input/output arrays
    
    __declspec(align(16)) int32_t array0[N];      // 4 x input arrays
    __declspec(align(16)) int32_t array1[N];
    __declspec(align(16)) int32_t array2[N];
    __declspec(align(16)) int32_t array3[N];
    __declspec(align(16)) int32_t array_sum[N];   // output array
    
    for (size_t i = 0; i < N; i += 4)
    {
        __m128i v0 = _mm_load_si128(&array0[i]); // load 4 x vectors of 4 x int
        __m128i v1 = _mm_load_si128(&array1[i]);
        __m128i v2 = _mm_load_si128(&array2[i]);
        __m128i v3 = _mm_load_si128(&array3[i]);
        __m128i vsum = _mm_add_epi32(v0, v1);    // sum vectors
        __m128i vsum = _mm_add_epi32(vsum, v2);
        __m128i vsum = _mm_add_epi32(vsum, v3);
        _mm_store_si128(&array_sum[i], vsum);    // store sum
    }
    

    When it comes to working with intrinsics, I like including <immintrin.h> and compiling with gcc -march=native. This gives access to all the instruction set extensions actually available on the current hardware.

    On the other question, yes you can certainly do that, but it is only efficient if you do it on arrays of integers A, B, and C.

    Example:

    __declspec(align(16)) int32_t A[N]; // input arrays
    __declspec(align(16)) int32_t B[N];
    __declspec(align(16)) int32_t C[N];
    __declspec(align(16)) int32_t R[N]; // output array
    
    __m128i* As = (__m128i*)A; // cast them to SSE type, avoids separate load/store calls later
    __m128i* Bs = (__m128i*)B;
    __m128i* Cs = (__m128i*)C;
    __m128i* Rs = (__m128i*)R;
    
    __m128i A_mask = _mm_set1_epi32(1<<31); // select these bits from A, B, and C
    __m128i B_mask = _mm_set1_epi32(1);
    __m128i C_mask = _mm_set1_epi32(0xffffffff ^ ( 1<<31 | 1 ));
    
    for (size_t i = 0; i < N / 4; i ++)
    {
        __m128i a = _mm_and_si128(A_mask, As[i]);
        __m128i b = _mm_and_si128(B_mask, Bs[i]);
        __m128i c = _mm_and_si128(C_mask, Cs[i]);
        Rs[i] = _mm_or_si128( _mm_or_si128(a, b), c );
    }
    

    Aliasing the int32_t arrays to __m128i as I have done above is not more efficient, it should compile to the exact same code if the compiler is decent, but it results in much less verbose code. Recommended 🙂

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

Sidebar

Related Questions

me again. Sorry about this. Following on from my previous question (I think I
Sorry for the redundancy, I should've asked this in my previous question here: What's
My previous question got a lot cons. I will try again, with a code
I'm very sorry for my previous miswording. So I rephrased this question as follows:
Sorry I changed the previous question. I have problem with .htaccess rewrite rule on
Sorry about my previous question. The question I have to answer is this: Body
sorry if this question seems a bit open ended, but I'm often wanting to
Sorry but yet another question on JavaScript. From my previous post, I was able
and sorry should the question be redundant, but I've been looking through previous questions
I m really sorry for unclear question title but I am a bit confused

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.