-2

I have a struct data structure as follow:

         struct{
                  int value;
                  int pos;
               }S[10];

How can i align this structure into an SSE register such that consecutive register locations has only the S[i].value and not S[i].pos.

Thank you

vinod c
  • 11
  • 4
  • Explain again? Maybe a picture or something? – Marc Glisse Sep 14 '14 at 14:09
  • My structure has 2 entries - value and pos. I want __m128i reg to be aligned to this stucture such that consecutive entries in the register holds only the value and not pos. If i align simply as per the convention, consecutive entries will hold both value and pos. – vinod c Sep 14 '14 at 14:14
  • 1
    You have to have the values and pos as separate arrays. – perh Sep 14 '14 at 14:21

1 Answers1

3

There is, with current compilers, no way to let it reorganise your data such that successive eleemnts in an array are in a different order than what you declare in a struct.

Edit:

Note that to re-arrange the layout of a struct would require the compiler to know exactly how the struct is defined and used EVERYWHERE at once, and typically compilers will not even try to do that. It may be possible to generate code that loads the data in such a way that the calculation can be done in an SSE register.

End Edit.

In other words

struct{
    int value;
    int pos;
}S[10];

will always end up with an alternating pattern of value, pos, value, pos, etc.

If you want consecuteive values of of value, value ... followed by pos, pos, ... then you will need to write your structure declaration differently, e.g.

struct 
{
    int value[10];
    int pos[10];
} S;

Of course, this will also mean that you have to modify any code accessing S from s[x].pos to s.pos[x], etc.

If you also want to ensure that the value and pos can actually be loaded by SSE instructions without difficulty, you need to use the alignment attribute/declaration specifications, so that each of the value and pos are aligned to 16-byte boundaries.

So, in gcc:

struct 
{
   int value[10] __attribute__((aligned(16)));
   int pos[10] __attribute__((aligned(16)));
} S;

In MS compatible compiler:

struct 
{
    __declspec(align(16)) int value[10];
    __declspec(align(16)) int pos[10];
} S;

Edit:

Note also that the number 10 is not partiuclarly great here if you want to use SSE instructions, as you are left with two elements that don't fit in an SSE register.

End Edit.

Mats Petersson
  • 126,704
  • 14
  • 140
  • 227
  • Thank you so much. If possible can you please tell whether it is possible to apply a user defined function on all values of a 4 bytes SSE register? – vinod c Sep 14 '14 at 15:10
  • You'll have to explain that one better - what function, and what is a 4-byte SSE register? – Mats Petersson Sep 14 '14 at 15:11
  • I have a macro : ((val1) & val2) >> pass), here val1 is a register of type __m128i... I need this macro to apply on all four values of va1 – vinod c Sep 14 '14 at 15:17
  • 1
    So, you want to do 4 and and 4 shift operations at once? Have you checked the output from your compiler? I find that both gcc and clang in modern variants will use SSE instructions to solve things quite regularly (given reasonable code that can be vectorized, of coruse) – Mats Petersson Sep 14 '14 at 15:38