Home » Archimedes archive » Archimedes World » AW-1996-03-Disc 2.adf » !AcornAns_AcornAns » MathFns/s/subrouts1

MathFns/s/subrouts1

This website contains an archive of files for the Acorn Electron, BBC Micro, Acorn Archimedes, Commodore 16 and Commodore 64 computers, which Dominic Ford has rescued from his private collection of floppy disks and cassettes.

Some of these files were originally commercial releases in the 1980s and 1990s, but they are now widely available online. I assume that copyright over them is no longer being asserted. If you own the copyright and would like files to be removed, please contact me.

Tape/disk: Home » Archimedes archive » Archimedes World » AW-1996-03-Disc 2.adf » !AcornAns_AcornAns
Filename: MathFns/s/subrouts1
Read OK:
File size: 3BAE bytes
Load address: 0000
Exec address: 0000
File contents
max16	*	&7fffffff	;max positive number in 16 bit fixed point format (2^31-1)/65536
min16	*	&80000000	;min negative number				  -2^31   /65536
one	*	65536



; rbbcinc
; a leaf APCS function
;
; C prototype:
; int rbbcinc(int r, int k)
;
; given limits 0 <= r < 2^k, return (r � 1) where � denotes a reversed bit ordering increment,
; subject to the stated limits.
; NB in this case, for the function f: n -> {for (k=c=0; c<n; c++, k=rbbcinc(k, l); return k;},
; we have f(f(n))=n.
; NB2 algo requires 2 <= k <= 32, but doesn't check for this - BEWARE!
; (for k=1 get no action taken, while for any other bad k get code executed at unintended address,
;  hence unpredictable & likely system fatal).
;

        EXPORT  rbbcinc

rbnsta  DCB     "rbbcinc", 0
        ALIGN
rbnend  DCD     &ff000000 + rbnend - rbnsta

rbbcinc

        rbbc    a1, a2, a3
        MOVS    pc, lr



; pow16
; a leaf APCS function
;
; C prototype:
; int pow16(int a, int b)
;
; returns 65536 * (a/65536)^(b/65536)
;
; is about 5 to 40 times faster than FPEmulator working with double floats
;
; nb if a<0 require b an integer
;
; nb2 unexpected return values:
; a=b=0				returns 1	actual value ill defined
; a<0, b non integer		returns 0	actual value complex
; abs(bln(a)) > max16		unknown errors likely (to avoid, restrict b to �(2^11)one, roughly)
; a=0, b<0			returns max16	actual value +infinity
; a & b st result out of range	unknown result
;
; nb precise nature of errors for other values not yet confirmed acceptable, though should be okay except
; perhaps in extreme cases (as of 22/11/95)
;
; Few subsequent notes on error:
;
; For result much bigger than one:
; since exp16 only yields 16 significant bits, pow16 gives no more than this, hence for result much bigger
; than one get increasing error, eg for a around 64one & b=1.03125one get errors st answer (around 73one)
; is only correct to top 15 or 16 bits (ie error upto around one/1024 to one/256)
;
; For 0<=a<=one, & b eg 2one, 3one, 13.125one etc get error typically no more than in low 1 or 2 bits
;
; For a large (say a>one) & b<0 get error typically in only lowest bit or no error
;
;   ****  Strongly recommend this fn is only used where accuracy not crucial or with limited range ****
;   ****  of arguments where you can confirm yourself accuracy in that range is adequate.          ****
;

        EXPORT  pow16

pwnsta  DCB     "pow16", 0
        ALIGN
pwnend  DCD     &ff000000 + pwnend - pwnsta

pow16

	CMP	a2, #0
	MOVEQ	a1, #&10000		;if b=0 return one directly
	MOVEQS	pc, lr
	CMP	a2, #&10000
	MOVEQS	pc, lr			;handle trival case b=one directly, by returning a
	CMP	a1, #0
	MOVGT	ip, #0
	BGT	pow16_apos
	BNE	pow16_aneg
	CMP	a2, #0
	MOV	a1, #0			;if a=0 and b>0 return 0
	MOVLT	a1, #max16+1		;if a=0 and b<0 return max16
	SUBLT	a1, a1, #1
	MOVS	pc, lr
pow16_aneg
	MOVS	ip, a2, LSL #16
	MOVNE	a1, #0			;if a<0 and b not an integer return 0
	MOVNES	pc, lr
	AND	ip, a2, #&10000		;if a<0 then go on to evaluate (65536 * (-a/65536)^(b/65536))
	RSB	a1, a1, #0		;with sign subsequently forced to + or - according to whether b/one
pow16_apos				;is even or odd ( eg (-2.5)^3 is just (-1)^3 * 2.5^3 )
	STMFD	sp!, {v1, v2, lr}
	MOV	v1, ip			;now evaluate (65536 * (a/65536)^(b/65536)), for a>0
	MOV	v2, a2			;via exp16( ln16(a) * b / 65536 )
	BL	ln16			;nb this uses property that exp(b.log(a)) = exp(log(a^b)) = a^b
        mul16	a1, v2, a1, a2, a3, a4
	BL	exp16
	CMP	v1, #0
	RSBNE	a1, a1, #0		;finally switch sign on answer if originally a<0 and b/one was odd
	LDMFD	sp!, {v1, v2, pc}^



; ln16
; a leaf APCS function
;
; C prototype:
; int ln16(int a)
;
; returns 65536 * ln (a/65536)
;
; is about 30 times faster than FPEmulator working with double floats
;

        EXPORT  ln16

lnnsta  DCB     "ln16", 0
        ALIGN
lnnend  DCD     &ff000000 + lnnend - lnnsta

ln16

	CMP	a1, #0			;if a<=0 return min16
	MOVLE	a1, #min16
	MOVLES	pc, lr			;else most significant bit is between b30 & b0
	GBLA	counter
counter	SETA	30			;find msb & store (msb_index - 15) in a4
	WHILE	counter > 1
	TST	a1, #1<<counter
	MOVNE	a4, #counter-15
	BNE	ln16_gotmsb
counter	SETA	counter-1
	WEND
	TST	a1, #1<<1
	MOVNE	a4, #1-15
	MOVEQ	a4, #0-15
ln16_gotmsb				;if we set z = a1/(2^a4), will have ln(a/one) = ln(z/one * 2^a4)
	CMP	a4, #2			;					      = ln(z/one) + a4*ln2
	SUBGT	a2, a4, #2		;with z in range [0.5,1)
	MOVGT	a1, a1, LSR a2		;so calc a1 = 4*( a1/(2^a4) ) - 3*one
	RSBLE	a2, a4, #2		;which puts a1 in range �one suitable for approximation by poly
	MOVLE	a1, a1, LSL a2		;leaving us to calculate a4*one*ln2 + one*ln((a1+3one)/4one)
	SUB	a1, a1, #&30000
	ADD	a2, a1, a1, LSL #2	;start polynomial approximation calculation on a1
	RSB	a2, a2, a1, LSL #10
	MOV	a2, a2, ASR #16		;for details of how this works see comments against similar code
	SUB	a2, a2, #&000e00	;in exp16 function, directly below this function
	SUB	a2, a2, #&000034
        MOV	ip, a1
	mul16c	a2, ip, a2, a3
	ADD	a2, a2, #&003200
	ADD	a2, a2, #&000031
        MOV	ip, a1
	mul16c	a2, ip, a2, a3
	SUB	a2, a2, #&00e200
	SUB	a2, a2, #&0000f2
        MOV	ip, a1
	mul16c	a2, ip, a2, a3
	ADD	a2, a2, #&050000
	ADD	a2, a2, #&005500
	ADD	a2, a2, #&000065
        MOV	ip, a1
	mul16c	a2, ip, a2, a3
	SUB	a2, a2, #&040000
	SUB	a2, a2, #&009a00	;end of polynomial approximation calculation
	SUB	a1, a2, #&000059	;a1 now holds (2^20)*(approximated value)
	ADD	ip, a4, a4, LSL #1
	ADD	a2, ip, a4, LSL #3	;now add in the a4*one*ln2 term using an explicit binary expansion
	ADD	a2, a4, a2, LSL #4	;of approximately one*ln2
	RSB	a3, a4, a4, LSL #3
	ADD	a2, a3, a2, LSL #4	;nb we actually add approx (2^20)*ln2, & then divide by 16
	ADD	a2, a4, a2, LSL #3	;to reduce truncation error
	ADD	a1, a1, a2, LSL #5
	ADD	a1, a1, ip, ASR #1
	MOV	a1, a1, ASR #4
        MOVS    pc, lr



; exp16
; a leaf APCS function
;
; C prototype:
; int exp16(int a)
;
; returns 65536 * exp (a/65536)
;
; is about 45 times faster than FPEmulator working with double floats
;

        EXPORT  exp16

epnsta  DCB     "exp16", 0
        ALIGN
epnend  DCD     &ff000000 + epnend - epnsta

exp16					;this fn returns a value with only about 17 significant binary
					;digits - eg for 'a' small or negative, get near maximal accuracy
					;but for 'a' large (upto around 10one) where exp has value
	CMP	a1, #12*65536		;~20000one can get error upto roughly 0.1one
	MOVGT	a1, #max16+1
	SUBGT	a1, a1, #1
	MOVGTS	pc, lr
	CMP	a1, #-12*65536
	MOVLT	a1, #0
	MOVLTS	pc, lr			;otherwise know |a| <= 12one
	RSB	a2, a1, a1, LSL #3	;now set a = a/ln2 using explicit mul by 2_1.01110001010101000111
	ADD	a3, a1, a1, LSL #2	;nb only need 1st 20 digits given range on a
	ADD	a3, a3, a3, LSL #4	;nb2 this calc is approximate only, though will usually yield an
	ADD	a1, a2, a1, LSL #4	;answer correct to the stored 16 binary places (else error one/65536)
	ADD	a1, a1, a3, ASR #10
	ADD	a1, a1, a2, ASR #16	;thus exp(original a)  = exp(new a * ln2) = 2^(new a), eval'd below:
	ADD	a1, a1, #8
	MOV	a1, a1, ASR #4		;calc a = a/ln2 complete
	MOV	a4, a1, ASR #16		;a4 now holds integer component of a
	BIC	a1, a1, a4, LSL #16	;a1 now holds fractional component in range [0,1)*one
	CMP	a4, #15			;do another check on a to ensure exp(a) is in range
	MOVGE	a1, #max16+1		;& if not return maximal or minimal value as appropriate
	SUBGE	a1, a1, #1
	MOVGE	pc, lr
	CMP	a4, #-16
	MOVLT	a1, #0
	MOVLTS	pc, lr			;else need to return value (one * 2^(a4 + a1/one))
	MOV	a1, a1, LSL #1		;			 = (one * 2^(a1/one) * 2^a4)
	SUB	a1, a1, #&10000		;convert a1 to lie in [-1,-1), then apply poly approximation:
	RSB	a2, a1, a1, LSL #3
	ADD	a2, a1, a2, LSL #6
	MOV	a2, a2, ASR #15		;a2 = 0.0008561*(2^20)*(a1/one)
	ADD	a2, a2, #&002800
	ADD	a2, a2, #&00007e	;a2 = 0.0008561*(2^20)*(a1/one) + 0.0098857*(2^20)
        MOV	ip, a1
	mul16c	a2, ip, a2, a3		;a2 = 0.0008561*(2^20)*(a1/one)^2 + 0.0098857*(2^20)*(a1/one)
	ADD	a2, a2, #&015000
	ADD	a2, a2, #&000be0	; etc
        MOV	ip, a1
	mul16c	a2, ip, a2, a3
	ADD	a2, a2, #&070000
	ADD	a2, a2, #&00d700
	ADD	a2, a2, #&00007e
        MOV	ip, a1
	mul16c	a2, ip, a2, a3		;until we have
	ADD	a2, a2, #&160000	;a2 = (2^20) ( 0.0008561(a1/one)^4 + 0.0098857(a1/one)^3 +
	ADD	a2, a2, #&00a000	;	       0.0849301(a1/one)^2 + 0.4901106(a1/one) +
	ADD	a2, a2, #&0000a7	;	       1.14142138(a1/one)                           )
	MOV	a1, a2, ASR #4		;now divide by 16 removing most truncation error from above calcs,
	CMP	a4, #0			;leaving a2 = (2^16)*(approximated value)
	RSBLT	a4, a4, #0
	MOVLT	a1, a1, ASR a4		;finally carry out the (a2 = a2 * 2^a4) step
	MOVLTS	pc, lr
	MOVS	a1, a1, LSL a4
	MOVMI	a1, #max16+1
	SUBMI	a1, a1, #1
        MOVS    pc, lr



; cos16
; a leaf APCS function
;
; C prototype:
; int cos16(int a)
;
; returns 65536 * cos ((a/65536)(pi/2))
;
; is about 44 times faster than FPEmulator working with double floats
;

        EXPORT  cos16

csnsta  DCB     "cos16", 0
        ALIGN
csnend  DCD     &ff000000 + csnend - csnsta

cos16

	EOR	a4, a1, a1, LSL #1	;since cos is periodic and cos((a/65536)(pi/2)) has period 4*one
	TST	a4, #&20000		;eval is simpler than for ln or exp:
	MOV	a4, #0			;we need only to truncate a to [0,4one) and then approx the function
	MOVNE	a4, #1			;via a poly
	TST	a1, #&10000
	MOV	a1, a1, LSL #16		;in fact further symmetries in cos over this range allow us to
	MOV	a1, a1, LSR #16		;actually only approximate function over range [0,one)
	RSBEQ	a1, a1, #&10000		;and reconstruct it over remainder of range [one,4one) from that part
	MOV	a1, a1, LSL #1		;eg  cos( (a/65536)(pi/2) ) for a in [2one, 3one)
	SUB	a1, a1, #&10000		; = -cos( ((a-2one)/65536)(pi/2) )
	RSB	a2, a1, a1, LSL #3
	ADD	a2, a1, a2, LSL #8
	MOV	a2, a2, ASR #16
	ADD	a2, a2, #&002c00
	ADD	a2, a2, #&000086
        MOV	ip, a1
	mul16c	a2, ip, a2, a3
	SUB	a2, a2, #&00e900
	SUB	a2, a2, #&0000bd
        MOV	ip, a1
	mul16c	a2, ip, a2, a3
	SUB	a2, a2, #&030000
	SUB	a2, a2, #&007c00
	SUB	a2, a2, #&0000c6
        MOV	ip, a1
	mul16c	a2, ip, a2, a3
	ADD	a2, a2, #&080000
	ADD	a2, a2, #&00E200
	ADD	a2, a2, #&0000BD
        MOV	ip, a1
	mul16c	a2, ip, a2, a3
	ADD	a2, a2, #&0b0000
	ADD	a2, a2, #&005000
	ADD	a2, a2, #&000050
	MOV	a1, a2, ASR #4
	TST	a4, #1
	RSBNE	a1, a1, #0
        MOVS    pc, lr



; sin16
; a leaf APCS function
;
; C prototype:
; int sin16(int a)
;
; returns 65536 * sin ((a/65536)(pi/2))
;
; is about 44 times faster than FPEmulator working with double floats
;

        EXPORT  sin16

snnsta  DCB     "sin16", 0
        ALIGN
snnend  DCD     &ff000000 + snnend - snnsta

sin16

	TST	a1, #&20000		;eval of sin is almost identical to that of cos
	MOV	a4, #0
	MOVNE	a4, #1
	TST	a1, #&10000
	MOV	a1, a1, LSL #16
	MOV	a1, a1, LSR #16
	RSBNE	a1, a1, #&10000
	MOV	a1, a1, LSL #1
	SUB	a1, a1, #&10000
	RSB	a2, a1, a1, LSL #3
	ADD	a2, a1, a2, LSL #8
	MOV	a2, a2, ASR #16
	ADD	a2, a2, #&002c00
	ADD	a2, a2, #&000086
        MOV	ip, a1
	mul16c	a2, ip, a2, a3
	SUB	a2, a2, #&00e900
	SUB	a2, a2, #&0000bd
        MOV	ip, a1
	mul16c	a2, ip, a2, a3
	SUB	a2, a2, #&030000
	SUB	a2, a2, #&007c00
	SUB	a2, a2, #&0000c6
        MOV	ip, a1
	mul16c	a2, ip, a2, a3
	ADD	a2, a2, #&080000
	ADD	a2, a2, #&00E200
	ADD	a2, a2, #&0000BD
        MOV	ip, a1
	mul16c	a2, ip, a2, a3
	ADD	a2, a2, #&0b0000
	ADD	a2, a2, #&005000
	ADD	a2, a2, #&000050
	MOV	a1, a2, ASR #4
	TST	a4, #1
	RSBNE	a1, a1, #0
        MOVS    pc, lr



; gauss16
; a leaf APCS function
;
; C prototype:
; int gauss16(void)
;
; returns 65536 * ( pseudo randon variable with approximate distribution N(0,1) )
; note, the approximate gaussian distribution is achieved via the sum of n pseudo U[0,65535]
;       random variables & application of the central limit theorem
;       here we use n=8, giving an actual range of �(sqrt24) (*65536).
;
;       for general n, recall we need to return:
;       (sum n U[0,65535] random variables) * 2sqrt(3n)*65536/(65535n)  -  sqrt(3n)*65536
;

	EXPORT	gauss16

gansta  DCB     "gauss16", 0
        ALIGN
ganend  DCD     &ff000000 + ganend - gansta

gauss16

	ADR	a1, gaussseed1
	LDMIA	a1, {a2, a3}

	MOVS    a3, a3, LSR #1
        MOVS    a4, a2, RRX
        ADC     a3, a3, a3
        EOR     a4, a4, a2, LSL #12
        EOR     a2, a4, a4, LSR #20
	MOV	ip, a2, LSR #16
	MOV	a4, a2, LSL #16
	ADD	ip, ip, a4, LSR #16

	MOVS    a3, a3, LSR #1
        MOVS    a4, a2, RRX
        ADC     a3, a3, a3
        EOR     a4, a4, a2, LSL #12
        EOR     a2, a4, a4, LSR #20
	ADD	ip, ip, a2, LSR #16
	MOV	a4, a2, LSL #16
	ADD	ip, ip, a4, LSR #16

	MOVS    a3, a3, LSR #1
        MOVS    a4, a2, RRX
        ADC     a3, a3, a3
        EOR     a4, a4, a2, LSL #12
        EOR     a2, a4, a4, LSR #20
	ADD	ip, ip, a2, LSR #16
	MOV	a4, a2, LSL #16
	ADD	ip, ip, a4, LSR #16

	MOVS    a3, a3, LSR #1
        MOVS    a4, a2, RRX
        ADC     a3, a3, a3
        EOR     a4, a4, a2, LSL #12
        EOR     a2, a4, a4, LSR #20
	ADD	ip, ip, a2, LSR #16
	MOV	a4, a2, LSL #16
	ADD	ip, ip, a4, LSR #16		;sum now in ip

	STMIA	a1, {a2, a3}

	RSB	a1, ip, ip, ASL #3
	RSB	a2, ip, ip, ASL #2
	ADD	a1, a2, a1, ASL #4
	ADD	a1, a1, ip, ASL #9
	ADD	a2, ip, ip, ASL #4
	ADD	a2, a2, ip, ASL #6
	ADD	a1, a1, a2, LSR #10
	MOV	a1, a1, LSR #9
	SUB	a1, a1, #&4E000
	SUB	a1, a1, #&00620
	SUB	a1, a1, #&00004

        MOVS    pc, lr

gaussseed1	DCD	-1		;bits b0-b31 of seed
		DCD	-1		;bit  b32 of seed (in lsb of word)

; sgauss16
; a leaf APCS function
;
; C prototype:
; void sgauss16(int seed)
;
; sets the seed for gauss16
;

	EXPORT	sgauss16

sgnsta  DCB     "sgauss16", 0
        ALIGN
sgnend  DCD     &ff000000 + sgnend - sgnsta

sgauss16

	ADR	a2, gaussseed1
	MOV	a3, #1
	STMIA	a2, {a1, a3}
        MOVS    pc, lr



; div_frac16
; a leaf APCS function
;
; C prototype:
; int div_frac16(int number, int divisor)
;
; returns integer part of 65536*number/divisor
; assumes abs number < 65536 * abs divisor
; if this needs checking, must be done by caller
;

        EXPORT  div_frac16

dfnsta  DCB     "div_frac16", 0
        ALIGN
dfnend  DCD     &ff000000 + dfnend - dfnsta

div_frac16

        div16   a1, a2, a1, a3, a4
        MOVS    pc, lr



; mul_frac16
; a leaf APCS function
;
; C prototype:
; int mul_frac16(int x, int a)
;
; returns 32-bit signed integer x*a/65536
; assumes result fits into 32-bit signed representation
; note, no other restrictions on a - if can guarantee abs a < 65536, use mul_frac16c instead as is quicker
;

        EXPORT  mul_frac16

mfnsta  DCB     "mul_frac16", 0
        ALIGN
mfnend  DCD     &ff000000 + mfnend - mfnsta

mul_frac16

        mul16   a1, a2, a1, a3, a4, ip
        MOVS    pc, lr



; mul_frac16c
; a leaf APCS function
;
; C prototype:
; int mul_frac16c(int x, int a)
;
; returns 32-bit signed integer x*a/65536
; assumes abs a <=65536
; if it is possible that abs a > 65536, caller must check range & either not call fn or round down to 65536
;

        EXPORT  mul_frac16c

mfcnsta DCB     "mul_frac16c", 0
        ALIGN
mfcnend DCD     &ff000000 + mfcnend - mfcnsta

mul_frac16c

        mul16c  a1, a2, a1, a3
        MOVS    pc, lr



; sqrt_frac28
; a leaf APCS function
;
; C prototype:
; int sqrt_frac28(unsigned int x)
;
; returns 32-bit integer sqrt(x<<28)
;

        EXPORT  sqrt_frac28

sqfnsta DCB     "sqrt_frac28", 0
        ALIGN
sqfnend DCD     &ff000000 + sqfnend - sqfnsta

sqrt_frac28

        sqrt28  a1, a1, a2, a3, a4, ip
        MOVS    pc, lr



        END
00000000  0a 6d 61 78 31 36 09 2a  09 26 37 66 66 66 66 66  |.max16.*.&7fffff|
00000010  66 66 09 3b 6d 61 78 20  70 6f 73 69 74 69 76 65  |ff.;max positive|
00000020  20 6e 75 6d 62 65 72 20  69 6e 20 31 36 20 62 69  | number in 16 bi|
00000030  74 20 66 69 78 65 64 20  70 6f 69 6e 74 20 66 6f  |t fixed point fo|
00000040  72 6d 61 74 20 28 32 5e  33 31 2d 31 29 2f 36 35  |rmat (2^31-1)/65|
00000050  35 33 36 0a 6d 69 6e 31  36 09 2a 09 26 38 30 30  |536.min16.*.&800|
00000060  30 30 30 30 30 09 3b 6d  69 6e 20 6e 65 67 61 74  |00000.;min negat|
00000070  69 76 65 20 6e 75 6d 62  65 72 09 09 09 09 20 20  |ive number....  |
00000080  2d 32 5e 33 31 20 20 20  2f 36 35 35 33 36 0a 6f  |-2^31   /65536.o|
00000090  6e 65 09 2a 09 36 35 35  33 36 0a 0a 0a 0a 3b 20  |ne.*.65536....; |
000000a0  72 62 62 63 69 6e 63 0a  3b 20 61 20 6c 65 61 66  |rbbcinc.; a leaf|
000000b0  20 41 50 43 53 20 66 75  6e 63 74 69 6f 6e 0a 3b  | APCS function.;|
000000c0  0a 3b 20 43 20 70 72 6f  74 6f 74 79 70 65 3a 0a  |.; C prototype:.|
000000d0  3b 20 69 6e 74 20 72 62  62 63 69 6e 63 28 69 6e  |; int rbbcinc(in|
000000e0  74 20 72 2c 20 69 6e 74  20 6b 29 0a 3b 0a 3b 20  |t r, int k).;.; |
000000f0  67 69 76 65 6e 20 6c 69  6d 69 74 73 20 30 20 3c  |given limits 0 <|
00000100  3d 20 72 20 3c 20 32 5e  6b 2c 20 72 65 74 75 72  |= r < 2^k, retur|
00000110  6e 20 28 72 20 a4 20 31  29 20 77 68 65 72 65 20  |n (r . 1) where |
00000120  a4 20 64 65 6e 6f 74 65  73 20 61 20 72 65 76 65  |. denotes a reve|
00000130  72 73 65 64 20 62 69 74  20 6f 72 64 65 72 69 6e  |rsed bit orderin|
00000140  67 20 69 6e 63 72 65 6d  65 6e 74 2c 0a 3b 20 73  |g increment,.; s|
00000150  75 62 6a 65 63 74 20 74  6f 20 74 68 65 20 73 74  |ubject to the st|
00000160  61 74 65 64 20 6c 69 6d  69 74 73 2e 0a 3b 20 4e  |ated limits..; N|
00000170  42 20 69 6e 20 74 68 69  73 20 63 61 73 65 2c 20  |B in this case, |
00000180  66 6f 72 20 74 68 65 20  66 75 6e 63 74 69 6f 6e  |for the function|
00000190  20 66 3a 20 6e 20 2d 3e  20 7b 66 6f 72 20 28 6b  | f: n -> {for (k|
000001a0  3d 63 3d 30 3b 20 63 3c  6e 3b 20 63 2b 2b 2c 20  |=c=0; c<n; c++, |
000001b0  6b 3d 72 62 62 63 69 6e  63 28 6b 2c 20 6c 29 3b  |k=rbbcinc(k, l);|
000001c0  20 72 65 74 75 72 6e 20  6b 3b 7d 2c 0a 3b 20 77  | return k;},.; w|
000001d0  65 20 68 61 76 65 20 66  28 66 28 6e 29 29 3d 6e  |e have f(f(n))=n|
000001e0  2e 0a 3b 20 4e 42 32 20  61 6c 67 6f 20 72 65 71  |..; NB2 algo req|
000001f0  75 69 72 65 73 20 32 20  3c 3d 20 6b 20 3c 3d 20  |uires 2 <= k <= |
00000200  33 32 2c 20 62 75 74 20  64 6f 65 73 6e 27 74 20  |32, but doesn't |
00000210  63 68 65 63 6b 20 66 6f  72 20 74 68 69 73 20 2d  |check for this -|
00000220  20 42 45 57 41 52 45 21  0a 3b 20 28 66 6f 72 20  | BEWARE!.; (for |
00000230  6b 3d 31 20 67 65 74 20  6e 6f 20 61 63 74 69 6f  |k=1 get no actio|
00000240  6e 20 74 61 6b 65 6e 2c  20 77 68 69 6c 65 20 66  |n taken, while f|
00000250  6f 72 20 61 6e 79 20 6f  74 68 65 72 20 62 61 64  |or any other bad|
00000260  20 6b 20 67 65 74 20 63  6f 64 65 20 65 78 65 63  | k get code exec|
00000270  75 74 65 64 20 61 74 20  75 6e 69 6e 74 65 6e 64  |uted at unintend|
00000280  65 64 20 61 64 64 72 65  73 73 2c 0a 3b 20 20 68  |ed address,.;  h|
00000290  65 6e 63 65 20 75 6e 70  72 65 64 69 63 74 61 62  |ence unpredictab|
000002a0  6c 65 20 26 20 6c 69 6b  65 6c 79 20 73 79 73 74  |le & likely syst|
000002b0  65 6d 20 66 61 74 61 6c  29 2e 0a 3b 0a 0a 20 20  |em fatal)..;..  |
000002c0  20 20 20 20 20 20 45 58  50 4f 52 54 20 20 72 62  |      EXPORT  rb|
000002d0  62 63 69 6e 63 0a 0a 72  62 6e 73 74 61 20 20 44  |bcinc..rbnsta  D|
000002e0  43 42 20 20 20 20 20 22  72 62 62 63 69 6e 63 22  |CB     "rbbcinc"|
000002f0  2c 20 30 0a 20 20 20 20  20 20 20 20 41 4c 49 47  |, 0.        ALIG|
00000300  4e 0a 72 62 6e 65 6e 64  20 20 44 43 44 20 20 20  |N.rbnend  DCD   |
00000310  20 20 26 66 66 30 30 30  30 30 30 20 2b 20 72 62  |  &ff000000 + rb|
00000320  6e 65 6e 64 20 2d 20 72  62 6e 73 74 61 0a 0a 72  |nend - rbnsta..r|
00000330  62 62 63 69 6e 63 0a 0a  20 20 20 20 20 20 20 20  |bbcinc..        |
00000340  72 62 62 63 20 20 20 20  61 31 2c 20 61 32 2c 20  |rbbc    a1, a2, |
00000350  61 33 0a 20 20 20 20 20  20 20 20 4d 4f 56 53 20  |a3.        MOVS |
00000360  20 20 20 70 63 2c 20 6c  72 0a 0a 0a 0a 3b 20 70  |   pc, lr....; p|
00000370  6f 77 31 36 0a 3b 20 61  20 6c 65 61 66 20 41 50  |ow16.; a leaf AP|
00000380  43 53 20 66 75 6e 63 74  69 6f 6e 0a 3b 0a 3b 20  |CS function.;.; |
00000390  43 20 70 72 6f 74 6f 74  79 70 65 3a 0a 3b 20 69  |C prototype:.; i|
000003a0  6e 74 20 70 6f 77 31 36  28 69 6e 74 20 61 2c 20  |nt pow16(int a, |
000003b0  69 6e 74 20 62 29 0a 3b  0a 3b 20 72 65 74 75 72  |int b).;.; retur|
000003c0  6e 73 20 36 35 35 33 36  20 2a 20 28 61 2f 36 35  |ns 65536 * (a/65|
000003d0  35 33 36 29 5e 28 62 2f  36 35 35 33 36 29 0a 3b  |536)^(b/65536).;|
000003e0  0a 3b 20 69 73 20 61 62  6f 75 74 20 35 20 74 6f  |.; is about 5 to|
000003f0  20 34 30 20 74 69 6d 65  73 20 66 61 73 74 65 72  | 40 times faster|
00000400  20 74 68 61 6e 20 46 50  45 6d 75 6c 61 74 6f 72  | than FPEmulator|
00000410  20 77 6f 72 6b 69 6e 67  20 77 69 74 68 20 64 6f  | working with do|
00000420  75 62 6c 65 20 66 6c 6f  61 74 73 0a 3b 0a 3b 20  |uble floats.;.; |
00000430  6e 62 20 69 66 20 61 3c  30 20 72 65 71 75 69 72  |nb if a<0 requir|
00000440  65 20 62 20 61 6e 20 69  6e 74 65 67 65 72 0a 3b  |e b an integer.;|
00000450  0a 3b 20 6e 62 32 20 75  6e 65 78 70 65 63 74 65  |.; nb2 unexpecte|
00000460  64 20 72 65 74 75 72 6e  20 76 61 6c 75 65 73 3a  |d return values:|
00000470  0a 3b 20 61 3d 62 3d 30  09 09 09 09 72 65 74 75  |.; a=b=0....retu|
00000480  72 6e 73 20 31 09 61 63  74 75 61 6c 20 76 61 6c  |rns 1.actual val|
00000490  75 65 20 69 6c 6c 20 64  65 66 69 6e 65 64 0a 3b  |ue ill defined.;|
000004a0  20 61 3c 30 2c 20 62 20  6e 6f 6e 20 69 6e 74 65  | a<0, b non inte|
000004b0  67 65 72 09 09 72 65 74  75 72 6e 73 20 30 09 61  |ger..returns 0.a|
000004c0  63 74 75 61 6c 20 76 61  6c 75 65 20 63 6f 6d 70  |ctual value comp|
000004d0  6c 65 78 0a 3b 20 61 62  73 28 62 6c 6e 28 61 29  |lex.; abs(bln(a)|
000004e0  29 20 3e 20 6d 61 78 31  36 09 09 75 6e 6b 6e 6f  |) > max16..unkno|
000004f0  77 6e 20 65 72 72 6f 72  73 20 6c 69 6b 65 6c 79  |wn errors likely|
00000500  20 28 74 6f 20 61 76 6f  69 64 2c 20 72 65 73 74  | (to avoid, rest|
00000510  72 69 63 74 20 62 20 74  6f 20 b1 28 32 5e 31 31  |rict b to .(2^11|
00000520  29 6f 6e 65 2c 20 72 6f  75 67 68 6c 79 29 0a 3b  |)one, roughly).;|
00000530  20 61 3d 30 2c 20 62 3c  30 09 09 09 72 65 74 75  | a=0, b<0...retu|
00000540  72 6e 73 20 6d 61 78 31  36 09 61 63 74 75 61 6c  |rns max16.actual|
00000550  20 76 61 6c 75 65 20 2b  69 6e 66 69 6e 69 74 79  | value +infinity|
00000560  0a 3b 20 61 20 26 20 62  20 73 74 20 72 65 73 75  |.; a & b st resu|
00000570  6c 74 20 6f 75 74 20 6f  66 20 72 61 6e 67 65 09  |lt out of range.|
00000580  75 6e 6b 6e 6f 77 6e 20  72 65 73 75 6c 74 0a 3b  |unknown result.;|
00000590  0a 3b 20 6e 62 20 70 72  65 63 69 73 65 20 6e 61  |.; nb precise na|
000005a0  74 75 72 65 20 6f 66 20  65 72 72 6f 72 73 20 66  |ture of errors f|
000005b0  6f 72 20 6f 74 68 65 72  20 76 61 6c 75 65 73 20  |or other values |
000005c0  6e 6f 74 20 79 65 74 20  63 6f 6e 66 69 72 6d 65  |not yet confirme|
000005d0  64 20 61 63 63 65 70 74  61 62 6c 65 2c 20 74 68  |d acceptable, th|
000005e0  6f 75 67 68 20 73 68 6f  75 6c 64 20 62 65 20 6f  |ough should be o|
000005f0  6b 61 79 20 65 78 63 65  70 74 0a 3b 20 70 65 72  |kay except.; per|
00000600  68 61 70 73 20 69 6e 20  65 78 74 72 65 6d 65 20  |haps in extreme |
00000610  63 61 73 65 73 20 28 61  73 20 6f 66 20 32 32 2f  |cases (as of 22/|
00000620  31 31 2f 39 35 29 0a 3b  0a 3b 20 46 65 77 20 73  |11/95).;.; Few s|
00000630  75 62 73 65 71 75 65 6e  74 20 6e 6f 74 65 73 20  |ubsequent notes |
00000640  6f 6e 20 65 72 72 6f 72  3a 0a 3b 0a 3b 20 46 6f  |on error:.;.; Fo|
00000650  72 20 72 65 73 75 6c 74  20 6d 75 63 68 20 62 69  |r result much bi|
00000660  67 67 65 72 20 74 68 61  6e 20 6f 6e 65 3a 0a 3b  |gger than one:.;|
00000670  20 73 69 6e 63 65 20 65  78 70 31 36 20 6f 6e 6c  | since exp16 onl|
00000680  79 20 79 69 65 6c 64 73  20 31 36 20 73 69 67 6e  |y yields 16 sign|
00000690  69 66 69 63 61 6e 74 20  62 69 74 73 2c 20 70 6f  |ificant bits, po|
000006a0  77 31 36 20 67 69 76 65  73 20 6e 6f 20 6d 6f 72  |w16 gives no mor|
000006b0  65 20 74 68 61 6e 20 74  68 69 73 2c 20 68 65 6e  |e than this, hen|
000006c0  63 65 20 66 6f 72 20 72  65 73 75 6c 74 20 6d 75  |ce for result mu|
000006d0  63 68 20 62 69 67 67 65  72 0a 3b 20 74 68 61 6e  |ch bigger.; than|
000006e0  20 6f 6e 65 20 67 65 74  20 69 6e 63 72 65 61 73  | one get increas|
000006f0  69 6e 67 20 65 72 72 6f  72 2c 20 65 67 20 66 6f  |ing error, eg fo|
00000700  72 20 61 20 61 72 6f 75  6e 64 20 36 34 6f 6e 65  |r a around 64one|
00000710  20 26 20 62 3d 31 2e 30  33 31 32 35 6f 6e 65 20  | & b=1.03125one |
00000720  67 65 74 20 65 72 72 6f  72 73 20 73 74 20 61 6e  |get errors st an|
00000730  73 77 65 72 20 28 61 72  6f 75 6e 64 20 37 33 6f  |swer (around 73o|
00000740  6e 65 29 0a 3b 20 69 73  20 6f 6e 6c 79 20 63 6f  |ne).; is only co|
00000750  72 72 65 63 74 20 74 6f  20 74 6f 70 20 31 35 20  |rrect to top 15 |
00000760  6f 72 20 31 36 20 62 69  74 73 20 28 69 65 20 65  |or 16 bits (ie e|
00000770  72 72 6f 72 20 75 70 74  6f 20 61 72 6f 75 6e 64  |rror upto around|
00000780  20 6f 6e 65 2f 31 30 32  34 20 74 6f 20 6f 6e 65  | one/1024 to one|
00000790  2f 32 35 36 29 0a 3b 0a  3b 20 46 6f 72 20 30 3c  |/256).;.; For 0<|
000007a0  3d 61 3c 3d 6f 6e 65 2c  20 26 20 62 20 65 67 20  |=a<=one, & b eg |
000007b0  32 6f 6e 65 2c 20 33 6f  6e 65 2c 20 31 33 2e 31  |2one, 3one, 13.1|
000007c0  32 35 6f 6e 65 20 65 74  63 20 67 65 74 20 65 72  |25one etc get er|
000007d0  72 6f 72 20 74 79 70 69  63 61 6c 6c 79 20 6e 6f  |ror typically no|
000007e0  20 6d 6f 72 65 20 74 68  61 6e 20 69 6e 20 6c 6f  | more than in lo|
000007f0  77 20 31 20 6f 72 20 32  20 62 69 74 73 0a 3b 0a  |w 1 or 2 bits.;.|
00000800  3b 20 46 6f 72 20 61 20  6c 61 72 67 65 20 28 73  |; For a large (s|
00000810  61 79 20 61 3e 6f 6e 65  29 20 26 20 62 3c 30 20  |ay a>one) & b<0 |
00000820  67 65 74 20 65 72 72 6f  72 20 74 79 70 69 63 61  |get error typica|
00000830  6c 6c 79 20 69 6e 20 6f  6e 6c 79 20 6c 6f 77 65  |lly in only lowe|
00000840  73 74 20 62 69 74 20 6f  72 20 6e 6f 20 65 72 72  |st bit or no err|
00000850  6f 72 0a 3b 0a 3b 20 20  20 2a 2a 2a 2a 20 20 53  |or.;.;   ****  S|
00000860  74 72 6f 6e 67 6c 79 20  72 65 63 6f 6d 6d 65 6e  |trongly recommen|
00000870  64 20 74 68 69 73 20 66  6e 20 69 73 20 6f 6e 6c  |d this fn is onl|
00000880  79 20 75 73 65 64 20 77  68 65 72 65 20 61 63 63  |y used where acc|
00000890  75 72 61 63 79 20 6e 6f  74 20 63 72 75 63 69 61  |uracy not crucia|
000008a0  6c 20 6f 72 20 77 69 74  68 20 6c 69 6d 69 74 65  |l or with limite|
000008b0  64 20 72 61 6e 67 65 20  2a 2a 2a 2a 0a 3b 20 20  |d range ****.;  |
000008c0  20 2a 2a 2a 2a 20 20 6f  66 20 61 72 67 75 6d 65  | ****  of argume|
000008d0  6e 74 73 20 77 68 65 72  65 20 79 6f 75 20 63 61  |nts where you ca|
000008e0  6e 20 63 6f 6e 66 69 72  6d 20 79 6f 75 72 73 65  |n confirm yourse|
000008f0  6c 66 20 61 63 63 75 72  61 63 79 20 69 6e 20 74  |lf accuracy in t|
00000900  68 61 74 20 72 61 6e 67  65 20 69 73 20 61 64 65  |hat range is ade|
00000910  71 75 61 74 65 2e 20 20  20 20 20 20 20 20 20 20  |quate.          |
00000920  2a 2a 2a 2a 0a 3b 0a 0a  20 20 20 20 20 20 20 20  |****.;..        |
00000930  45 58 50 4f 52 54 20 20  70 6f 77 31 36 0a 0a 70  |EXPORT  pow16..p|
00000940  77 6e 73 74 61 20 20 44  43 42 20 20 20 20 20 22  |wnsta  DCB     "|
00000950  70 6f 77 31 36 22 2c 20  30 0a 20 20 20 20 20 20  |pow16", 0.      |
00000960  20 20 41 4c 49 47 4e 0a  70 77 6e 65 6e 64 20 20  |  ALIGN.pwnend  |
00000970  44 43 44 20 20 20 20 20  26 66 66 30 30 30 30 30  |DCD     &ff00000|
00000980  30 20 2b 20 70 77 6e 65  6e 64 20 2d 20 70 77 6e  |0 + pwnend - pwn|
00000990  73 74 61 0a 0a 70 6f 77  31 36 0a 0a 09 43 4d 50  |sta..pow16...CMP|
000009a0  09 61 32 2c 20 23 30 0a  09 4d 4f 56 45 51 09 61  |.a2, #0..MOVEQ.a|
000009b0  31 2c 20 23 26 31 30 30  30 30 09 09 3b 69 66 20  |1, #&10000..;if |
000009c0  62 3d 30 20 72 65 74 75  72 6e 20 6f 6e 65 20 64  |b=0 return one d|
000009d0  69 72 65 63 74 6c 79 0a  09 4d 4f 56 45 51 53 09  |irectly..MOVEQS.|
000009e0  70 63 2c 20 6c 72 0a 09  43 4d 50 09 61 32 2c 20  |pc, lr..CMP.a2, |
000009f0  23 26 31 30 30 30 30 0a  09 4d 4f 56 45 51 53 09  |#&10000..MOVEQS.|
00000a00  70 63 2c 20 6c 72 09 09  09 3b 68 61 6e 64 6c 65  |pc, lr...;handle|
00000a10  20 74 72 69 76 61 6c 20  63 61 73 65 20 62 3d 6f  | trival case b=o|
00000a20  6e 65 20 64 69 72 65 63  74 6c 79 2c 20 62 79 20  |ne directly, by |
00000a30  72 65 74 75 72 6e 69 6e  67 20 61 0a 09 43 4d 50  |returning a..CMP|
00000a40  09 61 31 2c 20 23 30 0a  09 4d 4f 56 47 54 09 69  |.a1, #0..MOVGT.i|
00000a50  70 2c 20 23 30 0a 09 42  47 54 09 70 6f 77 31 36  |p, #0..BGT.pow16|
00000a60  5f 61 70 6f 73 0a 09 42  4e 45 09 70 6f 77 31 36  |_apos..BNE.pow16|
00000a70  5f 61 6e 65 67 0a 09 43  4d 50 09 61 32 2c 20 23  |_aneg..CMP.a2, #|
00000a80  30 0a 09 4d 4f 56 09 61  31 2c 20 23 30 09 09 09  |0..MOV.a1, #0...|
00000a90  3b 69 66 20 61 3d 30 20  61 6e 64 20 62 3e 30 20  |;if a=0 and b>0 |
00000aa0  72 65 74 75 72 6e 20 30  0a 09 4d 4f 56 4c 54 09  |return 0..MOVLT.|
00000ab0  61 31 2c 20 23 6d 61 78  31 36 2b 31 09 09 3b 69  |a1, #max16+1..;i|
00000ac0  66 20 61 3d 30 20 61 6e  64 20 62 3c 30 20 72 65  |f a=0 and b<0 re|
00000ad0  74 75 72 6e 20 6d 61 78  31 36 0a 09 53 55 42 4c  |turn max16..SUBL|
00000ae0  54 09 61 31 2c 20 61 31  2c 20 23 31 0a 09 4d 4f  |T.a1, a1, #1..MO|
00000af0  56 53 09 70 63 2c 20 6c  72 0a 70 6f 77 31 36 5f  |VS.pc, lr.pow16_|
00000b00  61 6e 65 67 0a 09 4d 4f  56 53 09 69 70 2c 20 61  |aneg..MOVS.ip, a|
00000b10  32 2c 20 4c 53 4c 20 23  31 36 0a 09 4d 4f 56 4e  |2, LSL #16..MOVN|
00000b20  45 09 61 31 2c 20 23 30  09 09 09 3b 69 66 20 61  |E.a1, #0...;if a|
00000b30  3c 30 20 61 6e 64 20 62  20 6e 6f 74 20 61 6e 20  |<0 and b not an |
00000b40  69 6e 74 65 67 65 72 20  72 65 74 75 72 6e 20 30  |integer return 0|
00000b50  0a 09 4d 4f 56 4e 45 53  09 70 63 2c 20 6c 72 0a  |..MOVNES.pc, lr.|
00000b60  09 41 4e 44 09 69 70 2c  20 61 32 2c 20 23 26 31  |.AND.ip, a2, #&1|
00000b70  30 30 30 30 09 09 3b 69  66 20 61 3c 30 20 74 68  |0000..;if a<0 th|
00000b80  65 6e 20 67 6f 20 6f 6e  20 74 6f 20 65 76 61 6c  |en go on to eval|
00000b90  75 61 74 65 20 28 36 35  35 33 36 20 2a 20 28 2d  |uate (65536 * (-|
00000ba0  61 2f 36 35 35 33 36 29  5e 28 62 2f 36 35 35 33  |a/65536)^(b/6553|
00000bb0  36 29 29 0a 09 52 53 42  09 61 31 2c 20 61 31 2c  |6))..RSB.a1, a1,|
00000bc0  20 23 30 09 09 3b 77 69  74 68 20 73 69 67 6e 20  | #0..;with sign |
00000bd0  73 75 62 73 65 71 75 65  6e 74 6c 79 20 66 6f 72  |subsequently for|
00000be0  63 65 64 20 74 6f 20 2b  20 6f 72 20 2d 20 61 63  |ced to + or - ac|
00000bf0  63 6f 72 64 69 6e 67 20  74 6f 20 77 68 65 74 68  |cording to wheth|
00000c00  65 72 20 62 2f 6f 6e 65  0a 70 6f 77 31 36 5f 61  |er b/one.pow16_a|
00000c10  70 6f 73 09 09 09 09 3b  69 73 20 65 76 65 6e 20  |pos....;is even |
00000c20  6f 72 20 6f 64 64 20 28  20 65 67 20 28 2d 32 2e  |or odd ( eg (-2.|
00000c30  35 29 5e 33 20 69 73 20  6a 75 73 74 20 28 2d 31  |5)^3 is just (-1|
00000c40  29 5e 33 20 2a 20 32 2e  35 5e 33 20 29 0a 09 53  |)^3 * 2.5^3 )..S|
00000c50  54 4d 46 44 09 73 70 21  2c 20 7b 76 31 2c 20 76  |TMFD.sp!, {v1, v|
00000c60  32 2c 20 6c 72 7d 0a 09  4d 4f 56 09 76 31 2c 20  |2, lr}..MOV.v1, |
00000c70  69 70 09 09 09 3b 6e 6f  77 20 65 76 61 6c 75 61  |ip...;now evalua|
00000c80  74 65 20 28 36 35 35 33  36 20 2a 20 28 61 2f 36  |te (65536 * (a/6|
00000c90  35 35 33 36 29 5e 28 62  2f 36 35 35 33 36 29 29  |5536)^(b/65536))|
00000ca0  2c 20 66 6f 72 20 61 3e  30 0a 09 4d 4f 56 09 76  |, for a>0..MOV.v|
00000cb0  32 2c 20 61 32 09 09 09  3b 76 69 61 20 65 78 70  |2, a2...;via exp|
00000cc0  31 36 28 20 6c 6e 31 36  28 61 29 20 2a 20 62 20  |16( ln16(a) * b |
00000cd0  2f 20 36 35 35 33 36 20  29 0a 09 42 4c 09 6c 6e  |/ 65536 )..BL.ln|
00000ce0  31 36 09 09 09 3b 6e 62  20 74 68 69 73 20 75 73  |16...;nb this us|
00000cf0  65 73 20 70 72 6f 70 65  72 74 79 20 74 68 61 74  |es property that|
00000d00  20 65 78 70 28 62 2e 6c  6f 67 28 61 29 29 20 3d  | exp(b.log(a)) =|
00000d10  20 65 78 70 28 6c 6f 67  28 61 5e 62 29 29 20 3d  | exp(log(a^b)) =|
00000d20  20 61 5e 62 0a 20 20 20  20 20 20 20 20 6d 75 6c  | a^b.        mul|
00000d30  31 36 09 61 31 2c 20 76  32 2c 20 61 31 2c 20 61  |16.a1, v2, a1, a|
00000d40  32 2c 20 61 33 2c 20 61  34 0a 09 42 4c 09 65 78  |2, a3, a4..BL.ex|
00000d50  70 31 36 0a 09 43 4d 50  09 76 31 2c 20 23 30 0a  |p16..CMP.v1, #0.|
00000d60  09 52 53 42 4e 45 09 61  31 2c 20 61 31 2c 20 23  |.RSBNE.a1, a1, #|
00000d70  30 09 09 3b 66 69 6e 61  6c 6c 79 20 73 77 69 74  |0..;finally swit|
00000d80  63 68 20 73 69 67 6e 20  6f 6e 20 61 6e 73 77 65  |ch sign on answe|
00000d90  72 20 69 66 20 6f 72 69  67 69 6e 61 6c 6c 79 20  |r if originally |
00000da0  61 3c 30 20 61 6e 64 20  62 2f 6f 6e 65 20 77 61  |a<0 and b/one wa|
00000db0  73 20 6f 64 64 0a 09 4c  44 4d 46 44 09 73 70 21  |s odd..LDMFD.sp!|
00000dc0  2c 20 7b 76 31 2c 20 76  32 2c 20 70 63 7d 5e 0a  |, {v1, v2, pc}^.|
00000dd0  0a 0a 0a 3b 20 6c 6e 31  36 0a 3b 20 61 20 6c 65  |...; ln16.; a le|
00000de0  61 66 20 41 50 43 53 20  66 75 6e 63 74 69 6f 6e  |af APCS function|
00000df0  0a 3b 0a 3b 20 43 20 70  72 6f 74 6f 74 79 70 65  |.;.; C prototype|
00000e00  3a 0a 3b 20 69 6e 74 20  6c 6e 31 36 28 69 6e 74  |:.; int ln16(int|
00000e10  20 61 29 0a 3b 0a 3b 20  72 65 74 75 72 6e 73 20  | a).;.; returns |
00000e20  36 35 35 33 36 20 2a 20  6c 6e 20 28 61 2f 36 35  |65536 * ln (a/65|
00000e30  35 33 36 29 0a 3b 0a 3b  20 69 73 20 61 62 6f 75  |536).;.; is abou|
00000e40  74 20 33 30 20 74 69 6d  65 73 20 66 61 73 74 65  |t 30 times faste|
00000e50  72 20 74 68 61 6e 20 46  50 45 6d 75 6c 61 74 6f  |r than FPEmulato|
00000e60  72 20 77 6f 72 6b 69 6e  67 20 77 69 74 68 20 64  |r working with d|
00000e70  6f 75 62 6c 65 20 66 6c  6f 61 74 73 0a 3b 0a 0a  |ouble floats.;..|
00000e80  20 20 20 20 20 20 20 20  45 58 50 4f 52 54 20 20  |        EXPORT  |
00000e90  6c 6e 31 36 0a 0a 6c 6e  6e 73 74 61 20 20 44 43  |ln16..lnnsta  DC|
00000ea0  42 20 20 20 20 20 22 6c  6e 31 36 22 2c 20 30 0a  |B     "ln16", 0.|
00000eb0  20 20 20 20 20 20 20 20  41 4c 49 47 4e 0a 6c 6e  |        ALIGN.ln|
00000ec0  6e 65 6e 64 20 20 44 43  44 20 20 20 20 20 26 66  |nend  DCD     &f|
00000ed0  66 30 30 30 30 30 30 20  2b 20 6c 6e 6e 65 6e 64  |f000000 + lnnend|
00000ee0  20 2d 20 6c 6e 6e 73 74  61 0a 0a 6c 6e 31 36 0a  | - lnnsta..ln16.|
00000ef0  0a 09 43 4d 50 09 61 31  2c 20 23 30 09 09 09 3b  |..CMP.a1, #0...;|
00000f00  69 66 20 61 3c 3d 30 20  72 65 74 75 72 6e 20 6d  |if a<=0 return m|
00000f10  69 6e 31 36 0a 09 4d 4f  56 4c 45 09 61 31 2c 20  |in16..MOVLE.a1, |
00000f20  23 6d 69 6e 31 36 0a 09  4d 4f 56 4c 45 53 09 70  |#min16..MOVLES.p|
00000f30  63 2c 20 6c 72 09 09 09  3b 65 6c 73 65 20 6d 6f  |c, lr...;else mo|
00000f40  73 74 20 73 69 67 6e 69  66 69 63 61 6e 74 20 62  |st significant b|
00000f50  69 74 20 69 73 20 62 65  74 77 65 65 6e 20 62 33  |it is between b3|
00000f60  30 20 26 20 62 30 0a 09  47 42 4c 41 09 63 6f 75  |0 & b0..GBLA.cou|
00000f70  6e 74 65 72 0a 63 6f 75  6e 74 65 72 09 53 45 54  |nter.counter.SET|
00000f80  41 09 33 30 09 09 09 3b  66 69 6e 64 20 6d 73 62  |A.30...;find msb|
00000f90  20 26 20 73 74 6f 72 65  20 28 6d 73 62 5f 69 6e  | & store (msb_in|
00000fa0  64 65 78 20 2d 20 31 35  29 20 69 6e 20 61 34 0a  |dex - 15) in a4.|
00000fb0  09 57 48 49 4c 45 09 63  6f 75 6e 74 65 72 20 3e  |.WHILE.counter >|
00000fc0  20 31 0a 09 54 53 54 09  61 31 2c 20 23 31 3c 3c  | 1..TST.a1, #1<<|
00000fd0  63 6f 75 6e 74 65 72 0a  09 4d 4f 56 4e 45 09 61  |counter..MOVNE.a|
00000fe0  34 2c 20 23 63 6f 75 6e  74 65 72 2d 31 35 0a 09  |4, #counter-15..|
00000ff0  42 4e 45 09 6c 6e 31 36  5f 67 6f 74 6d 73 62 0a  |BNE.ln16_gotmsb.|
00001000  63 6f 75 6e 74 65 72 09  53 45 54 41 09 63 6f 75  |counter.SETA.cou|
00001010  6e 74 65 72 2d 31 0a 09  57 45 4e 44 0a 09 54 53  |nter-1..WEND..TS|
00001020  54 09 61 31 2c 20 23 31  3c 3c 31 0a 09 4d 4f 56  |T.a1, #1<<1..MOV|
00001030  4e 45 09 61 34 2c 20 23  31 2d 31 35 0a 09 4d 4f  |NE.a4, #1-15..MO|
00001040  56 45 51 09 61 34 2c 20  23 30 2d 31 35 0a 6c 6e  |VEQ.a4, #0-15.ln|
00001050  31 36 5f 67 6f 74 6d 73  62 09 09 09 09 3b 69 66  |16_gotmsb....;if|
00001060  20 77 65 20 73 65 74 20  7a 20 3d 20 61 31 2f 28  | we set z = a1/(|
00001070  32 5e 61 34 29 2c 20 77  69 6c 6c 20 68 61 76 65  |2^a4), will have|
00001080  20 6c 6e 28 61 2f 6f 6e  65 29 20 3d 20 6c 6e 28  | ln(a/one) = ln(|
00001090  7a 2f 6f 6e 65 20 2a 20  32 5e 61 34 29 0a 09 43  |z/one * 2^a4)..C|
000010a0  4d 50 09 61 34 2c 20 23  32 09 09 09 3b 09 09 09  |MP.a4, #2...;...|
000010b0  09 09 20 20 20 20 20 20  3d 20 6c 6e 28 7a 2f 6f  |..      = ln(z/o|
000010c0  6e 65 29 20 2b 20 61 34  2a 6c 6e 32 0a 09 53 55  |ne) + a4*ln2..SU|
000010d0  42 47 54 09 61 32 2c 20  61 34 2c 20 23 32 09 09  |BGT.a2, a4, #2..|
000010e0  3b 77 69 74 68 20 7a 20  69 6e 20 72 61 6e 67 65  |;with z in range|
000010f0  20 5b 30 2e 35 2c 31 29  0a 09 4d 4f 56 47 54 09  | [0.5,1)..MOVGT.|
00001100  61 31 2c 20 61 31 2c 20  4c 53 52 20 61 32 09 09  |a1, a1, LSR a2..|
00001110  3b 73 6f 20 63 61 6c 63  20 61 31 20 3d 20 34 2a  |;so calc a1 = 4*|
00001120  28 20 61 31 2f 28 32 5e  61 34 29 20 29 20 2d 20  |( a1/(2^a4) ) - |
00001130  33 2a 6f 6e 65 0a 09 52  53 42 4c 45 09 61 32 2c  |3*one..RSBLE.a2,|
00001140  20 61 34 2c 20 23 32 09  09 3b 77 68 69 63 68 20  | a4, #2..;which |
00001150  70 75 74 73 20 61 31 20  69 6e 20 72 61 6e 67 65  |puts a1 in range|
00001160  20 b1 6f 6e 65 20 73 75  69 74 61 62 6c 65 20 66  | .one suitable f|
00001170  6f 72 20 61 70 70 72 6f  78 69 6d 61 74 69 6f 6e  |or approximation|
00001180  20 62 79 20 70 6f 6c 79  0a 09 4d 4f 56 4c 45 09  | by poly..MOVLE.|
00001190  61 31 2c 20 61 31 2c 20  4c 53 4c 20 61 32 09 09  |a1, a1, LSL a2..|
000011a0  3b 6c 65 61 76 69 6e 67  20 75 73 20 74 6f 20 63  |;leaving us to c|
000011b0  61 6c 63 75 6c 61 74 65  20 61 34 2a 6f 6e 65 2a  |alculate a4*one*|
000011c0  6c 6e 32 20 2b 20 6f 6e  65 2a 6c 6e 28 28 61 31  |ln2 + one*ln((a1|
000011d0  2b 33 6f 6e 65 29 2f 34  6f 6e 65 29 0a 09 53 55  |+3one)/4one)..SU|
000011e0  42 09 61 31 2c 20 61 31  2c 20 23 26 33 30 30 30  |B.a1, a1, #&3000|
000011f0  30 0a 09 41 44 44 09 61  32 2c 20 61 31 2c 20 61  |0..ADD.a2, a1, a|
00001200  31 2c 20 4c 53 4c 20 23  32 09 3b 73 74 61 72 74  |1, LSL #2.;start|
00001210  20 70 6f 6c 79 6e 6f 6d  69 61 6c 20 61 70 70 72  | polynomial appr|
00001220  6f 78 69 6d 61 74 69 6f  6e 20 63 61 6c 63 75 6c  |oximation calcul|
00001230  61 74 69 6f 6e 20 6f 6e  20 61 31 0a 09 52 53 42  |ation on a1..RSB|
00001240  09 61 32 2c 20 61 32 2c  20 61 31 2c 20 4c 53 4c  |.a2, a2, a1, LSL|
00001250  20 23 31 30 0a 09 4d 4f  56 09 61 32 2c 20 61 32  | #10..MOV.a2, a2|
00001260  2c 20 41 53 52 20 23 31  36 09 09 3b 66 6f 72 20  |, ASR #16..;for |
00001270  64 65 74 61 69 6c 73 20  6f 66 20 68 6f 77 20 74  |details of how t|
00001280  68 69 73 20 77 6f 72 6b  73 20 73 65 65 20 63 6f  |his works see co|
00001290  6d 6d 65 6e 74 73 20 61  67 61 69 6e 73 74 20 73  |mments against s|
000012a0  69 6d 69 6c 61 72 20 63  6f 64 65 0a 09 53 55 42  |imilar code..SUB|
000012b0  09 61 32 2c 20 61 32 2c  20 23 26 30 30 30 65 30  |.a2, a2, #&000e0|
000012c0  30 09 3b 69 6e 20 65 78  70 31 36 20 66 75 6e 63  |0.;in exp16 func|
000012d0  74 69 6f 6e 2c 20 64 69  72 65 63 74 6c 79 20 62  |tion, directly b|
000012e0  65 6c 6f 77 20 74 68 69  73 20 66 75 6e 63 74 69  |elow this functi|
000012f0  6f 6e 0a 09 53 55 42 09  61 32 2c 20 61 32 2c 20  |on..SUB.a2, a2, |
00001300  23 26 30 30 30 30 33 34  0a 20 20 20 20 20 20 20  |#&000034.       |
00001310  20 4d 4f 56 09 69 70 2c  20 61 31 0a 09 6d 75 6c  | MOV.ip, a1..mul|
00001320  31 36 63 09 61 32 2c 20  69 70 2c 20 61 32 2c 20  |16c.a2, ip, a2, |
00001330  61 33 0a 09 41 44 44 09  61 32 2c 20 61 32 2c 20  |a3..ADD.a2, a2, |
00001340  23 26 30 30 33 32 30 30  0a 09 41 44 44 09 61 32  |#&003200..ADD.a2|
00001350  2c 20 61 32 2c 20 23 26  30 30 30 30 33 31 0a 20  |, a2, #&000031. |
00001360  20 20 20 20 20 20 20 4d  4f 56 09 69 70 2c 20 61  |       MOV.ip, a|
00001370  31 0a 09 6d 75 6c 31 36  63 09 61 32 2c 20 69 70  |1..mul16c.a2, ip|
00001380  2c 20 61 32 2c 20 61 33  0a 09 53 55 42 09 61 32  |, a2, a3..SUB.a2|
00001390  2c 20 61 32 2c 20 23 26  30 30 65 32 30 30 0a 09  |, a2, #&00e200..|
000013a0  53 55 42 09 61 32 2c 20  61 32 2c 20 23 26 30 30  |SUB.a2, a2, #&00|
000013b0  30 30 66 32 0a 20 20 20  20 20 20 20 20 4d 4f 56  |00f2.        MOV|
000013c0  09 69 70 2c 20 61 31 0a  09 6d 75 6c 31 36 63 09  |.ip, a1..mul16c.|
000013d0  61 32 2c 20 69 70 2c 20  61 32 2c 20 61 33 0a 09  |a2, ip, a2, a3..|
000013e0  41 44 44 09 61 32 2c 20  61 32 2c 20 23 26 30 35  |ADD.a2, a2, #&05|
000013f0  30 30 30 30 0a 09 41 44  44 09 61 32 2c 20 61 32  |0000..ADD.a2, a2|
00001400  2c 20 23 26 30 30 35 35  30 30 0a 09 41 44 44 09  |, #&005500..ADD.|
00001410  61 32 2c 20 61 32 2c 20  23 26 30 30 30 30 36 35  |a2, a2, #&000065|
00001420  0a 20 20 20 20 20 20 20  20 4d 4f 56 09 69 70 2c  |.        MOV.ip,|
00001430  20 61 31 0a 09 6d 75 6c  31 36 63 09 61 32 2c 20  | a1..mul16c.a2, |
00001440  69 70 2c 20 61 32 2c 20  61 33 0a 09 53 55 42 09  |ip, a2, a3..SUB.|
00001450  61 32 2c 20 61 32 2c 20  23 26 30 34 30 30 30 30  |a2, a2, #&040000|
00001460  0a 09 53 55 42 09 61 32  2c 20 61 32 2c 20 23 26  |..SUB.a2, a2, #&|
00001470  30 30 39 61 30 30 09 3b  65 6e 64 20 6f 66 20 70  |009a00.;end of p|
00001480  6f 6c 79 6e 6f 6d 69 61  6c 20 61 70 70 72 6f 78  |olynomial approx|
00001490  69 6d 61 74 69 6f 6e 20  63 61 6c 63 75 6c 61 74  |imation calculat|
000014a0  69 6f 6e 0a 09 53 55 42  09 61 31 2c 20 61 32 2c  |ion..SUB.a1, a2,|
000014b0  20 23 26 30 30 30 30 35  39 09 3b 61 31 20 6e 6f  | #&000059.;a1 no|
000014c0  77 20 68 6f 6c 64 73 20  28 32 5e 32 30 29 2a 28  |w holds (2^20)*(|
000014d0  61 70 70 72 6f 78 69 6d  61 74 65 64 20 76 61 6c  |approximated val|
000014e0  75 65 29 0a 09 41 44 44  09 69 70 2c 20 61 34 2c  |ue)..ADD.ip, a4,|
000014f0  20 61 34 2c 20 4c 53 4c  20 23 31 0a 09 41 44 44  | a4, LSL #1..ADD|
00001500  09 61 32 2c 20 69 70 2c  20 61 34 2c 20 4c 53 4c  |.a2, ip, a4, LSL|
00001510  20 23 33 09 3b 6e 6f 77  20 61 64 64 20 69 6e 20  | #3.;now add in |
00001520  74 68 65 20 61 34 2a 6f  6e 65 2a 6c 6e 32 20 74  |the a4*one*ln2 t|
00001530  65 72 6d 20 75 73 69 6e  67 20 61 6e 20 65 78 70  |erm using an exp|
00001540  6c 69 63 69 74 20 62 69  6e 61 72 79 20 65 78 70  |licit binary exp|
00001550  61 6e 73 69 6f 6e 0a 09  41 44 44 09 61 32 2c 20  |ansion..ADD.a2, |
00001560  61 34 2c 20 61 32 2c 20  4c 53 4c 20 23 34 09 3b  |a4, a2, LSL #4.;|
00001570  6f 66 20 61 70 70 72 6f  78 69 6d 61 74 65 6c 79  |of approximately|
00001580  20 6f 6e 65 2a 6c 6e 32  0a 09 52 53 42 09 61 33  | one*ln2..RSB.a3|
00001590  2c 20 61 34 2c 20 61 34  2c 20 4c 53 4c 20 23 33  |, a4, a4, LSL #3|
000015a0  0a 09 41 44 44 09 61 32  2c 20 61 33 2c 20 61 32  |..ADD.a2, a3, a2|
000015b0  2c 20 4c 53 4c 20 23 34  09 3b 6e 62 20 77 65 20  |, LSL #4.;nb we |
000015c0  61 63 74 75 61 6c 6c 79  20 61 64 64 20 61 70 70  |actually add app|
000015d0  72 6f 78 20 28 32 5e 32  30 29 2a 6c 6e 32 2c 20  |rox (2^20)*ln2, |
000015e0  26 20 74 68 65 6e 20 64  69 76 69 64 65 20 62 79  |& then divide by|
000015f0  20 31 36 0a 09 41 44 44  09 61 32 2c 20 61 34 2c  | 16..ADD.a2, a4,|
00001600  20 61 32 2c 20 4c 53 4c  20 23 33 09 3b 74 6f 20  | a2, LSL #3.;to |
00001610  72 65 64 75 63 65 20 74  72 75 6e 63 61 74 69 6f  |reduce truncatio|
00001620  6e 20 65 72 72 6f 72 0a  09 41 44 44 09 61 31 2c  |n error..ADD.a1,|
00001630  20 61 31 2c 20 61 32 2c  20 4c 53 4c 20 23 35 0a  | a1, a2, LSL #5.|
00001640  09 41 44 44 09 61 31 2c  20 61 31 2c 20 69 70 2c  |.ADD.a1, a1, ip,|
00001650  20 41 53 52 20 23 31 0a  09 4d 4f 56 09 61 31 2c  | ASR #1..MOV.a1,|
00001660  20 61 31 2c 20 41 53 52  20 23 34 0a 20 20 20 20  | a1, ASR #4.    |
00001670  20 20 20 20 4d 4f 56 53  20 20 20 20 70 63 2c 20  |    MOVS    pc, |
00001680  6c 72 0a 0a 0a 0a 3b 20  65 78 70 31 36 0a 3b 20  |lr....; exp16.; |
00001690  61 20 6c 65 61 66 20 41  50 43 53 20 66 75 6e 63  |a leaf APCS func|
000016a0  74 69 6f 6e 0a 3b 0a 3b  20 43 20 70 72 6f 74 6f  |tion.;.; C proto|
000016b0  74 79 70 65 3a 0a 3b 20  69 6e 74 20 65 78 70 31  |type:.; int exp1|
000016c0  36 28 69 6e 74 20 61 29  0a 3b 0a 3b 20 72 65 74  |6(int a).;.; ret|
000016d0  75 72 6e 73 20 36 35 35  33 36 20 2a 20 65 78 70  |urns 65536 * exp|
000016e0  20 28 61 2f 36 35 35 33  36 29 0a 3b 0a 3b 20 69  | (a/65536).;.; i|
000016f0  73 20 61 62 6f 75 74 20  34 35 20 74 69 6d 65 73  |s about 45 times|
00001700  20 66 61 73 74 65 72 20  74 68 61 6e 20 46 50 45  | faster than FPE|
00001710  6d 75 6c 61 74 6f 72 20  77 6f 72 6b 69 6e 67 20  |mulator working |
00001720  77 69 74 68 20 64 6f 75  62 6c 65 20 66 6c 6f 61  |with double floa|
00001730  74 73 0a 3b 0a 0a 20 20  20 20 20 20 20 20 45 58  |ts.;..        EX|
00001740  50 4f 52 54 20 20 65 78  70 31 36 0a 0a 65 70 6e  |PORT  exp16..epn|
00001750  73 74 61 20 20 44 43 42  20 20 20 20 20 22 65 78  |sta  DCB     "ex|
00001760  70 31 36 22 2c 20 30 0a  20 20 20 20 20 20 20 20  |p16", 0.        |
00001770  41 4c 49 47 4e 0a 65 70  6e 65 6e 64 20 20 44 43  |ALIGN.epnend  DC|
00001780  44 20 20 20 20 20 26 66  66 30 30 30 30 30 30 20  |D     &ff000000 |
00001790  2b 20 65 70 6e 65 6e 64  20 2d 20 65 70 6e 73 74  |+ epnend - epnst|
000017a0  61 0a 0a 65 78 70 31 36  09 09 09 09 09 3b 74 68  |a..exp16.....;th|
000017b0  69 73 20 66 6e 20 72 65  74 75 72 6e 73 20 61 20  |is fn returns a |
000017c0  76 61 6c 75 65 20 77 69  74 68 20 6f 6e 6c 79 20  |value with only |
000017d0  61 62 6f 75 74 20 31 37  20 73 69 67 6e 69 66 69  |about 17 signifi|
000017e0  63 61 6e 74 20 62 69 6e  61 72 79 0a 09 09 09 09  |cant binary.....|
000017f0  09 3b 64 69 67 69 74 73  20 2d 20 65 67 20 66 6f  |.;digits - eg fo|
00001800  72 20 27 61 27 20 73 6d  61 6c 6c 20 6f 72 20 6e  |r 'a' small or n|
00001810  65 67 61 74 69 76 65 2c  20 67 65 74 20 6e 65 61  |egative, get nea|
00001820  72 20 6d 61 78 69 6d 61  6c 20 61 63 63 75 72 61  |r maximal accura|
00001830  63 79 0a 09 09 09 09 09  3b 62 75 74 20 66 6f 72  |cy......;but for|
00001840  20 27 61 27 20 6c 61 72  67 65 20 28 75 70 74 6f  | 'a' large (upto|
00001850  20 61 72 6f 75 6e 64 20  31 30 6f 6e 65 29 20 77  | around 10one) w|
00001860  68 65 72 65 20 65 78 70  20 68 61 73 20 76 61 6c  |here exp has val|
00001870  75 65 0a 09 43 4d 50 09  61 31 2c 20 23 31 32 2a  |ue..CMP.a1, #12*|
00001880  36 35 35 33 36 09 09 3b  7e 32 30 30 30 30 6f 6e  |65536..;~20000on|
00001890  65 20 63 61 6e 20 67 65  74 20 65 72 72 6f 72 20  |e can get error |
000018a0  75 70 74 6f 20 72 6f 75  67 68 6c 79 20 30 2e 31  |upto roughly 0.1|
000018b0  6f 6e 65 0a 09 4d 4f 56  47 54 09 61 31 2c 20 23  |one..MOVGT.a1, #|
000018c0  6d 61 78 31 36 2b 31 0a  09 53 55 42 47 54 09 61  |max16+1..SUBGT.a|
000018d0  31 2c 20 61 31 2c 20 23  31 0a 09 4d 4f 56 47 54  |1, a1, #1..MOVGT|
000018e0  53 09 70 63 2c 20 6c 72  0a 09 43 4d 50 09 61 31  |S.pc, lr..CMP.a1|
000018f0  2c 20 23 2d 31 32 2a 36  35 35 33 36 0a 09 4d 4f  |, #-12*65536..MO|
00001900  56 4c 54 09 61 31 2c 20  23 30 0a 09 4d 4f 56 4c  |VLT.a1, #0..MOVL|
00001910  54 53 09 70 63 2c 20 6c  72 09 09 09 3b 6f 74 68  |TS.pc, lr...;oth|
00001920  65 72 77 69 73 65 20 6b  6e 6f 77 20 7c 61 7c 20  |erwise know |a| |
00001930  3c 3d 20 31 32 6f 6e 65  0a 09 52 53 42 09 61 32  |<= 12one..RSB.a2|
00001940  2c 20 61 31 2c 20 61 31  2c 20 4c 53 4c 20 23 33  |, a1, a1, LSL #3|
00001950  09 3b 6e 6f 77 20 73 65  74 20 61 20 3d 20 61 2f  |.;now set a = a/|
00001960  6c 6e 32 20 75 73 69 6e  67 20 65 78 70 6c 69 63  |ln2 using explic|
00001970  69 74 20 6d 75 6c 20 62  79 20 32 5f 31 2e 30 31  |it mul by 2_1.01|
00001980  31 31 30 30 30 31 30 31  30 31 30 31 30 30 30 31  |1100010101010001|
00001990  31 31 0a 09 41 44 44 09  61 33 2c 20 61 31 2c 20  |11..ADD.a3, a1, |
000019a0  61 31 2c 20 4c 53 4c 20  23 32 09 3b 6e 62 20 6f  |a1, LSL #2.;nb o|
000019b0  6e 6c 79 20 6e 65 65 64  20 31 73 74 20 32 30 20  |nly need 1st 20 |
000019c0  64 69 67 69 74 73 20 67  69 76 65 6e 20 72 61 6e  |digits given ran|
000019d0  67 65 20 6f 6e 20 61 0a  09 41 44 44 09 61 33 2c  |ge on a..ADD.a3,|
000019e0  20 61 33 2c 20 61 33 2c  20 4c 53 4c 20 23 34 09  | a3, a3, LSL #4.|
000019f0  3b 6e 62 32 20 74 68 69  73 20 63 61 6c 63 20 69  |;nb2 this calc i|
00001a00  73 20 61 70 70 72 6f 78  69 6d 61 74 65 20 6f 6e  |s approximate on|
00001a10  6c 79 2c 20 74 68 6f 75  67 68 20 77 69 6c 6c 20  |ly, though will |
00001a20  75 73 75 61 6c 6c 79 20  79 69 65 6c 64 20 61 6e  |usually yield an|
00001a30  0a 09 41 44 44 09 61 31  2c 20 61 32 2c 20 61 31  |..ADD.a1, a2, a1|
00001a40  2c 20 4c 53 4c 20 23 34  09 3b 61 6e 73 77 65 72  |, LSL #4.;answer|
00001a50  20 63 6f 72 72 65 63 74  20 74 6f 20 74 68 65 20  | correct to the |
00001a60  73 74 6f 72 65 64 20 31  36 20 62 69 6e 61 72 79  |stored 16 binary|
00001a70  20 70 6c 61 63 65 73 20  28 65 6c 73 65 20 65 72  | places (else er|
00001a80  72 6f 72 20 6f 6e 65 2f  36 35 35 33 36 29 0a 09  |ror one/65536)..|
00001a90  41 44 44 09 61 31 2c 20  61 31 2c 20 61 33 2c 20  |ADD.a1, a1, a3, |
00001aa0  41 53 52 20 23 31 30 0a  09 41 44 44 09 61 31 2c  |ASR #10..ADD.a1,|
00001ab0  20 61 31 2c 20 61 32 2c  20 41 53 52 20 23 31 36  | a1, a2, ASR #16|
00001ac0  09 3b 74 68 75 73 20 65  78 70 28 6f 72 69 67 69  |.;thus exp(origi|
00001ad0  6e 61 6c 20 61 29 20 20  3d 20 65 78 70 28 6e 65  |nal a)  = exp(ne|
00001ae0  77 20 61 20 2a 20 6c 6e  32 29 20 3d 20 32 5e 28  |w a * ln2) = 2^(|
00001af0  6e 65 77 20 61 29 2c 20  65 76 61 6c 27 64 20 62  |new a), eval'd b|
00001b00  65 6c 6f 77 3a 0a 09 41  44 44 09 61 31 2c 20 61  |elow:..ADD.a1, a|
00001b10  31 2c 20 23 38 0a 09 4d  4f 56 09 61 31 2c 20 61  |1, #8..MOV.a1, a|
00001b20  31 2c 20 41 53 52 20 23  34 09 09 3b 63 61 6c 63  |1, ASR #4..;calc|
00001b30  20 61 20 3d 20 61 2f 6c  6e 32 20 63 6f 6d 70 6c  | a = a/ln2 compl|
00001b40  65 74 65 0a 09 4d 4f 56  09 61 34 2c 20 61 31 2c  |ete..MOV.a4, a1,|
00001b50  20 41 53 52 20 23 31 36  09 09 3b 61 34 20 6e 6f  | ASR #16..;a4 no|
00001b60  77 20 68 6f 6c 64 73 20  69 6e 74 65 67 65 72 20  |w holds integer |
00001b70  63 6f 6d 70 6f 6e 65 6e  74 20 6f 66 20 61 0a 09  |component of a..|
00001b80  42 49 43 09 61 31 2c 20  61 31 2c 20 61 34 2c 20  |BIC.a1, a1, a4, |
00001b90  4c 53 4c 20 23 31 36 09  3b 61 31 20 6e 6f 77 20  |LSL #16.;a1 now |
00001ba0  68 6f 6c 64 73 20 66 72  61 63 74 69 6f 6e 61 6c  |holds fractional|
00001bb0  20 63 6f 6d 70 6f 6e 65  6e 74 20 69 6e 20 72 61  | component in ra|
00001bc0  6e 67 65 20 5b 30 2c 31  29 2a 6f 6e 65 0a 09 43  |nge [0,1)*one..C|
00001bd0  4d 50 09 61 34 2c 20 23  31 35 09 09 09 3b 64 6f  |MP.a4, #15...;do|
00001be0  20 61 6e 6f 74 68 65 72  20 63 68 65 63 6b 20 6f  | another check o|
00001bf0  6e 20 61 20 74 6f 20 65  6e 73 75 72 65 20 65 78  |n a to ensure ex|
00001c00  70 28 61 29 20 69 73 20  69 6e 20 72 61 6e 67 65  |p(a) is in range|
00001c10  0a 09 4d 4f 56 47 45 09  61 31 2c 20 23 6d 61 78  |..MOVGE.a1, #max|
00001c20  31 36 2b 31 09 09 3b 26  20 69 66 20 6e 6f 74 20  |16+1..;& if not |
00001c30  72 65 74 75 72 6e 20 6d  61 78 69 6d 61 6c 20 6f  |return maximal o|
00001c40  72 20 6d 69 6e 69 6d 61  6c 20 76 61 6c 75 65 20  |r minimal value |
00001c50  61 73 20 61 70 70 72 6f  70 72 69 61 74 65 0a 09  |as appropriate..|
00001c60  53 55 42 47 45 09 61 31  2c 20 61 31 2c 20 23 31  |SUBGE.a1, a1, #1|
00001c70  0a 09 4d 4f 56 47 45 09  70 63 2c 20 6c 72 0a 09  |..MOVGE.pc, lr..|
00001c80  43 4d 50 09 61 34 2c 20  23 2d 31 36 0a 09 4d 4f  |CMP.a4, #-16..MO|
00001c90  56 4c 54 09 61 31 2c 20  23 30 0a 09 4d 4f 56 4c  |VLT.a1, #0..MOVL|
00001ca0  54 53 09 70 63 2c 20 6c  72 09 09 09 3b 65 6c 73  |TS.pc, lr...;els|
00001cb0  65 20 6e 65 65 64 20 74  6f 20 72 65 74 75 72 6e  |e need to return|
00001cc0  20 76 61 6c 75 65 20 28  6f 6e 65 20 2a 20 32 5e  | value (one * 2^|
00001cd0  28 61 34 20 2b 20 61 31  2f 6f 6e 65 29 29 0a 09  |(a4 + a1/one))..|
00001ce0  4d 4f 56 09 61 31 2c 20  61 31 2c 20 4c 53 4c 20  |MOV.a1, a1, LSL |
00001cf0  23 31 09 09 3b 09 09 09  20 3d 20 28 6f 6e 65 20  |#1..;... = (one |
00001d00  2a 20 32 5e 28 61 31 2f  6f 6e 65 29 20 2a 20 32  |* 2^(a1/one) * 2|
00001d10  5e 61 34 29 0a 09 53 55  42 09 61 31 2c 20 61 31  |^a4)..SUB.a1, a1|
00001d20  2c 20 23 26 31 30 30 30  30 09 09 3b 63 6f 6e 76  |, #&10000..;conv|
00001d30  65 72 74 20 61 31 20 74  6f 20 6c 69 65 20 69 6e  |ert a1 to lie in|
00001d40  20 5b 2d 31 2c 2d 31 29  2c 20 74 68 65 6e 20 61  | [-1,-1), then a|
00001d50  70 70 6c 79 20 70 6f 6c  79 20 61 70 70 72 6f 78  |pply poly approx|
00001d60  69 6d 61 74 69 6f 6e 3a  0a 09 52 53 42 09 61 32  |imation:..RSB.a2|
00001d70  2c 20 61 31 2c 20 61 31  2c 20 4c 53 4c 20 23 33  |, a1, a1, LSL #3|
00001d80  0a 09 41 44 44 09 61 32  2c 20 61 31 2c 20 61 32  |..ADD.a2, a1, a2|
00001d90  2c 20 4c 53 4c 20 23 36  0a 09 4d 4f 56 09 61 32  |, LSL #6..MOV.a2|
00001da0  2c 20 61 32 2c 20 41 53  52 20 23 31 35 09 09 3b  |, a2, ASR #15..;|
00001db0  61 32 20 3d 20 30 2e 30  30 30 38 35 36 31 2a 28  |a2 = 0.0008561*(|
00001dc0  32 5e 32 30 29 2a 28 61  31 2f 6f 6e 65 29 0a 09  |2^20)*(a1/one)..|
00001dd0  41 44 44 09 61 32 2c 20  61 32 2c 20 23 26 30 30  |ADD.a2, a2, #&00|
00001de0  32 38 30 30 0a 09 41 44  44 09 61 32 2c 20 61 32  |2800..ADD.a2, a2|
00001df0  2c 20 23 26 30 30 30 30  37 65 09 3b 61 32 20 3d  |, #&00007e.;a2 =|
00001e00  20 30 2e 30 30 30 38 35  36 31 2a 28 32 5e 32 30  | 0.0008561*(2^20|
00001e10  29 2a 28 61 31 2f 6f 6e  65 29 20 2b 20 30 2e 30  |)*(a1/one) + 0.0|
00001e20  30 39 38 38 35 37 2a 28  32 5e 32 30 29 0a 20 20  |098857*(2^20).  |
00001e30  20 20 20 20 20 20 4d 4f  56 09 69 70 2c 20 61 31  |      MOV.ip, a1|
00001e40  0a 09 6d 75 6c 31 36 63  09 61 32 2c 20 69 70 2c  |..mul16c.a2, ip,|
00001e50  20 61 32 2c 20 61 33 09  09 3b 61 32 20 3d 20 30  | a2, a3..;a2 = 0|
00001e60  2e 30 30 30 38 35 36 31  2a 28 32 5e 32 30 29 2a  |.0008561*(2^20)*|
00001e70  28 61 31 2f 6f 6e 65 29  5e 32 20 2b 20 30 2e 30  |(a1/one)^2 + 0.0|
00001e80  30 39 38 38 35 37 2a 28  32 5e 32 30 29 2a 28 61  |098857*(2^20)*(a|
00001e90  31 2f 6f 6e 65 29 0a 09  41 44 44 09 61 32 2c 20  |1/one)..ADD.a2, |
00001ea0  61 32 2c 20 23 26 30 31  35 30 30 30 0a 09 41 44  |a2, #&015000..AD|
00001eb0  44 09 61 32 2c 20 61 32  2c 20 23 26 30 30 30 62  |D.a2, a2, #&000b|
00001ec0  65 30 09 3b 20 65 74 63  0a 20 20 20 20 20 20 20  |e0.; etc.       |
00001ed0  20 4d 4f 56 09 69 70 2c  20 61 31 0a 09 6d 75 6c  | MOV.ip, a1..mul|
00001ee0  31 36 63 09 61 32 2c 20  69 70 2c 20 61 32 2c 20  |16c.a2, ip, a2, |
00001ef0  61 33 0a 09 41 44 44 09  61 32 2c 20 61 32 2c 20  |a3..ADD.a2, a2, |
00001f00  23 26 30 37 30 30 30 30  0a 09 41 44 44 09 61 32  |#&070000..ADD.a2|
00001f10  2c 20 61 32 2c 20 23 26  30 30 64 37 30 30 0a 09  |, a2, #&00d700..|
00001f20  41 44 44 09 61 32 2c 20  61 32 2c 20 23 26 30 30  |ADD.a2, a2, #&00|
00001f30  30 30 37 65 0a 20 20 20  20 20 20 20 20 4d 4f 56  |007e.        MOV|
00001f40  09 69 70 2c 20 61 31 0a  09 6d 75 6c 31 36 63 09  |.ip, a1..mul16c.|
00001f50  61 32 2c 20 69 70 2c 20  61 32 2c 20 61 33 09 09  |a2, ip, a2, a3..|
00001f60  3b 75 6e 74 69 6c 20 77  65 20 68 61 76 65 0a 09  |;until we have..|
00001f70  41 44 44 09 61 32 2c 20  61 32 2c 20 23 26 31 36  |ADD.a2, a2, #&16|
00001f80  30 30 30 30 09 3b 61 32  20 3d 20 28 32 5e 32 30  |0000.;a2 = (2^20|
00001f90  29 20 28 20 30 2e 30 30  30 38 35 36 31 28 61 31  |) ( 0.0008561(a1|
00001fa0  2f 6f 6e 65 29 5e 34 20  2b 20 30 2e 30 30 39 38  |/one)^4 + 0.0098|
00001fb0  38 35 37 28 61 31 2f 6f  6e 65 29 5e 33 20 2b 0a  |857(a1/one)^3 +.|
00001fc0  09 41 44 44 09 61 32 2c  20 61 32 2c 20 23 26 30  |.ADD.a2, a2, #&0|
00001fd0  30 61 30 30 30 09 3b 09  20 20 20 20 20 20 20 30  |0a000.;.       0|
00001fe0  2e 30 38 34 39 33 30 31  28 61 31 2f 6f 6e 65 29  |.0849301(a1/one)|
00001ff0  5e 32 20 2b 20 30 2e 34  39 30 31 31 30 36 28 61  |^2 + 0.4901106(a|
00002000  31 2f 6f 6e 65 29 20 2b  0a 09 41 44 44 09 61 32  |1/one) +..ADD.a2|
00002010  2c 20 61 32 2c 20 23 26  30 30 30 30 61 37 09 3b  |, a2, #&0000a7.;|
00002020  09 20 20 20 20 20 20 20  31 2e 31 34 31 34 32 31  |.       1.141421|
00002030  33 38 28 61 31 2f 6f 6e  65 29 20 20 20 20 20 20  |38(a1/one)      |
00002040  20 20 20 20 20 20 20 20  20 20 20 20 20 20 20 20  |                |
00002050  20 20 20 20 20 29 0a 09  4d 4f 56 09 61 31 2c 20  |     )..MOV.a1, |
00002060  61 32 2c 20 41 53 52 20  23 34 09 09 3b 6e 6f 77  |a2, ASR #4..;now|
00002070  20 64 69 76 69 64 65 20  62 79 20 31 36 20 72 65  | divide by 16 re|
00002080  6d 6f 76 69 6e 67 20 6d  6f 73 74 20 74 72 75 6e  |moving most trun|
00002090  63 61 74 69 6f 6e 20 65  72 72 6f 72 20 66 72 6f  |cation error fro|
000020a0  6d 20 61 62 6f 76 65 20  63 61 6c 63 73 2c 0a 09  |m above calcs,..|
000020b0  43 4d 50 09 61 34 2c 20  23 30 09 09 09 3b 6c 65  |CMP.a4, #0...;le|
000020c0  61 76 69 6e 67 20 61 32  20 3d 20 28 32 5e 31 36  |aving a2 = (2^16|
000020d0  29 2a 28 61 70 70 72 6f  78 69 6d 61 74 65 64 20  |)*(approximated |
000020e0  76 61 6c 75 65 29 0a 09  52 53 42 4c 54 09 61 34  |value)..RSBLT.a4|
000020f0  2c 20 61 34 2c 20 23 30  0a 09 4d 4f 56 4c 54 09  |, a4, #0..MOVLT.|
00002100  61 31 2c 20 61 31 2c 20  41 53 52 20 61 34 09 09  |a1, a1, ASR a4..|
00002110  3b 66 69 6e 61 6c 6c 79  20 63 61 72 72 79 20 6f  |;finally carry o|
00002120  75 74 20 74 68 65 20 28  61 32 20 3d 20 61 32 20  |ut the (a2 = a2 |
00002130  2a 20 32 5e 61 34 29 20  73 74 65 70 0a 09 4d 4f  |* 2^a4) step..MO|
00002140  56 4c 54 53 09 70 63 2c  20 6c 72 0a 09 4d 4f 56  |VLTS.pc, lr..MOV|
00002150  53 09 61 31 2c 20 61 31  2c 20 4c 53 4c 20 61 34  |S.a1, a1, LSL a4|
00002160  0a 09 4d 4f 56 4d 49 09  61 31 2c 20 23 6d 61 78  |..MOVMI.a1, #max|
00002170  31 36 2b 31 0a 09 53 55  42 4d 49 09 61 31 2c 20  |16+1..SUBMI.a1, |
00002180  61 31 2c 20 23 31 0a 20  20 20 20 20 20 20 20 4d  |a1, #1.        M|
00002190  4f 56 53 20 20 20 20 70  63 2c 20 6c 72 0a 0a 0a  |OVS    pc, lr...|
000021a0  0a 3b 20 63 6f 73 31 36  0a 3b 20 61 20 6c 65 61  |.; cos16.; a lea|
000021b0  66 20 41 50 43 53 20 66  75 6e 63 74 69 6f 6e 0a  |f APCS function.|
000021c0  3b 0a 3b 20 43 20 70 72  6f 74 6f 74 79 70 65 3a  |;.; C prototype:|
000021d0  0a 3b 20 69 6e 74 20 63  6f 73 31 36 28 69 6e 74  |.; int cos16(int|
000021e0  20 61 29 0a 3b 0a 3b 20  72 65 74 75 72 6e 73 20  | a).;.; returns |
000021f0  36 35 35 33 36 20 2a 20  63 6f 73 20 28 28 61 2f  |65536 * cos ((a/|
00002200  36 35 35 33 36 29 28 70  69 2f 32 29 29 0a 3b 0a  |65536)(pi/2)).;.|
00002210  3b 20 69 73 20 61 62 6f  75 74 20 34 34 20 74 69  |; is about 44 ti|
00002220  6d 65 73 20 66 61 73 74  65 72 20 74 68 61 6e 20  |mes faster than |
00002230  46 50 45 6d 75 6c 61 74  6f 72 20 77 6f 72 6b 69  |FPEmulator worki|
00002240  6e 67 20 77 69 74 68 20  64 6f 75 62 6c 65 20 66  |ng with double f|
00002250  6c 6f 61 74 73 0a 3b 0a  0a 20 20 20 20 20 20 20  |loats.;..       |
00002260  20 45 58 50 4f 52 54 20  20 63 6f 73 31 36 0a 0a  | EXPORT  cos16..|
00002270  63 73 6e 73 74 61 20 20  44 43 42 20 20 20 20 20  |csnsta  DCB     |
00002280  22 63 6f 73 31 36 22 2c  20 30 0a 20 20 20 20 20  |"cos16", 0.     |
00002290  20 20 20 41 4c 49 47 4e  0a 63 73 6e 65 6e 64 20  |   ALIGN.csnend |
000022a0  20 44 43 44 20 20 20 20  20 26 66 66 30 30 30 30  | DCD     &ff0000|
000022b0  30 30 20 2b 20 63 73 6e  65 6e 64 20 2d 20 63 73  |00 + csnend - cs|
000022c0  6e 73 74 61 0a 0a 63 6f  73 31 36 0a 0a 09 45 4f  |nsta..cos16...EO|
000022d0  52 09 61 34 2c 20 61 31  2c 20 61 31 2c 20 4c 53  |R.a4, a1, a1, LS|
000022e0  4c 20 23 31 09 3b 73 69  6e 63 65 20 63 6f 73 20  |L #1.;since cos |
000022f0  69 73 20 70 65 72 69 6f  64 69 63 20 61 6e 64 20  |is periodic and |
00002300  63 6f 73 28 28 61 2f 36  35 35 33 36 29 28 70 69  |cos((a/65536)(pi|
00002310  2f 32 29 29 20 68 61 73  20 70 65 72 69 6f 64 20  |/2)) has period |
00002320  34 2a 6f 6e 65 0a 09 54  53 54 09 61 34 2c 20 23  |4*one..TST.a4, #|
00002330  26 32 30 30 30 30 09 09  3b 65 76 61 6c 20 69 73  |&20000..;eval is|
00002340  20 73 69 6d 70 6c 65 72  20 74 68 61 6e 20 66 6f  | simpler than fo|
00002350  72 20 6c 6e 20 6f 72 20  65 78 70 3a 0a 09 4d 4f  |r ln or exp:..MO|
00002360  56 09 61 34 2c 20 23 30  09 09 09 3b 77 65 20 6e  |V.a4, #0...;we n|
00002370  65 65 64 20 6f 6e 6c 79  20 74 6f 20 74 72 75 6e  |eed only to trun|
00002380  63 61 74 65 20 61 20 74  6f 20 5b 30 2c 34 6f 6e  |cate a to [0,4on|
00002390  65 29 20 61 6e 64 20 74  68 65 6e 20 61 70 70 72  |e) and then appr|
000023a0  6f 78 20 74 68 65 20 66  75 6e 63 74 69 6f 6e 0a  |ox the function.|
000023b0  09 4d 4f 56 4e 45 09 61  34 2c 20 23 31 09 09 09  |.MOVNE.a4, #1...|
000023c0  3b 76 69 61 20 61 20 70  6f 6c 79 0a 09 54 53 54  |;via a poly..TST|
000023d0  09 61 31 2c 20 23 26 31  30 30 30 30 0a 09 4d 4f  |.a1, #&10000..MO|
000023e0  56 09 61 31 2c 20 61 31  2c 20 4c 53 4c 20 23 31  |V.a1, a1, LSL #1|
000023f0  36 09 09 3b 69 6e 20 66  61 63 74 20 66 75 72 74  |6..;in fact furt|
00002400  68 65 72 20 73 79 6d 6d  65 74 72 69 65 73 20 69  |her symmetries i|
00002410  6e 20 63 6f 73 20 6f 76  65 72 20 74 68 69 73 20  |n cos over this |
00002420  72 61 6e 67 65 20 61 6c  6c 6f 77 20 75 73 20 74  |range allow us t|
00002430  6f 0a 09 4d 4f 56 09 61  31 2c 20 61 31 2c 20 4c  |o..MOV.a1, a1, L|
00002440  53 52 20 23 31 36 09 09  3b 61 63 74 75 61 6c 6c  |SR #16..;actuall|
00002450  79 20 6f 6e 6c 79 20 61  70 70 72 6f 78 69 6d 61  |y only approxima|
00002460  74 65 20 66 75 6e 63 74  69 6f 6e 20 6f 76 65 72  |te function over|
00002470  20 72 61 6e 67 65 20 5b  30 2c 6f 6e 65 29 0a 09  | range [0,one)..|
00002480  52 53 42 45 51 09 61 31  2c 20 61 31 2c 20 23 26  |RSBEQ.a1, a1, #&|
00002490  31 30 30 30 30 09 09 3b  61 6e 64 20 72 65 63 6f  |10000..;and reco|
000024a0  6e 73 74 72 75 63 74 20  69 74 20 6f 76 65 72 20  |nstruct it over |
000024b0  72 65 6d 61 69 6e 64 65  72 20 6f 66 20 72 61 6e  |remainder of ran|
000024c0  67 65 20 5b 6f 6e 65 2c  34 6f 6e 65 29 20 66 72  |ge [one,4one) fr|
000024d0  6f 6d 20 74 68 61 74 20  70 61 72 74 0a 09 4d 4f  |om that part..MO|
000024e0  56 09 61 31 2c 20 61 31  2c 20 4c 53 4c 20 23 31  |V.a1, a1, LSL #1|
000024f0  09 09 3b 65 67 20 20 63  6f 73 28 20 28 61 2f 36  |..;eg  cos( (a/6|
00002500  35 35 33 36 29 28 70 69  2f 32 29 20 29 20 66 6f  |5536)(pi/2) ) fo|
00002510  72 20 61 20 69 6e 20 5b  32 6f 6e 65 2c 20 33 6f  |r a in [2one, 3o|
00002520  6e 65 29 0a 09 53 55 42  09 61 31 2c 20 61 31 2c  |ne)..SUB.a1, a1,|
00002530  20 23 26 31 30 30 30 30  09 09 3b 20 3d 20 2d 63  | #&10000..; = -c|
00002540  6f 73 28 20 28 28 61 2d  32 6f 6e 65 29 2f 36 35  |os( ((a-2one)/65|
00002550  35 33 36 29 28 70 69 2f  32 29 20 29 0a 09 52 53  |536)(pi/2) )..RS|
00002560  42 09 61 32 2c 20 61 31  2c 20 61 31 2c 20 4c 53  |B.a2, a1, a1, LS|
00002570  4c 20 23 33 0a 09 41 44  44 09 61 32 2c 20 61 31  |L #3..ADD.a2, a1|
00002580  2c 20 61 32 2c 20 4c 53  4c 20 23 38 0a 09 4d 4f  |, a2, LSL #8..MO|
00002590  56 09 61 32 2c 20 61 32  2c 20 41 53 52 20 23 31  |V.a2, a2, ASR #1|
000025a0  36 0a 09 41 44 44 09 61  32 2c 20 61 32 2c 20 23  |6..ADD.a2, a2, #|
000025b0  26 30 30 32 63 30 30 0a  09 41 44 44 09 61 32 2c  |&002c00..ADD.a2,|
000025c0  20 61 32 2c 20 23 26 30  30 30 30 38 36 0a 20 20  | a2, #&000086.  |
000025d0  20 20 20 20 20 20 4d 4f  56 09 69 70 2c 20 61 31  |      MOV.ip, a1|
000025e0  0a 09 6d 75 6c 31 36 63  09 61 32 2c 20 69 70 2c  |..mul16c.a2, ip,|
000025f0  20 61 32 2c 20 61 33 0a  09 53 55 42 09 61 32 2c  | a2, a3..SUB.a2,|
00002600  20 61 32 2c 20 23 26 30  30 65 39 30 30 0a 09 53  | a2, #&00e900..S|
00002610  55 42 09 61 32 2c 20 61  32 2c 20 23 26 30 30 30  |UB.a2, a2, #&000|
00002620  30 62 64 0a 20 20 20 20  20 20 20 20 4d 4f 56 09  |0bd.        MOV.|
00002630  69 70 2c 20 61 31 0a 09  6d 75 6c 31 36 63 09 61  |ip, a1..mul16c.a|
00002640  32 2c 20 69 70 2c 20 61  32 2c 20 61 33 0a 09 53  |2, ip, a2, a3..S|
00002650  55 42 09 61 32 2c 20 61  32 2c 20 23 26 30 33 30  |UB.a2, a2, #&030|
00002660  30 30 30 0a 09 53 55 42  09 61 32 2c 20 61 32 2c  |000..SUB.a2, a2,|
00002670  20 23 26 30 30 37 63 30  30 0a 09 53 55 42 09 61  | #&007c00..SUB.a|
00002680  32 2c 20 61 32 2c 20 23  26 30 30 30 30 63 36 0a  |2, a2, #&0000c6.|
00002690  20 20 20 20 20 20 20 20  4d 4f 56 09 69 70 2c 20  |        MOV.ip, |
000026a0  61 31 0a 09 6d 75 6c 31  36 63 09 61 32 2c 20 69  |a1..mul16c.a2, i|
000026b0  70 2c 20 61 32 2c 20 61  33 0a 09 41 44 44 09 61  |p, a2, a3..ADD.a|
000026c0  32 2c 20 61 32 2c 20 23  26 30 38 30 30 30 30 0a  |2, a2, #&080000.|
000026d0  09 41 44 44 09 61 32 2c  20 61 32 2c 20 23 26 30  |.ADD.a2, a2, #&0|
000026e0  30 45 32 30 30 0a 09 41  44 44 09 61 32 2c 20 61  |0E200..ADD.a2, a|
000026f0  32 2c 20 23 26 30 30 30  30 42 44 0a 20 20 20 20  |2, #&0000BD.    |
00002700  20 20 20 20 4d 4f 56 09  69 70 2c 20 61 31 0a 09  |    MOV.ip, a1..|
00002710  6d 75 6c 31 36 63 09 61  32 2c 20 69 70 2c 20 61  |mul16c.a2, ip, a|
00002720  32 2c 20 61 33 0a 09 41  44 44 09 61 32 2c 20 61  |2, a3..ADD.a2, a|
00002730  32 2c 20 23 26 30 62 30  30 30 30 0a 09 41 44 44  |2, #&0b0000..ADD|
00002740  09 61 32 2c 20 61 32 2c  20 23 26 30 30 35 30 30  |.a2, a2, #&00500|
00002750  30 0a 09 41 44 44 09 61  32 2c 20 61 32 2c 20 23  |0..ADD.a2, a2, #|
00002760  26 30 30 30 30 35 30 0a  09 4d 4f 56 09 61 31 2c  |&000050..MOV.a1,|
00002770  20 61 32 2c 20 41 53 52  20 23 34 0a 09 54 53 54  | a2, ASR #4..TST|
00002780  09 61 34 2c 20 23 31 0a  09 52 53 42 4e 45 09 61  |.a4, #1..RSBNE.a|
00002790  31 2c 20 61 31 2c 20 23  30 0a 20 20 20 20 20 20  |1, a1, #0.      |
000027a0  20 20 4d 4f 56 53 20 20  20 20 70 63 2c 20 6c 72  |  MOVS    pc, lr|
000027b0  0a 0a 0a 0a 3b 20 73 69  6e 31 36 0a 3b 20 61 20  |....; sin16.; a |
000027c0  6c 65 61 66 20 41 50 43  53 20 66 75 6e 63 74 69  |leaf APCS functi|
000027d0  6f 6e 0a 3b 0a 3b 20 43  20 70 72 6f 74 6f 74 79  |on.;.; C prototy|
000027e0  70 65 3a 0a 3b 20 69 6e  74 20 73 69 6e 31 36 28  |pe:.; int sin16(|
000027f0  69 6e 74 20 61 29 0a 3b  0a 3b 20 72 65 74 75 72  |int a).;.; retur|
00002800  6e 73 20 36 35 35 33 36  20 2a 20 73 69 6e 20 28  |ns 65536 * sin (|
00002810  28 61 2f 36 35 35 33 36  29 28 70 69 2f 32 29 29  |(a/65536)(pi/2))|
00002820  0a 3b 0a 3b 20 69 73 20  61 62 6f 75 74 20 34 34  |.;.; is about 44|
00002830  20 74 69 6d 65 73 20 66  61 73 74 65 72 20 74 68  | times faster th|
00002840  61 6e 20 46 50 45 6d 75  6c 61 74 6f 72 20 77 6f  |an FPEmulator wo|
00002850  72 6b 69 6e 67 20 77 69  74 68 20 64 6f 75 62 6c  |rking with doubl|
00002860  65 20 66 6c 6f 61 74 73  0a 3b 0a 0a 20 20 20 20  |e floats.;..    |
00002870  20 20 20 20 45 58 50 4f  52 54 20 20 73 69 6e 31  |    EXPORT  sin1|
00002880  36 0a 0a 73 6e 6e 73 74  61 20 20 44 43 42 20 20  |6..snnsta  DCB  |
00002890  20 20 20 22 73 69 6e 31  36 22 2c 20 30 0a 20 20  |   "sin16", 0.  |
000028a0  20 20 20 20 20 20 41 4c  49 47 4e 0a 73 6e 6e 65  |      ALIGN.snne|
000028b0  6e 64 20 20 44 43 44 20  20 20 20 20 26 66 66 30  |nd  DCD     &ff0|
000028c0  30 30 30 30 30 20 2b 20  73 6e 6e 65 6e 64 20 2d  |00000 + snnend -|
000028d0  20 73 6e 6e 73 74 61 0a  0a 73 69 6e 31 36 0a 0a  | snnsta..sin16..|
000028e0  09 54 53 54 09 61 31 2c  20 23 26 32 30 30 30 30  |.TST.a1, #&20000|
000028f0  09 09 3b 65 76 61 6c 20  6f 66 20 73 69 6e 20 69  |..;eval of sin i|
00002900  73 20 61 6c 6d 6f 73 74  20 69 64 65 6e 74 69 63  |s almost identic|
00002910  61 6c 20 74 6f 20 74 68  61 74 20 6f 66 20 63 6f  |al to that of co|
00002920  73 0a 09 4d 4f 56 09 61  34 2c 20 23 30 0a 09 4d  |s..MOV.a4, #0..M|
00002930  4f 56 4e 45 09 61 34 2c  20 23 31 0a 09 54 53 54  |OVNE.a4, #1..TST|
00002940  09 61 31 2c 20 23 26 31  30 30 30 30 0a 09 4d 4f  |.a1, #&10000..MO|
00002950  56 09 61 31 2c 20 61 31  2c 20 4c 53 4c 20 23 31  |V.a1, a1, LSL #1|
00002960  36 0a 09 4d 4f 56 09 61  31 2c 20 61 31 2c 20 4c  |6..MOV.a1, a1, L|
00002970  53 52 20 23 31 36 0a 09  52 53 42 4e 45 09 61 31  |SR #16..RSBNE.a1|
00002980  2c 20 61 31 2c 20 23 26  31 30 30 30 30 0a 09 4d  |, a1, #&10000..M|
00002990  4f 56 09 61 31 2c 20 61  31 2c 20 4c 53 4c 20 23  |OV.a1, a1, LSL #|
000029a0  31 0a 09 53 55 42 09 61  31 2c 20 61 31 2c 20 23  |1..SUB.a1, a1, #|
000029b0  26 31 30 30 30 30 0a 09  52 53 42 09 61 32 2c 20  |&10000..RSB.a2, |
000029c0  61 31 2c 20 61 31 2c 20  4c 53 4c 20 23 33 0a 09  |a1, a1, LSL #3..|
000029d0  41 44 44 09 61 32 2c 20  61 31 2c 20 61 32 2c 20  |ADD.a2, a1, a2, |
000029e0  4c 53 4c 20 23 38 0a 09  4d 4f 56 09 61 32 2c 20  |LSL #8..MOV.a2, |
000029f0  61 32 2c 20 41 53 52 20  23 31 36 0a 09 41 44 44  |a2, ASR #16..ADD|
00002a00  09 61 32 2c 20 61 32 2c  20 23 26 30 30 32 63 30  |.a2, a2, #&002c0|
00002a10  30 0a 09 41 44 44 09 61  32 2c 20 61 32 2c 20 23  |0..ADD.a2, a2, #|
00002a20  26 30 30 30 30 38 36 0a  20 20 20 20 20 20 20 20  |&000086.        |
00002a30  4d 4f 56 09 69 70 2c 20  61 31 0a 09 6d 75 6c 31  |MOV.ip, a1..mul1|
00002a40  36 63 09 61 32 2c 20 69  70 2c 20 61 32 2c 20 61  |6c.a2, ip, a2, a|
00002a50  33 0a 09 53 55 42 09 61  32 2c 20 61 32 2c 20 23  |3..SUB.a2, a2, #|
00002a60  26 30 30 65 39 30 30 0a  09 53 55 42 09 61 32 2c  |&00e900..SUB.a2,|
00002a70  20 61 32 2c 20 23 26 30  30 30 30 62 64 0a 20 20  | a2, #&0000bd.  |
00002a80  20 20 20 20 20 20 4d 4f  56 09 69 70 2c 20 61 31  |      MOV.ip, a1|
00002a90  0a 09 6d 75 6c 31 36 63  09 61 32 2c 20 69 70 2c  |..mul16c.a2, ip,|
00002aa0  20 61 32 2c 20 61 33 0a  09 53 55 42 09 61 32 2c  | a2, a3..SUB.a2,|
00002ab0  20 61 32 2c 20 23 26 30  33 30 30 30 30 0a 09 53  | a2, #&030000..S|
00002ac0  55 42 09 61 32 2c 20 61  32 2c 20 23 26 30 30 37  |UB.a2, a2, #&007|
00002ad0  63 30 30 0a 09 53 55 42  09 61 32 2c 20 61 32 2c  |c00..SUB.a2, a2,|
00002ae0  20 23 26 30 30 30 30 63  36 0a 20 20 20 20 20 20  | #&0000c6.      |
00002af0  20 20 4d 4f 56 09 69 70  2c 20 61 31 0a 09 6d 75  |  MOV.ip, a1..mu|
00002b00  6c 31 36 63 09 61 32 2c  20 69 70 2c 20 61 32 2c  |l16c.a2, ip, a2,|
00002b10  20 61 33 0a 09 41 44 44  09 61 32 2c 20 61 32 2c  | a3..ADD.a2, a2,|
00002b20  20 23 26 30 38 30 30 30  30 0a 09 41 44 44 09 61  | #&080000..ADD.a|
00002b30  32 2c 20 61 32 2c 20 23  26 30 30 45 32 30 30 0a  |2, a2, #&00E200.|
00002b40  09 41 44 44 09 61 32 2c  20 61 32 2c 20 23 26 30  |.ADD.a2, a2, #&0|
00002b50  30 30 30 42 44 0a 20 20  20 20 20 20 20 20 4d 4f  |000BD.        MO|
00002b60  56 09 69 70 2c 20 61 31  0a 09 6d 75 6c 31 36 63  |V.ip, a1..mul16c|
00002b70  09 61 32 2c 20 69 70 2c  20 61 32 2c 20 61 33 0a  |.a2, ip, a2, a3.|
00002b80  09 41 44 44 09 61 32 2c  20 61 32 2c 20 23 26 30  |.ADD.a2, a2, #&0|
00002b90  62 30 30 30 30 0a 09 41  44 44 09 61 32 2c 20 61  |b0000..ADD.a2, a|
00002ba0  32 2c 20 23 26 30 30 35  30 30 30 0a 09 41 44 44  |2, #&005000..ADD|
00002bb0  09 61 32 2c 20 61 32 2c  20 23 26 30 30 30 30 35  |.a2, a2, #&00005|
00002bc0  30 0a 09 4d 4f 56 09 61  31 2c 20 61 32 2c 20 41  |0..MOV.a1, a2, A|
00002bd0  53 52 20 23 34 0a 09 54  53 54 09 61 34 2c 20 23  |SR #4..TST.a4, #|
00002be0  31 0a 09 52 53 42 4e 45  09 61 31 2c 20 61 31 2c  |1..RSBNE.a1, a1,|
00002bf0  20 23 30 0a 20 20 20 20  20 20 20 20 4d 4f 56 53  | #0.        MOVS|
00002c00  20 20 20 20 70 63 2c 20  6c 72 0a 0a 0a 0a 3b 20  |    pc, lr....; |
00002c10  67 61 75 73 73 31 36 0a  3b 20 61 20 6c 65 61 66  |gauss16.; a leaf|
00002c20  20 41 50 43 53 20 66 75  6e 63 74 69 6f 6e 0a 3b  | APCS function.;|
00002c30  0a 3b 20 43 20 70 72 6f  74 6f 74 79 70 65 3a 0a  |.; C prototype:.|
00002c40  3b 20 69 6e 74 20 67 61  75 73 73 31 36 28 76 6f  |; int gauss16(vo|
00002c50  69 64 29 0a 3b 0a 3b 20  72 65 74 75 72 6e 73 20  |id).;.; returns |
00002c60  36 35 35 33 36 20 2a 20  28 20 70 73 65 75 64 6f  |65536 * ( pseudo|
00002c70  20 72 61 6e 64 6f 6e 20  76 61 72 69 61 62 6c 65  | randon variable|
00002c80  20 77 69 74 68 20 61 70  70 72 6f 78 69 6d 61 74  | with approximat|
00002c90  65 20 64 69 73 74 72 69  62 75 74 69 6f 6e 20 4e  |e distribution N|
00002ca0  28 30 2c 31 29 20 29 0a  3b 20 6e 6f 74 65 2c 20  |(0,1) ).; note, |
00002cb0  74 68 65 20 61 70 70 72  6f 78 69 6d 61 74 65 20  |the approximate |
00002cc0  67 61 75 73 73 69 61 6e  20 64 69 73 74 72 69 62  |gaussian distrib|
00002cd0  75 74 69 6f 6e 20 69 73  20 61 63 68 69 65 76 65  |ution is achieve|
00002ce0  64 20 76 69 61 20 74 68  65 20 73 75 6d 20 6f 66  |d via the sum of|
00002cf0  20 6e 20 70 73 65 75 64  6f 20 55 5b 30 2c 36 35  | n pseudo U[0,65|
00002d00  35 33 35 5d 0a 3b 20 20  20 20 20 20 20 72 61 6e  |535].;       ran|
00002d10  64 6f 6d 20 76 61 72 69  61 62 6c 65 73 20 26 20  |dom variables & |
00002d20  61 70 70 6c 69 63 61 74  69 6f 6e 20 6f 66 20 74  |application of t|
00002d30  68 65 20 63 65 6e 74 72  61 6c 20 6c 69 6d 69 74  |he central limit|
00002d40  20 74 68 65 6f 72 65 6d  0a 3b 20 20 20 20 20 20  | theorem.;      |
00002d50  20 68 65 72 65 20 77 65  20 75 73 65 20 6e 3d 38  | here we use n=8|
00002d60  2c 20 67 69 76 69 6e 67  20 61 6e 20 61 63 74 75  |, giving an actu|
00002d70  61 6c 20 72 61 6e 67 65  20 6f 66 20 b1 28 73 71  |al range of .(sq|
00002d80  72 74 32 34 29 20 28 2a  36 35 35 33 36 29 2e 0a  |rt24) (*65536)..|
00002d90  3b 0a 3b 20 20 20 20 20  20 20 66 6f 72 20 67 65  |;.;       for ge|
00002da0  6e 65 72 61 6c 20 6e 2c  20 72 65 63 61 6c 6c 20  |neral n, recall |
00002db0  77 65 20 6e 65 65 64 20  74 6f 20 72 65 74 75 72  |we need to retur|
00002dc0  6e 3a 0a 3b 20 20 20 20  20 20 20 28 73 75 6d 20  |n:.;       (sum |
00002dd0  6e 20 55 5b 30 2c 36 35  35 33 35 5d 20 72 61 6e  |n U[0,65535] ran|
00002de0  64 6f 6d 20 76 61 72 69  61 62 6c 65 73 29 20 2a  |dom variables) *|
00002df0  20 32 73 71 72 74 28 33  6e 29 2a 36 35 35 33 36  | 2sqrt(3n)*65536|
00002e00  2f 28 36 35 35 33 35 6e  29 20 20 2d 20 20 73 71  |/(65535n)  -  sq|
00002e10  72 74 28 33 6e 29 2a 36  35 35 33 36 0a 3b 0a 0a  |rt(3n)*65536.;..|
00002e20  09 45 58 50 4f 52 54 09  67 61 75 73 73 31 36 0a  |.EXPORT.gauss16.|
00002e30  0a 67 61 6e 73 74 61 20  20 44 43 42 20 20 20 20  |.gansta  DCB    |
00002e40  20 22 67 61 75 73 73 31  36 22 2c 20 30 0a 20 20  | "gauss16", 0.  |
00002e50  20 20 20 20 20 20 41 4c  49 47 4e 0a 67 61 6e 65  |      ALIGN.gane|
00002e60  6e 64 20 20 44 43 44 20  20 20 20 20 26 66 66 30  |nd  DCD     &ff0|
00002e70  30 30 30 30 30 20 2b 20  67 61 6e 65 6e 64 20 2d  |00000 + ganend -|
00002e80  20 67 61 6e 73 74 61 0a  0a 67 61 75 73 73 31 36  | gansta..gauss16|
00002e90  0a 0a 09 41 44 52 09 61  31 2c 20 67 61 75 73 73  |...ADR.a1, gauss|
00002ea0  73 65 65 64 31 0a 09 4c  44 4d 49 41 09 61 31 2c  |seed1..LDMIA.a1,|
00002eb0  20 7b 61 32 2c 20 61 33  7d 0a 0a 09 4d 4f 56 53  | {a2, a3}...MOVS|
00002ec0  20 20 20 20 61 33 2c 20  61 33 2c 20 4c 53 52 20  |    a3, a3, LSR |
00002ed0  23 31 0a 20 20 20 20 20  20 20 20 4d 4f 56 53 20  |#1.        MOVS |
00002ee0  20 20 20 61 34 2c 20 61  32 2c 20 52 52 58 0a 20  |   a4, a2, RRX. |
00002ef0  20 20 20 20 20 20 20 41  44 43 20 20 20 20 20 61  |       ADC     a|
00002f00  33 2c 20 61 33 2c 20 61  33 0a 20 20 20 20 20 20  |3, a3, a3.      |
00002f10  20 20 45 4f 52 20 20 20  20 20 61 34 2c 20 61 34  |  EOR     a4, a4|
00002f20  2c 20 61 32 2c 20 4c 53  4c 20 23 31 32 0a 20 20  |, a2, LSL #12.  |
00002f30  20 20 20 20 20 20 45 4f  52 20 20 20 20 20 61 32  |      EOR     a2|
00002f40  2c 20 61 34 2c 20 61 34  2c 20 4c 53 52 20 23 32  |, a4, a4, LSR #2|
00002f50  30 0a 09 4d 4f 56 09 69  70 2c 20 61 32 2c 20 4c  |0..MOV.ip, a2, L|
00002f60  53 52 20 23 31 36 0a 09  4d 4f 56 09 61 34 2c 20  |SR #16..MOV.a4, |
00002f70  61 32 2c 20 4c 53 4c 20  23 31 36 0a 09 41 44 44  |a2, LSL #16..ADD|
00002f80  09 69 70 2c 20 69 70 2c  20 61 34 2c 20 4c 53 52  |.ip, ip, a4, LSR|
00002f90  20 23 31 36 0a 0a 09 4d  4f 56 53 20 20 20 20 61  | #16...MOVS    a|
00002fa0  33 2c 20 61 33 2c 20 4c  53 52 20 23 31 0a 20 20  |3, a3, LSR #1.  |
00002fb0  20 20 20 20 20 20 4d 4f  56 53 20 20 20 20 61 34  |      MOVS    a4|
00002fc0  2c 20 61 32 2c 20 52 52  58 0a 20 20 20 20 20 20  |, a2, RRX.      |
00002fd0  20 20 41 44 43 20 20 20  20 20 61 33 2c 20 61 33  |  ADC     a3, a3|
00002fe0  2c 20 61 33 0a 20 20 20  20 20 20 20 20 45 4f 52  |, a3.        EOR|
00002ff0  20 20 20 20 20 61 34 2c  20 61 34 2c 20 61 32 2c  |     a4, a4, a2,|
00003000  20 4c 53 4c 20 23 31 32  0a 20 20 20 20 20 20 20  | LSL #12.       |
00003010  20 45 4f 52 20 20 20 20  20 61 32 2c 20 61 34 2c  | EOR     a2, a4,|
00003020  20 61 34 2c 20 4c 53 52  20 23 32 30 0a 09 41 44  | a4, LSR #20..AD|
00003030  44 09 69 70 2c 20 69 70  2c 20 61 32 2c 20 4c 53  |D.ip, ip, a2, LS|
00003040  52 20 23 31 36 0a 09 4d  4f 56 09 61 34 2c 20 61  |R #16..MOV.a4, a|
00003050  32 2c 20 4c 53 4c 20 23  31 36 0a 09 41 44 44 09  |2, LSL #16..ADD.|
00003060  69 70 2c 20 69 70 2c 20  61 34 2c 20 4c 53 52 20  |ip, ip, a4, LSR |
00003070  23 31 36 0a 0a 09 4d 4f  56 53 20 20 20 20 61 33  |#16...MOVS    a3|
00003080  2c 20 61 33 2c 20 4c 53  52 20 23 31 0a 20 20 20  |, a3, LSR #1.   |
00003090  20 20 20 20 20 4d 4f 56  53 20 20 20 20 61 34 2c  |     MOVS    a4,|
000030a0  20 61 32 2c 20 52 52 58  0a 20 20 20 20 20 20 20  | a2, RRX.       |
000030b0  20 41 44 43 20 20 20 20  20 61 33 2c 20 61 33 2c  | ADC     a3, a3,|
000030c0  20 61 33 0a 20 20 20 20  20 20 20 20 45 4f 52 20  | a3.        EOR |
000030d0  20 20 20 20 61 34 2c 20  61 34 2c 20 61 32 2c 20  |    a4, a4, a2, |
000030e0  4c 53 4c 20 23 31 32 0a  20 20 20 20 20 20 20 20  |LSL #12.        |
000030f0  45 4f 52 20 20 20 20 20  61 32 2c 20 61 34 2c 20  |EOR     a2, a4, |
00003100  61 34 2c 20 4c 53 52 20  23 32 30 0a 09 41 44 44  |a4, LSR #20..ADD|
00003110  09 69 70 2c 20 69 70 2c  20 61 32 2c 20 4c 53 52  |.ip, ip, a2, LSR|
00003120  20 23 31 36 0a 09 4d 4f  56 09 61 34 2c 20 61 32  | #16..MOV.a4, a2|
00003130  2c 20 4c 53 4c 20 23 31  36 0a 09 41 44 44 09 69  |, LSL #16..ADD.i|
00003140  70 2c 20 69 70 2c 20 61  34 2c 20 4c 53 52 20 23  |p, ip, a4, LSR #|
00003150  31 36 0a 0a 09 4d 4f 56  53 20 20 20 20 61 33 2c  |16...MOVS    a3,|
00003160  20 61 33 2c 20 4c 53 52  20 23 31 0a 20 20 20 20  | a3, LSR #1.    |
00003170  20 20 20 20 4d 4f 56 53  20 20 20 20 61 34 2c 20  |    MOVS    a4, |
00003180  61 32 2c 20 52 52 58 0a  20 20 20 20 20 20 20 20  |a2, RRX.        |
00003190  41 44 43 20 20 20 20 20  61 33 2c 20 61 33 2c 20  |ADC     a3, a3, |
000031a0  61 33 0a 20 20 20 20 20  20 20 20 45 4f 52 20 20  |a3.        EOR  |
000031b0  20 20 20 61 34 2c 20 61  34 2c 20 61 32 2c 20 4c  |   a4, a4, a2, L|
000031c0  53 4c 20 23 31 32 0a 20  20 20 20 20 20 20 20 45  |SL #12.        E|
000031d0  4f 52 20 20 20 20 20 61  32 2c 20 61 34 2c 20 61  |OR     a2, a4, a|
000031e0  34 2c 20 4c 53 52 20 23  32 30 0a 09 41 44 44 09  |4, LSR #20..ADD.|
000031f0  69 70 2c 20 69 70 2c 20  61 32 2c 20 4c 53 52 20  |ip, ip, a2, LSR |
00003200  23 31 36 0a 09 4d 4f 56  09 61 34 2c 20 61 32 2c  |#16..MOV.a4, a2,|
00003210  20 4c 53 4c 20 23 31 36  0a 09 41 44 44 09 69 70  | LSL #16..ADD.ip|
00003220  2c 20 69 70 2c 20 61 34  2c 20 4c 53 52 20 23 31  |, ip, a4, LSR #1|
00003230  36 09 09 3b 73 75 6d 20  6e 6f 77 20 69 6e 20 69  |6..;sum now in i|
00003240  70 0a 0a 09 53 54 4d 49  41 09 61 31 2c 20 7b 61  |p...STMIA.a1, {a|
00003250  32 2c 20 61 33 7d 0a 0a  09 52 53 42 09 61 31 2c  |2, a3}...RSB.a1,|
00003260  20 69 70 2c 20 69 70 2c  20 41 53 4c 20 23 33 0a  | ip, ip, ASL #3.|
00003270  09 52 53 42 09 61 32 2c  20 69 70 2c 20 69 70 2c  |.RSB.a2, ip, ip,|
00003280  20 41 53 4c 20 23 32 0a  09 41 44 44 09 61 31 2c  | ASL #2..ADD.a1,|
00003290  20 61 32 2c 20 61 31 2c  20 41 53 4c 20 23 34 0a  | a2, a1, ASL #4.|
000032a0  09 41 44 44 09 61 31 2c  20 61 31 2c 20 69 70 2c  |.ADD.a1, a1, ip,|
000032b0  20 41 53 4c 20 23 39 0a  09 41 44 44 09 61 32 2c  | ASL #9..ADD.a2,|
000032c0  20 69 70 2c 20 69 70 2c  20 41 53 4c 20 23 34 0a  | ip, ip, ASL #4.|
000032d0  09 41 44 44 09 61 32 2c  20 61 32 2c 20 69 70 2c  |.ADD.a2, a2, ip,|
000032e0  20 41 53 4c 20 23 36 0a  09 41 44 44 09 61 31 2c  | ASL #6..ADD.a1,|
000032f0  20 61 31 2c 20 61 32 2c  20 4c 53 52 20 23 31 30  | a1, a2, LSR #10|
00003300  0a 09 4d 4f 56 09 61 31  2c 20 61 31 2c 20 4c 53  |..MOV.a1, a1, LS|
00003310  52 20 23 39 0a 09 53 55  42 09 61 31 2c 20 61 31  |R #9..SUB.a1, a1|
00003320  2c 20 23 26 34 45 30 30  30 0a 09 53 55 42 09 61  |, #&4E000..SUB.a|
00003330  31 2c 20 61 31 2c 20 23  26 30 30 36 32 30 0a 09  |1, a1, #&00620..|
00003340  53 55 42 09 61 31 2c 20  61 31 2c 20 23 26 30 30  |SUB.a1, a1, #&00|
00003350  30 30 34 0a 0a 20 20 20  20 20 20 20 20 4d 4f 56  |004..        MOV|
00003360  53 20 20 20 20 70 63 2c  20 6c 72 0a 0a 67 61 75  |S    pc, lr..gau|
00003370  73 73 73 65 65 64 31 09  44 43 44 09 2d 31 09 09  |ssseed1.DCD.-1..|
00003380  3b 62 69 74 73 20 62 30  2d 62 33 31 20 6f 66 20  |;bits b0-b31 of |
00003390  73 65 65 64 0a 09 09 44  43 44 09 2d 31 09 09 3b  |seed...DCD.-1..;|
000033a0  62 69 74 20 20 62 33 32  20 6f 66 20 73 65 65 64  |bit  b32 of seed|
000033b0  20 28 69 6e 20 6c 73 62  20 6f 66 20 77 6f 72 64  | (in lsb of word|
000033c0  29 0a 0a 3b 20 73 67 61  75 73 73 31 36 0a 3b 20  |)..; sgauss16.; |
000033d0  61 20 6c 65 61 66 20 41  50 43 53 20 66 75 6e 63  |a leaf APCS func|
000033e0  74 69 6f 6e 0a 3b 0a 3b  20 43 20 70 72 6f 74 6f  |tion.;.; C proto|
000033f0  74 79 70 65 3a 0a 3b 20  76 6f 69 64 20 73 67 61  |type:.; void sga|
00003400  75 73 73 31 36 28 69 6e  74 20 73 65 65 64 29 0a  |uss16(int seed).|
00003410  3b 0a 3b 20 73 65 74 73  20 74 68 65 20 73 65 65  |;.; sets the see|
00003420  64 20 66 6f 72 20 67 61  75 73 73 31 36 0a 3b 0a  |d for gauss16.;.|
00003430  0a 09 45 58 50 4f 52 54  09 73 67 61 75 73 73 31  |..EXPORT.sgauss1|
00003440  36 0a 0a 73 67 6e 73 74  61 20 20 44 43 42 20 20  |6..sgnsta  DCB  |
00003450  20 20 20 22 73 67 61 75  73 73 31 36 22 2c 20 30  |   "sgauss16", 0|
00003460  0a 20 20 20 20 20 20 20  20 41 4c 49 47 4e 0a 73  |.        ALIGN.s|
00003470  67 6e 65 6e 64 20 20 44  43 44 20 20 20 20 20 26  |gnend  DCD     &|
00003480  66 66 30 30 30 30 30 30  20 2b 20 73 67 6e 65 6e  |ff000000 + sgnen|
00003490  64 20 2d 20 73 67 6e 73  74 61 0a 0a 73 67 61 75  |d - sgnsta..sgau|
000034a0  73 73 31 36 0a 0a 09 41  44 52 09 61 32 2c 20 67  |ss16...ADR.a2, g|
000034b0  61 75 73 73 73 65 65 64  31 0a 09 4d 4f 56 09 61  |aussseed1..MOV.a|
000034c0  33 2c 20 23 31 0a 09 53  54 4d 49 41 09 61 32 2c  |3, #1..STMIA.a2,|
000034d0  20 7b 61 31 2c 20 61 33  7d 0a 20 20 20 20 20 20  | {a1, a3}.      |
000034e0  20 20 4d 4f 56 53 20 20  20 20 70 63 2c 20 6c 72  |  MOVS    pc, lr|
000034f0  0a 0a 0a 0a 3b 20 64 69  76 5f 66 72 61 63 31 36  |....; div_frac16|
00003500  0a 3b 20 61 20 6c 65 61  66 20 41 50 43 53 20 66  |.; a leaf APCS f|
00003510  75 6e 63 74 69 6f 6e 0a  3b 0a 3b 20 43 20 70 72  |unction.;.; C pr|
00003520  6f 74 6f 74 79 70 65 3a  0a 3b 20 69 6e 74 20 64  |ototype:.; int d|
00003530  69 76 5f 66 72 61 63 31  36 28 69 6e 74 20 6e 75  |iv_frac16(int nu|
00003540  6d 62 65 72 2c 20 69 6e  74 20 64 69 76 69 73 6f  |mber, int diviso|
00003550  72 29 0a 3b 0a 3b 20 72  65 74 75 72 6e 73 20 69  |r).;.; returns i|
00003560  6e 74 65 67 65 72 20 70  61 72 74 20 6f 66 20 36  |nteger part of 6|
00003570  35 35 33 36 2a 6e 75 6d  62 65 72 2f 64 69 76 69  |5536*number/divi|
00003580  73 6f 72 0a 3b 20 61 73  73 75 6d 65 73 20 61 62  |sor.; assumes ab|
00003590  73 20 6e 75 6d 62 65 72  20 3c 20 36 35 35 33 36  |s number < 65536|
000035a0  20 2a 20 61 62 73 20 64  69 76 69 73 6f 72 0a 3b  | * abs divisor.;|
000035b0  20 69 66 20 74 68 69 73  20 6e 65 65 64 73 20 63  | if this needs c|
000035c0  68 65 63 6b 69 6e 67 2c  20 6d 75 73 74 20 62 65  |hecking, must be|
000035d0  20 64 6f 6e 65 20 62 79  20 63 61 6c 6c 65 72 0a  | done by caller.|
000035e0  3b 0a 0a 20 20 20 20 20  20 20 20 45 58 50 4f 52  |;..        EXPOR|
000035f0  54 20 20 64 69 76 5f 66  72 61 63 31 36 0a 0a 64  |T  div_frac16..d|
00003600  66 6e 73 74 61 20 20 44  43 42 20 20 20 20 20 22  |fnsta  DCB     "|
00003610  64 69 76 5f 66 72 61 63  31 36 22 2c 20 30 0a 20  |div_frac16", 0. |
00003620  20 20 20 20 20 20 20 41  4c 49 47 4e 0a 64 66 6e  |       ALIGN.dfn|
00003630  65 6e 64 20 20 44 43 44  20 20 20 20 20 26 66 66  |end  DCD     &ff|
00003640  30 30 30 30 30 30 20 2b  20 64 66 6e 65 6e 64 20  |000000 + dfnend |
00003650  2d 20 64 66 6e 73 74 61  0a 0a 64 69 76 5f 66 72  |- dfnsta..div_fr|
00003660  61 63 31 36 0a 0a 20 20  20 20 20 20 20 20 64 69  |ac16..        di|
00003670  76 31 36 20 20 20 61 31  2c 20 61 32 2c 20 61 31  |v16   a1, a2, a1|
00003680  2c 20 61 33 2c 20 61 34  0a 20 20 20 20 20 20 20  |, a3, a4.       |
00003690  20 4d 4f 56 53 20 20 20  20 70 63 2c 20 6c 72 0a  | MOVS    pc, lr.|
000036a0  0a 0a 0a 3b 20 6d 75 6c  5f 66 72 61 63 31 36 0a  |...; mul_frac16.|
000036b0  3b 20 61 20 6c 65 61 66  20 41 50 43 53 20 66 75  |; a leaf APCS fu|
000036c0  6e 63 74 69 6f 6e 0a 3b  0a 3b 20 43 20 70 72 6f  |nction.;.; C pro|
000036d0  74 6f 74 79 70 65 3a 0a  3b 20 69 6e 74 20 6d 75  |totype:.; int mu|
000036e0  6c 5f 66 72 61 63 31 36  28 69 6e 74 20 78 2c 20  |l_frac16(int x, |
000036f0  69 6e 74 20 61 29 0a 3b  0a 3b 20 72 65 74 75 72  |int a).;.; retur|
00003700  6e 73 20 33 32 2d 62 69  74 20 73 69 67 6e 65 64  |ns 32-bit signed|
00003710  20 69 6e 74 65 67 65 72  20 78 2a 61 2f 36 35 35  | integer x*a/655|
00003720  33 36 0a 3b 20 61 73 73  75 6d 65 73 20 72 65 73  |36.; assumes res|
00003730  75 6c 74 20 66 69 74 73  20 69 6e 74 6f 20 33 32  |ult fits into 32|
00003740  2d 62 69 74 20 73 69 67  6e 65 64 20 72 65 70 72  |-bit signed repr|
00003750  65 73 65 6e 74 61 74 69  6f 6e 0a 3b 20 6e 6f 74  |esentation.; not|
00003760  65 2c 20 6e 6f 20 6f 74  68 65 72 20 72 65 73 74  |e, no other rest|
00003770  72 69 63 74 69 6f 6e 73  20 6f 6e 20 61 20 2d 20  |rictions on a - |
00003780  69 66 20 63 61 6e 20 67  75 61 72 61 6e 74 65 65  |if can guarantee|
00003790  20 61 62 73 20 61 20 3c  20 36 35 35 33 36 2c 20  | abs a < 65536, |
000037a0  75 73 65 20 6d 75 6c 5f  66 72 61 63 31 36 63 20  |use mul_frac16c |
000037b0  69 6e 73 74 65 61 64 20  61 73 20 69 73 20 71 75  |instead as is qu|
000037c0  69 63 6b 65 72 0a 3b 0a  0a 20 20 20 20 20 20 20  |icker.;..       |
000037d0  20 45 58 50 4f 52 54 20  20 6d 75 6c 5f 66 72 61  | EXPORT  mul_fra|
000037e0  63 31 36 0a 0a 6d 66 6e  73 74 61 20 20 44 43 42  |c16..mfnsta  DCB|
000037f0  20 20 20 20 20 22 6d 75  6c 5f 66 72 61 63 31 36  |     "mul_frac16|
00003800  22 2c 20 30 0a 20 20 20  20 20 20 20 20 41 4c 49  |", 0.        ALI|
00003810  47 4e 0a 6d 66 6e 65 6e  64 20 20 44 43 44 20 20  |GN.mfnend  DCD  |
00003820  20 20 20 26 66 66 30 30  30 30 30 30 20 2b 20 6d  |   &ff000000 + m|
00003830  66 6e 65 6e 64 20 2d 20  6d 66 6e 73 74 61 0a 0a  |fnend - mfnsta..|
00003840  6d 75 6c 5f 66 72 61 63  31 36 0a 0a 20 20 20 20  |mul_frac16..    |
00003850  20 20 20 20 6d 75 6c 31  36 20 20 20 61 31 2c 20  |    mul16   a1, |
00003860  61 32 2c 20 61 31 2c 20  61 33 2c 20 61 34 2c 20  |a2, a1, a3, a4, |
00003870  69 70 0a 20 20 20 20 20  20 20 20 4d 4f 56 53 20  |ip.        MOVS |
00003880  20 20 20 70 63 2c 20 6c  72 0a 0a 0a 0a 3b 20 6d  |   pc, lr....; m|
00003890  75 6c 5f 66 72 61 63 31  36 63 0a 3b 20 61 20 6c  |ul_frac16c.; a l|
000038a0  65 61 66 20 41 50 43 53  20 66 75 6e 63 74 69 6f  |eaf APCS functio|
000038b0  6e 0a 3b 0a 3b 20 43 20  70 72 6f 74 6f 74 79 70  |n.;.; C prototyp|
000038c0  65 3a 0a 3b 20 69 6e 74  20 6d 75 6c 5f 66 72 61  |e:.; int mul_fra|
000038d0  63 31 36 63 28 69 6e 74  20 78 2c 20 69 6e 74 20  |c16c(int x, int |
000038e0  61 29 0a 3b 0a 3b 20 72  65 74 75 72 6e 73 20 33  |a).;.; returns 3|
000038f0  32 2d 62 69 74 20 73 69  67 6e 65 64 20 69 6e 74  |2-bit signed int|
00003900  65 67 65 72 20 78 2a 61  2f 36 35 35 33 36 0a 3b  |eger x*a/65536.;|
00003910  20 61 73 73 75 6d 65 73  20 61 62 73 20 61 20 3c  | assumes abs a <|
00003920  3d 36 35 35 33 36 0a 3b  20 69 66 20 69 74 20 69  |=65536.; if it i|
00003930  73 20 70 6f 73 73 69 62  6c 65 20 74 68 61 74 20  |s possible that |
00003940  61 62 73 20 61 20 3e 20  36 35 35 33 36 2c 20 63  |abs a > 65536, c|
00003950  61 6c 6c 65 72 20 6d 75  73 74 20 63 68 65 63 6b  |aller must check|
00003960  20 72 61 6e 67 65 20 26  20 65 69 74 68 65 72 20  | range & either |
00003970  6e 6f 74 20 63 61 6c 6c  20 66 6e 20 6f 72 20 72  |not call fn or r|
00003980  6f 75 6e 64 20 64 6f 77  6e 20 74 6f 20 36 35 35  |ound down to 655|
00003990  33 36 0a 3b 0a 0a 20 20  20 20 20 20 20 20 45 58  |36.;..        EX|
000039a0  50 4f 52 54 20 20 6d 75  6c 5f 66 72 61 63 31 36  |PORT  mul_frac16|
000039b0  63 0a 0a 6d 66 63 6e 73  74 61 20 44 43 42 20 20  |c..mfcnsta DCB  |
000039c0  20 20 20 22 6d 75 6c 5f  66 72 61 63 31 36 63 22  |   "mul_frac16c"|
000039d0  2c 20 30 0a 20 20 20 20  20 20 20 20 41 4c 49 47  |, 0.        ALIG|
000039e0  4e 0a 6d 66 63 6e 65 6e  64 20 44 43 44 20 20 20  |N.mfcnend DCD   |
000039f0  20 20 26 66 66 30 30 30  30 30 30 20 2b 20 6d 66  |  &ff000000 + mf|
00003a00  63 6e 65 6e 64 20 2d 20  6d 66 63 6e 73 74 61 0a  |cnend - mfcnsta.|
00003a10  0a 6d 75 6c 5f 66 72 61  63 31 36 63 0a 0a 20 20  |.mul_frac16c..  |
00003a20  20 20 20 20 20 20 6d 75  6c 31 36 63 20 20 61 31  |      mul16c  a1|
00003a30  2c 20 61 32 2c 20 61 31  2c 20 61 33 0a 20 20 20  |, a2, a1, a3.   |
00003a40  20 20 20 20 20 4d 4f 56  53 20 20 20 20 70 63 2c  |     MOVS    pc,|
00003a50  20 6c 72 0a 0a 0a 0a 3b  20 73 71 72 74 5f 66 72  | lr....; sqrt_fr|
00003a60  61 63 32 38 0a 3b 20 61  20 6c 65 61 66 20 41 50  |ac28.; a leaf AP|
00003a70  43 53 20 66 75 6e 63 74  69 6f 6e 0a 3b 0a 3b 20  |CS function.;.; |
00003a80  43 20 70 72 6f 74 6f 74  79 70 65 3a 0a 3b 20 69  |C prototype:.; i|
00003a90  6e 74 20 73 71 72 74 5f  66 72 61 63 32 38 28 75  |nt sqrt_frac28(u|
00003aa0  6e 73 69 67 6e 65 64 20  69 6e 74 20 78 29 0a 3b  |nsigned int x).;|
00003ab0  0a 3b 20 72 65 74 75 72  6e 73 20 33 32 2d 62 69  |.; returns 32-bi|
00003ac0  74 20 69 6e 74 65 67 65  72 20 73 71 72 74 28 78  |t integer sqrt(x|
00003ad0  3c 3c 32 38 29 0a 3b 0a  0a 20 20 20 20 20 20 20  |<<28).;..       |
00003ae0  20 45 58 50 4f 52 54 20  20 73 71 72 74 5f 66 72  | EXPORT  sqrt_fr|
00003af0  61 63 32 38 0a 0a 73 71  66 6e 73 74 61 20 44 43  |ac28..sqfnsta DC|
00003b00  42 20 20 20 20 20 22 73  71 72 74 5f 66 72 61 63  |B     "sqrt_frac|
00003b10  32 38 22 2c 20 30 0a 20  20 20 20 20 20 20 20 41  |28", 0.        A|
00003b20  4c 49 47 4e 0a 73 71 66  6e 65 6e 64 20 44 43 44  |LIGN.sqfnend DCD|
00003b30  20 20 20 20 20 26 66 66  30 30 30 30 30 30 20 2b  |     &ff000000 +|
00003b40  20 73 71 66 6e 65 6e 64  20 2d 20 73 71 66 6e 73  | sqfnend - sqfns|
00003b50  74 61 0a 0a 73 71 72 74  5f 66 72 61 63 32 38 0a  |ta..sqrt_frac28.|
00003b60  0a 20 20 20 20 20 20 20  20 73 71 72 74 32 38 20  |.        sqrt28 |
00003b70  20 61 31 2c 20 61 31 2c  20 61 32 2c 20 61 33 2c  | a1, a1, a2, a3,|
00003b80  20 61 34 2c 20 69 70 0a  20 20 20 20 20 20 20 20  | a4, ip.        |
00003b90  4d 4f 56 53 20 20 20 20  70 63 2c 20 6c 72 0a 0a  |MOVS    pc, lr..|
00003ba0  0a 0a 20 20 20 20 20 20  20 20 45 4e 44 0a        |..        END.|
00003bae