The branch main has been updated by imp:

URL: 
https://cgit.FreeBSD.org/src/commit/?id=85e449cfcfdf3a3ea99bae7fed9f3cd436eb21ec

commit 85e449cfcfdf3a3ea99bae7fed9f3cd436eb21ec
Author:     K Rin <[email protected]>
AuthorDate: 2025-04-10 02:33:21 +0000
Commit:     Warner Losh <[email protected]>
CommitDate: 2026-06-10 12:35:03 +0000

    libc/tests: copy ieeefp tests out from contrib/netbsd-tests and rename them 
as FreeBSD test convention.
    
    Reviewed by: imp
    Pull Request: https://github.com/freebsd/freebsd-src/pull/1656
---
 lib/libc/tests/gen/Makefile          |   6 +-
 lib/libc/tests/gen/fpclassify_test.c | 206 +++++++++++++++++++
 lib/libc/tests/gen/fpsetmask_test.c  | 369 +++++++++++++++++++++++++++++++++++
 lib/libc/tests/gen/fpsetround_test.c | 163 ++++++++++++++++
 4 files changed, 741 insertions(+), 3 deletions(-)

diff --git a/lib/libc/tests/gen/Makefile b/lib/libc/tests/gen/Makefile
index 97b32827a66a..395171a86bac 100644
--- a/lib/libc/tests/gen/Makefile
+++ b/lib/libc/tests/gen/Makefile
@@ -6,6 +6,7 @@ ATF_TESTS_C+=           dlopen_empty_test
 ATF_TESTS_C+=          fmtcheck2_test
 ATF_TESTS_C+=          fmtmsg_test
 ATF_TESTS_C+=          fnmatch2_test
+ATF_TESTS_C+=          fpclassify_test
 ATF_TESTS_C+=          fpclassify2_test
 .if ${COMPILER_FEATURES:Mblocks}
 ATF_TESTS_C+=          fts_blocks_test
@@ -16,6 +17,8 @@ ATF_TESTS_C+=         fts_open_test
 ATF_TESTS_C+=          fts_options_test
 ATF_TESTS_C+=          fts_regress_test
 ATF_TESTS_C+=          fts_set_test
+ATF_TESTS_C+=          fpsetmask_test
+ATF_TESTS_C+=          fpsetround_test
 ATF_TESTS_C+=          ftw_test
 ATF_TESTS_C+=          getentropy_test
 ATF_TESTS_C+=          getmntinfo_test
@@ -68,9 +71,6 @@ NETBSD_ATF_TESTS_C+=  cpuset_test
 NETBSD_ATF_TESTS_C+=   dir_test
 NETBSD_ATF_TESTS_C+=   floatunditf_test
 NETBSD_ATF_TESTS_C+=   fnmatch_test
-NETBSD_ATF_TESTS_C+=   fpclassify_test
-NETBSD_ATF_TESTS_C+=   fpsetmask_test
-NETBSD_ATF_TESTS_C+=   fpsetround_test
 NETBSD_ATF_TESTS_C+=   ftok_test
 NETBSD_ATF_TESTS_C+=   getcwd_test
 NETBSD_ATF_TESTS_C+=   getgrent_test
