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