Home | History | Annotate | Line # | Download | only in gen
flt_rounds.c revision 1.5
      1  1.5   simonb /*	$NetBSD: flt_rounds.c,v 1.5 2001/05/25 12:14:05 simonb Exp $	*/
      2  1.2  mycroft 
      3  1.5   simonb /*
      4  1.5   simonb  * Copyright (c) 1996 Mark Brinicombe
      5  1.5   simonb  * All rights reserved.
      6  1.5   simonb  *
      7  1.5   simonb  * Redistribution and use in source and binary forms, with or without
      8  1.5   simonb  * modification, are permitted provided that the following conditions
      9  1.5   simonb  * are met:
     10  1.5   simonb  * 1. Redistributions of source code must retain the above copyright
     11  1.5   simonb  *    notice, this list of conditions and the following disclaimer.
     12  1.5   simonb  * 2. Redistributions in binary form must reproduce the above copyright
     13  1.5   simonb  *    notice, this list of conditions and the following disclaimer in the
     14  1.5   simonb  *    documentation and/or other materials provided with the distribution.
     15  1.5   simonb  * 3. All advertising materials mentioning features or use of this software
     16  1.5   simonb  *    must display the following acknowledgement:
     17  1.5   simonb  *      This product includes software developed by Mark Brinicombe
     18  1.5   simonb  *	for the NetBSD Project.
     19  1.5   simonb  * 4. The name of the author may not be used to endorse or promote products
     20  1.5   simonb  *    derived from this software without specific prior written permission
     21  1.5   simonb  *
     22  1.5   simonb  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     23  1.5   simonb  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     24  1.5   simonb  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     25  1.5   simonb  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     26  1.5   simonb  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     27  1.5   simonb  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     28  1.5   simonb  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     29  1.5   simonb  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     30  1.5   simonb  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     31  1.5   simonb  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     32  1.5   simonb  */
     33  1.5   simonb 
     34  1.5   simonb #include <ieeefp.h>
     35  1.2  mycroft #include <float.h>
     36  1.1  thorpej 
     37  1.1  thorpej static const int map[] = {
     38  1.1  thorpej 	1,	/* round to nearest */
     39  1.1  thorpej 	0,	/* round to zero */
     40  1.1  thorpej 	2,	/* round to positive infinity */
     41  1.1  thorpej 	3	/* round to negative infinity */
     42  1.1  thorpej };
     43  1.1  thorpej 
     44  1.1  thorpej int
     45  1.1  thorpej __flt_rounds()
     46  1.1  thorpej {
     47  1.5   simonb #ifdef _SOFT_FLOAT
     48  1.5   simonb 	return map[fpgetround()];
     49  1.5   simonb #else
     50  1.1  thorpej 	double tmp;
     51  1.1  thorpej 	int x;
     52  1.1  thorpej 
     53  1.4     danw 	__asm__ __volatile("mffs %0; stfiwx %0,0,%1" : "=f"(tmp): "b"(&x));
     54  1.1  thorpej 	return map[x & 0x03];
     55  1.5   simonb #endif
     56  1.1  thorpej }
     57