summaryrefslogtreecommitdiff
path: root/src/regress/lib/libc/ieeefp/except/except.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/regress/lib/libc/ieeefp/except/except.c')
-rw-r--r--src/regress/lib/libc/ieeefp/except/except.c112
1 files changed, 63 insertions, 49 deletions
diff --git a/src/regress/lib/libc/ieeefp/except/except.c b/src/regress/lib/libc/ieeefp/except/except.c
index 0ffdcdd468..22bd6809a8 100644
--- a/src/regress/lib/libc/ieeefp/except/except.c
+++ b/src/regress/lib/libc/ieeefp/except/except.c
@@ -1,21 +1,47 @@
1/* $OpenBSD: except.c,v 1.9 2004/12/22 00:54:39 david Exp $ */
2
3#include <sys/types.h>
4#include <unistd.h>
1#include <stdio.h> 5#include <stdio.h>
6#include <stdlib.h>
2#include <signal.h> 7#include <signal.h>
3#include <assert.h> 8#include <assert.h>
4#include <ieeefp.h> 9#include <ieeefp.h>
5#include <float.h> 10#include <float.h>
11#include <err.h>
6 12
7void sigfpe(); 13volatile sig_atomic_t signal_status;
8volatile sig_atomic_t signal_cought;
9 14
10static volatile const double one = 1.0; 15volatile const double one = 1.0;
11static volatile const double zero = 0.0; 16volatile const double zero = 0.0;
12static volatile const double huge = DBL_MAX; 17volatile const double huge = DBL_MAX;
13static volatile const double tiny = DBL_MIN; 18volatile const double tiny = DBL_MIN;
14 19
15main() 20void
21sigfpe(int sig, siginfo_t *si, void *v)
16{ 22{
23 char buf[132];
24
25 if (si) {
26 snprintf(buf, sizeof(buf), "sigfpe: addr=%p, code=%d\n",
27 si->si_addr, si->si_code);
28 write(1, buf, strlen(buf));
29 }
30 _exit(signal_status);
31}
32
33
34int
35main(int argc, char *argv[])
36{
37 struct sigaction sa;
17 volatile double x; 38 volatile double x;
18 39
40 if (argc != 2) {
41 fprintf(stderr, "usage: %s condition\n", argv[0]);
42 exit(1);
43 }
44
19 /* 45 /*
20 * check to make sure that all exceptions are masked and 46 * check to make sure that all exceptions are masked and
21 * that the accumulated exception status is clear. 47 * that the accumulated exception status is clear.
@@ -23,65 +49,53 @@ main()
23 assert(fpgetmask() == 0); 49 assert(fpgetmask() == 0);
24 assert(fpgetsticky() == 0); 50 assert(fpgetsticky() == 0);
25 51
26 /* set up signal handler */ 52 memset(&sa, 0, sizeof(sa));
27 signal (SIGFPE, sigfpe); 53 sa.sa_sigaction = sigfpe;
28 signal_cought = 0; 54 sa.sa_flags = SA_SIGINFO;
55 sigaction(SIGFPE, &sa, NULL);
56 signal_status = 1;
29 57
30 /* trip divide by zero */ 58 /* trip divide by zero */
31 x = one / zero; 59 x = one / zero;
32 assert (fpgetsticky() & FP_X_DZ); 60 assert(fpgetsticky() & FP_X_DZ);
33 assert (signal_cought == 0);
34 fpsetsticky(0); 61 fpsetsticky(0);
35 62
36 /* trip invalid operation */ 63 /* trip invalid operation */
37 x = zero / zero; 64 x = zero / zero;
38 assert (fpgetsticky() & FP_X_INV); 65 assert(fpgetsticky() & FP_X_INV);
39 assert (signal_cought == 0);
40 fpsetsticky(0); 66 fpsetsticky(0);
41 67
42 /* trip overflow */ 68 /* trip overflow */
43 x = huge * huge; 69 x = huge * huge;
44 assert (fpgetsticky() & FP_X_OFL); 70 assert(fpgetsticky() & FP_X_OFL);
45 assert (signal_cought == 0);
46 fpsetsticky(0); 71 fpsetsticky(0);
47 72
48 /* trip underflow */ 73 /* trip underflow */
49 x = tiny * tiny; 74 x = tiny * tiny;
50 assert (fpgetsticky() & FP_X_UFL); 75 assert(fpgetsticky() & FP_X_UFL);
51 assert (signal_cought == 0);
52 fpsetsticky(0); 76 fpsetsticky(0);
53 77
54#if 0 78 signal_status = 0;
55 /* unmask and then trip divide by zero */
56 fpsetmask(FP_X_DZ);
57 x = one / zero;
58 assert (signal_cought == 1);
59 signal_cought = 0;
60
61 /* unmask and then trip invalid operation */
62 fpsetmask(FP_X_INV);
63 x = zero / zero;
64 assert (signal_cought == 1);
65 signal_cought = 0;
66 79
67 /* unmask and then trip overflow */ 80 if (strcmp(argv[1], "fltdiv") == 0) {
68 fpsetmask(FP_X_OFL); 81 /* unmask and then trip divide by zero */
69 x = huge * huge; 82 fpsetmask(FP_X_DZ);
70 assert (signal_cought == 1); 83 x = one / zero;
71 signal_cought = 0; 84 } else if (strcmp(argv[1], "fltinv") == 0) {
72 85 /* unmask and then trip invalid operation */
73 /* unmask and then trip underflow */ 86 fpsetmask(FP_X_INV);
74 fpsetmask(FP_X_UFL); 87 x = zero / zero;
75 x = tiny * tiny; 88 } else if (strcmp(argv[1], "fltovf") == 0) {
76 assert (signal_cought == 1); 89 /* unmask and then trip overflow */
77 signal_cought = 0; 90 fpsetmask(FP_X_OFL);
78#endif 91 x = huge * huge;
92 } else if (strcmp(argv[1], "fltund") == 0) {
93 /* unmask and then trip underflow */
94 fpsetmask(FP_X_UFL);
95 x = tiny * tiny;
96 } else {
97 errx(1, "unrecognized condition %s", argv[1]);
98 }
79 99
80 exit(0); 100 errx(1, "signal wasn't caught");
81}
82
83void
84sigfpe()
85{
86 signal_cought = 1;
87} 101}