summaryrefslogtreecommitdiff
path: root/src/fenv/m68k/fenv.c
blob: d0658e6784777e3aee5bb10c5f40ff18aaba926f (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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#include <fenv.h>
#include <features.h>

#if __HAVE_68881__ || __mcffpu__

static unsigned getsr()
{
	unsigned v;
	__asm__ __volatile__ ("fmove.l %%fpsr,%0" : "=dm"(v));
	return v;
}

static void setsr(unsigned v)
{
	__asm__ __volatile__ ("fmove.l %0,%%fpsr" : : "dm"(v));
}

static unsigned getcr()
{
	unsigned v;
	__asm__ __volatile__ ("fmove.l %%fpcr,%0" : "=dm"(v));
	return v;
}

static void setcr(unsigned v)
{
	__asm__ __volatile__ ("fmove.l %0,%%fpcr" : : "dm"(v));
}

int feclearexcept(int mask)
{
	if (mask & ~FE_ALL_EXCEPT) return -1;
	setsr(getsr() & ~mask);
	return 0;
}

int feraiseexcept(int mask)
{
	if (mask & ~FE_ALL_EXCEPT) return -1;
	setsr(getsr() | mask);
	return 0;
}

int fetestexcept(int mask)
{
	return getsr() & mask;
}

int fegetround(void)
{
	return getcr() & FE_UPWARD;
}

hidden int __fesetround(int r)
{
	setcr((getcr() & ~FE_UPWARD) | r);
	return 0;
}

int fegetenv(fenv_t *envp)
{
	envp->__control_register = getcr();
	envp->__status_register = getsr();
	__asm__ __volatile__ ("fmove.l %%fpiar,%0"
		: "=dm"(envp->__instruction_address));
	return 0;
}

int fesetenv(const fenv_t *envp)
{
	static const fenv_t default_env = { 0 };
	if (envp == FE_DFL_ENV)
		envp = &default_env;
	setcr(envp->__control_register);
	setsr(envp->__status_register);
	__asm__ __volatile__ ("fmove.l %0,%%fpiar"
		: : "dm"(envp->__instruction_address));
	return 0;
}

#else

#include "../fenv.c"

#endif