Sign in to follow this  

Getting around __declspec ( align )

This topic is 3466 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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!

Share this post


Link to post
Share on other sites
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?)

Share this post


Link to post
Share on other sites

This topic is 3466 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this