473,395 Members | 2,468 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

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

Oct 29 '08 #1
5 2502
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
Oct 29 '08 #2
Richard Heathfield <rj*@see.sig.invalidwrites:
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
Oct 29 '08 #3
Richard Heathfield <rj*@see.sig.invalidwrote:
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

Oct 29 '08 #4
m...@cs.wisc.edu (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
Oct 29 '08 #5
Peter Nilsson <ai***@acay.com.auwrote:
m...@cs.wisc.edu (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
er******@student.uu.se
Oct 30 '08 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: siliconmike | last post by:
http://dev.mysql.com/doc/mysql/en/freebsd.html says: You can link MySQL on FreeBSD 4.x against the LinuxThreads library, which avoids a few of the problems that the native FreeBSD thread...
13
by: Uwe Mayer | last post by:
<posted & mailed> Hi, AFAICT there seems to be a bug on FreeBSD's Python 2.3.4 open function. The documentation states: > Modes 'r+', 'w+' and 'a+' open the file for updating (note that...
0
by: Bill Moran | last post by:
I'm having some problems. Hopefully there are some FreeBSD folks here that can help me out, if not, I'll try the FreeBSD lists next. I'm running Postgres 7.4 installed from a just cvsupped...
1
by: Vyacheslav Sotnikov | last post by:
Hello python-list, is anybody has any success in taking this components live together? I need to access remote Oracle 9i from freebsd 6.2 machine using python 2.4.4. Thanks in advance for answer.
4
by: robert | last post by:
When i freeze a python app (simple - no strange sys calls) for x86 Linux, does this stuff run well also on x86 FreeBSD? Robert
2
by: Ruslan A Dautkhanov | last post by:
Hello ! I'm about to install O9i on FreeBSD box. uname -a: FreeBSD stat2.scn.ru 5.2.1-RELEASE-p3 FreeBSD 5.2.1-RELEASE-p3 #2: Fri Apr 23 19:19:43 KRAST 2004...
0
by: Akira Kitada | last post by:
Hi list, I was trying to build Python 2.6 on FreeBSD 4.11 and found it failed to build some of the modules. """ Failed to find the necessary bits to build these modules: _bsddb ...
0
by: Akira Kitada | last post by:
Hi Marc-Andre, Thanks for the suggestion. I opened a ticket for this issue: http://bugs.python.org/issue4204 Now I understand the state of the multiprocessing module, but it's too bad to see...
0
by: M.-A. Lemburg | last post by:
On 2008-10-25 20:19, Akira Kitada wrote: Thanks. The errors you are getting appear to be related to either some missing header files or a missing symbol definition to enable these - looking...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.