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