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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T02:26:12+00:00 2026-05-24T02:26:12+00:00

I’m currently trying to most efficiently do an in-place multiplication of an array of

  • 0

I’m currently trying to most efficiently do an in-place multiplication of an array of complex numbers (memory aligned the same way the std::complex would be but currently using our own ADT) by an array of scalar values that is the same size as the complex number array.

The algorithm is already parallelized, i.e. the calling object splits the work up into threads. This calculation is done on arrays in the 100s of millions – so, it can take some time to complete. CUDA is not a solution for this product, although I wish it was. I do have access to boost and thus have some potential to use BLAS/uBLAS.

I’m thinking, however, that SIMD might yield much better results, but I’m not familiar enough with how to do this with complex numbers. The code I have now is as follows (remember this is chunked up into threads which correspond to the number of cores on the target machine). The target machine is also unknown. So, a generic approach is probably best.

void cmult_scalar_inplace(fcomplex *values, const int start, const int end, const float *scalar)
{
    for (register int idx = start; idx < end; ++idx)
    {
        values[idx].real *= scalar[idx];
        values[idx].imag *= scalar[idx];
    }
}

fcomplex is defined as follows:

struct fcomplex
{
    float real;
    float imag;
};

I’ve tried manually unrolling the loop, as my finally loop count will always be a power of 2, but the compiler is already doing that for me (I’ve unrolled as far as 32). I’ve tried a const float reference to the scalar – in thinking I’d save one access – and that proved to be equal to the what the compiler was already doing. I’ve tried STL and transform, which game close results, but still worse. I’ve also tried casting to std::complex and allow it to use the overloaded operator for scalar * complex for the multiplication but this ultimately produced the same results.

So, anyone with any ideas? Much appreciation is given for your time in considering this! Target platform is Windows. I’m using Visual Studio 2008. Product cannot contain GPL code as well! Thanks so much.

  • 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-24T02:26:13+00:00Added an answer on May 24, 2026 at 2:26 am

    You can do this fairly easily with SSE, e.g.

    void cmult_scalar_inplace(fcomplex *values, const int start, const int end, const float *scalar)
    {
        for (int idx = start; idx < end; idx += 2)
        {
            __m128 vc = _mm_load_ps((float *)&values[idx]);
            __m128 vk = _mm_set_ps(scalar[idx + 1], scalar[idx + 1], scalar[idx], scalar[idx]);
            vc = _mm_mul_ps(vc, vk);
            _mm_store_ps((float *)&values[idx], vc);
        }
    }
    

    Note that values and scalar need to be 16 byte aligned.

    Or you could just use the Intel ICC compiler and let it do the hard work for you.


    UPDATE

    Here is an improved version which unrolls the loop by a factor of 2 and uses a single load instruction to get 4 scalar values which are then unpacked into two vectors:

    void cmult_scalar_inplace(fcomplex *values, const int start, const int end, const float *scalar)
    {
        for (int idx = start; idx < end; idx += 4)
        {
            __m128 vc0 = _mm_load_ps((float *)&values[idx]);
            __m128 vc1 = _mm_load_ps((float *)&values[idx + 2]);
            __m128 vk = _mm_load_ps(&scalar[idx]);
            __m128 vk0 = _mm_shuffle_ps(vk, vk, 0x50);
            __m128 vk1 = _mm_shuffle_ps(vk, vk, 0xfa);
            vc0 = _mm_mul_ps(vc0, vk0);
            vc1 = _mm_mul_ps(vc1, vk1);
            _mm_store_ps((float *)&values[idx], vc0);
            _mm_store_ps((float *)&values[idx + 2], vc1);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I want use html5's new tag to play a wav file (currently only supported
I am trying to render a haml file in a javascript response like so:
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I am currently running into a problem where an element is coming back from
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka

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.