1 /* $OpenBSD: flt_rounds.c,v 1.6 2016/07/26 19:07:09 guenther Exp $ */
2 /*
3 * Copyright (c) 2006 Miodrag Vallat.
4 *
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice, this permission notice, and the disclaimer below
8 * appear in all copies.
9 *
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 */
18
19 #include <sys/types.h>
20 #include <float.h>
21 #include <ieeefp.h>
22
23 static const int rndmap[] = {
24 1, /* round to nearest */
25 0, /* round to zero */
26 2, /* round to positive infinity */
27 3 /* round to negative infinity */
28 };
29
30 int
__flt_rounds()31 __flt_rounds()
32 {
33 #if !defined(SOFTFLOAT)
34 register_t fpscr;
35
36 __asm__ volatile ("sts fpscr, %0" : "=r" (fpscr));
37 return rndmap[fpscr & 0x03];
38 #else
39 return rndmap[fpgetround()];
40 #endif
41 }
42 DEF_STRONG(__flt_rounds);
43