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