Browse Prior Art Database

Endian Test for Determining the Endian Mode of Program Execution

IP.com Disclosure Number: IPCOM000116286D
Original Publication Date: 1995-Aug-01
Included in the Prior Art Database: 2005-Mar-30
Document File: 4 page(s) / 103K

Publishing Venue

IBM

Related People

Gillig, JR: AUTHOR

Abstract

This invention disclosure defines a test that can be used by a program to determine whether the processor it is currently running on is executing in Little-Endian (LE) mode or Big-Endian (BE) mode.

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

Endian Test for Determining the Endian Mode of Program Execution

      This invention disclosure defines a test that can be used by a
program to determine whether the processor it is currently running on
is executing in Little-Endian (LE) mode or Big-Endian (BE) mode.

      Endian is essentially an addressing model that affects the byte
ordering of both data and instructions stored in computer memory, as
illustrated in Fig. 1.  The BE addressing model assigns or maps the
lowest address to the highest-order data byte (that is, the Most
Significant Byte or "leftmost" byte) of a multibyte scalar data item.
The LE addressing model assigns or maps the lowest address to the
lowest-order data byte (that is, the Least Significant Byte or
"rightmost" byte) of a multibyte scalar data item.  BE and LE are the
prevalent addressing models.

      A multibyte scalar data type such as an integer, pointer,
float, or executable instruction has endianess (is BE or LE) while a
single-byte character does not.  A LE scalar data item is equivalent
to a byte-reversed BE scalar data item.  A multibyte scalar, although
it has more than one byte and is composed of smaller addressable
units of storage, should be treated as a single, indivisible data
item by an executing program.  Program code that addresses subunits
of storage within a multibyte scalar data item becomes dependent on
the byte addressing order.  A program should be designed and written
as endian neutral or independent of endian so that it is source code
portable, without code change to execute on processors of the
opposite endian mode.  In cases where a program must be endian
specific, then when porting it to the opposite endian that code must
be rewritten or, alternatively, it can be made adaptive.

      If a program queries or tests the processor to decide on the
endian mode of its current execution so as to take a LE processing
path or BE processing path at run time, then the program is said to
be endian adaptive.  If the processor does not provide a means for
telling software its endian mode, then a program test can be
improvised.  An "Endian Test" can be implemented as described in Fig.
2.

An endian test illustrative of the invention is implemented in
the C++ programming language below:
  #define  BE  1
  #define  LE  0
   char  endian (void)
     { short x = 0x0100;
       return *((char *) &x); // return 0x01 for BE and 0x00 for LE
   }
  and endian macros based on this test can...