freebsd swab()

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Mick Charles Beaver

    #1

    freebsd swab()

    Hello,

    Would anyone know why the FreeBSD implementation of swab() uses an
    unsigned long for its temporary variable when swapping bytes?

    FreeBSD CVS of swab.c:
    http://tinyurl.com/5kn75y

    Thanks,
    Mick

  • Richard Heathfield

    #2
    Re: freebsd swab()

    Mick Charles Beaver said:
    Hello,
    >
    Would anyone know why the FreeBSD implementation of swab() uses an
    unsigned long for its temporary variable when swapping bytes?
    Well, they gotta use something, haven't they? And unsigned long is a
    reasonable choice. (Presumably sizeof(unsigned long) is 8 on that system.)

    Personally, I use memcpy (and a buffer somewhat larger than 8), but the way
    they do it is fine, although those casts are a bit pointless, aren't they?

    --
    Richard Heathfield <http://www.cpax.org.uk >
    Email: -http://www. +rjh@
    Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
    "Usenet is a strange place" - dmr 29 July 1999

    Comment

    • Phil Carmody

      #3
      Re: freebsd swab()

      Richard Heathfield <rjh@see.sig.in validwrites:
      Mick Charles Beaver said:
      >
      >Hello,
      >>
      >Would anyone know why the FreeBSD implementation of swab() uses an
      >unsigned long for its temporary variable when swapping bytes?
      >
      Well, they gotta use something, haven't they? And unsigned long is a
      reasonable choice. (Presumably sizeof(unsigned long) is 8 on that system.)
      >
      Personally, I use memcpy (and a buffer somewhat larger than 8), but the way
      they do it is fine, although those casts are a bit pointless, aren't they?
      I'd not use their memcpy to do a swab on your DS9000, though.
      Firstly, it doesn't swab, and secondly I think it invokes UB.

      Phil
      --
      Christianity has such a contemptible opinion of human nature that it does
      not believe a man can tell the truth unless frightened by a belief in God.
      No lower opinion of the human race has ever been expressed.
      -- Robert Green Ingersoll (1833-1899), American politician and scientist

      Comment

      • Mick Charles Beaver

        #4
        Re: freebsd swab()

        Richard Heathfield <rjh@see.sig.in validwrote:
        Well, they gotta use something, haven't they? And unsigned long is a
        reasonable choice. (Presumably sizeof(unsigned long) is 8 on that
        system.)
        My question could be better stated as:

        Why is using the unsigned long as the temp variable a better choice than
        using a char? From my reading of the code, the #define STEP is doing
        something as simple as:

        char a, b;
        unsigned long temp;

        temp = a;
        a = b;
        b = temp;

        If I were to write that myself, I would have made temp a char...
        I realize there is no loss of precision in the above case, though.

        -Mick

        Comment

        • Peter Nilsson

          #5
          Re: freebsd swab()

          m...@cs.wisc.ed u (Mick Charles Beaver) wrote:
          Would anyone know why the FreeBSD implementation of
          swab() uses an unsigned long for its temporary
          variable when swapping bytes?
          >
          FreeBSD CVS of swab.c:http://tinyurl.com/5kn75y
          Unsigned char would certainly be a better choice,
          also fp and tp should be unsigned char pointers IMO.

          It's possibly the case that unsigned long optimises
          better than a character type on certain pathological
          systems. [Metrowerks for 68k used to be attrocious
          at optimising expressions involving only integer
          types narrower than int.]

          Certainly modern compilers should be capable of
          treating temp as a character type if need be.

          But a bigger question is: why does it uses int for n?

          I think you'll need to ask the authors.

          --
          Peter

          Comment

          • Erik Trulsson

            #6
            Re: freebsd swab()

            Peter Nilsson <airia@acay.com .auwrote:
            m...@cs.wisc.ed u (Mick Charles Beaver) wrote:
            >Would anyone know why the FreeBSD implementation of
            >swab() uses an unsigned long for its temporary
            >variable when swapping bytes?
            >>
            >FreeBSD CVS of swab.c:http://tinyurl.com/5kn75y
            >
            Unsigned char would certainly be a better choice,
            also fp and tp should be unsigned char pointers IMO.
            >
            It's possibly the case that unsigned long optimises
            better than a character type on certain pathological
            systems. [Metrowerks for 68k used to be attrocious
            at optimising expressions involving only integer
            types narrower than int.]
            >
            Certainly modern compilers should be capable of
            treating temp as a character type if need be.
            >
            But a bigger question is: why does it uses int for n?
            >
            I think you'll need to ask the authors.
            If you can find the authors - and if they remember the
            answers to those questions. If you check the CVS history
            of that file (see URL above) you will see that most of it
            has not been changed since it was imported from the BSD4.4-lite
            sources back in 1994. It may well have been unchanged for several
            years before that, but I do not have access to the commit logs
            (if they even exist) of the historical BSD sources.

            One possibility is that there were good reasons for writing the code in that
            way back then (15-20 years ago), but that those reasons are no longer
            relevant.



            --
            <Insert your favourite quote here.>
            Erik Trulsson
            ertr1013@studen t.uu.se

            Comment

            Working...