xref: /openbsd-src/lib/libc/arch/alpha/gen/fpsetround.c (revision 298116df5b000b61a69743d21c92035418df8900)
1*298116dfSespie /*	$OpenBSD: fpsetround.c,v 1.4 2005/08/07 16:40:13 espie Exp $	*/
28d0618b8Sniklas /*	$NetBSD: fpsetround.c,v 1.1 1995/04/29 05:09:57 cgd Exp $	*/
38d0618b8Sniklas 
4df930be7Sderaadt /*
5df930be7Sderaadt  * Copyright (c) 1995 Christopher G. Demetriou
6df930be7Sderaadt  * All rights reserved.
7df930be7Sderaadt  *
8df930be7Sderaadt  * Redistribution and use in source and binary forms, with or without
9df930be7Sderaadt  * modification, are permitted provided that the following conditions
10df930be7Sderaadt  * are met:
11df930be7Sderaadt  * 1. Redistributions of source code must retain the above copyright
12df930be7Sderaadt  *    notice, this list of conditions and the following disclaimer.
13df930be7Sderaadt  * 2. Redistributions in binary form must reproduce the above copyright
14df930be7Sderaadt  *    notice, this list of conditions and the following disclaimer in the
15df930be7Sderaadt  *    documentation and/or other materials provided with the distribution.
16df930be7Sderaadt  * 3. All advertising materials mentioning features or use of this software
17df930be7Sderaadt  *    must display the following acknowledgement:
18df930be7Sderaadt  *      This product includes software developed by Christopher G. Demetriou
19df930be7Sderaadt  *	for the NetBSD Project.
20df930be7Sderaadt  * 4. The name of the author may not be used to endorse or promote products
21df930be7Sderaadt  *    derived from this software without specific prior written permission
22df930be7Sderaadt  *
23df930be7Sderaadt  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
24df930be7Sderaadt  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
25df930be7Sderaadt  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
26df930be7Sderaadt  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
27df930be7Sderaadt  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
28df930be7Sderaadt  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29df930be7Sderaadt  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30df930be7Sderaadt  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31df930be7Sderaadt  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
32df930be7Sderaadt  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33df930be7Sderaadt  */
34df930be7Sderaadt 
35df930be7Sderaadt #include <sys/types.h>
36df930be7Sderaadt #include <ieeefp.h>
37df930be7Sderaadt 
38df930be7Sderaadt fp_rnd
fpsetround(rnd_dir)39df930be7Sderaadt fpsetround(rnd_dir)
40df930be7Sderaadt 	fp_rnd rnd_dir;
41df930be7Sderaadt {
42df930be7Sderaadt 	double fpcrval;
43df930be7Sderaadt 	u_int64_t old, new;
44df930be7Sderaadt 
45df930be7Sderaadt 	__asm__("trapb");
46df930be7Sderaadt 	__asm__("mf_fpcr %0" : "=f" (fpcrval));
47df930be7Sderaadt 	__asm__("trapb");
48df930be7Sderaadt 	old = *(u_int64_t *)&fpcrval;
49df930be7Sderaadt 
50df930be7Sderaadt 	new = old & ~(long)0x0c00000000000000;
51df930be7Sderaadt 	new = (long)rnd_dir << 58;
52df930be7Sderaadt 	*(u_int64_t *)&fpcrval = new;
53df930be7Sderaadt 
54df930be7Sderaadt 	__asm__("trapb");
55df930be7Sderaadt 	__asm__("mt_fpcr %0" : : "f" (fpcrval));
56df930be7Sderaadt 	__asm__("trapb");
57df930be7Sderaadt 
58df930be7Sderaadt 	return ((old >> 58) & 0x3);
59df930be7Sderaadt }
60