diff --git a/lib/libc/tests/gen/fpclassify_test.c 
b/lib/libc/tests/gen/fpclassify_test.c
new file mode 100644
index 000000000000..21dea9e8fee7
--- /dev/null
+++ b/lib/libc/tests/gen/fpclassify_test.c
@@ -0,0 +1,206 @@
+/* $NetBSD: t_fpclassify.c,v 1.3 2011/10/01 21:47:08 christos Exp $ */
+
+/*-
+ * Copyright (c) 2011 The NetBSD Foundation, Inc.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
+ * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
+ * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
+ * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include <atf-c.h>
+
+#include <float.h>
+#include <math.h>
+#include <stdio.h>
+#include <string.h>
+
+#ifndef _FLOAT_IEEE754
+
+ATF_TC(no_test);
+ATF_TC_HEAD(no_test, tc)
+{
+       atf_tc_set_md_var(tc, "descr", "Dummy test");
+}
+
+ATF_TC_BODY(no_test,tc)
+{
+       atf_tc_skip("Test not available on this architecture");
+}
+
+#else /* defined(_FLOAT_IEEE754) */
+
+ATF_TC(fpclassify_float);
+ATF_TC_HEAD(fpclassify_float, tc)
+{
+
+       atf_tc_set_md_var(tc, "descr", "Test float operations");
+}
+
+ATF_TC_BODY(fpclassify_float, tc)
+{
+       float d0, d1, d2, f, ip;
+       int e, i;
+
+       d0 = FLT_MIN;
+       ATF_REQUIRE_EQ(fpclassify(d0), FP_NORMAL);
+       f = frexpf(d0, &e);
+       ATF_REQUIRE_EQ(e, FLT_MIN_EXP);
+       ATF_REQUIRE_EQ(f, 0.5);
+       d1 = d0;
+
+       /* shift a "1" bit through the mantissa (skip the implicit bit) */
+       for (i = 1; i < FLT_MANT_DIG; i++) {
+               d1 /= 2;
+               ATF_REQUIRE_EQ(fpclassify(d1), FP_SUBNORMAL);
+               ATF_REQUIRE(d1 > 0 && d1 < d0);
+
+               d2 = ldexpf(d0, -i);
+               ATF_REQUIRE_EQ(d2, d1);
+
+               d2 = modff(d1, &ip);
+               ATF_REQUIRE_EQ(d2, d1);
+               ATF_REQUIRE_EQ(ip, 0);
+
+               f = frexpf(d1, &e);
+               ATF_REQUIRE_EQ(e, FLT_MIN_EXP - i);
+               ATF_REQUIRE_EQ(f, 0.5);
+       }
+
+       d1 /= 2;
+       ATF_REQUIRE_EQ(fpclassify(d1), FP_ZERO);
+       f = frexpf(d1, &e);
+       ATF_REQUIRE_EQ(e, 0);
+       ATF_REQUIRE_EQ(f, 0);
+}
+
+ATF_TC(fpclassify_double);
+ATF_TC_HEAD(fpclassify_double, tc)
+{
+
+       atf_tc_set_md_var(tc, "descr", "Test double operations");
+}
+
+ATF_TC_BODY(fpclassify_double, tc)
+{
+       double d0, d1, d2, f, ip;
+       int e, i;
+
+       d0 = DBL_MIN;
+       ATF_REQUIRE_EQ(fpclassify(d0), FP_NORMAL);
+       f = frexp(d0, &e);
+       ATF_REQUIRE_EQ(e, DBL_MIN_EXP);
+       ATF_REQUIRE_EQ(f, 0.5);
+       d1 = d0;
+
+       /* shift a "1" bit through the mantissa (skip the implicit bit) */
+       for (i = 1; i < DBL_MANT_DIG; i++) {
+               d1 /= 2;
+               ATF_REQUIRE_EQ(fpclassify(d1), FP_SUBNORMAL);
+               ATF_REQUIRE(d1 > 0 && d1 < d0);
+
+               d2 = ldexp(d0, -i);
+               ATF_REQUIRE_EQ(d2, d1);
+
+               d2 = modf(d1, &ip);
+               ATF_REQUIRE_EQ(d2, d1);
+               ATF_REQUIRE_EQ(ip, 0);
+
+               f = frexp(d1, &e);
+               ATF_REQUIRE_EQ(e, DBL_MIN_EXP - i);
+               ATF_REQUIRE_EQ(f, 0.5);
+       }
+
+       d1 /= 2;
+       ATF_REQUIRE_EQ(fpclassify(d1), FP_ZERO);
+       f = frexp(d1, &e);
+       ATF_REQUIRE_EQ(e, 0);
+       ATF_REQUIRE_EQ(f, 0);
+}
+
+/*
+ * XXX NetBSD doesn't have long-double flavors of frexp, ldexp, and modf,
+ * XXX so this test is disabled.
+ */
+
+#ifdef TEST_LONG_DOUBLE
+
+ATF_TC(fpclassify_long_double);
+ATF_TC_HEAD(fpclassify_long_double, tc)
+{
+
+       atf_tc_set_md_var(tc, "descr", "Test long double operations");
+}
+
+ATF_TC_BODY(fpclassify_long_double, tc)
+{
+       long double d0, d1, d2, f, ip;
+       int e, i;
+
+       d0 = LDBL_MIN;
+       ATF_REQUIRE_EQ(fpclassify(d0), FP_NORMAL);
+       f = frexpl(d0, &e);
+       ATF_REQUIRE_EQ(e, LDBL_MIN_EXP);
+       ATF_REQUIRE_EQ(f, 0.5);
+       d1 = d0;
+
+       /* shift a "1" bit through the mantissa (skip the implicit bit) */
+       for (i = 1; i < LDBL_MANT_DIG; i++) {
+               d1 /= 2;
+               ATF_REQUIRE_EQ(fpclassify(d1), FP_SUBNORMAL);
+               ATF_REQUIRE(d1 > 0 && d1 < d0);
+
+               d2 = ldexpl(d0, -i);
+               ATF_REQUIRE_EQ(d2, d1);
+
+               d2 = modfl(d1, &ip);
+               ATF_REQUIRE_EQ(d2, d1);
+               ATF_REQUIRE_EQ(ip, 0);
+
+               f = frexpl(d1, &e);
+               ATF_REQUIRE_EQ(e, LDBL_MIN_EXP - i);
+               ATF_REQUIRE_EQ(f, 0.5);
+       }
+
+       d1 /= 2;
+       ATF_REQUIRE_EQ(fpclassify(d1), FP_ZERO);
+       f = frexpl(d1, &e);
+       ATF_REQUIRE_EQ(e, 0);
+       ATF_REQUIRE_EQ(f, 0);
+}
+#endif /* TEST_LONG_DOUBLE */
+#endif /* _FLOAT_IEEE754 */
+
+ATF_TP_ADD_TCS(tp)
+{
+
+#ifndef _FLOAT_IEEE754
+       ATF_TP_ADD_TC(tp, no_test);
+#else
+       ATF_TP_ADD_TC(tp, fpclassify_float);
+       ATF_TP_ADD_TC(tp, fpclassify_double);
+#ifdef TEST_LONG_DOUBLE
+       ATF_TP_ADD_TC(tp, fpclassify_long_double);
+#endif /* TEST_LONG_DOUBLE */
+#endif /* _FLOAT_IEEE754 */
+
+       return atf_no_error();
+}
diff --git a/lib/libc/tests/gen/fpsetmask_test.c 
b/lib/libc/tests/gen/fpsetmask_test.c
new file mode 100644
index 000000000000..1c2d9316e5a7
--- /dev/null
+++ b/lib/libc/tests/gen/fpsetmask_test.c
@@ -0,0 +1,369 @@
+/*     $NetBSD: t_fpsetmask.c,v 1.16 2016/03/12 11:55:14 martin Exp $ */
+
+/*-
+ * Copyright (c) 1995 The NetBSD Foundation, Inc.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
+ * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
+ * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
+ * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include <sys/param.h>
+
+#include <atf-c.h>
+
+#include <stdio.h>
+#include <signal.h>
+#include <float.h>
+#include <setjmp.h>
+#include <stdlib.h>
+#include <string.h>
+
+#ifndef _FLOAT_IEEE754
+
+ATF_TC(no_test);
+ATF_TC_HEAD(no_test, tc)
+{
+
+       atf_tc_set_md_var(tc, "descr", "Dummy test case");
+}
+
+ATF_TC_BODY(no_test, tc)
+{
+
+       atf_tc_skip("Test not available on this architecture.");
+}
+
+#else /* defined(_FLOAT_IEEE754) */
+
+#include <ieeefp.h>
+
+#if __arm__ && !__SOFTFP__
+       /*
+        * Some NEON fpus do not implement IEEE exception handling,
+        * skip these tests if running on them and compiled for
+        * hard float.
+        */
+#define        FPU_PREREQ()                                                    
\
+       if (0 == fpsetmask(fpsetmask(FP_X_INV)))                        \
+               atf_tc_skip("FPU does not implement exception handling");
+#endif
+
+#ifndef FPU_PREREQ
+#define        FPU_PREREQ()    /* nothing */
+#endif
+
+void           sigfpe(int, siginfo_t *, void *);
+
+volatile sig_atomic_t signal_caught;
+volatile int sicode;
+
+static volatile const float    f_one   = 1.0;
+static volatile const float    f_zero  = 0.0;
+static volatile const double   d_one   = 1.0;
+static volatile const double   d_zero  = 0.0;
+static volatile const long double ld_one  = 1.0;
+static volatile const long double ld_zero = 0.0;
+
+static volatile const float    f_huge = FLT_MAX;
+static volatile const float    f_tiny = FLT_MIN;
+static volatile const double   d_huge = DBL_MAX;
+static volatile const double   d_tiny = DBL_MIN;
+static volatile const long double ld_huge = LDBL_MAX;
+static volatile const long double ld_tiny = LDBL_MIN;
+
+static volatile float f_x;
+static volatile double d_x;
+static volatile long double ld_x;
+
+/* trip divide by zero */
+static void
+f_dz(void)
+{
+
+       f_x = f_one / f_zero;
+}
+
+static void
+d_dz(void)
+{
+
+       d_x = d_one / d_zero;
+}
+
+static void
+ld_dz(void)
+{
+
+       ld_x = ld_one / ld_zero;
+}
+
+/* trip invalid operation */
+static void
+d_inv(void)
+{
+
+       d_x = d_zero / d_zero;
+}
+
+static void
+ld_inv(void)
+{
+
+       ld_x = ld_zero / ld_zero;
+}
+
+static void
+f_inv(void)
+{
+
+       f_x = f_zero / f_zero;
+}
+
+/* trip overflow */
+static void
+f_ofl(void)
+{
+
+       f_x = f_huge * f_huge;
+}
+
+static void
+d_ofl(void)
+{
+
+       d_x = d_huge * d_huge;
+}
+
+static void
+ld_ofl(void)
+{
+
+       ld_x = ld_huge * ld_huge;
+}
+
+/* trip underflow */
+static void
+f_ufl(void)
+{
+
+       f_x = f_tiny * f_tiny;
+}
+
+static void
+d_ufl(void)
+{
+
+       d_x = d_tiny * d_tiny;
+}
+
+static void
+ld_ufl(void)
+{
+
+       ld_x = ld_tiny * ld_tiny;
+}
+
+struct ops {
+       void (*op)(void);
+       fp_except mask;
+       int sicode;
+};
+
+static const struct ops float_ops[] = {
+       { f_dz, FP_X_DZ, FPE_FLTDIV },
+       { f_inv, FP_X_INV, FPE_FLTINV },
+       { f_ofl, FP_X_OFL, FPE_FLTOVF },
+       { f_ufl, FP_X_UFL, FPE_FLTUND },
+       { NULL, 0, 0 }
+};
+
+static const struct ops double_ops[] = {
+       { d_dz, FP_X_DZ, FPE_FLTDIV },
+       { d_inv, FP_X_INV, FPE_FLTINV },
+       { d_ofl, FP_X_OFL, FPE_FLTOVF },
+       { d_ufl, FP_X_UFL, FPE_FLTUND },
+       { NULL, 0, 0 }
+};
+
+static const struct ops long_double_ops[] = {
+       { ld_dz, FP_X_DZ, FPE_FLTDIV },
+       { ld_inv, FP_X_INV, FPE_FLTINV },
+       { ld_ofl, FP_X_OFL, FPE_FLTOVF },
+       { ld_ufl, FP_X_UFL, FPE_FLTUND },
+       { NULL, 0, 0 }
+};
+
+static sigjmp_buf b;
+
+static void
+fpsetmask_masked(const struct ops *test_ops)
+{
+       struct sigaction sa;
+       fp_except ex1, ex2;
+       const struct ops *t;
+
+       /* mask all exceptions, clear history */
+       fpsetmask(0);
+       fpsetsticky(0);
+
+       /* set up signal handler */
+       sa.sa_sigaction = sigfpe;
+       sigemptyset(&sa.sa_mask);
+       sa.sa_flags = SA_SIGINFO;
+       sigaction(SIGFPE, &sa, 0);
+       signal_caught = 0;
+
+       /*
+        * exceptions masked, check whether "sticky" bits are set correctly
+        */
+       for (t = test_ops; t->op != NULL; t++) {
+               (*t->op)();
+               ex1 = fpgetsticky();
+               ATF_CHECK_EQ(ex1 & t->mask, t->mask);
+               ATF_CHECK_EQ(signal_caught, 0);
+
+               /* check correct fpsetsticky() behaviour */
+               ex2 = fpsetsticky(0);
+               ATF_CHECK_EQ(fpgetsticky(), 0);
+               ATF_CHECK_EQ(ex1, ex2);
+       }
+}
+
+/* force delayed exceptions to be delivered */
+#define BARRIER() fpsetmask(0); f_x = f_one * f_one
+
+static void
+fpsetmask_unmasked(const struct ops *test_ops)
+{
+       struct sigaction sa;
+       int r;
+       const struct ops *volatile t;
+
+       /* mask all exceptions, clear history */
+       fpsetmask(0);
+       fpsetsticky(0);
+
+       /* set up signal handler */
+       sa.sa_sigaction = sigfpe;
+       sigemptyset(&sa.sa_mask);
+       sa.sa_flags = SA_SIGINFO;
+       sigaction(SIGFPE, &sa, 0);
+       signal_caught = 0;
+
+       /*
+        * exception unmasked, check SIGFPE delivery and correct siginfo
+        */
+       for (t = test_ops; t->op != NULL; t++) {
+               fpsetmask(t->mask);
+               r = sigsetjmp(b, 1);
+               if (!r) {
+                       (*t->op)();
+                       BARRIER();
+               }
+               ATF_CHECK_EQ(signal_caught, 1);
+               ATF_CHECK_EQ(sicode, t->sicode);
+               signal_caught = 0;
+       }
+}
+
+void
+sigfpe(int s, siginfo_t *si, void *c)
+{
+       signal_caught = 1;
+       sicode = si->si_code;
+       siglongjmp(b, 1);
+}
+
+#define TEST(m, t)                                                     \
+       ATF_TC(m##_##t);                                                \
+                                                                       \
+       ATF_TC_HEAD(m##_##t, tc)                                        \
+       {                                                               \
+                                                                       \
+               atf_tc_set_md_var(tc, "descr",                          \
+                   "Test " ___STRING(m) " exceptions for "             \
+                   ___STRING(t) "values");                             \
+       }                                                               \
+                                                                       \
+       ATF_TC_BODY(m##_##t, tc)                                        \
+       {                                                               \
+                                                                       \
+               FPU_PREREQ();                                           \
+                                                                       \
+               if (strcmp(MACHINE, "macppc") == 0)                     \
+                       atf_tc_expect_fail("PR port-macppc/46319");     \
+                                                                       \
+               if (isQEMU())                                           \
+                       atf_tc_expect_fail("PR misc/44767");            \
+                                                                       \
+               m(t##_ops);                                             \
+       }
+
+TEST(fpsetmask_masked, float)
+TEST(fpsetmask_masked, double)
+TEST(fpsetmask_masked, long_double)
+TEST(fpsetmask_unmasked, float)
+TEST(fpsetmask_unmasked, double)
+TEST(fpsetmask_unmasked, long_double)
+
+ATF_TC(fpsetmask_basic);
+ATF_TC_HEAD(fpsetmask_basic, tc)
+{
+       atf_tc_set_md_var(tc, "descr", "A basic test of fpsetmask(3)");
+}
+
+ATF_TC_BODY(fpsetmask_basic, tc)
+{
+       size_t i;
+       fp_except_t msk, lst[] = { FP_X_INV, FP_X_DZ, FP_X_OFL, FP_X_UFL };
+
+       FPU_PREREQ();
+
+       msk = fpgetmask();
+       for (i = 0; i < __arraycount(lst); i++) {
+               fpsetmask(msk | lst[i]);
+               ATF_CHECK((fpgetmask() & lst[i]) != 0);
+               fpsetmask(msk & ~lst[i]);
+               ATF_CHECK((fpgetmask() & lst[i]) == 0);
+       }
+
+}
+
+#endif /* defined(_FLOAT_IEEE754) */
+
+ATF_TP_ADD_TCS(tp)
+{
+
+#ifndef _FLOAT_IEEE754
+       ATF_TP_ADD_TC(tp, no_test);
+#else
+       ATF_TP_ADD_TC(tp, fpsetmask_basic);
+       ATF_TP_ADD_TC(tp, fpsetmask_masked_float);
+       ATF_TP_ADD_TC(tp, fpsetmask_masked_double);
+       ATF_TP_ADD_TC(tp, fpsetmask_masked_long_double);
+       ATF_TP_ADD_TC(tp, fpsetmask_unmasked_float);
+       ATF_TP_ADD_TC(tp, fpsetmask_unmasked_double);
+       ATF_TP_ADD_TC(tp, fpsetmask_unmasked_long_double);
+#endif
+
+       return atf_no_error();
+}
diff --git a/lib/libc/tests/gen/fpsetround_test.c 
b/lib/libc/tests/gen/fpsetround_test.c
new file mode 100644
index 000000000000..0f23e74e57c8
--- /dev/null
+++ b/lib/libc/tests/gen/fpsetround_test.c
@@ -0,0 +1,163 @@
+/* $NetBSD: t_fpsetround.c,v 1.6 2011/10/01 17:46:10 christos Exp $ */
+
+/*-
+ * Copyright (c) 2011 The NetBSD Foundation, Inc.
+ * All rights reserved.
+ *
+ * This code is derived from software contributed to The NetBSD Foundation
+ * by Christos Zoulas.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ * 3. All advertising materials mentioning features or use of this software
+ *    must display the following acknowledgement:
+ *        This product includes software developed by the NetBSD
+ *        Foundation, Inc. and its contributors.
+ * 4. Neither the name of The NetBSD Foundation nor the names of its
+ *    contributors may be used to endorse or promote products derived
+ *    from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
+ * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
+ * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
+ * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+#include <sys/cdefs.h>
+__RCSID("$NetBSD: t_fpsetround.c,v 1.6 2011/10/01 17:46:10 christos Exp $");
+
+#include <float.h>
+#include <math.h>
+#include <stdlib.h>
+#include <string.h>
+#include <stdio.h>
+
+#include <atf-c.h>
+
+ATF_TC(fpsetround_basic);
+ATF_TC_HEAD(fpsetround_basic, tc)
+{
+
+       atf_tc_set_md_var(tc, "descr",
+           "Minimal testing of fpgetround(3) and fpsetround(3)");
+}
+
+#ifdef _FLOAT_IEEE754
+#include <ieeefp.h>
+
+static const struct {
+       const char *n;
+       int rm;
+       int rf;
+} rnd[] = {
+       { "RN", FP_RN, 1 },
+       { "RP", FP_RP, 2 },
+       { "RM", FP_RM, 3 },
+       { "RZ", FP_RZ, 0 },
+
+};
+
+static const struct {
+       const char *n;
+       int v[4];
+} tst[] = {    /*  RN  RP  RM  RZ */
+       {  "1.1", {  1,  1,  2,  1 } },
+       {  "1.5", {  1,  2,  2,  1 } },
+       {  "1.9", {  1,  2,  2,  1 } },
+       {  "2.1", {  2,  2,  3,  2 } },
+       {  "2.5", {  2,  2,  3,  2 } },
+       {  "2.9", {  2,  3,  3,  2 } },
+       { "-1.1", { -1, -1, -1, -2 } },
+       { "-1.5", { -1, -2, -1, -2 } },
+       { "-1.9", { -1, -2, -1, -2 } },
+       { "-2.1", { -2, -2, -2, -3 } },
+       { "-2.5", { -2, -2, -2, -3 } },
+       { "-2.9", { -2, -3, -2, -3 } },
+};
+
+static const char *
+getname(int r)
+{
+       for (size_t i = 0; i < __arraycount(rnd); i++)
+               if (rnd[i].rm == r)
+                       return rnd[i].n;
+       return "*unknown*";
+}
+
+static void
+test(int r)
+{
+       int did = 0;
+       for (size_t i = 0; i < __arraycount(tst); i++) {
+               double d = strtod(tst[i].n, NULL);
+               int g = (int)rint(d);
+               int e = tst[i].v[r];
+               ATF_CHECK_EQ(g, e);
+               if (g != e) {
+                       if (!did) {
+                               fprintf(stderr, "Mode Value Result Expected\n");
+                               did = 1;
+                       }
+                       fprintf(stderr, "%4.4s %-5.5s %6d %8d\n", rnd[r].n,
+                           tst[i].n, (int)rint(d), tst[i].v[r]);
+               }
+       }
+}
+#endif
+
+
+ATF_TC_BODY(fpsetround_basic, tc)
+{
+
+#ifndef _FLOAT_IEEE754
+       atf_tc_skip("Test not applicable on this architecture.");
+#else
+       int r;
+
+       ATF_CHECK_EQ(r = fpgetround(), FP_RN);
+       if (FP_RN != r)
+               fprintf(stderr, "default expected=%s got=%s\n", getname(FP_RN),
+                   getname(r));
+       ATF_CHECK_EQ(FLT_ROUNDS, 1);
+
+       for (size_t i = 0; i < __arraycount(rnd); i++) {
+               const size_t j = (i + 1) & 3;
+               const int o = rnd[i].rm;
+               const int n = rnd[j].rm;
+
+               ATF_CHECK_EQ(r = fpsetround(n), o);
+               if (o != r)
+                       fprintf(stderr, "set %s expected=%s got=%s\n",
+                           getname(n), getname(o), getname(r));
+               ATF_CHECK_EQ(r = fpgetround(), n);
+               if (n != r)
+                       fprintf(stderr, "get expected=%s got=%s\n", getname(n),
+                           getname(r));
+               ATF_CHECK_EQ(r = FLT_ROUNDS, rnd[j].rf);
+               if (r != rnd[j].rf)
+                       fprintf(stderr, "rounds expected=%x got=%x\n",
+                           rnd[j].rf, r);
+               test(r);
+       }
+#endif /* _FLOAT_IEEE754 */
+}
+
+ATF_TP_ADD_TCS(tp)
+{
+
+       ATF_TP_ADD_TC(tp, fpsetround_basic);
+
+       return atf_no_error();
+}

Reply via email to