FloatCode for Scheme B                        Date: 1952
     
    Written by R.A Brooker and A.Glennie

    
    Floatcode - Bodleian Special Collection 
    MS. Eng. misc. b. 260-C.42.pdf
    p5-p6, p41-p55
     
    Explanation on how to use FloatCode can be found in Programming Manual 
    Second edition: p122-p139 (section Chapter 5. Aids to Coding)
    
     
    [Routine FLOATCODE A]               [Routine FLOATCODE B]
 
    [Col :          S ]                 [Col I          U ]       
                                               
    | //// | / | //// |                 | $$VO | / | BIZG |                   
    | E/// | E | ///@ |                 | /IQG | E | A:ZG |
    | //// | @ | //// |                 | SU/P | @ | ZUE: |                  
    | @/// | A | ///: |       jmptbl1 W | $U// | A | /U/G |                  
    | //// | : | //// |               H | $U// | : | TUZT |                  
    | :/// | S | ///# |               Y | $D// | S | IU/L |                  
    | //// | I | //// |               P | AU// | I | TUET |                  
    | #/// | U | ///T |               Q | $U// | U | DSQG |                  
    | //// | # | //// |               O | $U// | # | TUZZ |                  
    | T/// | D | $$$$ |               B | $U// | D | //KC |                  
    | //// | R | $$$$ |               G | $U// | R | //$/ |                  
    | /E// | J | //// |               " | @J// | J | OITR |
    | //// | N | /U// |               M | /J// | N | KITA |
    | /@// | F | //// | F(A) mant     X | GI// | F | J:/N |
    | //// | C | ///# | F(A) mant     V | "I// | C | BITR |
    | /:// | K | /T$$ | F(A) exp   addr | //// | K | TITA |
    | //// | T | //// | F(R) mant  temp | //I/ | T | ZIQO |
    | /#// | Z | //// | F(R) mant  temp | //// | Z | TSUP |                  
    | //// | L | //// | F(R) exp        | @E// | L |      |
    | /T// | W | //// | temp       temp | //// | W |      | 
    | //// | H | //// | temp       temp | //// | H |      | Floatcode 'control' (=program counter)
    | //E/ | Y | //// | temp            | //// | Y |      | first stack entry
    | //// | P | //// | temp            | //// | P |      |___
    | //@/ | Q | /I// |                 | //// | Q |      | second stack entry
    | //// | O | O#// | W jmptbl2       | $$$/ | O |      |___
    | //:/ | B | P#// | H               | M$$$ | B |      | third stack entry
    | //// | G | "I// | (*) was ////    | $/// | G |      |___
    | //#/ | " | //// |                 | A:ZG | " |      | fourth stack entry
    | //// | M | C#// | Q               | LI/: | M |      |___
    | //T/ | X | C#// | O               | KIQO | X |      | 
    | //// | V | SJ// | B               | //Q/ | V |      | 
    | ///E | $ | D#// | G               | LUZA | $ |      | 
                
                

    [Routine FLOATCODE C]               [Routine FLOATCODE D]

    [Col #          D ]                 [Col R          J ]       
                                               
    | KIQO | / | FS/K |                 | KIQO | / | //// |                   
    | $DQG | E | WST/ |                 | VJQG | E | FSTF |
    | A#QB | @ | FSTA |                 | ORQB | @ | PR/H |                  
    | ///K | A | /:QO |                 | KSQO | A | KIQO |                  
    | Y:/N | : | PSQG |                 | WR/P | : | $RQG |                  
    | W:TD | S | PSQB |                 | //$$ | S | SU/P |                  
    | WS/U | I | ODQO |                 | A:QG | I | XJQO |                  
    | YS/U | U | PSQG |                 | MR/J | U | YSQG |                  
    | ZIQO | # | S:/O |                 | FS/J | # | LSQB |                  
    | SSUP | D | DSUN |                 | FS/E | D | :ST/ |                  
    | WST/ | R | XDQG |                 | :S/J | R | A:/Q |                  
    | TSTA | J | A:/O |                 | CR/H | J | WSTA |
    | YST/ | N | II/P |                 | W:QG | N | TSTF |
    | LSTA | F | /:UN |                 | DS/O | F | TSTA |
    | II/P | C | IS/C |                 | SRQG | C | WSTF |
    | TS/K | K | FS/F |                 | E:/O | K | "J/H |
    | WS/F | T | FS/E |                 | /:QO | T | WSTK |
    | ISTI | Z | IS/N |                 | W:QG | Z | WSTA |                  
    | WS/A | L | ID/H |                 | KSQB | L | WS/K |                  
    | LST/ | W | KSQO |                 | URT: | W | TS/D |
    | YSTC | H | VDQG |                 | H:/C | H | TS/J |
    | YSTA | Y | KSQB |                 | FS/N | Y | ISTI |
    | ESUP | P | FST# |                 | GD/I | P | TS/A |
    | WSTF | Q | A:/Q |                 | KS/N | Q | TJT: |
    | WSTA | O | IE// |                 | //TA | O | WS/D |
    | WS/K | B | FSTK |                 | VRQO | B | ISTI |
    | KSQO | G | E:TD |                 | KSQB | G | II/H |
    | YSQG | " | FSTA |                 | II/P | " | RJ/I |
    | U:/O | M | II/P |                 | //// | M | QJ/P |
    | PSQB | X | GV$$ |                 | ///O | X | I/// |
    | YSQO | V | V$$$ |                 | /T$$ | V | //TM |
    | KSQB | $ | ///Z |                 | //#/ | $ | //// |
                                        

    [End]    

   Note by Roberto Sancho: address GS was changed from //// to "///
   This is the entry point on FLOATCODE RCS. This address is expected by
   B.INPUT when processing G warning character. original Doc describes this 
   behaviour even if the listing does not reflects it (so I added the 
   missing address)

   Important locations

   [FS, CS]=F(A) mantissa (signed fraction)
   [KS]    =F(A) exponent (signed int)

   [TS, ZS]=F(R) mantissa (signed fraction)
   [LS]    =F(R) exponent (signed int)

   [KI]=addr referenced into current FLOATCODE instruction
   [ZI]=current FLOATCODE instruction
   [HU]=Floatcode 'control' (=program counter)
                           
   Representation of float numbers in storage
   
   MMMM MMEE   -> 40 bit value MMMMMM = mantissa (1/2 > |a| >= 1/4)
                               EE     = exponent (256 > p > -256)
   
   Some float constants 

   //// /#:/   ->  1.0   (mantissa=//// /#, exp= :/ =0 0100 00000)
   //// /#I/   ->  2.0                     (exp= I/ =0 1100 00000)
   //// /##/   ->  4.0                     (exp= #/ =0 0010 00000)
   //// /#R/   ->  8.0                     (exp= R/ =0 1010 00000)
   //// /RR/   -> 10.0   (mantissa=//// /R, exp= R/ =0 1010 00000)
   .... ./VK   -> 10^76  maxint        (max exp= VK =0 1111 11110)   
   .... ./ET   -> 10^-87 minint        (min exp= ET =1 0000 00001)   

   QNNS KCTT   -> 1x10^(-75) -> near zero
   MQVA LNI/   -> 3.141592 -> PI

   machine code 

   AU/P -> closure (return from subroutine)
   II/P -> return to 'control' -> go exec next floatcode instr
   SU/P -> goto floatcode instr at [B7]+1

   ss    ss$O     call intercode subroutine from  
   ss+1  EI/P     machine code. On subroutine termination
   ss+2  nn/V     machine code program execution continues
   ss+3           at address ss+3

   floatcode instructions

   nn b W    F(A)=F(A)+F(nn)       Add float operand to float accumulator
   nn b H    F(A)=F(A)-F(nn)       Substract float operand from float accumulator
   nn b Y    F(n)=F(A), F(A)=0     Store float accumulator, then clear it (well set F(A) exponent to -256)
   /  / P    dummy instruction
   nn b Q    F(A)=F(A)+F(R)*F(nn)  Add product 
   nn b O    F(A)=F(A)-F(R)*F(nn)  Substract product 
   nn b B    F(R)=1/F(nn)          Set multiplier as inverse of float operand 
   nn b G    F(R)=F(nn)            Set multiplier 
   nn / "    goto floatcode inst at addr nn+1
   nn / M    if F(A)>0 goto floatcode inst at addr nn+1, else F(A)=-F(A)
   nn / X    call as adroutine cue at dir entry nn
   nn / V    call as subroutine cue at dir entry nn
   nn / $    goto machinecode instr at addr nn+1
   /U / $    closure (return from FLOATCODE subroutine) 
   
   Annotated disassembly

   160  /S  ////  
   161  ES  ///@  
   162  @S  ////  
   163  AS  ///:  
   164  :S  ////  
   165  SS  ///#  
   166  IS  ////  
   167  US  ///T  
   168  #S  ////  
   169  DS  $$$$  
   170  RS  $$$$  
   171  JS  ////  
   172  NS  /U//  
   173  FS  ////  
   174  CS  ///#  
   175  KS  /T$$  
   176  TS  ////  
   177  ZS  ////  
   178  LS  ////  
   179  WS  ////  
   180  HS  ////  
   181  YS  ////  
   182  PS  ////  
   183  QS  /I//  
   184  OS  O#//  w floatcode instr jumptable 1
   185  BS  P#//  h
   186  GS  "I//  
   187  "S  ////  
   188  MS  C#//  q
   189  XS  C#//  o
   190  VS  SJ//  b
   191  $S  D#//  g
   192  /I  $$VO   
   193  EI  /IQG  
   194  @I  SU/P  
   195  AI  $U//  w floatcode instr jumptable 1
   196  :I  $U//  h
   197  SI  $D//  y
   198  II  AU//  addr for P instr (dummy): goto 228
   199  UI  $U//  q
   200  #I  $U//  o
   201  DI  $U//  b
   202  RI  $U//  g
   203  JI  @J//  "
   204  NI  /J//  m
   205  FI  GI//  addr for X instr (call subroutine): goto 219
   206  CI  "I//  addr for V instr (call subroutine): goto 220
   207  KI  ////  
   208  TI  //I/  
   209  ZI  ////  
   210  LI  @E//  Read dir from Track 34L to S0
   211  WI  ////  
   212  HI  ////  
   213  YI  ////  
   214  PI  ////  
   215  QI  ////  
   216  OI  $$$/  
   217  BI  M$$$  
   218  GI  $///  
   219  "I  A:ZG  B1=B1-2
   220  MI  LI/:  Entry for subroutine: read dir in S0
   221  XI  KIQO  B7=[KI]=addr of FC instr = routine entru in dir
   222  VI  //Q/  AL=[B7]
   223  $I  LUZA  
   224  /U  BIZG  B1=B1+4
   225  EU  A:ZG  B1=B1-2
   226  @U  ZUE:  Read routine
   227  AU  /U/G  Dummy stop /G

                  Exec next FLOATCODE instr
   228  :U  TUZT  B7=[HU] restore FLOATCODE control in B7
   229  SU  IU/L  Dummy Stop /L
   230  IU  TUET  if (B>=0) goto XXX
   231  UU  DSQG  incr B7 (B7=FLOATCODE interpretive program counter, called 'control' in original doc)
   232  #U  TUZZ  [HU]=B7. Save FLOATCODE 'control'
   233  DU  //KC  D=[B7] read FLOATCODE instruction
   234  RU  //$/  AL=[B7] read FLOATCODE instruction
   235  JU  OITR  A=A AND $$$/ ....
   236  NU  KITA  [KI]=AL= address of FC intruction
   237  FU  J:/N  shift AL 1 char right
   238  CU  BITR  AND AL -> AL=/nnb o///   where nn=addr, b=index, o=floatcode intructions
   239  KU  TITA  Save AL in [TI, ZI] 
   240  TU  ZIQO  B7=[ZI] -> load FLOATCODE instruction in B7
   241  ZU  TSUP  goto [TS + B7] instr jumptable 1
                  instr B7=V -> call subroutine -> goto 220
                           X -> goto adroutine -> goto 219
                           P -> dummy instruction -> goto 228
                           $ -> goto addr nn+1 (to exec ferranti machine code)
                           Y -> store and clear F(A) -> goto 320
                           M -> if F(A)>0 goto FC nn+1, else F(A)=-F(A) -> goto 353
                           " -> goto FC nn+1 -> goto 355
                           WH QO BG -> arithmetic operation -> goto 256

   242  LU        
   243  WU        
   244  HU        
   245  YU        
   246  PU        
   247  QU        
   248  OU        
   249  BU        
   250  GU        
   251  "U        
   252  MU        
   253  XU        
   254  VU        
   255  $U        


                  Floatcode arithmetic operations WH QO BG 

                  get operand value
   256  /#  KIQO  B7=addr into the FC instr (from [KI])
   257  E#  $DQG  B7=B7 + ///K
   258  @#  A#QB  set instr to get FC operand value
   259  A#  ///K  get D=operand value                         //// /#I/ = 2

                  unpack operand  
   260  :#  Y:/N  Shift -> AL=mantissa, AM=exponent           //// ///#  I/// //// 
   261  S#  W:TD  OR mantissa /T// ///                        /T// ///#  I/// ////   
   262  I#  WS/U  [WS]=mantissa
   263  U#  YS/U  [YS]=exponent, A=0

   264  ##  ZIQO  B7=[ZI]=FC intruction
   265  D#  SSUP  goto [SS + B7] instr jumptable 2
                  instr B7=W  -> Add to F(A) -> goto 281
                           H  -> Sub from F(A) -> goto 279
                           G  -> load in F(R) -> goto 266
                           B  -> load inverse in F(R) -> goto 358
                           QO -> mult -> goto 271

                  G floatcode instruction -> F(R)=operand

   266  R#  WST/  A=[WS]=Operand mantissa         
   267  J#  TSTA  [TS]=A=F(R) mantissa
   268  N#  YST/  A=[YS]=Operando exponent
   269  F#  LSTA  [LS]=A=F(R) exponent
   270  C#  II/P  goto 228 to exec next FC instr 

                  Q/O floatcode instruction -> F(A)=F(A) +/- F(R)*operand

   271  K#  TS/K  D=F(R) mantissa
   272  T#  WS/F  A=D*[WS]=F(R) mantissa * operand mantissa
   273  Z#  ISTI  add //// ///T 
   274  L#  WS/A  operand mantissa=AM
   275  W#  LST/  A=[LS]=F(R) exponent
   276  H#  YSTC  add [YS]=operand exponent
   277  Y#  YSTA  save back to [YS]=operand exponent
   278  P#  ESUP  if (B7=23 Q///) goto 281 -> add product to acc
                  if (B7=24 O///) goto 279 -> subst product from acc

                  H floatcode instruction -> substraction

   279  Q#  WSTF  A=-[WS]=-Operand mantissa         
   280  O#  WSTA  [WS]=A -> change sign of operando, proceed to addition

                  W floatcode instruction -> addition                  

   281  B#  WS/K  D=[WS]=Operand mantissa                                    	/T// ///#, A=0
                  check operand exp vs F(A) exp 
   282  G#  KSQO  B7=[KS]=F(A) exponent (signed int)                     	R///  (=10) -> F(A)=8.0                    
   283  "#  YSQG  B7=B7-[YS] -> B7 = F(A) exponent - operand exponent  		R/// - I/// -> 10-6=4
   284  M#  U:/O  if (B>=0) goto 293 -> goto if F(A) exp >= operand exp

   285  X#  PSQB  [PS]=B7 save B7 to reverse F(A) and operand
   286  V#  YSQO  B7=[YS]=operand exponent  
   287  $#  KSQB  [KS]=B7 -> F(A) exponent = operand exponent (this is the greater exponent)
   288  /D  FS/K  D=[FS]=F(A) mantissa                                 //// ///#
   289  ED  WST/  AL=[WS]=Operand mantissa                             /T// ///#
   290  @D  FSTA  [FS]=AL -> F(A) mantissa=operand                     F(A)=operand mantissa, D=F(A) mantissa
   291  AD  /:QO  B7=0
   292  :D  PSQG  B7=B7-[FS] -> change sign of B7 -> now B7=operand exponent - F(A) exponent and is positive

   293  SD  PSQB  [PS]=B7 -> save exponents difference (is >= 0)		[FS]=4=exp diff
   294  ID  ODQO  B7=38
   295  UD  PSQG  B7=B7-[PS] 
   296  #D  S:/O  if (B>=0) goto 301 -> if (exponents difference < 38) goto 
   297  DD  DSUN  
   298  RD  XDQG  
   299  JD  A:/O  if (B>=0) goto xxx
   300  ND  II/P  goto 228 to exec next FC instr (because exponents difference > 38)

                  here: F(A)=biggest value (bigger exp), D=mantissa to add, B7=exp difference

   301  FD  /:UN  Shift D to scale it down according to B7 -> result in AM	/T// ///# (#=00010) -> /@// ///E (E=10000)
   302  CD  IS/C  D=//// ///T
   303  KD  FS/F  AM=F(A) mantissa + scaled down D operand (2 msb are zero)	AM=/R// ///S (S=10100)
   304  TD  FS/E  save F(A) mantisa
   305  ZD  IS/N  AM=AM + //// ///# (#=00010) -> test: is last non sign ...	
   306  LD  ID/H  if (AM>=0) goto 313            ... digit 1? no -> goto 313
   307  WD  KSQO  
   308  HD  VDQG  
   309  YD  KSQB  
   310  PD  FST#  AL=[FS] 
   311  QD  A:/Q  goto 314
   312  OD  IE//  const 38

                  adjust round-off by making last digit odd
   313  BD  FSTK  A=[FS]*2 -> AL=F(A) mantissa * 2				AL=/H// ///G (G=01011)
   314  GD  E:TD  OR E/// //// to AL to round off
   315  "D  FSTA  [FS]=AL -> F(A)=resulting mantissa
   316  MD  II/P  goto 228 to exec next FC instr 
   317  XD  GV$$  
   318  VD  V$$$  
   319  $D  ///Z 
 
                  Floatcode instruction Y -> store and clear F(A) 

   320  /R  KIQO  B7=[KI] = address where to store float accum F(A)
   321  ER  VJQG  set B7 = addr TA (instr store accum at addr)
   322  @R  ORQB  store instr at [OR] -> the transfer order
   323  AR  KSQO  
   324  :R  WR/P  goto 328
   325  SR  //$$  const -1024
   326  IR  A:QG  Standarize numerical part ...
   327  UR  MR/J  .. adjusting exponent accordingly
   328  #R  FS/J  
   329  DR  FS/E  
   330  RR  :S/J  
   331  JR  CR/H  
   332  NR  W:QG  
   333  FR  DS/O  Dynamic stop if p (corrected) >= 256
   334  CR  SRQG  
   335  KR  E:/O  if p (corrected) < -512, then ... 
   336  TR  /:QO  ... replace p ...
   337  ZR  W:QG  ... by -512
   338  LR  KSQB  
   339  WR  URT:  
   340  HR  H:/C  Packing sequence
   341  YR  FS/N  
   342  PR  GD/I  
   343  QR  KS/N  
   344  OR  //TA  exec Transfer order, clear acc
   345  BR  VRQO  Set exponent ...
   346  GR  KSQB  ... of F(A)=-512
   347  "R  II/P  return to 'control' -> goto 228

   348  MR  ////  const ...
   349  XR  ///O        ... -1/4
   350  VR  /T$$  const ...
   351  $R  //#/        ... -512
   352  /J  ////  

                  Floatcode instruction M -> if F(A)>0 goto FC nn+1, else F(A)=-F(A) 

   353  EJ  FSTF  A=-[FS]=-F(A) mantissa
   354  @J  PR/H  if (A>=0) goto 315 -> goto if F(A) was negative -> just store F(A)=-F(A) and continue to next instr

                  Floatcode instruction " -> goto FC nn+1

   355  AJ  KIQO  B7=[KI]=addr of FC instr
   356  :J  $RQG  make B7 negative so is intrepreted as FC 'control'
   357  SJ  SU/P  goto 231 -> goto FC instr [B7]+1

                  B floatcode instruction -> F(R)=1 / operand

   358  IJ  XJQO  B7=6
   359  UJ  YSQG  B7=B7-[YS] operand exponent
   360  #J  LSQB  [LS] F(R) exponent = B7
   361  DJ  :ST/  A=//// ///#
   362  RJ  A:/Q  goto 365
   363  JJ  WSTA  
   364  NJ  TSTF  
   365  FJ  TSTA  [TS] F(R) mantissa=AL, A=0
   366  CJ  WSTF  A=-[WS]=-Operand mantissa         
   367  KJ  "J/H  if (A>=0) goto xxx goto if operand is neg
   368  TJ  WSTK  
   369  ZJ  WSTA  
   370  LJ  WS/K  
   371  WJ  TS/D  
   372  HJ  TS/J  
   373  YJ  ISTI  
   374  PJ  TS/A  
   375  QJ  TJT:  
   376  OJ  WS/D  
   377  BJ  ISTI  
   378  GJ  II/H  
   379  "J  RJ/I  
   380  MJ  QJ/P  
   381  XJ  I///  const 6
   382  VJ  //TM  const - (//TA)
   383  $J  ////  
                  
