xref: /netbsd-src/lib/libm/arch/vax/n_support.S (revision 2a399c6883d870daece976daec6ffa7bb7f934ce)
1/*	$NetBSD: n_support.S,v 1.1 1995/10/10 23:40:30 ragge 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 *	@(#)support.s	8.1 (Berkeley) 6/4/93
35 */
36	.data
37	.align	2
38_sccsid:
39.asciz	"@(#)support.s	1.3 (Berkeley) 8/21/85; 8.1 (ucb.elefunt) 6/4/93"
40
41/*
42 * copysign(x,y),
43 * logb(x),
44 * scalb(x,N),
45 * finite(x),
46 * drem(x,y),
47 * Coded in vax assembly language by K.C. Ng,  3/14/85.
48 * Revised by K.C. Ng on 4/9/85.
49 */
50
51/*
52 * double copysign(x,y)
53 * double x,y;
54 */
55	.globl	_copysign
56	.text
57	.align	1
58_copysign:
59	.word	0x4
60	movq	4(ap),r0		# load x into r0
61	bicw3	$0x807f,r0,r2		# mask off the exponent of x
62	beql	Lz			# if zero or reserved op then return x
63	bicw3	$0x7fff,12(ap),r2	# copy the sign bit of y into r2
64	bicw2	$0x8000,r0		# replace x by |x|
65	bisw2	r2,r0			# copy the sign bit of y to x
66Lz:	ret
67
68/*
69 * double logb(x)
70 * double x;
71 */
72	.globl	_logb
73	.text
74	.align	1
75_logb:
76	.word	0x0
77	bicl3	$0xffff807f,4(ap),r0	# mask off the exponent of x
78	beql    Ln
79	ashl	$-7,r0,r0		# get the bias exponent
80	subl2	$129,r0			# get the unbias exponent
81	cvtld	r0,r0			# return the answer in double
82	ret
83Ln:	movq	4(ap),r0		# r0:1 = x (zero or reserved op)
84	bneq	1f			# simply return if reserved op
85	movq 	$0x0000fe00ffffcfff,r0  # -2147483647.0
861:	ret
87
88/*
89 * long finite(x)
90 * double x;
91 */
92	.globl	_finite
93	.text
94	.align	1
95_finite:
96	.word	0x0000
97	bicw3	$0x7f,4(ap),r0		# mask off the mantissa
98	cmpw	r0,$0x8000		# to see if x is the reserved op
99	beql	1f			# if so, return FALSE (0)
100	movl	$1,r0			# else return TRUE (1)
101	ret
1021:	clrl	r0
103	ret
104
105/*
106 * double scalb(x,N)
107 * double x; double N;
108 */
109	.globl	_scalb
110	.set	ERANGE,34
111	.text
112	.align	1
113_scalb:
114	.word	0x3c
115	movq	4(ap),r0
116	bicl3	$0xffff807f,r0,r3
117	beql	ret1			# 0 or reserved operand
118	movq	12(ap),r4
119	cvtdl	r4, r2
120	cmpl	r2,$0x12c
121	bgeq	ovfl
122	cmpl	r2,$-0x12c
123	bleq	unfl
124	ashl	$7,r2,r2
125	addl2	r2,r3
126	bleq	unfl
127	cmpl	r3,$0x8000
128	bgeq	ovfl
129	addl2	r2,r0
130	ret
131ovfl:	pushl	$ERANGE
132	calls	$1,_infnan		# if it returns
133	bicw3	$0x7fff,4(ap),r2	# get the sign of input arg
134	bisw2	r2,r0			# re-attach the sign to r0/1
135	ret
136unfl:	movq	$0,r0
137ret1:	ret
138
139/*
140 * DREM(X,Y)
141 * RETURN X REM Y =X-N*Y, N=[X/Y] ROUNDED (ROUNDED TO EVEN IN THE HALF WAY CASE)
142 * DOUBLE PRECISION (VAX D format 56 bits)
143 * CODED IN VAX ASSEMBLY LANGUAGE BY K.C. NG, 4/8/85.
144 */
145	.globl	_drem
146	.set	EDOM,33
147	.text
148	.align	1
149_drem:
150	.word	0xffc
151	subl2	$12,sp
152	movq	4(ap),r0		#r0=x
153	movq	12(ap),r2		#r2=y
154	jeql	Rop			#if y=0 then generate reserved op fault
155	bicw3	$0x007f,r0,r4		#check if x is Rop
156	cmpw	r4,$0x8000
157	jeql	Ret			#if x is Rop then return Rop
158	bicl3	$0x007f,r2,r4		#check if y is Rop
159	cmpw	r4,$0x8000
160	jeql	Ret			#if y is Rop then return Rop
161	bicw2	$0x8000,r2		#y  := |y|
162	movw	$0,-4(fp)		#-4(fp) = nx := 0
163	cmpw	r2,$0x1c80		#yexp ? 57
164	bgtr	C1			#if yexp > 57 goto C1
165	addw2	$0x1c80,r2		#scale up y by 2**57
166	movw	$0x1c80,-4(fp)		#nx := 57 (exponent field)
167C1:
168	movw	-4(fp),-8(fp)		#-8(fp) = nf := nx
169	bicw3	$0x7fff,r0,-12(fp)	#-12(fp) = sign of x
170	bicw2	$0x8000,r0		#x  := |x|
171	movq	r2,r10			#y1 := y
172	bicl2	$0xffff07ff,r11		#clear the last 27 bits of y1
173loop:
174	cmpd	r0,r2			#x ? y
175	bleq	E1			#if x <= y goto E1
176 /* begin argument reduction */
177	movq	r2,r4			#t =y
178	movq	r10,r6			#t1=y1
179	bicw3	$0x807f,r0,r8		#xexp= exponent of x
180	bicw3	$0x807f,r2,r9		#yexp= exponent fo y
181	subw2	r9,r8			#xexp-yexp
182	subw2	$0x0c80,r8		#k=xexp-yexp-25(exponent bit field)
183	blss	C2			#if k<0 goto C2
184	addw2	r8,r4			#t +=k
185	addw2	r8,r6			#t1+=k, scale up t and t1
186C2:
187	divd3	r4,r0,r8		#x/t
188	cvtdl	r8,r8			#n=[x/t] truncated
189	cvtld	r8,r8			#float(n)
190	subd2	r6,r4			#t:=t-t1
191	muld2	r8,r4			#n*(t-t1)
192	muld2	r8,r6			#n*t1
193	subd2	r6,r0			#x-n*t1
194	subd2	r4,r0			#(x-n*t1)-n*(t-t1)
195	brb	loop
196E1:
197	movw	-4(fp),r6		#r6=nx
198	beql	C3			#if nx=0 goto C3
199	addw2	r6,r0			#x:=x*2**57 scale up x by nx
200	movw	$0,-4(fp)		#clear nx
201	brb	loop
202C3:
203	movq	r2,r4			#r4 = y
204	subw2	$0x80,r4		#r4 = y/2
205	cmpd	r0,r4			#x:y/2
206	blss	E2			#if x < y/2 goto E2
207	bgtr	C4			#if x > y/2 goto C4
208	cvtdl	r8,r8			#ifix(float(n))
209	blbc	r8,E2			#if the last bit is zero, goto E2
210C4:
211	subd2	r2,r0			#x-y
212E2:
213	xorw2	-12(fp),r0		#x^sign (exclusive or)
214	movw	-8(fp),r6		#r6=nf
215	bicw3	$0x807f,r0,r8		#r8=exponent of x
216	bicw2	$0x7f80,r0		#clear the exponent of x
217	subw2	r6,r8			#r8=xexp-nf
218	bgtr	C5			#if xexp-nf is positive goto C5
219	movw	$0,r8			#clear r8
220	movq	$0,r0			#x underflow to zero
221C5:
222	bisw2	r8,r0			/* put r8 into x's exponent field */
223	ret
224Rop:					#Reserved operand
225	pushl	$EDOM
226	calls	$1,_infnan		#generate reserved op fault
227	ret
228Ret:
229	movq	$0x8000,r0		#propagate reserved op
230	ret
231