1/* $NetBSD: ldexp.S,v 1.7 2002/11/10 18:10:25 thorpej 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 <mips/asm.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.7 2002/11/10 18:10:25 thorpej Exp $") 44#endif /* LIBC_SCCS and not lint */ 45 46#ifdef __ABICALLS__ 47 .abicalls 48#endif 49 50#define DEXP_INF 0x7ff 51#define DEXP_BIAS 1023 52#define DEXP_MIN -1022 53#define DEXP_MAX 1023 54#define DFRAC_BITS 52 55#define DIMPL_ONE 0x00100000 56#define DLEAD_ZEROS 31 - 20 57#define STICKYBIT 1 58#define GUARDBIT 0x80000000 59#define DSIGNAL_NAN 0x00040000 60#define DQUIET_NAN0 0x0007ffff 61#define DQUIET_NAN1 0xffffffff 62 63/* 64 * double ldexp(x, N) 65 * double x; int N; 66 * 67 * Return x * (2**N), for integer values N. 68 */ 69LEAF(ldexp) 70 mfc1 v1, $f13 # get MSW of x 71 mfc1 t3, $f12 # get LSW of x 72 sll t1, v1, 1 # get x exponent 73 srl t1, t1, 32 - 11 74 beq t1, DEXP_INF, 9f # is it a NAN or infinity? 75 beq t1, zero, 1f # zero or denormalized number? 76 addu t1, t1, a2 # scale exponent 77 sll v0, a2, 20 # position N for addition 78 bge t1, DEXP_INF, 8f # overflow? 79 addu v0, v0, v1 # multiply by (2**N) 80 ble t1, zero, 4f # underflow? 81 mtc1 v0, $f1 # save MSW of result 82 mtc1 t3, $f0 # save LSW of result 83 j ra 841: 85 sll t2, v1, 32 - 20 # get x fraction 86 srl t2, t2, 32 - 20 87 srl t0, v1, 31 # get x sign 88 bne t2, zero, 1f 89 beq t3, zero, 9f # result is zero 901: 91/* 92 * Find out how many leading zero bits are in t2,t3 and put in t9. 93 */ 94 move v0, t2 95 move t9, zero 96 bne t2, zero, 1f 97 move v0, t3 98 addu t9, 32 991: 100 srl ta0, v0, 16 101 bne ta0, zero, 1f 102 addu t9, 16 103 sll v0, 16 1041: 105 srl ta0, v0, 24 106 bne ta0, zero, 1f 107 addu t9, 8 108 sll v0, 8 1091: 110 srl ta0, v0, 28 111 bne ta0, zero, 1f 112 addu t9, 4 113 sll v0, 4 1141: 115 srl ta0, v0, 30 116 bne ta0, zero, 1f 117 addu t9, 2 118 sll v0, 2 1191: 120 srl ta0, v0, 31 121 bne ta0, zero, 1f 122 addu t9, 1 123/* 124 * Now shift t2,t3 the correct number of bits. 125 */ 1261: 127 subu t9, t9, DLEAD_ZEROS # dont count normal leading zeros 128 li t1, DEXP_MIN + DEXP_BIAS 129 subu t1, t1, t9 # adjust exponent 130 addu t1, t1, a2 # scale exponent 131 li v0, 32 132 blt t9, v0, 1f 133 subu t9, t9, v0 # shift fraction left >= 32 bits 134 sll t2, t3, t9 135 move t3, zero 136 b 2f 1371: 138 subu v0, v0, t9 # shift fraction left < 32 bits 139 sll t2, t2, t9 140 srl ta0, t3, v0 141 or t2, t2, ta0 142 sll t3, t3, t9 1432: 144 bge t1, DEXP_INF, 8f # overflow? 145 ble t1, zero, 4f # underflow? 146 sll t2, t2, 32 - 20 # clear implied one bit 147 srl t2, t2, 32 - 20 1483: 149 sll t1, t1, 31 - 11 # reposition exponent 150 sll t0, t0, 31 # reposition sign 151 or t0, t0, t1 # put result back together 152 or t0, t0, t2 153 mtc1 t0, $f1 # save MSW of result 154 mtc1 t3, $f0 # save LSW of result 155 j ra 1564: 157 li v0, 0x80000000 158 ble t1, -52, 7f # is result too small for denorm? 159 sll t2, v1, 31 - 20 # clear exponent, extract fraction 160 or t2, t2, v0 # set implied one bit 161 blt t1, -30, 2f # will all bits in t3 be shifted out? 162 srl t2, t2, 31 - 20 # shift fraction back to normal position 163 subu t1, t1, 1 164 sll ta0, t2, t1 # shift right t2,t3 based on exponent 165 srl t8, t3, t1 # save bits shifted out 166 negu t1 167 srl t3, t3, t1 168 or t3, t3, ta0 169 srl t2, t2, t1 170 bge t8, zero, 1f # does result need to be rounded? 171 addu t3, t3, 1 # round result 172 sltu ta0, t3, 1 173 sll t8, t8, 1 174 addu t2, t2, ta0 175 bne t8, zero, 1f # round result to nearest 176 and t3, t3, ~1 1771: 178 mtc1 t3, $f0 # save denormalized result (LSW) 179 mtc1 t2, $f1 # save denormalized result (MSW) 180 bge v1, zero, 1f # should result be negative? 181 neg.d $f0, $f0 # negate result 1821: 183 j ra 1842: 185 mtc1 zero, $f1 # exponent and upper fraction 186 addu t1, t1, 20 # compute amount to shift right by 187 sll t8, t2, t1 # save bits shifted out 188 negu t1 189 srl t3, t2, t1 190 bge t8, zero, 1f # does result need to be rounded? 191 addu t3, t3, 1 # round result 192 sltu ta0, t3, 1 193 sll t8, t8, 1 194 mtc1 ta0, $f1 # exponent and upper fraction 195 bne t8, zero, 1f # round result to nearest 196 and t3, t3, ~1 1971: 198 mtc1 t3, $f0 199 bge v1, zero, 1f # is result negative? 200 neg.d $f0, $f0 # negate result 2011: 202 j ra 2037: 204 mtc1 zero, $f0 # result is zero 205 mtc1 zero, $f1 206 beq t0, zero, 1f # is result positive? 207 neg.d $f0, $f0 # negate result 2081: 209 j ra 2108: 211 li t1, 0x7ff00000 # result is infinity (MSW) 212 mtc1 t1, $f1 213 mtc1 zero, $f0 # result is infinity (LSW) 214 bge v1, zero, 1f # should result be negative infinity? 215 neg.d $f0, $f0 # result is negative infinity 2161: 217 add.d $f0, $f0 # cause overflow faults if enabled 218 j ra 2199: 220 mov.d $f0, $f12 # yes, result is just x 221 j ra 222END(ldexp) 223