1/* $NetBSD: n_support.S,v 1.13 2024/07/17 12:00:48 riastradh 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. Neither the name of the University nor the names of its contributors 15 * may be used to endorse or promote products derived from this software 16 * without specific prior written permission. 17 * 18 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 21 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 28 * SUCH DAMAGE. 29 * 30 * @(#)support.s 8.1 (Berkeley) 6/4/93 31 */ 32#include <machine/asm.h> 33 34WEAK_ALIAS(logbl,logb) 35WEAK_ALIAS(copysignl, _copysignl) 36WEAK_ALIAS(_copysignl, copysign) 37 38 .text 39_sccsid: 40 .asciz "@(#)support.s\t1.3 (Berkeley) 8/21/85; 8.1 (ucb.elefunt) 6/4/93" 41 42/* 43 * copysign(x,y), 44 * logb(x), 45 * scalb(x,N), 46 * finite(x), 47 * drem(x,y), 48 * Coded in vax assembly language by K.C. Ng, 3/14/85. 49 * Revised by K.C. Ng on 4/9/85. 50 */ 51 52/* 53 * double copysign(double x,double y) 54 */ 55 56ENTRY(copysign, 0) 57 movq 4(%ap),%r0 # load x into %r0 58 bicw3 $0x807f,%r0,%r2 # mask off the exponent of x 59 beql Lz # if zero or reserved op then return x 60 bicw3 $0x7fff,12(%ap),%r2 # copy the sign bit of y into %r2 61 bicw2 $0x8000,%r0 # replace x by |x| 62 bisw2 %r2,%r0 # copy the sign bit of y to x 63Lz: ret 64END(copysign) 65 66ENTRY(copysignf, 0) 67 movl 4(%ap),%r0 # load x into %r0 68 bicw3 $0x807f,%r0,%r2 # mask off the exponent of x 69 beql 1f # if zero or reserved op then return x 70 bicw3 $0x7fff,8(%ap),%r2 # copy the sign bit of y into %r2 71 bicw2 $0x8000,%r0 # replace x by |x| 72 bisw2 %r2,%r0 # copy the sign bit of y to x 731: ret 74END(copysignf) 75 76/* 77 * float logbf(float x); 78 */ 79ENTRY(logbf, 0) 80 cvtfd 4(%ap),-(%sp) 81 calls $2,_C_LABEL(logb) 82 cvtdf %r0,%r0 83 ret 84END(logbf) 85 86/* 87 * double logb(double x); 88 */ 89ENTRY(logb, 0) 90 bicl3 $0xffff807f,4(%ap),%r0 # mask off the exponent of x 91 beql Ln 92 ashl $-7,%r0,%r0 # get the bias exponent 93 subl2 $129,%r0 # get the unbias exponent 94 cvtld %r0,%r0 # return the answer in double 95 ret 96Ln: movq 4(%ap),%r0 # %r0:1 = x (zero or reserved op) 97 bneq 1f # simply return if reserved op 98 movq $0x0000fe00ffffcfff,%r0 # -2147483647.0 991: ret 100END(logb) 101 102/* 103 * long finite(double x); 104 */ 105#ifndef __GFLOAT__ 106WEAK_ALIAS(finitef, _finitef) 107STRONG_ALIAS(_finitef, _finite) 108#endif 109WEAK_ALIAS(finite, _finite) 110ENTRY(_finite, 0) 111 bicw3 $0x7f,4(%ap),%r0 # mask off the significand 112 cmpw %r0,$0x8000 # to see if x is the reserved op 113 beql 1f # if so, return FALSE (0) 114 movl $1,%r0 # else return TRUE (1) 115 ret 1161: clrl %r0 117 ret 118END(_finite) 119 120/* int isnan(double x); 121 */ 122#if 0 123ENTRY(isnan, 0) 124 clrl %r0 125 ret 126#endif 127 128/* int isnanf(float x); 129 */ 130ENTRY(isnanf, 0) 131 clrl %r0 132 ret 133END(isnanf) 134 135/* 136 * double scalb(x,N) 137 * double x; double N; 138 */ 139 .set ERANGE,34 140 141ENTRY(scalb, 0) 142 movq 4(%ap),%r0 143 bicl3 $0xffff807f,%r0,%r3 144 beql ret1 # 0 or reserved operand 145 movq 12(%ap),%r4 146 cvtdl %r4, %r2 147 cmpl %r2,$0x12c 148 bgeq ovfl 149 cmpl %r2,$-0x12c 150 bleq unfl 151 ashl $7,%r2,%r2 152 addl2 %r2,%r3 153 bleq unfl 154 cmpl %r3,$0x8000 155 bgeq ovfl 156 addl2 %r2,%r0 157 ret 158ovfl: pushl $ERANGE 159 calls $1,_C_LABEL(infnan) # if it returns 160 bicw3 $0x7fff,4(%ap),%r2 # get the sign of input arg 161 bisw2 %r2,%r0 # re-attach the sign to %r0/1 162 ret 163unfl: movq $0,%r0 164ret1: ret 165END(scalb) 166 167/* 168 * DREM(X,Y) 169 * RETURN X REM Y =X-N*Y, N=[X/Y] ROUNDED (ROUNDED TO EVEN IN THE HALF WAY CASE) 170 * DOUBLE PRECISION (VAX D format 56 bits) 171 * CODED IN VAX ASSEMBLY LANGUAGE BY K.C. NG, 4/8/85. 172 */ 173 .set EDOM,33 174 175ENTRY(drem, 0x0fc0) 176 subl2 $12,%sp 177 movq 4(%ap),%r0 #%r0=x 178 movq 12(%ap),%r2 #%r2=y 179 jeql Rop #if y=0 then generate reserved op fault 180 bicw3 $0x007f,%r0,%r4 #check if x is Rop 181 cmpw %r4,$0x8000 182 jeql Ret #if x is Rop then return Rop 183 bicl3 $0x007f,%r2,%r4 #check if y is Rop 184 cmpw %r4,$0x8000 185 jeql Ret #if y is Rop then return Rop 186 bicw2 $0x8000,%r2 #y := |y| 187 movw $0,-4(%fp) #-4(%fp) = nx := 0 188 cmpw %r2,$0x1c80 #yexp ? 57 189 bgtr C1 #if yexp > 57 goto C1 190 addw2 $0x1c80,%r2 #scale up y by 2**57 191 movw $0x1c80,-4(%fp) #nx := 57 (exponent field) 192C1: 193 movw -4(%fp),-8(%fp) #-8(%fp) = nf := nx 194 bicw3 $0x7fff,%r0,-12(%fp) #-12(%fp) = sign of x 195 bicw2 $0x8000,%r0 #x := |x| 196 movq %r2,%r10 #y1 := y 197 bicl2 $0xffff07ff,%r11 #clear the last 27 bits of y1 198loop: 199 cmpd %r0,%r2 #x ? y 200 bleq E1 #if x <= y goto E1 201 /* begin argument reduction */ 202 movq %r2,%r4 #t =y 203 movq %r10,%r6 #t1=y1 204 bicw3 $0x807f,%r0,%r8 #xexp= exponent of x 205 bicw3 $0x807f,%r2,%r9 #yexp= exponent fo y 206 subw2 %r9,%r8 #xexp-yexp 207 subw2 $0x0c80,%r8 #k=xexp-yexp-25(exponent bit field) 208 blss C2 #if k<0 goto C2 209 addw2 %r8,%r4 #t +=k 210 addw2 %r8,%r6 #t1+=k, scale up t and t1 211C2: 212 divd3 %r4,%r0,%r8 #x/t 213 cvtdl %r8,%r8 #n=[x/t] truncated 214 cvtld %r8,%r8 #float(n) 215 subd2 %r6,%r4 #t:=t-t1 216 muld2 %r8,%r4 #n*(t-t1) 217 muld2 %r8,%r6 #n*t1 218 subd2 %r6,%r0 #x-n*t1 219 subd2 %r4,%r0 #(x-n*t1)-n*(t-t1) 220 jbr loop 221E1: 222 movw -4(%fp),%r6 #%r6=nx 223 beql C3 #if nx=0 goto C3 224 addw2 %r6,%r0 #x:=x*2**57 scale up x by nx 225 movw $0,-4(%fp) #clear nx 226 jbr loop 227C3: 228 movq %r2,%r4 #%r4 = y 229 subw2 $0x80,%r4 #%r4 = y/2 230 cmpd %r0,%r4 #x:y/2 231 blss E2 #if x < y/2 goto E2 232 bgtr C4 #if x > y/2 goto C4 233 cvtdl %r8,%r8 #ifix(float(n)) 234 blbc %r8,E2 #if the last bit is zero, goto E2 235C4: 236 subd2 %r2,%r0 #x-y 237E2: 238 xorw2 -12(%fp),%r0 #x^sign (exclusive or) 239 movw -8(%fp),%r6 #%r6=nf 240 bicw3 $0x807f,%r0,%r8 #%r8=exponent of x 241 bicw2 $0x7f80,%r0 #clear the exponent of x 242 subw2 %r6,%r8 #%r8=xexp-nf 243 bgtr C5 #if xexp-nf is positive goto C5 244 movw $0,%r8 #clear %r8 245 movq $0,%r0 #x underflow to zero 246C5: 247 bisw2 %r8,%r0 /* put %r8 into x's exponent field */ 248 ret 249Rop: #Reserved operand 250 pushl $EDOM 251 calls $1,_C_LABEL(infnan) #generate reserved op fault 252 ret 253Ret: 254 movq $0x8000,%r0 #propagate reserved op 255 ret 256END(drem) 257