Home » Recent acquisitions » Acorn ADFS disks » adfs_ArchimedesWorld_15_03.adf » !AcornAns_AcornAns » Flocking/C/s/subrouts1

Flocking/C/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 » Recent acquisitions » Acorn ADFS disks » adfs_ArchimedesWorld_15_03.adf » !AcornAns_AcornAns
Filename: Flocking/C/s/subrouts1
Read OK:
File size: 5E54 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
	MOVGES	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                                   )
	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



; sig16
; a leaf APCS function
;
; C prototype:
; int sig16(int a)
;
; returns 65536 / ( 1 +  exp (-a/65536) )
; the sigmoid function for neural net code
;
; is about 53 times faster than FPEmulator working with double floats
;

        EXPORT  sig16

sdnsta  DCB     "sig16", 0
        ALIGN
sdnend  DCD     &ff000000 + sdnend - sdnsta

sig16	CMP	a1, #12*65536
	MOVGT	a1, #one
	MOVGTS	pc, lr
	CMP	a1, #-12*65536
	MOVLT	a1, #0
	MOVLTS	pc, lr			;otherwise know |a| <= 12one
	ANDS	ip, a1, #1<<31		;copy sign of a into b31 of ip
	RSBMI	a1, a1, #0		;ensure a>=0
	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
	BLT	sig16_l1
	MOV	a1, #1			;& if not, return near maximal or minimal value as appropriate
	TST	ip, #1<<31
	RSBEQ	a1, a1, #one
	MOVS	pc,lr
sig16_l1
	ORR	a4, a4, ip		;(temporarily store sign of original a in a4)
	MOV	a1, a1, LSL #1		;else need  value (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                                   )
	MOV	a1, a2, ASR #4		;now divide by 16 removing most truncation error from above calcs,
					;leaving a2 = (2^16)*(approximated value)
	BIC	ip, a4, #1<<31		;remove temp sign bit from a4
	MOVS	a2, a1, LSL ip		;finally carry out the (a2 = a2 * 2^a4) step
	ADDCCS	a2, a2, #one		;then add in one
	MOVCS	a1, a4, LSR #15		;& if overflow, return maximal or minimal value as appropriate
	EORCS	a1, a1, #one
	MOVCSS	pc, lr
	MOV	a1, #0			;result for operation a1 = 2^32 / a2
	MOV	a3, #1<<16		;remainder reg
	GBLA	counter
counter	SETA	16
	WHILE	counter > 0
	ADD	a1, a1, a1
	ADD	a3, a3, a3
	CMP	a3, a2
	SUBHS	a3, a3, a2
	ORRHS	a1, a1, #1
counter SETA	counter-1
	WEND				;reciprocal calculated
	CMP	a2, a3, LSL #1
	ADDLS	a1, a1, #1
	TST	a4, #1<<31		;and finally, if original argument > 0
	RSBEQ	a1, a1, #one		;then flip value about y=1/2 line in graph of sig fn
        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



; acs16
; a leaf APCS function
;
; C prototype:
; int acs16(int a)
;
; returns 65536 * 2/pi * arccos (a/65536)
;
; for a out of range (ie abs a > 65536), reset a to �65536 as appropriate before eval
;
; is about 24 times faster than FPEmulator working with double floats
;

        EXPORT  acs16

acnsta  DCB     "acs16", 0
        ALIGN
acnend  DCD     &ff000000 + acnend - acnsta

acs16

	CMP	a1, #65536
	MOVGE	a1, #0
	MOVGES	pc, lr
	CMP	a1, #-65536
	MOVLE	a1, #2*65536
	MOVLES	pc, lr
	STMFD	sp!, {lr}		;now have arg in range (-one,one)
	CMP	a1, #0
	RSBMI	a1, a1, #0
	MOVMI	a4, #1			;b0 in a4 set iff final result r needs replacing by 2*one-r
	MOVPL	a4, #0			;now have arg in range [0,one)
	SUB	a2, a1, #&b500
	SUBS	a2, a2, #&0005
	BLT	acs16_arglow		;if arg < one/sqrt2 go and apply poly else first transmute into here
	ORR	a4, a4, #2		;b1 in a4 clear iff penultimate result r need be replaced by one-r
	MUL	a2, a1, a1
	MOV	a1, a2, LSR #16
	RSB	a1, a1, #65536
	sqrt16	a1, a1, a2, a3, ip, lr	;arg transmuted (ie replaced by one-sqrt(arg^2))
acs16_arglow
	ADD	a3, a1, a1, ASL #2
	ADD	a2, a3, a1, ASL #4
	ADD	a2, a2, a3, ASL #5
	ADD	a2, a2, a3, ASR	#8
	ADD	a2, a2, #32
	MOV	a1, a2, ASR #6		;range transform applied (ie replaced by arg*2sqrt2-one)
	SUB	a1, a1, #&10000		;so arg lies in range [-1,1]
	ADD	a2, a1, a1, LSL #2	;now apply poly
	ADD	a2, a2, a2, LSL #5
	MOV	a2, a2, ASR #14
	ADD	a2, a2, #&000500
	ADD	a2, a2, #&0000ae
        MOV	ip, a1
	mul16c	a2, ip, a2, a3
	ADD	a2, a2, #&000800
	ADD	a2, a2, #&000088
        MOV	ip, a1
	mul16c	a2, ip, a2, a3
	ADD	a2, a2, #&002000
	ADD	a2, a2, #&00009a
        MOV	ip, a1
	mul16c	a2, ip, a2, a3
	ADD	a2, a2, #&004600
	ADD	a2, a2, #&00009a
        MOV	ip, a1
	mul16c	a2, ip, a2, a3
	ADD	a2, a2, #&030000
	ADD	a2, a2, #&00d900
	ADD	a2, a2, #&0000b4
        MOV	ip, a1
	mul16c	a2, ip, a2, a3
	ADD	a2, a2, #&030000
	ADD	a2, a2, #&00ae00
	ADD	a2, a2, #&000052
	MOV	a1, a2, ASR #4
	TST	a4, #2
	RSBEQ	a1, a1, #65536
	TST	a4, #1
	RSBNE	a1, a1, #2*65536
	LDMFD	sp!, {pc}^



; asn16
; a leaf APCS function
;
; C prototype:
; int asn16(int a)
;
; returns 65536 * 2/pi * arcsin (a/65536)
;
; for a out of range (ie abs a > 65536), reset a to �65536 as appropriate before eval
;
; is about 24 times faster than FPEmulator working with double floats
;

        EXPORT  asn16

asnsta  DCB     "asn16", 0
        ALIGN
asnend  DCD     &ff000000 + asnend - asnsta

asn16

	CMP	a1, #65536
	MOVGE	a1, #65536
	MOVGES	pc, lr
	ADDS	a2, a1, #65536		;equiv to CMP a1,#-65536
	SUBLE	a1, a1, a2		;equiv to MOVLE a1,#-65536 (which wouldn't work due to bad constant)
	MOVLES	pc, lr
	STMFD	sp!, {lr}		;now have arg in range (-one,one)
	CMP	a1, #0
	RSBMI	a1, a1, #0
	MOVMI	a4, #1			;b0 in a4 set iff final result needs negating
	MOVPL	a4, #0			;now have arg in range [0,one)
	SUB	a2, a1, #&b500
	SUBS	a2, a2, #&0005
	BLT	asn16_arglow		;if arg < one/sqrt2 go and apply poly else first transmute into here
	ORR	a4, a4, #2		;b1 in a4 set iff penultimate result r needs to be replaced by one-r
	MUL	a2, a1, a1
	MOV	a1, a2, LSR #16
	RSB	a1, a1, #65536
	sqrt16	a1, a1, a2, a3, ip, lr	;arg transmuted (ie replaced by one-sqrt(arg^2))
asn16_arglow
	ADD	a3, a1, a1, ASL #2
	ADD	a2, a3, a1, ASL #4
	ADD	a2, a2, a3, ASL #5
	ADD	a2, a2, a3, ASR	#8
	ADD	a2, a2, #32
	MOV	a1, a2, ASR #6		;range transform applied (ie replaced by arg*2sqrt2-one)
	SUB	a1, a1, #&10000		;so arg lies in range [-1,1]
	ADD	a2, a1, a1, LSL #2	;now apply poly
	ADD	a2, a2, a2, LSL #5
	MOV	a2, a2, ASR #14
	ADD	a2, a2, #&000500
	ADD	a2, a2, #&0000ae
        MOV	ip, a1
	mul16c	a2, ip, a2, a3
	ADD	a2, a2, #&000800
	ADD	a2, a2, #&000088
        MOV	ip, a1
	mul16c	a2, ip, a2, a3
	ADD	a2, a2, #&002000
	ADD	a2, a2, #&00009a
        MOV	ip, a1
	mul16c	a2, ip, a2, a3
	ADD	a2, a2, #&004600
	ADD	a2, a2, #&00009a
        MOV	ip, a1
	mul16c	a2, ip, a2, a3
	ADD	a2, a2, #&030000
	ADD	a2, a2, #&00d900
	ADD	a2, a2, #&0000b4
        MOV	ip, a1
	mul16c	a2, ip, a2, a3
	ADD	a2, a2, #&030000
	ADD	a2, a2, #&00ae00
	ADD	a2, a2, #&000052
	MOV	a1, a2, ASR #4
	TST	a4, #2
	RSBNE	a1, a1, #65536
	TST	a4, #1
	RSBNE	a1, a1, #0
	LDMFD	sp!, {pc}^



; 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



; rand16
; a leaf APCS function
;
; C prototype:
; int rand16(void)
;
; returns 65536 * ( pseudo randon variable with approximate distribution U(-1,1) )
;

	EXPORT	rand16

ransta  DCB     "rand16", 0
        ALIGN
ranend  DCD     &ff000000 + ranend - ransta

rand16

	ADR	ip, randseed1
	LDMIA	ip, {a2, a3}
rand16_loop
	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	a1, a2, LSL #15
	MOV	a1, a1, ASR #15
	CMP	a1, #&ffff0000
	BEQ	rand16_loop
	STMIA	ip, {a2, a3}
        MOVS	pc, lr



; randu16
; a leaf APCS function
;
; C prototype:
; int randu16(void)
;
; returns 65536 * ( pseudo randon variable with approximate distribution U(0,1) )
;

	EXPORT	randu16

runsta  DCB     "randu16", 0
        ALIGN
runend  DCD     &ff000000 + runend - runsta

randu16

	ADR	ip, randseed1
	LDMIA	ip, {a2, a3}
randu16_loop
	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
	MOVS	a1, a2, LSR #16
	BEQ	randu16_loop
	STMIA	ip, {a2, a3}
        MOVS	pc, lr

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

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

	EXPORT	srand16

srnsta  DCB     "srand16", 0
        ALIGN
srnend  DCD     &ff000000 + srnend - srnsta

srand16

	ADR	a2, randseed1
	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_frac16
; a leaf APCS function
;
; C prototype:
; int sqrt_frac16(unsigned int x)
;
; returns 32-bit integer sqrt(x<<16)
;

        EXPORT  sqrt_frac16

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

sqrt_frac16

        sqrt16  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 53  09 70 63 2c 20 6c 72 0a  |..MOVGES.pc, lr.|
