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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T19:11:08+00:00 2026-05-22T19:11:08+00:00

I need to compare two buffers chunk-wise for equality. I don’t need information about

  • 0

I need to compare two buffers chunk-wise for equality. I don’t need information about the relation of the two buffers, just if each two chunks are equal or not. My intel machine supports up to SSE4.2

The naive approach is:

const size_t CHUNK_SIZE = 16; //128bit for SSE2 integer registers
const int ARRAY_SIZE = 200000000;

char* array_1 = (char*)_aligned_malloc(ARRAY_SIZE, 16);
char* array_2 = (char*)_aligned_malloc(ARRAY_SIZE, 16);

for (size_t i = 0; i < ARRAY_SIZE; )
{
    volatile bool result = memcmp(array_1+i, array_2+i, CHUNK_SIZE);
    i += CHUNK_SIZE;
}

Compared to my first try using SSE ever:

union U
{
    __m128i m;
    volatile int i[4];
} res;

for (size_t i = 0; i < ARRAY_SIZE; )
{
    __m128i* pa1 = (__m128i*)(array_1+i);
    __m128i* pa2 = (__m128i*)(array_2+i);
    res.m = _mm_cmpeq_epi32(*pa1, *pa2);
    volatile bool result =  ( (res.i[0]==0) || (res.i[1]==0) || (res.i[2]==0) || (res.i[3]==0) );
    i += CHUNK_SIZE;
}

The gain in speed is about 33%. Could I do any better?

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

    You really shouldn’t be using scalar code and unions to test all the individual vector elements – do something like this instead:

    for (size_t i = 0; i < ARRAY_SIZE; i += CHUNK_SIZE)
    {
        const __m128i a1 = _mm_load_si128(array_1 + i);
        const __m128i a2 = _mm_load_si128(array_2 + i);
        const __m128i vcmp = _mm_cmpeq_epi32(a1, a2);
        const int vmask = _mm_movemask_epi8(vcmp);
        const bool result = (vmask == 0xffff);
        // you probably want to break here if you get a mismatch ???
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to compare the integer part of two doubles for inequality and I'm
I need to compare two dates using the Oracle decode function to see if
I need to compare two numbers and look for similarities in more significant bits.
i need to compare two objects but compare a number of their properties in
In my Java application i need to compare two list's element whether it is
I have two dates, formated like "Y-m-d H:i:s". I need to compare these two
I need to compare two fix messages (say two ExecutionReports) in QuickFIXJ. Let's call
I need to compare two different files of the instance File in Java and
I need to compare strings in shell: var1=mtu eth0 if [ $var1 == mtu
I need to compare 2 strings as equal such as these: Lubeck == Lübeck

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.