xref: /netbsd-src/lib/libc/arch/alpha/gen/fpsetround.c (revision 638c9cf50238afde93c6d11d33b6eebfe2c72e03)
1*638c9cf5She /* $NetBSD: fpsetround.c,v 1.12 2012/03/21 20:07:52 he Exp $ */
23e14f389Scgd 
33e14f389Scgd /*
43e14f389Scgd  * Copyright (c) 1995 Christopher G. Demetriou
53e14f389Scgd  * All rights reserved.
63e14f389Scgd  *
73e14f389Scgd  * Redistribution and use in source and binary forms, with or without
83e14f389Scgd  * modification, are permitted provided that the following conditions
93e14f389Scgd  * are met:
103e14f389Scgd  * 1. Redistributions of source code must retain the above copyright
113e14f389Scgd  *    notice, this list of conditions and the following disclaimer.
123e14f389Scgd  * 2. Redistributions in binary form must reproduce the above copyright
133e14f389Scgd  *    notice, this list of conditions and the following disclaimer in the
143e14f389Scgd  *    documentation and/or other materials provided with the distribution.
153e14f389Scgd  * 3. All advertising materials mentioning features or use of this software
163e14f389Scgd  *    must display the following acknowledgement:
17db755e7cScgd  *          This product includes software developed for the
1899410184Ssalo  *          NetBSD Project.  See http://www.NetBSD.org/ for
19db755e7cScgd  *          information about NetBSD.
203e14f389Scgd  * 4. The name of the author may not be used to endorse or promote products
21db755e7cScgd  *    derived from this software without specific prior written permission.
223e14f389Scgd  *
233e14f389Scgd  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
243e14f389Scgd  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
253e14f389Scgd  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
263e14f389Scgd  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
273e14f389Scgd  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
283e14f389Scgd  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
293e14f389Scgd  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
303e14f389Scgd  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
313e14f389Scgd  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
323e14f389Scgd  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33db755e7cScgd  *
34db755e7cScgd  * <<Id: LICENSE,v 1.2 2000/06/14 15:57:33 cgd Exp>>
353e14f389Scgd  */
363e14f389Scgd 
375d3e8294Sthorpej #include <sys/cdefs.h>
3888c3eadbSlukem #if defined(LIBC_SCCS) && !defined(lint)
39*638c9cf5She __RCSID("$NetBSD: fpsetround.c,v 1.12 2012/03/21 20:07:52 he Exp $");
4088c3eadbSlukem #endif /* LIBC_SCCS and not lint */
415d3e8294Sthorpej 
425d3e8294Sthorpej #include "namespace.h"
435d3e8294Sthorpej 
443e14f389Scgd #include <sys/types.h>
453e14f389Scgd #include <ieeefp.h>
463e14f389Scgd 
475d3e8294Sthorpej #ifdef __weak_alias
__weak_alias(fpsetround,_fpsetround)485d3e8294Sthorpej __weak_alias(fpsetround,_fpsetround)
495d3e8294Sthorpej #endif
505d3e8294Sthorpej 
513e14f389Scgd fp_rnd
52f3fc9a46Smatt fpsetround(fp_rnd rnd_dir)
533e14f389Scgd {
54f3fc9a46Smatt 	union {
55f3fc9a46Smatt 		double d;
56f3fc9a46Smatt 		uint64_t u64;
57f3fc9a46Smatt 	} fpcrval;
58f3fc9a46Smatt 	fp_rnd old;
593e14f389Scgd 
60f3fc9a46Smatt 	__asm("excb; mf_fpcr %0; excb" : "=f" (fpcrval.d));
61*638c9cf5She 	old = (fp_rnd)(fpcrval.u64 >> 58) & 0x3;
623e14f389Scgd 
63f3fc9a46Smatt 	rnd_dir ^= old;
643e14f389Scgd 
65f3fc9a46Smatt 	fpcrval.u64 ^= (long)rnd_dir << 58;
663e14f389Scgd 
67f3fc9a46Smatt 	__asm("excb; mt_fpcr %0; excb" : : "f" (fpcrval.d));
68f3fc9a46Smatt 
69f3fc9a46Smatt 	return old;
703e14f389Scgd }
71