00001c80  09 43 4d 50 09 61 34 2c  20 23 2d 31 36 0a 09 4d  |.CMP.a4, #-16..M|
00001c90  4f 56 4c 54 09 61 31 2c  20 23 30 0a 09 4d 4f 56  |OVLT.a1, #0..MOV|
00001ca0  4c 54 53 09 70 63 2c 20  6c 72 09 09 09 3b 65 6c  |LTS.pc, lr...;el|
00001cb0  73 65 20 6e 65 65 64 20  74 6f 20 72 65 74 75 72  |se need to retur|
00001cc0  6e 20 76 61 6c 75 65 20  28 6f 6e 65 20 2a 20 32  |n value (one * 2|
00001cd0  5e 28 61 34 20 2b 20 61  31 2f 6f 6e 65 29 29 0a  |^(a4 + a1/one)).|
00001ce0  09 4d 4f 56 09 61 31 2c  20 61 31 2c 20 4c 53 4c  |.MOV.a1, a1, LSL|
00001cf0  20 23 31 09 09 3b 09 09  09 20 3d 20 28 6f 6e 65  | #1..;... = (one|
00001d00  20 2a 20 32 5e 28 61 31  2f 6f 6e 65 29 20 2a 20  | * 2^(a1/one) * |
00001d10  32 5e 61 34 29 0a 09 53  55 42 09 61 31 2c 20 61  |2^a4)..SUB.a1, a|
00001d20  31 2c 20 23 26 31 30 30  30 30 09 09 3b 63 6f 6e  |1, #&10000..;con|
00001d30  76 65 72 74 20 61 31 20  74 6f 20 6c 69 65 20 69  |vert a1 to lie i|
00001d40  6e 20 5b 2d 31 2c 2d 31  29 2c 20 74 68 65 6e 20  |n [-1,-1), then |
00001d50  61 70 70 6c 79 20 70 6f  6c 79 20 61 70 70 72 6f  |apply poly appro|
00001d60  78 69 6d 61 74 69 6f 6e  3a 0a 09 52 53 42 09 61  |ximation:..RSB.a|
00001d70  32 2c 20 61 31 2c 20 61  31 2c 20 4c 53 4c 20 23  |2, a1, a1, LSL #|
00001d80  33 0a 09 41 44 44 09 61  32 2c 20 61 31 2c 20 61  |3..ADD.a2, a1, a|
00001d90  32 2c 20 4c 53 4c 20 23  36 0a 09 4d 4f 56 09 61  |2, LSL #6..MOV.a|
00001da0  32 2c 20 61 32 2c 20 41  53 52 20 23 31 35 09 09  |2, a2, ASR #15..|
00001db0  3b 61 32 20 3d 20 30 2e  30 30 30 38 35 36 31 2a  |;a2 = 0.0008561*|
00001dc0  28 32 5e 32 30 29 2a 28  61 31 2f 6f 6e 65 29 0a  |(2^20)*(a1/one).|
00001dd0  09 41 44 44 09 61 32 2c  20 61 32 2c 20 23 26 30  |.ADD.a2, a2, #&0|
00001de0  30 32 38 30 30 0a 09 41  44 44 09 61 32 2c 20 61  |02800..ADD.a2, a|
00001df0  32 2c 20 23 26 30 30 30  30 37 65 09 3b 61 32 20  |2, #&00007e.;a2 |
00001e00  3d 20 30 2e 30 30 30 38  35 36 31 2a 28 32 5e 32  |= 0.0008561*(2^2|
00001e10  30 29 2a 28 61 31 2f 6f  6e 65 29 20 2b 20 30 2e  |0)*(a1/one) + 0.|
00001e20  30 30 39 38 38 35 37 2a  28 32 5e 32 30 29 0a 20  |0098857*(2^20). |
00001e30  20 20 20 20 20 20 20 4d  4f 56 09 69 70 2c 20 61  |       MOV.ip, a|
00001e40  31 0a 09 6d 75 6c 31 36  63 09 61 32 2c 20 69 70  |1..mul16c.a2, ip|
00001e50  2c 20 61 32 2c 20 61 33  09 09 3b 61 32 20 3d 20  |, a2, a3..;a2 = |
00001e60  30 2e 30 30 30 38 35 36  31 2a 28 32 5e 32 30 29  |0.0008561*(2^20)|
00001e70  2a 28 61 31 2f 6f 6e 65  29 5e 32 20 2b 20 30 2e  |*(a1/one)^2 + 0.|
00001e80  30 30 39 38 38 35 37 2a  28 32 5e 32 30 29 2a 28  |0098857*(2^20)*(|
00001e90  61 31 2f 6f 6e 65 29 0a  09 41 44 44 09 61 32 2c  |a1/one)..ADD.a2,|
00001ea0  20 61 32 2c 20 23 26 30  31 35 30 30 30 0a 09 41  | a2, #&015000..A|
00001eb0  44 44 09 61 32 2c 20 61  32 2c 20 23 26 30 30 30  |DD.a2, a2, #&000|
00001ec0  62 65 30 09 3b 20 65 74  63 0a 20 20 20 20 20 20  |be0.; etc.      |
00001ed0  20 20 4d 4f 56 09 69 70  2c 20 61 31 0a 09 6d 75  |  MOV.ip, a1..mu|
00001ee0  6c 31 36 63 09 61 32 2c  20 69 70 2c 20 61 32 2c  |l16c.a2, ip, a2,|
00001ef0  20 61 33 0a 09 41 44 44  09 61 32 2c 20 61 32 2c  | a3..ADD.a2, a2,|
00001f00  20 23 26 30 37 30 30 30  30 0a 09 41 44 44 09 61  | #&070000..ADD.a|
00001f10  32 2c 20 61 32 2c 20 23  26 30 30 64 37 30 30 0a  |2, a2, #&00d700.|
00001f20  09 41 44 44 09 61 32 2c  20 61 32 2c 20 23 26 30  |.ADD.a2, a2, #&0|
00001f30  30 30 30 37 65 0a 20 20  20 20 20 20 20 20 4d 4f  |0007e.        MO|
00001f40  56 09 69 70 2c 20 61 31  0a 09 6d 75 6c 31 36 63  |V.ip, a1..mul16c|
00001f50  09 61 32 2c 20 69 70 2c  20 61 32 2c 20 61 33 09  |.a2, ip, a2, a3.|
00001f60  09 3b 75 6e 74 69 6c 20  77 65 20 68 61 76 65 0a  |.;until we have.|
00001f70  09 41 44 44 09 61 32 2c  20 61 32 2c 20 23 26 31  |.ADD.a2, a2, #&1|
00001f80  36 30 30 30 30 09 3b 61  32 20 3d 20 28 32 5e 32  |60000.;a2 = (2^2|
00001f90  30 29 20 28 20 30 2e 30  30 30 38 35 36 31 28 61  |0) ( 0.0008561(a|
00001fa0  31 2f 6f 6e 65 29 5e 34  20 2b 20 30 2e 30 30 39  |1/one)^4 + 0.009|
00001fb0  38 38 35 37 28 61 31 2f  6f 6e 65 29 5e 33 20 2b  |8857(a1/one)^3 +|
00001fc0  0a 09 41 44 44 09 61 32  2c 20 61 32 2c 20 23 26  |..ADD.a2, a2, #&|
00001fd0  30 30 61 30 30 30 09 3b  09 20 20 20 20 20 20 20  |00a000.;.       |
00001fe0  30 2e 30 38 34 39 33 30  31 28 61 31 2f 6f 6e 65  |0.0849301(a1/one|
00001ff0  29 5e 32 20 2b 20 30 2e  34 39 30 31 31 30 36 28  |)^2 + 0.4901106(|
00002000  61 31 2f 6f 6e 65 29 20  2b 0a 09 41 44 44 09 61  |a1/one) +..ADD.a|
00002010  32 2c 20 61 32 2c 20 23  26 30 30 30 30 61 37 09  |2, a2, #&0000a7.|
00002020  3b 09 20 20 20 20 20 20  20 31 2e 31 34 31 34 32  |;.       1.14142|
00002030  31 33 38 20 20 20 20 20  20 20 20 20 20 20 20 20  |138             |
00002040  20 20 20 20 20 20 20 20  20 20 20 20 20 20 20 20  |                |
00002050  20 20 20 20 20 20 29 0a  09 4d 4f 56 09 61 31 2c  |      )..MOV.a1,|
00002060  20 61 32 2c 20 41 53 52  20 23 34 09 09 3b 6e 6f  | a2, ASR #4..;no|
00002070  77 20 64 69 76 69 64 65  20 62 79 20 31 36 20 72  |w divide by 16 r|
00002080  65 6d 6f 76 69 6e 67 20  6d 6f 73 74 20 74 72 75  |emoving most tru|
00002090  6e 63 61 74 69 6f 6e 20  65 72 72 6f 72 20 66 72  |ncation error fr|
000020a0  6f 6d 20 61 62 6f 76 65  20 63 61 6c 63 73 2c 0a  |om above calcs,.|
000020b0  09 43 4d 50 09 61 34 2c  20 23 30 09 09 09 3b 6c  |.CMP.a4, #0...;l|
000020c0  65 61 76 69 6e 67 20 61  32 20 3d 20 28 32 5e 31  |eaving a2 = (2^1|
000020d0  36 29 2a 28 61 70 70 72  6f 78 69 6d 61 74 65 64  |6)*(approximated|
000020e0  20 76 61 6c 75 65 29 0a  09 52 53 42 4c 54 09 61  | value)..RSBLT.a|
000020f0  34 2c 20 61 34 2c 20 23  30 0a 09 4d 4f 56 4c 54  |4, a4, #0..MOVLT|
00002100  09 61 31 2c 20 61 31 2c  20 41 53 52 20 61 34 09  |.a1, a1, ASR a4.|
00002110  09 3b 66 69 6e 61 6c 6c  79 20 63 61 72 72 79 20  |.;finally carry |
00002120  6f 75 74 20 74 68 65 20  28 61 32 20 3d 20 61 32  |out the (a2 = a2|
00002130  20 2a 20 32 5e 61 34 29  20 73 74 65 70 0a 09 4d  | * 2^a4) step..M|
00002140  4f 56 4c 54 53 09 70 63  2c 20 6c 72 0a 09 4d 4f  |OVLTS.pc, lr..MO|
00002150  56 53 09 61 31 2c 20 61  31 2c 20 4c 53 4c 20 61  |VS.a1, a1, LSL a|
00002160  34 0a 09 4d 4f 56 4d 49  09 61 31 2c 20 23 6d 61  |4..MOVMI.a1, #ma|
00002170  78 31 36 2b 31 0a 09 53  55 42 4d 49 09 61 31 2c  |x16+1..SUBMI.a1,|
00002180  20 61 31 2c 20 23 31 0a  20 20 20 20 20 20 20 20  | a1, #1.        |
00002190  4d 4f 56 53 20 20 20 20  70 63 2c 20 6c 72 0a 0a  |MOVS    pc, lr..|
000021a0  0a 0a 3b 20 73 69 67 31  36 0a 3b 20 61 20 6c 65  |..; sig16.; a le|
000021b0  61 66 20 41 50 43 53 20  66 75 6e 63 74 69 6f 6e  |af APCS function|
000021c0  0a 3b 0a 3b 20 43 20 70  72 6f 74 6f 74 79 70 65  |.;.; C prototype|
000021d0  3a 0a 3b 20 69 6e 74 20  73 69 67 31 36 28 69 6e  |:.; int sig16(in|
000021e0  74 20 61 29 0a 3b 0a 3b  20 72 65 74 75 72 6e 73  |t a).;.; returns|
000021f0  20 36 35 35 33 36 20 2f  20 28 20 31 20 2b 20 20  | 65536 / ( 1 +  |
00002200  65 78 70 20 28 2d 61 2f  36 35 35 33 36 29 20 29  |exp (-a/65536) )|
00002210  0a 3b 20 74 68 65 20 73  69 67 6d 6f 69 64 20 66  |.; the sigmoid f|
00002220  75 6e 63 74 69 6f 6e 20  66 6f 72 20 6e 65 75 72  |unction for neur|
00002230  61 6c 20 6e 65 74 20 63  6f 64 65 0a 3b 0a 3b 20  |al net code.;.; |
00002240  69 73 20 61 62 6f 75 74  20 35 33 20 74 69 6d 65  |is about 53 time|
00002250  73 20 66 61 73 74 65 72  20 74 68 61 6e 20 46 50  |s faster than FP|
00002260  45 6d 75 6c 61 74 6f 72  20 77 6f 72 6b 69 6e 67  |Emulator working|
00002270  20 77 69 74 68 20 64 6f  75 62 6c 65 20 66 6c 6f  | with double flo|
00002280  61 74 73 0a 3b 0a 0a 20  20 20 20 20 20 20 20 45  |ats.;..        E|
00002290  58 50 4f 52 54 20 20 73  69 67 31 36 0a 0a 73 64  |XPORT  sig16..sd|
000022a0  6e 73 74 61 20 20 44 43  42 20 20 20 20 20 22 73  |nsta  DCB     "s|
000022b0  69 67 31 36 22 2c 20 30  0a 20 20 20 20 20 20 20  |ig16", 0.       |
000022c0  20 41 4c 49 47 4e 0a 73  64 6e 65 6e 64 20 20 44  | ALIGN.sdnend  D|
000022d0  43 44 20 20 20 20 20 26  66 66 30 30 30 30 30 30  |CD     &ff000000|
000022e0  20 2b 20 73 64 6e 65 6e  64 20 2d 20 73 64 6e 73  | + sdnend - sdns|
000022f0  74 61 0a 0a 73 69 67 31  36 09 43 4d 50 09 61 31  |ta..sig16.CMP.a1|
00002300  2c 20 23 31 32 2a 36 35  35 33 36 0a 09 4d 4f 56  |, #12*65536..MOV|
00002310  47 54 09 61 31 2c 20 23  6f 6e 65 0a 09 4d 4f 56  |GT.a1, #one..MOV|
00002320  47 54 53 09 70 63 2c 20  6c 72 0a 09 43 4d 50 09  |GTS.pc, lr..CMP.|
00002330  61 31 2c 20 23 2d 31 32  2a 36 35 35 33 36 0a 09  |a1, #-12*65536..|
00002340  4d 4f 56 4c 54 09 61 31  2c 20 23 30 0a 09 4d 4f  |MOVLT.a1, #0..MO|
00002350  56 4c 54 53 09 70 63 2c  20 6c 72 09 09 09 3b 6f  |VLTS.pc, lr...;o|
00002360  74 68 65 72 77 69 73 65  20 6b 6e 6f 77 20 7c 61  |therwise know |a|
00002370  7c 20 3c 3d 20 31 32 6f  6e 65 0a 09 41 4e 44 53  || <= 12one..ANDS|
00002380  09 69 70 2c 20 61 31 2c  20 23 31 3c 3c 33 31 09  |.ip, a1, #1<<31.|
00002390  09 3b 63 6f 70 79 20 73  69 67 6e 20 6f 66 20 61  |.;copy sign of a|
000023a0  20 69 6e 74 6f 20 62 33  31 20 6f 66 20 69 70 0a  | into b31 of ip.|
000023b0  09 52 53 42 4d 49 09 61  31 2c 20 61 31 2c 20 23  |.RSBMI.a1, a1, #|
000023c0  30 09 09 3b 65 6e 73 75  72 65 20 61 3e 3d 30 0a  |0..;ensure a>=0.|
000023d0  09 52 53 42 09 61 32 2c  20 61 31 2c 20 61 31 2c  |.RSB.a2, a1, a1,|
000023e0  20 4c 53 4c 20 23 33 09  3b 6e 6f 77 20 73 65 74  | LSL #3.;now set|
000023f0  20 61 20 3d 20 61 2f 6c  6e 32 20 75 73 69 6e 67  | a = a/ln2 using|
00002400  20 65 78 70 6c 69 63 69  74 20 6d 75 6c 20 62 79  | explicit mul by|
00002410  20 32 5f 31 2e 30 31 31  31 30 30 30 31 30 31 30  | 2_1.01110001010|
00002420  31 30 31 30 30 30 31 31  31 0a 09 41 44 44 09 61  |101000111..ADD.a|
00002430  33 2c 20 61 31 2c 20 61  31 2c 20 4c 53 4c 20 23  |3, a1, a1, LSL #|
00002440  32 09 3b 6e 62 20 6f 6e  6c 79 20 6e 65 65 64 20  |2.;nb only need |
00002450  31 73 74 20 32 30 20 64  69 67 69 74 73 20 67 69  |1st 20 digits gi|
00002460  76 65 6e 20 72 61 6e 67  65 20 6f 6e 20 61 0a 09  |ven range on a..|
00002470  41 44 44 09 61 33 2c 20  61 33 2c 20 61 33 2c 20  |ADD.a3, a3, a3, |
00002480  4c 53 4c 20 23 34 09 3b  6e 62 32 20 74 68 69 73  |LSL #4.;nb2 this|
00002490  20 63 61 6c 63 20 69 73  20 61 70 70 72 6f 78 69  | calc is approxi|
000024a0  6d 61 74 65 20 6f 6e 6c  79 2c 20 74 68 6f 75 67  |mate only, thoug|
000024b0  68 20 77 69 6c 6c 20 75  73 75 61 6c 6c 79 20 79  |h will usually y|
000024c0  69 65 6c 64 20 61 6e 0a  09 41 44 44 09 61 31 2c  |ield an..ADD.a1,|
000024d0  20 61 32 2c 20 61 31 2c  20 4c 53 4c 20 23 34 09  | a2, a1, LSL #4.|
000024e0  3b 61 6e 73 77 65 72 20  63 6f 72 72 65 63 74 20  |;answer correct |
000024f0  74 6f 20 74 68 65 20 73  74 6f 72 65 64 20 31 36  |to the stored 16|
00002500  20 62 69 6e 61 72 79 20  70 6c 61 63 65 73 20 28  | binary places (|
00002510  65 6c 73 65 20 65 72 72  6f 72 20 6f 6e 65 2f 36  |else error one/6|
00002520  35 35 33 36 29 0a 09 41  44 44 09 61 31 2c 20 61  |5536)..ADD.a1, a|
00002530  31 2c 20 61 33 2c 20 41  53 52 20 23 31 30 0a 09  |1, a3, ASR #10..|
00002540  41 44 44 09 61 31 2c 20  61 31 2c 20 61 32 2c 20  |ADD.a1, a1, a2, |
00002550  41 53 52 20 23 31 36 09  3b 74 68 75 73 20 65 78  |ASR #16.;thus ex|
00002560  70 28 6f 72 69 67 69 6e  61 6c 20 61 29 20 20 3d  |p(original a)  =|
00002570  20 65 78 70 28 6e 65 77  20 61 20 2a 20 6c 6e 32  | exp(new a * ln2|
00002580  29 20 3d 20 32 5e 28 6e  65 77 20 61 29 2c 20 65  |) = 2^(new a), e|
00002590  76 61 6c 27 64 20 62 65  6c 6f 77 3a 0a 09 41 44  |val'd below:..AD|
000025a0  44 09 61 31 2c 20 61 31  2c 20 23 38 0a 09 4d 4f  |D.a1, a1, #8..MO|
000025b0  56 09 61 31 2c 20 61 31  2c 20 41 53 52 20 23 34  |V.a1, a1, ASR #4|
000025c0  09 09 3b 63 61 6c 63 20  61 20 3d 20 61 2f 6c 6e  |..;calc a = a/ln|
000025d0  32 20 63 6f 6d 70 6c 65  74 65 0a 09 4d 4f 56 09  |2 complete..MOV.|
000025e0  61 34 2c 20 61 31 2c 20  41 53 52 20 23 31 36 09  |a4, a1, ASR #16.|
000025f0  09 3b 61 34 20 6e 6f 77  20 68 6f 6c 64 73 20 69  |.;a4 now holds i|
00002600  6e 74 65 67 65 72 20 63  6f 6d 70 6f 6e 65 6e 74  |nteger component|
00002610  20 6f 66 20 61 0a 09 42  49 43 09 61 31 2c 20 61  | of a..BIC.a1, a|
00002620  31 2c 20 61 34 2c 20 4c  53 4c 20 23 31 36 09 3b  |1, a4, LSL #16.;|
00002630  61 31 20 6e 6f 77 20 68  6f 6c 64 73 20 66 72 61  |a1 now holds fra|
00002640  63 74 69 6f 6e 61 6c 20  63 6f 6d 70 6f 6e 65 6e  |ctional componen|
00002650  74 20 69 6e 20 72 61 6e  67 65 20 5b 30 2c 31 29  |t in range [0,1)|
00002660  2a 6f 6e 65 0a 09 43 4d  50 09 61 34 2c 20 23 31  |*one..CMP.a4, #1|
00002670  35 09 09 09 3b 64 6f 20  61 6e 6f 74 68 65 72 20  |5...;do another |
00002680  63 68 65 63 6b 20 6f 6e  20 61 20 74 6f 20 65 6e  |check on a to en|
00002690  73 75 72 65 20 65 78 70  28 61 29 20 69 73 20 69  |sure exp(a) is i|
000026a0  6e 20 72 61 6e 67 65 0a  09 42 4c 54 09 73 69 67  |n range..BLT.sig|
000026b0  31 36 5f 6c 31 0a 09 4d  4f 56 09 61 31 2c 20 23  |16_l1..MOV.a1, #|
000026c0  31 09 09 09 3b 26 20 69  66 20 6e 6f 74 2c 20 72  |1...;& if not, r|
000026d0  65 74 75 72 6e 20 6e 65  61 72 20 6d 61 78 69 6d  |eturn near maxim|
000026e0  61 6c 20 6f 72 20 6d 69  6e 69 6d 61 6c 20 76 61  |al or minimal va|
000026f0  6c 75 65 20 61 73 20 61  70 70 72 6f 70 72 69 61  |lue as appropria|
00002700  74 65 0a 09 54 53 54 09  69 70 2c 20 23 31 3c 3c  |te..TST.ip, #1<<|
00002710  33 31 0a 09 52 53 42 45  51 09 61 31 2c 20 61 31  |31..RSBEQ.a1, a1|
00002720  2c 20 23 6f 6e 65 0a 09  4d 4f 56 53 09 70 63 2c  |, #one..MOVS.pc,|
00002730  6c 72 0a 73 69 67 31 36  5f 6c 31 0a 09 4f 52 52  |lr.sig16_l1..ORR|
00002740  09 61 34 2c 20 61 34 2c  20 69 70 09 09 3b 28 74  |.a4, a4, ip..;(t|
00002750  65 6d 70 6f 72 61 72 69  6c 79 20 73 74 6f 72 65  |emporarily store|
00002760  20 73 69 67 6e 20 6f 66  20 6f 72 69 67 69 6e 61  | sign of origina|
00002770  6c 20 61 20 69 6e 20 61  34 29 0a 09 4d 4f 56 09  |l a in a4)..MOV.|
00002780  61 31 2c 20 61 31 2c 20  4c 53 4c 20 23 31 09 09  |a1, a1, LSL #1..|
00002790  3b 65 6c 73 65 20 6e 65  65 64 20 20 76 61 6c 75  |;else need  valu|
000027a0  65 20 28 6f 6e 65 20 2a  20 32 5e 28 61 31 2f 6f  |e (one * 2^(a1/o|
000027b0  6e 65 29 20 2a 20 32 5e  61 34 29 0a 09 53 55 42  |ne) * 2^a4)..SUB|
000027c0  09 61 31 2c 20 61 31 2c  20 23 26 31 30 30 30 30  |.a1, a1, #&10000|
000027d0  09 09 3b 63 6f 6e 76 65  72 74 20 61 31 20 74 6f  |..;convert a1 to|
000027e0  20 6c 69 65 20 69 6e 20  5b 2d 31 2c 2d 31 29 2c  | lie in [-1,-1),|
000027f0  20 74 68 65 6e 20 61 70  70 6c 79 20 70 6f 6c 79  | then apply poly|
00002800  20 61 70 70 72 6f 78 69  6d 61 74 69 6f 6e 3a 0a  | approximation:.|
00002810  09 52 53 42 09 61 32 2c  20 61 31 2c 20 61 31 2c  |.RSB.a2, a1, a1,|
00002820  20 4c 53 4c 20 23 33 0a  09 41 44 44 09 61 32 2c  | LSL #3..ADD.a2,|
00002830  20 61 31 2c 20 61 32 2c  20 4c 53 4c 20 23 36 0a  | a1, a2, LSL #6.|
00002840  09 4d 4f 56 09 61 32 2c  20 61 32 2c 20 41 53 52  |.MOV.a2, a2, ASR|
00002850  20 23 31 35 09 09 3b 61  32 20 3d 20 30 2e 30 30  | #15..;a2 = 0.00|
00002860  30 38 35 36 31 2a 28 32  5e 32 30 29 2a 28 61 31  |08561*(2^20)*(a1|
00002870  2f 6f 6e 65 29 0a 09 41  44 44 09 61 32 2c 20 61  |/one)..ADD.a2, a|
00002880  32 2c 20 23 26 30 30 32  38 30 30 0a 09 41 44 44  |2, #&002800..ADD|
00002890  09 61 32 2c 20 61 32 2c  20 23 26 30 30 30 30 37  |.a2, a2, #&00007|
000028a0  65 09 3b 61 32 20 3d 20  30 2e 30 30 30 38 35 36  |e.;a2 = 0.000856|
000028b0  31 2a 28 32 5e 32 30 29  2a 28 61 31 2f 6f 6e 65  |1*(2^20)*(a1/one|
000028c0  29 20 2b 20 30 2e 30 30  39 38 38 35 37 2a 28 32  |) + 0.0098857*(2|
000028d0  5e 32 30 29 0a 20 20 20  20 20 20 20 20 4d 4f 56  |^20).        MOV|
000028e0  09 69 70 2c 20 61 31 0a  09 6d 75 6c 31 36 63 09  |.ip, a1..mul16c.|
000028f0  61 32 2c 20 69 70 2c 20  61 32 2c 20 61 33 09 09  |a2, ip, a2, a3..|
00002900  3b 61 32 20 3d 20 30 2e  30 30 30 38 35 36 31 2a  |;a2 = 0.0008561*|
00002910  28 32 5e 32 30 29 2a 28  61 31 2f 6f 6e 65 29 5e  |(2^20)*(a1/one)^|
00002920  32 20 2b 20 30 2e 30 30  39 38 38 35 37 2a 28 32  |2 + 0.0098857*(2|
00002930  5e 32 30 29 2a 28 61 31  2f 6f 6e 65 29 0a 09 41  |^20)*(a1/one)..A|
00002940  44 44 09 61 32 2c 20 61  32 2c 20 23 26 30 31 35  |DD.a2, a2, #&015|
00002950  30 30 30 0a 09 41 44 44  09 61 32 2c 20 61 32 2c  |000..ADD.a2, a2,|
00002960  20 23 26 30 30 30 62 65  30 09 3b 20 65 74 63 0a  | #&000be0.; etc.|
00002970  20 20 20 20 20 20 20 20  4d 4f 56 09 69 70 2c 20  |        MOV.ip, |
00002980  61 31 0a 09 6d 75 6c 31  36 63 09 61 32 2c 20 69  |a1..mul16c.a2, i|
00002990  70 2c 20 61 32 2c 20 61  33 0a 09 41 44 44 09 61  |p, a2, a3..ADD.a|
000029a0  32 2c 20 61 32 2c 20 23  26 30 37 30 30 30 30 0a  |2, a2, #&070000.|
000029b0  09 41 44 44 09 61 32 2c  20 61 32 2c 20 23 26 30  |.ADD.a2, a2, #&0|
000029c0  30 64 37 30 30 0a 09 41  44 44 09 61 32 2c 20 61  |0d700..ADD.a2, a|
000029d0  32 2c 20 23 26 30 30 30  30 37 65 0a 20 20 20 20  |2, #&00007e.    |
000029e0  20 20 20 20 4d 4f 56 09  69 70 2c 20 61 31 0a 09  |    MOV.ip, a1..|
000029f0  6d 75 6c 31 36 63 09 61  32 2c 20 69 70 2c 20 61  |mul16c.a2, ip, a|
00002a00  32 2c 20 61 33 09 09 3b  75 6e 74 69 6c 20 77 65  |2, a3..;until we|
00002a10  20 68 61 76 65 0a 09 41  44 44 09 61 32 2c 20 61  | have..ADD.a2, a|
00002a20  32 2c 20 23 26 31 36 30  30 30 30 09 3b 61 32 20  |2, #&160000.;a2 |
00002a30  3d 20 28 32 5e 32 30 29  20 28 20 30 2e 30 30 30  |= (2^20) ( 0.000|
00002a40  38 35 36 31 28 61 31 2f  6f 6e 65 29 5e 34 20 2b  |8561(a1/one)^4 +|
00002a50  20 30 2e 30 30 39 38 38  35 37 28 61 31 2f 6f 6e  | 0.0098857(a1/on|
00002a60  65 29 5e 33 20 2b 0a 09  41 44 44 09 61 32 2c 20  |e)^3 +..ADD.a2, |
00002a70  61 32 2c 20 23 26 30 30  61 30 30 30 09 3b 09 20  |a2, #&00a000.;. |
00002a80  20 20 20 20 20 20 30 2e  30 38 34 39 33 30 31 28  |      0.0849301(|
00002a90  61 31 2f 6f 6e 65 29 5e  32 20 2b 20 30 2e 34 39  |a1/one)^2 + 0.49|
00002aa0  30 31 31 30 36 28 61 31  2f 6f 6e 65 29 20 2b 0a  |01106(a1/one) +.|
00002ab0  09 41 44 44 09 61 32 2c  20 61 32 2c 20 23 26 30  |.ADD.a2, a2, #&0|
00002ac0  30 30 30 61 37 09 3b 09  20 20 20 20 20 20 20 31  |000a7.;.       1|
00002ad0  2e 31 34 31 34 32 31 33  38 20 20 20 20 20 20 20  |.14142138       |
00002ae0  20 20 20 20 20 20 20 20  20 20 20 20 20 20 20 20  |                |
00002af0  20 20 20 20 20 20 20 20  20 20 20 20 29 0a 09 4d  |            )..M|
00002b00  4f 56 09 61 31 2c 20 61  32 2c 20 41 53 52 20 23  |OV.a1, a2, ASR #|
00002b10  34 09 09 3b 6e 6f 77 20  64 69 76 69 64 65 20 62  |4..;now divide b|
00002b20  79 20 31 36 20 72 65 6d  6f 76 69 6e 67 20 6d 6f  |y 16 removing mo|
00002b30  73 74 20 74 72 75 6e 63  61 74 69 6f 6e 20 65 72  |st truncation er|
00002b40  72 6f 72 20 66 72 6f 6d  20 61 62 6f 76 65 20 63  |ror from above c|
00002b50  61 6c 63 73 2c 0a 09 09  09 09 09 3b 6c 65 61 76  |alcs,......;leav|
00002b60  69 6e 67 20 61 32 20 3d  20 28 32 5e 31 36 29 2a  |ing a2 = (2^16)*|
00002b70  28 61 70 70 72 6f 78 69  6d 61 74 65 64 20 76 61  |(approximated va|
00002b80  6c 75 65 29 0a 09 42 49  43 09 69 70 2c 20 61 34  |lue)..BIC.ip, a4|
00002b90  2c 20 23 31 3c 3c 33 31  09 09 3b 72 65 6d 6f 76  |, #1<<31..;remov|
00002ba0  65 20 74 65 6d 70 20 73  69 67 6e 20 62 69 74 20  |e temp sign bit |
00002bb0  66 72 6f 6d 20 61 34 0a  09 4d 4f 56 53 09 61 32  |from a4..MOVS.a2|
00002bc0  2c 20 61 31 2c 20 4c 53  4c 20 69 70 09 09 3b 66  |, a1, LSL ip..;f|
00002bd0  69 6e 61 6c 6c 79 20 63  61 72 72 79 20 6f 75 74  |inally carry out|
00002be0  20 74 68 65 20 28 61 32  20 3d 20 61 32 20 2a 20  | the (a2 = a2 * |
00002bf0  32 5e 61 34 29 20 73 74  65 70 0a 09 41 44 44 43  |2^a4) step..ADDC|
00002c00  43 53 09 61 32 2c 20 61  32 2c 20 23 6f 6e 65 09  |CS.a2, a2, #one.|
00002c10  09 3b 74 68 65 6e 20 61  64 64 20 69 6e 20 6f 6e  |.;then add in on|
00002c20  65 0a 09 4d 4f 56 43 53  09 61 31 2c 20 61 34 2c  |e..MOVCS.a1, a4,|
00002c30  20 4c 53 52 20 23 31 35  09 09 3b 26 20 69 66 20  | LSR #15..;& if |
00002c40  6f 76 65 72 66 6c 6f 77  2c 20 72 65 74 75 72 6e  |overflow, return|
00002c50  20 6d 61 78 69 6d 61 6c  20 6f 72 20 6d 69 6e 69  | maximal or mini|
00002c60  6d 61 6c 20 76 61 6c 75  65 20 61 73 20 61 70 70  |mal value as app|
00002c70  72 6f 70 72 69 61 74 65  0a 09 45 4f 52 43 53 09  |ropriate..EORCS.|
00002c80  61 31 2c 20 61 31 2c 20  23 6f 6e 65 0a 09 4d 4f  |a1, a1, #one..MO|
00002c90  56 43 53 53 09 70 63 2c  20 6c 72 0a 09 4d 4f 56  |VCSS.pc, lr..MOV|
00002ca0  09 61 31 2c 20 23 30 09  09 09 3b 72 65 73 75 6c  |.a1, #0...;resul|
00002cb0  74 20 66 6f 72 20 6f 70  65 72 61 74 69 6f 6e 20  |t for operation |
00002cc0  61 31 20 3d 20 32 5e 33  32 20 2f 20 61 32 0a 09  |a1 = 2^32 / a2..|
00002cd0  4d 4f 56 09 61 33 2c 20  23 31 3c 3c 31 36 09 09  |MOV.a3, #1<<16..|
00002ce0  3b 72 65 6d 61 69 6e 64  65 72 20 72 65 67 0a 09  |;remainder reg..|
00002cf0  47 42 4c 41 09 63 6f 75  6e 74 65 72 0a 63 6f 75  |GBLA.counter.cou|
00002d00  6e 74 65 72 09 53 45 54  41 09 31 36 0a 09 57 48  |nter.SETA.16..WH|
00002d10  49 4c 45 09 63 6f 75 6e  74 65 72 20 3e 20 30 0a  |ILE.counter > 0.|
00002d20  09 41 44 44 09 61 31 2c  20 61 31 2c 20 61 31 0a  |.ADD.a1, a1, a1.|
00002d30  09 41 44 44 09 61 33 2c  20 61 33 2c 20 61 33 0a  |.ADD.a3, a3, a3.|
00002d40  09 43 4d 50 09 61 33 2c  20 61 32 0a 09 53 55 42  |.CMP.a3, a2..SUB|
00002d50  48 53 09 61 33 2c 20 61  33 2c 20 61 32 0a 09 4f  |HS.a3, a3, a2..O|
00002d60  52 52 48 53 09 61 31 2c  20 61 31 2c 20 23 31 0a  |RRHS.a1, a1, #1.|
00002d70  63 6f 75 6e 74 65 72 20  53 45 54 41 09 63 6f 75  |counter SETA.cou|
00002d80  6e 74 65 72 2d 31 0a 09  57 45 4e 44 09 09 09 09  |nter-1..WEND....|
00002d90  3b 72 65 63 69 70 72 6f  63 61 6c 20 63 61 6c 63  |;reciprocal calc|
00002da0  75 6c 61 74 65 64 0a 09  43 4d 50 09 61 32 2c 20  |ulated..CMP.a2, |
00002db0  61 33 2c 20 4c 53 4c 20  23 31 0a 09 41 44 44 4c  |a3, LSL #1..ADDL|
00002dc0  53 09 61 31 2c 20 61 31  2c 20 23 31 0a 09 54 53  |S.a1, a1, #1..TS|
00002dd0  54 09 61 34 2c 20 23 31  3c 3c 33 31 09 09 3b 61  |T.a4, #1<<31..;a|
00002de0  6e 64 20 66 69 6e 61 6c  6c 79 2c 20 69 66 20 6f  |nd finally, if o|
00002df0  72 69 67 69 6e 61 6c 20  61 72 67 75 6d 65 6e 74  |riginal argument|
00002e00  20 3e 20 30 0a 09 52 53  42 45 51 09 61 31 2c 20  | > 0..RSBEQ.a1, |
00002e10  61 31 2c 20 23 6f 6e 65  09 09 3b 74 68 65 6e 20  |a1, #one..;then |
00002e20  66 6c 69 70 20 76 61 6c  75 65 20 61 62 6f 75 74  |flip value about|
00002e30  20 79 3d 31 2f 32 20 6c  69 6e 65 20 69 6e 20 67  | y=1/2 line in g|
00002e40  72 61 70 68 20 6f 66 20  73 69 67 20 66 6e 0a 20  |raph of sig fn. |
00002e50  20 20 20 20 20 20 20 4d  4f 56 53 20 20 20 20 70  |       MOVS    p|
00002e60  63 2c 20 6c 72 0a 0a 0a  0a 3b 20 63 6f 73 31 36  |c, lr....; cos16|
00002e70  0a 3b 20 61 20 6c 65 61  66 20 41 50 43 53 20 66  |.; a leaf APCS f|
00002e80  75 6e 63 74 69 6f 6e 0a  3b 0a 3b 20 43 20 70 72  |unction.;.; C pr|
00002e90  6f 74 6f 74 79 70 65 3a  0a 3b 20 69 6e 74 20 63  |ototype:.; int c|
00002ea0  6f 73 31 36 28 69 6e 74  20 61 29 0a 3b 0a 3b 20  |os16(int a).;.; |
00002eb0  72 65 74 75 72 6e 73 20  36 35 35 33 36 20 2a 20  |returns 65536 * |
00002ec0  63 6f 73 20 28 28 61 2f  36 35 35 33 36 29 28 70  |cos ((a/65536)(p|
00002ed0  69 2f 32 29 29 0a 3b 0a  3b 20 69 73 20 61 62 6f  |i/2)).;.; is abo|
00002ee0  75 74 20 34 34 20 74 69  6d 65 73 20 66 61 73 74  |ut 44 times fast|
00002ef0  65 72 20 74 68 61 6e 20  46 50 45 6d 75 6c 61 74  |er than FPEmulat|
00002f00  6f 72 20 77 6f 72 6b 69  6e 67 20 77 69 74 68 20  |or working with |
00002f10  64 6f 75 62 6c 65 20 66  6c 6f 61 74 73 0a 3b 0a  |double floats.;.|
00002f20  0a 20 20 20 20 20 20 20  20 45 58 50 4f 52 54 20  |.        EXPORT |
00002f30  20 63 6f 73 31 36 0a 0a  63 73 6e 73 74 61 20 20  | cos16..csnsta  |
00002f40  44 43 42 20 20 20 20 20  22 63 6f 73 31 36 22 2c  |DCB     "cos16",|
00002f50  20 30 0a 20 20 20 20 20  20 20 20 41 4c 49 47 4e  | 0.        ALIGN|
00002f60  0a 63 73 6e 65 6e 64 20  20 44 43 44 20 20 20 20  |.csnend  DCD    |
00002f70  20 26 66 66 30 30 30 30  30 30 20 2b 20 63 73 6e  | &ff000000 + csn|
00002f80  65 6e 64 20 2d 20 63 73  6e 73 74 61 0a 0a 63 6f  |end - csnsta..co|
00002f90  73 31 36 0a 0a 09 45 4f  52 09 61 34 2c 20 61 31  |s16...EOR.a4, a1|
00002fa0  2c 20 61 31 2c 20 4c 53  4c 20 23 31 09 3b 73 69  |, a1, LSL #1.;si|
00002fb0  6e 63 65 20 63 6f 73 20  69 73 20 70 65 72 69 6f  |nce cos is perio|
00002fc0  64 69 63 20 61 6e 64 20  63 6f 73 28 28 61 2f 36  |dic and cos((a/6|
00002fd0  35 35 33 36 29 28 70 69  2f 32 29 29 20 68 61 73  |5536)(pi/2)) has|
00002fe0  20 70 65 72 69 6f 64 20  34 2a 6f 6e 65 0a 09 54  | period 4*one..T|
00002ff0  53 54 09 61 34 2c 20 23  26 32 30 30 30 30 09 09  |ST.a4, #&20000..|
00003000  3b 65 76 61 6c 20 69 73  20 73 69 6d 70 6c 65 72  |;eval is simpler|
00003010  20 74 68 61 6e 20 66 6f  72 20 6c 6e 20 6f 72 20  | than for ln or |
00003020  65 78 70 3a 0a 09 4d 4f  56 09 61 34 2c 20 23 30  |exp:..MOV.a4, #0|
00003030  09 09 09 3b 77 65 20 6e  65 65 64 20 6f 6e 6c 79  |...;we need only|
00003040  20 74 6f 20 74 72 75 6e  63 61 74 65 20 61 20 74  | to truncate a t|
00003050  6f 20 5b 30 2c 34 6f 6e  65 29 20 61 6e 64 20 74  |o [0,4one) and t|
00003060  68 65 6e 20 61 70 70 72  6f 78 20 74 68 65 20 66  |hen approx the f|
00003070  75 6e 63 74 69 6f 6e 0a  09 4d 4f 56 4e 45 09 61  |unction..MOVNE.a|
00003080  34 2c 20 23 31 09 09 09  3b 76 69 61 20 61 20 70  |4, #1...;via a p|
00003090  6f 6c 79 0a 09 54 53 54  09 61 31 2c 20 23 26 31  |oly..TST.a1, #&1|
000030a0  30 30 30 30 0a 09 4d 4f  56 09 61 31 2c 20 61 31  |0000..MOV.a1, a1|
000030b0  2c 20 4c 53 4c 20 23 31  36 09 09 3b 69 6e 20 66  |, LSL #16..;in f|
000030c0  61 63 74 20 66 75 72 74  68 65 72 20 73 79 6d 6d  |act further symm|
000030d0  65 74 72 69 65 73 20 69  6e 20 63 6f 73 20 6f 76  |etries in cos ov|
000030e0  65 72 20 74 68 69 73 20  72 61 6e 67 65 20 61 6c  |er this range al|
000030f0  6c 6f 77 20 75 73 20 74  6f 0a 09 4d 4f 56 09 61  |low us to..MOV.a|
00003100  31 2c 20 61 31 2c 20 4c  53 52 20 23 31 36 09 09  |1, a1, LSR #16..|
00003110  3b 61 63 74 75 61 6c 6c  79 20 6f 6e 6c 79 20 61  |;actually only a|
00003120  70 70 72 6f 78 69 6d 61  74 65 20 66 75 6e 63 74  |pproximate funct|
00003130  69 6f 6e 20 6f 76 65 72  20 72 61 6e 67 65 20 5b  |ion over range [|
00003140  30 2c 6f 6e 65 29 0a 09  52 53 42 45 51 09 61 31  |0,one)..RSBEQ.a1|
00003150  2c 20 61 31 2c 20 23 26  31 30 30 30 30 09 09 3b  |, a1, #&10000..;|
00003160  61 6e 64 20 72 65 63 6f  6e 73 74 72 75 63 74 20  |and reconstruct |
00003170  69 74 20 6f 76 65 72 20  72 65 6d 61 69 6e 64 65  |it over remainde|
00003180  72 20 6f 66 20 72 61 6e  67 65 20 5b 6f 6e 65 2c  |r of range [one,|
00003190  34 6f 6e 65 29 20 66 72  6f 6d 20 74 68 61 74 20  |4one) from that |
000031a0  70 61 72 74 0a 09 4d 4f  56 09 61 31 2c 20 61 31  |part..MOV.a1, a1|
000031b0  2c 20 4c 53 4c 20 23 31  09 09 3b 65 67 20 20 63  |, LSL #1..;eg  c|
000031c0  6f 73 28 20 28 61 2f 36  35 35 33 36 29 28 70 69  |os( (a/65536)(pi|
000031d0  2f 32 29 20 29 20 66 6f  72 20 61 20 69 6e 20 5b  |/2) ) for a in [|
000031e0  32 6f 6e 65 2c 20 33 6f  6e 65 29 0a 09 53 55 42  |2one, 3one)..SUB|
000031f0  09 61 31 2c 20 61 31 2c  20 23 26 31 30 30 30 30  |.a1, a1, #&10000|
00003200  09 09 3b 20 3d 20 2d 63  6f 73 28 20 28 28 61 2d  |..; = -cos( ((a-|
00003210  32 6f 6e 65 29 2f 36 35  35 33 36 29 28 70 69 2f  |2one)/65536)(pi/|
00003220  32 29 20 29 0a 09 52 53  42 09 61 32 2c 20 61 31  |2) )..RSB.a2, a1|
00003230  2c 20 61 31 2c 20 4c 53  4c 20 23 33 0a 09 41 44  |, a1, LSL #3..AD|
00003240  44 09 61 32 2c 20 61 31  2c 20 61 32 2c 20 4c 53  |D.a2, a1, a2, LS|
00003250  4c 20 23 38 0a 09 4d 4f  56 09 61 32 2c 20 61 32  |L #8..MOV.a2, a2|
00003260  2c 20 41 53 52 20 23 31  36 0a 09 41 44 44 09 61  |, ASR #16..ADD.a|
00003270  32 2c 20 61 32 2c 20 23  26 30 30 32 63 30 30 0a  |2, a2, #&002c00.|
00003280  09 41 44 44 09 61 32 2c  20 61 32 2c 20 23 26 30  |.ADD.a2, a2, #&0|
00003290  30 30 30 38 36 0a 20 20  20 20 20 20 20 20 4d 4f  |00086.        MO|
000032a0  56 09 69 70 2c 20 61 31  0a 09 6d 75 6c 31 36 63  |V.ip, a1..mul16c|
000032b0  09 61 32 2c 20 69 70 2c  20 61 32 2c 20 61 33 0a  |.a2, ip, a2, a3.|
000032c0  09 53 55 42 09 61 32 2c  20 61 32 2c 20 23 26 30  |.SUB.a2, a2, #&0|
000032d0  30 65 39 30 30 0a 09 53  55 42 09 61 32 2c 20 61  |0e900..SUB.a2, a|
000032e0  32 2c 20 23 26 30 30 30  30 62 64 0a 20 20 20 20  |2, #&0000bd.    |
000032f0  20 20 20 20 4d 4f 56 09  69 70 2c 20 61 31 0a 09  |    MOV.ip, a1..|
00003300  6d 75 6c 31 36 63 09 61  32 2c 20 69 70 2c 20 61  |mul16c.a2, ip, a|
00003310  32 2c 20 61 33 0a 09 53  55 42 09 61 32 2c 20 61  |2, a3..SUB.a2, a|
00003320  32 2c 20 23 26 30 33 30  30 30 30 0a 09 53 55 42  |2, #&030000..SUB|
00003330  09 61 32 2c 20 61 32 2c  20 23 26 30 30 37 63 30  |.a2, a2, #&007c0|
00003340  30 0a 09 53 55 42 09 61  32 2c 20 61 32 2c 20 23  |0..SUB.a2, a2, #|
00003350  26 30 30 30 30 63 36 0a  20 20 20 20 20 20 20 20  |&0000c6.        |
00003360  4d 4f 56 09 69 70 2c 20  61 31 0a 09 6d 75 6c 31  |MOV.ip, a1..mul1|
00003370  36 63 09 61 32 2c 20 69  70 2c 20 61 32 2c 20 61  |6c.a2, ip, a2, a|
00003380  33 0a 09 41 44 44 09 61  32 2c 20 61 32 2c 20 23  |3..ADD.a2, a2, #|
00003390  26 30 38 30 30 30 30 0a  09 41 44 44 09 61 32 2c  |&080000..ADD.a2,|
000033a0  20 61 32 2c 20 23 26 30  30 45 32 30 30 0a 09 41  | a2, #&00E200..A|
000033b0  44 44 09 61 32 2c 20 61  32 2c 20 23 26 30 30 30  |DD.a2, a2, #&000|
000033c0  30 42 44 0a 20 20 20 20  20 20 20 20 4d 4f 56 09  |0BD.        MOV.|
000033d0  69 70 2c 20 61 31 0a 09  6d 75 6c 31 36 63 09 61  |ip, a1..mul16c.a|
000033e0  32 2c 20 69 70 2c 20 61  32 2c 20 61 33 0a 09 41  |2, ip, a2, a3..A|
000033f0  44 44 09 61 32 2c 20 61  32 2c 20 23 26 30 62 30  |DD.a2, a2, #&0b0|
00003400  30 30 30 0a 09 41 44 44  09 61 32 2c 20 61 32 2c  |000..ADD.a2, a2,|
00003410  20 23 26 30 30 35 30 30  30 0a 09 41 44 44 09 61  | #&005000..ADD.a|
00003420  32 2c 20 61 32 2c 20 23  26 30 30 30 30 35 30 0a  |2, a2, #&000050.|
00003430  09 4d 4f 56 09 61 31 2c  20 61 32 2c 20 41 53 52  |.MOV.a1, a2, ASR|
00003440  20 23 34 0a 09 54 53 54  09 61 34 2c 20 23 31 0a  | #4..TST.a4, #1.|
00003450  09 52 53 42 4e 45 09 61  31 2c 20 61 31 2c 20 23  |.RSBNE.a1, a1, #|
00003460  30 0a 20 20 20 20 20 20  20 20 4d 4f 56 53 20 20  |0.        MOVS  |
00003470  20 20 70 63 2c 20 6c 72  0a 0a 0a 0a 3b 20 73 69  |  pc, lr....; si|
00003480  6e 31 36 0a 3b 20 61 20  6c 65 61 66 20 41 50 43  |n16.; a leaf APC|
00003490  53 20 66 75 6e 63 74 69  6f 6e 0a 3b 0a 3b 20 43  |S function.;.; C|
000034a0  20 70 72 6f 74 6f 74 79  70 65 3a 0a 3b 20 69 6e  | prototype:.; in|
000034b0  74 20 73 69 6e 31 36 28  69 6e 74 20 61 29 0a 3b  |t sin16(int a).;|
000034c0  0a 3b 20 72 65 74 75 72  6e 73 20 36 35 35 33 36  |.; returns 65536|
000034d0  20 2a 20 73 69 6e 20 28  28 61 2f 36 35 35 33 36  | * sin ((a/65536|
000034e0  29 28 70 69 2f 32 29 29  0a 3b 0a 3b 20 69 73 20  |)(pi/2)).;.; is |
000034f0  61 62 6f 75 74 20 34 34  20 74 69 6d 65 73 20 66  |about 44 times f|
00003500  61 73 74 65 72 20 74 68  61 6e 20 46 50 45 6d 75  |aster than FPEmu|
00003510  6c 61 74 6f 72 20 77 6f  72 6b 69 6e 67 20 77 69  |lator working wi|
00003520  74 68 20 64 6f 75 62 6c  65 20 66 6c 6f 61 74 73  |th double floats|
00003530  0a 3b 0a 0a 20 20 20 20  20 20 20 20 45 58 50 4f  |.;..        EXPO|
00003540  52 54 20 20 73 69 6e 31  36 0a 0a 73 6e 6e 73 74  |RT  sin16..snnst|
00003550  61 20 20 44 43 42 20 20  20 20 20 22 73 69 6e 31  |a  DCB     "sin1|
00003560  36 22 2c 20 30 0a 20 20  20 20 20 20 20 20 41 4c  |6", 0.        AL|
00003570  49 47 4e 0a 73 6e 6e 65  6e 64 20 20 44 43 44 20  |IGN.snnend  DCD |
00003580  20 20 20 20 26 66 66 30  30 30 30 30 30 20 2b 20  |    &ff000000 + |
00003590  73 6e 6e 65 6e 64 20 2d  20 73 6e 6e 73 74 61 0a  |snnend - snnsta.|
000035a0  0a 73 69 6e 31 36 0a 0a  09 54 53 54 09 61 31 2c  |.sin16...TST.a1,|
000035b0  20 23 26 32 30 30 30 30  09 09 3b 65 76 61 6c 20  | #&20000..;eval |
000035c0  6f 66 20 73 69 6e 20 69  73 20 61 6c 6d 6f 73 74  |of sin is almost|
000035d0  20 69 64 65 6e 74 69 63  61 6c 20 74 6f 20 74 68  | identical to th|
000035e0  61 74 20 6f 66 20 63 6f  73 0a 09 4d 4f 56 09 61  |at of cos..MOV.a|
000035f0  34 2c 20 23 30 0a 09 4d  4f 56 4e 45 09 61 34 2c  |4, #0..MOVNE.a4,|
00003600  20 23 31 0a 09 54 53 54  09 61 31 2c 20 23 26 31  | #1..TST.a1, #&1|
00003610  30 30 30 30 0a 09 4d 4f  56 09 61 31 2c 20 61 31  |0000..MOV.a1, a1|
00003620  2c 20 4c 53 4c 20 23 31  36 0a 09 4d 4f 56 09 61  |, LSL #16..MOV.a|
00003630  31 2c 20 61 31 2c 20 4c  53 52 20 23 31 36 0a 09  |1, a1, LSR #16..|
00003640  52 53 42 4e 45 09 61 31  2c 20 61 31 2c 20 23 26  |RSBNE.a1, a1, #&|
00003650  31 30 30 30 30 0a 09 4d  4f 56 09 61 31 2c 20 61  |10000..MOV.a1, a|
00003660  31 2c 20 4c 53 4c 20 23  31 0a 09 53 55 42 09 61  |1, LSL #1..SUB.a|
00003670  31 2c 20 61 31 2c 20 23  26 31 30 30 30 30 0a 09  |1, a1, #&10000..|
00003680  52 53 42 09 61 32 2c 20  61 31 2c 20 61 31 2c 20  |RSB.a2, a1, a1, |
00003690  4c 53 4c 20 23 33 0a 09  41 44 44 09 61 32 2c 20  |LSL #3..ADD.a2, |
000036a0  61 31 2c 20 61 32 2c 20  4c 53 4c 20 23 38 0a 09  |a1, a2, LSL #8..|
000036b0  4d 4f 56 09 61 32 2c 20  61 32 2c 20 41 53 52 20  |MOV.a2, a2, ASR |
000036c0  23 31 36 0a 09 41 44 44  09 61 32 2c 20 61 32 2c  |#16..ADD.a2, a2,|
000036d0  20 23 26 30 30 32 63 30  30 0a 09 41 44 44 09 61  | #&002c00..ADD.a|
000036e0  32 2c 20 61 32 2c 20 23  26 30 30 30 30 38 36 0a  |2, a2, #&000086.|
000036f0  20 20 20 20 20 20 20 20  4d 4f 56 09 69 70 2c 20  |        MOV.ip, |
00003700  61 31 0a 09 6d 75 6c 31  36 63 09 61 32 2c 20 69  |a1..mul16c.a2, i|
00003710  70 2c 20 61 32 2c 20 61  33 0a 09 53 55 42 09 61  |p, a2, a3..SUB.a|
00003720  32 2c 20 61 32 2c 20 23  26 30 30 65 39 30 30 0a  |2, a2, #&00e900.|
00003730  09 53 55 42 09 61 32 2c  20 61 32 2c 20 23 26 30  |.SUB.a2, a2, #&0|
00003740  30 30 30 62 64 0a 20 20  20 20 20 20 20 20 4d 4f  |000bd.        MO|
00003750  56 09 69 70 2c 20 61 31  0a 09 6d 75 6c 31 36 63  |V.ip, a1..mul16c|
00003760  09 61 32 2c 20 69 70 2c  20 61 32 2c 20 61 33 0a  |.a2, ip, a2, a3.|
00003770  09 53 55 42 09 61 32 2c  20 61 32 2c 20 23 26 30  |.SUB.a2, a2, #&0|
00003780  33 30 30 30 30 0a 09 53  55 42 09 61 32 2c 20 61  |30000..SUB.a2, a|
00003790  32 2c 20 23 26 30 30 37  63 30 30 0a 09 53 55 42  |2, #&007c00..SUB|
000037a0  09 61 32 2c 20 61 32 2c  20 23 26 30 30 30 30 63  |.a2, a2, #&0000c|
000037b0  36 0a 20 20 20 20 20 20  20 20 4d 4f 56 09 69 70  |6.        MOV.ip|
000037c0  2c 20 61 31 0a 09 6d 75  6c 31 36 63 09 61 32 2c  |, a1..mul16c.a2,|
000037d0  20 69 70 2c 20 61 32 2c  20 61 33 0a 09 41 44 44  | ip, a2, a3..ADD|
000037e0  09 61 32 2c 20 61 32 2c  20 23 26 30 38 30 30 30  |.a2, a2, #&08000|
000037f0  30 0a 09 41 44 44 09 61  32 2c 20 61 32 2c 20 23  |0..ADD.a2, a2, #|
00003800  26 30 30 45 32 30 30 0a  09 41 44 44 09 61 32 2c  |&00E200..ADD.a2,|
00003810  20 61 32 2c 20 23 26 30  30 30 30 42 44 0a 20 20  | a2, #&0000BD.  |
00003820  20 20 20 20 20 20 4d 4f  56 09 69 70 2c 20 61 31  |      MOV.ip, a1|
00003830  0a 09 6d 75 6c 31 36 63  09 61 32 2c 20 69 70 2c  |..mul16c.a2, ip,|
00003840  20 61 32 2c 20 61 33 0a  09 41 44 44 09 61 32 2c  | a2, a3..ADD.a2,|
00003850  20 61 32 2c 20 23 26 30  62 30 30 30 30 0a 09 41  | a2, #&0b0000..A|
00003860  44 44 09 61 32 2c 20 61  32 2c 20 23 26 30 30 35  |DD.a2, a2, #&005|
00003870  30 30 30 0a 09 41 44 44  09 61 32 2c 20 61 32 2c  |000..ADD.a2, a2,|
00003880  20 23 26 30 30 30 30 35  30 0a 09 4d 4f 56 09 61  | #&000050..MOV.a|
00003890  31 2c 20 61 32 2c 20 41  53 52 20 23 34 0a 09 54  |1, a2, ASR #4..T|
000038a0  53 54 09 61 34 2c 20 23  31 0a 09 52 53 42 4e 45  |ST.a4, #1..RSBNE|
000038b0  09 61 31 2c 20 61 31 2c  20 23 30 0a 20 20 20 20  |.a1, a1, #0.    |
000038c0  20 20 20 20 4d 4f 56 53  20 20 20 20 70 63 2c 20  |    MOVS    pc, |
000038d0  6c 72 0a 0a 0a 0a 3b 20  61 63 73 31 36 0a 3b 20  |lr....; acs16.; |
000038e0  61 20 6c 65 61 66 20 41  50 43 53 20 66 75 6e 63  |a leaf APCS func|
000038f0  74 69 6f 6e 0a 3b 0a 3b  20 43 20 70 72 6f 74 6f  |tion.;.; C proto|
00003900  74 79 70 65 3a 0a 3b 20  69 6e 74 20 61 63 73 31  |type:.; int acs1|
00003910  36 28 69 6e 74 20 61 29  0a 3b 0a 3b 20 72 65 74  |6(int a).;.; ret|
00003920  75 72 6e 73 20 36 35 35  33 36 20 2a 20 32 2f 70  |urns 65536 * 2/p|
00003930  69 20 2a 20 61 72 63 63  6f 73 20 28 61 2f 36 35  |i * arccos (a/65|
00003940  35 33 36 29 0a 3b 0a 3b  20 66 6f 72 20 61 20 6f  |536).;.; for a o|
00003950  75 74 20 6f 66 20 72 61  6e 67 65 20 28 69 65 20  |ut of range (ie |
00003960  61 62 73 20 61 20 3e 20  36 35 35 33 36 29 2c 20  |abs a > 65536), |
00003970  72 65 73 65 74 20 61 20  74 6f 20 b1 36 35 35 33  |reset a to .6553|
00003980  36 20 61 73 20 61 70 70  72 6f 70 72 69 61 74 65  |6 as appropriate|
00003990  20 62 65 66 6f 72 65 20  65 76 61 6c 0a 3b 0a 3b  | before eval.;.;|
000039a0  20 69 73 20 61 62 6f 75  74 20 32 34 20 74 69 6d  | is about 24 tim|
000039b0  65 73 20 66 61 73 74 65  72 20 74 68 61 6e 20 46  |es faster than F|
000039c0  50 45 6d 75 6c 61 74 6f  72 20 77 6f 72 6b 69 6e  |PEmulator workin|
000039d0  67 20 77 69 74 68 20 64  6f 75 62 6c 65 20 66 6c  |g with double fl|
000039e0  6f 61 74 73 0a 3b 0a 0a  20 20 20 20 20 20 20 20  |oats.;..        |
000039f0  45 58 50 4f 52 54 20 20  61 63 73 31 36 0a 0a 61  |EXPORT  acs16..a|
00003a00  63 6e 73 74 61 20 20 44  43 42 20 20 20 20 20 22  |cnsta  DCB     "|
00003a10  61 63 73 31 36 22 2c 20  30 0a 20 20 20 20 20 20  |acs16", 0.      |
00003a20  20 20 41 4c 49 47 4e 0a  61 63 6e 65 6e 64 20 20  |  ALIGN.acnend  |
00003a30  44 43 44 20 20 20 20 20  26 66 66 30 30 30 30 30  |DCD     &ff00000|
00003a40  30 20 2b 20 61 63 6e 65  6e 64 20 2d 20 61 63 6e  |0 + acnend - acn|
00003a50  73 74 61 0a 0a 61 63 73  31 36 0a 0a 09 43 4d 50  |sta..acs16...CMP|
00003a60  09 61 31 2c 20 23 36 35  35 33 36 0a 09 4d 4f 56  |.a1, #65536..MOV|
00003a70  47 45 09 61 31 2c 20 23  30 0a 09 4d 4f 56 47 45  |GE.a1, #0..MOVGE|
00003a80  53 09 70 63 2c 20 6c 72  0a 09 43 4d 50 09 61 31  |S.pc, lr..CMP.a1|
00003a90  2c 20 23 2d 36 35 35 33  36 0a 09 4d 4f 56 4c 45  |, #-65536..MOVLE|
00003aa0  09 61 31 2c 20 23 32 2a  36 35 35 33 36 0a 09 4d  |.a1, #2*65536..M|
00003ab0  4f 56 4c 45 53 09 70 63  2c 20 6c 72 0a 09 53 54  |OVLES.pc, lr..ST|
00003ac0  4d 46 44 09 73 70 21 2c  20 7b 6c 72 7d 09 09 3b  |MFD.sp!, {lr}..;|
00003ad0  6e 6f 77 20 68 61 76 65  20 61 72 67 20 69 6e 20  |now have arg in |
00003ae0  72 61 6e 67 65 20 28 2d  6f 6e 65 2c 6f 6e 65 29  |range (-one,one)|
00003af0  0a 09 43 4d 50 09 61 31  2c 20 23 30 0a 09 52 53  |..CMP.a1, #0..RS|
00003b00  42 4d 49 09 61 31 2c 20  61 31 2c 20 23 30 0a 09  |BMI.a1, a1, #0..|
00003b10  4d 4f 56 4d 49 09 61 34  2c 20 23 31 09 09 09 3b  |MOVMI.a4, #1...;|
00003b20  62 30 20 69 6e 20 61 34  20 73 65 74 20 69 66 66  |b0 in a4 set iff|
00003b30  20 66 69 6e 61 6c 20 72  65 73 75 6c 74 20 72 20  | final result r |
00003b40  6e 65 65 64 73 20 72 65  70 6c 61 63 69 6e 67 20  |needs replacing |
00003b50  62 79 20 32 2a 6f 6e 65  2d 72 0a 09 4d 4f 56 50  |by 2*one-r..MOVP|
00003b60  4c 09 61 34 2c 20 23 30  09 09 09 3b 6e 6f 77 20  |L.a4, #0...;now |
00003b70  68 61 76 65 20 61 72 67  20 69 6e 20 72 61 6e 67  |have arg in rang|
00003b80  65 20 5b 30 2c 6f 6e 65  29 0a 09 53 55 42 09 61  |e [0,one)..SUB.a|
00003b90  32 2c 20 61 31 2c 20 23  26 62 35 30 30 0a 09 53  |2, a1, #&b500..S|
00003ba0  55 42 53 09 61 32 2c 20  61 32 2c 20 23 26 30 30  |UBS.a2, a2, #&00|
00003bb0  30 35 0a 09 42 4c 54 09  61 63 73 31 36 5f 61 72  |05..BLT.acs16_ar|
00003bc0  67 6c 6f 77 09 09 3b 69  66 20 61 72 67 20 3c 20  |glow..;if arg < |
00003bd0  6f 6e 65 2f 73 71 72 74  32 20 67 6f 20 61 6e 64  |one/sqrt2 go and|
00003be0  20 61 70 70 6c 79 20 70  6f 6c 79 20 65 6c 73 65  | apply poly else|
00003bf0  20 66 69 72 73 74 20 74  72 61 6e 73 6d 75 74 65  | first transmute|
00003c00  20 69 6e 74 6f 20 68 65  72 65 0a 09 4f 52 52 09  | into here..ORR.|
00003c10  61 34 2c 20 61 34 2c 20  23 32 09 09 3b 62 31 20  |a4, a4, #2..;b1 |
00003c20  69 6e 20 61 34 20 63 6c  65 61 72 20 69 66 66 20  |in a4 clear iff |
00003c30  70 65 6e 75 6c 74 69 6d  61 74 65 20 72 65 73 75  |penultimate resu|
00003c40  6c 74 20 72 20 6e 65 65  64 20 62 65 20 72 65 70  |lt r need be rep|
00003c50  6c 61 63 65 64 20 62 79  20 6f 6e 65 2d 72 0a 09  |laced by one-r..|
00003c60  4d 55 4c 09 61 32 2c 20  61 31 2c 20 61 31 0a 09  |MUL.a2, a1, a1..|
00003c70  4d 4f 56 09 61 31 2c 20  61 32 2c 20 4c 53 52 20  |MOV.a1, a2, LSR |
00003c80  23 31 36 0a 09 52 53 42  09 61 31 2c 20 61 31 2c  |#16..RSB.a1, a1,|
00003c90  20 23 36 35 35 33 36 0a  09 73 71 72 74 31 36 09  | #65536..sqrt16.|
00003ca0  61 31 2c 20 61 31 2c 20  61 32 2c 20 61 33 2c 20  |a1, a1, a2, a3, |
00003cb0  69 70 2c 20 6c 72 09 3b  61 72 67 20 74 72 61 6e  |ip, lr.;arg tran|
00003cc0  73 6d 75 74 65 64 20 28  69 65 20 72 65 70 6c 61  |smuted (ie repla|
00003cd0  63 65 64 20 62 79 20 6f  6e 65 2d 73 71 72 74 28  |ced by one-sqrt(|
00003ce0  61 72 67 5e 32 29 29 0a  61 63 73 31 36 5f 61 72  |arg^2)).acs16_ar|
00003cf0  67 6c 6f 77 0a 09 41 44  44 09 61 33 2c 20 61 31  |glow..ADD.a3, a1|
00003d00  2c 20 61 31 2c 20 41 53  4c 20 23 32 0a 09 41 44  |, a1, ASL #2..AD|
00003d10  44 09 61 32 2c 20 61 33  2c 20 61 31 2c 20 41 53  |D.a2, a3, a1, AS|
00003d20  4c 20 23 34 0a 09 41 44  44 09 61 32 2c 20 61 32  |L #4..ADD.a2, a2|
00003d30  2c 20 61 33 2c 20 41 53  4c 20 23 35 0a 09 41 44  |, a3, ASL #5..AD|
00003d40  44 09 61 32 2c 20 61 32  2c 20 61 33 2c 20 41 53  |D.a2, a2, a3, AS|
00003d50  52 09 23 38 0a 09 41 44  44 09 61 32 2c 20 61 32  |R.#8..ADD.a2, a2|
00003d60  2c 20 23 33 32 0a 09 4d  4f 56 09 61 31 2c 20 61  |, #32..MOV.a1, a|
00003d70  32 2c 20 41 53 52 20 23  36 09 09 3b 72 61 6e 67  |2, ASR #6..;rang|
00003d80  65 20 74 72 61 6e 73 66  6f 72 6d 20 61 70 70 6c  |e transform appl|
00003d90  69 65 64 20 28 69 65 20  72 65 70 6c 61 63 65 64  |ied (ie replaced|
00003da0  20 62 79 20 61 72 67 2a  32 73 71 72 74 32 2d 6f  | by arg*2sqrt2-o|
00003db0  6e 65 29 0a 09 53 55 42  09 61 31 2c 20 61 31 2c  |ne)..SUB.a1, a1,|
00003dc0  20 23 26 31 30 30 30 30  09 09 3b 73 6f 20 61 72  | #&10000..;so ar|
00003dd0  67 20 6c 69 65 73 20 69  6e 20 72 61 6e 67 65 20  |g lies in range |
00003de0  5b 2d 31 2c 31 5d 0a 09  41 44 44 09 61 32 2c 20  |[-1,1]..ADD.a2, |
00003df0  61 31 2c 20 61 31 2c 20  4c 53 4c 20 23 32 09 3b  |a1, a1, LSL #2.;|
00003e00  6e 6f 77 20 61 70 70 6c  79 20 70 6f 6c 79 0a 09  |now apply poly..|
00003e10  41 44 44 09 61 32 2c 20  61 32 2c 20 61 32 2c 20  |ADD.a2, a2, a2, |
00003e20  4c 53 4c 20 23 35 0a 09  4d 4f 56 09 61 32 2c 20  |LSL #5..MOV.a2, |
00003e30  61 32 2c 20 41 53 52 20  23 31 34 0a 09 41 44 44  |a2, ASR #14..ADD|
00003e40  09 61 32 2c 20 61 32 2c  20 23 26 30 30 30 35 30  |.a2, a2, #&00050|
00003e50  30 0a 09 41 44 44 09 61  32 2c 20 61 32 2c 20 23  |0..ADD.a2, a2, #|
00003e60  26 30 30 30 30 61 65 0a  20 20 20 20 20 20 20 20  |&0000ae.        |
00003e70  4d 4f 56 09 69 70 2c 20  61 31 0a 09 6d 75 6c 31  |MOV.ip, a1..mul1|
00003e80  36 63 09 61 32 2c 20 69  70 2c 20 61 32 2c 20 61  |6c.a2, ip, a2, a|
00003e90  33 0a 09 41 44 44 09 61  32 2c 20 61 32 2c 20 23  |3..ADD.a2, a2, #|
00003ea0  26 30 30 30 38 30 30 0a  09 41 44 44 09 61 32 2c  |&000800..ADD.a2,|
00003eb0  20 61 32 2c 20 23 26 30  30 30 30 38 38 0a 20 20  | a2, #&000088.  |
00003ec0  20 20 20 20 20 20 4d 4f  56 09 69 70 2c 20 61 31  |      MOV.ip, a1|
00003ed0  0a 09 6d 75 6c 31 36 63  09 61 32 2c 20 69 70 2c  |..mul16c.a2, ip,|
00003ee0  20 61 32 2c 20 61 33 0a  09 41 44 44 09 61 32 2c  | a2, a3..ADD.a2,|
00003ef0  20 61 32 2c 20 23 26 30  30 32 30 30 30 0a 09 41  | a2, #&002000..A|
00003f00  44 44 09 61 32 2c 20 61  32 2c 20 23 26 30 30 30  |DD.a2, a2, #&000|
00003f10  30 39 61 0a 20 20 20 20  20 20 20 20 4d 4f 56 09  |09a.        MOV.|
00003f20  69 70 2c 20 61 31 0a 09  6d 75 6c 31 36 63 09 61  |ip, a1..mul16c.a|
00003f30  32 2c 20 69 70 2c 20 61  32 2c 20 61 33 0a 09 41  |2, ip, a2, a3..A|
00003f40  44 44 09 61 32 2c 20 61  32 2c 20 23 26 30 30 34  |DD.a2, a2, #&004|
00003f50  36 30 30 0a 09 41 44 44  09 61 32 2c 20 61 32 2c  |600..ADD.a2, a2,|
00003f60  20 23 26 30 30 30 30 39  61 0a 20 20 20 20 20 20  | #&00009a.      |
00003f70  20 20 4d 4f 56 09 69 70  2c 20 61 31 0a 09 6d 75  |  MOV.ip, a1..mu|
00003f80  6c 31 36 63 09 61 32 2c  20 69 70 2c 20 61 32 2c  |l16c.a2, ip, a2,|
00003f90  20 61 33 0a 09 41 44 44  09 61 32 2c 20 61 32 2c  | a3..ADD.a2, a2,|
00003fa0  20 23 26 30 33 30 30 30  30 0a 09 41 44 44 09 61  | #&030000..ADD.a|
00003fb0  32 2c 20 61 32 2c 20 23  26 30 30 64 39 30 30 0a  |2, a2, #&00d900.|
00003fc0  09 41 44 44 09 61 32 2c  20 61 32 2c 20 23 26 30  |.ADD.a2, a2, #&0|
00003fd0  30 30 30 62 34 0a 20 20  20 20 20 20 20 20 4d 4f  |000b4.        MO|
00003fe0  56 09 69 70 2c 20 61 31  0a 09 6d 75 6c 31 36 63  |V.ip, a1..mul16c|
00003ff0  09 61 32 2c 20 69 70 2c  20 61 32 2c 20 61 33 0a  |.a2, ip, a2, a3.|
00004000  09 41 44 44 09 61 32 2c  20 61 32 2c 20 23 26 30  |.ADD.a2, a2, #&0|
00004010  33 30 30 30 30 0a 09 41  44 44 09 61 32 2c 20 61  |30000..ADD.a2, a|
00004020  32 2c 20 23 26 30 30 61  65 30 30 0a 09 41 44 44  |2, #&00ae00..ADD|
00004030  09 61 32 2c 20 61 32 2c  20 23 26 30 30 30 30 35  |.a2, a2, #&00005|
00004040  32 0a 09 4d 4f 56 09 61  31 2c 20 61 32 2c 20 41  |2..MOV.a1, a2, A|
00004050  53 52 20 23 34 0a 09 54  53 54 09 61 34 2c 20 23  |SR #4..TST.a4, #|
00004060  32 0a 09 52 53 42 45 51  09 61 31 2c 20 61 31 2c  |2..RSBEQ.a1, a1,|
00004070  20 23 36 35 35 33 36 0a  09 54 53 54 09 61 34 2c  | #65536..TST.a4,|
00004080  20 23 31 0a 09 52 53 42  4e 45 09 61 31 2c 20 61  | #1..RSBNE.a1, a|
00004090  31 2c 20 23 32 2a 36 35  35 33 36 0a 09 4c 44 4d  |1, #2*65536..LDM|
000040a0  46 44 09 73 70 21 2c 20  7b 70 63 7d 5e 0a 0a 0a  |FD.sp!, {pc}^...|
000040b0  0a 3b 20 61 73 6e 31 36  0a 3b 20 61 20 6c 65 61  |.; asn16.; a lea|
000040c0  66 20 41 50 43 53 20 66  75 6e 63 74 69 6f 6e 0a  |f APCS function.|
000040d0  3b 0a 3b 20 43 20 70 72  6f 74 6f 74 79 70 65 3a  |;.; C prototype:|
000040e0  0a 3b 20 69 6e 74 20 61  73 6e 31 36 28 69 6e 74  |.; int asn16(int|
000040f0  20 61 29 0a 3b 0a 3b 20  72 65 74 75 72 6e 73 20  | a).;.; returns |
00004100  36 35 35 33 36 20 2a 20  32 2f 70 69 20 2a 20 61  |65536 * 2/pi * a|
00004110  72 63 73 69 6e 20 28 61  2f 36 35 35 33 36 29 0a  |rcsin (a/65536).|
00004120  3b 0a 3b 20 66 6f 72 20  61 20 6f 75 74 20 6f 66  |;.; for a out of|
00004130  20 72 61 6e 67 65 20 28  69 65 20 61 62 73 20 61  | range (ie abs a|
00004140  20 3e 20 36 35 35 33 36  29 2c 20 72 65 73 65 74  | > 65536), reset|
00004150  20 61 20 74 6f 20 b1 36  35 35 33 36 20 61 73 20  | a to .65536 as |
00004160  61 70 70 72 6f 70 72 69  61 74 65 20 62 65 66 6f  |appropriate befo|
00004170  72 65 20 65 76 61 6c 0a  3b 0a 3b 20 69 73 20 61  |re eval.;.; is a|
00004180  62 6f 75 74 20 32 34 20  74 69 6d 65 73 20 66 61  |bout 24 times fa|
00004190  73 74 65 72 20 74 68 61  6e 20 46 50 45 6d 75 6c  |ster than FPEmul|
000041a0  61 74 6f 72 20 77 6f 72  6b 69 6e 67 20 77 69 74  |ator working wit|
000041b0  68 20 64 6f 75 62 6c 65  20 66 6c 6f 61 74 73 0a  |h double floats.|
000041c0  3b 0a 0a 20 20 20 20 20  20 20 20 45 58 50 4f 52  |;..        EXPOR|
000041d0  54 20 20 61 73 6e 31 36  0a 0a 61 73 6e 73 74 61  |T  asn16..asnsta|
000041e0  20 20 44 43 42 20 20 20  20 20 22 61 73 6e 31 36  |  DCB     "asn16|
000041f0  22 2c 20 30 0a 20 20 20  20 20 20 20 20 41 4c 49  |", 0.        ALI|
00004200  47 4e 0a 61 73 6e 65 6e  64 20 20 44 43 44 20 20  |GN.asnend  DCD  |
00004210  20 20 20 26 66 66 30 30  30 30 30 30 20 2b 20 61  |   &ff000000 + a|
00004220  73 6e 65 6e 64 20 2d 20  61 73 6e 73 74 61 0a 0a  |snend - asnsta..|
00004230  61 73 6e 31 36 0a 0a 09  43 4d 50 09 61 31 2c 20  |asn16...CMP.a1, |
00004240  23 36 35 35 33 36 0a 09  4d 4f 56 47 45 09 61 31  |#65536..MOVGE.a1|
00004250  2c 20 23 36 35 35 33 36  0a 09 4d 4f 56 47 45 53  |, #65536..MOVGES|
00004260  09 70 63 2c 20 6c 72 0a  09 41 44 44 53 09 61 32  |.pc, lr..ADDS.a2|
00004270  2c 20 61 31 2c 20 23 36  35 35 33 36 09 09 3b 65  |, a1, #65536..;e|
00004280  71 75 69 76 20 74 6f 20  43 4d 50 20 61 31 2c 23  |quiv to CMP a1,#|
00004290  2d 36 35 35 33 36 0a 09  53 55 42 4c 45 09 61 31  |-65536..SUBLE.a1|
000042a0  2c 20 61 31 2c 20 61 32  09 09 3b 65 71 75 69 76  |, a1, a2..;equiv|
000042b0  20 74 6f 20 4d 4f 56 4c  45 20 61 31 2c 23 2d 36  | to MOVLE a1,#-6|
000042c0  35 35 33 36 20 28 77 68  69 63 68 20 77 6f 75 6c  |5536 (which woul|
000042d0  64 6e 27 74 20 77 6f 72  6b 20 64 75 65 20 74 6f  |dn't work due to|
000042e0  20 62 61 64 20 63 6f 6e  73 74 61 6e 74 29 0a 09  | bad constant)..|
000042f0  4d 4f 56 4c 45 53 09 70  63 2c 20 6c 72 0a 09 53  |MOVLES.pc, lr..S|
00004300  54 4d 46 44 09 73 70 21  2c 20 7b 6c 72 7d 09 09  |TMFD.sp!, {lr}..|
00004310  3b 6e 6f 77 20 68 61 76  65 20 61 72 67 20 69 6e  |;now have arg in|
00004320  20 72 61 6e 67 65 20 28  2d 6f 6e 65 2c 6f 6e 65  | range (-one,one|
00004330  29 0a 09 43 4d 50 09 61  31 2c 20 23 30 0a 09 52  |)..CMP.a1, #0..R|
00004340  53 42 4d 49 09 61 31 2c  20 61 31 2c 20 23 30 0a  |SBMI.a1, a1, #0.|
00004350  09 4d 4f 56 4d 49 09 61  34 2c 20 23 31 09 09 09  |.MOVMI.a4, #1...|
00004360  3b 62 30 20 69 6e 20 61  34 20 73 65 74 20 69 66  |;b0 in a4 set if|
00004370  66 20 66 69 6e 61 6c 20  72 65 73 75 6c 74 20 6e  |f final result n|
00004380  65 65 64 73 20 6e 65 67  61 74 69 6e 67 0a 09 4d  |eeds negating..M|
00004390  4f 56 50 4c 09 61 34 2c  20 23 30 09 09 09 3b 6e  |OVPL.a4, #0...;n|
000043a0  6f 77 20 68 61 76 65 20  61 72 67 20 69 6e 20 72  |ow have arg in r|
000043b0  61 6e 67 65 20 5b 30 2c  6f 6e 65 29 0a 09 53 55  |ange [0,one)..SU|
000043c0  42 09 61 32 2c 20 61 31  2c 20 23 26 62 35 30 30  |B.a2, a1, #&b500|
000043d0  0a 09 53 55 42 53 09 61  32 2c 20 61 32 2c 20 23  |..SUBS.a2, a2, #|
000043e0  26 30 30 30 35 0a 09 42  4c 54 09 61 73 6e 31 36  |&0005..BLT.asn16|
000043f0  5f 61 72 67 6c 6f 77 09  09 3b 69 66 20 61 72 67  |_arglow..;if arg|
00004400  20 3c 20 6f 6e 65 2f 73  71 72 74 32 20 67 6f 20  | < one/sqrt2 go |
00004410  61 6e 64 20 61 70 70 6c  79 20 70 6f 6c 79 20 65  |and apply poly e|
00004420  6c 73 65 20 66 69 72 73  74 20 74 72 61 6e 73 6d  |lse first transm|
00004430  75 74 65 20 69 6e 74 6f  20 68 65 72 65 0a 09 4f  |ute into here..O|
00004440  52 52 09 61 34 2c 20 61  34 2c 20 23 32 09 09 3b  |RR.a4, a4, #2..;|
00004450  62 31 20 69 6e 20 61 34  20 73 65 74 20 69 66 66  |b1 in a4 set iff|
00004460  20 70 65 6e 75 6c 74 69  6d 61 74 65 20 72 65 73  | penultimate res|
00004470  75 6c 74 20 72 20 6e 65  65 64 73 20 74 6f 20 62  |ult r needs to b|
00004480  65 20 72 65 70 6c 61 63  65 64 20 62 79 20 6f 6e  |e replaced by on|
00004490  65 2d 72 0a 09 4d 55 4c  09 61 32 2c 20 61 31 2c  |e-r..MUL.a2, a1,|
000044a0  20 61 31 0a 09 4d 4f 56  09 61 31 2c 20 61 32 2c  | a1..MOV.a1, a2,|
000044b0  20 4c 53 52 20 23 31 36  0a 09 52 53 42 09 61 31  | LSR #16..RSB.a1|
000044c0  2c 20 61 31 2c 20 23 36  35 35 33 36 0a 09 73 71  |, a1, #65536..sq|
000044d0  72 74 31 36 09 61 31 2c  20 61 31 2c 20 61 32 2c  |rt16.a1, a1, a2,|
000044e0  20 61 33 2c 20 69 70 2c  20 6c 72 09 3b 61 72 67  | a3, ip, lr.;arg|
000044f0  20 74 72 61 6e 73 6d 75  74 65 64 20 28 69 65 20  | transmuted (ie |
00004500  72 65 70 6c 61 63 65 64  20 62 79 20 6f 6e 65 2d  |replaced by one-|
00004510  73 71 72 74 28 61 72 67  5e 32 29 29 0a 61 73 6e  |sqrt(arg^2)).asn|
00004520  31 36 5f 61 72 67 6c 6f  77 0a 09 41 44 44 09 61  |16_arglow..ADD.a|
00004530  33 2c 20 61 31 2c 20 61  31 2c 20 41 53 4c 20 23  |3, a1, a1, ASL #|
00004540  32 0a 09 41 44 44 09 61  32 2c 20 61 33 2c 20 61  |2..ADD.a2, a3, a|
00004550  31 2c 20 41 53 4c 20 23  34 0a 09 41 44 44 09 61  |1, ASL #4..ADD.a|
00004560  32 2c 20 61 32 2c 20 61  33 2c 20 41 53 4c 20 23  |2, a2, a3, ASL #|
00004570  35 0a 09 41 44 44 09 61  32 2c 20 61 32 2c 20 61  |5..ADD.a2, a2, a|
00004580  33 2c 20 41 53 52 09 23  38 0a 09 41 44 44 09 61  |3, ASR.#8..ADD.a|
00004590  32 2c 20 61 32 2c 20 23  33 32 0a 09 4d 4f 56 09  |2, a2, #32..MOV.|
000045a0  61 31 2c 20 61 32 2c 20  41 53 52 20 23 36 09 09  |a1, a2, ASR #6..|
000045b0  3b 72 61 6e 67 65 20 74  72 61 6e 73 66 6f 72 6d  |;range transform|
000045c0  20 61 70 70 6c 69 65 64  20 28 69 65 20 72 65 70  | applied (ie rep|
000045d0  6c 61 63 65 64 20 62 79  20 61 72 67 2a 32 73 71  |laced by arg*2sq|
000045e0  72 74 32 2d 6f 6e 65 29  0a 09 53 55 42 09 61 31  |rt2-one)..SUB.a1|
000045f0  2c 20 61 31 2c 20 23 26  31 30 30 30 30 09 09 3b  |, a1, #&10000..;|
00004600  73 6f 20 61 72 67 20 6c  69 65 73 20 69 6e 20 72  |so arg lies in r|
00004610  61 6e 67 65 20 5b 2d 31  2c 31 5d 0a 09 41 44 44  |ange [-1,1]..ADD|
00004620  09 61 32 2c 20 61 31 2c  20 61 31 2c 20 4c 53 4c  |.a2, a1, a1, LSL|
00004630  20 23 32 09 3b 6e 6f 77  20 61 70 70 6c 79 20 70  | #2.;now apply p|
00004640  6f 6c 79 0a 09 41 44 44  09 61 32 2c 20 61 32 2c  |oly..ADD.a2, a2,|
00004650  20 61 32 2c 20 4c 53 4c  20 23 35 0a 09 4d 4f 56  | a2, LSL #5..MOV|
00004660  09 61 32 2c 20 61 32 2c  20 41 53 52 20 23 31 34  |.a2, a2, ASR #14|
00004670  0a 09 41 44 44 09 61 32  2c 20 61 32 2c 20 23 26  |..ADD.a2, a2, #&|
00004680  30 30 30 35 30 30 0a 09  41 44 44 09 61 32 2c 20  |000500..ADD.a2, |
00004690  61 32 2c 20 23 26 30 30  30 30 61 65 0a 20 20 20  |a2, #&0000ae.   |
000046a0  20 20 20 20 20 4d 4f 56  09 69 70 2c 20 61 31 0a  |     MOV.ip, a1.|
000046b0  09 6d 75 6c 31 36 63 09  61 32 2c 20 69 70 2c 20  |.mul16c.a2, ip, |
000046c0  61 32 2c 20 61 33 0a 09  41 44 44 09 61 32 2c 20  |a2, a3..ADD.a2, |
000046d0  61 32 2c 20 23 26 30 30  30 38 30 30 0a 09 41 44  |a2, #&000800..AD|
000046e0  44 09 61 32 2c 20 61 32  2c 20 23 26 30 30 30 30  |D.a2, a2, #&0000|
000046f0  38 38 0a 20 20 20 20 20  20 20 20 4d 4f 56 09 69  |88.        MOV.i|
00004700  70 2c 20 61 31 0a 09 6d  75 6c 31 36 63 09 61 32  |p, a1..mul16c.a2|
00004710  2c 20 69 70 2c 20 61 32  2c 20 61 33 0a 09 41 44  |, ip, a2, a3..AD|
00004720  44 09 61 32 2c 20 61 32  2c 20 23 26 30 30 32 30  |D.a2, a2, #&0020|
00004730  30 30 0a 09 41 44 44 09  61 32 2c 20 61 32 2c 20  |00..ADD.a2, a2, |
00004740  23 26 30 30 30 30 39 61  0a 20 20 20 20 20 20 20  |#&00009a.       |
00004750  20 4d 4f 56 09 69 70 2c  20 61 31 0a 09 6d 75 6c  | MOV.ip, a1..mul|
00004760  31 36 63 09 61 32 2c 20  69 70 2c 20 61 32 2c 20  |16c.a2, ip, a2, |
00004770  61 33 0a 09 41 44 44 09  61 32 2c 20 61 32 2c 20  |a3..ADD.a2, a2, |
00004780  23 26 30 30 34 36 30 30  0a 09 41 44 44 09 61 32  |#&004600..ADD.a2|
00004790  2c 20 61 32 2c 20 23 26  30 30 30 30 39 61 0a 20  |, a2, #&00009a. |
000047a0  20 20 20 20 20 20 20 4d  4f 56 09 69 70 2c 20 61  |       MOV.ip, a|
000047b0  31 0a 09 6d 75 6c 31 36  63 09 61 32 2c 20 69 70  |1..mul16c.a2, ip|
000047c0  2c 20 61 32 2c 20 61 33  0a 09 41 44 44 09 61 32  |, a2, a3..ADD.a2|
000047d0  2c 20 61 32 2c 20 23 26  30 33 30 30 30 30 0a 09  |, a2, #&030000..|
000047e0  41 44 44 09 61 32 2c 20  61 32 2c 20 23 26 30 30  |ADD.a2, a2, #&00|
000047f0  64 39 30 30 0a 09 41 44  44 09 61 32 2c 20 61 32  |d900..ADD.a2, a2|
00004800  2c 20 23 26 30 30 30 30  62 34 0a 20 20 20 20 20  |, #&0000b4.     |
00004810  20 20 20 4d 4f 56 09 69  70 2c 20 61 31 0a 09 6d  |   MOV.ip, a1..m|
00004820  75 6c 31 36 63 09 61 32  2c 20 69 70 2c 20 61 32  |ul16c.a2, ip, a2|
00004830  2c 20 61 33 0a 09 41 44  44 09 61 32 2c 20 61 32  |, a3..ADD.a2, a2|
00004840  2c 20 23 26 30 33 30 30  30 30 0a 09 41 44 44 09  |, #&030000..ADD.|
00004850  61 32 2c 20 61 32 2c 20  23 26 30 30 61 65 30 30  |a2, a2, #&00ae00|
00004860  0a 09 41 44 44 09 61 32  2c 20 61 32 2c 20 23 26  |..ADD.a2, a2, #&|
00004870  30 30 30 30 35 32 0a 09  4d 4f 56 09 61 31 2c 20  |000052..MOV.a1, |
00004880  61 32 2c 20 41 53 52 20  23 34 0a 09 54 53 54 09  |a2, ASR #4..TST.|
00004890  61 34 2c 20 23 32 0a 09  52 53 42 4e 45 09 61 31  |a4, #2..RSBNE.a1|
000048a0  2c 20 61 31 2c 20 23 36  35 35 33 36 0a 09 54 53  |, a1, #65536..TS|
000048b0  54 09 61 34 2c 20 23 31  0a 09 52 53 42 4e 45 09  |T.a4, #1..RSBNE.|
000048c0  61 31 2c 20 61 31 2c 20  23 30 0a 09 4c 44 4d 46  |a1, a1, #0..LDMF|
000048d0  44 09 73 70 21 2c 20 7b  70 63 7d 5e 0a 0a 0a 0a  |D.sp!, {pc}^....|
000048e0  3b 20 67 61 75 73 73 31  36 0a 3b 20 61 20 6c 65  |; gauss16.; a le|
000048f0  61 66 20 41 50 43 53 20  66 75 6e 63 74 69 6f 6e  |af APCS function|
00004900  0a 3b 0a 3b 20 43 20 70  72 6f 74 6f 74 79 70 65  |.;.; C prototype|
00004910  3a 0a 3b 20 69 6e 74 20  67 61 75 73 73 31 36 28  |:.; int gauss16(|
00004920  76 6f 69 64 29 0a 3b 0a  3b 20 72 65 74 75 72 6e  |void).;.; return|
00004930  73 20 36 35 35 33 36 20  2a 20 28 20 70 73 65 75  |s 65536 * ( pseu|
00004940  64 6f 20 72 61 6e 64 6f  6e 20 76 61 72 69 61 62  |do randon variab|
00004950  6c 65 20 77 69 74 68 20  61 70 70 72 6f 78 69 6d  |le with approxim|
00004960  61 74 65 20 64 69 73 74  72 69 62 75 74 69 6f 6e  |ate distribution|
00004970  20 4e 28 30 2c 31 29 20  29 0a 3b 20 6e 6f 74 65  | N(0,1) ).; note|
00004980  2c 20 74 68 65 20 61 70  70 72 6f 78 69 6d 61 74  |, the approximat|
00004990  65 20 67 61 75 73 73 69  61 6e 20 64 69 73 74 72  |e gaussian distr|
000049a0  69 62 75 74 69 6f 6e 20  69 73 20 61 63 68 69 65  |ibution is achie|
000049b0  76 65 64 20 76 69 61 20  74 68 65 20 73 75 6d 20  |ved via the sum |
000049c0  6f 66 20 6e 20 70 73 65  75 64 6f 20 55 5b 30 2c  |of n pseudo U[0,|
000049d0  36 35 35 33 35 5d 0a 3b  20 20 20 20 20 20 20 72  |65535].;       r|
000049e0  61 6e 64 6f 6d 20 76 61  72 69 61 62 6c 65 73 20  |andom variables |
000049f0  26 20 61 70 70 6c 69 63  61 74 69 6f 6e 20 6f 66  |& application of|
00004a00  20 74 68 65 20 63 65 6e  74 72 61 6c 20 6c 69 6d  | the central lim|
00004a10  69 74 20 74 68 65 6f 72  65 6d 0a 3b 20 20 20 20  |it theorem.;    |
00004a20  20 20 20 68 65 72 65 20  77 65 20 75 73 65 20 6e  |   here we use n|
00004a30  3d 38 2c 20 67 69 76 69  6e 67 20 61 6e 20 61 63  |=8, giving an ac|
00004a40  74 75 61 6c 20 72 61 6e  67 65 20 6f 66 20 b1 28  |tual range of .(|
00004a50  73 71 72 74 32 34 29 20  28 2a 36 35 35 33 36 29  |sqrt24) (*65536)|
00004a60  2e 0a 3b 0a 3b 20 20 20  20 20 20 20 66 6f 72 20  |..;.;       for |
00004a70  67 65 6e 65 72 61 6c 20  6e 2c 20 72 65 63 61 6c  |general n, recal|
00004a80  6c 20 77 65 20 6e 65 65  64 20 74 6f 20 72 65 74  |l we need to ret|
00004a90  75 72 6e 3a 0a 3b 20 20  20 20 20 20 20 28 73 75  |urn:.;       (su|
00004aa0  6d 20 6e 20 55 5b 30 2c  36 35 35 33 35 5d 20 72  |m n U[0,65535] r|
00004ab0  61 6e 64 6f 6d 20 76 61  72 69 61 62 6c 65 73 29  |andom variables)|
00004ac0  20 2a 20 32 73 71 72 74  28 33 6e 29 2a 36 35 35  | * 2sqrt(3n)*655|
00004ad0  33 36 2f 28 36 35 35 33  35 6e 29 20 20 2d 20 20  |36/(65535n)  -  |
00004ae0  73 71 72 74 28 33 6e 29  2a 36 35 35 33 36 0a 3b  |sqrt(3n)*65536.;|
00004af0  0a 0a 09 45 58 50 4f 52  54 09 67 61 75 73 73 31  |...EXPORT.gauss1|
00004b00  36 0a 0a 67 61 6e 73 74  61 20 20 44 43 42 20 20  |6..gansta  DCB  |
00004b10  20 20 20 22 67 61 75 73  73 31 36 22 2c 20 30 0a  |   "gauss16", 0.|
00004b20  20 20 20 20 20 20 20 20  41 4c 49 47 4e 0a 67 61  |        ALIGN.ga|
00004b30  6e 65 6e 64 20 20 44 43  44 20 20 20 20 20 26 66  |nend  DCD     &f|
00004b40  66 30 30 30 30 30 30 20  2b 20 67 61 6e 65 6e 64  |f000000 + ganend|
00004b50  20 2d 20 67 61 6e 73 74  61 0a 0a 67 61 75 73 73  | - gansta..gauss|
00004b60  31 36 0a 0a 09 41 44 52  09 61 31 2c 20 67 61 75  |16...ADR.a1, gau|
00004b70  73 73 73 65 65 64 31 0a  09 4c 44 4d 49 41 09 61  |ssseed1..LDMIA.a|
00004b80  31 2c 20 7b 61 32 2c 20  61 33 7d 0a 0a 09 4d 4f  |1, {a2, a3}...MO|
00004b90  56 53 20 20 20 20 61 33  2c 20 61 33 2c 20 4c 53  |VS    a3, a3, LS|
00004ba0  52 20 23 31 0a 20 20 20  20 20 20 20 20 4d 4f 56  |R #1.        MOV|
00004bb0  53 20 20 20 20 61 34 2c  20 61 32 2c 20 52 52 58  |S    a4, a2, RRX|
00004bc0  0a 20 20 20 20 20 20 20  20 41 44 43 20 20 20 20  |.        ADC    |
00004bd0  20 61 33 2c 20 61 33 2c  20 61 33 0a 20 20 20 20  | a3, a3, a3.    |
00004be0  20 20 20 20 45 4f 52 20  20 20 20 20 61 34 2c 20  |    EOR     a4, |
00004bf0  61 34 2c 20 61 32 2c 20  4c 53 4c 20 23 31 32 0a  |a4, a2, LSL #12.|
00004c00  20 20 20 20 20 20 20 20  45 4f 52 20 20 20 20 20  |        EOR     |
00004c10  61 32 2c 20 61 34 2c 20  61 34 2c 20 4c 53 52 20  |a2, a4, a4, LSR |
00004c20  23 32 30 0a 09 4d 4f 56  09 69 70 2c 20 61 32 2c  |#20..MOV.ip, a2,|
00004c30  20 4c 53 52 20 23 31 36  0a 09 4d 4f 56 09 61 34  | LSR #16..MOV.a4|
00004c40  2c 20 61 32 2c 20 4c 53  4c 20 23 31 36 0a 09 41  |, a2, LSL #16..A|
00004c50  44 44 09 69 70 2c 20 69  70 2c 20 61 34 2c 20 4c  |DD.ip, ip, a4, L|
00004c60  53 52 20 23 31 36 0a 0a  09 4d 4f 56 53 20 20 20  |SR #16...MOVS   |
00004c70  20 61 33 2c 20 61 33 2c  20 4c 53 52 20 23 31 0a  | a3, a3, LSR #1.|
00004c80  20 20 20 20 20 20 20 20  4d 4f 56 53 20 20 20 20  |        MOVS    |
00004c90  61 34 2c 20 61 32 2c 20  52 52 58 0a 20 20 20 20  |a4, a2, RRX.    |
00004ca0  20 20 20 20 41 44 43 20  20 20 20 20 61 33 2c 20  |    ADC     a3, |
00004cb0  61 33 2c 20 61 33 0a 20  20 20 20 20 20 20 20 45  |a3, a3.        E|
00004cc0  4f 52 20 20 20 20 20 61  34 2c 20 61 34 2c 20 61  |OR     a4, a4, a|
00004cd0  32 2c 20 4c 53 4c 20 23  31 32 0a 20 20 20 20 20  |2, LSL #12.     |
00004ce0  20 20 20 45 4f 52 20 20  20 20 20 61 32 2c 20 61  |   EOR     a2, a|
00004cf0  34 2c 20 61 34 2c 20 4c  53 52 20 23 32 30 0a 09  |4, a4, LSR #20..|
00004d00  41 44 44 09 69 70 2c 20  69 70 2c 20 61 32 2c 20  |ADD.ip, ip, a2, |
00004d10  4c 53 52 20 23 31 36 0a  09 4d 4f 56 09 61 34 2c  |LSR #16..MOV.a4,|
00004d20  20 61 32 2c 20 4c 53 4c  20 23 31 36 0a 09 41 44  | a2, LSL #16..AD|
00004d30  44 09 69 70 2c 20 69 70  2c 20 61 34 2c 20 4c 53  |D.ip, ip, a4, LS|
00004d40  52 20 23 31 36 0a 0a 09  4d 4f 56 53 20 20 20 20  |R #16...MOVS    |
00004d50  61 33 2c 20 61 33 2c 20  4c 53 52 20 23 31 0a 20  |a3, a3, LSR #1. |
00004d60  20 20 20 20 20 20 20 4d  4f 56 53 20 20 20 20 61  |       MOVS    a|
00004d70  34 2c 20 61 32 2c 20 52  52 58 0a 20 20 20 20 20  |4, a2, RRX.     |
00004d80  20 20 20 41 44 43 20 20  20 20 20 61 33 2c 20 61  |   ADC     a3, a|
00004d90  33 2c 20 61 33 0a 20 20  20 20 20 20 20 20 45 4f  |3, a3.        EO|
00004da0  52 20 20 20 20 20 61 34  2c 20 61 34 2c 20 61 32  |R     a4, a4, a2|
00004db0  2c 20 4c 53 4c 20 23 31  32 0a 20 20 20 20 20 20  |, LSL #12.      |
00004dc0  20 20 45 4f 52 20 20 20  20 20 61 32 2c 20 61 34  |  EOR     a2, a4|
00004dd0  2c 20 61 34 2c 20 4c 53  52 20 23 32 30 0a 09 41  |, a4, LSR #20..A|
00004de0  44 44 09 69 70 2c 20 69  70 2c 20 61 32 2c 20 4c  |DD.ip, ip, a2, L|
00004df0  53 52 20 23 31 36 0a 09  4d 4f 56 09 61 34 2c 20  |SR #16..MOV.a4, |
00004e00  61 32 2c 20 4c 53 4c 20  23 31 36 0a 09 41 44 44  |a2, LSL #16..ADD|
00004e10  09 69 70 2c 20 69 70 2c  20 61 34 2c 20 4c 53 52  |.ip, ip, a4, LSR|
00004e20  20 23 31 36 0a 0a 09 4d  4f 56 53 20 20 20 20 61  | #16...MOVS    a|
00004e30  33 2c 20 61 33 2c 20 4c  53 52 20 23 31 0a 20 20  |3, a3, LSR #1.  |
00004e40  20 20 20 20 20 20 4d 4f  56 53 20 20 20 20 61 34  |      MOVS    a4|
00004e50  2c 20 61 32 2c 20 52 52  58 0a 20 20 20 20 20 20  |, a2, RRX.      |
00004e60  20 20 41 44 43 20 20 20  20 20 61 33 2c 20 61 33  |  ADC     a3, a3|
00004e70  2c 20 61 33 0a 20 20 20  20 20 20 20 20 45 4f 52  |, a3.        EOR|
00004e80  20 20 20 20 20 61 34 2c  20 61 34 2c 20 61 32 2c  |     a4, a4, a2,|
00004e90  20 4c 53 4c 20 23 31 32  0a 20 20 20 20 20 20 20  | LSL #12.       |
00004ea0  20 45 4f 52 20 20 20 20  20 61 32 2c 20 61 34 2c  | EOR     a2, a4,|
00004eb0  20 61 34 2c 20 4c 53 52  20 23 32 30 0a 09 41 44  | a4, LSR #20..AD|
00004ec0  44 09 69 70 2c 20 69 70  2c 20 61 32 2c 20 4c 53  |D.ip, ip, a2, LS|
00004ed0  52 20 23 31 36 0a 09 4d  4f 56 09 61 34 2c 20 61  |R #16..MOV.a4, a|
00004ee0  32 2c 20 4c 53 4c 20 23  31 36 0a 09 41 44 44 09  |2, LSL #16..ADD.|
00004ef0  69 70 2c 20 69 70 2c 20  61 34 2c 20 4c 53 52 20  |ip, ip, a4, LSR |
00004f00  23 31 36 09 3b 73 75 6d  20 6e 6f 77 20 69 6e 20  |#16.;sum now in |
00004f10  69 70 0a 0a 09 53 54 4d  49 41 09 61 31 2c 20 7b  |ip...STMIA.a1, {|
00004f20  61 32 2c 20 61 33 7d 0a  0a 09 52 53 42 09 61 31  |a2, a3}...RSB.a1|
00004f30  2c 20 69 70 2c 20 69 70  2c 20 41 53 4c 20 23 33  |, ip, ip, ASL #3|
00004f40  0a 09 52 53 42 09 61 32  2c 20 69 70 2c 20 69 70  |..RSB.a2, ip, ip|
00004f50  2c 20 41 53 4c 20 23 32  0a 09 41 44 44 09 61 31  |, ASL #2..ADD.a1|
00004f60  2c 20 61 32 2c 20 61 31  2c 20 41 53 4c 20 23 34  |, a2, a1, ASL #4|
00004f70  0a 09 41 44 44 09 61 31  2c 20 61 31 2c 20 69 70  |..ADD.a1, a1, ip|
00004f80  2c 20 41 53 4c 20 23 39  0a 09 41 44 44 09 61 32  |, ASL #9..ADD.a2|
00004f90  2c 20 69 70 2c 20 69 70  2c 20 41 53 4c 20 23 34  |, ip, ip, ASL #4|
00004fa0  0a 09 41 44 44 09 61 32  2c 20 61 32 2c 20 69 70  |..ADD.a2, a2, ip|
00004fb0  2c 20 41 53 4c 20 23 36  0a 09 41 44 44 09 61 31  |, ASL #6..ADD.a1|
00004fc0  2c 20 61 31 2c 20 61 32  2c 20 4c 53 52 20 23 31  |, a1, a2, LSR #1|
00004fd0  30 0a 09 4d 4f 56 09 61  31 2c 20 61 31 2c 20 4c  |0..MOV.a1, a1, L|
00004fe0  53 52 20 23 39 0a 09 53  55 42 09 61 31 2c 20 61  |SR #9..SUB.a1, a|
00004ff0  31 2c 20 23 26 34 45 30  30 30 0a 09 53 55 42 09  |1, #&4E000..SUB.|
00005000  61 31 2c 20 61 31 2c 20  23 26 30 30 36 32 30 0a  |a1, a1, #&00620.|
00005010  09 53 55 42 09 61 31 2c  20 61 31 2c 20 23 26 30  |.SUB.a1, a1, #&0|
00005020  30 30 30 34 0a 0a 20 20  20 20 20 20 20 20 4d 4f  |0004..        MO|
00005030  56 53 20 20 20 20 70 63  2c 20 6c 72 0a 0a 67 61  |VS    pc, lr..ga|
00005040  75 73 73 73 65 65 64 31  09 44 43 44 09 2d 31 09  |ussseed1.DCD.-1.|
00005050  09 3b 62 69 74 73 20 62  30 2d 62 33 31 20 6f 66  |.;bits b0-b31 of|
00005060  20 73 65 65 64 0a 09 09  44 43 44 09 2d 31 09 09  | seed...DCD.-1..|
00005070  3b 62 69 74 20 20 62 33  32 20 6f 66 20 73 65 65  |;bit  b32 of see|
00005080  64 20 28 69 6e 20 6c 73  62 20 6f 66 20 77 6f 72  |d (in lsb of wor|
00005090  64 29 0a 0a 3b 20 73 67  61 75 73 73 31 36 0a 3b  |d)..; sgauss16.;|
000050a0  20 61 20 6c 65 61 66 20  41 50 43 53 20 66 75 6e  | a leaf APCS fun|
000050b0  63 74 69 6f 6e 0a 3b 0a  3b 20 43 20 70 72 6f 74  |ction.;.; C prot|
000050c0  6f 74 79 70 65 3a 0a 3b  20 76 6f 69 64 20 73 67  |otype:.; void sg|
000050d0  61 75 73 73 31 36 28 69  6e 74 20 73 65 65 64 29  |auss16(int seed)|
000050e0  0a 3b 0a 3b 20 73 65 74  73 20 74 68 65 20 73 65  |.;.; sets the se|
000050f0  65 64 20 66 6f 72 20 67  61 75 73 73 31 36 0a 3b  |ed for gauss16.;|
00005100  0a 0a 09 45 58 50 4f 52  54 09 73 67 61 75 73 73  |...EXPORT.sgauss|
00005110  31 36 0a 0a 73 67 6e 73  74 61 20 20 44 43 42 20  |16..sgnsta  DCB |
00005120  20 20 20 20 22 73 67 61  75 73 73 31 36 22 2c 20  |    "sgauss16", |
00005130  30 0a 20 20 20 20 20 20  20 20 41 4c 49 47 4e 0a  |0.        ALIGN.|
00005140  73 67 6e 65 6e 64 20 20  44 43 44 20 20 20 20 20  |sgnend  DCD     |
00005150  26 66 66 30 30 30 30 30  30 20 2b 20 73 67 6e 65  |&ff000000 + sgne|
00005160  6e 64 20 2d 20 73 67 6e  73 74 61 0a 0a 73 67 61  |nd - sgnsta..sga|
00005170  75 73 73 31 36 0a 0a 09  41 44 52 09 61 32 2c 20  |uss16...ADR.a2, |
00005180  67 61 75 73 73 73 65 65  64 31 0a 09 4d 4f 56 09  |gaussseed1..MOV.|
00005190  61 33 2c 20 23 31 0a 09  53 54 4d 49 41 09 61 32  |a3, #1..STMIA.a2|
000051a0  2c 20 7b 61 31 2c 20 61  33 7d 0a 20 20 20 20 20  |, {a1, a3}.     |
000051b0  20 20 20 4d 4f 56 53 20  20 20 20 70 63 2c 20 6c  |   MOVS    pc, l|
000051c0  72 0a 0a 0a 0a 3b 20 72  61 6e 64 31 36 0a 3b 20  |r....; rand16.; |
000051d0  61 20 6c 65 61 66 20 41  50 43 53 20 66 75 6e 63  |a leaf APCS func|
000051e0  74 69 6f 6e 0a 3b 0a 3b  20 43 20 70 72 6f 74 6f  |tion.;.; C proto|
000051f0  74 79 70 65 3a 0a 3b 20  69 6e 74 20 72 61 6e 64  |type:.; int rand|
00005200  31 36 28 76 6f 69 64 29  0a 3b 0a 3b 20 72 65 74  |16(void).;.; ret|
00005210  75 72 6e 73 20 36 35 35  33 36 20 2a 20 28 20 70  |urns 65536 * ( p|
00005220  73 65 75 64 6f 20 72 61  6e 64 6f 6e 20 76 61 72  |seudo randon var|
00005230  69 61 62 6c 65 20 77 69  74 68 20 61 70 70 72 6f  |iable with appro|
00005240  78 69 6d 61 74 65 20 64  69 73 74 72 69 62 75 74  |ximate distribut|
00005250  69 6f 6e 20 55 28 2d 31  2c 31 29 20 29 0a 3b 0a  |ion U(-1,1) ).;.|
00005260  0a 09 45 58 50 4f 52 54  09 72 61 6e 64 31 36 0a  |..EXPORT.rand16.|
00005270  0a 72 61 6e 73 74 61 20  20 44 43 42 20 20 20 20  |.ransta  DCB    |
00005280  20 22 72 61 6e 64 31 36  22 2c 20 30 0a 20 20 20  | "rand16", 0.   |
00005290  20 20 20 20 20 41 4c 49  47 4e 0a 72 61 6e 65 6e  |     ALIGN.ranen|
000052a0  64 20 20 44 43 44 20 20  20 20 20 26 66 66 30 30  |d  DCD     &ff00|
000052b0  30 30 30 30 20 2b 20 72  61 6e 65 6e 64 20 2d 20  |0000 + ranend - |
000052c0  72 61 6e 73 74 61 0a 0a  72 61 6e 64 31 36 0a 0a  |ransta..rand16..|
000052d0  09 41 44 52 09 69 70 2c  20 72 61 6e 64 73 65 65  |.ADR.ip, randsee|
000052e0  64 31 0a 09 4c 44 4d 49  41 09 69 70 2c 20 7b 61  |d1..LDMIA.ip, {a|
000052f0  32 2c 20 61 33 7d 0a 72  61 6e 64 31 36 5f 6c 6f  |2, a3}.rand16_lo|
00005300  6f 70 0a 09 4d 4f 56 53  09 61 33 2c 20 61 33 2c  |op..MOVS.a3, a3,|
00005310  20 4c 53 52 20 23 31 0a  20 20 20 20 20 20 20 20  | LSR #1.        |
00005320  4d 4f 56 53 09 61 34 2c  20 61 32 2c 20 52 52 58  |MOVS.a4, a2, RRX|
00005330  0a 20 20 20 20 20 20 20  20 41 44 43 09 61 33 2c  |.        ADC.a3,|
00005340  20 61 33 2c 20 61 33 0a  20 20 20 20 20 20 20 20  | a3, a3.        |
00005350  45 4f 52 09 61 34 2c 20  61 34 2c 20 61 32 2c 20  |EOR.a4, a4, a2, |
00005360  4c 53 4c 20 23 31 32 0a  20 20 20 20 20 20 20 20  |LSL #12.        |
00005370  45 4f 52 09 61 32 2c 20  61 34 2c 20 61 34 2c 20  |EOR.a2, a4, a4, |
00005380  4c 53 52 20 23 32 30 0a  09 4d 4f 56 09 61 31 2c  |LSR #20..MOV.a1,|
00005390  20 61 32 2c 20 4c 53 4c  20 23 31 35 0a 09 4d 4f  | a2, LSL #15..MO|
000053a0  56 09 61 31 2c 20 61 31  2c 20 41 53 52 20 23 31  |V.a1, a1, ASR #1|
000053b0  35 0a 09 43 4d 50 09 61  31 2c 20 23 26 66 66 66  |5..CMP.a1, #&fff|
000053c0  66 30 30 30 30 0a 09 42  45 51 09 72 61 6e 64 31  |f0000..BEQ.rand1|
000053d0  36 5f 6c 6f 6f 70 0a 09  53 54 4d 49 41 09 69 70  |6_loop..STMIA.ip|
000053e0  2c 20 7b 61 32 2c 20 61  33 7d 0a 20 20 20 20 20  |, {a2, a3}.     |
000053f0  20 20 20 4d 4f 56 53 09  70 63 2c 20 6c 72 0a 0a  |   MOVS.pc, lr..|
00005400  0a 0a 3b 20 72 61 6e 64  75 31 36 0a 3b 20 61 20  |..; randu16.; a |
00005410  6c 65 61 66 20 41 50 43  53 20 66 75 6e 63 74 69  |leaf APCS functi|
00005420  6f 6e 0a 3b 0a 3b 20 43  20 70 72 6f 74 6f 74 79  |on.;.; C prototy|
00005430  70 65 3a 0a 3b 20 69 6e  74 20 72 61 6e 64 75 31  |pe:.; int randu1|
00005440  36 28 76 6f 69 64 29 0a  3b 0a 3b 20 72 65 74 75  |6(void).;.; retu|
00005450  72 6e 73 20 36 35 35 33  36 20 2a 20 28 20 70 73  |rns 65536 * ( ps|
00005460  65 75 64 6f 20 72 61 6e  64 6f 6e 20 76 61 72 69  |eudo randon vari|
00005470  61 62 6c 65 20 77 69 74  68 20 61 70 70 72 6f 78  |able with approx|
00005480  69 6d 61 74 65 20 64 69  73 74 72 69 62 75 74 69  |imate distributi|
00005490  6f 6e 20 55 28 30 2c 31  29 20 29 0a 3b 0a 0a 09  |on U(0,1) ).;...|
000054a0  45 58 50 4f 52 54 09 72  61 6e 64 75 31 36 0a 0a  |EXPORT.randu16..|
000054b0  72 75 6e 73 74 61 20 20  44 43 42 20 20 20 20 20  |runsta  DCB     |
000054c0  22 72 61 6e 64 75 31 36  22 2c 20 30 0a 20 20 20  |"randu16", 0.   |
000054d0  20 20 20 20 20 41 4c 49  47 4e 0a 72 75 6e 65 6e  |     ALIGN.runen|
000054e0  64 20 20 44 43 44 20 20  20 20 20 26 66 66 30 30  |d  DCD     &ff00|
000054f0  30 30 30 30 20 2b 20 72  75 6e 65 6e 64 20 2d 20  |0000 + runend - |
00005500  72 75 6e 73 74 61 0a 0a  72 61 6e 64 75 31 36 0a  |runsta..randu16.|
00005510  0a 09 41 44 52 09 69 70  2c 20 72 61 6e 64 73 65  |..ADR.ip, randse|
00005520  65 64 31 0a 09 4c 44 4d  49 41 09 69 70 2c 20 7b  |ed1..LDMIA.ip, {|
00005530  61 32 2c 20 61 33 7d 0a  72 61 6e 64 75 31 36 5f  |a2, a3}.randu16_|
00005540  6c 6f 6f 70 0a 09 4d 4f  56 53 09 61 33 2c 20 61  |loop..MOVS.a3, a|
00005550  33 2c 20 4c 53 52 20 23  31 0a 20 20 20 20 20 20  |3, LSR #1.      |
00005560  20 20 4d 4f 56 53 09 61  34 2c 20 61 32 2c 20 52  |  MOVS.a4, a2, R|
00005570  52 58 0a 20 20 20 20 20  20 20 20 41 44 43 09 61  |RX.        ADC.a|
00005580  33 2c 20 61 33 2c 20 61  33 0a 20 20 20 20 20 20  |3, a3, a3.      |
00005590  20 20 45 4f 52 09 61 34  2c 20 61 34 2c 20 61 32  |  EOR.a4, a4, a2|
000055a0  2c 20 4c 53 4c 20 23 31  32 0a 20 20 20 20 20 20  |, LSL #12.      |
000055b0  20 20 45 4f 52 09 61 32  2c 20 61 34 2c 20 61 34  |  EOR.a2, a4, a4|
000055c0  2c 20 4c 53 52 20 23 32  30 0a 09 4d 4f 56 53 09  |, LSR #20..MOVS.|
000055d0  61 31 2c 20 61 32 2c 20  4c 53 52 20 23 31 36 0a  |a1, a2, LSR #16.|
000055e0  09 42 45 51 09 72 61 6e  64 75 31 36 5f 6c 6f 6f  |.BEQ.randu16_loo|
000055f0  70 0a 09 53 54 4d 49 41  09 69 70 2c 20 7b 61 32  |p..STMIA.ip, {a2|
00005600  2c 20 61 33 7d 0a 20 20  20 20 20 20 20 20 4d 4f  |, a3}.        MO|
00005610  56 53 09 70 63 2c 20 6c  72 0a 0a 72 61 6e 64 73  |VS.pc, lr..rands|
00005620  65 65 64 31 09 44 43 44  09 2d 31 09 09 3b 62 69  |eed1.DCD.-1..;bi|
00005630  74 73 20 62 30 2d 62 33  31 20 6f 66 20 73 65 65  |ts b0-b31 of see|
00005640  64 0a 09 09 44 43 44 09  2d 31 09 09 3b 62 69 74  |d...DCD.-1..;bit|
00005650  20 20 62 33 32 20 6f 66  20 73 65 65 64 20 28 69  |  b32 of seed (i|
00005660  6e 20 6c 73 62 20 6f 66  20 77 6f 72 64 29 0a 0a  |n lsb of word)..|
00005670  3b 20 73 72 61 6e 64 31  36 0a 3b 20 61 20 6c 65  |; srand16.; a le|
00005680  61 66 20 41 50 43 53 20  66 75 6e 63 74 69 6f 6e  |af APCS function|
00005690  0a 3b 0a 3b 20 43 20 70  72 6f 74 6f 74 79 70 65  |.;.; C prototype|
000056a0  3a 0a 3b 20 76 6f 69 64  20 73 72 61 6e 64 31 36  |:.; void srand16|
000056b0  28 69 6e 74 20 73 65 65  64 29 0a 3b 0a 3b 20 73  |(int seed).;.; s|
000056c0  65 74 73 20 74 68 65 20  73 65 65 64 20 66 6f 72  |ets the seed for|
000056d0  20 72 61 6e 64 31 36 0a  3b 0a 0a 09 45 58 50 4f  | rand16.;...EXPO|
000056e0  52 54 09 73 72 61 6e 64  31 36 0a 0a 73 72 6e 73  |RT.srand16..srns|
000056f0  74 61 20 20 44 43 42 20  20 20 20 20 22 73 72 61  |ta  DCB     "sra|
00005700  6e 64 31 36 22 2c 20 30  0a 20 20 20 20 20 20 20  |nd16", 0.       |
00005710  20 41 4c 49 47 4e 0a 73  72 6e 65 6e 64 20 20 44  | ALIGN.srnend  D|
00005720  43 44 20 20 20 20 20 26  66 66 30 30 30 30 30 30  |CD     &ff000000|
00005730  20 2b 20 73 72 6e 65 6e  64 20 2d 20 73 72 6e 73  | + srnend - srns|
00005740  74 61 0a 0a 73 72 61 6e  64 31 36 0a 0a 09 41 44  |ta..srand16...AD|
00005750  52 09 61 32 2c 20 72 61  6e 64 73 65 65 64 31 0a  |R.a2, randseed1.|
00005760  09 4d 4f 56 09 61 33 2c  20 23 31 0a 09 53 54 4d  |.MOV.a3, #1..STM|
00005770  49 41 09 61 32 2c 20 7b  61 31 2c 20 61 33 7d 0a  |IA.a2, {a1, a3}.|
00005780  20 20 20 20 20 20 20 20  4d 4f 56 53 20 20 20 20  |        MOVS    |
00005790  70 63 2c 20 6c 72 0a 0a  0a 0a 3b 20 64 69 76 5f  |pc, lr....; div_|
000057a0  66 72 61 63 31 36 0a 3b  20 61 20 6c 65 61 66 20  |frac16.; a leaf |
000057b0  41 50 43 53 20 66 75 6e  63 74 69 6f 6e 0a 3b 0a  |APCS function.;.|
000057c0  3b 20 43 20 70 72 6f 74  6f 74 79 70 65 3a 0a 3b  |; C prototype:.;|
000057d0  20 69 6e 74 20 64 69 76  5f 66 72 61 63 31 36 28  | int div_frac16(|
000057e0  69 6e 74 20 6e 75 6d 62  65 72 2c 20 69 6e 74 20  |int number, int |
000057f0  64 69 76 69 73 6f 72 29  0a 3b 0a 3b 20 72 65 74  |divisor).;.; ret|
00005800  75 72 6e 73 20 69 6e 74  65 67 65 72 20 70 61 72  |urns integer par|
00005810  74 20 6f 66 20 36 35 35  33 36 2a 6e 75 6d 62 65  |t of 65536*numbe|
00005820  72 2f 64 69 76 69 73 6f  72 0a 3b 20 61 73 73 75  |r/divisor.; assu|
00005830  6d 65 73 20 61 62 73 20  6e 75 6d 62 65 72 20 3c  |mes abs number <|
00005840  20 36 35 35 33 36 20 2a  20 61 62 73 20 64 69 76  | 65536 * abs div|
00005850  69 73 6f 72 0a 3b 20 69  66 20 74 68 69 73 20 6e  |isor.; if this n|
00005860  65 65 64 73 20 63 68 65  63 6b 69 6e 67 2c 20 6d  |eeds checking, m|
00005870  75 73 74 20 62 65 20 64  6f 6e 65 20 62 79 20 63  |ust be done by c|
00005880  61 6c 6c 65 72 0a 3b 0a  0a 20 20 20 20 20 20 20  |aller.;..       |
00005890  20 45 58 50 4f 52 54 20  20 64 69 76 5f 66 72 61  | EXPORT  div_fra|
000058a0  63 31 36 0a 0a 64 66 6e  73 74 61 20 20 44 43 42  |c16..dfnsta  DCB|
000058b0  20 20 20 20 20 22 64 69  76 5f 66 72 61 63 31 36  |     "div_frac16|
000058c0  22 2c 20 30 0a 20 20 20  20 20 20 20 20 41 4c 49  |", 0.        ALI|
000058d0  47 4e 0a 64 66 6e 65 6e  64 20 20 44 43 44 20 20  |GN.dfnend  DCD  |
000058e0  20 20 20 26 66 66 30 30  30 30 30 30 20 2b 20 64  |   &ff000000 + d|
000058f0  66 6e 65 6e 64 20 2d 20  64 66 6e 73 74 61 0a 0a  |fnend - dfnsta..|
00005900  64 69 76 5f 66 72 61 63  31 36 0a 0a 20 20 20 20  |div_frac16..    |
00005910  20 20 20 20 64 69 76 31  36 20 20 20 61 31 2c 20  |    div16   a1, |
00005920  61 32 2c 20 61 31 2c 20  61 33 2c 20 61 34 0a 20  |a2, a1, a3, a4. |
00005930  20 20 20 20 20 20 20 4d  4f 56 53 20 20 20 20 70  |       MOVS    p|
00005940  63 2c 20 6c 72 0a 0a 0a  0a 3b 20 6d 75 6c 5f 66  |c, lr....; mul_f|
00005950  72 61 63 31 36 0a 3b 20  61 20 6c 65 61 66 20 41  |rac16.; a leaf A|
00005960  50 43 53 20 66 75 6e 63  74 69 6f 6e 0a 3b 0a 3b  |PCS function.;.;|
00005970  20 43 20 70 72 6f 74 6f  74 79 70 65 3a 0a 3b 20  | C prototype:.; |
00005980  69 6e 74 20 6d 75 6c 5f  66 72 61 63 31 36 28 69  |int mul_frac16(i|
00005990  6e 74 20 78 2c 20 69 6e  74 20 61 29 0a 3b 0a 3b  |nt x, int a).;.;|
000059a0  20 72 65 74 75 72 6e 73  20 33 32 2d 62 69 74 20  | returns 32-bit |
000059b0  73 69 67 6e 65 64 20 69  6e 74 65 67 65 72 20 78  |signed integer x|
000059c0  2a 61 2f 36 35 35 33 36  0a 3b 20 61 73 73 75 6d  |*a/65536.; assum|
000059d0  65 73 20 72 65 73 75 6c  74 20 66 69 74 73 20 69  |es result fits i|
000059e0  6e 74 6f 20 33 32 2d 62  69 74 20 73 69 67 6e 65  |nto 32-bit signe|
000059f0  64 20 72 65 70 72 65 73  65 6e 74 61 74 69 6f 6e  |d representation|
00005a00  0a 3b 20 6e 6f 74 65 2c  20 6e 6f 20 6f 74 68 65  |.; note, no othe|
00005a10  72 20 72 65 73 74 72 69  63 74 69 6f 6e 73 20 6f  |r restrictions o|
00005a20  6e 20 61 20 2d 20 69 66  20 63 61 6e 20 67 75 61  |n a - if can gua|
00005a30  72 61 6e 74 65 65 20 61  62 73 20 61 20 3c 20 36  |rantee abs a < 6|
00005a40  35 35 33 36 2c 20 75 73  65 20 6d 75 6c 5f 66 72  |5536, use mul_fr|
00005a50  61 63 31 36 63 20 69 6e  73 74 65 61 64 20 61 73  |ac16c instead as|
00005a60  20 69 73 20 71 75 69 63  6b 65 72 0a 3b 0a 0a 20  | is quicker.;.. |
00005a70  20 20 20 20 20 20 20 45  58 50 4f 52 54 20 20 6d  |       EXPORT  m|
00005a80  75 6c 5f 66 72 61 63 31  36 0a 0a 6d 66 6e 73 74  |ul_frac16..mfnst|
00005a90  61 20 20 44 43 42 20 20  20 20 20 22 6d 75 6c 5f  |a  DCB     "mul_|
00005aa0  66 72 61 63 31 36 22 2c  20 30 0a 20 20 20 20 20  |frac16", 0.     |
00005ab0  20 20 20 41 4c 49 47 4e  0a 6d 66 6e 65 6e 64 20  |   ALIGN.mfnend |
00005ac0  20 44 43 44 20 20 20 20  20 26 66 66 30 30 30 30  | DCD     &ff0000|
00005ad0  30 30 20 2b 20 6d 66 6e  65 6e 64 20 2d 20 6d 66  |00 + mfnend - mf|
00005ae0  6e 73 74 61 0a 0a 6d 75  6c 5f 66 72 61 63 31 36  |nsta..mul_frac16|
00005af0  0a 0a 20 20 20 20 20 20  20 20 6d 75 6c 31 36 20  |..        mul16 |
00005b00  20 20 61 31 2c 20 61 32  2c 20 61 31 2c 20 61 33  |  a1, a2, a1, a3|
00005b10  2c 20 61 34 2c 20 69 70  0a 20 20 20 20 20 20 20  |, a4, ip.       |
00005b20  20 4d 4f 56 53 20 20 20  20 70 63 2c 20 6c 72 0a  | MOVS    pc, lr.|
00005b30  0a 0a 0a 3b 20 6d 75 6c  5f 66 72 61 63 31 36 63  |...; mul_frac16c|
00005b40  0a 3b 20 61 20 6c 65 61  66 20 41 50 43 53 20 66  |.; a leaf APCS f|
00005b50  75 6e 63 74 69 6f 6e 0a  3b 0a 3b 20 43 20 70 72  |unction.;.; C pr|
00005b60  6f 74 6f 74 79 70 65 3a  0a 3b 20 69 6e 74 20 6d  |ototype:.; int m|
00005b70  75 6c 5f 66 72 61 63 31  36 63 28 69 6e 74 20 78  |ul_frac16c(int x|
00005b80  2c 20 69 6e 74 20 61 29  0a 3b 0a 3b 20 72 65 74  |, int a).;.; ret|
00005b90  75 72 6e 73 20 33 32 2d  62 69 74 20 73 69 67 6e  |urns 32-bit sign|
00005ba0  65 64 20 69 6e 74 65 67  65 72 20 78 2a 61 2f 36  |ed integer x*a/6|
00005bb0  35 35 33 36 0a 3b 20 61  73 73 75 6d 65 73 20 61  |5536.; assumes a|
00005bc0  62 73 20 61 20 3c 3d 36  35 35 33 36 0a 3b 20 69  |bs a <=65536.; i|
00005bd0  66 20 69 74 20 69 73 20  70 6f 73 73 69 62 6c 65  |f it is possible|
00005be0  20 74 68 61 74 20 61 62  73 20 61 20 3e 20 36 35  | that abs a > 65|
00005bf0  35 33 36 2c 20 63 61 6c  6c 65 72 20 6d 75 73 74  |536, caller must|
00005c00  20 63 68 65 63 6b 20 72  61 6e 67 65 20 26 20 65  | check range & e|
00005c10  69 74 68 65 72 20 6e 6f  74 20 63 61 6c 6c 20 66  |ither not call f|
00005c20  6e 20 6f 72 20 72 6f 75  6e 64 20 64 6f 77 6e 20  |n or round down |
00005c30  74 6f 20 36 35 35 33 36  0a 3b 0a 0a 20 20 20 20  |to 65536.;..    |
00005c40  20 20 20 20 45 58 50 4f  52 54 20 20 6d 75 6c 5f  |    EXPORT  mul_|
00005c50  66 72 61 63 31 36 63 0a  0a 6d 66 63 6e 73 74 61  |frac16c..mfcnsta|
00005c60  20 44 43 42 20 20 20 20  20 22 6d 75 6c 5f 66 72  | DCB     "mul_fr|
00005c70  61 63 31 36 63 22 2c 20  30 0a 20 20 20 20 20 20  |ac16c", 0.      |
00005c80  20 20 41 4c 49 47 4e 0a  6d 66 63 6e 65 6e 64 20  |  ALIGN.mfcnend |
00005c90  44 43 44 20 20 20 20 20  26 66 66 30 30 30 30 30  |DCD     &ff00000|
00005ca0  30 20 2b 20 6d 66 63 6e  65 6e 64 20 2d 20 6d 66  |0 + mfcnend - mf|
00005cb0  63 6e 73 74 61 0a 0a 6d  75 6c 5f 66 72 61 63 31  |cnsta..mul_frac1|
00005cc0  36 63 0a 0a 20 20 20 20  20 20 20 20 6d 75 6c 31  |6c..        mul1|
00005cd0  36 63 20 20 61 31 2c 20  61 32 2c 20 61 31 2c 20  |6c  a1, a2, a1, |
00005ce0  61 33 0a 20 20 20 20 20  20 20 20 4d 4f 56 53 20  |a3.        MOVS |
00005cf0  20 20 20 70 63 2c 20 6c  72 0a 0a 0a 0a 3b 20 73  |   pc, lr....; s|
00005d00  71 72 74 5f 66 72 61 63  31 36 0a 3b 20 61 20 6c  |qrt_frac16.; a l|
00005d10  65 61 66 20 41 50 43 53  20 66 75 6e 63 74 69 6f  |eaf APCS functio|
00005d20  6e 0a 3b 0a 3b 20 43 20  70 72 6f 74 6f 74 79 70  |n.;.; C prototyp|
00005d30  65 3a 0a 3b 20 69 6e 74  20 73 71 72 74 5f 66 72  |e:.; int sqrt_fr|
00005d40  61 63 31 36 28 75 6e 73  69 67 6e 65 64 20 69 6e  |ac16(unsigned in|
00005d50  74 20 78 29 0a 3b 0a 3b  20 72 65 74 75 72 6e 73  |t x).;.; returns|
00005d60  20 33 32 2d 62 69 74 20  69 6e 74 65 67 65 72 20  | 32-bit integer |
00005d70  73 71 72 74 28 78 3c 3c  31 36 29 0a 3b 0a 0a 20  |sqrt(x<<16).;.. |
00005d80  20 20 20 20 20 20 20 45  58 50 4f 52 54 20 20 73  |       EXPORT  s|
00005d90  71 72 74 5f 66 72 61 63  31 36 0a 0a 73 71 66 6e  |qrt_frac16..sqfn|
00005da0  73 74 61 20 44 43 42 20  20 20 20 20 22 73 71 72  |sta DCB     "sqr|
00005db0  74 5f 66 72 61 63 31 36  22 2c 20 30 0a 20 20 20  |t_frac16", 0.   |
00005dc0  20 20 20 20 20 41 4c 49  47 4e 0a 73 71 66 6e 65  |     ALIGN.sqfne|
00005dd0  6e 64 20 44 43 44 20 20  20 20 20 26 66 66 30 30  |nd DCD     &ff00|
00005de0  30 30 30 30 20 2b 20 73  71 66 6e 65 6e 64 20 2d  |0000 + sqfnend -|
00005df0  20 73 71 66 6e 73 74 61  0a 0a 73 71 72 74 5f 66  | sqfnsta..sqrt_f|
00005e00  72 61 63 31 36 0a 0a 20  20 20 20 20 20 20 20 73  |rac16..        s|
00005e10  71 72 74 31 36 20 20 61  31 2c 20 61 31 2c 20 61  |qrt16  a1, a1, a|
00005e20  32 2c 20 61 33 2c 20 61  34 2c 20 69 70 0a 20 20  |2, a3, a4, ip.  |
00005e30  20 20 20 20 20 20 4d 4f  56 53 20 20 20 20 70 63  |      MOVS    pc|
00005e40  2c 20 6c 72 0a 0a 0a 0a  20 20 20 20 20 20 20 20  |, lr....        |
00005e50  45 4e 44 0a                                       |END.|
00005e54