freebsd swab()

  • Thread starter Mick Charles Beaver
  • Start date
M

Mick Charles Beaver

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
 
P

Phil Carmody

Richard Heathfield said:
Mick Charles Beaver said:


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
 
M

Mick Charles Beaver

Richard Heathfield said:
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
 
P

Peter Nilsson

[email protected] (Mick Charles Beaver) said:
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.
 
E

Erik Trulsson

Peter Nilsson said:
[email protected] (Mick Charles Beaver) said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top