Question about using #pragma pack

B

Boltar

If I use #pragma pack to byte align structures to non word size , will
this cause the program to run slightly slower if it is accessing and
setting non word aligned integers etc in the struct? Is there any
realignment going on silently underneath when the var is read and
written to memory?

B2003
 
J

jacob navia

Boltar said:
If I use #pragma pack to byte align structures to non word size , will
this cause the program to run slightly slower if it is accessing and
setting non word aligned integers etc in the struct?

yes

Is there any
 
R

Richard Tobin

Boltar said:
If I use #pragma pack to byte align structures to non word size , will
this cause the program to run slightly slower if it is accessing and
setting non word aligned integers etc in the struct?

Yes probably. Otherwise why would they bother to align them in the
usual case?

-- Richard
 
J

jacob navia

Boltar said:
I presume it must use 2 seperate get/put operations for a variable
crossing a word boundary then?

B2003

This depends on the processor
In some cases the alignment fault will be immediately trapped by the
processor that then issues two reads, or it could be that it is trapped
by the operating system, what would be MUCH MORE costly.

At each access you would pay the cost of an interupt servicing, and then
OS support.

Conclusion:

Do not use anything and leave the stuff to the compiler.
 
R

Richard Tobin

I presume it must use 2 seperate get/put operations for a variable
crossing a word boundary then?

It may be that the hardware or microcode does this for you (it does on
x86 for example), but it's still likely to be slower.

It might be that within a cache line it would be possible to do
unaligned reads and writes with no overhead, but I don't think that's
the case. Try comp.arch which has people who really understand this.

-- Richard
 
K

Kenneth Brody

Boltar said:
I presume it must use 2 seperate get/put operations for a variable
crossing a word boundary then?

"Best case scenario", probably.

However, there are other possibilities. Some hardware simply doesn't
allow such access, and causes an interrupt. On some platforms, this
will cause the program to crash. On others, the O/S will emulate the
unaligned access ability in the interrupt handler. For example, on
an unaligned read, it will read the two aligned chunks, pull out the
bits that you wanted, put then in the destination, and return from
the interrupt handler. (Hardly a simple "2 bus reads" degradation
in efficiency.)

You may want to look back at why you feel you need to pack the
structs.

--
+-------------------------+--------------------+-----------------------+
| Kenneth J. Brody | www.hvcomputer.com | #include |
| kenbrody/at\spamcop.net | www.fptech.com | <std_disclaimer.h> |
+-------------------------+--------------------+-----------------------+
Don't e-mail me at: <mailto:[email protected]>
 
K

Keith Thompson

Boltar said:
If I use #pragma pack to byte align structures to non word size , will
this cause the program to run slightly slower if it is accessing and
setting non word aligned integers etc in the struct? Is there any
realignment going on silently underneath when the var is read and
written to memory?

"#pragma pack" is a non-standard extension. If you want to know how
it works, you'll need to consult the documentation for your
implementation; different compilers might implement it differently, or
not at all.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Staff online

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,012
Latest member
RoxanneDzm

Latest Threads

Top