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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T03:43:03+00:00 2026-05-15T03:43:03+00:00

Given the arrays: int canvas[10][10]; int addon[10][10]; Where all the values range from 0

  • 0

Given the arrays:

int canvas[10][10];
int addon[10][10];

Where all the values range from 0 – 100, what is the fastest way in C++ to add those two arrays so each cell in canvas equals itself plus the corresponding cell value in addon?

IE, I want to achieve something like:

canvas += another;

So if canvas[0][0] =3 and addon[0][0] = 2 then canvas[0][0] = 5

Speed is essential here as I am writing a very simple program to brute force a knapsack type problem and there will be tens of millions of combinations.

And as a small extra question (thanks if you can help!) what would be the fastest way of checking if any of the values in canvas exceed 100? Loops are slow!

  • 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-15T03:43:04+00:00Added an answer on May 15, 2026 at 3:43 am

    Here is an SSE4 implementation that should perform pretty well on Nehalem (Core i7):

    #include <limits.h>
    #include <emmintrin.h>
    #include <smmintrin.h>
    
    static inline int canvas_add(int canvas[10][10], int addon[10][10])
    {
        __m128i * cp = (__m128i *)&canvas[0][0];
        const __m128i * ap = (__m128i *)&addon[0][0];
        const __m128i vlimit = _mm_set1_epi32(100);
        __m128i vmax = _mm_set1_epi32(INT_MIN);
        __m128i vcmp;
        int cmp;
        int i;
    
        for (i = 0; i < 10 * 10; i += 4)
        {
            __m128i vc = _mm_loadu_si128(cp);
            __m128i va = _mm_loadu_si128(ap);
    
            vc = _mm_add_epi32(vc, va);
            vmax = _mm_max_epi32(vmax, vc);   // SSE4 *
    
            _mm_storeu_si128(cp, vc);
    
            cp++;
            ap++;
        }
        vcmp = _mm_cmpgt_epi32(vmax, vlimit); // SSE4 *
        cmp = _mm_testz_si128(vcmp, vcmp);    // SSE4 *
        return cmp == 0;
    }
    

    Compile with gcc -msse4.1 ... or equivalent for your particular development environment.

    For older CPUs without SSE4 (and with much more expensive misaligned loads/stores) you’ll need to (a) use a suitable combination of SSE2/SSE3 intrinsics to replace the SSE4 operations (marked with an * above) and ideally (b) make sure your data is 16-byte aligned and use aligned loads/stores (_mm_load_si128/_mm_store_si128) in place of _mm_loadu_si128/_mm_storeu_si128.

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

Sidebar

Related Questions

Given an array a and two other int variables, k and temp, write a
Given this piece of code: (void)someFunction(void) { int array[] = {1,2,3,4,5,6,7,8,9,10}; } Where are
Given a list of arrays and lots of setup time I need to quickly
I have a string. I need to replace all instances of a given array
Given an array of integers, what is the simplest way to iterate over it
Given an array like {one two, three four five}, how'd you calculate the total
Given this array int [] myArray = {5,-11,2,3,14,5,-14,2}; I must be able to return
Given an array of n Objects, let's say it is an array of strings
Given an array of characters which forms a sentence of words, give an efficient
Given an array of items, each of which has a value and cost ,

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.