summaryrefslogtreecommitdiff
path: root/libmath/FPcontrol-MacOSX.c
blob: e36a5e5bbe15bb7a3a54a6023ac5e43c1180ca59 (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
86
87
88
89
90
91
92
93
94
#include "lib9.h"
#include "fpuctl.h"
#include "mathi.h"

#include<stdio.h>

void PPC_PrintFPSCR()
{
    ppc_fp_scr_t fpscr;

    fpscr = get_fp_scr();
    fprintf(stderr, "FPSCR = 0x%08x : 0x%08x\n",
            ((unsigned int *)&fpscr)[0],
            ((unsigned int *)&fpscr)[1]);
    fprintf(stderr, "FPSCR[ve] = %d\n", fpscr.ve);
    fprintf(stderr, "FPSCR[ze] = %d\n", fpscr.ze);
    fprintf(stderr, "FPSCR[ue] = %d\n", fpscr.ue);
    fprintf(stderr, "FPSCR[oe] = %d\n", fpscr.oe);
}

void
FPinit(void)
{
	ulong fcr9 = FPPDBL|FPRNR|FPINVAL|FPZDIV|FPUNFL|FPOVFL;
	setfsr(0);	/* Clear pending exceptions */
	setfcr(fcr9);
}

ulong
getFPstatus(void)
{
	ulong fsr = 0, fsr9 = getfsr();
	/* on specific machines, could be table lookup */
	if(fsr9&FPAINEX) fsr |= INEX;
	if(fsr9&FPAOVFL) fsr |= OVFL;
	if(fsr9&FPAUNFL) fsr |= UNFL;
	if(fsr9&FPAZDIV) fsr |= ZDIV;
	if(fsr9&FPAINVAL) fsr |= INVAL;
	return fsr;
}

ulong
FPstatus(ulong fsr, ulong mask)
{
	ulong fsr9 = 0;
	ulong old = getFPstatus();
	fsr = (fsr&mask) | (old&~mask);
	if(fsr&INEX) fsr9 |= FPAINEX;
	if(fsr&OVFL) fsr9 |= FPAOVFL;
	if(fsr&UNFL) fsr9 |= FPAUNFL;
	if(fsr&ZDIV) fsr9 |= FPAZDIV;
	if(fsr&INVAL) fsr9 |= FPAINVAL;
	setfsr(fsr9);
	return(old&mask);
}

ulong
getFPcontrol(void)
{
	ulong fcr = 0, fcr9 = getfcr();
	switch(fcr9&FPRMASK){
		case FPRNR:	fcr = RND_NR; break;
		case FPRNINF:	fcr = RND_NINF; break;
		case FPRPINF:	fcr = RND_PINF; break;
		case FPRZ:	fcr = RND_Z; break;
	}
	if(fcr9&FPINEX) fcr |= INEX;
	if(fcr9&FPOVFL) fcr |= OVFL;
	if(fcr9&FPUNFL) fcr |= UNFL;
	if(fcr9&FPZDIV) fcr |= ZDIV;
	if(fcr9&FPINVAL) fcr |= INVAL;
	return fcr;
}

ulong
FPcontrol(ulong fcr, ulong mask)
{
	ulong fcr9 = FPPDBL;
	ulong old = getFPcontrol();
	fcr = (fcr&mask) | (old&~mask);
	if(fcr&INEX) fcr9 |= FPINEX;
	if(fcr&OVFL) fcr9 |= FPOVFL;
	if(fcr&UNFL) fcr9 |= FPUNFL;
	if(fcr&ZDIV) fcr9 |= FPZDIV;
	if(fcr&INVAL) fcr9 |= FPINVAL;
	switch(fcr&RND_MASK){
		case RND_NR:	fcr9 |= FPRNR; break;
		case RND_NINF:	fcr9 |= FPRNINF; break;
		case RND_PINF:	fcr9 |= FPRPINF; break;
		case RND_Z:	fcr9 |= FPRZ; break;
	}
	setfcr(fcr9);
	return(old&mask);
}