xref: /netbsd-src/lib/libc/arch/powerpc64/gen/fpsetround.c (revision ce099b40997c43048fb78bd578195f81d2456523)
1*ce099b40Smartin /*	$NetBSD: fpsetround.c,v 1.2 2008/04/28 20:22:57 martin Exp $	*/
2d48f1466Sross 
3d48f1466Sross /*
4d48f1466Sross  * Copyright (c) 1999 The NetBSD Foundation, Inc.
5d48f1466Sross  * All rights reserved.
6d48f1466Sross  *
7d48f1466Sross  * This code is derived from software contributed to The NetBSD Foundation
8d48f1466Sross  * by Dan Winship.
9d48f1466Sross  *
10d48f1466Sross  * Redistribution and use in source and binary forms, with or without
11d48f1466Sross  * modification, are permitted provided that the following conditions
12d48f1466Sross  * are met:
13d48f1466Sross  * 1. Redistributions of source code must retain the above copyright
14d48f1466Sross  *    notice, this list of conditions and the following disclaimer.
15d48f1466Sross  * 2. Redistributions in binary form must reproduce the above copyright
16d48f1466Sross  *    notice, this list of conditions and the following disclaimer in the
17d48f1466Sross  *    documentation and/or other materials provided with the distribution.
18d48f1466Sross  *
19d48f1466Sross  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20d48f1466Sross  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21d48f1466Sross  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22d48f1466Sross  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23d48f1466Sross  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24d48f1466Sross  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25d48f1466Sross  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26d48f1466Sross  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27d48f1466Sross  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28d48f1466Sross  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29d48f1466Sross  * POSSIBILITY OF SUCH DAMAGE.
30d48f1466Sross  */
31d48f1466Sross 
32d48f1466Sross #include <sys/cdefs.h>
33d48f1466Sross #if defined(LIBC_SCCS) && !defined(lint)
34*ce099b40Smartin __RCSID("$NetBSD: fpsetround.c,v 1.2 2008/04/28 20:22:57 martin Exp $");
35d48f1466Sross #endif /* LIBC_SCCS and not lint */
36d48f1466Sross 
37d48f1466Sross #include "namespace.h"
38d48f1466Sross 
39d48f1466Sross #include <sys/types.h>
40d48f1466Sross #include <ieeefp.h>
41d48f1466Sross #include <powerpc/fpu.h>
42d48f1466Sross 
43d48f1466Sross #ifdef __weak_alias
__weak_alias(fpsetround,_fpsetround)44d48f1466Sross __weak_alias(fpsetround,_fpsetround)
45d48f1466Sross #endif
46d48f1466Sross 
47d48f1466Sross #define	ROUNDBITS	FPSCR_RN
48d48f1466Sross 
49d48f1466Sross fp_rnd
50d48f1466Sross fpsetround(fp_rnd rnd_dir)
51d48f1466Sross {
52d48f1466Sross 	uint64_t fpscr;
53d48f1466Sross 	fp_rnd old;
54d48f1466Sross 
55d48f1466Sross 	__asm volatile("mffs %0" : "=f"(fpscr));
56d48f1466Sross 	old = (uint32_t)fpscr & ROUNDBITS;
57d48f1466Sross 	fpscr &= ~ROUNDBITS;
58d48f1466Sross 	fpscr |= rnd_dir & ROUNDBITS;
59d48f1466Sross 	__asm volatile("mtfsf 0xff,%0" :: "f"(fpscr));
60d48f1466Sross 	return (old);
61d48f1466Sross }
62