Eight-byte alignment

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • glchin@hotmail.com

    #1

    Eight-byte alignment

    Does a compiler guarantee that the variable w below is placed on an
    eight-byte aligned address?


    void myFunction( long iFreq )
    {
    const double w = two_pi * iFreq;
    ...
    ...
    }
  • Victor Bazarov

    #2
    Re: Eight-byte alignment

    glchin@hotmail. com wrote:
    Does a compiler guarantee that the variable w below is placed on an
    eight-byte aligned address?
    No. There are no requirement in C++ Standard WRT specific alignment
    for any objects.

    You will find that every compiler/platform is different in that sense
    and that many compilers (if not all) have a way for you to control the
    alignment boundary.
    void myFunction( long iFreq )
    {
    const double w = two_pi * iFreq;
    ...
    ...
    }
    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask


    Comment

    • =?UTF-8?Q?Jeff=E2=98=A0Relf?=

      #3
      Re: Eight-byte alignment

      Re: This code, similar to yours, GLChin:
      “ main() { const double w = 0 ; } ”,

      The address of “ w ” is 8-byte-aligned.
      Using VC++ 8, you can check for yourself, like this:

      #pragma warning( disable: 4007 4189 4430 4508 )
      WinMain( int, int, int, int ) {
      const __int32 Int32 = 0 ; const __int64 Int64 = 0 ;

      int Implicit_Size_o f_Int32 = int( & Int64 ) - int( & Int32 );
      // Breaking here, “ Implicit_Size_o f_Int32 == 8 ”.
      }

      Comment

      • Carl Daniel [VC++ MVP]

        #4
        Re: Eight-byte alignment

        <glchin@hotmail .comwrote in message
        news:6d36462e-28fe-4db8-83a5-9c5d65836384@b4 0g2000prf.googl egroups.com...
        Does a compiler guarantee that the variable w below is placed on an
        eight-byte aligned address?
        >
        >
        void myFunction( long iFreq )
        {
        const double w = two_pi * iFreq;
        ...
        ...
        }
        Yes, the compiler always guarantees that variables are by default aligned
        correctly for their type. Since this is a double, it will be 8-byte
        aligned. Since this is a const double, there's no requirement that the
        compiler allocate any memory for it at all - it could be enregistered or
        re-calculated wherever used. In practice, the compiler probably assigns
        memory for it, which would be 8-byte aligned.

        The only time memory won't be aligned is when you've used #pragma pack, one
        of the memory alignment command-line options, or done pointer arithmetic
        yourself that doesn't honor the type's alignment.

        -cd


        Comment

        • Alexander Grigoriev

          #5
          Re: Eight-byte alignment

          I'm not sure that in 32-bit environment there is a stack frame alignment
          guarantee, other than 4 bytes, of course. Thus, any doubles might be
          unaligned.

          "Carl Daniel [VC++ MVP]" <cpdaniel_remov e_this_and_nosp am@mvps.org.nos pam>
          wrote in message news:uVuqvxdNIH A.5988@TK2MSFTN GP02.phx.gbl...
          <glchin@hotmail .comwrote in message
          news:6d36462e-28fe-4db8-83a5-9c5d65836384@b4 0g2000prf.googl egroups.com...
          >Does a compiler guarantee that the variable w below is placed on an
          >eight-byte aligned address?
          >>
          >>
          >void myFunction( long iFreq )
          >{
          >const double w = two_pi * iFreq;
          > ...
          > ...
          >}
          >
          Yes, the compiler always guarantees that variables are by default aligned
          correctly for their type. Since this is a double, it will be 8-byte
          aligned. Since this is a const double, there's no requirement that the
          compiler allocate any memory for it at all - it could be enregistered or
          re-calculated wherever used. In practice, the compiler probably assigns
          memory for it, which would be 8-byte aligned.
          >
          The only time memory won't be aligned is when you've used #pragma pack,
          one of the memory alignment command-line options, or done pointer
          arithmetic yourself that doesn't honor the type's alignment.
          >
          -cd
          >
          >

          Comment

          • James Kanze

            #6
            Re: Eight-byte alignment

            On Dec 3, 6:22 pm, glc...@hotmail. com wrote:
            Does a compiler guarantee that the variable w below is
            placed on an eight-byte aligned address?
            void myFunction( long iFreq )
            {
            const double w = two_pi * iFreq;
            ...
            ...
            }
            A compiler does, but I don't know if your using that compiler.
            Certainly not all compilers do---it wouldn't make sense on a
            machine where sizeof(double) is 6, for example, nor on a 16 bit
            machine. Depending on the hardware, it might not even make
            sense on a 32 bit machine (nor a 36 bit machine, for that
            matter).

            The real question is why do you care? The compiler will
            guarantee that w meets whatever requirements the hardware makes
            for effective access. And that's really all you care about.

            --
            James Kanze (GABI Software) email:james.kan ze@gmail.com
            Conseils en informatique orientée objet/
            Beratung in objektorientier ter Datenverarbeitu ng
            9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

            Comment

            • James Kanze

              #7
              Re: Eight-byte alignment

              On Dec 3, 7:43 pm, "Carl Daniel [VC++ MVP]"
              <cpdaniel_remov e_this_and_nos. ..@mvps.org.nos pamwrote:
              <glc...@hotmail .comwrote in message
              news:6d36462e-28fe-4db8-83a5-9c5d65836384@b4 0g2000prf.googl egroups.com...
              Does a compiler guarantee that the variable w below is
              placed on an eight-byte aligned address?
              void myFunction( long iFreq )
              {
              const double w = two_pi * iFreq;
              ...
              ...
              }
              Yes, the compiler always guarantees that variables are by
              default aligned correctly for their type.
              Which on most 32 bit machines is any multiple of 4. On the one
              48 bit machine I'm aware of, it would be a multiple of 6. On
              the various 36 bit machines I've seen or heard of, it would be a
              multiple of 4 as well. On the earlier 16 bit machines I
              worked on, it would be multiple of 2, and on the 8 bit machines,
              there were no alignment restrictions.
              Since this is a double, it will be 8-byte aligned.
              On a Sun Sparc, probably. On an Intel based 32 bit machine, I
              doubt it, and on the older, 16 bit Intels, almost certainly not.
              Since this is a const double, there's no requirement that the
              compiler allocate any memory for it at all - it could be
              enregistered or re-calculated wherever used. In practice, the
              compiler probably assigns memory for it, which would be 8-byte
              aligned.
              The only time memory won't be aligned is when you've used
              #pragma pack,
              Which is implementation defined. Maybe it starts a game of
              packman.

              --
              James Kanze (GABI Software) email:james.kan ze@gmail.com
              Conseils en informatique orientée objet/
              Beratung in objektorientier ter Datenverarbeitu ng
              9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

              Comment

              • James Kanze

                #8
                Re: Eight-byte alignment

                On Dec 3, 7:07 pm, Jeff?Relf <Jeff_R...@Yaho o.COMwrote:
                Re: This code, similar to yours, GLChin:
                ? main() { const double w = 0 ; } ?,
                The address of ? w ? is 8-byte-aligned.
                Using VC++ 8, you can check for yourself,
                You can check whether it is 8 byte aligned in one particular
                case, after whatever calls preceded it.
                like this:
                #pragma warning( disable: 4007 4189 4430 4508 )
                WinMain( int, int, int, int ) {
                const __int32 Int32 = 0 ; const __int64 Int64 = 0 ;
                int Implicit_Size_o f_Int32 = int( & Int64 ) - int( & Int32 );
                // Breaking here, ? Implicit_Size_o f_Int32 == 8 ?.
                }
                I'm not sure what that's supposed to check. It's not C++, so it
                doesn't tell us anything about what C++ does. And I don't see
                how it would be related to how VC++ would lay out a double.

                FWIW: VC++ doesn't guarantee 8 byte alignment. All of my
                compilers on Sparc do---perhaps because accessing a double at an
                address which isn't 8 byte aligned will cause a core dump:-).
                Curiously, g++ on both the Linux machine and the Windows
                machines here (32 bit Intel) also seems to guarantee 8 byte
                alignment.

                You might try something like the following:


                #include <iostream>

                double two_pi = 6.28 ;

                void
                f( long ifreq )
                {
                double w = two_pi * ifreq ;
                std::cout << &w << std::endl ;
                }

                void
                g()
                {
                f( 20 ) ;
                }

                template< size_t N >
                void
                h()
                {
                char dummy[ N ] ;
                f( 20 ) ;
                }

                template< size_t N >
                void
                i()
                {
                char dummy[ N ] ;
                double w ;
                std::cout << &w << std::endl ;
                }

                int
                main()
                {
                f( 20 ) ;
                g() ;
                h< 1 >() ;
                h< 2 >() ;
                h< 3 >() ;
                h< 4 >() ;
                h< 5 >() ;
                h< 6 >() ;
                h< 7 >() ;
                h< 8 >() ;
                i< 1 >() ;
                i< 2 >() ;
                i< 3 >() ;
                i< 4 >() ;
                i< 5 >() ;
                i< 6 >() ;
                i< 7 >() ;
                i< 8 >() ;
                return 0 ;
                }

                If all of the addresses output are multiples of 8, it still
                isn't guaranteed, but I'd guess that there is a very good chance
                of it being true. If they aren't, of course, you know that it
                isn't guaranteed.

                --
                James Kanze (GABI Software) email:james.kan ze@gmail.com
                Conseils en informatique orientée objet/
                Beratung in objektorientier ter Datenverarbeitu ng
                9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

                Comment

                • =?UTF-8?Q?Jeff=E2=98=A0Relf?=

                  #9
                  =?UTF-8?Q?=E2=80=9C?= VC++ 8.0 =?UTF-8?Q?=E2=80=9D?= 8-byte-aligns a =?UTF-8?Q?=E2=80=9C?= __int64 =?UTF-8?Q?=E2=80=9D,? = same as a =?UTF-8?Q?=E2=80=9C?= double =?UTF-8?Q?=E2=80=9D.? =

                  “ VC++ 8.0 ” 8-byte-aligns a “ __int64 ”, same as a “ double ”.

                  The code I showed ( news:Jeff_Relf_ 2007_Dec_3__10_ 7_AK@Cotse.NET )
                  is most definately VC++ 8.0,
                  complied and debuged using Visual Studio 2005.

                  Had you tried it yourself, Mr. Kanze, you'd know that;
                  but, naturally, you couldn't do that.

                  Comment

                  • Ben Voigt [C++ MVP]

                    #10
                    Re: &quot; VC++ 8.0 &quot; 8-byte-aligns a &quot; __int64 &quot;, same as a &quot; double &quot;.


                    "Jeff?Relf" <Jeff_Relf@Yaho o.COMwrote in message
                    news:Jeff_Relf_ 2007_Dec_4__2_5 2_Px@Cotse.NET. ..
                    >" VC++ 8.0 " 8-byte-aligns a " __int64 ", same as a " double ".
                    >
                    The code I showed ( news:Jeff_Relf_ 2007_Dec_3__10_ 7_AK@Cotse.NET )
                    is most definately VC++ 8.0,
                    complied and debuged using Visual Studio 2005.
                    >
                    Had you tried it yourself, Mr. Kanze, you'd know that;
                    but, naturally, you couldn't do that.
                    Furthermore, your code demonstrates packing, not alignment.


                    Comment

                    • Ben Voigt [C++ MVP]

                      #11
                      Re: &quot; VC++ 8.0 &quot; 8-byte-aligns a &quot; __int64 &quot;, same as a &quot; double &quot;.


                      "Jeff?Relf" <Jeff_Relf@Yaho o.COMwrote in message
                      news:Jeff_Relf_ 2007_Dec_4__2_5 2_Px@Cotse.NET. ..
                      >" VC++ 8.0 " 8-byte-aligns a " __int64 ", same as a " double ".
                      >
                      The code I showed ( news:Jeff_Relf_ 2007_Dec_3__10_ 7_AK@Cotse.NET )
                      is most definately VC++ 8.0,
                      complied and debuged using Visual Studio 2005.
                      >
                      Had you tried it yourself, Mr. Kanze, you'd know that;
                      but, naturally, you couldn't do that.
                      >
                      Jeff, your code shows nothing at all, because it is in the main function.
                      Stack alignment is dependent on the caller to some degree, if the caller
                      left the stack 4-byte aligned, then you would not have 8-byte alignment.


                      Comment

                      • =?UTF-8?Q?Jeff=E2=98=A0Relf?=

                        #12
                        Ben, how do you explain =?UTF-8?Q?=E2=80=9C?= Implicit_Size_o f_Int32 == 8 =?UTF-8?Q?=E2=80=9D?= ?

                        Re: This code of mine:
                        “ #pragma warning( disable: 4007 4189 4430 4508 )
                        WinMain( int, int, int, int ) {
                        const __int32 Int32 = 0 ; const __int64 Int64 = 0 ;

                        int Implicit_Size_o f_Int32 = int( & Int64 ) - int( & Int32 );
                        // Breaking here, “ Implicit_Size_o f_Int32 == 8 ”.
                        } ”.

                        How do you ( Ben ) explain “ Implicit_Size_o f_Int32 == 8 ”
                        if it's not a manifestation of 8-byte alignment ?

                        Remove the “ const ” terms, or change them to “ static ”,
                        and you still get the same results.

                        Comment

                        • James Kanze

                          #13
                          =?ISO-8859-1?Q?Re:_&quot;_ VC++_8.0_&quot; _8-byte-aligns_a_&quot; ___int64_&quot; ?==?ISO-8859-1?Q?,_same_as_a _&quot;_double_ &quot;.?=

                          On Dec 4, 11:52 pm, Jeff?Relf <Jeff_R...@Yaho o.COMwrote:
                          ? VC++ 8.0 ? 8-byte-aligns a ? __int64 ?, same as a ? double ?.
                          The code I showed (news:Jeff_Relf _2007_Dec_3__10 _7_AK@Cotse.NET )
                          is most definately VC++ 8.0,
                          complied and debuged using Visual Studio 2005.
                          Had you tried it yourself, Mr. Kanze, you'd know that;
                          but, naturally, you couldn't do that.
                          No I couldn't, because VC++ doesn't work on my machine (a Sun
                          Sparc). It's certainly not C++, and if you tried it with any
                          even halfway conformant compiler (e.g. g++ -std=c++98, or even
                          VC++, with the proper options), you'd know that.

                          --
                          James Kanze (GABI Software) email:james.kan ze@gmail.com
                          Conseils en informatique orientée objet/
                          Beratung in objektorientier ter Datenverarbeitu ng
                          9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

                          Comment

                          • Norbert Unterberg

                            #14
                            Re: Eight-byte alignment

                            glchin@hotmail. com schrieb:
                            Does a compiler guarantee that the variable w below is placed on an
                            eight-byte aligned address?
                            >
                            void myFunction( long iFreq )
                            {
                            const double w = two_pi * iFreq;
                            ...
                            ...
                            }
                            Some compilers do, some compilers don't.
                            Visual Studio 2005 does align that way, as long as you do not modify the
                            default alignment.

                            Have a look in the online help for "__alignof" and
                            "__declspec(ali gn(n))" to find more information:
                            http://msdn2.microsoft.com/en-us/lib...f4(VS.80).aspx
                            http://msdn2.microsoft.com/en-us/lib...65(VS.80).aspx

                            There is no portable way to guarantee a particular alignment. If you
                            really think you need one, you have to check the technical manuals of
                            all compilers that you need to support.

                            Norbert

                            Comment

                            • =?UTF-8?Q?Jeff=E2=98=A0Relf?=

                              #15
                              Re: Ben, how do you explain &quot; Implicit_Size_o f_Int32 == 8 &quot; ?

                              â“‹_Ben_Voigt_C , _D71eGS.Airband .NET Phx.GBL
                              2.48 Hours, Outlook_O19, Dec 5, 2007, 11._1 A, BS99eu

                              Re: This VC++ 8 code of mine, without the “ /O2 ” compiler option:
                              “ #pragma warning( disable: 4007 4189 4430 4508 )
                              WinMain( int, int, int, int ) {
                              const __int32 Int32 = 0 ; const __int64 Int64 = 0 ;

                              int Implicit_Size_o f_Int32 = int( & Int64 ) - int( & Int32 );
                              // Breaking here, ‘ Implicit_Size_o f_Int32 == 8 ’.
                              } ”.

                              Implicit_Size_o f_Int32 is 8 --because <-- VC++ aligned Int64.
                              Yes, that created some padding, but that's not the issue.

                              As I recently said in “ news:Jeff_Relf_ 2007_Dec_5__1_2 9_Pw@Cotse.NET ”,
                              the “ /O2 ” compiler option f u c k s things up.

                              Comment

                              Working...