xref: /netbsd-src/lib/libc/arch/powerpc/gen/fpsetround.c (revision 85f1d6f3df536538ac71cc2e0192d7595efbdd42)
1*85f1d6f3Smatt /*	$NetBSD: fpsetround.c,v 1.10 2011/07/10 21:18:47 matt Exp $	*/
2687108b4Sdanw 
3687108b4Sdanw /*
4687108b4Sdanw  * Copyright (c) 1999 The NetBSD Foundation, Inc.
5687108b4Sdanw  * All rights reserved.
6687108b4Sdanw  *
7687108b4Sdanw  * This code is derived from software contributed to The NetBSD Foundation
8687108b4Sdanw  * by Dan Winship.
9687108b4Sdanw  *
10687108b4Sdanw  * Redistribution and use in source and binary forms, with or without
11687108b4Sdanw  * modification, are permitted provided that the following conditions
12687108b4Sdanw  * are met:
13687108b4Sdanw  * 1. Redistributions of source code must retain the above copyright
14687108b4Sdanw  *    notice, this list of conditions and the following disclaimer.
15687108b4Sdanw  * 2. Redistributions in binary form must reproduce the above copyright
16687108b4Sdanw  *    notice, this list of conditions and the following disclaimer in the
17687108b4Sdanw  *    documentation and/or other materials provided with the distribution.
18687108b4Sdanw  *
19687108b4Sdanw  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20687108b4Sdanw  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21687108b4Sdanw  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22687108b4Sdanw  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23687108b4Sdanw  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24687108b4Sdanw  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25687108b4Sdanw  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26687108b4Sdanw  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27687108b4Sdanw  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28687108b4Sdanw  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29687108b4Sdanw  * POSSIBILITY OF SUCH DAMAGE.
30687108b4Sdanw  */
31687108b4Sdanw 
325d3e8294Sthorpej #include <sys/cdefs.h>
3388c3eadbSlukem #if defined(LIBC_SCCS) && !defined(lint)
34*85f1d6f3Smatt __RCSID("$NetBSD: fpsetround.c,v 1.10 2011/07/10 21:18:47 matt Exp $");
3588c3eadbSlukem #endif /* LIBC_SCCS and not lint */
365d3e8294Sthorpej 
375d3e8294Sthorpej #include "namespace.h"
385d3e8294Sthorpej 
39687108b4Sdanw #include <sys/types.h>
40687108b4Sdanw #include <ieeefp.h>
41386a0ebdSmatt #include <powerpc/fpu.h>
42687108b4Sdanw 
435d3e8294Sthorpej #ifdef __weak_alias
__weak_alias(fpsetround,_fpsetround)445d3e8294Sthorpej __weak_alias(fpsetround,_fpsetround)
455d3e8294Sthorpej #endif
465d3e8294Sthorpej 
47386a0ebdSmatt #define	ROUNDBITS	FPSCR_RN
48386a0ebdSmatt 
49687108b4Sdanw fp_rnd
50386a0ebdSmatt fpsetround(fp_rnd rnd_dir)
51687108b4Sdanw {
52*85f1d6f3Smatt 	union {
53*85f1d6f3Smatt 		double u_d;
54*85f1d6f3Smatt 		uint64_t u_fpscr;
55*85f1d6f3Smatt 	} ud;
56687108b4Sdanw 	fp_rnd old;
57687108b4Sdanw 
58*85f1d6f3Smatt 	__asm volatile("mffs %0" : "=f"(ud.u_d));
59*85f1d6f3Smatt 	old = (uint32_t)ud.u_fpscr & ROUNDBITS;
60*85f1d6f3Smatt 	ud.u_fpscr &= ~ROUNDBITS;
61*85f1d6f3Smatt 	ud.u_fpscr |= rnd_dir & ROUNDBITS;
62*85f1d6f3Smatt 	__asm volatile("mtfsf 0xff,%0" :: "f"(ud.u_d));
63687108b4Sdanw 	return (old);
64687108b4Sdanw }
65