Cost of byte swapping?

Cost of byte swapping?

Post by Dave Toppe » Tue, 27 Jan 1998 04:00:00



I'm grateful to so many folks who replied to my previous post re: byte
swapping.  I am trying to make a music app (running under Linux)
intelligent about BE and LE soundfiles ... both reading and writing.

In the process of doing all this ... I am curious about what kind of a
performance hit I'll be taking wrt. ALWAYS swapping bytes on a LE
machine (eg., PC).

DT

 
 
 

Cost of byte swapping?

Post by David Wrag » Wed, 28 Jan 1998 04:00:00



> I'm grateful to so many folks who replied to my previous post re: byte
> swapping.  I am trying to make a music app (running under Linux)
> intelligent about BE and LE soundfiles ... both reading and writing.

> In the process of doing all this ... I am curious about what kind of a
> performance hit I'll be taking wrt. ALWAYS swapping bytes on a LE
> machine (eg., PC).

Modern processors can swap bytes as fast as (or faster than)
their main memory can supply them, so it isn't worth worrying about.

Converting between obscure floating point formats is a different
matter, but everything uses IEEE[78]54 these days so that isn't a
problem either.

 
 
 

Cost of byte swapping?

Post by Terje Mathise » Wed, 28 Jan 1998 04:00:00



> I'm grateful to so many folks who replied to my previous post re: byte
> swapping.  I am trying to make a music app (running under Linux)
> intelligent about BE and LE soundfiles ... both reading and writing.

> In the process of doing all this ... I am curious about what kind of a
> performance hit I'll be taking wrt. ALWAYS swapping bytes on a LE
> machine (eg., PC).

Very low, as long as you can inline the proper asm opcode:

  BSWAP register

This is a one-cycle non-pairable instruction.

If you have 16-bit samples instead of 32-bit (yeah, I know this is more
probable!), then you have even more choices:

One of these:

  xchg al,ah
  xchg bl,bh
  xchg cl,ch
  xchg dl,dh

or any of these:

  ror ax,8
  ror bx,8
  ror cx,8
  ror dx,8
  ror si,8
  ror di,8
  ror bp,8

You can also use C:

  #define swap16(a) ( (((a) & 0xff) << 8) | (((a) >> 8) & 0xff))

Finally, for C code I would just make it portable:

  #define GET16BE(src) ((((byte *) src)[0] << 8) | ((byte *) src)[1])

No matter which version you use, it should be easy to be fast enough
that the memory loads & stores are the limiting factor, not the byte
swapping!

Terje

--

Using self-discipline, see http://www.eiffel.com/discipline
"almost all programming can be viewed as an exercise in caching"

 
 
 

Cost of byte swapping?

Post by Michael Meissne » Wed, 28 Jan 1998 04:00:00



> I'm grateful to so many folks who replied to my previous post re: byte
> swapping.  I am trying to make a music app (running under Linux)
> intelligent about BE and LE soundfiles ... both reading and writing.

> In the process of doing all this ... I am curious about what kind of a
> performance hit I'll be taking wrt. ALWAYS swapping bytes on a LE
> machine (eg., PC).

It obviously depends on how you swap the bytes.  If you use asm and/or asm
inserts for the x86 and you don't care about running on 386's, it is 1 extra
cycle (4 if using Cyrix) before the read or write to do a BSWAP instruction.

However, I really can't imagine that the reading/writing is all that
performance critical, so it's best to express the algorithm in a high level
fashion that works for either endian format (and hosts can be either endian as
well).

--
Michael Meissner, Cygnus Solutions (Massachusetts office)
4th floor, 955 Massachusetts Avenue, Cambridge, MA 02139, USA

 
 
 

1. How much time does swapping cost a process?

System- Linux Red Hat 5.1 Intel

I am working on a heavy numerical program that uses fairly large data
sets. These data sets are large enough to cause my computer to start
swapping like crazy. I know this is slow.

In order to track the effectiveness of the algorithm I am using
times() to return the utime and stime of the process. However, when
the silly thing runs it takes, say 8 minutes, but the times reported
back give me something on the order of 1.5 minutes.

The code that prints the times at the end of each iteration is:

   times(&timeinfo);
   printf("%f %f\n",(float)((float)timeinfo.tms_utime/(float)CLK_TCK),
                    (float)((float)timeinfo.tms_stime/(float)CLK_TCK));

I would like to be able to figure out how much the swapping is really
costing me? I thought stime would indicate how much time the system
was using, including time require to swap pages, but the times
reported come no where near the actual elapsed time used. This has
been checked even after killing any other major process, so the
program in question can run free.

Just in case times() is not particularly common I copied the man page
for it below:

Name
       times - get process times

Synopsis
       #include <sys/times.h>

       clock_t times(struct tms *buf);

Description
       times stores the current process times in buf.

       struct tms is as defined in /usr/include/sys/times.h:

       struct  tms  {
                    clock_t tms_utime;  /* user time */
                    clock_t tms_stime;  /* system time */
                    clock_t tms_cutime; /* user time of children */
                    clock_t tms_cstime; /* system time of children */
                    };

       times  returns the number of clock ticks that have elapsed
       since the system has been up.

Conforming to
       SVr4, SVID, POSIX, X/OPEN, BSD 4.3

See Also
       time(1), getrusage(2), wait(2)

--
-------------------------------------------------------------------------

MIT Course XIII-A  Missile Subspecialty    
-------------------------------------------------------------------------

2. An OS for m-o-o-t

3. 1/2 acquisition cost,lower management costs,way better security

4. Newbie wants to install networking. Help!

5. byte swapping for a double

6. User Configuration in RH5.1

7. byte swapping

8. user diskspace config

9. Mounting a byte-swapped volume?

10. swap/bytes/blocks? (solaris 8 issues)

11. Byte swapping question (Intel v. Next)

12. Byte Swapping ???

13. FreeBSD 3.1/libpcap 0.4 swapping byte order in IP header?