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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T19:48:42+00:00 2026-05-25T19:48:42+00:00

It struck me there must be a clever way to do this. This isn’t

  • 0

It struck me there must be a clever way to do this. This isn’t for homework, or work or anything. I was just noodling around with a file format that has data interleaved.

So, in generic C/C++, (or whatever) given some array

int x[] = ...

is there a clever way of splitting it into two short arrays

short sa1[], sa2[]

such that the int array is split down the middle

x[i] = 1111111111111111 1111111111111111
             sa1[i]         sa2[i]

Edit: Sorry if this is not phrased well. For each i-th element of the int array, the left-most 16 bits become the i-th element of one array, and the right-most 16bits become the i-th element of a 2nd array.

so given

x[i] = 0001111111111111 1111111100011111

then

sa1[i] = 0001111111111111
sa2[i] = 1111111100011111

I’m looking for non-obvious answers that do not loop over each element and shift and mask each element. That’s easy 🙂

  • 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-25T19:48:42+00:00Added an answer on May 25, 2026 at 7:48 pm

    There’s a lot of ways to do this:

    Assumptions:

    1. short is 16 bits.
    2. int is 32 bits.

    Method 1: (A simple loop)

    for (int i = 0; i < size; i++){
        int tmp = x[i];
        sa1[i] = (tmp      ) & 0xffff;
        sa2[i] = (tmp >> 16) & 0xffff;
    }
    

    Method 2: SSE2

    for (int i = 0; i < size / 8; i++){
        __m128i a0 = ((__m128i*)x)[2*i + 0];
        __m128i a1 = ((__m128i*)x)[2*i + 1];
    
        a0 = _mm_shufflelo_epi16(a0,216);
        a1 = _mm_shufflelo_epi16(a1,216);
        a0 = _mm_shufflehi_epi16(a0,216);
        a1 = _mm_shufflehi_epi16(a1,216);
        a0 = _mm_shuffle_epi32(a0,216);
        a1 = _mm_shuffle_epi32(a1,216);
    
        ((__m128i*)sa1)[i] = _mm_unpacklo_epi64(a0,a1);
        ((__m128i*)sa2)[i] = _mm_unpackhi_epi64(a0,a1);
    }
    

    This last example is very fast if the loop is further unrolled. I won’t be surprised if this can beat all byte-manipulation libraries.

    However, it has the following restrictions:

    1. The data must be aligned to 16 bytes.
    2. The number of iterations must be divisible by 8.
    3. It requires SSE2.

    The first two of these can be solved by cleanup code. It’s messy, but if you really desire performance, it may be worth it.

    EDIT:

    Yes this violates strict-aliasing, but it’s nearly impossible to use SSE intrinsics without doing so.

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

Sidebar

Related Questions

I'm stuck on this pretty simple problem and I know there must be a
This must be a relatively newb question but I am still stuck. I know
There are so many plugins/gems but must of them have outdated documentation or no
I'm at a complete loss. I feel like there must be some glaring, stupidly
This question is equal parts C# and Salesforce, there are probably solutions possible from
Is there a way to combine what's going to be output by static_assert? What
Yes I know there has been similar posts to this however after looking through
I always get stuck with managing rotation on iOS application, there must be some
I have been stuck for so long on this and just can not get
is there a difference between a struct in c++ and a struct in c#?

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.