flt_rounds.c revision 1.8 1 1.8 lukem /* $NetBSD: flt_rounds.c,v 1.8 2005/06/12 05:21:26 lukem 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.8 lukem __RCSID("$NetBSD: flt_rounds.c,v 1.8 2005/06/12 05:21:26 lukem Exp $");
37 1.8 lukem #endif /* LIBC_SCCS and not lint */
38 1.8 lukem
39 1.5 simonb #include <ieeefp.h>
40 1.2 mycroft #include <float.h>
41 1.6 kleink #include <stdint.h>
42 1.7 matt #include <powerpc/fpu.h>
43 1.1 thorpej
44 1.1 thorpej static const int map[] = {
45 1.1 thorpej 1, /* round to nearest */
46 1.1 thorpej 0, /* round to zero */
47 1.1 thorpej 2, /* round to positive infinity */
48 1.1 thorpej 3 /* round to negative infinity */
49 1.1 thorpej };
50 1.1 thorpej
51 1.1 thorpej int
52 1.7 matt __flt_rounds(void)
53 1.1 thorpej {
54 1.5 simonb #ifdef _SOFT_FLOAT
55 1.5 simonb return map[fpgetround()];
56 1.5 simonb #else
57 1.6 kleink uint64_t fpscr;
58 1.1 thorpej
59 1.6 kleink __asm__ __volatile("mffs %0" : "=f"(fpscr));
60 1.7 matt return map[((uint32_t)fpscr & FPSCR_RN)];
61 1.5 simonb #endif
62 1.1 thorpej }
63