Connecting Tech Pros Worldwide Forums | Help | Site Map

Question about using #pragma pack

Boltar
Guest
 
Posts: n/a
#1: Mar 20 '08
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

jacob navia
Guest
 
Posts: n/a
#2: Mar 20 '08

re: Question about using #pragma pack


Boltar wrote:
Quote:
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
Quote:
realignment going on silently underneath when the var is read and
written to memory?
>
no

Quote:
B2003

--
jacob navia
jacob at jacob point remcomp point fr
logiciels/informatique
http://www.cs.virginia.edu/~lcc-win32
Richard Tobin
Guest
 
Posts: n/a
#3: Mar 20 '08

re: Question about using #pragma pack


In article <9edcc905-13f1-4fb5-82e6-cecb999587ee@c65g2000hsa.googlegroups.com>,
Boltar <boltar2003@yahoo.co.ukwrote:
Quote:
>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
--
:wq
Boltar
Guest
 
Posts: n/a
#4: Mar 20 '08

re: Question about using #pragma pack


On Mar 20, 11:39 am, jacob navia <ja...@nospam.comwrote:
Quote:
Boltar wrote:
Quote:
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
>
Quote:
realignment going on silently underneath when the var is read and
written to memory?
>
no
I presume it must use 2 seperate get/put operations for a variable
crossing a word boundary then?

B2003

jacob navia
Guest
 
Posts: n/a
#5: Mar 20 '08

re: Question about using #pragma pack


Boltar wrote:
Quote:
On Mar 20, 11:39 am, jacob navia <ja...@nospam.comwrote:
Quote:
>Boltar wrote:
Quote:
>>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
>>
Quote:
>>realignment going on silently underneath when the var is read and
>>written to memory?
>no
>
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.


--
jacob navia
jacob at jacob point remcomp point fr
logiciels/informatique
http://www.cs.virginia.edu/~lcc-win32
Richard Tobin
Guest
 
Posts: n/a
#6: Mar 20 '08

re: Question about using #pragma pack


In article <94d3829e-7bbb-458b-ab64-e7fbf81be81e@c19g2000prf.googlegroups.com>,
Boltar <boltar2003@yahoo.co.ukwrote:
Quote:
>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
--
:wq
Kenneth Brody
Guest
 
Posts: n/a
#7: Mar 20 '08

re: Question about using #pragma pack


Boltar wrote:
Quote:
>
On Mar 20, 11:39 am, jacob navia <ja...@nospam.comwrote:
Quote:
Boltar wrote:
Quote:
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
Quote:
realignment going on silently underneath when the var is read and
written to memory?
no
>
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:ThisIsASpamTrap@gmail.com>

Closed Thread