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