1178580Simp/* $NetBSD: ldexp.S,v 1.8 2003/08/07 16:42:15 agc Exp $ */ 2178580Simp 3178580Simp/*- 4178580Simp * Copyright (c) 1991, 1993 5178580Simp * The Regents of the University of California. All rights reserved. 6178580Simp * 7178580Simp * This code is derived from software contributed to Berkeley by 8178580Simp * Ralph Campbell. 9178580Simp * 10178580Simp * Redistribution and use in source and binary forms, with or without 11178580Simp * modification, are permitted provided that the following conditions 12178580Simp * are met: 13178580Simp * 1. Redistributions of source code must retain the above copyright 14178580Simp * notice, this list of conditions and the following disclaimer. 15178580Simp * 2. Redistributions in binary form must reproduce the above copyright 16178580Simp * notice, this list of conditions and the following disclaimer in the 17178580Simp * documentation and/or other materials provided with the distribution. 18178580Simp * 3. Neither the name of the University nor the names of its contributors 19178580Simp * may be used to endorse or promote products derived from this software 20178580Simp * without specific prior written permission. 21178580Simp * 22178580Simp * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 23178580Simp * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 24178580Simp * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 25178580Simp * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 26178580Simp * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 27178580Simp * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 28178580Simp * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 29178580Simp * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 30178580Simp * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 31178580Simp * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 32178580Simp * SUCH DAMAGE. 33178580Simp */ 34178580Simp 35178580Simp#include <machine/asm.h> 36178580Simp__FBSDID("$FreeBSD$"); 37178580Simp 38178580Simp#if defined(LIBC_SCCS) && !defined(lint) 39178580Simp ASMSTR("from: @(#)ldexp.s 8.1 (Berkeley) 6/4/93") 40178580Simp ASMSTR("$NetBSD: ldexp.S,v 1.8 2003/08/07 16:42:15 agc Exp $") 41178580Simp#endif /* LIBC_SCCS and not lint */ 42178580Simp 43178580Simp#ifdef __ABICALLS__ 44178580Simp .abicalls 45178580Simp#endif 46178580Simp 47178580Simp#define DEXP_INF 0x7ff 48178580Simp#define DEXP_BIAS 1023 49178580Simp#define DEXP_MIN -1022 50178580Simp#define DEXP_MAX 1023 51178580Simp#define DFRAC_BITS 52 52178580Simp#define DIMPL_ONE 0x00100000 53178580Simp#define DLEAD_ZEROS 31 - 20 54178580Simp#define STICKYBIT 1 55178580Simp#define GUARDBIT 0x80000000 56178580Simp#define DSIGNAL_NAN 0x00040000 57178580Simp#define DQUIET_NAN0 0x0007ffff 58178580Simp#define DQUIET_NAN1 0xffffffff 59178580Simp 60178580Simp/* 61178580Simp * double ldexp(x, N) 62178580Simp * double x; int N; 63178580Simp * 64178580Simp * Return x * (2**N), for integer values N. 65178580Simp */ 66178580SimpLEAF(ldexp) 67178580Simp mfc1 v1, $f13 # get MSW of x 68178580Simp mfc1 t3, $f12 # get LSW of x 69178580Simp sll t1, v1, 1 # get x exponent 70178580Simp srl t1, t1, 32 - 11 71178580Simp beq t1, DEXP_INF, 9f # is it a NAN or infinity? 72178580Simp beq t1, zero, 1f # zero or denormalized number? 73178580Simp addu t1, t1, a2 # scale exponent 74178580Simp sll v0, a2, 20 # position N for addition 75178580Simp bge t1, DEXP_INF, 8f # overflow? 76178580Simp addu v0, v0, v1 # multiply by (2**N) 77178580Simp ble t1, zero, 4f # underflow? 78178580Simp mtc1 v0, $f1 # save MSW of result 79178580Simp mtc1 t3, $f0 # save LSW of result 80178580Simp j ra 81178580Simp1: 82178580Simp sll t2, v1, 32 - 20 # get x fraction 83178580Simp srl t2, t2, 32 - 20 84178580Simp srl t0, v1, 31 # get x sign 85178580Simp bne t2, zero, 1f 86178580Simp beq t3, zero, 9f # result is zero 87178580Simp1: 88178580Simp/* 89178580Simp * Find out how many leading zero bits are in t2,t3 and put in t9. 90178580Simp */ 91178580Simp move v0, t2 92178580Simp move t9, zero 93178580Simp bne t2, zero, 1f 94178580Simp move v0, t3 95178580Simp addu t9, 32 96178580Simp1: 97178580Simp srl ta0, v0, 16 98178580Simp bne ta0, zero, 1f 99178580Simp addu t9, 16 100178580Simp sll v0, 16 101178580Simp1: 102178580Simp srl ta0, v0, 24 103178580Simp bne ta0, zero, 1f 104178580Simp addu t9, 8 105178580Simp sll v0, 8 106178580Simp1: 107178580Simp srl ta0, v0, 28 108178580Simp bne ta0, zero, 1f 109178580Simp addu t9, 4 110178580Simp sll v0, 4 111178580Simp1: 112178580Simp srl ta0, v0, 30 113178580Simp bne ta0, zero, 1f 114178580Simp addu t9, 2 115178580Simp sll v0, 2 116178580Simp1: 117178580Simp srl ta0, v0, 31 118178580Simp bne ta0, zero, 1f 119178580Simp addu t9, 1 120178580Simp/* 121178580Simp * Now shift t2,t3 the correct number of bits. 122178580Simp */ 123178580Simp1: 124178580Simp subu t9, t9, DLEAD_ZEROS # dont count normal leading zeros 125178580Simp li t1, DEXP_MIN + DEXP_BIAS 126178580Simp subu t1, t1, t9 # adjust exponent 127178580Simp addu t1, t1, a2 # scale exponent 128178580Simp li v0, 32 129178580Simp blt t9, v0, 1f 130178580Simp subu t9, t9, v0 # shift fraction left >= 32 bits 131178580Simp sll t2, t3, t9 132178580Simp move t3, zero 133178580Simp b 2f 134178580Simp1: 135178580Simp subu v0, v0, t9 # shift fraction left < 32 bits 136178580Simp sll t2, t2, t9 137178580Simp srl ta0, t3, v0 138178580Simp or t2, t2, ta0 139178580Simp sll t3, t3, t9 140178580Simp2: 141178580Simp bge t1, DEXP_INF, 8f # overflow? 142178580Simp ble t1, zero, 4f # underflow? 143178580Simp sll t2, t2, 32 - 20 # clear implied one bit 144178580Simp srl t2, t2, 32 - 20 145178580Simp3: 146178580Simp sll t1, t1, 31 - 11 # reposition exponent 147178580Simp sll t0, t0, 31 # reposition sign 148178580Simp or t0, t0, t1 # put result back together 149178580Simp or t0, t0, t2 150178580Simp mtc1 t0, $f1 # save MSW of result 151178580Simp mtc1 t3, $f0 # save LSW of result 152178580Simp j ra 153178580Simp4: 154178580Simp li v0, 0x80000000 155178580Simp ble t1, -52, 7f # is result too small for denorm? 156178580Simp sll t2, v1, 31 - 20 # clear exponent, extract fraction 157178580Simp or t2, t2, v0 # set implied one bit 158178580Simp blt t1, -30, 2f # will all bits in t3 be shifted out? 159178580Simp srl t2, t2, 31 - 20 # shift fraction back to normal position 160178580Simp subu t1, t1, 1 161178580Simp sll ta0, t2, t1 # shift right t2,t3 based on exponent 162178580Simp srl t8, t3, t1 # save bits shifted out 163178580Simp negu t1 164178580Simp srl t3, t3, t1 165178580Simp or t3, t3, ta0 166178580Simp srl t2, t2, t1 167178580Simp bge t8, zero, 1f # does result need to be rounded? 168178580Simp addu t3, t3, 1 # round result 169178580Simp sltu ta0, t3, 1 170178580Simp sll t8, t8, 1 171178580Simp addu t2, t2, ta0 172178580Simp bne t8, zero, 1f # round result to nearest 173178580Simp and t3, t3, ~1 174178580Simp1: 175178580Simp mtc1 t3, $f0 # save denormalized result (LSW) 176178580Simp mtc1 t2, $f1 # save denormalized result (MSW) 177178580Simp bge v1, zero, 1f # should result be negative? 178178580Simp neg.d $f0, $f0 # negate result 179178580Simp1: 180178580Simp j ra 181178580Simp2: 182178580Simp mtc1 zero, $f1 # exponent and upper fraction 183178580Simp addu t1, t1, 20 # compute amount to shift right by 184178580Simp sll t8, t2, t1 # save bits shifted out 185178580Simp negu t1 186178580Simp srl t3, t2, t1 187178580Simp bge t8, zero, 1f # does result need to be rounded? 188178580Simp addu t3, t3, 1 # round result 189178580Simp sltu ta0, t3, 1 190178580Simp sll t8, t8, 1 191178580Simp mtc1 ta0, $f1 # exponent and upper fraction 192178580Simp bne t8, zero, 1f # round result to nearest 193178580Simp and t3, t3, ~1 194178580Simp1: 195178580Simp mtc1 t3, $f0 196178580Simp bge v1, zero, 1f # is result negative? 197178580Simp neg.d $f0, $f0 # negate result 198178580Simp1: 199178580Simp j ra 200178580Simp7: 201178580Simp mtc1 zero, $f0 # result is zero 202178580Simp mtc1 zero, $f1 203178580Simp beq t0, zero, 1f # is result positive? 204178580Simp neg.d $f0, $f0 # negate result 205178580Simp1: 206178580Simp j ra 207178580Simp8: 208178580Simp li t1, 0x7ff00000 # result is infinity (MSW) 209178580Simp mtc1 t1, $f1 210178580Simp mtc1 zero, $f0 # result is infinity (LSW) 211178580Simp bge v1, zero, 1f # should result be negative infinity? 212178580Simp neg.d $f0, $f0 # result is negative infinity 213178580Simp1: 214178580Simp add.d $f0, $f0 # cause overflow faults if enabled 215178580Simp j ra 216178580Simp9: 217178580Simp mov.d $f0, $f12 # yes, result is just x 218178580Simp j ra 219178580SimpEND(ldexp) 220