Multics Technical Bulletin                                MTB-683
  Fortran Typeless Functions.

  To:       Distribution

  From:     Michael H.  Mabey

  Date:     9 October 1984

  Subject:  Fortran Typeless Functions Specification.

  1.  Abstract

  This  MTB describes  the Multics  implementation of  the typeless
  functions  found in  GCOS Fortran.   The "typeless"  data type as
  well as the actual object code generated, is discussed.

  Comments on this MTB should be sent to the author -

       via Multics mail to:

          Mabey.Multics on System M

       via posted mail to:

          Michael Mabey
          Advanced Computing Technology Center
          Foothills Professional Building
          Room #301, 1620 - 29th Street N.W.
          Calgary, Alberta T2N-4L7
          CANADA

       via telephone to:

          (403)-270-5400
          (403)-270-5413

  ________________________________________

  Multics project  internal documentation; not to  be reproduced or
  distributed outside the Multics project.


  MTB-683                                Multics Technical Bulletin
                                        Fortran Typeless Functions.

                          TABLE OF CONTENTS

  Section    Page  Subject
  =======    ====  =======

  1             i  Abstract
  2             1  Introduction
  3             2  The Typeless Data Type
  3.1           2  . . Typeless Arithmetic
  3.2           2  . . Typeless Assignments
  4             3  The Typeless Functions
  4.1           3  . . The AND Function
  4.2           3  . . The BOOL Function
  4.3           3  . . The COMPL Function
  4.4           4  . . The FLD Function
  4.4.1         4  . . . . The FLD Function on The Right Hand Side
  4.4.2         4  . . . . The FLD Function on The Left Hand Side
  4.5           5  . . The OR Function
  4.6           6  . . The XOR Function
  5             7  The New Nontypeless Functions
  5.1           7  . . The ilr Function
  5.2           7  . . The ils Function
  5.3           7  . . The irl Function
  5.4           8  . . The irs Function


  Multics Technical Bulletin                                MTB-683
  Fortran Typeless Functions.

  2.  Introduction

  To aid in the migration of  GCOS software to Multics, ten builtin
  functions  have  been added  to  Multics Fortran.   The functions
  involve new concepts to the  language.  All the builtins are used
  to manipulate bits, and several return their values in a new data
  type  which GCOS  calls "typeless".  This  document discusses the
  Multics implementation of these GCOS functions.


  MTB-683                                Multics Technical Bulletin
                                        Fortran Typeless Functions.

  3.  The Typeless Data Type

  Six  of the  new functions, AND,  OR, XOR, BOOL,  COMPL, and FLD,
  return  their results  in a new  data type  called "typeless".  A
  typeless value is treated as a special form of integer.

  3.1.  Typeless Arithmetic

  There  are  no  typeless  variables or  constants,  only typeless
  expressions.  Typeless entities can only exist as a result of one
  of the six typeless functions.

  Typeless entities can only be combined with other typeless values
  or integers.  If an integer  is present in a typeless expression,
  the integer is treated as a typeless value within the expression.
  Typeless arithmetic  expressions can only contain  the four basic
  mathematical  operators:   "+",  "-",  "*",  and  "/".   Typeless
  relational  expressions   can  contain  any   of  the  relational
  operators  (".lt.", ".le",  ".eq.", ".ne.",  ".ge.", and ".gt.").
  When typeless values are combined, the operations are done in the
  same  manner  as they  are performed  on normal  signed integers.
  With the  arithmetic operators the result  is typeless.  With the
  relational operators, the result is logical.

  3.2.  Typeless Assignments

  Assignments  of  this data  type  can only  be to  variables that
  require exactly one word of storage.  The variables can be either
  scalar or subscripted.  The typeless value that is to be assigned
  is  copied into  the memory address  of the  variable without any
  conversion.   The  one exception  to  this is  in  assignments to
  logical variables.   If any bit  is set in the  typeless value, a
  .TRUE.  is  assigned to the logical.   Otherwise a .FALSE.  value
  is given.


  Multics Technical Bulletin                                MTB-683
  Fortran Typeless Functions.

  4.  The Typeless Functions

  None  of  the  typeless  functions requires  any  runtime support
  routines - all can be  generated with inline code.  A description
  of each function and the required ALM code is given:

  4.1.  The AND Function

  The AND function returns a bit  by bit logical product of its two
  or more arguments.  All arguments must have a one word data type.

  The alm code for the AND function is very simple:

            ldq       (argument 1)
            anq       (argument 2)
            ...
            anq       (argument N)

  The result is in the Q register.

  4.2.  The BOOL Function

  This  function  returns its  single  argument as  a  typeless bit
  string.  Its  function is analogous to  the PL/1 unspec function.
  The argument must have a one word data type.

  No alm code is necessary to perform the type conversion.

  4.3.  The COMPL Function

  The  COMPL  function  returns a  bit  string which  is  the one's
  complement of its single argument.   The argument must have a one
  word data type.

  The alm code:
            lcq       1,dl
            erq       (argument)

  The result is now in the Q register.


  MTB-683                                Multics Technical Bulletin
                                        Fortran Typeless Functions.

  4.4.  The FLD Function

  The FLD function  is used to manipulate individual  bits and is a
  special  case.  It  is the only  function that can  appear on the
  left hand side of an assignment statement as well as on the right
  hand side.

  4.4.1.  The FLD Function on The Right Hand Side

  The FLD function takes three  arguments.  The first two arguments
  are  integer expressions  where 0 <=  first argument <=  35, 1 <=
  second argument <=  36, and the sum of  argument one and argument
  two must  not be greater  than 36.  This function  extracts a bit
  field  of  argument2 bits  from  a 36  bit string  represented by
  argument3.   The  field is  extracted  starting from  argument1 -
  counting from left  to right where the 0'th  bit is the leftmost.
  The resulting field is right justified and the remaining bits are
  set to zero.

  The alm for this function is:
            ldq       (argument3)  load the value into the Q
            lda       (argument1)  load the starting position into A
            qls       0,al         shift Q left by the A
            lca       (argument2)  load the negated string length
            qrl       36,al        shift Q right by 36-string length

  The result is in the Q register.

  A few optimizations can be  made with constant arguments.  If the
  value of the  first or second argument is  known at compile time,
  it would not have to be loaded  into the A register to be used as
  an  offset  for  a  shift instruction.   Instead,  a  shift  of a
  constant amount  would be generated.   In addition, if  the first
  argument was  a zero, the  first shift instruction,  "qls", would
  not  be needed.   If the  first argument  is zero  and the second
  argument is thirty-six, then no  shifting would be needed at all.
  If the field to be extracted is right justified in argument three
  (i.e.  if 36 - (argument1 +  argument2) = 0), then a single "anq"
  of a bit mask could be generated.

  4.4.2.  The FLD Function on The Left Hand Side

  The three arguments to the FLD  function are the same on the left
  hand side as they are on  the right hand side, with the exception
  that  the  third  argument  must be  a  variable.   The argument2
  rightmost  bits  of the  value  to be  assigned is  inserted into
  argument3 starting at the bit position indicated be argument1.


  Multics Technical Bulletin                                MTB-683
  Fortran Typeless Functions.

  It would be  easy to generate a single  EIS intruction to perform
  the insertion, but  the start up time for  such an instruction is
  relatively  slow.  The  following ALM  sequence executes  in less
  time than an equivalent EIS intruction.

            lxl0      (argument 1)     load initial bit position in X0
            lxl1      (argument 2)     load string length in X1
            lda       (argument 3)     load word to be updated in A
            alr       0,x0             rotate left until the field
            alr       0,x1                 is right justified in the A
            era       (assigned value) xor in the new bit string
            ldq       0,dl             zero the Q
            lrs       0,x1             put the  field into the correct
            qrl       0,x0                 position in the Q
            ersq      (argument 3)     xor with origional word.

  The  variable to  be changed  (argument 3)  now has  the new bits
  inserted in the correct place.  If one of the first two arguments
  is constant, only  one X register would have to  be loaded in the
  above code.   As well, only  a single "alr"  instruction would be
  needed.

  If both  the first and  the second arguments are  constant, a bit
  mask can be generated and the code shorted even more.

            lda       (assigned value)
            als       (36 - argument1 - argument2)
            era       (argument 3)
            ana       (bit mask)
            ersa      (argument 3)

  A  further  optimization  can  be made  by  excluding  the second
  instruction, "als", if (36 - argument1 - argument2) is zero or if
  the assigned value itself is constant (as the left shift could be
  done  at compile  time).  If the  first argument is  zero and the
  second  argument thirty-six,  then a  simple assignment  could be
  generated.

  4.5.  The OR Function

  This function  returns the bit by  bit logical sum of  its two or
  more arguments.  All arguments must have a one word data type.

  The alm code:
            ldq       (argument 1)
            orq       (argument 2)
            ...
            orq       (argument N)


  MTB-683                                Multics Technical Bulletin
                                        Fortran Typeless Functions.

  The result is in the Q register.

  4.6.  The XOR Function

  This function returns the bit by bit "exclusive or" of its two or
  more arguments.  All arguments must have a one word data type.

  The alm code:
            ldq       (argument 1)
            erq       (argument 2)
            ...
            erq       (argument N)

  The result is left in the Q register.


  Multics Technical Bulletin                                MTB-683
  Fortran Typeless Functions.

  5.  The New Nontypeless Functions

  GCOS Fortran has four integer functions that perform bit shifting
  operations.   These  builtins were  added  to Multics  because of
  their natural relation to the typeless functions.

  Each  of the  following integer functions  accepts two arguments.
  Both  arguments  are integer  expressions.  The  returned integer
  value is the first argument shifted (or rotated) by the number of
  bit  positions  indicated  by   the  second  argument.   A  minor
  extension has been made to the Multics implementation:  the first
  argument to each of the functions may be a typeless expression.

  5.1.  The ilr Function

  This function returns an integer value derived from left rotating
  the first  argument by the  number of bit  positions indicated by
  the second argument.

  The ALM code:
            ldq       (argument 1)
            lda       (argument 2)
            qlr       0,al
  The result is in the Q register.

  5.2.  The ils Function

  This function returns an integer  value derived from shifting the
  first argument left  by the number of bit  positions indicated by
  the second argument.

  The ALM code:
            ldq       (argument 1)
            lda       (argument 2)
            qls       0,al
  The result is left in the Q register.

  5.3.  The irl Function

  This  function  returns  an  integer  value  derived  from  right
  shifting  (without  sign  extension)  the first  argument  by the
  number of bit positions indicated by the second argument.


  MTB-683                                Multics Technical Bulletin
                                        Fortran Typeless Functions.

  The ALM code:
            ldq       (argument 1)
            lda       (argument 2)
            qrl       0,al
  The result is in the Q register.

  5.4.  The irs Function

  This  function  returns  an  integer  value  derived  from  right
  shifting (with  sign extension) the first  argument by the number
  of bit positions indicated by the second argument.

  The ALM code:
            ldq       (argument 1)
            lda       (argument 2)
            qrs       0,al
  The result is in the Q register.