Module Name:    src
Committed By:   martin
Date:           Wed Nov 20 14:04:55 UTC 2024

Modified Files:
        src/lib/libc/arch/aarch64/gen [netbsd-9]: fpsetround.c
        src/tests/lib/libc/gen [netbsd-9]: t_fpsetround.c

Log Message:
Pull up following revision(s) (requested by riastradh in ticket #1923):

        tests/lib/libc/gen/t_fpsetround.c: revision 1.7
        tests/lib/libc/gen/t_fpsetround.c: revision 1.8
        lib/libc/arch/aarch64/gen/fpsetround.c: revision 1.4

fpsetround(3): Test that this doesn't flip on FTZ by accident.
PR port-arm/58782: fpsetround flips all the other fpcsr bits on aarch64

fpsetround(3): Don't toggle all the other bits in fpcr on aarch64.
PR port-arm/58782: fpsetround flips all the other fpcsr bits on aarch64


To generate a diff of this commit:
cvs rdiff -u -r1.2 -r1.2.16.1 src/lib/libc/arch/aarch64/gen/fpsetround.c
cvs rdiff -u -r1.6 -r1.6.44.1 src/tests/lib/libc/gen/t_fpsetround.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/lib/libc/arch/aarch64/gen/fpsetround.c
diff -u src/lib/libc/arch/aarch64/gen/fpsetround.c:1.2 src/lib/libc/arch/aarch64/gen/fpsetround.c:1.2.16.1
--- src/lib/libc/arch/aarch64/gen/fpsetround.c:1.2	Sat Dec 24 15:23:06 2016
+++ src/lib/libc/arch/aarch64/gen/fpsetround.c	Wed Nov 20 14:04:55 2024
@@ -29,7 +29,7 @@
 
 #include <sys/cdefs.h>
 
-__RCSID("$NetBSD: fpsetround.c,v 1.2 2016/12/24 15:23:06 maya Exp $");
+__RCSID("$NetBSD: fpsetround.c,v 1.2.16.1 2024/11/20 14:04:55 martin Exp $");
 
 #include <sys/param.h>
 #include <sys/bitops.h>
@@ -46,7 +46,7 @@ fp_rnd_t
 fpsetround(fp_rnd_t rnd)
 {
 	const uint32_t old_fpcr = reg_fpcr_read();
-	const uint32_t new_fpcr = (~old_fpcr & ~FPCR_RMODE)
+	const uint32_t new_fpcr = (old_fpcr & ~FPCR_RMODE)
 	    | __SHIFTIN(rnd, FPCR_RMODE);
 	reg_fpcr_write(new_fpcr);
 	return __SHIFTOUT(old_fpcr, FPCR_RMODE);

Index: src/tests/lib/libc/gen/t_fpsetround.c
diff -u src/tests/lib/libc/gen/t_fpsetround.c:1.6 src/tests/lib/libc/gen/t_fpsetround.c:1.6.44.1
--- src/tests/lib/libc/gen/t_fpsetround.c:1.6	Sat Oct  1 17:46:10 2011
+++ src/tests/lib/libc/gen/t_fpsetround.c	Wed Nov 20 14:04:55 2024
@@ -1,4 +1,4 @@
-/* $NetBSD: t_fpsetround.c,v 1.6 2011/10/01 17:46:10 christos Exp $ */
+/* $NetBSD: t_fpsetround.c,v 1.6.44.1 2024/11/20 14:04:55 martin Exp $ */
 
 /*-
  * Copyright (c) 2011 The NetBSD Foundation, Inc.
@@ -36,7 +36,7 @@
  * POSSIBILITY OF SUCH DAMAGE.
  */
 #include <sys/cdefs.h>
-__RCSID("$NetBSD: t_fpsetround.c,v 1.6 2011/10/01 17:46:10 christos Exp $");
+__RCSID("$NetBSD: t_fpsetround.c,v 1.6.44.1 2024/11/20 14:04:55 martin Exp $");
 
 #include <float.h>
 #include <math.h>
@@ -154,10 +154,51 @@ ATF_TC_BODY(fpsetround_basic, tc)
 #endif /* _FLOAT_IEEE754 */
 }
 
+ATF_TC(fpsetround_noftz);
+ATF_TC_HEAD(fpsetround_noftz, tc)
+{
+
+	atf_tc_set_md_var(tc, "descr",
+	    "Test fpsetround(3) does not toggle flush-to-zero mode");
+}
+ATF_TC_BODY(fpsetround_noftz, tc)
+{
+#if !defined(_FLOAT_IEEE754) || !defined(__DBL_DENORM_MIN__)
+	atf_tc_skip("no fpsetround or subnormals");
+#else
+	volatile double x = DBL_MIN;
+	volatile double y;
+	int r;
+
+	y = x/2;
+	ATF_CHECK_MSG(y != 0, "machine runs flush-to-zero by default");
+
+	/*
+	 * This curious test is a regression test for:
+	 *
+	 * PR port-arm/58782: fpsetround flips all the other fpcsr bits
+	 * on aarch64
+	 */
+
+	ATF_CHECK_EQ_MSG((r = fpsetround(FP_RN)), FP_RN,
+	    "r=%d FP_RN=%d", r, FP_RN);
+	y = x/2;
+	ATF_CHECK_MSG(y != 0,
+	    "machine runs flush-to-zero after one fpsetround call");
+
+	ATF_CHECK_EQ_MSG((r = fpsetround(FP_RN)), FP_RN,
+	    "r=%d FP_RN=%d", r, FP_RN);
+	y = x/2;
+	ATF_CHECK_MSG(y != 0,
+	    "machine runs flush-to-zero after two fpsetround calls");
+#endif
+}
+
 ATF_TP_ADD_TCS(tp)
 {
 
 	ATF_TP_ADD_TC(tp, fpsetround_basic);
+	ATF_TP_ADD_TC(tp, fpsetround_noftz);
 
 	return atf_no_error();
 }

Reply via email to