Question about using #pragma pack

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Boltar

    Question about using #pragma pack

    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

    #2
    Re: Question about using #pragma pack

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

    B2003

    --
    jacob navia
    jacob at jacob point remcomp point fr
    logiciels/informatique

    Comment

    • Richard Tobin

      #3
      Re: Question about using #pragma pack

      In article <9edcc905-13f1-4fb5-82e6-cecb999587ee@c6 5g2000hsa.googl egroups.com>,
      Boltar <boltar2003@yah oo.co.ukwrote:
      >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

      Comment

      • Boltar

        #4
        Re: Question about using #pragma pack

        On Mar 20, 11:39 am, jacob navia <ja...@nospam.c omwrote:
        Boltar wrote:
        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
        >
        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

        Comment

        • jacob navia

          #5
          Re: Question about using #pragma pack

          Boltar wrote:
          On Mar 20, 11:39 am, jacob navia <ja...@nospam.c omwrote:
          >Boltar wrote:
          >>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
          >>
          >>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

          Comment

          • Richard Tobin

            #6
            Re: Question about using #pragma pack

            In article <94d3829e-7bbb-458b-ab64-e7fbf81be81e@c1 9g2000prf.googl egroups.com>,
            Boltar <boltar2003@yah oo.co.ukwrote:
            >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

            Comment

            • Kenneth Brody

              #7
              Re: Question about using #pragma pack

              Boltar wrote:
              >
              On Mar 20, 11:39 am, jacob navia <ja...@nospam.c omwrote:
              Boltar wrote:
              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
              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:ThisIsA SpamTrap@gmail. com>

              Comment

              Working...