home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #19 / NN_1992_19.iso / spool / comp / theory / cellaut / 365 < prev    next >
Encoding:
Text File  |  1992-09-03  |  5.6 KB  |  129 lines

  1. Newsgroups: comp.theory.cell-automata
  2. Path: sparky!uunet!mcsun!ieunet!tcdcs!maths.tcd.ie!chughes
  3. From: chughes@maths.tcd.ie (Conrad Hughes)
  4. Subject: Fast parallel Life implementation
  5. Message-ID: <1992Sep3.122215.1746@maths.tcd.ie>
  6. Keywords: real mode life game parallel
  7. Organization: Dept. of Maths, Trinity College, Dublin, Ireland.
  8. References: <a_#n=la.pdh@netcom.com> <1992Aug27.102124.5016@maths.tcd.ie>
  9. Date: Thu, 3 Sep 1992 12:22:15 GMT
  10. Lines: 117
  11.  
  12.  
  13.   Here's my fast parallel life engine - I'm only including the core
  14. bit to calculate the new state of a group of thirty-two cells when
  15. given the state of that group and their eight sets of thirty-two
  16. neighbours.
  17.  
  18.   Plugging it into some other life program should be quite trivial.
  19. I include my original ARM2 assembler core and untested C source
  20. derived from it - I would add the caveat that I no longer have
  21. documentation for the standard rules of Conway's life, and consequently
  22. the final calculation may not be accurate - this doesn't seem to
  23. be the case however, as I get very "life-like" (sorry) behaviour
  24. from the program..
  25.  
  26.   If anybody has further questions about this, please contact me -
  27. I'll be glad (if somewhat slow) to help..  No guarantee, express or
  28. implied, &c. &c.
  29.  
  30.   If you feel like using this in something, I'd be interested to
  31. hear from you (and I'd appreciate a mention in the credits :-).
  32.  
  33.   Sorry for the delay in posting,
  34.     Conrad
  35.  
  36. -- ARM assembly version
  37. \ On entry:
  38. \   registers 0, 1, 2, 3, 5, 6, 7 & 8 each contain a set of thirty-two
  39. \   neighbours, in any grouping - i.e. R0 could be the up-left
  40. \   neighbours or the right neighbours, it doesn't matter.
  41. \   register 4 contains the current state of the cells to be processed.
  42. \ On exit:
  43. \   register 0 contains the new state of the processed group of cells.
  44. \ When I say 'contains', I mean that if bit 0 is set, the leftmost cell
  45. \   is alive, otherwise the leftmost cell is dead, bit 1 indicates life
  46. \   or death for the second cell from the left, and so on.  Big-endian
  47. \   systems may prefer a reverse order, it doesn't matter.
  48. \ These instructions operate in the following way:
  49. \   OP <reg0>, <reg1>, <reg2> means reg0 = reg1 OP reg 2
  50. \ where AND, ORR and EOR are bitwise and, or and exclusive-or operations,
  51. \ BIC is bit-clear, or 'AND NOT'.
  52. AND9,0,1:EOR0,0,1
  53. AND1,0,2:EOR0,0,2:EOR9,9,1
  54. AND1,0,3:EOR0,0,3:AND3,9,1:EOR9,9,1
  55. AND1,0,5:EOR0,0,5:AND5,9,1:EOR9,9,1:EOR3,3,5
  56. AND1,0,6:EOR0,0,6:AND6,9,1:EOR9,9,1:EOR3,3,6
  57. AND1,0,7:EOR0,0,7:AND7,9,1:EOR9,9,1:EOR3,3,7
  58. AND1,0,8:EOR0,0,8:AND8,9,1:EOR9,9,1:EOR3,3,8
  59. ORR0,0,4:AND0,0,9:BIC0,0,3
  60.  
  61. -- C version -- UNTESTED, verify correct behaviour before you trust it!
  62. /* LT should be a 32-bit int type, but for smaller or larger machines 
  63.  * could easily be 8, 16, 64 bits - whatever you like, really.
  64.  * cs is the group of cells to be worked on, and n1-n8 are the groups of
  65.  * neighbours.  If you stored your cell states in an array of 32-bit
  66.  * integers, you could process those in c[x][y] with the following
  67.  * instruction (I think):
  68.  * d[x][y] = nextgen(c[x][y], (c[x-1][y-1] << 31) | (c[x][y-1] >> 1),
  69.  *                   c[x][y-1], (c[x][y-1] << 1) | (c[x+1][y-1] >> 31),
  70.  *                   (c[x-1][y] << 31) | (c[x][y] >> 1),
  71.  *                   (c[x][y] << 1) | (c[x+1][y] >> 31),
  72.  *                   (c[x-1][y+1] << 31) | (c[x][y+1] >> 1), c[x][y+1],
  73.  *                   (c[x][y+1] << 1) | (c[x+1][y+1] >> 31));
  74.  * [the array must be unsigned for the right shift >> to work]
  75.  * Obviously, you can't store the new states back into c[][] without
  76.  * some kind of buffering, so for simplicity I'm just putting them
  77.  * into d[][] instead.
  78.  */
  79.  
  80. LT nextgen(LT cs, LT n1, LT n2, LT n3, LT n4, LT n5, LT n6, LT n7, LT n8)
  81.   {
  82.   LT b0, b1, b2, b3, c0, c1;
  83.  
  84. /* Adding the first two neighbours, max result is 2, needs two bits */
  85.   b1 = n1 & n2; /* Carry flags from adding n2 to n1 go into bit 1 */
  86.   b0 = n1 ^ n2; /* Update bit 0 */
  87.  
  88. /* Adding the third neighbour, max result is 3, still need only two bits */
  89.   c0 = b0 & n3; /* Carry flags from adding n3 to bit 0 */
  90.   b0 ^= n3;     /* Update bit 0 */
  91.   b1 ^= c0;     /* No overflow from bit 1 is possible yet, so just add carry */
  92.  
  93. /* Adding the fourth neighbour, we need three bits now */
  94.   c0 = b0 & n4; /* Carry flags, same as before */
  95.   b0 ^= n4;     /* Update bit 0 */
  96.   b2 = b1 & c0; /* Carry flags from adding carry to bit 1 go into bit 2 */
  97.   b1 ^= c0;     /* Update bit 1 */
  98.  
  99. /* Adding fifth neighbour, working with three bits */
  100.   c0 = b0 & n5; /* Carry flags */
  101.   b0 ^= n5;
  102.   c1 = b1 & c0; /* Carry flags from bit 1 */
  103.   b1 ^= c0;     /* Add carry to bit 1 */
  104.   b2 ^= c1;     /* Add carry flags to bit 2 */
  105.  
  106. /* Adding sixth and seventh neighbours is much the same as adding the fifth */
  107.   c0 = b0 & n6; b0 ^= n6; c1 = b1 & c0; b1 ^= c0; b2 ^= c1;
  108.   c0 = b0 & n7; b0 ^= n7; c1 = b1 & c0; b1 ^= c0; b2 ^= c1;
  109.  
  110. /* Adding the eighth neighbour could result in carry, but bit 3 is not
  111.  * necessary for results - when it is set, all other bits are clear, and
  112.  * consequently it becomes unnecessary in the application of the rules
  113.  */
  114.   c0 = b0 & n8; b0 ^= n8; c1 = b1 & c0; b1 ^= c0; b2 ^= c1;
  115.  
  116. /* Dead cells come to life if they have three neighbours alive,
  117.  *  i.e. bits 0 and 1 are set, bit 2 is clear.
  118.  * Live cells die unless they have two or three neighbours alive,
  119.  *  i.e. bit 1 is set, bit 2 is clear.
  120.  * These two combine in the following expression:
  121.  */
  122.   return((b0 | cs) & b1 & (-1 ^ b2));
  123. }
  124. -- 
  125. ,--------------------. Suicide City, Paranoid State. Geddout! trip to, heave'n'
  126. |chughes@maths.tcd.ie| ho, up, down, to'n'fro you have no word please leave us
  127. +-=>Conrad  Hughes<=-+ here close our eyes to the octopus ride isn't it good to
  128. `-------<SICK>-------' be lost in the wood isn't it bad so quiet there . . .
  129.