1*0a6a1f1dSLionel Sambuc /* $NetBSD: flt_rounds.c,v 1.5 2015/03/19 21:22:59 joerg Exp $ */
22fe8fb19SBen Gras
32fe8fb19SBen Gras /*
42fe8fb19SBen Gras * Copyright (c) 1996 Mark Brinicombe
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 by Mark Brinicombe
182fe8fb19SBen Gras * for the NetBSD Project.
192fe8fb19SBen Gras * 4. The name of the author may not be used to endorse or promote products
202fe8fb19SBen Gras * derived from this software without specific prior written permission
212fe8fb19SBen Gras *
222fe8fb19SBen Gras * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
232fe8fb19SBen Gras * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
242fe8fb19SBen Gras * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
252fe8fb19SBen Gras * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
262fe8fb19SBen Gras * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
272fe8fb19SBen Gras * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
282fe8fb19SBen Gras * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
292fe8fb19SBen Gras * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
302fe8fb19SBen Gras * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
312fe8fb19SBen Gras * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
322fe8fb19SBen Gras */
332fe8fb19SBen Gras
342fe8fb19SBen Gras #include <sys/cdefs.h>
352fe8fb19SBen Gras #if defined(LIBC_SCCS) && !defined(lint)
36*0a6a1f1dSLionel Sambuc __RCSID("$NetBSD: flt_rounds.c,v 1.5 2015/03/19 21:22:59 joerg Exp $");
372fe8fb19SBen Gras #endif /* LIBC_SCCS and not lint */
382fe8fb19SBen Gras
39*0a6a1f1dSLionel Sambuc #include "namespace.h"
402fe8fb19SBen Gras #include <sys/types.h>
412fe8fb19SBen Gras #include <ieeefp.h>
422fe8fb19SBen Gras
432fe8fb19SBen Gras static const int map[] = {
442fe8fb19SBen Gras 1, /* round to nearest */
452fe8fb19SBen Gras 2, /* round to positive infinity */
462fe8fb19SBen Gras 3, /* round to negative infinity */
472fe8fb19SBen Gras 0 /* round to zero */
482fe8fb19SBen Gras };
492fe8fb19SBen Gras
502fe8fb19SBen Gras /*
512fe8fb19SBen Gras * Return the current FP rounding mode
522fe8fb19SBen Gras *
532fe8fb19SBen Gras * Returns:
542fe8fb19SBen Gras * 0 - round to zero
552fe8fb19SBen Gras * 1 - round to nearest
562fe8fb19SBen Gras * 2 - round to postive infinity
572fe8fb19SBen Gras * 3 - round to negative infinity
582fe8fb19SBen Gras *
592fe8fb19SBen Gras * ok all we need to do is get the current FP rounding mode
602fe8fb19SBen Gras * index our map table and return the appropriate value.
612fe8fb19SBen Gras *
622fe8fb19SBen Gras * HOWEVER:
632fe8fb19SBen Gras * The ARM FPA codes the rounding mode into the actual FP instructions
642fe8fb19SBen Gras * so there is no such thing as a global rounding mode.
652fe8fb19SBen Gras * The default is round to nearest if rounding is not explicitly specified.
662fe8fb19SBen Gras * FP instructions generated by GCC will not explicitly specify a rounding
672fe8fb19SBen Gras * mode.
682fe8fb19SBen Gras *
692fe8fb19SBen Gras * So the best we can do it to return the rounding mode FP instructions
702fe8fb19SBen Gras * use if rounding is not specified which is round to nearest.
712fe8fb19SBen Gras *
722fe8fb19SBen Gras * This could change in the future with new floating point emulators or
732fe8fb19SBen Gras * soft float FP libraries.
742fe8fb19SBen Gras */
752fe8fb19SBen Gras
762fe8fb19SBen Gras int __flt_rounds(void);
772fe8fb19SBen Gras
782fe8fb19SBen Gras int
__flt_rounds(void)79f14fb602SLionel Sambuc __flt_rounds(void)
802fe8fb19SBen Gras {
812fe8fb19SBen Gras return(map[fpgetround()]);
822fe8fb19SBen Gras }
83