ldexp.S revision 1.4 1 /* $NetBSD: ldexp.S,v 1.4 1996/09/16 18:10:58 jonathan Exp $ */
2
3 /*-
4 * Copyright (c) 1991, 1993
5 * The Regents of the University of California. All rights reserved.
6 *
7 * This code is derived from software contributed to Berkeley by
8 * Ralph Campbell.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the University of
21 * California, Berkeley and its contributors.
22 * 4. Neither the name of the University nor the names of its contributors
23 * may be used to endorse or promote products derived from this software
24 * without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * SUCH DAMAGE.
37 */
38
39 #include <machine/machAsmDefs.h>
40
41 #if defined(LIBC_SCCS) && !defined(lint)
42 ASMSTR("from: @(#)ldexp.s 8.1 (Berkeley) 6/4/93")
43 ASMSTR("$NetBSD: ldexp.S,v 1.4 1996/09/16 18:10:58 jonathan Exp $")
44 #endif /* LIBC_SCCS and not lint */
45
46 #define DEXP_INF 0x7ff
47 #define DEXP_BIAS 1023
48 #define DEXP_MIN -1022
49 #define DEXP_MAX 1023
50 #define DFRAC_BITS 52
51 #define DIMPL_ONE 0x00100000
52 #define DLEAD_ZEROS 31 - 20
53 #define STICKYBIT 1
54 #define GUARDBIT 0x80000000
55 #define DSIGNAL_NAN 0x00040000
56 #define DQUIET_NAN0 0x0007ffff
57 #define DQUIET_NAN1 0xffffffff
58
59 /*
60 * double ldexp(x, N)
61 * double x; int N;
62 *
63 * Return x * (2**N), for integer values N.
64 */
65 LEAF(ldexp)
66 mfc1 v1, $f13 # get MSW of x
67 mfc1 t3, $f12 # get LSW of x
68 sll t1, v1, 1 # get x exponent
69 srl t1, t1, 32 - 11
70 beq t1, DEXP_INF, 9f # is it a NAN or infinity?
71 beq t1, zero, 1f # zero or denormalized number?
72 addu t1, t1, a2 # scale exponent
73 sll v0, a2, 20 # position N for addition
74 bge t1, DEXP_INF, 8f # overflow?
75 addu v0, v0, v1 # multiply by (2**N)
76 ble t1, zero, 4f # underflow?
77 mtc1 v0, $f1 # save MSW of result
78 mtc1 t3, $f0 # save LSW of result
79 j ra
80 1:
81 sll t2, v1, 32 - 20 # get x fraction
82 srl t2, t2, 32 - 20
83 srl t0, v1, 31 # get x sign
84 bne t2, zero, 1f
85 beq t3, zero, 9f # result is zero
86 1:
87 /*
88 * Find out how many leading zero bits are in t2,t3 and put in t9.
89 */
90 move v0, t2
91 move t9, zero
92 bne t2, zero, 1f
93 move v0, t3
94 addu t9, 32
95 1:
96 srl t4, v0, 16
97 bne t4, zero, 1f
98 addu t9, 16
99 sll v0, 16
100 1:
101 srl t4, v0, 24
102 bne t4, zero, 1f
103 addu t9, 8
104 sll v0, 8
105 1:
106 srl t4, v0, 28
107 bne t4, zero, 1f
108 addu t9, 4
109 sll v0, 4
110 1:
111 srl t4, v0, 30
112 bne t4, zero, 1f
113 addu t9, 2
114 sll v0, 2
115 1:
116 srl t4, v0, 31
117 bne t4, zero, 1f
118 addu t9, 1
119 /*
120 * Now shift t2,t3 the correct number of bits.
121 */
122 1:
123 subu t9, t9, DLEAD_ZEROS # dont count normal leading zeros
124 li t1, DEXP_MIN + DEXP_BIAS
125 subu t1, t1, t9 # adjust exponent
126 addu t1, t1, a2 # scale exponent
127 li v0, 32
128 blt t9, v0, 1f
129 subu t9, t9, v0 # shift fraction left >= 32 bits
130 sll t2, t3, t9
131 move t3, zero
132 b 2f
133 1:
134 subu v0, v0, t9 # shift fraction left < 32 bits
135 sll t2, t2, t9
136 srl t4, t3, v0
137 or t2, t2, t4
138 sll t3, t3, t9
139 2:
140 bge t1, DEXP_INF, 8f # overflow?
141 ble t1, zero, 4f # underflow?
142 sll t2, t2, 32 - 20 # clear implied one bit
143 srl t2, t2, 32 - 20
144 3:
145 sll t1, t1, 31 - 11 # reposition exponent
146 sll t0, t0, 31 # reposition sign
147 or t0, t0, t1 # put result back together
148 or t0, t0, t2
149 mtc1 t0, $f1 # save MSW of result
150 mtc1 t3, $f0 # save LSW of result
151 j ra
152 4:
153 li v0, 0x80000000
154 ble t1, -52, 7f # is result too small for denorm?
155 sll t2, v1, 31 - 20 # clear exponent, extract fraction
156 or t2, t2, v0 # set implied one bit
157 blt t1, -30, 2f # will all bits in t3 be shifted out?
158 srl t2, t2, 31 - 20 # shift fraction back to normal position
159 subu t1, t1, 1
160 sll t4, t2, t1 # shift right t2,t3 based on exponent
161 srl t8, t3, t1 # save bits shifted out
162 negu t1
163 srl t3, t3, t1
164 or t3, t3, t4
165 srl t2, t2, t1
166 bge t8, zero, 1f # does result need to be rounded?
167 addu t3, t3, 1 # round result
168 sltu t4, t3, 1
169 sll t8, t8, 1
170 addu t2, t2, t4
171 bne t8, zero, 1f # round result to nearest
172 and t3, t3, ~1
173 1:
174 mtc1 t3, $f0 # save denormalized result (LSW)
175 mtc1 t2, $f1 # save denormalized result (MSW)
176 bge v1, zero, 1f # should result be negative?
177 neg.d $f0, $f0 # negate result
178 1:
179 j ra
180 2:
181 mtc1 zero, $f1 # exponent and upper fraction
182 addu t1, t1, 20 # compute amount to shift right by
183 sll t8, t2, t1 # save bits shifted out
184 negu t1
185 srl t3, t2, t1
186 bge t8, zero, 1f # does result need to be rounded?
187 addu t3, t3, 1 # round result
188 sltu t4, t3, 1
189 sll t8, t8, 1
190 mtc1 t4, $f1 # exponent and upper fraction
191 bne t8, zero, 1f # round result to nearest
192 and t3, t3, ~1
193 1:
194 mtc1 t3, $f0
195 bge v1, zero, 1f # is result negative?
196 neg.d $f0, $f0 # negate result
197 1:
198 j ra
199 7:
200 mtc1 zero, $f0 # result is zero
201 mtc1 zero, $f1
202 beq t0, zero, 1f # is result positive?
203 neg.d $f0, $f0 # negate result
204 1:
205 j ra
206 8:
207 li t1, 0x7ff00000 # result is infinity (MSW)
208 mtc1 t1, $f1
209 mtc1 zero, $f0 # result is infinity (LSW)
210 bge v1, zero, 1f # should result be negative infinity?
211 neg.d $f0, $f0 # result is negative infinity
212 1:
213 add.d $f0, $f0 # cause overflow faults if enabled
214 j ra
215 9:
216 mov.d $f0, $f12 # yes, result is just x
217 j ra
218 END(ldexp)
219