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