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

  • Home
  • SEARCH
  • 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 5948231
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T17:04:42+00:00 2026-05-22T17:04:42+00:00

I am creating a multi-dimensional vector (mathematical vector) where I allow basic mathematical operations

  • 0

I am creating a multi-dimensional vector (mathematical vector) where I allow basic mathematical operations +,-,/,*,=. The template takes in two parameters, one is the type (int, float etc.) while the other is the size of the vector. Currently I am applying the operations via a for loop. Now considering the size is known at compile time, will the compiler unroll the loop? If not, is there a way to unroll it with no (or minimal) performance penalty?

template <typename T, u32 size>
class Vector
{
public:
    // Various functions for mathematical operations. 
    // The functions take in a Vector<T, size>.
    // Example:
    void add(const Vector<T, size>& vec)
    {
        for (u32 i = 0; i < size; ++i)
        {
            values[i] += vec[i];
        }
    }

private:
    T   values[size];
};

Before somebody comments Profile then optimize please note that this is the basis for my 3D graphics engine and it must be fast. Second, I want to know for the sake of educating myself.

  • 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-22T17:04:42+00:00Added an answer on May 22, 2026 at 5:04 pm

    You can do the following trick with disassembly to see how the particular code is compiled.

        Vector<int, 16> a, b;
        Vector<int, 65536> c, d;
    
        asm("xxx"); // marker
        a.Add(b);
        asm("yyy"); // marker
        c.Add(d);
        asm("zzz"); // marker
    

    Now compile

    gcc -O3 1.cc -S -o 1.s
    

    And see the disasm

        xxx
    # 0 "" 2
    #NO_APP
        movdqa  524248(%rsp), %xmm0
        leaq    524248(%rsp), %rsi
        paddd   524184(%rsp), %xmm0
        movdqa  %xmm0, 524248(%rsp)
        movdqa  524264(%rsp), %xmm0
        paddd   524200(%rsp), %xmm0
        movdqa  %xmm0, 524264(%rsp)
        movdqa  524280(%rsp), %xmm0
        paddd   524216(%rsp), %xmm0
        movdqa  %xmm0, 524280(%rsp)
        movdqa  524296(%rsp), %xmm0
        paddd   524232(%rsp), %xmm0
        movdqa  %xmm0, 524296(%rsp)
    #APP
    # 36 "1.cc" 1
        yyy
    # 0 "" 2
    #NO_APP
        leaq    262040(%rsp), %rdx
        leaq    -104(%rsp), %rcx
        xorl    %eax, %eax
        .p2align 4,,10
        .p2align 3
    .L2:
        movdqa  (%rcx,%rax), %xmm0
        paddd   (%rdx,%rax), %xmm0
        movdqa  %xmm0, (%rdx,%rax)
        addq    $16, %rax
        cmpq    $262144, %rax
        jne .L2
    #APP
    # 38 "1.cc" 1
        zzz
    

    As you see, the first loop was small enough to get unrolled. The second is the loop.

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

Sidebar

Related Questions

Problem Statement: I'm creating a template for multi tiered complicated calculations in MS Excel
I'm creating additional module to already multi-module maven project. And for this one I
Creating hashes of hashes in Ruby allows for convenient two (or more) dimensional lookups.
I am trying to expand on my question asked here: Creating a multi-dimensional array
I am trying to work with TinyMCE in creating a multi-textbox, click-to-edit type graphical
I'm creating a multi-part web form in ASP.NET that uses Panels for the different
I'm creating a multi-tenancy web site which hosts pages for clients. The first segment
I am creating some multi-threaded code, and I have created a JobDispatcher class that
I'm creating a multi-tenant ASP.NET application using NHibernate (shared schema). I had intended to
I am manually creating a multi-part MIME-formatted file and submitting it to an SMTP

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.