1/* $NetBSD: ldexp.S,v 1.8 2003/08/07 16:42:15 agc 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. 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 35#include <mips/asm.h> 36 37#if defined(LIBC_SCCS) && !defined(lint) 38 ASMSTR("from: @(#)ldexp.s 8.1 (Berkeley) 6/4/93") 39 ASMSTR("$NetBSD: ldexp.S,v 1.8 2003/08/07 16:42:15 agc Exp $") 40#endif /* LIBC_SCCS and not lint */ 41 42#ifdef __ABICALLS__ 43 .abicalls 44#endif 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 */ 65LEAF(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 801: 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 861: 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 951: 96 srl ta0, v0, 16 97 bne ta0, zero, 1f 98 addu t9, 16 99 sll v0, 16 1001: 101 srl ta0, v0, 24 102 bne ta0, zero, 1f 103 addu t9, 8 104 sll v0, 8 1051: 106 srl ta0, v0, 28 107 bne ta0, zero, 1f 108 addu t9, 4 109 sll v0, 4 1101: 111 srl ta0, v0, 30 112 bne ta0, zero, 1f 113 addu t9, 2 114 sll v0, 2 1151: 116 srl ta0, v0, 31 117 bne ta0, zero, 1f 118 addu t9, 1 119/* 120 * Now shift t2,t3 the correct number of bits. 121 */ 1221: 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 1331: 134 subu v0, v0, t9 # shift fraction left < 32 bits 135 sll t2, t2, t9 136 srl ta0, t3, v0 137 or t2, t2, ta0 138 sll t3, t3, t9 1392: 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 1443: 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 1524: 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 ta0, 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, ta0 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 ta0, t3, 1 169 sll t8, t8, 1 170 addu t2, t2, ta0 171 bne t8, zero, 1f # round result to nearest 172 and t3, t3, ~1 1731: 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 1781: 179 j ra 1802: 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 ta0, t3, 1 189 sll t8, t8, 1 190 mtc1 ta0, $f1 # exponent and upper fraction 191 bne t8, zero, 1f # round result to nearest 192 and t3, t3, ~1 1931: 194 mtc1 t3, $f0 195 bge v1, zero, 1f # is result negative? 196 neg.d $f0, $f0 # negate result 1971: 198 j ra 1997: 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 2041: 205 j ra 2068: 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 2121: 213 add.d $f0, $f0 # cause overflow faults if enabled 214 j ra 2159: 216 mov.d $f0, $f12 # yes, result is just x 217 j ra 218END(ldexp) 219