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