Collatz: Uses conditional JMP within REP block
tomcrawford
Posts: 1,126
in Propeller 2
Here is a link to the wikipedia article about the Collatz conjecture in case anybody needs a refresher. And here is link to a forum thread where we pretty much beat it to death.
So here is prop2 code that does a very simple single precision implementation.
So here is prop2 code that does a very simple single precision implementation.
rep @.mumble, #0 'repeat the following until it gets to one cmp current, #1 wcz 'did it get to one? if_E jmp #gotToOne 'okay, display and continue drvnot #scope2 test current, #1 wcz 'even or odd if_Z shr current, #1 'divide by two if even if_NZ mov temp, current 'multiply by three if odd if_NZ add current, temp if_NZ add current, temp if_NZ add current, #1 'and add one if odd add count, #1 'increment count cmp current, extent wcz 'largest current for this trial if_GT mov extent, current .mumbleThis is interesting only because I used a REP block that includes a conditional JMP to break out of the REP, when the current number gets to 1.
Comments
To me, this is interesting only because the code can be smaller and faster
That's a clever use of addx! This is even faster, by taking advantage of the fact that 3n+1 is always even:
Jonathan
That bit-field was put aside to make room for other features in the instruction set of Prop2. I think one of the biggies was AUGx prefix instructions.