xref: /minix3/lib/libm/arch/vax/n_cabs.S (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1*0a6a1f1dSLionel Sambuc/*	$NetBSD: n_cabs.S,v 1.7 2014/10/10 20:58:09 martin Exp $	*/
284d9c625SLionel Sambuc/*
384d9c625SLionel Sambuc * Copyright (c) 1985, 1993
484d9c625SLionel Sambuc *	The Regents of the University of California.  All rights reserved.
584d9c625SLionel Sambuc *
684d9c625SLionel Sambuc * Redistribution and use in source and binary forms, with or without
784d9c625SLionel Sambuc * modification, are permitted provided that the following conditions
884d9c625SLionel Sambuc * are met:
984d9c625SLionel Sambuc * 1. Redistributions of source code must retain the above copyright
1084d9c625SLionel Sambuc *    notice, this list of conditions and the following disclaimer.
1184d9c625SLionel Sambuc * 2. Redistributions in binary form must reproduce the above copyright
1284d9c625SLionel Sambuc *    notice, this list of conditions and the following disclaimer in the
1384d9c625SLionel Sambuc *    documentation and/or other materials provided with the distribution.
1484d9c625SLionel Sambuc * 3. Neither the name of the University nor the names of its contributors
1584d9c625SLionel Sambuc *    may be used to endorse or promote products derived from this software
1684d9c625SLionel Sambuc *    without specific prior written permission.
1784d9c625SLionel Sambuc *
1884d9c625SLionel Sambuc * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
1984d9c625SLionel Sambuc * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2084d9c625SLionel Sambuc * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2184d9c625SLionel Sambuc * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2284d9c625SLionel Sambuc * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2384d9c625SLionel Sambuc * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2484d9c625SLionel Sambuc * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2584d9c625SLionel Sambuc * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2684d9c625SLionel Sambuc * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2784d9c625SLionel Sambuc * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2884d9c625SLionel Sambuc * SUCH DAMAGE.
2984d9c625SLionel Sambuc *
3084d9c625SLionel Sambuc *	@(#)cabs.s	8.1 (Berkeley) 6/4/93
3184d9c625SLionel Sambuc */
3284d9c625SLionel Sambuc
3384d9c625SLionel Sambuc#include <machine/asm.h>
3484d9c625SLionel Sambuc
3584d9c625SLionel Sambuc	.globl	_C_LABEL(__libm_dsqrt_r5)
3684d9c625SLionel Sambuc/*
3784d9c625SLionel Sambuc * double precision complex absolute value
3884d9c625SLionel Sambuc * CABS by W. Kahan, 9/7/80.
3984d9c625SLionel Sambuc * Revised for reserved operands by E. LeBlanc, 8/18/82
4084d9c625SLionel Sambuc * argument for complex absolute value by reference, *4(%ap)
4184d9c625SLionel Sambuc * argument for cabs and hypot (C fcns) by value, 4(%ap)
4284d9c625SLionel Sambuc * output is in %r0:%r1 (error less than 0.86 ulps)
4384d9c625SLionel Sambuc */
4484d9c625SLionel Sambuc
4584d9c625SLionel Sambuc/*	entry for c functions cabs and hypot */
4684d9c625SLionel Sambuc#ifdef WEAK_ALIAS
4784d9c625SLionel SambucWEAK_ALIAS(hypotf, _hypotf)
4884d9c625SLionel Sambuc#endif
4984d9c625SLionel Sambuc
5084d9c625SLionel SambucENTRY(_hypotf, 0)
5184d9c625SLionel Sambuc	cvtfd	4(%ap),-(%sp)
5284d9c625SLionel Sambuc	calls	$2,_C_LABEL(_hypot)
5384d9c625SLionel Sambuc	cvtdf	%r0,%r0
5484d9c625SLionel Sambuc	ret
5584d9c625SLionel Sambuc
5684d9c625SLionel Sambuc#ifdef WEAK_ALIAS
5784d9c625SLionel SambucWEAK_ALIAS(hypot, _hypot)
58*0a6a1f1dSLionel SambucWEAK_ALIAS(hypotl, _hypot)
59*0a6a1f1dSLionel SambucWEAK_ALIAS(_hypotl, _hypot)
6084d9c625SLionel Sambuc#endif
6184d9c625SLionel Sambuc
6284d9c625SLionel SambucALTENTRY(cabs)
6384d9c625SLionel SambucENTRY(_hypot, 0x8040) 		# save %r6, enable floating overflow
6484d9c625SLionel Sambuc	movq	4(%ap),%r0	# %r0:1 = x
6584d9c625SLionel Sambuc	movq	12(%ap),%r2	# %r2:3 = y
6684d9c625SLionel Sambuc	jbr	cabs2
6784d9c625SLionel Sambuc
6884d9c625SLionel Sambuc/*	entry for Fortran use, call by:   d = abs(z) */
6984d9c625SLionel SambucENTRY(z_abs, 0x8040)		# save %r6, enable floating overflow
7084d9c625SLionel Sambuc	movl	4(%ap),%r2	# indirect addressing is necessary here
7184d9c625SLionel Sambuc	movq	(%r2)+,%r0	# %r0:1 = x
7284d9c625SLionel Sambuc	movq	(%r2),%r2		# %r2:3 = y
7384d9c625SLionel Sambuc
7484d9c625SLionel Sambuccabs2:
7584d9c625SLionel Sambuc	bicw3	$0x7f,%r0,%r4	# %r4 has signed biased exp of x
7684d9c625SLionel Sambuc	cmpw	$0x8000,%r4
7784d9c625SLionel Sambuc	jeql	return		# x is a reserved operand, so return it
7884d9c625SLionel Sambuc	bicw3	$0x7f,%r2,%r5	# %r5 has signed biased exp of y
7984d9c625SLionel Sambuc	cmpw	$0x8000,%r5
8084d9c625SLionel Sambuc	jneq	cont		/* y isn't a reserved operand */
8184d9c625SLionel Sambuc	movq	%r2,%r0		/* return y if it's reserved */
8284d9c625SLionel Sambuc	ret
8384d9c625SLionel Sambuc
8484d9c625SLionel Sambuccont:
8584d9c625SLionel Sambuc	bsbb	regs_set	# %r0:1 = dsqrt(x^2+y^2)/2^%r6
8684d9c625SLionel Sambuc	addw2	%r6,%r0		# unscaled cdabs in %r0:1
8784d9c625SLionel Sambuc	jvc	return		# unless it overflows
8884d9c625SLionel Sambuc	subw2	$0x80,%r0	# halve %r0 to get meaningful overflow
8984d9c625SLionel Sambuc	addd2	%r0,%r0		# overflow; %r0 is half of true abs value
9084d9c625SLionel Sambucreturn:
9184d9c625SLionel Sambuc	ret
9284d9c625SLionel Sambuc
9384d9c625SLionel SambucENTRY(__libm_cdabs_r6,0)	# ENTRY POINT for cdsqrt
9484d9c625SLionel Sambuc				# calculates a scaled (factor in %r6)
9584d9c625SLionel Sambuc				# complex absolute value
9684d9c625SLionel Sambuc
9784d9c625SLionel Sambuc	movq	(%r4)+,%r0	# %r0:%r1 = x via indirect addressing
9884d9c625SLionel Sambuc	movq	(%r4),%r2		# %r2:%r3 = y via indirect addressing
9984d9c625SLionel Sambuc
10084d9c625SLionel Sambuc	bicw3	$0x7f,%r0,%r5	# %r5 has signed biased exp of x
10184d9c625SLionel Sambuc	cmpw	$0x8000,%r5
10284d9c625SLionel Sambuc	jeql	cdreserved	# x is a reserved operand
10384d9c625SLionel Sambuc	bicw3	$0x7f,%r2,%r5	# %r5 has signed biased exp of y
10484d9c625SLionel Sambuc	cmpw	$0x8000,%r5
10584d9c625SLionel Sambuc	jneq	regs_set	/* y isn't a reserved operand either? */
10684d9c625SLionel Sambuc
10784d9c625SLionel Sambuccdreserved:
10884d9c625SLionel Sambuc	movl	*4(%ap),%r4	# %r4 -> (u,v), if x or y is reserved
10984d9c625SLionel Sambuc	movq	%r0,(%r4)+	# copy u and v as is and return
11084d9c625SLionel Sambuc	movq	%r2,(%r4)		# (again addressing is indirect)
11184d9c625SLionel Sambuc	ret
11284d9c625SLionel Sambuc
11384d9c625SLionel Sambucregs_set:
11484d9c625SLionel Sambuc	bicw2	$0x8000,%r0	# %r0:%r1 = dabs(x)
11584d9c625SLionel Sambuc	bicw2	$0x8000,%r2	# %r2:%r3 = dabs(y)
11684d9c625SLionel Sambuc	cmpw	%r0,%r2
11784d9c625SLionel Sambuc	jgeq	ordered
11884d9c625SLionel Sambuc	movq	%r0,%r4
11984d9c625SLionel Sambuc	movq	%r2,%r0
12084d9c625SLionel Sambuc	movq	%r4,%r2		# force y's exp <= x's exp
12184d9c625SLionel Sambucordered:
12284d9c625SLionel Sambuc	bicw3	$0x7f,%r0,%r6	# %r6 = exponent(x) + bias(129)
12384d9c625SLionel Sambuc	jeql	retsb		# if x = y = 0 then cdabs(x,y) = 0
12484d9c625SLionel Sambuc	subw2	$0x4780,%r6	# %r6 = exponent(x) - 14
12584d9c625SLionel Sambuc	subw2	%r6,%r0		# 2^14 <= scaled x < 2^15
12684d9c625SLionel Sambuc	bitw	$0xff80,%r2
12784d9c625SLionel Sambuc	jeql	retsb		# if y = 0 return dabs(x)
12884d9c625SLionel Sambuc	subw2	%r6,%r2
12984d9c625SLionel Sambuc	cmpw	$0x3780,%r2	# if scaled y < 2^-18
13084d9c625SLionel Sambuc	jgtr	retsb		#   return dabs(x)
13184d9c625SLionel Sambuc	emodd	%r0,$0,%r0,%r4,%r0	# %r4 + %r0:1 = scaled x^2
13284d9c625SLionel Sambuc	emodd	%r2,$0,%r2,%r5,%r2	# %r5 + %r2:3 = scaled y^2
13384d9c625SLionel Sambuc	addd2	%r2,%r0
13484d9c625SLionel Sambuc	addl2	%r5,%r4
13584d9c625SLionel Sambuc	cvtld	%r4,%r2
13684d9c625SLionel Sambuc	addd2	%r2,%r0		# %r0:1 = scaled x^2 + y^2
13784d9c625SLionel Sambuc	jmp	_C_LABEL(__libm_dsqrt_r5)+2
13884d9c625SLionel Sambuc				# %r0:1 = dsqrt(x^2+y^2)/2^%r6
13984d9c625SLionel Sambucretsb:
14084d9c625SLionel Sambuc	rsb			# error < 0.86 ulp
141