Welcome to mirror list, hosted at ThFree Co, Russian Federation.

ieeefp.c « riscv « machine « libc « newlib - cygwin.com/git/newlib-cygwin.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 60ecacfc2914346cca1798d39111d217de76cae6 (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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
/* 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 FreeBSD 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 ("fscsr %0" :: "r"(value));
}

static unsigned
frsr()
{
  unsigned value;
  asm volatile ("frcsr %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;
    }
}

static fp_except
frm_fp_except (unsigned except)
{
  fp_except fp = 0;
  if (except & (1 << 0))
    fp |= FP_X_IMP;
  if (except & (1 << 1))
    fp |= FP_X_UFL;
  if (except & (1 << 2))
    fp |= FP_X_OFL;
  if (except & (1 << 3))
    fp |= FP_X_DX;
  if (except & (1 << 4))
    fp |= FP_X_INV;
  return fp;
}

static unsigned
frm_except(fp_except fp)
{
  unsigned except = 0;
  if (fp & FP_X_IMP)
    except |= (1 << 0);
  if (fp & FP_X_UFL)
    except |= (1 << 1);
  if (fp & FP_X_OFL)
    except |= (1 << 2);
  if (fp & FP_X_DX)
    except |= (1 << 3);
  if (fp & FP_X_INV)
    except |= (1 << 4);
  return except;
}

#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 frm_fp_except(frsr ());
#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; break;
    case FP_RZ: new_rm = 1; break;
    case FP_RM: new_rm = 2; break;
    case FP_RP: new_rm = 3; break;
    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 (frm_except(sticky) | (fsr & ~0x1f));
  return frm_fp_except(fsr);
#else
  return -1;
#endif /* __riscv_flen */
}