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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T23:52:55+00:00 2026-05-22T23:52:55+00:00

I am trying to optimize a small piece of code with SSE intrinsics (I

  • 0

I am trying to optimize a small piece of code with SSE intrinsics (I am a complete beginner on the topic), but I am a little stuck on the use of conditionals.

My original code is:

unsigned long c;
unsigned long constant = 0x12345678;
unsigned long table[256];
int n, k;

for( n = 0; n < 256; n++ )
{
  c = n;
  for( k = 0; k < 8; k++ )
    {
      if( c & 1 ) c = constant ^ (c >> 1);
      else c >>= 1;
    }
  table[n] = c;
}

The goal of this code is to compute a crc table (the constant can be any polynomial, it doesn’t play a role here),

I suppose my optimized code would be something like:

__m128 x;
__m128 y;
__m128 *table;

x = _mm_set_ps(3, 2, 1, 0);
y = _mm_set_ps(3, 2, 1, 0);
//offset for incrementation
offset = _mm_set1_ps(4);

for( n = 0; n < 64; n++ )
{
    y = x;
    for( k = 0; k < 8; k++ )
    {
        //if do something with y
        //else do something with y
    }
    table[n] = y;
    x = _mm_add_epi32 (x, offset);
}

I have no idea how to go through the if-else statement, but I suspect there is a clever trick. Has anybody an idea on how to do that?

(Aside from this, my optimization is probably quite poor – any advice or correction on it would be treated with the greatest sympathy)

  • 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-22T23:52:56+00:00Added an answer on May 22, 2026 at 11:52 pm

    You can get rid of the if/else entirely. Back in the days when I produced MMX assembly code, that was a common programming activity. Let me start with a series of transformations on the “false” statement:

    c >>= 1;
    
    c = c >> 1;
    
    c = 0 ^ (c >> 1);
    

    Why did I introduce the exclusive-or? Because exclusive-or is also found in the “true” statement:

    c = constant ^ (c >> 1);
    

    Note the similarity? In the “true” part, we xor with a constant, and in the false part, we xor with zero.

    Now I’m going to show you a series of transformations on the entire if/else statement:

    if (c & 1)
        c = constant ^ (c >> 1);          // same as before
    else
        c =        0 ^ (c >> 1);          // just different layout
    
    if (c & 1)
        c =  constant      ^ (c >> 1);
    else
        c = (constant & 0) ^ (c >> 1);    // 0 == x & 0
    
    if (c & 1)
        c = (constant & -1) ^ (c >> 1);   // x == x & -1
    else
        c = (constant &  0) ^ (c >> 1);
    

    Now the two branches only differ in the second argument to the binary-and, which can be calculated trivially from the condition itself, thus enabling us to get rid of the if/else:

    c = (constant & -(c & 1)) ^ (c >> 1);
    

    Disclaimer: This solution only works on a two’s complement architecture where -1 means “all bits set”.

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

Sidebar

Related Questions

I am trying to optimize a small, highly used function which uses the high
I'm trying to optimize 'in the small' on a project of mine. There's a
I'm trying to optimize the following. The code bellow does this : If a
I have written a feed aggregator before but am trying to optimize it a
I am trying to optimize some stored procedures on a SQL Server 2000 database
I'm trying to optimize query performance and have had to resort to using optimizer
I'm trying to optimize a stored procedure I'm maintaining, and am wondering if anyone
I'm having trouble trying to optimize the following query for sql server 2005. Does
I've got a web application that I'm trying to optimize. Some of the controls
I'm trying to optimize the size of my Delphi classes so that they take

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.