xref: /netbsd-src/lib/libc/arch/sh3/gen/flt_rounds.c (revision 2fa7e14158d403140e8fb3c233c9e17417a69d87)
1*2fa7e141Sandvar /*	$NetBSD: flt_rounds.c,v 1.8 2022/04/08 10:17:52 andvar Exp $	*/
295d875fbSmsaitoh 
395d875fbSmsaitoh /*
495d875fbSmsaitoh  * Copyright (c) 1996 Mark Brinicombe
595d875fbSmsaitoh  * All rights reserved.
695d875fbSmsaitoh  *
795d875fbSmsaitoh  * Redistribution and use in source and binary forms, with or without
895d875fbSmsaitoh  * modification, are permitted provided that the following conditions
995d875fbSmsaitoh  * are met:
1095d875fbSmsaitoh  * 1. Redistributions of source code must retain the above copyright
1195d875fbSmsaitoh  *    notice, this list of conditions and the following disclaimer.
1295d875fbSmsaitoh  * 2. Redistributions in binary form must reproduce the above copyright
1395d875fbSmsaitoh  *    notice, this list of conditions and the following disclaimer in the
1495d875fbSmsaitoh  *    documentation and/or other materials provided with the distribution.
1595d875fbSmsaitoh  * 3. All advertising materials mentioning features or use of this software
1695d875fbSmsaitoh  *    must display the following acknowledgement:
1795d875fbSmsaitoh  *      This product includes software developed by Mark Brinicombe
1895d875fbSmsaitoh  *	for the NetBSD Project.
1995d875fbSmsaitoh  * 4. The name of the author may not be used to endorse or promote products
2095d875fbSmsaitoh  *    derived from this software without specific prior written permission
2195d875fbSmsaitoh  *
2295d875fbSmsaitoh  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
2395d875fbSmsaitoh  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
2495d875fbSmsaitoh  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
2595d875fbSmsaitoh  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
2695d875fbSmsaitoh  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
2795d875fbSmsaitoh  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
2895d875fbSmsaitoh  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
2995d875fbSmsaitoh  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
3095d875fbSmsaitoh  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
3195d875fbSmsaitoh  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
3295d875fbSmsaitoh  */
3395d875fbSmsaitoh 
3488c3eadbSlukem #include <sys/cdefs.h>
3588c3eadbSlukem #if defined(LIBC_SCCS) && !defined(lint)
36*2fa7e141Sandvar __RCSID("$NetBSD: flt_rounds.c,v 1.8 2022/04/08 10:17:52 andvar Exp $");
3788c3eadbSlukem #endif /* LIBC_SCCS and not lint */
3888c3eadbSlukem 
39b9e24006Sjoerg #include "namespace.h"
4095d875fbSmsaitoh #include <sys/types.h>
4195d875fbSmsaitoh #include <ieeefp.h>
4295d875fbSmsaitoh 
4395d875fbSmsaitoh static const int map[] = {
4495d875fbSmsaitoh 	1,	/* round to nearest */
456626e5c2Schs 	3,	/* round to positive infinity */
466626e5c2Schs 	2,	/* round to negative infinity */
4795d875fbSmsaitoh 	0	/* round to zero */
4895d875fbSmsaitoh };
4995d875fbSmsaitoh 
5095d875fbSmsaitoh /*
5195d875fbSmsaitoh  * Return the current FP rounding mode
5295d875fbSmsaitoh  *
5395d875fbSmsaitoh  * Returns:
5495d875fbSmsaitoh  *	0 - round to zero
5595d875fbSmsaitoh  *	1 - round to nearest
56*2fa7e141Sandvar  *	2 - round to positive infinity
5795d875fbSmsaitoh  *	3 - round to negative infinity
5895d875fbSmsaitoh  *
5995d875fbSmsaitoh  * ok all we need to do is get the current FP rounding mode
6095d875fbSmsaitoh  * index our map table and return the appropriate value.
6195d875fbSmsaitoh  *
6295d875fbSmsaitoh  * HOWEVER:
6395d875fbSmsaitoh  * The ARM FPA codes the rounding mode into the actual FP instructions
6495d875fbSmsaitoh  * so there is no such thing as a global rounding mode.
655d1e8b27Swiz  * The default is round to nearest if rounding is not explicitly specified.
6695d875fbSmsaitoh  * FP instructions generated by GCC will not explicitly specify a rounding
6795d875fbSmsaitoh  * mode.
6895d875fbSmsaitoh  *
6995d875fbSmsaitoh  * So the best we can do it to return the rounding mode FP instructions
7095d875fbSmsaitoh  * use if rounding is not specified which is round to nearest.
7195d875fbSmsaitoh  *
7295d875fbSmsaitoh  * This could change in the future with new floating point emulators or
7395d875fbSmsaitoh  * soft float FP libraries.
7495d875fbSmsaitoh  */
7595d875fbSmsaitoh 
7695d875fbSmsaitoh extern int __flt_rounds __P((void));
7795d875fbSmsaitoh 
7895d875fbSmsaitoh int
__flt_rounds(void)792bf75ee5She __flt_rounds(void)
8095d875fbSmsaitoh {
8195d875fbSmsaitoh 	return(map[fpgetround()]);
8295d875fbSmsaitoh }
83