xref: /freebsd/lib/libc/aarch64/gen/flt_rounds.c (revision 559a218c9b257775fb249b67945fe4a05b7a6b9f)
1*d422e6f9SAndrew Turner /*-
2*d422e6f9SAndrew Turner  * Copyright (c) 2012 Ian Lepore <freebsd@damnhippie.dyndns.org>
3*d422e6f9SAndrew Turner  * All rights reserved.
4*d422e6f9SAndrew Turner  *
5*d422e6f9SAndrew Turner  * Redistribution and use in source and binary forms, with or without
6*d422e6f9SAndrew Turner  * modification, are permitted provided that the following conditions
7*d422e6f9SAndrew Turner  * are met:
8*d422e6f9SAndrew Turner  * 1. Redistributions of source code must retain the above copyright
9*d422e6f9SAndrew Turner  *    notice, this list of conditions and the following disclaimer.
10*d422e6f9SAndrew Turner  * 2. Redistributions in binary form must reproduce the above copyright
11*d422e6f9SAndrew Turner  *    notice, this list of conditions and the following disclaimer in the
12*d422e6f9SAndrew Turner  *    documentation and/or other materials provided with the distribution.
13*d422e6f9SAndrew Turner  *
14*d422e6f9SAndrew Turner  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15*d422e6f9SAndrew Turner  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16*d422e6f9SAndrew Turner  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17*d422e6f9SAndrew Turner  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18*d422e6f9SAndrew Turner  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19*d422e6f9SAndrew Turner  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20*d422e6f9SAndrew Turner  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21*d422e6f9SAndrew Turner  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22*d422e6f9SAndrew Turner  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23*d422e6f9SAndrew Turner  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24*d422e6f9SAndrew Turner  * SUCH DAMAGE.
25*d422e6f9SAndrew Turner  */
26*d422e6f9SAndrew Turner 
27*d422e6f9SAndrew Turner #include <sys/types.h>
28*d422e6f9SAndrew Turner 
29*d422e6f9SAndrew Turner #include <fenv.h>
30*d422e6f9SAndrew Turner #include <float.h>
31*d422e6f9SAndrew Turner 
32*d422e6f9SAndrew Turner static int map[] = {
33*d422e6f9SAndrew Turner 	1,	/* round to nearest */
34*d422e6f9SAndrew Turner 	2,	/* round to positive infinity */
35*d422e6f9SAndrew Turner 	3,	/* round to negative infinity */
36*d422e6f9SAndrew Turner 	0	/* round to zero */
37*d422e6f9SAndrew Turner };
38*d422e6f9SAndrew Turner 
39*d422e6f9SAndrew Turner int
__flt_rounds(void)40*d422e6f9SAndrew Turner __flt_rounds(void)
41*d422e6f9SAndrew Turner {
42*d422e6f9SAndrew Turner 	uint64_t fpcr;
43*d422e6f9SAndrew Turner 
44*d422e6f9SAndrew Turner 	asm volatile("mrs	%0, fpcr" : "=r" (fpcr));
45*d422e6f9SAndrew Turner 	return map[(fpcr >> 22) & 3];
46*d422e6f9SAndrew Turner }
47