Initial revision
[ssr] / StraySrc / Libraries / Sapphire / s / sqrt
1 ;
2 ; sqrt.s
3 ;
4 ; Fast square root routines
5 ;
6 ; © 1995-1998 Straylight
7 ;
8
9 ;----- Licensing note -------------------------------------------------------
10 ;
11 ; This file is part of Straylight's Sapphire library.
12 ;
13 ; Sapphire is free software; you can redistribute it and/or modify
14 ; it under the terms of the GNU General Public License as published by
15 ; the Free Software Foundation; either version 2, or (at your option)
16 ; any later version.
17 ;
18 ; Sapphire is distributed in the hope that it will be useful,
19 ; but WITHOUT ANY WARRANTY; without even the implied warranty of
20 ; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 ; GNU General Public License for more details.
22 ;
23 ; You should have received a copy of the GNU General Public License
24 ; along with Sapphire. If not, write to the Free Software Foundation,
25 ; 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
26
27 ;----- Standard header ------------------------------------------------------
28
29 GET libs:header
30 GET libs:swis
31
32 GET libs:stream
33
34 ;----- Main code ------------------------------------------------------------
35
36 AREA |Sapphire$$Code|,CODE,READONLY
37
38 ; --- sqrt ---
39 ;
40 ; On entry: R0 == value to square-root
41 ;
42 ; On exit: R0 == the result
43 ;
44 ; Use: Evaluates the square root of the number given. This routine
45 ; is constructed from the information supplied by David Seal,
46 ; and is *extremely* fast.
47
48 EXPORT sqrt
49 sqrt ROUT
50
51 STMFD R13!,{R1-R4,R14} ;Stack registers
52 MOV R1,#0 ;Result so far
53 MOV R2,#0 ;Current remainder
54 MOV R3,#1 ;A '01' pair
55 MOV R4,#3 ;A nice mask
56
57 GBLA count
58 count SETA 0 ;Start the count at 30
59
60 WHILE count<=28 ;Set up the loop condition
61
62 AND R14,R4,R0,LSR #30-count
63 ORR R2,R14,R2,LSL #2
64 ORR R14,R3,R1,LSL #2
65 CMP R2,R14
66 ADC R1,R1,R1
67 SUBCS R2,R2,R14
68
69 count SETA count+2
70
71 WEND
72
73 AND R14,R4,R0
74 ORR R2,R14,R2,LSL #2
75 ORR R14,R3,R1,LSL #2
76 CMP R2,R14
77 ADC R1,R1,R1
78 SUBCS R2,R2,R14
79
80 c MOV R0,R1 ;Put the result in R0
81 LDMFD R13!,{R1-R4,PC}^ ;Return to caller
82
83 LTORG
84
85 ;----- That's all, folks ----------------------------------------------------
86
87 END