Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Aligning doubles to 8 byte boundaries

13 views
Skip to first unread message

Mark Farnan

unread,
Feb 4, 1999, 3:00:00 AM2/4/99
to

Does anyone know a way to consistently align doubles to an 8 byte boundary
in VC++ 6?

I’ve been running performance tests for floating point calculations, and
operations on doubles that break a quadword boundary (e.g. start at
0x00401F34 on the stack rather than at 0x00401F30 or 0x00401F38) take
anywhere from 4 to 8 times longer than their aligned counterparts (this is
on a Pentium II).

Some things I’ve tried:

The Developer Studio Project Setting: Code Generation -> Structure Member
Alignment setting (only operates on structures).

Optimizations off and on, for speed and for size, and turning off and on
individual optimization flags (Op, Og, Oi, Ot, Oy, Gs).

My test loop is:

void loop9()
{
long ia[9];
double f = 1.20;
double f2 = 1.00007;

for (long i = 0; i < 10000000; ++i) f = f2 * f;
}

Changing the size of the long array (say from 9 to 7, or some other number
depending on what the base pointer is at the time) changes the offset of the
doubles in memory, and when one or more of them wind up "quadword-offset" on
the stack, the test takes dramatically longer.

For the test I could probably put an 8 byte aligned structure in front of
the double declaration, but I’m really looking for a solution that will
apply to the million lines of code in our project that won’t require me to
visit the thousands of double locations.

Any thoughts would be appreciated.

Thanks,

Mark

Mark Meyers

unread,
Feb 4, 1999, 3:00:00 AM2/4/99
to
Here's a snip on the /Zp compiler option, which is what you get with what you
tried, and it mentions also global variables. Can you go global, or use
structs?


Syntax
/Zp

This option controls the alignment of structure members and global variables. By
default, the allocation of storage for structure members and global variables is
aligned on 8 bytes (/Zp8) or the size of the type, whichever is smaller. The
default value of 8 bytes provides maximum portability when using the Win32 API
to develop applications for Windows NT.
Use the /Zp option to specify the same packing for all structures in a module.
When you give the /Zpn option, where n is 1, 2, 4, 8, or 16, each structure
member after the first is stored on n-byte boundaries or the size of the member
type, whichever is smaller. If you use /Zp without an argument, structure
members are packed on 1-byte boundaries. No space is allowed between /Zp and its
argument.
Use the pack pragma in your source code to pack particular structures on
boundaries different from the packing specified on the command line.

-hope this helps

-Mark M

Mark Farnan

unread,
Feb 5, 1999, 3:00:00 AM2/5/99
to
Hi Mark M.,

I probably should have noted that

>> The Developer Studio Project Setting: Code Generation -> Structure Member

>> Alignment setting (only operates on structures),

that I mention *is* the /Zp compiler option. For a project our size it's
just not going to be feasible (or practical) to wrap all of our doubles in
structures. You'd think they'd give a way to do this aligning for
non-structures/non-globals. Frustrating, especially when the speed hit is so
severe.

The search continues. Thanks for your thoughts.

Mark F.

-------------------

Reuben Harris

unread,
Feb 5, 1999, 3:00:00 AM2/5/99
to
Wow - what a difference it makes! I tried your code - an aligned boundary
gives me c.296 ms where a mis-aligned boundary gives c.800 !! I never knew
it was *that* much of a hit.

On first glance there seems to be no way of forcing alignment of auto
variables... although someone here may find a way of doing so. However,
using the heap instead would appear to bring the alignment switch into play,
ie :

void loop9()
{
long ia[8];
double *pf = new double(1.20);
double *pf2 = new double(1.00007);

for (long i = 0; i < 10000000; ++i) *pf = (*pf2) * (*pf);
}


There is a price to pay - about 14ms on my machine for the above - but it's
an awful lot less than the non-aligned option.

Any good?


-- Reuben Harris
reu...@snowvalley.com

Mark Farnan wrote in message ...


>
>Does anyone know a way to consistently align doubles to an 8 byte boundary
>in VC++ 6?

<snip>

Mark Farnan

unread,
Feb 5, 1999, 3:00:00 AM2/5/99
to

>Wow - what a difference it makes! I tried your code - an aligned boundary
>gives me c.296 ms where a mis-aligned boundary gives c.800 !! I never knew
>it was *that* much of a hit.


And remember, more than 200 ms of that is the loop itself, so you're really
looking at closer to a 10x slowdown.

Your heap method does seem to be a good alternative - that worked well for
me in my tests. Still a pain to visit thousands of places in the main
project, though.

I'm going to look into the optimization flags more, also. One of them seemed
like it may have been aligning automatics, but unfortunately it was
incompatible with VC++ 6 Edit and Continue debug feature. Maybe I could just
turn it on for our release version if it looked promising, though.

I'll let you know if I find a solution. Thanks for your thoughts.

Mark

0 new messages