1*0a6a1f1dSLionel Sambuc /* $NetBSD: flt_rounds.c,v 1.10 2014/08/03 19:14:24 wiz Exp $ */
22fe8fb19SBen Gras
32fe8fb19SBen Gras /*
42fe8fb19SBen Gras * Copyright (c) 1995 Christopher G. Demetriou
52fe8fb19SBen Gras * All rights reserved.
62fe8fb19SBen Gras *
72fe8fb19SBen Gras * Redistribution and use in source and binary forms, with or without
82fe8fb19SBen Gras * modification, are permitted provided that the following conditions
92fe8fb19SBen Gras * are met:
102fe8fb19SBen Gras * 1. Redistributions of source code must retain the above copyright
112fe8fb19SBen Gras * notice, this list of conditions and the following disclaimer.
122fe8fb19SBen Gras * 2. Redistributions in binary form must reproduce the above copyright
132fe8fb19SBen Gras * notice, this list of conditions and the following disclaimer in the
142fe8fb19SBen Gras * documentation and/or other materials provided with the distribution.
152fe8fb19SBen Gras * 3. All advertising materials mentioning features or use of this software
162fe8fb19SBen Gras * must display the following acknowledgement:
172fe8fb19SBen Gras * This product includes software developed for the
182fe8fb19SBen Gras * NetBSD Project. See http://www.NetBSD.org/ for
192fe8fb19SBen Gras * information about NetBSD.
202fe8fb19SBen Gras * 4. The name of the author may not be used to endorse or promote products
212fe8fb19SBen Gras * derived from this software without specific prior written permission.
222fe8fb19SBen Gras *
232fe8fb19SBen Gras * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
242fe8fb19SBen Gras * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
252fe8fb19SBen Gras * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
262fe8fb19SBen Gras * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
272fe8fb19SBen Gras * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
282fe8fb19SBen Gras * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
292fe8fb19SBen Gras * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
302fe8fb19SBen Gras * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
312fe8fb19SBen Gras * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
322fe8fb19SBen Gras * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
332fe8fb19SBen Gras *
342fe8fb19SBen Gras * <<Id: LICENSE,v 1.2 2000/06/14 15:57:33 cgd Exp>>
352fe8fb19SBen Gras */
362fe8fb19SBen Gras
372fe8fb19SBen Gras #include <sys/cdefs.h>
382fe8fb19SBen Gras #if defined(LIBC_SCCS) && !defined(lint)
39*0a6a1f1dSLionel Sambuc __RCSID("$NetBSD: flt_rounds.c,v 1.10 2014/08/03 19:14:24 wiz Exp $");
402fe8fb19SBen Gras #endif /* LIBC_SCCS and not lint */
412fe8fb19SBen Gras
422fe8fb19SBen Gras #include <sys/types.h>
43e415d488SLionel Sambuc #include <machine/ieeefp.h>
442fe8fb19SBen Gras #include <machine/float.h>
452fe8fb19SBen Gras
46e415d488SLionel Sambuc /*
47*0a6a1f1dSLionel Sambuc * These come from <float.h> definition
48e415d488SLionel Sambuc */
49e415d488SLionel Sambuc #define FLT_ROUND_MAP \
50e415d488SLionel Sambuc ( (0 << (FP_RZ*2)) /* round to zero */ \
51e415d488SLionel Sambuc | (1 << (FP_RN*2)) /* round to nearest */ \
52e415d488SLionel Sambuc | (3 << (FP_RM*2)) /* round to negative infinity */ \
53e415d488SLionel Sambuc | (2 << (FP_RP*2))) /* round to positive infinity */
542fe8fb19SBen Gras
552fe8fb19SBen Gras int
__flt_rounds(void)56e415d488SLionel Sambuc __flt_rounds(void)
572fe8fb19SBen Gras {
58e415d488SLionel Sambuc union {
59e415d488SLionel Sambuc double d;
60e415d488SLionel Sambuc uint64_t u64;
61e415d488SLionel Sambuc } fpcrval;
62e415d488SLionel Sambuc uint64_t old;
632fe8fb19SBen Gras
64e415d488SLionel Sambuc __asm("excb; mf_fpcr %0; excb" : "=f" (fpcrval.d));
65e415d488SLionel Sambuc old = (fpcrval.u64 >> 58) & 3;
662fe8fb19SBen Gras
67e415d488SLionel Sambuc return (FLT_ROUND_MAP >> (old << 1)) & 3;
682fe8fb19SBen Gras }
69