1 /* $NetBSD: t_fpsetmask.c,v 1.15 2014/11/18 08:58:08 martin Exp $ */
4 * Copyright (c) 1995 The NetBSD Foundation, Inc.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
29 #include <sys/param.h>
42 #ifndef _FLOAT_IEEE754
45 ATF_TC_HEAD(no_test
, tc
)
48 atf_tc_set_md_var(tc
, "descr", "Dummy test case");
51 ATF_TC_BODY(no_test
, tc
)
54 atf_tc_skip("Test not available on this architecture.");
57 #else /* defined(_FLOAT_IEEE754) */
61 #if __arm__ && !__SOFTFP__
63 * Some NEON fpus do not implement IEEE exception handling,
64 * skip these tests if running on them and compiled for
67 #define FPU_PREREQ() \
68 if (0 == fpsetmask(fpsetmask(FP_X_INV))) \
69 atf_tc_skip("FPU does not implement exception handling");
73 #define FPU_PREREQ() /* nothing */
76 void sigfpe(int, siginfo_t
*, void *);
78 volatile sig_atomic_t signal_caught
;
81 static volatile const float f_one
= 1.0;
82 static volatile const float f_zero
= 0.0;
83 static volatile const double d_one
= 1.0;
84 static volatile const double d_zero
= 0.0;
85 static volatile const long double ld_one
= 1.0;
86 static volatile const long double ld_zero
= 0.0;
88 static volatile const float f_huge
= FLT_MAX
;
89 static volatile const float f_tiny
= FLT_MIN
;
90 static volatile const double d_huge
= DBL_MAX
;
91 static volatile const double d_tiny
= DBL_MIN
;
92 static volatile const long double ld_huge
= LDBL_MAX
;
93 static volatile const long double ld_tiny
= LDBL_MIN
;
95 static volatile float f_x
;
96 static volatile double d_x
;
97 static volatile long double ld_x
;
99 /* trip divide by zero */
104 f_x
= f_one
/ f_zero
;
111 d_x
= d_one
/ d_zero
;
118 ld_x
= ld_one
/ ld_zero
;
121 /* trip invalid operation */
126 d_x
= d_zero
/ d_zero
;
133 ld_x
= ld_zero
/ ld_zero
;
140 f_x
= f_zero
/ f_zero
;
148 f_x
= f_huge
* f_huge
;
155 d_x
= d_huge
* d_huge
;
162 ld_x
= ld_huge
* ld_huge
;
170 f_x
= f_tiny
* f_tiny
;
177 d_x
= d_tiny
* d_tiny
;
184 ld_x
= ld_tiny
* ld_tiny
;
193 static const struct ops float_ops
[] = {
194 { f_dz
, FP_X_DZ
, FPE_FLTDIV
},
195 { f_inv
, FP_X_INV
, FPE_FLTINV
},
196 { f_ofl
, FP_X_OFL
, FPE_FLTOVF
},
197 { f_ufl
, FP_X_UFL
, FPE_FLTUND
},
201 static const struct ops double_ops
[] = {
202 { d_dz
, FP_X_DZ
, FPE_FLTDIV
},
203 { d_inv
, FP_X_INV
, FPE_FLTINV
},
204 { d_ofl
, FP_X_OFL
, FPE_FLTOVF
},
205 { d_ufl
, FP_X_UFL
, FPE_FLTUND
},
209 static const struct ops long_double_ops
[] = {
210 { ld_dz
, FP_X_DZ
, FPE_FLTDIV
},
211 { ld_inv
, FP_X_INV
, FPE_FLTINV
},
212 { ld_ofl
, FP_X_OFL
, FPE_FLTOVF
},
213 { ld_ufl
, FP_X_UFL
, FPE_FLTUND
},
220 fpsetmask_masked(const struct ops
*test_ops
)
226 /* mask all exceptions, clear history */
230 /* set up signal handler */
231 sa
.sa_sigaction
= sigfpe
;
232 sigemptyset(&sa
.sa_mask
);
233 sa
.sa_flags
= SA_SIGINFO
;
234 sigaction(SIGFPE
, &sa
, 0);
238 * exceptions masked, check whether "sticky" bits are set correctly
240 for (t
= test_ops
; t
->op
!= NULL
; t
++) {
243 ATF_CHECK_EQ(ex1
& t
->mask
, t
->mask
);
244 ATF_CHECK_EQ(signal_caught
, 0);
246 /* check correct fpsetsticky() behaviour */
247 ex2
= fpsetsticky(0);
248 ATF_CHECK_EQ(fpgetsticky(), 0);
249 ATF_CHECK_EQ(ex1
, ex2
);
253 /* force delayed exceptions to be delivered */
254 #define BARRIER() fpsetmask(0); f_x = f_one * f_one
257 fpsetmask_unmasked(const struct ops
*test_ops
)
261 const struct ops
*volatile t
;
263 /* mask all exceptions, clear history */
267 /* set up signal handler */
268 sa
.sa_sigaction
= sigfpe
;
269 sigemptyset(&sa
.sa_mask
);
270 sa
.sa_flags
= SA_SIGINFO
;
271 sigaction(SIGFPE
, &sa
, 0);
275 * exception unmasked, check SIGFPE delivery and correct siginfo
277 for (t
= test_ops
; t
->op
!= NULL
; t
++) {
284 ATF_CHECK_EQ(signal_caught
, 1);
285 ATF_CHECK_EQ(sicode
, t
->sicode
);
291 sigfpe(int s
, siginfo_t
*si
, void *c
)
294 sicode
= si
->si_code
;
301 ATF_TC_HEAD(m##_##t, tc) \
304 atf_tc_set_md_var(tc, "descr", \
305 "Test " ___STRING(m) " exceptions for " \
306 ___STRING(t) "values"); \
309 ATF_TC_BODY(m##_##t, tc) \
314 if (strcmp(MACHINE, "macppc") == 0) \
315 atf_tc_expect_fail("PR port-macppc/46319"); \
318 atf_tc_expect_fail("PR misc/44767"); \
323 TEST(fpsetmask_masked
, float)
324 TEST(fpsetmask_masked
, double)
325 TEST(fpsetmask_masked
, long_double
)
326 TEST(fpsetmask_unmasked
, float)
327 TEST(fpsetmask_unmasked
, double)
328 TEST(fpsetmask_unmasked
, long_double
)
330 ATF_TC(fpsetmask_basic
);
331 ATF_TC_HEAD(fpsetmask_basic
, tc
)
333 atf_tc_set_md_var(tc
, "descr", "A basic test of fpsetmask(3)");
336 ATF_TC_BODY(fpsetmask_basic
, tc
)
339 fp_except_t msk
, lst
[] = { FP_X_INV
, FP_X_DZ
, FP_X_OFL
, FP_X_UFL
};
344 for (i
= 0; i
< __arraycount(lst
); i
++) {
345 fpsetmask(msk
| lst
[i
]);
346 ATF_CHECK((fpgetmask() & lst
[i
]) != 0);
347 fpsetmask(msk
& lst
[i
]);
348 ATF_CHECK((fpgetmask() & lst
[i
]) == 0);
353 #endif /* defined(_FLOAT_IEEE754) */
358 #ifndef _FLOAT_IEEE754
359 ATF_TP_ADD_TC(tp
, no_test
);
361 ATF_TP_ADD_TC(tp
, fpsetmask_basic
);
362 ATF_TP_ADD_TC(tp
, fpsetmask_masked_float
);
363 ATF_TP_ADD_TC(tp
, fpsetmask_masked_double
);
364 ATF_TP_ADD_TC(tp
, fpsetmask_masked_long_double
);
365 ATF_TP_ADD_TC(tp
, fpsetmask_unmasked_float
);
366 ATF_TP_ADD_TC(tp
, fpsetmask_unmasked_double
);
367 ATF_TP_ADD_TC(tp
, fpsetmask_unmasked_long_double
);
370 return atf_no_error();