next up previous
Next: Character_IO Up: simlib Previous: MemStatistics

UnsafeConversion


     NAME
	  unsafeconversion - Routines to override types	for simple
	  values

     DESCRIPTION
	  This class contains routines that change type	of
	  bitpatterns. They are	useful when interfacing	to external
	  routines or doing I/O	to unformatted interfaces. It can for
	  example copy a real number to	an integer variable and	vice
	  versa	without	changing the actual bitpattern.

     EXAMPLE
	     external class UnsafeConversion;
	     ref(UnsafeConversion) Override;
	     integer Bitpattern;
	     real X;
	     Override:-new UnsafeConversion;
	     Bitpattern:=<read as integer from pipe>;
	     X:=Override.ItoReal(Bitpattern);

     AUTHOR
	  Boris	Magnusson, Lund	Institute of Technology.

     USE
	  Declaration: external	class UnsafeConversion;
	  Compilation: simcomp -L=/usr/local/simulabin -I=simlib
	  Linking: simld ... -lsimlib

     DETAILED INTERFACE
	     class UnsafeConversion;
	  This	class  contains	  routines   that   change   type   on
	  bitpatterns.	They  are  useful when interfacing to external
	  routines or doing I/O	 to  unformatted  interfaces.  If  for
	  example a bitpattern has been	obtained as an integer,	but it
	  is know that the bitpattern should really be interpreted  as
	  a  real. Accordingly,	a program can use this class to	output
	  a real number	as an integer bitpattern.

	  The routines	converting  to/from  16-bit  integers  can  be
	  useful  when	interfacing to C-programs that use this	packed
	  form in e. g.	 structs or in arrays, since Simula  is	 using
	  32bit	integers also for 16-bit values.

	  Supers: -
	  Kind:	Instantiable
	  Init:	none
	  Sequencing:
	   (ItoSS / SStoI / ItoReal  /RealtoI /	IItoLong / LongtoII)*

     OPERATIONS
     ItoSS
	     procedure ItoSS(I,s1_result,s2_result);
	     name s1_result,s2_result;
	     integer i;
	     short integer s1_result;!:=I//2**16, sign extended;
	     short integer s2_result;!:=mod(I,2**16), sign extended;
	  Split	an 32-bit integer into two 16-bit intergers containing
	  the  most  significant(s1)  and  least significant(s2) bits.
	  Both results are sign	extended.

     SStoI
	     integer procedure SStoI(s1,s2);
	     short integer s1,s2;
	  Combine  two	16-bit	integers  into	one  32-bit   integer.
	  Returns  s1*2**16+s2,	 except	 that  the  numbers are	masked
	  logically AND	together.

     ItoReal
	     real procedure ItoReal(Bits);
	     integer Bits; ! Formally an integer but bitpattern	of a
	     real;
	  This procedure returns  the  same  bitpattern	 as  given  as
	  parameter, but formally as a real. Can be used to circumvent
	  Simula typechecking of simple	values.

     RealtoI
	     integer procedure RealtoI(Bits);
	     real Bits;	! Formally a real but bitpattern of an
	     integer;
	  This procedure returns  the  same  bitpattern	 as  given  as
	  parameter,  but  formally  as	 an  integer.  Can  be used to
	  circumvent Simula typechecking of simple values.

     IItoLong
	     long real procedure IItoLong(I1,I2);
	     integer I1,I2;
	  Combine two 32-bit integers into one 64-bit long real.  This
	  procedure  returns the bitpatterns given to it, changing the
	  type to long	real.  I1  is  the  high  order	 bits  of  the
	  representation  of  a	 long real (including the sign bit and
	  exponent) while I2 is	the low	order bits of the mantissa.

     LongtoII
	     procedure LongtoII(Bits,I1,I2);
	     name I1,I2;
	     Long real Bits;
	     integer I1,I2;
	  Splits one 64-bit long real into  two	 32-bit	 integer  bit-
	  patterns.   This  procedure  returns the bitpattern given to
	  it.  I1 is the high order bits of the	 representation	 of  a
	  long	real (including	the sign bit and exponent) while I2 is
	  the low order	bits of	the mantissa.