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