diff options
Diffstat (limited to 'src/regress/lib/libc/ieeefp/except/except.c')
| -rw-r--r-- | src/regress/lib/libc/ieeefp/except/except.c | 116 |
1 files changed, 68 insertions, 48 deletions
diff --git a/src/regress/lib/libc/ieeefp/except/except.c b/src/regress/lib/libc/ieeefp/except/except.c index 0ffdcdd468..1d11877777 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.10 2006/05/15 14:00:22 kettenis 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 | ||
| 7 | void sigfpe(); | 13 | volatile sig_atomic_t signal_status; |
| 8 | volatile sig_atomic_t signal_cought; | ||
| 9 | 14 | ||
| 10 | static volatile const double one = 1.0; | 15 | volatile const double one = 1.0; |
| 11 | static volatile const double zero = 0.0; | 16 | volatile const double zero = 0.0; |
| 12 | static volatile const double huge = DBL_MAX; | 17 | volatile const double huge = DBL_MAX; |
| 13 | static volatile const double tiny = DBL_MIN; | 18 | volatile const double tiny = DBL_MIN; |
| 14 | 19 | ||
| 15 | main() | 20 | void |
| 21 | sigfpe(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 | |||
| 34 | int | ||
| 35 | main(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,59 @@ 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 | /* |
| 81 | } | 101 | * attempt to trigger the exception on machines where |
| 102 | * floating-point exceptions are deferred. | ||
| 103 | */ | ||
| 104 | x = one * one; | ||
| 82 | 105 | ||
| 83 | void | 106 | errx(1, "signal wasn't caught"); |
| 84 | sigfpe() | ||
| 85 | { | ||
| 86 | signal_cought = 1; | ||
| 87 | } | 107 | } |
