summaryrefslogtreecommitdiffstats
path: root/newlib/libc/machine/riscv/ieeefp.c
blob: 5288877a6dadca4d09c567df6f2547197b62028e (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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
/* Copyright (c) 2017  SiFive Inc. All rights reserved.

   This copyrighted material is made available to anyone wishing to use,
   modify, copy, or redistribute it subject to the terms and conditions
   of the BSD License.   This program is distributed in the hope that
   it will be useful, but WITHOUT ANY WARRANTY expressed or implied,
   including the implied warranties of MERCHANTABILITY or FITNESS FOR
   A PARTICULAR PURPOSE.  A copy of this license is available at
   http://www.opensource.org/licenses.
*/

#include <ieeefp.h>

#ifdef __riscv_flen
static void
fssr(unsigned value)
{
  asm volatile ("fssr %0" :: "r"(value));
}

static unsigned
frsr()
{
  unsigned value;
  asm volatile ("frsr %0" : "=r" (value));
  return value;
}

static fp_rnd
frm_fp_rnd (unsigned frm)
{
  switch (frm)
    {
    case 0: return FP_RN;
    case 1: return FP_RZ;
    case 2: return FP_RM;
    case 3: return FP_RP;
    /* 4 ~ 7 is invalid value, so just retun FP_RP.  */
    default:return FP_RP;
    }
}

#endif /* __riscv_flen */

fp_except
fpgetmask(void)
{
  return 0;
}

fp_rnd
fpgetround(void)
{
#ifdef __riscv_flen
  unsigned rm = (frsr () >> 5) & 0x7;
  return frm_fp_rnd (rm);
#else
  return FP_RZ;
#endif /* __riscv_flen */
}

fp_except
fpgetsticky(void)
{
#ifdef __riscv_flen
  return frsr () & 0x1f;
#else
  return 0;
#endif /* __riscv_flen */
}

fp_except
fpsetmask(fp_except mask)
{
  return -1;
}

fp_rnd
fpsetround(fp_rnd rnd_dir)
{
#ifdef __riscv_flen
  unsigned fsr = frsr ();
  unsigned rm = (fsr >> 5) & 0x7;
  unsigned new_rm;
  switch (rnd_dir)
    {
    case FP_RN: new_rm = 0;
    case FP_RZ: new_rm = 1;
    case FP_RM: new_rm = 2;
    case FP_RP: new_rm = 3;
    default:    return -1;
    }
  fssr (new_rm << 5 | fsr & 0x1f);
  return frm_fp_rnd (rm);
#else
  return -1;
#endif /* __riscv_flen */
}

fp_except
fpsetsticky(fp_except sticky)
{
#ifdef __riscv_flen
  unsigned fsr = frsr ();
  fssr (sticky & 0x1f | fsr & ~0x1f);
  return fsr & 0x1f;
#else
  return -1;
#endif /* __riscv_flen */
}