From d9169dd71cb9efdda7d7c480ec0993f087a4f44f Mon Sep 17 00:00:00 2001 From: Adam Sampson Date: Tue, 10 Apr 2018 22:14:17 +0100 Subject: [PATCH] Fix the inner loop when run from 100. FACTOR 26 modified the inner loop to run from 100 rather than from the ACs, with a comment indicating that this was faster on the KL10. But it didn't have the intended effect; the code modifies itself using TRC and HRRI, which only have a 4-bit AC field, so the inner loop's step size wasn't being changed. The factoring algorithm still worked by chance, because the step size is initialised to 2 and the TRC/HRRI harmlessly changed A instead, but it had to consider every odd number as a possible factor. When the code isn't running from the ACs, there are plenty of spare ACs, so keep the step size in AC 7 instead for the KL version. --- src/rz/{factor.27 => factor.28} | 24 +++++++++++++++--------- 1 file changed, 15 insertions(+), 9 deletions(-) rename src/rz/{factor.27 => factor.28} (90%) diff --git a/src/rz/factor.27 b/src/rz/factor.28 similarity index 90% rename from src/rz/factor.27 rename to src/rz/factor.28 index e5837d5e..5b43c9e7 100644 --- a/src/rz/factor.27 +++ b/src/rz/factor.28 @@ -20,15 +20,20 @@ INFOC==3 PDLL==100 +;The inner loop of the trial division code. + +;On pre-KL10 machines, it's faster to run from the ACs. IFE KL10, LOC 7 -;This is the inner loop of the trial division code. +;On the KL10, we can't TRC/HRRI the ADDI at 101, but we have a free AC instead. +IFN KL10, INCR=11 + ;CD is the number being tested. DV is the candidate factor. -;The TRC/ADDI self-modifying code means that it alternates between -;adding 2 and 4 to DV on each iteration, so the factors it tries are -;5, 7, 11, 13, 17, 19, 23, 25... i.e. it skips multiples of 2 and 3. -;(See for more about this sequence.) -TEST: TRC INCR,6 ;Complement the 2 and 4 bits in ADDI below -INCR: ADDI DV,2 ;Next candidate; modified by TEST/NO3 +;The TRC means that it alternates between adding 2 and 4 to DV on each +;iteration, so the factors it tries are 5, 7, 11, 13, 17, 19, 23, 25... +;i.e. it skips multiples of 2 and 3. (See .) +TEST: TRC INCR,6 ;Complement the 2 and 4 bits in the step size +IFE KL10,INCR: ADDI DV,2 ;Next candidate; modified by TEST/NO3 +IFN KL10, ADD DV,INCR ;Next candidate RETEST: MOVE Z,C MOVE B,D IDIV Z,DV ;Z := Z div DV; A := Z mod DV @@ -36,11 +41,12 @@ RETEST: MOVE Z,C CAMLE DV,A JUMPE Z,PRIME ;No more factors to try, so CD is prime JUMPN B,TEST ;Not divisible by E; try the next candidate -;That's the end of the registers; the instruction below is at 20. +;If we started at 7, the instruction below is at 20 (after the ACs). JRST FOUND ;Found a factor (but CD may still be composite) -;Entry point IFE KL10, LOC 100 + +;Entry point GO: MOVE P,[-PDLL,,PDL] .OPEN TYIC,[.UAI,,'TTY] JRST KILL