home *** CD-ROM | disk | FTP | other *** search
/ Photo CD Demo 1 / Demo.bin / gems / gemsiii / cyclic.c < prev    next >
Text File  |  1992-04-01  |  3KB  |  99 lines

  1. /*
  2.  * Graphics Gems III: Fast Generation of Cyclic Sequences
  3.  *
  4.  * programmed by Alan Paeth (awpaeth@alumni.caltech.edu)
  5.  *
  6.  * These functions are implemented as macros which fall into two classes:
  7.  *
  8.  *   sequences -- generate a cyclic set of N arbitrary values
  9.  *   triggers  -- generate a cyclic set of N boolean test conditions.
  10.  *
  11.  * Macros come as a set. The first initializes (declarative), the second steps.
  12.  * These are paired for  VARIABLE / BOOLEAN  triggering use.
  13.  * The first are called  "sequN" such as "getch()" and
  14.  * will not introduce side-effects. A third macro named "testM" provides
  15.  * boolean triggering expressions without altering the current step. Its value
  16.  * is "true" after step "i" if the ith character in string M is non-zero.
  17.  *
  18.  * Note: these macros have been tuned for performance and internal consistancy.
  19.  * Unlike the book exposition, a cycle's current value is undefined until
  20.  * *after* the first call to "cyclN" or "stepN". This style facilitates fast
  21.  * testing as in "do { } while(condition)" loops at the expense of additional
  22.  * first-use overhead, total amortized loop time remaining unchanged. The code
  23.  * takes advantage of both register variables and the C "comma" operator.
  24.  * Loops may be cycled symbolically, thereby reordering the initialization or
  25.  * step macros to take advantage of specific compiler or problem optimizations.
  26.  */
  27.  
  28. /*
  29.  * Sequences
  30.  *
  31.  * The macro "sequN(parm1,...,parmN) initializes a sequence N values;
  32.  * the macro "cyclN()" advances to (and returns in "t1") the next cyclic value.
  33.  */
  34.  
  35. /* cycle: [a, b]  (eqn 2.2) */
  36.  
  37. #define sequ2(a,b) register int t1, t2; t1 = (int)(b); t2 = t1^(int)(a)
  38. #define cycl2() (t1 ^= t2)
  39.  
  40. /* cycle: [a, b, c] (eqn 3.3) */
  41.  
  42. #define sequ3(a,b,c) register int t1, t2, t3; \
  43.   t1 = (int)(c); t2 = t1^(int)(b); t3 = t2^(int)(a)
  44. #define cycl3() (t2 ^= (t1 ^ t3), t1 ^= t2)
  45.  
  46.  
  47. /* cycle: [a, b, c, d, e, f]  (eqn 6.2) 
  48.  *
  49.  * register t1 may be removed if value is not kept; t4 provides only an offset
  50.  * a second table may be easily added to the LHS of the innermost subexpression 
  51.  * values may be cast as ints or the macro rewritten to support floats, etc.  
  52.  */
  53.  
  54. #define sequ6(a,b,c,d,e,f) register int t1,t2,t3,t4[7],*t5;t2=t3=1;t5= &t4[3];\
  55.    t4[1]=(a); t4[0]=(b); t4[2]=(c); t4[5]=(d); t4[6]=(e); t4[4]=(f)
  56. #define cycl6() (t1=t5[(t2 += t3, t3 += (~t2))])
  57.  
  58. /* cycle: [1, 2, 3]   (eqn 3.3b, end) */
  59.  
  60. #define sequ123() register int t1, t2; t1 = 3; t2 = 1
  61. #define cycl123() (t2 ^= t1, t1 ^= t2)
  62.  
  63.  
  64. /*
  65.  * Triggers
  66.  *
  67.  * The macro "trigN() initializes a boolean cycle of N states.
  68.  * the macro "stepN()" advances the state
  69.  * the macro "testM" is a true expression at step i if M's ith char is '1'.
  70.  */
  71.  
  72. /* trigger mod 2 (eqn 2.1) */
  73.  
  74. #define trig2() register int t1 = 0
  75. #define step2() (t1 = !t1)
  76. #define test01() (!t1)
  77. #define test10() (t1)
  78.  
  79. /* trigger mod 3 (eqn 3.2) */
  80.  
  81. #define trig3() register int t1, t2; t1 = t2 = 1
  82. #define step3() (t1 ^= t2, t2 ^= t1)
  83. #define test100() (!t1)
  84. #define test010() (!t2)
  85. #define test001() (t1 == t2)
  86. #define test110() (t1 != t2)
  87.  
  88. /* trigger mod 6 (eqn 6.3) */
  89.  
  90. #define trig6() register int t1, t2; t1 = -1; t2 = 1
  91. #define step6() (t1 += t2, t2 += ~t1)
  92. #define test100000() ((t1 == 0) && (t2 == 0))
  93. #define test100100() (!t2)
  94. #define test101000() (t1 == t2)
  95. #define test110000() (!t1)
  96. #define test110100() ((t1 == 0) || (t2 == 0))
  97. #define test010111() (t1 != t2)
  98. #define test011111() ((t1 != 0) || (t2 != 0))
  99.