1 /* $NetBSD: t_fpsetmask.c,v 1.12 2013/04/14 16:03:06 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.
30 #include <atf-c/config.h>
41 #ifndef _FLOAT_IEEE754
44 ATF_TC_HEAD(no_test
, tc
)
47 atf_tc_set_md_var(tc
, "descr", "Dummy test case");
50 ATF_TC_BODY(no_test
, tc
)
53 atf_tc_skip("Test not available on this architecture.");
56 #else /* defined(_FLOAT_IEEE754) */
60 const char *skip_mesg
;
61 const char *skip_arch
;
63 void sigfpe(int, siginfo_t
*, void *);
65 volatile sig_atomic_t signal_caught
;
68 static volatile const float f_one
= 1.0;
69 static volatile const float f_zero
= 0.0;
70 static volatile const double d_one
= 1.0;
71 static volatile const double d_zero
= 0.0;
72 static volatile const long double ld_one
= 1.0;
73 static volatile const long double ld_zero
= 0.0;
75 static volatile const float f_huge
= FLT_MAX
;
76 static volatile const float f_tiny
= FLT_MIN
;
77 static volatile const double d_huge
= DBL_MAX
;
78 static volatile const double d_tiny
= DBL_MIN
;
79 static volatile const long double ld_huge
= LDBL_MAX
;
80 static volatile const long double ld_tiny
= LDBL_MIN
;
82 static volatile float f_x
;
83 static volatile double d_x
;
84 static volatile long double ld_x
;
86 /* trip divide by zero */
105 ld_x
= ld_one
/ ld_zero
;
108 /* trip invalid operation */
113 d_x
= d_zero
/ d_zero
;
120 ld_x
= ld_zero
/ ld_zero
;
127 f_x
= f_zero
/ f_zero
;
135 f_x
= f_huge
* f_huge
;
142 d_x
= d_huge
* d_huge
;
149 ld_x
= ld_huge
* ld_huge
;
157 f_x
= f_tiny
* f_tiny
;
164 d_x
= d_tiny
* d_tiny
;
171 ld_x
= ld_tiny
* ld_tiny
;
180 static const struct ops float_ops
[] = {
181 { f_dz
, FP_X_DZ
, FPE_FLTDIV
},
182 { f_inv
, FP_X_INV
, FPE_FLTINV
},
183 { f_ofl
, FP_X_OFL
, FPE_FLTOVF
},
184 { f_ufl
, FP_X_UFL
, FPE_FLTUND
},
188 static const struct ops double_ops
[] = {
189 { d_dz
, FP_X_DZ
, FPE_FLTDIV
},
190 { d_inv
, FP_X_INV
, FPE_FLTINV
},
191 { d_ofl
, FP_X_OFL
, FPE_FLTOVF
},
192 { d_ufl
, FP_X_UFL
, FPE_FLTUND
},
196 static const struct ops long_double_ops
[] = {
197 { ld_dz
, FP_X_DZ
, FPE_FLTDIV
},
198 { ld_inv
, FP_X_INV
, FPE_FLTINV
},
199 { ld_ofl
, FP_X_OFL
, FPE_FLTOVF
},
200 { ld_ufl
, FP_X_UFL
, FPE_FLTUND
},
207 fpsetmask_masked(const struct ops
*test_ops
)
213 /* mask all exceptions, clear history */
217 /* set up signal handler */
218 sa
.sa_sigaction
= sigfpe
;
219 sigemptyset(&sa
.sa_mask
);
220 sa
.sa_flags
= SA_SIGINFO
;
221 sigaction(SIGFPE
, &sa
, 0);
225 * exceptions masked, check whether "sticky" bits are set correctly
227 for (t
= test_ops
; t
->op
!= NULL
; t
++) {
230 ATF_CHECK_EQ(ex1
& t
->mask
, t
->mask
);
231 ATF_CHECK_EQ(signal_caught
, 0);
233 /* check correct fpsetsticky() behaviour */
234 ex2
= fpsetsticky(0);
235 ATF_CHECK_EQ(fpgetsticky(), 0);
236 ATF_CHECK_EQ(ex1
, ex2
);
240 /* force delayed exceptions to be delivered */
241 #define BARRIER() fpsetmask(0); f_x = f_one * f_one
244 fpsetmask_unmasked(const struct ops
*test_ops
)
248 const struct ops
*volatile t
;
250 /* mask all exceptions, clear history */
254 /* set up signal handler */
255 sa
.sa_sigaction
= sigfpe
;
256 sigemptyset(&sa
.sa_mask
);
257 sa
.sa_flags
= SA_SIGINFO
;
258 sigaction(SIGFPE
, &sa
, 0);
262 * exception unmasked, check SIGFPE delivery and correct siginfo
264 for (t
= test_ops
; t
->op
!= NULL
; t
++) {
271 ATF_CHECK_EQ(signal_caught
, 1);
272 ATF_CHECK_EQ(sicode
, t
->sicode
);
278 sigfpe(int s
, siginfo_t
*si
, void *c
)
281 sicode
= si
->si_code
;
288 ATF_TC_HEAD(m##_##t, tc) \
291 atf_tc_set_md_var(tc, "descr", \
292 "Test " ___STRING(m) " exceptions for " \
293 ___STRING(t) "values"); \
296 ATF_TC_BODY(m##_##t, tc) \
298 if (strcmp(atf_config_get("atf_arch"), "macppc") == 0) \
299 atf_tc_expect_fail("PR port-macppc/46319"); \
302 atf_tc_expect_fail("PR misc/44767"); \
307 TEST(fpsetmask_masked
, float)
308 TEST(fpsetmask_masked
, double)
309 TEST(fpsetmask_masked
, long_double
)
310 TEST(fpsetmask_unmasked
, float)
311 TEST(fpsetmask_unmasked
, double)
312 TEST(fpsetmask_unmasked
, long_double
)
314 ATF_TC(fpsetmask_basic
);
315 ATF_TC_HEAD(fpsetmask_basic
, tc
)
317 atf_tc_set_md_var(tc
, "descr", "A basic test of fpsetmask(3)");
320 ATF_TC_BODY(fpsetmask_basic
, tc
)
323 fp_except_t msk
, lst
[] = { FP_X_INV
, FP_X_DZ
, FP_X_OFL
, FP_X_UFL
};
326 for (i
= 0; i
< __arraycount(lst
); i
++) {
327 fpsetmask(msk
| lst
[i
]);
328 ATF_CHECK((fpgetmask() & lst
[i
]) != 0);
329 fpsetmask(msk
& lst
[i
]);
330 ATF_CHECK((fpgetmask() & lst
[i
]) == 0);
335 #endif /* defined(_FLOAT_IEEE754) */
340 #ifndef _FLOAT_IEEE754
341 ATF_TP_ADD_TC(tp
, no_test
);
343 ATF_TP_ADD_TC(tp
, fpsetmask_basic
);
344 ATF_TP_ADD_TC(tp
, fpsetmask_masked_float
);
345 ATF_TP_ADD_TC(tp
, fpsetmask_masked_double
);
346 ATF_TP_ADD_TC(tp
, fpsetmask_masked_long_double
);
347 ATF_TP_ADD_TC(tp
, fpsetmask_unmasked_float
);
348 ATF_TP_ADD_TC(tp
, fpsetmask_unmasked_double
);
349 ATF_TP_ADD_TC(tp
, fpsetmask_unmasked_long_double
);
352 return atf_no_error();