Browse Prior Art Database

Efficient Byte Swapping

IP.com Disclosure Number: IPCOM000118691D
Original Publication Date: 1997-May-01
Included in the Prior Art Database: 2005-Apr-01
Document File: 4 page(s) / 75K

Publishing Venue

IBM

Related People

Johnson, A: AUTHOR

Abstract

Integers are held in two formats according to the machine type, least significant byte first and most significant byte first. Least significant byte first is used by Intel and Motorola processors. Most significant byte first is used by IBM Mainframe, PowerPC* and as the default order for network protocols such as Transmission Control Protocol/Internet Protocol (TCP/IP). Data held in Network order needs to be converted to and from Intel order to be operated on by the machine. This is done by swapping the order of the bytes in a 16-bit or 32-bit word. Most significant byte in number = A, least = D Number = ABCD Byte address n+0 n+1 n+2 n+3 Intel D C B A IBM A B C D

This text was extracted from an ASCII text file.
This is the abbreviated version, containing approximately 53% of the total text.

Efficient Byte Swapping

      Integers are held in two formats according to the machine type,
least significant byte first and most significant byte first.  Least
significant byte first is used by Intel and Motorola processors.
Most significant byte first is used by IBM Mainframe, PowerPC* and as
the default order for network protocols such as Transmission Control
Protocol/Internet Protocol (TCP/IP).  Data held in Network order
needs to be converted to and from Intel order to be operated on by
the machine.  This is done by swapping the order of the bytes in a
16-bit or 32-bit word.
  Most significant byte in number = A, least = D
  Number = ABCD
           Byte address
           n+0 n+1 n+2 n+3
  Intel   D   C   B   A
  IBM   A   B   C   D

      If a number in IBM order in memory is read by an Intel
processor into a register, it will come out as DCBA and need to be
byte-swapped to be usable.

      The IBM C Set and Visual Age C++ compilers have a built in
rotate left and rotate right function, which map efficiently to Intel
instructions.  These can be used to create a function to swap bytes
of an integer.

To swap bytes:
  include <bultin.h>
  define ntohs(x)  (_srotl((x), 8)

To swap bytes in a 32-bit integer:
  define ntohs(x)  (_rotl(x, 8) & 0x00ff00ff | \
         ntohl(x)   _rotr(x, 8) & 0xff00ff00)

Rearranging the bytes in memory before reading them is one commonly
used idea.
  mov     al,[ebx+3]
  xchg    [ebx],al
  mov     al,[ebx+2]
  xchg    [ebx+1],al
  mov     eax,[ebx]

This requires many memory references, which can be slow.  Swapping
the bytes in a 16 bit quantity by rotating the 16-bit quantity by 8
bits is well known.
     rol     ax,8
  or
     ror     ax,8
  or
     xchg    al,ah
  though only the former two are accessible using the C compiler
built in instructions.

The standard way of byte swapping a 32-bit quantity is the following
assembler sequence:
     ror     ax,8
     ror     eax,16
     ror     ax,8

      This is quite efficient but not directly generatable from C
code.  The 16-bit instructions cause problems for the Intel
Pentium-Pro processor, as interleaving 16-bit and 32-bit instructions
causes the pipeline to be flushed, slowing things down.
         The Intel 486 instruction
      bswap
is the most efficient way of doing this but is not available on the
386 proces...