1
0
mirror of https://github.com/PDP-10/its.git synced 2026-01-13 15:27:28 +00:00

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.
This commit is contained in:
Adam Sampson 2018-04-10 22:14:17 +01:00 committed by Adam Sampson
parent 5e908804ce
commit d9169dd71c

View File

@ -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 <https://oeis.org/A007310> 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 <https://oeis.org/A007310>.)
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