1169695Skan/*	$NetBSD: fpsetround.c,v 1.1 2006/07/01 16:37:20 ross Exp $	*/
2169695Skan
3169695Skan/*
4169695Skan * Copyright (c) 1999 The NetBSD Foundation, Inc.
5169695Skan * All rights reserved.
6169695Skan *
7169695Skan * This code is derived from software contributed to The NetBSD Foundation
8169695Skan * by Dan Winship.
9169695Skan *
10169695Skan * Redistribution and use in source and binary forms, with or without
11169695Skan * modification, are permitted provided that the following conditions
12169695Skan * are met:
13169695Skan * 1. Redistributions of source code must retain the above copyright
14169695Skan *    notice, this list of conditions and the following disclaimer.
15169695Skan * 2. Redistributions in binary form must reproduce the above copyright
16169695Skan *    notice, this list of conditions and the following disclaimer in the
17169695Skan *    documentation and/or other materials provided with the distribution.
18169695Skan *
19169695Skan * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20169695Skan * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21169695Skan * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22169695Skan * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23169695Skan * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24169695Skan * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25169695Skan * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26169695Skan * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27169695Skan * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28169695Skan * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29169695Skan * POSSIBILITY OF SUCH DAMAGE.
30169695Skan */
31169695Skan
32169695Skan#include <sys/cdefs.h>
33169695Skan#if defined(LIBC_SCCS) && !defined(lint)
34169695Skan__RCSID("$NetBSD: fpsetround.c,v 1.1 2006/07/01 16:37:20 ross Exp $");
35169695Skan#endif /* LIBC_SCCS and not lint */
36169695Skan
37169695Skan#include "namespace.h"
38169695Skan
39169695Skan#include <sys/types.h>
40169695Skan#include <ieeefp.h>
41169695Skan#include <powerpc/fpu.h>
42169695Skan
43169695Skan#ifdef __weak_alias
44169695Skan__weak_alias(fpsetround,_fpsetround)
45169695Skan#endif
46169695Skan
47169695Skan#define	ROUNDBITS	FPSCR_RN
48169695Skan
49169695Skanfp_rnd
50169695Skanfpsetround(fp_rnd rnd_dir)
51169695Skan{
52169695Skan	uint64_t fpscr;
53169695Skan	fp_rnd old;
54169695Skan
55169695Skan	__asm volatile("mffs %0" : "=f"(fpscr));
56169695Skan	old = (uint32_t)fpscr & ROUNDBITS;
57169695Skan	fpscr &= ~ROUNDBITS;
58169695Skan	fpscr |= rnd_dir & ROUNDBITS;
59169695Skan	__asm volatile("mtfsf 0xff,%0" :: "f"(fpscr));
60169695Skan	return (old);
61169695Skan}
62169695Skan