#include #include /* the original */ int syracuse1(int num) { int count ; count = 0 ; while (num != 1) { if (num%2==0) { num = num/2 ; } else { num = 3*num + 1 ; } ++count ; } return count ; } /* by the rules */ int syracuse2(int num) { int count, _d1, _d2, _d3, _d4 ; count = 0 ; sloop: _d1 = num != 1 ; if (_d1 == 0) goto eloop ; _d2 = num & 1 ; _d3 = _d2 == 0 ; if (_d3 == 0) goto elslab ; num = num >> 1 ; goto jnlab ; elslab : _d4 = 3*num ; num = _d4 + 1 ; jnlab: ++count ; goto sloop ; eloop: return count ; } /* no multiplication! */ int syracuse3(int num) { int count, _d1, _d2, _d3, _d4, _d5 ; count = 0 ; sloop: _d1 = num != 1 ; if (_d1 == 0) goto eloop ; _d2 = num & 1 ; _d3 = _d2 == 0 ; if (_d3 == 0) goto elslab ; num = num >> 1 ; goto jnlab ; elslab : _d4 = num+num ; _d5 = _d4+num ; num = _d5 + 1 ; jnlab: ++count ; goto sloop ; eloop: return count ; } /* Taking advantage of Z bits and WREG */ int syracuse4(int num) { int count, WREG ; count = 0 ; sloop: if ((num - 1) == 0) goto eloop ; if ((num & 1) != 0) goto elslab ; num = num >> 1; goto jnlab ; elslab : WREG = num + num ; WREG = num + WREG ; num = 1 + WREG ; jnlab: ++count ; goto sloop ; eloop: return count ; } /* the original -- with odd cases unrolled */ int syracuse5(int num) { int count ; count = 0 ; while (num != 1) { if ((num & 1) == 0) { num = num >> 1 ; ++count ; } else { num = (3*num + 1)/2 ; count += 2 ; } } return count ; } /* -- with shift replacing multiplication */ int syracuse6(int num) { int count ; count = 0 ; while (num != 1) { int halfnum = num >> 1 ; int oddnum = num & 1 ; if (oddnum == 0) { num = halfnum ; ++count ; } else { num = num + halfnum + 1 ; count += 2 ; } } return count ; } /* -- more PICish */ int syracuse7(int num) { int count, halfnum, oddnum ; count = 0 ; sloop: halfnum = num >> 1 ; oddnum = num & 1 ; if (halfnum == 0) goto eloop ; if (oddnum != 0) goto elslab ; num = halfnum ; ++count ; goto jnlab ; elslab: num = num + halfnum + 1 ; count += 2 ; jnlab : goto sloop ; eloop: return count ; } /* -- even more PICish */ int syracuse8(int num) { int count, WREG, Cbit ; count = 0 ; sloop: WREG = num >> 1 ; /* use lsr to */ Cbit = num & 1 ; /* set WREG and Cbit */ if (WREG == 0) goto eloop ; if (Cbit != 0) goto elslab ; num = WREG ; ++count ; goto jnlab ; elslab: num = num + WREG + Cbit ; /* use addc */ count += 2 ; jnlab : goto sloop ; eloop: return count ; } /* Like my PIC solution */ int syracuse9(int num) { int count, WREG, Cbit ; count = 0 ; goto tstloop ; xodd: num = num + WREG + Cbit ; /* use addc */ count += 2 ; goto tstloop ; xeven: num = WREG ; ++count ; tstloop: WREG = num >> 1 ; /* use lsr to */ Cbit = num & 1 ; /* set WREG and Cbit */ if (Cbit == 0) goto xeven ; if (WREG != 0) goto xodd ; return count ; } int main(int argc, char **argv) { int n = 123456789 ; printf("%d\n", syracuse1(n)) ; printf("%d\n", syracuse2(n)) ; printf("%d\n", syracuse3(n)) ; printf("%d\n", syracuse4(n)) ; printf("%d\n", syracuse5(n)) ; printf("%d\n", syracuse6(n)) ; printf("%d\n", syracuse7(n)) ; printf("%d\n", syracuse8(n)) ; printf("%d\n", syracuse9(n)) ; return (EXIT_SUCCESS) ; }