SOURCE VIEWER

prime_core.inc

Original bytes preserved. This is a browser-readable view.

DOWNLOAD ORIGINAL ↓

; ======================================================================
; CS617 CH04 PRIME CORE - BASELINE R001
;
; ISPRIME32
;   input:  $15-$18 unsigned 32-bit n, low word first
;   output: $4 = 1 prime, 0 composite
;   input value restored on return
;
; NEXTPR32
;   input:  $15-$18 unsigned 32-bit n
;   output: $15-$18 smallest representable prime >= n
;           $4 = 1 success, 0 if no such uint32 prime exists
;
; Dependencies:
;   UDIV3216NZ / UDIV32NZ from frozen CS617 Chapter 01
;   P16_ODD absolute little-endian prime table, zero sentinel
;
; Design rule:
;   q,r = divmod(n,p)
;   if q < p -> p*p > n -> prime
;   else if r == 0 -> composite
;   else continue
;
; Bound MUST be tested before zero remainder.  n=p is prime.
; ======================================================================

#LIST

ISPRIME32:
; Keep original n in registers preserved by accepted UDIV32NZ.
        LDM     $24,$15,4

; n < 2 -> composite; n == 2 -> prime.
        SBW     $0,$0
        SBCW    $17,$0
        JR      NZ,IP_EVEN
        LDW     $0,2
        SBCW    $15,$0
        JR      C,IP_COMP
        JR      Z,IP_PRIME

IP_EVEN:
; Every remaining even candidate is composite.
        ANC     $15,$SY
        JR      Z,IP_COMP

; Odd trial divisors begin at 3.
        PRE     IZ,P16_ODD

IP_LOOP:
; Absolute table is little-endian uint16. LDI advances IZ one byte.
        LDI     $5,(IZ+$SX)
        LDI     $6,(IZ+$SX)

; Zero sentinel means the complete required divisor table was exhausted.
        ANCW    $5,$5
        JR      Z,IP_PRIME

; Zero-extend p and restore n for this destructive divmod call.
        SBW     $7,$7
        LDM     $15,$24,4
        CAL     UDIV3216NZ

; q >= 65536 implies q > every table p; do not attempt low-word compare.
        ANCW    $17,$17
        JR      NZ,IP_REM

; q < p is exactly p*p > n for positive p.
        SBCW    $15,$5
        JR      C,IP_PRIME

IP_REM:
; Bound did not fire. A zero remainder now proves compositeness.
        ANCW    $10,$10
        JR      Z,IP_COMP
        JR      IP_LOOP

IP_PRIME:
        LD      $4,1
        LDM     $15,$24,4
        RTN

IP_COMP:
        LD      $4,0
        LDM     $15,$24,4
        RTN

; ----------------------------------------------------------------------
; NEXTPR32 - smallest representable prime >= n.
; ----------------------------------------------------------------------
NEXTPR32:
; 0,1,2 all map to 2.
        SBW     $0,$0
        SBCW    $17,$0
        JR      NZ,NP_NORM
        LDW     $5,2
        SBCW    $15,$5
        JR      C,NP_TWO
        JR      Z,NP_TWO

NP_NORM:
; Above 2 only odd candidates need testing.
        ANC     $15,$SY
        JR      NZ,NP_TEST
        LDW     $5,1
        ADW     $15,$5
        JR      NC,NP_TEST
        ADW     $17,$SY
        JR      C,NP_NONE

NP_TEST:
        CAL     ISPRIME32
        AN      $4,$4
        JR      NZ,NP_OK

; Next odd candidate, with explicit uint32 overflow detection.
        LDW     $5,2
        ADW     $15,$5
        JR      NC,NP_TEST
        ADW     $17,$SY
        JR      C,NP_NONE
        JR      NP_TEST

NP_TWO:
        LDW     $15,2
        SBW     $17,$17
NP_OK:
        LD      $4,1
        RTN
NP_NONE:
        LD      $4,0
        RTN