xref: /openbsd-src/lib/libc/arch/arm/gen/flt_rounds.c (revision 2c53affbcc0119d6480b86c18f2790523b6a0aad)
1*2c53affbSjmc /*	$OpenBSD: flt_rounds.c,v 1.6 2022/12/27 17:10:05 jmc Exp $	*/
2d987040fSdrahn /*	$NetBSD: flt_rounds.c,v 1.1 2000/12/29 20:13:48 bjh21 Exp $	*/
3d987040fSdrahn 
4d987040fSdrahn /*
5d987040fSdrahn  * Copyright (c) 1996 Mark Brinicombe
6d987040fSdrahn  * All rights reserved.
7d987040fSdrahn  *
8d987040fSdrahn  * Redistribution and use in source and binary forms, with or without
9d987040fSdrahn  * modification, are permitted provided that the following conditions
10d987040fSdrahn  * are met:
11d987040fSdrahn  * 1. Redistributions of source code must retain the above copyright
12d987040fSdrahn  *    notice, this list of conditions and the following disclaimer.
13d987040fSdrahn  * 2. Redistributions in binary form must reproduce the above copyright
14d987040fSdrahn  *    notice, this list of conditions and the following disclaimer in the
15d987040fSdrahn  *    documentation and/or other materials provided with the distribution.
16d987040fSdrahn  * 3. All advertising materials mentioning features or use of this software
17d987040fSdrahn  *    must display the following acknowledgement:
18d987040fSdrahn  *      This product includes software developed by Mark Brinicombe
19d987040fSdrahn  *	for the NetBSD Project.
20d987040fSdrahn  * 4. The name of the author may not be used to endorse or promote products
21d987040fSdrahn  *    derived from this software without specific prior written permission
22d987040fSdrahn  *
23d987040fSdrahn  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
24d987040fSdrahn  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
25d987040fSdrahn  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
26d987040fSdrahn  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
27d987040fSdrahn  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
28d987040fSdrahn  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29d987040fSdrahn  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30d987040fSdrahn  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31d987040fSdrahn  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
32d987040fSdrahn  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33d987040fSdrahn  */
34d987040fSdrahn 
35d987040fSdrahn #include <sys/types.h>
36d987040fSdrahn #include <ieeefp.h>
37a2104120Sguenther #include <float.h>
38d987040fSdrahn 
39d987040fSdrahn static const int map[] = {
40d987040fSdrahn 	1,	/* round to nearest */
41d987040fSdrahn 	2,	/* round to positive infinity */
42d987040fSdrahn 	3,	/* round to negative infinity */
43d987040fSdrahn 	0	/* round to zero */
44d987040fSdrahn };
45d987040fSdrahn 
46d987040fSdrahn /*
47d987040fSdrahn  * Return the current FP rounding mode
48d987040fSdrahn  *
49d987040fSdrahn  * Returns:
50d987040fSdrahn  *	0 - round to zero
51d987040fSdrahn  *	1 - round to nearest
52*2c53affbSjmc  *	2 - round to positive infinity
53d987040fSdrahn  *	3 - round to negative infinity
54d987040fSdrahn  *
55d987040fSdrahn  * ok all we need to do is get the current FP rounding mode
56d987040fSdrahn  * index our map table and return the appropriate value.
57d987040fSdrahn  *
58d987040fSdrahn  * HOWEVER:
59d987040fSdrahn  * The ARM FPA codes the rounding mode into the actual FP instructions
60d987040fSdrahn  * so there is no such thing as a global rounding mode.
61596f05b7Sjmc  * The default is round to nearest if rounding is not explicitly specified.
62d987040fSdrahn  * FP instructions generated by GCC will not explicitly specify a rounding
63d987040fSdrahn  * mode.
64d987040fSdrahn  *
65d987040fSdrahn  * So the best we can do it to return the rounding mode FP instructions
66d987040fSdrahn  * use if rounding is not specified which is round to nearest.
67d987040fSdrahn  *
68d987040fSdrahn  * This could change in the future with new floating point emulators or
69d987040fSdrahn  * soft float FP libraries.
70d987040fSdrahn  */
71d987040fSdrahn 
72d987040fSdrahn int
__flt_rounds()73d987040fSdrahn __flt_rounds()
74d987040fSdrahn {
75d987040fSdrahn 	return(map[fpgetround()]);
76d987040fSdrahn }
77af3276d5Sguenther DEF_STRONG(__flt_rounds);
78