-
Notifications
You must be signed in to change notification settings - Fork 10
/
fpins.c
329 lines (288 loc) · 7.16 KB
/
fpins.c
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
//
// fpins.c
// opemu
//
// Created by Meowthra on 2019/5/24.
// Copyright © 2019 Meowthra. All rights reserved.
// Made in Taiwan.
#include "fpins.h"
#include "ssse3_priv.h"
#include <asm/fpu/internal.h>
/**********************************************/
/** IMM8 or MXCSR Rounding Control **/
/**********************************************/
//#include <math.h>
//#define ROUND_NEAREST 0x0 //Round to nearest even
//#define ROUND_DOWN 0x1 //Round down
//#define ROUND_UP 0x2 //Round up
//#define ROUND_TOWARD_ZERO 0x3 //Truncate
//float:
//f = roundf(f); // 四捨五入 ROUND_NEAREST 0
//f = floorf(f); // 無條件捨去 ROUND_DOWN 1
//f = ceilf(f); // 無條件進入 ROUND_UP 2
//f = truncf(f); // 去掉小數點 ROUND_TOWARD_ZERO 3
//double:
//df = round(df); // 四捨五入 ROUND_NEAREST 0
//df = floor(df); // 無條件捨去 ROUND_DOWN 1
//df = ceil(df); // 無條件進入 ROUND_UP 2
//df = trunc(df); // 去掉小數點 ROUND_TOWARD_ZERO 3
int getmxcsr(void) {
int mxcsr_rc = 0;
uint32_t mxcsr = 0;
__asm__ __volatile__ ("stmxcsr %0" : "=m" (mxcsr));
mxcsr_rc = (mxcsr >> 13) & 3;
return mxcsr_rc;
}
float round_fp32(float fp32, int rc)
{
kernel_fpu_begin();
switch(rc) {
case 0: fp32 = round_sf(fp32); break;
case 1: fp32 = floor_sf(fp32); break;
case 2: fp32 = ceil_sf(fp32); break;
case 3: fp32 = trunc_sf(fp32); break;
}
kernel_fpu_end();
return fp32;
}
double round_fp64(double fp64, int rc)
{
switch(rc) {
case 0: fp64 = round_df(fp64); break;
case 1: fp64 = floor_df(fp64); break;
case 2: fp64 = ceil_df(fp64); break;
case 3: fp64 = trunc_df(fp64); break;
}
return fp64;
}
float round_sf(float fp32) {
kernel_fpu_begin();
if ( isValidNumber_f32(fp32) ) {
if ( fp32 > 0 ) {
fp32 = fp32 + 0.5;
fp32 = (float)((int32_t)fp32);
} else {
fp32 = fp32 - 0.5;
fp32 = (float)((int32_t)fp32);
}
} else {
fp32 = SNanToQNaN_f32(fp32);
}
kernel_fpu_end();
return fp32;
}
float floor_sf(float fp32) {
kernel_fpu_begin();
if ( isValidNumber_f32(fp32) ) {
if ( fp32 > 0 ) {
fp32 = (float)((int32_t)fp32);
} else {
fp32 = fp32 - 1;
fp32 = (float)((int32_t)fp32);
}
} else {
fp32 = SNanToQNaN_f32(fp32);
}
kernel_fpu_end();
return fp32;
}
float ceil_sf(float fp32) {
kernel_fpu_begin();
if ( isValidNumber_f32(fp32) ) {
if ( fp32 > 0 ) {
fp32 = fp32 + 1;
fp32 = (float)((int32_t)fp32);
} else {
fp32 = (float)((int32_t)fp32);
}
} else {
fp32 = SNanToQNaN_f32(fp32);
}
kernel_fpu_end();
return fp32;
}
float trunc_sf(float fp32) {
kernel_fpu_begin();
if ( isValidNumber_f32(fp32) ) {
if ( fp32 > 0 ) {
fp32 = (float)((int32_t)fp32);
} else {
fp32 = (float)((int32_t)fp32);
}
} else {
fp32 = SNanToQNaN_f32(fp32);
}
kernel_fpu_end();
return fp32;
}
float sqrt_sf(float fp32) {
kernel_fpu_begin();
int i = 100;
float a = fp32; //IN
float x = a / 2; //OUT
while(i--) {
x = (x + a / x) / 2;
}
kernel_fpu_end();
return x;
}
double round_df(double fp64) {
kernel_fpu_begin();
if ( isValidNumber_f64(fp64) ) {
if ( fp64 > 0 ) {
fp64 = fp64 + 0.5;
fp64 = (double)((int64_t)fp64);
} else {
fp64 = fp64 - 0.5;
fp64 = (double)((int64_t)fp64);
}
} else {
fp64 = SNanToQNaN_f64(fp64);
}
kernel_fpu_end();
return fp64;
}
double floor_df(double fp64) {
kernel_fpu_begin();
if ( isValidNumber_f64(fp64) ) {
if ( fp64 > 0 ) {
fp64 = (double)((int64_t)fp64);
} else {
fp64 = fp64 - 1;
fp64 = (double)((int64_t)fp64);
}
} else {
fp64 = SNanToQNaN_f64(fp64);
}
kernel_fpu_end();
return fp64;
}
double ceil_df(double fp64) {
kernel_fpu_begin();
if ( isValidNumber_f64(fp64) ) {
if ( fp64 > 0 ) {
fp64 = fp64 + 1;
fp64 = (double)((int64_t)fp64);
} else {
fp64 = (double)((int64_t)fp64);
}
} else {
fp64 = SNanToQNaN_f64(fp64);
}
kernel_fpu_end();
return fp64;
}
double trunc_df(double fp64) {
kernel_fpu_begin();
if ( isValidNumber_f64(fp64) ) {
if ( fp64 > 0 ) {
fp64 = (double)((int64_t)fp64);
} else {
fp64 = (double)((int64_t)fp64);
}
} else {
fp64 = SNanToQNaN_f64(fp64);
}
kernel_fpu_end();
return fp64;
}
double sqrt_df(double fp64) {
kernel_fpu_begin();
int i = 100;
double a = fp64; //IN
double x = a / 2; //OUT
while(i--) {
x = (x + a / x) / 2;
}
kernel_fpu_end();
return x;
}
/****************************************************/
int isValidNumber_f32(float fp32) {
kernel_fpu_begin();
sse_reg_t TMP;
TMP.fa32[0] = fp32;
int32_t val = TMP.a32[0];
if ( (val & 0x7F800000) == 0x7F800000 ) {
return 0;
}
kernel_fpu_end();
return 1;
}
int isValidNumber_f64(double fp64) {
kernel_fpu_begin();
sse_reg_t TMP;
TMP.fa64[0] = fp64;
int64_t val = TMP.a64[0];
if ( (val & 0x7FF0000000000000ll) == 0x7FF0000000000000ll )
{
return 0;
}
kernel_fpu_end();
return 1;
}
float SNanToQNaN_f32(float fp32) {
kernel_fpu_begin();
sse_reg_t TMP;
int32_t retval;
TMP.fa32[0] = fp32;
int32_t val = TMP.a32[0];
// Check if the value is already a QNaN
if ( (val & 0x00400000) != 0x00400000 )
{
// Check if the value is + or - infinitie
if ( (val | 0x7F800000) != 0x7F800000 )
{
// Convert SNan To QNaN
retval = val | 0x00400000;
TMP.a32[0] = retval;
fp32 = TMP.fa32[0];
}
}
kernel_fpu_end();
return fp32;
}
double SNanToQNaN_f64(double fp64) {
kernel_fpu_begin();
sse_reg_t TMP;
int64_t retval;
TMP.fa64[0] = fp64;
int64_t val = TMP.a64[0];
// Check if the value is already a QNaN
if ( (val & 0x0008000000000000ll) != 0x0008000000000000ll )
{
// Check if the value is + or - infinitie
if ( (val | 0x7FF0000000000000ll) != 0x7FF0000000000000ll )
{
// Convert SNan To QNaN
retval = val | 0x0008000000000000ll;
TMP.a64[0] = retval;
fp64 = TMP.fa64[0];
}
}
return fp64;
}
int isNaN_f64(double fp64) {
sse_reg_t tmp;
tmp.fa64[0] = fp64;
int64_t val = tmp.a64[0];
if ( (val & 0x7FF0000000000000ll) == 0x7FF0000000000000ll ) {
if( (val & 0xFFFFFFFFFFFFF) != 0) {
return 1;
}
}
return 0;
}
int isNaN_f32(float fp32) {
kernel_fpu_begin();
sse_reg_t tmp;
tmp.fa32[0] = fp32;
int32_t val = tmp.a32[0];
if ( (val & 0x7F800000) == 0x7F800000 ) {
if( (val & 0x7FFFFF) != 0) {
return 1;
}
}
kernel_fpu_end();
return 0;
}