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