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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T22:44:56+00:00 2026-06-12T22:44:56+00:00

How do you use __m256d ? Say I want to use the Intel AVX

  • 0

How do you use __m256d?

Say I want to use the Intel AVX instruction _mm256_add_pd on a simple Vector3 class with 3-64 bit double precision components (x, y, and z). What is the correct way to use this?

Since x, y and z are members of the Vector3 class, _can I declare them in union with an __m256d variable?

union Vector3
{
  struct { double x,y,z ; } ;
  __m256d _register ;  // the Intel register?
} ;

Then can I go:

Vector3 add( const Vector3& o )
{
  Vector3 result;
  result._register = _mm256_add_pd( _register, o._register ) ; // add 'em
  return result; 
}

Is that going to work? Or do I need to declare temporaries,

Vector3 add( const Vector3& o )
{
  __m256d d1 = *(__m256d*)(&x) ; // ? Cast to __m256d?
  __m256d d2 = *(__m256d*)(&o.x) ; // ? Cast to __m256d?
  __m256d result = _mm256_add_pd( d1, d2 ) ; // add 'em
  return Vector3( result ) ; // make a ctor that accepts __m256d?
}

Edit

I came up with this example,

#include <stdio.h>
#include <intrin.h>

int main()
{
  __m256d a, b, res;

  for( int i = 0; i < sizeof(__m256d)/sizeof(double); i++ )
  {
    a.m256d_f64[i] = i ;
    b.m256d_f64[i] = 2*i ;
  }

  // Perform __4__ adds.
  res = _mm256_add_pd(a, b);

  for( int i = 0; i < sizeof(__m256d)/sizeof(double); i++ )
  {
    printf("%f + %f = %f\n", a.m256d_f64[i], b.m256d_f64[i], res.m256d_f64[i]);
  }
  puts("");
}

I guess the question is now, does _mm256_add_pd do load operations automatically, or will something get messed up if I don’t declare my __m256d registers as locals close to where they are used? (I’m afraid of a hotel room / deskdrawer type problem)

Edit 2:

I tried adding an __m256 register to my rather large project, and I got a whole bunch of

error C2719: ‘value’: formal parameter with __declspec(align(’32’)) won’t be aligned

Errors, it leads me to believe that you can’t keep __m256 registers inside a class, instead they should be declared as locals?

  • 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-12T22:44:57+00:00Added an answer on June 12, 2026 at 10:44 pm

    First I’d like to clear up a little confusion. __m256d isn’t a type of register, it’s a data type that can be loaded into an AVX register. A __m256d is no more a register than an int is a register. There are a few ways to get data in and out of an __m256d (or any other vector type):

    Using a union: Yes, the union trick works. It works very well, since the union will generally have the correct alignment (although malloc might not, use posix_memalign or _aligned_malloc).

    class Vector3 {
    public:
        Vector3(double xx, double yy, double zz);
        Vector3(__m256d vvec);
    
    
        Vector3 operator+(const Vector3 &other) const
        {
            return Vector3(_mm256_add_pd(vec, other.vec));
        }
    
        union {
            struct {
                double x, y, z;
            };
            __m256d vec; // a data field, maybe a register, maybe not
        };
    };
    

    Using intrinsics: Inside a function, it’s usually easier to use intrinsics to get data in and out of a vector type.

    __m256d vec = ...;
    double x, y, z;
    vec = _mm256_add_pd(vec, _mm256_set_pd(x, y, z, 0.0));
    

    Using pointer casts: Casting pointers is the last resort for a couple of reasons.

    1. The pointer might not be aligned correctly.

    2. Casting pointers can sometimes mess with the compiler’s aliasing analysis.

    3. Pointer casting bypasses a number of safety guarantees.

    So I’d only use pointer casting to plow through a big array of data.

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

Sidebar

Related Questions

The Intel Advanced Vector Extensions (AVX) offers no dot product in the 256-bit version
use case is simple: I want to run some boiler plate code before each
use C#,want to upload excel file on google doc. bellow syntax use to upload
Use a Content Delivery Network (CDN) Compress components with gzip Configure entity tags (ETags)
Use scenario is pretty simple: I have a desktop only application that could be
Use Memcache php class to interact with the memcached. At the beginning all works
use WWW::Mechanize; my $mech = WWW::Mechanize->new; $mech->get( $url ); say $mech->text; How could I
Use case: we have some project meta-data files which we want tracked, but are
use PHP and MySQL. Want my website to have the feature of image uploading
use Parallel::ForkManager; use LWP::Simple; my $pm=new Parallel::ForkManager(10); our $a =0; @LINK=( 10,203, 20, 20

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.