summaryrefslogtreecommitdiff
path: root/src/regress/lib/libc/setjmp-fpu/setjmp-fpu.c
blob: 45b3c1ee0e9724d7768fb606bbb405921cfa729d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#include <fenv.h>
#include <setjmp.h>

int
TEST_SETJMP(int argc, char *argv[])
{
	jmp_buf env;
	int rv;

	/* Set up the FPU control word register. */
	fesetround(FE_UPWARD);
	fedisableexcept(FE_ALL_EXCEPT);
	feenableexcept(FE_DIVBYZERO);

	rv = SETJMP(env, 0);

	if (rv == 0) {
		fexcept_t flag = FE_OVERFLOW;

		/* Mess with the FPU control word. */
		fesetround(FE_DOWNWARD);
		fedisableexcept(FE_DIVBYZERO);

		/* Set the FPU exception flags. */
		fesetexceptflag(&flag, FE_ALL_EXCEPT);

		LONGJMP(env, 1);
	} else if (rv == 1) {
		fexcept_t flag = 0;

		/* Verify that the FPU control word is preserved. */
		if (fegetround() != FE_UPWARD
		    || fegetexcept() != FE_DIVBYZERO)
			return (1);

		/* Verify that the FPU exception flags weren't clobbered. */
		fegetexceptflag(&flag, FE_ALL_EXCEPT);
		if (flag != FE_OVERFLOW)
			return (1);

		return (0);
	}

	/* This is not supposed to happen. */
	return (1);
}