xref: /freebsd/lib/libc/riscv/gen/flt_rounds.c (revision 312a05c39e5fd79b37ee6f922462232797f56a2a)
10bfee928SRuslan Bukin /*-
2*7804dd52SRuslan Bukin  * Copyright (c) 2015-2016 Ruslan Bukin <br@bsdpad.com>
30bfee928SRuslan Bukin  * All rights reserved.
40bfee928SRuslan Bukin  *
50bfee928SRuslan Bukin  * Portions of this software were developed by SRI International and the
60bfee928SRuslan Bukin  * University of Cambridge Computer Laboratory under DARPA/AFRL contract
70bfee928SRuslan Bukin  * FA8750-10-C-0237 ("CTSRD"), as part of the DARPA CRASH research programme.
80bfee928SRuslan Bukin  *
90bfee928SRuslan Bukin  * Portions of this software were developed by the University of Cambridge
100bfee928SRuslan Bukin  * Computer Laboratory as part of the CTSRD Project, with support from the
110bfee928SRuslan Bukin  * UK Higher Education Innovation Fund (HEIF).
120bfee928SRuslan Bukin  *
130bfee928SRuslan Bukin  * Redistribution and use in source and binary forms, with or without
140bfee928SRuslan Bukin  * modification, are permitted provided that the following conditions
150bfee928SRuslan Bukin  * are met:
160bfee928SRuslan Bukin  * 1. Redistributions of source code must retain the above copyright
170bfee928SRuslan Bukin  *    notice, this list of conditions and the following disclaimer.
180bfee928SRuslan Bukin  * 2. Redistributions in binary form must reproduce the above copyright
190bfee928SRuslan Bukin  *    notice, this list of conditions and the following disclaimer in the
200bfee928SRuslan Bukin  *    documentation and/or other materials provided with the distribution.
210bfee928SRuslan Bukin  *
220bfee928SRuslan Bukin  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
230bfee928SRuslan Bukin  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
240bfee928SRuslan Bukin  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
250bfee928SRuslan Bukin  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
260bfee928SRuslan Bukin  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
270bfee928SRuslan Bukin  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
280bfee928SRuslan Bukin  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
290bfee928SRuslan Bukin  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
300bfee928SRuslan Bukin  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
310bfee928SRuslan Bukin  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
320bfee928SRuslan Bukin  * SUCH DAMAGE.
330bfee928SRuslan Bukin  */
340bfee928SRuslan Bukin 
350bfee928SRuslan Bukin #include <sys/types.h>
360bfee928SRuslan Bukin 
370bfee928SRuslan Bukin #include <fenv.h>
380bfee928SRuslan Bukin #include <float.h>
390bfee928SRuslan Bukin 
400bfee928SRuslan Bukin int
__flt_rounds(void)410bfee928SRuslan Bukin __flt_rounds(void)
420bfee928SRuslan Bukin {
43*7804dd52SRuslan Bukin 	uint64_t mode;
440bfee928SRuslan Bukin 
45*7804dd52SRuslan Bukin 	__asm __volatile("csrr %0, fcsr" : "=r" (mode));
460bfee928SRuslan Bukin 
47*7804dd52SRuslan Bukin 	switch (mode & _ROUND_MASK) {
480bfee928SRuslan Bukin 	case FE_TOWARDZERO:
490bfee928SRuslan Bukin 		return (0);
500bfee928SRuslan Bukin 	case FE_TONEAREST:
510bfee928SRuslan Bukin 		return (1);
520bfee928SRuslan Bukin 	case FE_UPWARD:
530bfee928SRuslan Bukin 		return (2);
540bfee928SRuslan Bukin 	case FE_DOWNWARD:
550bfee928SRuslan Bukin 		return (3);
560bfee928SRuslan Bukin 	}
570bfee928SRuslan Bukin 
580bfee928SRuslan Bukin 	return (-1);
590bfee928SRuslan Bukin }
60