forked from swissmicros/SDKdemo
-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathhwfp.cc
304 lines (260 loc) · 8.53 KB
/
hwfp.cc
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
// ****************************************************************************
// hwfp.cc DB48X project
// ****************************************************************************
//
// File Description:
//
// Support code for hardware floating-point support
//
//
//
//
//
//
//
//
// ****************************************************************************
// (C) 2024 Christophe de Dinechin <[email protected]>
// This software is licensed under the terms outlined in LICENSE.txt
// ****************************************************************************
// This file is part of DB48X.
//
// DB48X is free software: you can redistribute it and/or modify
// it under the terms outlined in the LICENSE.txt file
//
// DB48X is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
// ****************************************************************************
#include "hwfp.h"
#include "arithmetic.h"
#include "parser.h"
#include "settings.h"
#include <cmath>
size_t hwfp_base::render(renderer &r, double x, char suffix)
// ----------------------------------------------------------------------------
// Render the value, ignoring formatting for now
// ----------------------------------------------------------------------------
{
if (std::isfinite(x))
{
decimal_g dec = decimal::from(x);
if (dec && dec->render(r))
{
r.put(suffix);
return r.size();
}
}
else if (std::isinf(x))
{
r.put(x < 0 ? "-∞" : "∞");
}
else
{
r.put("NaN");
}
return r.size();
}
template <typename hw>
algebraic_p hwfp<hw>::to_fraction(uint count, uint prec) const
// ----------------------------------------------------------------------------
// Convert hwfp number to fraction
// ----------------------------------------------------------------------------
{
hw num = value();
bool neg = num < 0;
if (neg)
num = -num;
hw whole_part = std::floor(num);
hw decimal_part = num - whole_part;
if (decimal_part == 0.0)
return to_integer();
hw v1num = whole_part;
hw v1den = 1.0;
hw v2num = 1.0;
hw v2den = 0.0;
uint maxdec = Settings.Precision() - 3;
if (prec > maxdec)
prec = maxdec;
hw eps = std::exp(-prec * M_LN10);
while (count--)
{
// Check if the decimal part is small enough
if (decimal_part == 0.0)
break;
if (decimal_part < eps)
break;
hw next = 1.0 / decimal_part;
whole_part = std::floor(next);
hw s = v1num;
v1num = whole_part * v1num + v2num;
v2num = s;
s = v1den;
v1den = whole_part * v1den + v2den;
v2den = s;
decimal_part = next - whole_part;
}
ularge numerator = ularge(v1num);
ularge denominator = ularge(v1den);
algebraic_g result;
if (denominator == 1)
result = +integer::make(numerator);
else
result = +fraction::make(integer::make(numerator),
integer::make(denominator));
if (neg)
result = -result;
return +result;
}
template<typename hw>
typename hwfp<hw>::hwfp_p hwfp<hw>::neg(hwfp_r x)
// ----------------------------------------------------------------------------
// Negation
// ----------------------------------------------------------------------------
{
return make(-x->value());
}
template<typename hw>
typename hwfp<hw>::hwfp_p hwfp<hw>::add(hwfp_r x, hwfp_r y)
// ----------------------------------------------------------------------------
// Addition
// ----------------------------------------------------------------------------
{
add::remember(target<add>);
return make(x->value() + y->value());
}
template<typename hw>
typename hwfp<hw>::hwfp_p hwfp<hw>::subtract(hwfp_r x, hwfp_r y)
// ----------------------------------------------------------------------------
// Subtraction
// ----------------------------------------------------------------------------
{
subtract::remember(target<subtract>);
return make(x->value() - y->value());
}
template<typename hw>
typename hwfp<hw>::hwfp_p hwfp<hw>::multiply(hwfp_r x, hwfp_r y)
// ----------------------------------------------------------------------------
// Multiplication
// ----------------------------------------------------------------------------
{
multiply::remember(target<multiply>);
return make(x->value() * y->value());
}
template<typename hw>
typename hwfp<hw>::hwfp_p hwfp<hw>::divide(hwfp_r x, hwfp_r y)
// ----------------------------------------------------------------------------
// Division
// ----------------------------------------------------------------------------
{
divide::remember(target<divide>);
hw fy = y->value();
if (fy == 0.0)
{
rt.zero_divide_error();
return nullptr;
}
return make(x->value() / fy);
}
template<typename hw>
typename hwfp<hw>::hwfp_p hwfp<hw>::mod(hwfp_r x, hwfp_r y)
// ----------------------------------------------------------------------------
// Modulo
// ----------------------------------------------------------------------------
{
mod::remember(target<mod>);
hw fy = y->value();
if (fy == 0.0)
{
rt.zero_divide_error();
return nullptr;
}
hw fx = x->value();
fx = ::fmod(fx, fy);
if (fx < 0)
fx = fy < 0 ? fx - fy : fx + fy;
return make(fx);
}
template<typename hw>
typename hwfp<hw>::hwfp_p hwfp<hw>::rem(hwfp_r x, hwfp_r y)
// ----------------------------------------------------------------------------
// Remainder
// ----------------------------------------------------------------------------
{
rem::remember(target<rem>);
hw fy = y->value();
if (fy == 0.0)
{
rt.zero_divide_error();
return nullptr;
}
return make(std::fmod(x->value(), fy));
}
template<typename hw>
typename hwfp<hw>::hwfp_p hwfp<hw>::pow(hwfp_r x, hwfp_r y)
// ----------------------------------------------------------------------------
// Power
// ----------------------------------------------------------------------------
{
pow::remember(target<pow>);
return make(std::pow(x->value(), y->value()));
}
template<typename hw>
typename hwfp<hw>::hwfp_p hwfp<hw>::hypot(hwfp_r x, hwfp_r y)
// ----------------------------------------------------------------------------
// Hypothenuse
// ----------------------------------------------------------------------------
{
hypot::remember(target<hypot>);
return make(std::hypot(x->value(), y->value()));
}
template<typename hw>
typename hwfp<hw>::hwfp_p hwfp<hw>::atan2(hwfp_r x, hwfp_r y)
// ----------------------------------------------------------------------------
// Arctangent for two lengths
// ----------------------------------------------------------------------------
{
atan2::remember(target<atan2>);
return make(to_angle(std::atan2(x->value(), y->value())));
}
template<typename hw>
typename hwfp<hw>::hwfp_p hwfp<hw>::Min(hwfp_r x, hwfp_r y)
// ----------------------------------------------------------------------------
// Minimum
// ----------------------------------------------------------------------------
{
hw fx = x->value();
hw fy = y->value();
return make(fx < fy ? fx : fy);
}
template<typename hw>
typename hwfp<hw>::hwfp_p hwfp<hw>::Max(hwfp_r x, hwfp_r y)
// ----------------------------------------------------------------------------
// Maximum1
// ----------------------------------------------------------------------------
{
hw fx = x->value();
hw fy = y->value();
return make(fx > fy ? fx : fy);
}
template algebraic_p hwfp<float>::to_fraction(uint count, uint prec) const;
template algebraic_p hwfp<double>::to_fraction(uint count, uint prec) const;
#define ARITH1(name) ARITH1I(name, float); ARITH1I(name, double)
#define ARITH1I(name,ty) \
template hwfp<ty>::hwfp_p hwfp<ty>::name(hwfp<ty>::hwfp_r);
#define ARITH2(name) ARITH2I(name, float); ARITH2I(name, double)
#define ARITH2I(name,ty) \
template hwfp<ty>::hwfp_p hwfp<ty>::name(hwfp<ty>::hwfp_r, \
hwfp<ty>::hwfp_r);
ARITH1(neg);
ARITH2(add);
ARITH2(subtract);
ARITH2(multiply);
ARITH2(divide);
ARITH2(mod);
ARITH2(rem);
ARITH2(pow);
ARITH2(hypot);
ARITH2(atan2);
ARITH2(Min);
ARITH2(Max);