1*87561671Swiz /* $NetBSD: flt_rounds.c,v 1.10 2014/08/03 19:14:24 wiz Exp $ */
23e14f389Scgd
33e14f389Scgd /*
43e14f389Scgd * Copyright (c) 1995 Christopher G. Demetriou
53e14f389Scgd * All rights reserved.
63e14f389Scgd *
73e14f389Scgd * Redistribution and use in source and binary forms, with or without
83e14f389Scgd * modification, are permitted provided that the following conditions
93e14f389Scgd * are met:
103e14f389Scgd * 1. Redistributions of source code must retain the above copyright
113e14f389Scgd * notice, this list of conditions and the following disclaimer.
123e14f389Scgd * 2. Redistributions in binary form must reproduce the above copyright
133e14f389Scgd * notice, this list of conditions and the following disclaimer in the
143e14f389Scgd * documentation and/or other materials provided with the distribution.
153e14f389Scgd * 3. All advertising materials mentioning features or use of this software
163e14f389Scgd * must display the following acknowledgement:
17db755e7cScgd * This product includes software developed for the
1899410184Ssalo * NetBSD Project. See http://www.NetBSD.org/ for
19db755e7cScgd * information about NetBSD.
203e14f389Scgd * 4. The name of the author may not be used to endorse or promote products
21db755e7cScgd * derived from this software without specific prior written permission.
223e14f389Scgd *
233e14f389Scgd * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
243e14f389Scgd * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
253e14f389Scgd * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
263e14f389Scgd * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
273e14f389Scgd * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
283e14f389Scgd * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
293e14f389Scgd * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
303e14f389Scgd * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
313e14f389Scgd * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
323e14f389Scgd * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33db755e7cScgd *
34db755e7cScgd * <<Id: LICENSE,v 1.2 2000/06/14 15:57:33 cgd Exp>>
353e14f389Scgd */
363e14f389Scgd
3788c3eadbSlukem #include <sys/cdefs.h>
3888c3eadbSlukem #if defined(LIBC_SCCS) && !defined(lint)
39*87561671Swiz __RCSID("$NetBSD: flt_rounds.c,v 1.10 2014/08/03 19:14:24 wiz Exp $");
4088c3eadbSlukem #endif /* LIBC_SCCS and not lint */
4188c3eadbSlukem
423e14f389Scgd #include <sys/types.h>
43f3fc9a46Smatt #include <machine/ieeefp.h>
44ef909f42Sthorpej #include <machine/float.h>
453e14f389Scgd
46f3fc9a46Smatt /*
47*87561671Swiz * These come from <float.h> definition
48f3fc9a46Smatt */
49f3fc9a46Smatt #define FLT_ROUND_MAP \
50f3fc9a46Smatt ( (0 << (FP_RZ*2)) /* round to zero */ \
51f3fc9a46Smatt | (1 << (FP_RN*2)) /* round to nearest */ \
52f3fc9a46Smatt | (3 << (FP_RM*2)) /* round to negative infinity */ \
53f3fc9a46Smatt | (2 << (FP_RP*2))) /* round to positive infinity */
543e14f389Scgd
553e14f389Scgd int
__flt_rounds(void)56f3fc9a46Smatt __flt_rounds(void)
573e14f389Scgd {
58f3fc9a46Smatt union {
59f3fc9a46Smatt double d;
60f3fc9a46Smatt uint64_t u64;
61f3fc9a46Smatt } fpcrval;
62f3fc9a46Smatt uint64_t old;
633e14f389Scgd
64f3fc9a46Smatt __asm("excb; mf_fpcr %0; excb" : "=f" (fpcrval.d));
65f3fc9a46Smatt old = (fpcrval.u64 >> 58) & 3;
663e14f389Scgd
67f3fc9a46Smatt return (FLT_ROUND_MAP >> (old << 1)) & 3;
683e14f389Scgd }
69