public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
From: Brian Budge <brian.budge@gmail.com>
To: Jie Zhang <jie.zhang@analog.com>
Cc: Benjamin Redelings I <benjamin_redelings@ncsu.edu>, gcc-help@gcc.gnu.org
Subject: Re: Correct way to make a 16-byte aligned double* for SSE  	vectorization?
Date: Thu, 31 Dec 2009 20:28:00 -0000	[thread overview]
Message-ID: <5b7094580912310841g6d8c957fueb7c451cd0d75309@mail.gmail.com> (raw)
In-Reply-To: <4B3C0E6A.2070706@analog.com>

The reason it won't work is that you're saying the pointer itself
needs to be 16 (or 8) byte aligned.  You need the address that the
pointer points to to be aligned.

On the stack:

__attribute__ ((aligned(16)) real myArray[32];

On the heap (*nix):
real *myArray;
posix_memalign((void**)&myArray, 16, 32 * sizeof(real));

or for more portability you could use the SSE intrinsic mm_malloc.

To know why the one version you posted works, we'd need to see the
calling code of f.   In general, it shouldn't work if malloc or new
are used to allocate the memory passed in, but it might be that the
memory is allocated on the stack?

  Brian

On Wed, Dec 30, 2009 at 6:37 PM, Jie Zhang <jie.zhang@analog.com> wrote:
> Hi,
>
> On 12/31/2009 03:30 AM, Benjamin Redelings I wrote:
>>
>> Hi,
>>
>> I am trying to figure out how to make a double* that is 16-byte aligned
>> in the way that SSE instructions want. Hopefully this would allow GCC to
>> auto-vectorize loops in a better way. The problem that I am having is
>> that I want a pointer to an aligned double, not an aligned pointer to a
>> double.
>>
>> I am compiling with these options:
>> % gcc -c test.C -O3 -ftree-vectorizer-verbose=3 -ffast-math
>>
>> According to the output of the vectorizer, none of the three ways
>> (below) of declaring an aligned pointer actually work. They are treated
>> as unaligned accesses, so presumably the location of the pointer itself
>> is being aligned, but it does not point to an aligned location. In
>> contrast, if I define an aligned double, and then define a pointer to
>> it, this works. Is this recommended?
>>
> Below is just taken from the GCC Manual:
> [quote]
> As another example,
>
>     char *__attribute__((aligned(8))) *f;
>
> specifies the type “pointer to 8-byte-aligned pointer to char”. Note again
> that this does not work with most attributes; for example, the usage of
> `aligned' and `noreturn' attributes given above is not yet supported.
> [/quote]
>
> If it had been supported, you could use
>
>> //typedef const real __attribute__((aligned(16))) *SSE_PTR;
>
> But since it is not yet supported now, you have to use
>
>> typedef double real;
>>
>> // these two lines work (together)
>> typedef real aligned_real __attribute__((aligned(16)));
>> typedef const aligned_real* SSE_PTR;
>>
>
> Jie
>

  reply	other threads:[~2009-12-31 16:41 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2009-12-30 21:39 Benjamin Redelings I
2009-12-31  9:36 ` Jie Zhang
2009-12-31 20:28   ` Brian Budge [this message]
     [not found]     ` <4B3D0948.5040407@ncsu.edu>
2010-01-01  0:14       ` Brian Budge

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=5b7094580912310841g6d8c957fueb7c451cd0d75309@mail.gmail.com \
    --to=brian.budge@gmail.com \
    --cc=benjamin_redelings@ncsu.edu \
    --cc=gcc-help@gcc.gnu.org \
    --cc=jie.zhang@analog.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).