flt_rounds.c revision 1.6
11.6Sandvar/* $NetBSD: flt_rounds.c,v 1.6 2022/04/08 10:17:52 andvar Exp $ */ 21.1Sbjh21 31.1Sbjh21/* 41.1Sbjh21 * Copyright (c) 1996 Mark Brinicombe 51.1Sbjh21 * All rights reserved. 61.1Sbjh21 * 71.1Sbjh21 * Redistribution and use in source and binary forms, with or without 81.1Sbjh21 * modification, are permitted provided that the following conditions 91.1Sbjh21 * are met: 101.1Sbjh21 * 1. Redistributions of source code must retain the above copyright 111.1Sbjh21 * notice, this list of conditions and the following disclaimer. 121.1Sbjh21 * 2. Redistributions in binary form must reproduce the above copyright 131.1Sbjh21 * notice, this list of conditions and the following disclaimer in the 141.1Sbjh21 * documentation and/or other materials provided with the distribution. 151.1Sbjh21 * 3. All advertising materials mentioning features or use of this software 161.1Sbjh21 * must display the following acknowledgement: 171.1Sbjh21 * This product includes software developed by Mark Brinicombe 181.1Sbjh21 * for the NetBSD Project. 191.1Sbjh21 * 4. The name of the author may not be used to endorse or promote products 201.1Sbjh21 * derived from this software without specific prior written permission 211.1Sbjh21 * 221.1Sbjh21 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 231.1Sbjh21 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 241.1Sbjh21 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 251.1Sbjh21 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 261.1Sbjh21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 271.1Sbjh21 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 281.1Sbjh21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 291.1Sbjh21 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 301.1Sbjh21 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 311.1Sbjh21 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 321.1Sbjh21 */ 331.1Sbjh21 341.2Slukem#include <sys/cdefs.h> 351.2Slukem#if defined(LIBC_SCCS) && !defined(lint) 361.6Sandvar__RCSID("$NetBSD: flt_rounds.c,v 1.6 2022/04/08 10:17:52 andvar Exp $"); 371.2Slukem#endif /* LIBC_SCCS and not lint */ 381.2Slukem 391.5Sjoerg#include "namespace.h" 401.1Sbjh21#include <sys/types.h> 411.1Sbjh21#include <ieeefp.h> 421.1Sbjh21 431.1Sbjh21static const int map[] = { 441.1Sbjh21 1, /* round to nearest */ 451.1Sbjh21 2, /* round to positive infinity */ 461.1Sbjh21 3, /* round to negative infinity */ 471.1Sbjh21 0 /* round to zero */ 481.1Sbjh21}; 491.1Sbjh21 501.1Sbjh21/* 511.1Sbjh21 * Return the current FP rounding mode 521.1Sbjh21 * 531.1Sbjh21 * Returns: 541.1Sbjh21 * 0 - round to zero 551.1Sbjh21 * 1 - round to nearest 561.6Sandvar * 2 - round to positive infinity 571.1Sbjh21 * 3 - round to negative infinity 581.1Sbjh21 * 591.1Sbjh21 * ok all we need to do is get the current FP rounding mode 601.1Sbjh21 * index our map table and return the appropriate value. 611.1Sbjh21 * 621.1Sbjh21 * HOWEVER: 631.1Sbjh21 * The ARM FPA codes the rounding mode into the actual FP instructions 641.1Sbjh21 * so there is no such thing as a global rounding mode. 651.3Swiz * The default is round to nearest if rounding is not explicitly specified. 661.1Sbjh21 * FP instructions generated by GCC will not explicitly specify a rounding 671.1Sbjh21 * mode. 681.1Sbjh21 * 691.1Sbjh21 * So the best we can do it to return the rounding mode FP instructions 701.1Sbjh21 * use if rounding is not specified which is round to nearest. 711.1Sbjh21 * 721.1Sbjh21 * This could change in the future with new floating point emulators or 731.1Sbjh21 * soft float FP libraries. 741.1Sbjh21 */ 751.1Sbjh21 761.1Sbjh21int __flt_rounds(void); 771.1Sbjh21 781.1Sbjh21int 791.4She__flt_rounds(void) 801.1Sbjh21{ 811.1Sbjh21 return(map[fpgetround()]); 821.1Sbjh21} 83