Does UNALIGNED attribute exist in GNU C?

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

    Does UNALIGNED attribute exist in GNU C?

    Hello,

    What is equivalent of __unaligned attribute in GNU C?
    I've searched for some time and it seems that it just does not exist...

    /*
    For HP and new Microsoft compilers it works this way:
    long_pointer = (some unaligned address)
    long_value = (* (__unaligned long *) long_pointer );

    This causes the compiler to emit code that gets the value
    in some slow but sure way, but avoiding exceptions
    */

    No need to suggest workarounds please...

    Regards
    --PA


  • Roger Leigh

    #2
    Re: Does UNALIGNED attribute exist in GNU C?

    -----BEGIN PGP SIGNED MESSAGE-----
    Hash: SHA1

    "Pavel A." <pavel_a@NOwrit emeNO.com> writes:
    [color=blue]
    > What is equivalent of __unaligned attribute in GNU C?
    > I've searched for some time and it seems that it just does not exist...[/color]

    It appears not to. That's probably because it should not be needed
    unless your code is buggy. If your code is standard C, the compiler
    should have aligned everything nicely for you. However, you didn't
    post the code that creates the unaligned pointer.

    Also:
    - - Some CPUs allow unaligned access.
    - - Some operating systems can transparently handle unaligned access on
    systems that don't support it.


    - --
    Roger Leigh
    Printing on GNU/Linux? http://gimp-print.sourceforge.net/
    Debian GNU/Linux http://www.debian.org/
    GPG Public Key: 0x25BFB848. Please sign and encrypt your mail.
    -----BEGIN PGP SIGNATURE-----
    Version: GnuPG v1.4.1 (GNU/Linux)
    Comment: Processed by Mailcrypt 3.5.8 <http://mailcrypt.sourc eforge.net/>

    iD8DBQFCjOFQVcF caSW/uEgRAu+aAKDDQqL P5PeBpB4RHt2Wn8 bGN4lEXwCfQa4U
    MDJleeQZG8u63b7 TJZExNog=
    =XMLL
    -----END PGP SIGNATURE-----

    Comment

    • those who know me have no need of my name

      #3
      Re: Does UNALIGNED attribute exist in GNU C?

      [fu-t set]

      in comp.lang.c i read:
      [color=blue]
      >What is equivalent of __unaligned attribute in GNU C?
      >I've searched for some time and it seems that it just does not exist...[/color]

      correct -- there is nothing standard.

      --
      a signature

      Comment

      • Pavel A.

        #4
        Re: Does UNALIGNED attribute exist in GNU C?

        Thanks Roger for your reply.
        I was looking how to implement functions like LoadUnalignedLo ng /
        StoreUnalignedL ong
        (the need for these is obvious... if you get unaligned structure from file
        or network etc).
        So will do it anyway else.

        --PA



        "Roger Leigh" <${rleigh}@inva lid.whinlatter. ukfsn.org.inval id> wrote in
        message news:87oeb7kpta .fsf@hardknott. home.whinlatter .ukfsn.org...[color=blue]
        > -----BEGIN PGP SIGNED MESSAGE-----
        > Hash: SHA1
        >
        > "Pavel A." <pavel_a@NOwrit emeNO.com> writes:
        >[color=green]
        > > What is equivalent of __unaligned attribute in GNU C?
        > > I've searched for some time and it seems that it just does not exist...[/color]
        >
        > It appears not to. That's probably because it should not be needed
        > unless your code is buggy. If your code is standard C, the compiler
        > should have aligned everything nicely for you. However, you didn't
        > post the code that creates the unaligned pointer.
        >
        > Also:
        > - - Some CPUs allow unaligned access.
        > - - Some operating systems can transparently handle unaligned access on
        > systems that don't support it.
        >
        >
        > - --
        > Roger Leigh
        > Printing on GNU/Linux? http://gimp-print.sourceforge.net/
        > Debian GNU/Linux http://www.debian.org/
        > GPG Public Key: 0x25BFB848. Please sign and encrypt your[/color]
        mail.[color=blue]
        > -----BEGIN PGP SIGNATURE-----
        > Version: GnuPG v1.4.1 (GNU/Linux)
        > Comment: Processed by Mailcrypt 3.5.8 <http://mailcrypt.sourc eforge.net/>
        >
        > iD8DBQFCjOFQVcF caSW/uEgRAu+aAKDDQqL P5PeBpB4RHt2Wn8 bGN4lEXwCfQa4U
        > MDJleeQZG8u63b7 TJZExNog=
        > =XMLL
        > -----END PGP SIGNATURE-----[/color]


        Comment

        • CBFalconer

          #5
          Re: Does UNALIGNED attribute exist in GNU C?

          "Pavel A." wrote:[color=blue]
          >
          > I was looking how to implement functions like LoadUnalignedLo ng /
          > StoreUnalignedL ong (the need for these is obvious... if you get
          > unaligned structure from file or network etc).
          > So will do it anyway else.[/color]

          Please don't toppost. It is not acceptable in c.l.c. See sig
          below.

          There is no such need. How do you get an "unaligned structure"?
          All you can receive from a network of file is some gestalt of
          bytes, which should have a defined relationship to a field in a
          structure. The details of a structure are not valid outside the
          system that created it, and can be freely changed, even by a
          compiler upgrade.

          --
          Some informative links:
          news:news.annou nce.newusers
          Latest news coverage, email, free stock quotes, live scores and video are just the beginning. Discover more every day at Yahoo!






          Comment

          • Roger Leigh

            #6
            Re: Does UNALIGNED attribute exist in GNU C?

            -----BEGIN PGP SIGNED MESSAGE-----
            Hash: SHA1

            "Pavel A." <pavel_a@NOwrit emeNO.com> writes:

            [Please don't top-post]
            [color=blue]
            > I was looking how to implement functions like LoadUnalignedLo ng /
            > StoreUnalignedL ong
            > (the need for these is obvious... if you get unaligned structure from file
            > or network etc).[/color]

            I've never seen those non-standard functions. Due to the hideous
            naming, I assume they come from MS Windows?

            The need for them aside, you could always memcpy to a known aligned
            memory location before reading/writing. It should be fairly cheap,
            but still less than ideal. If your software is properly written, you
            simply won't have alignment issues.


            - --
            Roger Leigh
            Printing on GNU/Linux? http://gimp-print.sourceforge.net/
            Debian GNU/Linux http://www.debian.org/
            GPG Public Key: 0x25BFB848. Please sign and encrypt your mail.
            -----BEGIN PGP SIGNATURE-----
            Version: GnuPG v1.4.1 (GNU/Linux)
            Comment: Processed by Mailcrypt 3.5.8 <http://mailcrypt.sourc eforge.net/>

            iD8DBQFCkMXUVcF caSW/uEgRAq1GAJ9GYG9 AuzlZcETFP5rGat DYmrDu7gCfX20d
            TKSkV3oERlS9U9D F21grf60=
            =BlzH
            -----END PGP SIGNATURE-----

            Comment

            • Richard Bos

              #7
              Re: Does UNALIGNED attribute exist in GNU C?

              Roger Leigh <${rleigh}@inva lid.whinlatter. ukfsn.org.inval id> wrote:
              [color=blue]
              > "Pavel A." <pavel_a@NOwrit emeNO.com> writes:
              >
              > [Please don't top-post]
              >[color=green]
              > > I was looking how to implement functions like LoadUnalignedLo ng /
              > > StoreUnalignedL ong
              > > (the need for these is obvious... if you get unaligned structure from file
              > > or network etc).[/color]
              >
              > I've never seen those non-standard functions. Due to the hideous
              > naming, I assume they come from MS Windows?[/color]

              You'd say so, but AFAICT not even that.
              [color=blue]
              > The need for them aside, you could always memcpy to a known aligned
              > memory location before reading/writing. It should be fairly cheap,
              > but still less than ideal.[/color]

              If you have a halfway decently defined network or file format, you won't
              be reading directly into your objects if that's impossible. You'll be
              manipulating the byte _values_, rather than their representations in
              memory.

              Richard

              Comment

              • Thomas Carter

                #8
                Re: Does UNALIGNED attribute exist in GNU C?

                On Thu, 19 May 2005 19:56:17 +0100, Roger Leigh wrote:
                [color=blue]
                > -----BEGIN PGP SIGNED MESSAGE-----
                > Hash: SHA1
                >
                > "Pavel A." <pavel_a@NOwrit emeNO.com> writes:
                >[color=green]
                >> What is equivalent of __unaligned attribute in GNU C?
                >> I've searched for some time and it seems that it just does not exist...[/color]
                >
                > It appears not to. That's probably because it should not be needed
                > unless your code is buggy.[/color]

                I respectfully disagree: In networking applications, for performance
                reasons (to avoid data copying) one must frequently deal with portions of
                buffers that are not correctly aligned.


                Comment

                • Tim Prince

                  #9
                  Re: Does UNALIGNED attribute exist in GNU C?

                  Thomas Carter wrote:[color=blue]
                  > On Thu, 19 May 2005 19:56:17 +0100, Roger Leigh wrote:
                  >
                  >[color=green]
                  >>-----BEGIN PGP SIGNED MESSAGE-----
                  >>Hash: SHA1
                  >>
                  >>"Pavel A." <pavel_a@NOwrit emeNO.com> writes:
                  >>
                  >>[color=darkred]
                  >>>What is equivalent of __unaligned attribute in GNU C?
                  >>>I've searched for some time and it seems that it just does not exist...[/color]
                  >>
                  >>It appears not to. That's probably because it should not be needed
                  >>unless your code is buggy.[/color]
                  >
                  >
                  > I respectfully disagree: In networking applications, for performance
                  > reasons (to avoid data copying) one must frequently deal with portions of
                  > buffers that are not correctly aligned.
                  >
                  >[/color]
                  That depends on what you mean by unaligned. The C standards don't allow
                  the kinds of type punning which would place, for example, a long long
                  int at an odd short int address. Code which does so is non-portable.
                  Even Microsoft C, in the newer 64-bit versions, gets away from the
                  requirement to maximize support of 64-bit data located on any 32-bit
                  boundary, but this NG is about standard C.
                  If you mean, for example, a string of ints starting at any legal int
                  boundary, that's the default, and doesn't require any attribute or
                  pragma. gcc does support specification of higher alignments, such as
                  those which would allow the use of aligned pair load and store instructions.

                  Comment

                  • Lawrence Kirby

                    #10
                    Re: Does UNALIGNED attribute exist in GNU C?

                    On Sat, 18 Jun 2005 14:50:47 +0000, Thomas Carter wrote:
                    [color=blue]
                    > On Thu, 19 May 2005 19:56:17 +0100, Roger Leigh wrote:
                    >[color=green]
                    >> -----BEGIN PGP SIGNED MESSAGE-----
                    >> Hash: SHA1
                    >>
                    >> "Pavel A." <pavel_a@NOwrit emeNO.com> writes:
                    >>[color=darkred]
                    >>> What is equivalent of __unaligned attribute in GNU C?
                    >>> I've searched for some time and it seems that it just does not exist...[/color]
                    >>
                    >> It appears not to. That's probably because it should not be needed
                    >> unless your code is buggy.[/color]
                    >
                    > I respectfully disagree: In networking applications, for performance
                    > reasons (to avoid data copying) one must frequently deal with portions of
                    > buffers that are not correctly aligned.[/color]

                    This is a generalisation which is the for most part wrong. Not totally
                    wrong but still mostly wrong. However a discussion of why is beyond the
                    scope of comp.lang.c (i.e. where I am reading this).

                    Lawrence

                    Comment

                    • Thomas Carter

                      #11
                      Re: Does UNALIGNED attribute exist in GNU C?

                      On Sun, 19 Jun 2005 13:36:52 +0100, Lawrence Kirby wrote:
                      [color=blue]
                      > On Sat, 18 Jun 2005 14:50:47 +0000, Thomas Carter wrote:
                      >[color=green]
                      >> On Thu, 19 May 2005 19:56:17 +0100, Roger Leigh wrote:
                      >>[color=darkred]
                      >>> -----BEGIN PGP SIGNED MESSAGE-----
                      >>> Hash: SHA1
                      >>>
                      >>> "Pavel A." <pavel_a@NOwrit emeNO.com> writes:
                      >>>
                      >>>> What is equivalent of __unaligned attribute in GNU C?
                      >>>> I've searched for some time and it seems that it just does not exist...
                      >>>
                      >>> It appears not to. That's probably because it should not be needed
                      >>> unless your code is buggy.[/color]
                      >>
                      >> I respectfully disagree: In networking applications, for performance
                      >> reasons (to avoid data copying) one must frequently deal with portions of
                      >> buffers that are not correctly aligned.[/color]
                      >
                      > This is a generalisation which is the for most part wrong. Not totally
                      > wrong but still mostly wrong.[/color]

                      typedef unsigned int UInt32 ;
                      UInt32 * x = (UInt32 *) malloc(10) ;
                      UInt32 * p = x + 1 ;

                      f(p) ;

                      f() is now dealing with an unaligned buffer. Lots of examples similar to
                      this can be found e.g. in the OpenSSL library.

                      That's what I meant.

                      Comment

                      • Paul Pluzhnikov

                        #12
                        Re: Does UNALIGNED attribute exist in GNU C?

                        Thomas Carter <T.Carter@nanob ots.com> writes:
                        [color=blue]
                        > typedef unsigned int UInt32 ;
                        > UInt32 * x = (UInt32 *) malloc(10) ;
                        > UInt32 * p = x + 1 ;
                        >
                        > f(p) ;
                        >
                        > f() is now dealing with an unaligned buffer.[/color]

                        No, it isn't. Perhaps you need a refresher on how 'C' pointers work?

                        Cheers,
                        --
                        In order to understand recursion you must first understand recursion.
                        Remove /-nsp/ for email.

                        Comment

                        • Martin Ambuhl

                          #13
                          Re: Does UNALIGNED attribute exist in GNU C?

                          Thomas Carter wrote:
                          [color=blue]
                          > typedef unsigned int UInt32 ;
                          > UInt32 * x = (UInt32 *) malloc(10) ;
                          > UInt32 * p = x + 1 ;
                          >
                          > f(p) ;
                          >
                          > f() is now dealing with an unaligned buffer.[/color]

                          No it isn't.

                          Go over the following carefully, testing it with your implementation.
                          Then go back to your elementary C text and learn how pointer arithmetic
                          works.

                          #include <stdio.h>
                          #include <stdlib.h>

                          #define MAGICNUMBER 10

                          int main(void)
                          {
                          unsigned int *x = malloc(MAGICNUM BER);
                          unsigned int *p = x + 1;
                          unsigned i;
                          if (!x) {
                          fprintf(stderr, "The allocation of x failed.\n"
                          "Bailing out ...\n");
                          exit(EXIT_FAILU RE);
                          }
                          printf("The buffer x is of %u unsigned ints.\n"
                          "Each is properly aligned.\n"
                          "The addresses x[i] are:\n", MAGICNUMBER);
                          for (i = 0; i < MAGICNUMBER; i++)
                          printf(" &x[%u] = %p, x+%u = %p\n",
                          i, (void *) &x[i], i, (void *) (x + i));
                          printf("\nAnd p, initialized to point at x + 1, has the\n"
                          "same value as the correctly aligned &x[1] above:\n"
                          " p = %p\n", (void *) p);
                          free(x);
                          return 0;
                          }


                          [output on my implementation]
                          The buffer x is of 10 unsigned ints.
                          Each is properly aligned.
                          The addresses x[i] are:
                          &x[0] = 20d50, x+0 = 20d50
                          &x[1] = 20d54, x+1 = 20d54
                          &x[2] = 20d58, x+2 = 20d58
                          &x[3] = 20d5c, x+3 = 20d5c
                          &x[4] = 20d60, x+4 = 20d60
                          &x[5] = 20d64, x+5 = 20d64
                          &x[6] = 20d68, x+6 = 20d68
                          &x[7] = 20d6c, x+7 = 20d6c
                          &x[8] = 20d70, x+8 = 20d70
                          &x[9] = 20d74, x+9 = 20d74

                          And p, initialized to point at x + 1, has the
                          same value as the correctly aligned &x[1] above:
                          p = 20d54

                          Comment

                          • Maurizio Loreti

                            #14
                            Re: Does UNALIGNED attribute exist in GNU C?

                            Thomas Carter <T.Carter@nanob ots.com> writes:
                            [color=blue]
                            > On Sun, 19 Jun 2005 13:36:52 +0100, Lawrence Kirby wrote:
                            >[color=green]
                            > > On Sat, 18 Jun 2005 14:50:47 +0000, Thomas Carter wrote:
                            > >[color=darkred]
                            > >> On Thu, 19 May 2005 19:56:17 +0100, Roger Leigh wrote:
                            > >>
                            > >>> -----BEGIN PGP SIGNED MESSAGE-----
                            > >>> Hash: SHA1
                            > >>>
                            > >>> "Pavel A." <pavel_a@NOwrit emeNO.com> writes:
                            > >>>
                            > >>>> What is equivalent of __unaligned attribute in GNU C?
                            > >>>> I've searched for some time and it seems that it just does not exist...
                            > >>>
                            > >>> It appears not to. That's probably because it should not be needed
                            > >>> unless your code is buggy.
                            > >>
                            > >> I respectfully disagree: In networking applications, for performance
                            > >> reasons (to avoid data copying) one must frequently deal with portions of
                            > >> buffers that are not correctly aligned.[/color]
                            > >
                            > > This is a generalisation which is the for most part wrong. Not totally
                            > > wrong but still mostly wrong.[/color]
                            >
                            > typedef unsigned int UInt32 ;
                            > UInt32 * x = (UInt32 *) malloc(10) ;
                            > UInt32 * p = x + 1 ;
                            >
                            > f(p) ;
                            >
                            > f() is now dealing with an unaligned buffer. Lots of examples similar to[/color]

                            1) Wrong. p will point to x + sizeof(UInt32) .
                            2) If UInt32 means "unsigned int 32 bits long" there is no guarantee
                            that a plain "unsigned int" is 32 bits long. Actually, with my
                            compiler & platform of choice, it is 64 bits long.

                            --
                            Maurizio Loreti http://www.pd.infn.it/~loreti/mlo.html
                            Dept. of Physics, Univ. of Padova, Italy ROT13: ybergv@cq.vasa. vg

                            Comment

                            • Chris Torek

                              #15
                              Re: Does UNALIGNED attribute exist in GNU C?

                              In article <BSgte.7326$eM6 .2629@newsread3 .news.atl.earth link.net>
                              Martin Ambuhl <mambuhl@earthl ink.net> wrote:[color=blue]
                              >#define MAGICNUMBER 10[/color]

                              [the magic number came from elsewhere; all Martin Ambuhl did was
                              move it to a #define.]
                              [color=blue]
                              > unsigned int *x = malloc(MAGICNUM BER);[/color]
                              ....[color=blue]
                              > if (!x) {
                              > fprintf(stderr, "The allocation of x failed.\n"
                              > "Bailing out ...\n");
                              > exit(EXIT_FAILU RE);
                              > }
                              > printf("The buffer x is of %u unsigned ints.\n"
                              > "Each is properly aligned.\n"
                              > "The addresses x[i] are:\n", MAGICNUMBER);[/color]

                              It is worth noting here that "x" now points to ten *bytes*, not
                              ten "unsigned int"s. If sizeof(unsigned int) is 2, this is five
                              "unsigned int"s; if sizeof(unsigned int) is 4, this is two with
                              two leftover bytes; and if sizeof(unsigned int) is 8, this is one
                              unsigned int, with two leftover bytes again. Only in the case when
                              sizeof(unsigned int) == 1 -- which is probably not supported by
                              GCC, although this does occur on some standalone implementations
                              -- is it ten unsigned ints.

                              Of course, it is true that sometimes one must deal with "misaligned "
                              memory, in places like network drivers (Ethernet headers are,
                              inconveniently, 14 bytes instead of 16). But there are ways to
                              deal with this without ever using a single line of nonstandard C.
                              Sometimes -- perhaps even "often" or "usually" -- the portability
                              gain of doing byte-at-a-time arithmetic to work with 16 and 32 bit
                              values outweighs the (usually tiny) performance gain one can obtain
                              using a compiler's "special features" for accessing unaligned value.
                              (The only way to find out for sure, of course, is to do both,
                              and measure.)
                              --
                              In-Real-Life: Chris Torek, Wind River Systems
                              Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W) +1 801 277 2603
                              email: forget about it http://web.torek.net/torek/index.html
                              Reading email is like searching for food in the garbage, thanks to spammers.

                              Comment

                              Working...