n_sqrt.S revision 1.2 1 /* $NetBSD: n_sqrt.S,v 1.2 1998/10/31 02:06:02 matt Exp $ */
2 /*
3 * Copyright (c) 1985, 1993
4 * The Regents of the University of California. All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. All advertising materials mentioning features or use of this software
15 * must display the following acknowledgement:
16 * This product includes software developed by the University of
17 * California, Berkeley and its contributors.
18 * 4. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * SUCH DAMAGE.
33 *
34 * @(#)sqrt.s 8.1 (Berkeley) 6/4/93
35 */
36
37 /*
38 * double sqrt(arg) revised August 15,1982
39 * double arg;
40 * if(arg<0.0) { _errno = EDOM; return(<a reserved operand>); }
41 * if arg is a reserved operand it is returned as it is
42 * W. Kahan's magic square root
43 * coded by Heidi Stettner and revised by Emile LeBlanc 8/18/82
44 *
45 * entry points:_d_sqrt address of double arg is on the stack
46 * _sqrt double arg is on the stack
47 */
48 .text
49 .align 1
50 .globl _sqrt
51 .type _sqrt,@function
52 .globl _d_sqrt
53 .type _d_sqrt,@function
54 .globl libm$dsqrt_r5
55 .type libm$dsqrt_r5,@label
56 .set EDOM,33
57
58 _d_sqrt:
59 .word 0x003c # save r5,r4,r3,r2
60 movq *4(ap),r0
61 jmp dsqrt2
62 _sqrt:
63 .word 0x003c # save r5,r4,r3,r2
64 movq 4(ap),r0
65 dsqrt2: bicw3 $0x807f,r0,r2 # check exponent of input
66 jeql noexp # biased exponent is zero -> 0.0 or reserved
67 bsbb libm$dsqrt_r5
68 noexp: ret
69
70 /* **************************** internal procedure */
71
72 libm$dsqrt_r5: /* ENTRY POINT FOR cdabs and cdsqrt */
73 /* returns double square root scaled by */
74 /* 2^r6 */
75
76 movd r0,r4
77 jleq nonpos # argument is not positive
78 movzwl r4,r2
79 ashl $-1,r2,r0
80 addw2 $0x203c,r0 # r0 has magic initial approximation
81 /*
82 * Do two steps of Heron's rule
83 * ((arg/guess) + guess) / 2 = better guess
84 */
85 divf3 r0,r4,r2
86 addf2 r2,r0
87 subw2 $0x80,r0 # divide by two
88
89 divf3 r0,r4,r2
90 addf2 r2,r0
91 subw2 $0x80,r0 # divide by two
92
93 /* Scale argument and approximation to prevent over/underflow */
94
95 bicw3 $0x807f,r4,r1
96 subw2 $0x4080,r1 # r1 contains scaling factor
97 subw2 r1,r4
98 movl r0,r2
99 subw2 r1,r2
100
101 /* Cubic step
102 *
103 * b = a + 2*a*(n-a*a)/(n+3*a*a) where b is better approximation,
104 * a is approximation, and n is the original argument.
105 * (let s be scale factor in the following comments)
106 */
107 clrl r1
108 clrl r3
109 muld2 r0,r2 # r2:r3 = a*a/s
110 subd2 r2,r4 # r4:r5 = n/s - a*a/s
111 addw2 $0x100,r2 # r2:r3 = 4*a*a/s
112 addd2 r4,r2 # r2:r3 = n/s + 3*a*a/s
113 muld2 r0,r4 # r4:r5 = a*n/s - a*a*a/s
114 divd2 r2,r4 # r4:r5 = a*(n-a*a)/(n+3*a*a)
115 addw2 $0x80,r4 # r4:r5 = 2*a*(n-a*a)/(n+3*a*a)
116 addd2 r4,r0 # r0:r1 = a + 2*a*(n-a*a)/(n+3*a*a)
117 rsb # DONE!
118 nonpos:
119 jneq negarg
120 ret # argument and root are zero
121 negarg:
122 pushl $EDOM
123 calls $1,_infnan # generate the reserved op fault
124 ret
125