* Copyright (c) 1991, 1993
* The Regents of the University of California. All rights reserved.
* This code is derived from software contributed to Berkeley by
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by the University of
* California, Berkeley and its contributors.
* 4. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
#include <machine/machAsmDefs.h>
#if defined(LIBC_SCCS) && !defined(lint)
ASMSTR("@(#)ldexp.s 8.1 (Berkeley) 6/4/93")
#endif /* LIBC_SCCS and not lint */
#define DIMPL_ONE 0x00100000
#define DLEAD_ZEROS 31 - 20
#define GUARDBIT 0x80000000
#define DSIGNAL_NAN 0x00040000
#define DQUIET_NAN0 0x0007ffff
#define DQUIET_NAN1 0xffffffff
* Return x * (2**N), for integer values N.
mfc1 v1, $f13 # get MSW of x
mfc1 t3, $f12 # get LSW of x
sll t1, v1, 1 # get x exponent
beq t1, DEXP_INF, 9f # is it a NAN or infinity?
beq t1, zero, 1f # zero or denormalized number?
addu t1, t1, a2 # scale exponent
sll v0, a2, 20 # position N for addition
bge t1, DEXP_INF, 8f # overflow?
addu v0, v0, v1 # multiply by (2**N)
ble t1, zero, 4f # underflow?
mtc1 v0, $f1 # save MSW of result
mtc1 t3, $f0 # save LSW of result
sll t2, v1, 32 - 20 # get x fraction
srl t0, v1, 31 # get x sign
beq t3, zero, 9f # result is zero
* Find out how many leading zero bits are in t2,t3 and put in t9.
* Now shift t2,t3 the correct number of bits.
subu t9, t9, DLEAD_ZEROS # dont count normal leading zeros
li t1, DEXP_MIN + DEXP_BIAS
subu t1, t1, t9 # adjust exponent
addu t1, t1, a2 # scale exponent
subu t9, t9, v0 # shift fraction left >= 32 bits
subu v0, v0, t9 # shift fraction left < 32 bits
bge t1, DEXP_INF, 8f # overflow?
ble t1, zero, 4f # underflow?
sll t2, t2, 32 - 20 # clear implied one bit
sll t1, t1, 31 - 11 # reposition exponent
sll t0, t0, 31 # reposition sign
or t0, t0, t1 # put result back together
mtc1 t0, $f1 # save MSW of result
mtc1 t3, $f0 # save LSW of result
ble t1, -52, 7f # is result too small for denorm?
sll t2, v1, 31 - 20 # clear exponent, extract fraction
or t2, t2, v0 # set implied one bit
blt t1, -30, 2f # will all bits in t3 be shifted out?
srl t2, t2, 31 - 20 # shift fraction back to normal position
sll t4, t2, t1 # shift right t2,t3 based on exponent
srl t8, t3, t1 # save bits shifted out
bge t8, zero, 1f # does result need to be rounded?
addu t3, t3, 1 # round result
bne t8, zero, 1f # round result to nearest
mtc1 t3, $f0 # save denormalized result (LSW)
mtc1 t2, $f1 # save denormalized result (MSW)
bge v1, zero, 1f # should result be negative?
neg.d $f0, $f0 # negate result
mtc1 zero, $f1 # exponent and upper fraction
addu t1, t1, 20 # compute amount to shift right by
sll t8, t2, t1 # save bits shifted out
bge t8, zero, 1f # does result need to be rounded?
addu t3, t3, 1 # round result
mtc1 t4, $f1 # exponent and upper fraction
bne t8, zero, 1f # round result to nearest
bge v1, zero, 1f # is result negative?
neg.d $f0, $f0 # negate result
mtc1 zero, $f0 # result is zero
beq t0, zero, 1f # is result positive?
neg.d $f0, $f0 # negate result
li t1, 0x7ff00000 # result is infinity (MSW)
mtc1 zero, $f0 # result is infinity (LSW)
bge v1, zero, 1f # should result be negative infinity?
neg.d $f0, $f0 # result is negative infinity
add.d $f0, $f0 # cause overflow faults if enabled
mov.d $f0, $f12 # yes, result is just x