Home | History | Annotate | Line # | Download | only in gen
      1  1.5  joerg /*	$NetBSD: modf.S,v 1.5 2013/09/12 15:36:16 joerg Exp $	*/
      2  1.1    eeh 
      3  1.1    eeh /*
      4  1.1    eeh  * Copyright (c) 1992, 1993
      5  1.1    eeh  *	The Regents of the University of California.  All rights reserved.
      6  1.1    eeh  *
      7  1.1    eeh  * This software was developed by the Computer Systems Engineering group
      8  1.1    eeh  * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
      9  1.1    eeh  * contributed to Berkeley.
     10  1.1    eeh  *
     11  1.1    eeh  * Redistribution and use in source and binary forms, with or without
     12  1.1    eeh  * modification, are permitted provided that the following conditions
     13  1.1    eeh  * are met:
     14  1.1    eeh  * 1. Redistributions of source code must retain the above copyright
     15  1.1    eeh  *    notice, this list of conditions and the following disclaimer.
     16  1.1    eeh  * 2. Redistributions in binary form must reproduce the above copyright
     17  1.1    eeh  *    notice, this list of conditions and the following disclaimer in the
     18  1.1    eeh  *    documentation and/or other materials provided with the distribution.
     19  1.4    agc  * 3. Neither the name of the University nor the names of its contributors
     20  1.1    eeh  *    may be used to endorse or promote products derived from this software
     21  1.1    eeh  *    without specific prior written permission.
     22  1.1    eeh  *
     23  1.1    eeh  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     24  1.1    eeh  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     25  1.1    eeh  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     26  1.1    eeh  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     27  1.1    eeh  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     28  1.1    eeh  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     29  1.1    eeh  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     30  1.1    eeh  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     31  1.1    eeh  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     32  1.1    eeh  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     33  1.1    eeh  * SUCH DAMAGE.
     34  1.1    eeh  *
     35  1.1    eeh  * from: Header: modf.s,v 1.3 92/06/20 00:00:54 torek Exp
     36  1.1    eeh  */
     37  1.1    eeh 
     38  1.1    eeh #include <machine/asm.h>
     39  1.1    eeh #if defined(LIBC_SCCS) && !defined(lint)
     40  1.1    eeh #if 0
     41  1.1    eeh 	.asciz "@(#)modf.s	8.1 (Berkeley) 6/4/93"
     42  1.1    eeh #else
     43  1.5  joerg 	RCSID("$NetBSD: modf.S,v 1.5 2013/09/12 15:36:16 joerg Exp $")
     44  1.1    eeh #endif
     45  1.1    eeh #endif /* LIBC_SCCS and not lint */
     46  1.1    eeh 
     47  1.1    eeh #include <machine/fsr.h>
     48  1.1    eeh 
     49  1.1    eeh /*
     50  1.1    eeh  * double modf(double val, double *iptr)
     51  1.1    eeh  *
     52  1.1    eeh  * Returns the fractional part of `val', storing the integer part of
     53  1.1    eeh  * `val' in *iptr.  Both *iptr and the return value have the same sign
     54  1.1    eeh  * as `val'.
     55  1.1    eeh  *
     56  1.1    eeh  * Method:
     57  1.1    eeh  *
     58  1.1    eeh  * We use the fpu's normalization hardware to compute the integer portion
     59  1.1    eeh  * of the double precision argument.  Sun IEEE double precision numbers
     60  1.1    eeh  * have 52 bits of mantissa, 11 bits of exponent, and one bit of sign,
     61  1.1    eeh  * with the sign occupying bit 31 of word 0, and the exponent bits 30:20
     62  1.1    eeh  * of word 0.  Thus, values >= 2^52 are by definition integers.
     63  1.1    eeh  *
     64  1.1    eeh  * If we take a value that is in the range [+0..2^52) and add 2^52, all
     65  1.1    eeh  * of the fractional bits fall out and all of the integer bits are summed
     66  1.1    eeh  * with 2^52.  If we then subtract 2^52, we get those integer bits back.
     67  1.1    eeh  * This must be done with rounding set to `towards 0' or `towards -inf'.
     68  1.1    eeh  * `Toward -inf' fails when the value is 0 (we get -0 back)....
     69  1.1    eeh  *
     70  1.1    eeh  * Note that this method will work anywhere, but is machine dependent in
     71  1.1    eeh  * various aspects.
     72  1.1    eeh  *
     73  1.1    eeh  * Stack usage:
     74  1.2    eeh  *	4@[%fp + BIAS - 4]	saved %fsr
     75  1.2    eeh  *	4@[%fp + BIAS - 8]	new %fsr with rounding set to `towards 0'
     76  1.2    eeh  *	8@[%fp + BIAS - 16]	space for moving between %i and %f registers
     77  1.1    eeh  * Register usage:
     78  1.3    eeh  *	%f0:f1		double val;
     79  1.1    eeh  *	%l0		scratch
     80  1.1    eeh  *	%l1		sign bit (0x80000000)
     81  1.3    eeh  *	%i1		double *iptr;
     82  1.1    eeh  *	%f2:f3		`magic number' 2^52, in fpu registers
     83  1.1    eeh  *	%f4:f5		double v, in fpu registers
     84  1.3    eeh  *	%f6:f7		double temp.
     85  1.1    eeh  */
     86  1.1    eeh 
     87  1.1    eeh 	.align	8
     88  1.1    eeh Lmagic:
     89  1.1    eeh 	.word	0x43300000	! sign = 0, exponent = 52 + 1023, mantissa = 0
     90  1.1    eeh 	.word	0		! (i.e., .double 0r4503599627370496e+00)
     91  1.1    eeh 
     92  1.1    eeh L0:
     93  1.1    eeh 	.word	0		! 0.0
     94  1.1    eeh 	.word	0
     95  1.1    eeh 
     96  1.1    eeh ENTRY(modf)
     97  1.2    eeh 	save	%sp, -CC64FSZ-16, %sp
     98  1.1    eeh 
     99  1.1    eeh 	/*
    100  1.3    eeh 	 * First, compute v = abs(val)
    101  1.3    eeh 	 */
    102  1.3    eeh 	fabsd	%f0, %f4		! %f4:f5 = v
    103  1.3    eeh 	fcmped	%fcc1, %f0, %f4		! %fcc1 = (val == abs(val))
    104  1.5  joerg #ifdef __PIC__
    105  1.1    eeh 	PICCY_SET(Lmagic, %l0, %o7)
    106  1.1    eeh 	ldd	[%l0], %f2
    107  1.1    eeh #else
    108  1.1    eeh 	sethi	%hi(Lmagic), %l0
    109  1.1    eeh 	ldd	[%l0 + %lo(Lmagic)], %f2
    110  1.1    eeh #endif
    111  1.1    eeh 
    112  1.1    eeh 	/*
    113  1.1    eeh 	 * Is %f4:f5 >= %f2:f3 ?  If so, it is all integer bits.
    114  1.1    eeh 	 * It is probably less, though.
    115  1.1    eeh 	 */
    116  1.1    eeh 	fcmped	%f4, %f2
    117  1.1    eeh 	fbuge	Lbig			! if >= (or unordered), go out
    118  1.1    eeh 	nop
    119  1.1    eeh 
    120  1.1    eeh 	/*
    121  1.1    eeh 	 * v < 2^52, so add 2^52, then subtract 2^52, but do it all
    122  1.1    eeh 	 * with rounding set towards zero.  We leave any enabled
    123  1.1    eeh 	 * traps enabled, but change the rounding mode.  This might
    124  1.1    eeh 	 * not be so good.  Oh well....
    125  1.1    eeh 	 */
    126  1.3    eeh 	st	%fsr, [%fp + BIAS - 4]	! %l5 = current FSR mode
    127  1.1    eeh 	set	FSR_RD, %l3		! %l3 = rounding direction mask
    128  1.2    eeh 	ld	[%fp + BIAS - 4], %l5
    129  1.1    eeh 	set	FSR_RD_RZ << FSR_RD_SHIFT, %l4
    130  1.1    eeh 	andn	%l5, %l3, %l6
    131  1.1    eeh 	or	%l6, %l4, %l6		! round towards zero, please
    132  1.1    eeh 	and	%l5, %l3, %l5		! save original rounding mode
    133  1.2    eeh 	st	%l6, [%fp + BIAS - 8]
    134  1.2    eeh 	ld	[%fp + BIAS - 8], %fsr
    135  1.1    eeh 
    136  1.1    eeh 	faddd	%f4, %f2, %f4		! %f4:f5 += 2^52
    137  1.1    eeh 	fsubd	%f4, %f2, %f4		! %f4:f5 -= 2^52
    138  1.1    eeh 
    139  1.1    eeh 	/*
    140  1.1    eeh 	 * Restore %fsr, but leave exceptions accrued.
    141  1.1    eeh 	 */
    142  1.2    eeh 	st	%fsr, [%fp + BIAS - 4]
    143  1.2    eeh 	ld	[%fp + BIAS - 4], %l6
    144  1.1    eeh 	andn	%l6, %l3, %l6		! %l6 = %fsr & ~FSR_RD;
    145  1.1    eeh 	or	%l5, %l6, %l5		! %l5 |= %l6;
    146  1.2    eeh 	st	%l5, [%fp + BIAS - 4]
    147  1.3    eeh 	ld	[%fp + BIAS - 4], %fsr	! restore %fsr, leaving accrued stuff
    148  1.1    eeh 
    149  1.1    eeh 	/*
    150  1.1    eeh 	 * Now insert the original sign in %f4:f5.
    151  1.3    eeh 	 * %fcc1 should still have the reults of (val == abs(val))
    152  1.3    eeh 	 * from above, so we use a conditional move on %fcc1 to:
    153  1.3    eeh 	 *
    154  1.3    eeh 	 *	%f4 = (val == abs(val)) ? %f4 : -%f4
    155  1.3    eeh 	 *
    156  1.1    eeh 	 */
    157  1.3    eeh 	fnegd	%f4, %f6
    158  1.3    eeh 	fmovdnz	%fcc1, %f6, %f4
    159  1.1    eeh 1:
    160  1.1    eeh 
    161  1.1    eeh 	/*
    162  1.1    eeh 	 * The value in %f4:f5 is now the integer portion of the original
    163  1.3    eeh 	 * argument.  We need to store this in *ival (%i1), subtract it
    164  1.3    eeh 	 * from the original value argument (%d0), and return the result.
    165  1.1    eeh 	 */
    166  1.3    eeh 	std	%f4, [%i1]		! *ival = %f4:f5;
    167  1.1    eeh 	fsubd	%f0, %f4, %f0		! %f0:f1 -= %f4:f5;
    168  1.1    eeh 	ret
    169  1.1    eeh 	restore
    170  1.1    eeh 
    171  1.1    eeh Lbig:
    172  1.1    eeh 	/*
    173  1.1    eeh 	 * We get here if the original comparison of %f4:f5 (v) to
    174  1.1    eeh 	 * %f2:f3 (2^52) came out `greater or unordered'.  In this
    175  1.1    eeh 	 * case the integer part is the original value, and the
    176  1.1    eeh 	 * fractional part is 0.
    177  1.1    eeh 	 */
    178  1.5  joerg #ifdef __PIC__
    179  1.1    eeh 	PICCY_SET(L0, %l0, %o7)
    180  1.3    eeh 	std	%f0, [%i1]		! *ival = val;
    181  1.1    eeh 	ldd	[%l0], %f0		! return 0.0;
    182  1.1    eeh #else
    183  1.1    eeh 	sethi	%hi(L0), %l0
    184  1.3    eeh 	std	%f0, [%i1]		! *ival = val;
    185  1.1    eeh 	ldd	[%l0 + %lo(L0)], %f0	! return 0.0;
    186  1.1    eeh #endif
    187  1.1    eeh 	ret
    188  1.1    eeh 	restore
    189  1.3    eeh 
    190