# Copyright (c) 1987 Regents of the University of California.
# Redistribution and use in source and binary forms are permitted
# provided that this notice is preserved and that due credit is given
# to the University of California at Berkeley. The name of the University
# may not be used to endorse or promote products derived from this
# software without specific prior written permission. This software
# is provided ``as is'' without express or implied warranty.
# All recipients should regard themselves as participants in an ongoing
# research project and hence should feel obligated to report their
# experiences (good or bad) with these elementary function codes, using
# the sendbug(8) program, to the authors.
# @(#)cabs.s 5.3 (Berkeley) %G%
.asciz "@(#)cabs.s 5.3 5.3 (ucb.elefunt) %G%"
# double precision complex absolute value
# CABS by W. Kahan, 9/7/80.
# Revised for reserved operands by E. LeBlanc, 8/18/82
# argument for complex absolute value by reference, *4(fp)
# argument for cabs and hypot (C fcns) by value, 4(fp)
# entry for c functions cabs and hypot
.word 0x807c # save r2-r6, enable floating overflow
movl 12(fp),r2 # r2:3 = y
# entry for Fortran use, call by: d = abs(z)
.word 0x807c # save r2-r6, enable floating overflow
movl 4(fp),r4 # indirect addressing is necessary here
1: andl3 $0xff800000,r0,r4 # r4 has signed biased exp of x
beql 2f # x is a reserved operand, so return it
andl3 $0xff800000,r2,r5 # r5 has signed biased exp of y
bneq 3f # y isn't a reserved operand
movl r2,r0 # return y if it's reserved
3: callf $4,regs_set # r0:1 = dsqrt(x^2+y^2)/2^r6
addl2 r6,r0 # unscaled cdabs in r0:1
jvc 2b # unless it overflows
subl2 $0x800000,r0 # halve r0 to get meaningful overflow
addd r0 # overflow; r0 is half of true abs value
andl2 $0x7fffffff,r0 # r0:r1 = dabs(x)
andl2 $0x7fffffff,r2 # r2:r3 = dabs(y)
movl r4,r2 # force y's exp <= x's exp
4: andl3 $0xff800000,r0,r6 # r6 = exponent(x) + bias(129)
beql 5f # if x = y = 0 then cdabs(x,y) = 0
subl2 $0x47800000,r6 # r6 = exponent(x) - 14
subl2 r6,r0 # 2^14 <= scaled x < 2^15
beql 5f # if y = 0 return dabs(x)
cmpl $0x37800000,r2 # if scaled y < 2^-18
std r0 # r0:1 = scaled x^2
muld r2 # acc = scaled y^2
callf $12,_sqrt # r0:1 = dsqrt(x^2+y^2)/2^r6