xref: /netbsd-src/lib/libc/arch/arm/hardfloat/fpsetround.c (revision 5e9ff8d2570beb62171375d17b4a0c86e8082c8f)
1*5e9ff8d2Smatt /*	$NetBSD: fpsetround.c,v 1.8 2013/02/03 01:50:54 matt Exp $	*/
25aefcfdcSbjh21 
360e85183Smatt /*-
460e85183Smatt  * Copyright (c) 2013 The NetBSD Foundation, Inc.
55aefcfdcSbjh21  * All rights reserved.
65aefcfdcSbjh21  *
760e85183Smatt  * This code is derived from software contributed to The NetBSD Foundation
860e85183Smatt  * by Matt Thomas of 3am Software Foundry.
960e85183Smatt  *
105aefcfdcSbjh21  * Redistribution and use in source and binary forms, with or without
115aefcfdcSbjh21  * modification, are permitted provided that the following conditions
125aefcfdcSbjh21  * are met:
135aefcfdcSbjh21  * 1. Redistributions of source code must retain the above copyright
145aefcfdcSbjh21  *    notice, this list of conditions and the following disclaimer.
155aefcfdcSbjh21  * 2. Redistributions in binary form must reproduce the above copyright
165aefcfdcSbjh21  *    notice, this list of conditions and the following disclaimer in the
175aefcfdcSbjh21  *    documentation and/or other materials provided with the distribution.
185aefcfdcSbjh21  *
1960e85183Smatt  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
2060e85183Smatt  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
2160e85183Smatt  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
2260e85183Smatt  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
2360e85183Smatt  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
2460e85183Smatt  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
2560e85183Smatt  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
2660e85183Smatt  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
2760e85183Smatt  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
2860e85183Smatt  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
2960e85183Smatt  * POSSIBILITY OF SUCH DAMAGE.
305aefcfdcSbjh21  */
315aefcfdcSbjh21 
3260e85183Smatt #ifndef __VFP_FP__
3360e85183Smatt #error FPA is not supported anymore
3460e85183Smatt #endif
3560e85183Smatt 
365d3e8294Sthorpej #include <sys/cdefs.h>
3788c3eadbSlukem #if defined(LIBC_SCCS) && !defined(lint)
38*5e9ff8d2Smatt __RCSID("$NetBSD: fpsetround.c,v 1.8 2013/02/03 01:50:54 matt Exp $");
3988c3eadbSlukem #endif /* LIBC_SCCS and not lint */
405d3e8294Sthorpej 
415d3e8294Sthorpej #include "namespace.h"
425d3e8294Sthorpej 
435aefcfdcSbjh21 #include <sys/types.h>
445aefcfdcSbjh21 #include <ieeefp.h>
455aefcfdcSbjh21 
46*5e9ff8d2Smatt #include <arm/vfpreg.h>
47*5e9ff8d2Smatt 
485d3e8294Sthorpej #ifdef __weak_alias
__weak_alias(fpsetround,_fpsetround)495d3e8294Sthorpej __weak_alias(fpsetround,_fpsetround)
505d3e8294Sthorpej #endif
515d3e8294Sthorpej 
525aefcfdcSbjh21 /*
535aefcfdcSbjh21  * Return the current FP rounding mode
545aefcfdcSbjh21  */
555aefcfdcSbjh21 
565aefcfdcSbjh21 fp_rnd
5760e85183Smatt fpsetround(fp_rnd new_rnd)
585aefcfdcSbjh21 {
5960e85183Smatt 	__CTASSERT(__SHIFTOUT(VFP_FPSCR_RN, VFP_FPSCR_RMODE) == FP_RN);
6060e85183Smatt 	__CTASSERT(__SHIFTOUT(VFP_FPSCR_RP, VFP_FPSCR_RMODE) == FP_RP);
6160e85183Smatt 	__CTASSERT(__SHIFTOUT(VFP_FPSCR_RM, VFP_FPSCR_RMODE) == FP_RM);
6260e85183Smatt 	__CTASSERT(__SHIFTOUT(VFP_FPSCR_RZ, VFP_FPSCR_RMODE) == FP_RZ);
6360e85183Smatt 	uint32_t fpscr;
6460e85183Smatt 	__asm __volatile("vmrs %0, fpscr" : "=r" (fpscr));
6560e85183Smatt 	fp_rnd old_rnd = __SHIFTOUT(fpscr, VFP_FPSCR_RMODE);
6660e85183Smatt 	fpscr ^= __SHIFTIN(new_rnd ^ old_rnd, VFP_FPSCR_RMODE);
6760e85183Smatt 	__asm __volatile("vmsr fpscr, %0" :: "r" (fpscr));
6860e85183Smatt 	return old_rnd;
695aefcfdcSbjh21 }
70