summaryrefslogtreecommitdiff
path: root/sysdeps/unix/sysv/linux/alpha/setfpucw.c
blob: 43e8536b2078bb43038e0d05d974222dcf43aa5e (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
#include <fpu_control.h>

#include <asm/fpu.h>

extern void		__ieee_set_fp_control (unsigned long);
extern unsigned long	__ieee_get_fp_control (void);


static inline unsigned long
rdfpcr (void)
{
    unsigned long fpcr;

    asm ("trapb; mf_fpcr $f0; trapb; stt $f0,%0" : "m="(fpcr));
    return fpcr;
}


static inline void
wrfpcr (unsigned long fpcr)
{
    asm volatile ("ldt $f0,%0; trapb; mt_fpcr $f0; trapb" :: "m"(fpcr));
}


void
__setfpucw (unsigned short fpu_control)
{
    unsigned long fpcr = 0, fpcw = 0;

    if (!fpu_control)
	fpu_control = _FPU_DEFAULT;

    /* first, set dynamic rounding mode: */

    fpcr = rdfpcr();
    fpcr &= ~FPCR_DYN_MASK;
    switch (fpu_control & 0xc00) {
      case _FPU_RC_NEAREST:	fpcr |= FPCR_DYN_NORMAL; break;
      case _FPU_RC_DOWN:	fpcr |= FPCR_DYN_MINUS; break;
      case _FPU_RC_UP:		fpcr |= FPCR_DYN_PLUS; break;
      case _FPU_RC_ZERO:	fpcr |= FPCR_DYN_CHOPPED; break;
    }
    wrfpcr(fpcr);

    /* now tell kernel about traps that we like to hear about: */

    fpcw = __ieee_get_fp_control();
    fpcw &= ~IEEE_TRAP_ENABLE_MASK;

    if (!(fpu_control & _FPU_MASK_IM))
	fpcw |= IEEE_TRAP_ENABLE_INV;
    if (!(fpu_control & _FPU_MASK_DM))
	fpcw |= IEEE_TRAP_ENABLE_UNF;
    if (!(fpu_control & _FPU_MASK_ZM))
	fpcw |= IEEE_TRAP_ENABLE_DZE;
    if (!(fpu_control & _FPU_MASK_OM))
	fpcw |= IEEE_TRAP_ENABLE_OVF;
    if (!(fpu_control & _FPU_MASK_PM))
	fpcw |= IEEE_TRAP_ENABLE_INE;

    __ieee_set_fp_control(fpcw);

    __fpu_control = fpu_control;	/* update global copy */
}