n_cabs.S revision 1.6 1 1.6 mhitch /* $NetBSD: n_cabs.S,v 1.6 2008/03/20 16:41:26 mhitch 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.5 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 * @(#)cabs.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.3 matt .globl _C_LABEL(__libm_dsqrt_r5)
36 1.1 ragge /*
37 1.1 ragge * double precision complex absolute value
38 1.1 ragge * CABS by W. Kahan, 9/7/80.
39 1.1 ragge * Revised for reserved operands by E. LeBlanc, 8/18/82
40 1.4 matt * argument for complex absolute value by reference, *4(%ap)
41 1.4 matt * argument for cabs and hypot (C fcns) by value, 4(%ap)
42 1.4 matt * output is in %r0:%r1 (error less than 0.86 ulps)
43 1.1 ragge */
44 1.1 ragge
45 1.1 ragge /* entry for c functions cabs and hypot */
46 1.6 mhitch #ifdef WEAK_ALIAS
47 1.6 mhitch WEAK_ALIAS(hypotf, _hypotf)
48 1.6 mhitch #endif
49 1.6 mhitch
50 1.6 mhitch ENTRY(_hypotf, 0)
51 1.6 mhitch cvtfd 4(%ap),-(%sp)
52 1.6 mhitch calls $2,_C_LABEL(_hypot)
53 1.6 mhitch cvtdf %r0,%r0
54 1.6 mhitch ret
55 1.6 mhitch
56 1.6 mhitch #ifdef WEAK_ALIAS
57 1.6 mhitch WEAK_ALIAS(hypot, _hypot)
58 1.6 mhitch #endif
59 1.6 mhitch
60 1.3 matt ALTENTRY(cabs)
61 1.6 mhitch ENTRY(_hypot, 0x8040) # save %r6, enable floating overflow
62 1.4 matt movq 4(%ap),%r0 # %r0:1 = x
63 1.4 matt movq 12(%ap),%r2 # %r2:3 = y
64 1.3 matt jbr cabs2
65 1.3 matt
66 1.1 ragge /* entry for Fortran use, call by: d = abs(z) */
67 1.4 matt ENTRY(z_abs, 0x8040) # save %r6, enable floating overflow
68 1.4 matt movl 4(%ap),%r2 # indirect addressing is necessary here
69 1.4 matt movq (%r2)+,%r0 # %r0:1 = x
70 1.4 matt movq (%r2),%r2 # %r2:3 = y
71 1.1 ragge
72 1.1 ragge cabs2:
73 1.4 matt bicw3 $0x7f,%r0,%r4 # %r4 has signed biased exp of x
74 1.4 matt cmpw $0x8000,%r4
75 1.1 ragge jeql return # x is a reserved operand, so return it
76 1.4 matt bicw3 $0x7f,%r2,%r5 # %r5 has signed biased exp of y
77 1.4 matt cmpw $0x8000,%r5
78 1.1 ragge jneq cont /* y isn't a reserved operand */
79 1.4 matt movq %r2,%r0 /* return y if it's reserved */
80 1.1 ragge ret
81 1.1 ragge
82 1.1 ragge cont:
83 1.4 matt bsbb regs_set # %r0:1 = dsqrt(x^2+y^2)/2^%r6
84 1.4 matt addw2 %r6,%r0 # unscaled cdabs in %r0:1
85 1.1 ragge jvc return # unless it overflows
86 1.4 matt subw2 $0x80,%r0 # halve %r0 to get meaningful overflow
87 1.4 matt addd2 %r0,%r0 # overflow; %r0 is half of true abs value
88 1.1 ragge return:
89 1.1 ragge ret
90 1.1 ragge
91 1.3 matt ENTRY(__libm_cdabs_r6,0) # ENTRY POINT for cdsqrt
92 1.4 matt # calculates a scaled (factor in %r6)
93 1.1 ragge # complex absolute value
94 1.1 ragge
95 1.4 matt movq (%r4)+,%r0 # %r0:%r1 = x via indirect addressing
96 1.4 matt movq (%r4),%r2 # %r2:%r3 = y via indirect addressing
97 1.1 ragge
98 1.4 matt bicw3 $0x7f,%r0,%r5 # %r5 has signed biased exp of x
99 1.4 matt cmpw $0x8000,%r5
100 1.1 ragge jeql cdreserved # x is a reserved operand
101 1.4 matt bicw3 $0x7f,%r2,%r5 # %r5 has signed biased exp of y
102 1.4 matt cmpw $0x8000,%r5
103 1.1 ragge jneq regs_set /* y isn't a reserved operand either? */
104 1.1 ragge
105 1.1 ragge cdreserved:
106 1.4 matt movl *4(%ap),%r4 # %r4 -> (u,v), if x or y is reserved
107 1.4 matt movq %r0,(%r4)+ # copy u and v as is and return
108 1.4 matt movq %r2,(%r4) # (again addressing is indirect)
109 1.1 ragge ret
110 1.1 ragge
111 1.1 ragge regs_set:
112 1.4 matt bicw2 $0x8000,%r0 # %r0:%r1 = dabs(x)
113 1.4 matt bicw2 $0x8000,%r2 # %r2:%r3 = dabs(y)
114 1.4 matt cmpw %r0,%r2
115 1.1 ragge jgeq ordered
116 1.4 matt movq %r0,%r4
117 1.4 matt movq %r2,%r0
118 1.4 matt movq %r4,%r2 # force y's exp <= x's exp
119 1.1 ragge ordered:
120 1.4 matt bicw3 $0x7f,%r0,%r6 # %r6 = exponent(x) + bias(129)
121 1.1 ragge jeql retsb # if x = y = 0 then cdabs(x,y) = 0
122 1.4 matt subw2 $0x4780,%r6 # %r6 = exponent(x) - 14
123 1.4 matt subw2 %r6,%r0 # 2^14 <= scaled x < 2^15
124 1.4 matt bitw $0xff80,%r2
125 1.1 ragge jeql retsb # if y = 0 return dabs(x)
126 1.4 matt subw2 %r6,%r2
127 1.4 matt cmpw $0x3780,%r2 # if scaled y < 2^-18
128 1.1 ragge jgtr retsb # return dabs(x)
129 1.4 matt emodd %r0,$0,%r0,%r4,%r0 # %r4 + %r0:1 = scaled x^2
130 1.4 matt emodd %r2,$0,%r2,%r5,%r2 # %r5 + %r2:3 = scaled y^2
131 1.4 matt addd2 %r2,%r0
132 1.4 matt addl2 %r5,%r4
133 1.4 matt cvtld %r4,%r2
134 1.4 matt addd2 %r2,%r0 # %r0:1 = scaled x^2 + y^2
135 1.3 matt jmp _C_LABEL(__libm_dsqrt_r5)+2
136 1.4 matt # %r0:1 = dsqrt(x^2+y^2)/2^%r6
137 1.1 ragge retsb:
138 1.1 ragge rsb # error < 0.86 ulp
139