0

EDIT:This is from a working project of mine. Working. I declared some char arrays at the beginning of .cpp file(even before the #include parts). Then i could use these arrays with "instructions for 16-Byte aligned variables".

Question: what happens if i use this .cpp file in another .cpp file as an include? Could i use that arrays for aligned operations in another projects too?

Question-2: is there a short-cut for this? I dont want to place all my variables in the beginning.

Some of the code:(i worked on some 16-Byte-aligned arrays)

    //I put these arrays at the beginning, so they are aligned
//for the movaps instructions(x2 speed for reading and writing memory)
float v1[16];
float v2[16];
char counters[32];
char array_of_ones[32];
char source_array[4096];
char destination_array[4096];   
struct bit_field
{
    bf1:32; 
    bf2:32;
    bf3:32;
    bf4:32;
}some_area;
struct bit_mask_x
{
    bf1:32;
    bf2:32;
    bf3:32;
    bf4:32;
}some_mask;
float var_fast[16];
char alignment_purge[5];    //for the unalignment tests
char unaligned_source_array[4096];
char unaligned_destination_array[4096];



#include <math.h>
#include<stdlib.h>
#include<stdio.h>
#include<time.h>
.....
.....

What happens if i include this program in another like this:

#include <math.h>
#include<my_aligned.h> <-------- or my_aligned.cpp
#include<stdio.h>
#include<time.h>

Do i have to use a .h file for this?

Thanks...

huseyin tugrul buyukisik
  • 11,469
  • 4
  • 45
  • 97

2 Answers2

1

Have you actually tried if this aligns your variables correctly? When you compile, the executable always has a header whose size may not be a multiple of 16. Also, alignment_purge may not really get the variables following it out of alignment, because the compiler may add padding. Finally, the headers don't introduce variables, so if you put your variables above or below headers, that doesn't change anything.

You can take a look at this question to see how to request aligned memory.

As a side note, normally you wouldn't want to include source files into another file. See this question on this subject.

Community
  • 1
  • 1
Shahbaz
  • 46,337
  • 19
  • 116
  • 182
  • @tuğrulbüyükışık, just because it's working on your computer, with your particular compiler, doesn't mean it's correct. You may just as well have been lucky. – Shahbaz Jul 25 '12 at 08:56
0

Putting variable declarations at the top of a file is not required to enforce any specific alignment. You may just be getting lucky here, or it may be an idiosyncrasy of your compiler.

If you're using gcc, you can use the aligned attribute to request the correct alignment - other compilers probably have equivalent extensions or #PRAGMAs.

eg. your variable declarations with gcc's extension would look like:

float v1[16] __attribute__ ((aligned (16)));
float v2[16] __attribute__ ((aligned (16)));

If you need it to be entirely portable, I think your only solution is to dynamically allocate a large block of memory, and then manage the alignment of allocated blocks within it yourself.


Note that the alignment only needs to be enforced where the variables are actually stored, in the .cpp file. You can just forward declare them in a header, and that will let you refer to them in your other files. #includeing the .cpp file is not only unnecessary, it will cause a link error as every file has its own copy of the variables with the same names.


OP is using Digital Mars (if you'd mentioned this right away, I would have looked it up).

Searching for digital mars alignment, the first hit is the pragma documentation. I looked at align first, and it referred me to pack.

Using this, your code would look like:

#pragma pack(push, 16)
float v1[16];
float v2[16];
// ... any other aligned variables defined here
#pragma pack(pop)

However, pack affects the alignment of members inside structures - it's not clear what it does for global variables.

I think, to be certain, you need to write an aligned allocator: start by searching _aligned allocation C++` for example, and post a dedicated question if you can't figure it out.

Useless
  • 64,155
  • 6
  • 88
  • 132