1125920Sache/*	$NetBSD: flt_rounds.c,v 1.10 2005/12/24 23:10:08 perry Exp $	*/
253178Sobrien
353178Sobrien/*
453178Sobrien * Copyright (c) 1996 Mark Brinicombe
553178Sobrien * All rights reserved.
653178Sobrien *
753178Sobrien * Redistribution and use in source and binary forms, with or without
853178Sobrien * modification, are permitted provided that the following conditions
9125920Sache * are met:
1053178Sobrien * 1. Redistributions of source code must retain the above copyright
1153178Sobrien *    notice, this list of conditions and the following disclaimer.
1253178Sobrien * 2. Redistributions in binary form must reproduce the above copyright
1353178Sobrien *    notice, this list of conditions and the following disclaimer in the
1453178Sobrien *    documentation and/or other materials provided with the distribution.
1553178Sobrien * 3. All advertising materials mentioning features or use of this software
1653178Sobrien *    must display the following acknowledgement:
1753178Sobrien *      This product includes software developed by Mark Brinicombe
1853178Sobrien *	for the NetBSD Project.
1953178Sobrien * 4. The name of the author may not be used to endorse or promote products
2053178Sobrien *    derived from this software without specific prior written permission
2153178Sobrien *
2253178Sobrien * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
2353178Sobrien * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
2453178Sobrien * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
2553178Sobrien * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
2653178Sobrien * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
2753178Sobrien * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
2853178Sobrien * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
2953178Sobrien * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
3053178Sobrien * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
3153178Sobrien * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
3253178Sobrien */
3353178Sobrien
3453178Sobrien#include <sys/cdefs.h>
3553178Sobrien#if defined(LIBC_SCCS) && !defined(lint)
3653178Sobrien__RCSID("$NetBSD: flt_rounds.c,v 1.10 2005/12/24 23:10:08 perry Exp $");
3753178Sobrien#endif /* LIBC_SCCS and not lint */
3853178Sobrien
3953178Sobrien#include <ieeefp.h>
4053178Sobrien#include <float.h>
4153178Sobrien#include <stdint.h>
4253178Sobrien#include <powerpc/fpu.h>
4353178Sobrien
4453178Sobrienstatic const int map[] = {
4553178Sobrien	1,	/* round to nearest */
4653178Sobrien	0,	/* round to zero */
4753178Sobrien	2,	/* round to positive infinity */
4853178Sobrien	3	/* round to negative infinity */
4953178Sobrien};
5053178Sobrien
5153178Sobrienint
5253178Sobrien__flt_rounds(void)
5353178Sobrien{
5453178Sobrien#ifdef _SOFT_FLOAT
5553178Sobrien	return map[fpgetround()];
5653178Sobrien#else
5753178Sobrien	uint64_t fpscr;
5853178Sobrien
5953178Sobrien	__asm volatile("mffs %0" : "=f"(fpscr));
6053178Sobrien	return map[((uint32_t)fpscr & FPSCR_RN)];
6153178Sobrien#endif
6253178Sobrien}
6353178Sobrien