1*b5aa3b33Sguenther /* $OpenBSD: fpsetround.c,v 1.3 2014/04/18 15:09:52 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>
20cf252584Smiod #include <ieeefp.h>
21cf252584Smiod
22cf252584Smiod fp_rnd
fpsetround(fp_rnd rnd_dir)23cf252584Smiod fpsetround(fp_rnd rnd_dir)
24cf252584Smiod {
25cf252584Smiod register_t fpscr, nfpscr;
26243b5df4Smiod #if defined(__SH4__) && !defined(__SH4_NOFPU__)
27243b5df4Smiod extern register_t __fpscr_values[2];
28243b5df4Smiod #endif
29cf252584Smiod
30*b5aa3b33Sguenther __asm__ volatile ("sts fpscr, %0" : "=r" (fpscr));
31cf252584Smiod if (rnd_dir == FP_RN || rnd_dir == FP_RZ) {
32cf252584Smiod nfpscr = (fpscr & ~0x03) | rnd_dir;
33243b5df4Smiod #if defined(__SH4__) && !defined(__SH4_NOFPU__)
34243b5df4Smiod __fpscr_values[0] = (__fpscr_values[0] & ~0x03) | rnd_dir;
35243b5df4Smiod __fpscr_values[1] = (__fpscr_values[1] & ~0x03) | rnd_dir;
36243b5df4Smiod #endif
37*b5aa3b33Sguenther __asm__ volatile ("lds %0, fpscr" : : "r" (nfpscr));
38cf252584Smiod }
39cf252584Smiod /* else how report an error? */
40cf252584Smiod
41cf252584Smiod return (fpscr & 0x03);
42cf252584Smiod }
43