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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T03:43:56+00:00 2026-06-07T03:43:56+00:00

This is a question that came to mind while reading the brilliant answer by

  • 0

This is a question that came to mind while reading the brilliant answer by Mysticial to the question: why is it faster to process a sorted array than an unsorted array?

Context for the types involved:

const unsigned arraySize = 32768;
int data[arraySize];
long long sum = 0;

In his answer he explains that the Intel Compiler (ICC) optimizes this:

for (int i = 0; i < 100000; ++i)
    for (int c = 0; c < arraySize; ++c)
        if (data[c] >= 128)
            sum += data[c];

…into something equivalent to this:

for (int c = 0; c < arraySize; ++c)
    if (data[c] >= 128)
        for (int i = 0; i < 100000; ++i)
            sum += data[c];

The optimizer is recognizing that these are equivalent and is therefore exchanging the loops, moving the branch outside the inner loop. Very clever!

But why doesn’t it do this?

for (int c = 0; c < arraySize; ++c)
    if (data[c] >= 128)
        sum += 100000 * data[c];

Hopefully Mysticial (or anyone else) can give an equally brilliant answer. I’ve never learned about the optimizations discussed in that other question before, so I’m really grateful for this.

  • 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-07T03:43:57+00:00Added an answer on June 7, 2026 at 3:43 am

    The compiler can’t generally transform

    for (int c = 0; c < arraySize; ++c)
        if (data[c] >= 128)
            for (int i = 0; i < 100000; ++i)
                sum += data[c];
    

    into

    for (int c = 0; c < arraySize; ++c)
        if (data[c] >= 128)
            sum += 100000 * data[c];
    

    because the latter could lead to overflow of signed integers where the former doesn’t. Even with guaranteed wrap-around behaviour for overflow of signed two’s complement integers, it would change the result (if data[c] is 30000, the product would become -1294967296 for the typical 32-bit ints with wrap around, while 100000 times adding 30000 to sum would, if that doesn’t overflow, increase sum by 3000000000). Note that the same holds for unsigned quantities, with different numbers, overflow of 100000 * data[c] would typically introduce a reduction modulo 2^32 that must not appear in the final result.

    It could transform it into

    for (int c = 0; c < arraySize; ++c)
        if (data[c] >= 128)
            sum += 100000LL * data[c];  // resp. 100000ull
    

    though, if, as usual, long long is sufficiently larger than int.

    Why it doesn’t do that, I can’t tell, I guess it’s what Mysticial said, “apparently, it does not run a loop-collapsing pass after loop-interchange”.

    Note that the loop-interchange itself is not generally valid (for signed integers), since

    for (int c = 0; c < arraySize; ++c)
        if (condition(data[c]))
            for (int i = 0; i < 100000; ++i)
                sum += data[c];
    

    can lead to overflow where

    for (int i = 0; i < 100000; ++i)
        for (int c = 0; c < arraySize; ++c)
            if (condition(data[c]))
                sum += data[c];
    

    wouldn’t. It’s kosher here, since the condition ensures all data[c] that are added have the same sign, so if one overflows, both do.

    I wouldn’t be too sure that the compiler took that into account, though (@Mysticial, could you try with a condition like data[c] & 0x80 or so that can be true for positive and negative values?). I had compilers make invalid optimisations (for example, a couple of years ago, I had an ICC (11.0, iirc) use signed-32-bit-int-to-double conversion in 1.0/n where n was an unsigned int. Was about twice as fast as gcc’s output. But wrong, a lot of values were larger than 2^31, oops.).

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

Sidebar

Related Questions

This question came to my mind while I am reading the post Why doesn't
This question came into my mind while generating sample data for a SO-answer. I
This question came to mind after reading the answer to this question; which basically
This question came to mind while I was writing a class that iterates over
This question came to my mind while working on 2 projects in AI and
So here I am tonight with this question that came up into my mind
I am preparing for my interview and came across this question: Consider that i
This question came about because the cells gem specifies template directories using File.join('app','cells'). That
This question came to my mind quite a few times. Let my explain my
This question has just came in to my mind, and I'm not in a

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.