xref: /openbsd-src/lib/libc/arch/powerpc64/gen/fpsetround.c (revision e11d2af5d4ec4cbb24b31f0bfebeb108824be235)
1*e11d2af5Sdrahn /*	$OpenBSD: fpsetround.c,v 1.1 2020/06/25 02:03:55 drahn Exp $	*/
2*e11d2af5Sdrahn /*	$NetBSD: fpsetround.c,v 1.1 1999/07/07 01:55:08 danw Exp $	*/
3*e11d2af5Sdrahn 
4*e11d2af5Sdrahn /*
5*e11d2af5Sdrahn  * Copyright (c) 1999 The NetBSD Foundation, Inc.
6*e11d2af5Sdrahn  * All rights reserved.
7*e11d2af5Sdrahn  *
8*e11d2af5Sdrahn  * This code is derived from software contributed to The NetBSD Foundation
9*e11d2af5Sdrahn  * by Dan Winship.
10*e11d2af5Sdrahn  *
11*e11d2af5Sdrahn  * Redistribution and use in source and binary forms, with or without
12*e11d2af5Sdrahn  * modification, are permitted provided that the following conditions
13*e11d2af5Sdrahn  * are met:
14*e11d2af5Sdrahn  * 1. Redistributions of source code must retain the above copyright
15*e11d2af5Sdrahn  *    notice, this list of conditions and the following disclaimer.
16*e11d2af5Sdrahn  * 2. Redistributions in binary form must reproduce the above copyright
17*e11d2af5Sdrahn  *    notice, this list of conditions and the following disclaimer in the
18*e11d2af5Sdrahn  *    documentation and/or other materials provided with the distribution.
19*e11d2af5Sdrahn  *
20*e11d2af5Sdrahn  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21*e11d2af5Sdrahn  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22*e11d2af5Sdrahn  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23*e11d2af5Sdrahn  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24*e11d2af5Sdrahn  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25*e11d2af5Sdrahn  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26*e11d2af5Sdrahn  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27*e11d2af5Sdrahn  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28*e11d2af5Sdrahn  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29*e11d2af5Sdrahn  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30*e11d2af5Sdrahn  * POSSIBILITY OF SUCH DAMAGE.
31*e11d2af5Sdrahn  */
32*e11d2af5Sdrahn 
33*e11d2af5Sdrahn #include <sys/types.h>
34*e11d2af5Sdrahn #include <ieeefp.h>
35*e11d2af5Sdrahn 
36*e11d2af5Sdrahn fp_rnd
fpsetround(rnd_dir)37*e11d2af5Sdrahn fpsetround(rnd_dir)
38*e11d2af5Sdrahn 	fp_rnd rnd_dir;
39*e11d2af5Sdrahn {
40*e11d2af5Sdrahn 	u_int64_t fpscr;
41*e11d2af5Sdrahn 	fp_rnd old;
42*e11d2af5Sdrahn 
43*e11d2af5Sdrahn 	__asm__ volatile("mffs %0" : "=f"(fpscr));
44*e11d2af5Sdrahn 	old = fpscr & 0x3;
45*e11d2af5Sdrahn 	fpscr = (fpscr & 0xfffffffcULL) | rnd_dir;
46*e11d2af5Sdrahn 	__asm__ volatile("mtfsf 0xff,%0" :: "f"(fpscr));
47*e11d2af5Sdrahn 	return (old);
48*e11d2af5Sdrahn }
49