Getting around __declspec ( align )

Started by
3 comments, last by Spoonbender 15 years, 9 months ago
I am trying to uncover a portable means of aligning data within a register. I initially started out with windows, so my code is littered with __declspec statements that accomplish just that ( __declspec ( align ) ). Is there a way to do this without this statement? Is there a Linux/Unix counterpart to this statement? Any help would be appreciated.
Advertisement
Look in the documentation for your compiler (gcc on *nix I assume).

The language provides no mechanism for this, the typical method is to use the preprocessor to gate which compiler-specific extensions are used to control the alignment. It can't always be done with macros because it doesn't always end needing to be applied in the same spots in the class declaration, etc, so you need to resign yourself to your fate: you're going to have a decent amount of
#if MSVC// ...a...#else// ...b...#endif 

and the like.

Alternatively, if its an option, write code so that you don't care about the alignment and such.
Well, I guess it COULD be an option. I am only using this stuff to help with 3D calculations. I know that aligned data runs much faster in registers because it bypasses the registers need to align it after retrieving it. I have thought of the compiler conditional solution you have mentioned ( in fact, I have identical code in my main.h file ), I'm just new to .gcc compilers and I'm afraid I haven't got a clue. ( VS junky ;) ) I'll look into what you have mentioned. Thanks!
O yeah, by the way, I'm using Cygwin on Windows XP.
Quote:Original post by jmau0438
Well, I guess it COULD be an option. I am only using this stuff to help with 3D calculations. I know that aligned data runs much faster in registers because it bypasses the registers need to align it after retrieving it.

Usually, data will be aligned by default. Both MSVC and GCC generally ensures that data is aligned. Of course there are exceptions though. If you compile without optimizations, it may not be aligned, and if you use SIMD (SSE) datatypes, you'll have to manually align. (And if you ask for a struct to be packed, it also won't be able to align members)

Otherwise, though, you can generally count on the compiler to handle it.

Protip: Before you waste a lot of time on dubious optimizations, make sure that it's actually necessary (does it give you enough extra performance to be worthwhile, and are you sure the compiler won't do it for you in any case?)

This topic is closed to new replies.

Advertisement