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