home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #26 / NN_1992_26.iso / spool / comp / arch / 10409 < prev    next >
Encoding:
Internet Message Format  |  1992-11-04  |  4.5 KB

  1. Xref: sparky comp.arch:10409 comp.lang.misc:3492
  2. Newsgroups: comp.arch,comp.lang.misc
  3. Path: sparky!uunet!haven.umd.edu!darwin.sura.net!spool.mu.edu!news.nd.edu!mentor.cc.purdue.edu!pop.stat.purdue.edu!hrubin
  4. From: hrubin@pop.stat.purdue.edu (Herman Rubin)
  5. Subject: Re: A challenge to the anti-goto
  6. Message-ID: <Bx8x1y.C2@mentor.cc.purdue.edu>
  7. Sender: news@mentor.cc.purdue.edu (USENET News)
  8. Organization: Purdue University Statistics Department
  9. References: <Bwznzx.Dr1@mentor.cc.purdue.edu> <Bx0BuG.s4@mentor.cc.purdue.edu> <1992Nov5.111943.21554@harlqn.co.uk>
  10. Date: Thu, 5 Nov 1992 13:49:09 GMT
  11. Lines: 162
  12.  
  13. In article <1992Nov5.111943.21554@harlqn.co.uk> daveb@harlqn.co.uk (Dave Berry) writes:
  14. >It's easy to get rid of these gotos.  Every goto can be replaced by a
  15. >tail-recursive function call.  This can be compiled into the same machine
  16. >code.  Here is Herman's example, gratuitously rewritten in ML.
  17.  
  18. >(Note: I decided not to make the program functional, for clarity.  Also
  19. >for clarity I left out all occurrences of the dereference operator.  I
  20. >don't know if an existing ML compiler would produce code as efficient as
  21. >the original C - they aren't usually optimised for this sort of work.
  22. >But that's not the point - you could write this in C if your compiler
  23. >optimises tail-recursion.)
  24.  
  25. This is only part of the challenge.  The other part of the challenge is
  26. to do this in such a way that the resulting code will be at least nearly
  27. as efficient.  In order to accomplish this, one thing  necessary is that
  28. every one of the function calls be a totally-non-context switching function,
  29. with nothing added to the stack.  Now it matters not to me what you call
  30. such a beast, BUT IT IS A GOTO.
  31.  
  32. Another explicit inefficiency is that dropthroughs do not seem to occur.
  33. add() should dropthrough to start(), and the numbered labels should drop-
  34. through to the next lower number.  These are essentially costless 
  35. efficiencies in the object code.
  36.  
  37. I have never seen ML code before.  If the above features could be added,
  38. I would have no problem with it.  But, as I have pointed out, these
  39. features are equivalent to reordering code, gotos, and dropthroughs,
  40. and they do not seem to be provided in the language.  Would the compiler
  41. do it?  If so, it meets the challenge for this code.
  42.  
  43. >(Another note: In both ML and C it would be possible (and possibly
  44. >advisable) to use other features of the language to structure some
  45. >parts of this program.)
  46.  
  47.  
  48. >let
  49. >  val n = ref 0
  50. >  val m = ref 0
  51. >  val b = ref B
  52.  
  53. >  fun start() =
  54. >  ( g := ...;
  55. >    body()
  56. >  )
  57.  
  58. >  fun body() =
  59. >    case g of
  60. >      1 => add()
  61. >    | 2 => ()
  62. >    | 3 => start()
  63. >    | 4 => label2()
  64. >    | 5 => start()
  65. >    | 6 => start()
  66. >    | 7 => (g4 := 1; label3())
  67. >    | 8 => ( h := ...;
  68. >             g4 := h>>1;
  69. >             if h&1 then
  70. >             ( g4+=2;
  71. >           label3()
  72. >             )
  73. >             else
  74. >               label4()
  75. >           )
  76. >    | _ => if g > 8 andalso g <= 12 then
  77. >             start()
  78. >           else
  79. >           ( g16 := (g-9)>>4;
  80. >             g &= 3;
  81. >             if g=1 then
  82. >           label5()
  83. >             else if g=2 orelse g=3 then
  84. >           start()
  85. >             else
  86. >             ( h := ...;
  87. >           g4 := (h+1)>>1;
  88. >           if h&1 then
  89. >           ( j := 6;
  90. >             labelh()
  91. >           ) 
  92. >           else
  93. >           ( j := 7;
  94. >             g := ...;
  95. >             if g mod 3 <> 2 then start() else loop()
  96. >           ) 
  97. >             ) 
  98. >           )
  99.  
  100. >  fun add() = (n += 1; start ())
  101.  
  102. >  fun loop() =
  103. >    if bit then
  104. >      labelh ()
  105. >    else
  106. >    ( j += 1;
  107. >      g := ...;
  108. >      if (2<<(g-1)/j)&1 = 0 then
  109. >    start()
  110. >      else
  111. >    loop ();
  112. >    )
  113.  
  114. >  fun labelh() =
  115. >  ( i := C(j);
  116. >    b >>= 1;
  117. >    if i=j orelse i=0 then
  118. >      labelh()
  119. >    else
  120. >    ( m |= b;
  121. >      case i of
  122. >    1 => ()
  123. >      | 2 => label2()
  124. >      | 3 => label3()
  125. >      | 4 => label4()
  126. >      | _ => label5()
  127. >    )
  128. >  )
  129.  
  130. >  fun label5 () =
  131. >  ( b >>= g16;
  132. >    m |= b;
  133. >    g := ...;
  134. >    if bit then
  135. >      if g&1 then label2() else ()
  136. >    else
  137. >    ( g4 := (g+1)>>1;
  138. >      if g&1 then label3() else label4()
  139. >    )
  140. >  )
  141.  
  142. >  fun label4() =
  143. >  ( g := ...;
  144. >    if g4 < g then
  145. >    ( m |= (b>>g4 | b>>g);
  146. >      ()
  147. >    ) else
  148. >    ( m |= b>>g;
  149. >      if bit then () else (b >>= 1; label3())
  150. >    )
  151. >  )
  152.  
  153. >  fun label3() =
  154. >  ( b >>= g4;
  155. >    m |= b;
  156. >    b <<= 1;
  157. >    label2()
  158. >  )
  159.  
  160. >  fun label2() =
  161. >  ( g = ...;
  162. >    m |= (b>>g);
  163. >    ()
  164. >  )
  165. >in
  166. >  start()
  167. >end;
  168.  
  169.  
  170. -- 
  171. Herman Rubin, Dept. of Statistics, Purdue Univ., West Lafayette IN47907-1399
  172. Phone: (317)494-6054
  173. hrubin@pop.stat.purdue.edu (Internet, bitnet)  
  174. {purdue,pur-ee}!pop.stat!hrubin(UUCP)
  175.