14.3 Endian Interface

ORIGIN '~beta/basiclib/betaenv';
LIB_DEF 'sysutilsendian' '../lib';
(* Operations for testing whether or not the host system uses
 * big endian or little endian data representation.
 * In addition there are operations for converting a long (4bytes)
 * and a short (2 bytes) memory location from big/little to
 * little/big endian.
 *)
MDBODY linux   'private/endian_littleBody'
       nti     'private/endian_littleBody'
       default 'private/endian_bigBody';

---lib:attributes---

(* Predicates indicating the endian-type of the host system. *)

isBigEndian:
  (# b: @boolean
    ...
  exit b
  #);
isLittleEndian:
  (# b: @boolean
    ...
  exit b
  #);

(* BSD like conversion routines. These operations are safe. *)

ntohl:
  (# i: @Integer;
  enter i
  ...
  exit i
  #);
htonl:
  (# i: @Integer;
  enter i
  ...
  exit i
  #);

ntohs:
  (# i: @Integer;
  enter i
  ...
  exit i
  #);
htons:
  (# i: @Integer;
  enter i
  ...
  exit i
  #);


(* Usage of the following operations is strongly discouraged for the
 * reasons explained below! (Use the supported operations above)
 * 
 * WARNING: byteSwapShort and byteSwapLong are NOT safe operations,
 * since they take a memory argument as a parameter. This require
 * use of the low-level primitive @@. 
 * Ex:
 *    X: @integer; R: [100] @int16
 *    @@XX->byteSwapLong;
 *    (for i: R.range repeat
 *         @@R[i]->byteSwapShort
 *    for)
 * After @@X/@@R[i] has been computed, a grabage collection MUTS NOT happen,
 * since this invalidates the computed addresses. It is therefore
 * recommedn to use statis versions like
 * 
 *     BSL: @ byteSwapLong
 *     @@X->BSL
 * 
 * Another alternative is to use the TOS operations directly.
 * The TOS operations are currently only implemented for Linux
 *)
byteSwapShort:
  (# adr: @integer
  enter adr
  do adr->TOS'%byteSwapShort'
  #);
byteSwapLong:
  (# adr: @integer
  enter adr
  do adr->TOS'%byteSwapLong'
  #)


14.3 Endian Interface
© 1994-2002 Mjølner Informatics
[Modified: Tuesday January 11th 2000 at 16:32]