author | Claus Gittinger <cg@exept.de> |
Sun, 01 Dec 2019 05:26:20 +0100 | |
changeset 5328 | f1d46108f6f8 |
parent 5295 | 9ddaf6f44007 |
child 5337 | 390df14ceffd |
permissions | -rw-r--r-- |
4983 | 1 |
"{ Package: 'stx:libbasic2' }" |
2 |
||
3 |
"{ NameSpace: Smalltalk }" |
|
4 |
||
5 |
LimitedPrecisionReal variableByteSubclass:#QuadFloat |
|
6 |
instanceVariableNames:'' |
|
7 |
classVariableNames:'QuadFloatZero QuadFloatOne Pi E Epsilon NaN PositiveInfinity |
|
5269
83163a7005f1
Fallback to version 1.36 - mingw couldn't compile
Stefan Vogel <sv@exept.de>
parents:
5268
diff
changeset
|
8 |
NegativeInfinity Halfpi HalfpiNegative Phi' |
4983 | 9 |
poolDictionaries:'' |
10 |
category:'Magnitude-Numbers' |
|
11 |
! |
|
12 |
||
4991 | 13 |
!QuadFloat primitiveDefinitions! |
14 |
%{ |
|
5287 | 15 |
#ifdef __xxosx__ |
16 |
# define SUPPORT_QUADFLOAT |
|
17 |
#endif |
|
5027 | 18 |
|
5276 | 19 |
#include <math.h> |
4998 | 20 |
|
5276 | 21 |
extern float128_t STX_addq(float128_t, float128_t, int); |
22 |
extern float128_t STX_mulq(float128_t, float128_t); |
|
23 |
extern float128_t STX_divq(float128_t, float128_t); |
|
24 |
extern float128_t STX_negq(float128_t); |
|
25 |
extern float128_t STX_absq(float128_t); |
|
26 |
extern float128_t STX_floorq(float128_t); |
|
27 |
extern float128_t STX_frexpq(float128_t, int*); |
|
28 |
extern float128_t STX_ceilq(float128_t); |
|
29 |
extern float128_t STX_logq(float128_t); |
|
30 |
extern float128_t STX_log10q(float128_t); |
|
31 |
extern float128_t STX_log2q(float128_t); |
|
32 |
extern float128_t STX_expq(float128_t); |
|
33 |
extern float128_t STX_sinq(float128_t); |
|
34 |
extern float128_t STX_cosq(float128_t); |
|
35 |
extern float128_t STX_tanq(float128_t); |
|
36 |
extern float128_t STX_sinhq(float128_t); |
|
37 |
extern float128_t STX_coshq(float128_t); |
|
38 |
extern float128_t STX_tanhq(float128_t); |
|
39 |
extern float128_t STX_asinq(float128_t); |
|
40 |
extern float128_t STX_acosq(float128_t); |
|
41 |
extern float128_t STX_atanq(float128_t); |
|
42 |
extern float128_t STX_asinhq(float128_t); |
|
43 |
extern float128_t STX_acoshq(float128_t); |
|
44 |
extern float128_t STX_atanhq(float128_t); |
|
45 |
extern float128_t STX_qZero; |
|
46 |
extern float128_t STX_dbltoq(double); |
|
47 |
extern float128_t STX_inttoq(long); |
|
48 |
extern double STX_qtodbl(float128_t); |
|
49 |
extern int STX_isNanq(float128_t*); |
|
50 |
extern int STX_prcmpq(float128_t*, float128_t*); |
|
5013 | 51 |
|
5276 | 52 |
#define STX_isfiniteq(q) (!STX_isNanq(&(q)) && !STX_isInfq(&(q))) |
53 |
#define STX_eqq(x1, x2) (STX_prcmpq (&(x1), &(x2)) == 0) |
|
54 |
#define STX_neqq(x1, x2) (STX_prcmpq (&(x1), &(x2)) != 0) |
|
55 |
#define STX_gtq(x1, x2) (STX_prcmpq (&(x1), &(x2)) > 0) |
|
56 |
#define STX_geq(x1, x2) (STX_prcmpq (&(x1), &(x2)) >= 0) |
|
57 |
#define STX_ltq(x1, x2) (STX_prcmpq (&(x1), &(x2)) < 0) |
|
58 |
#define STX_leq(x1, x2) (STX_prcmpq (&(x1), &(x2)) <= 0) |
|
4992 | 59 |
|
5276 | 60 |
#ifdef __win32__ |
61 |
/* |
|
62 |
* no finite(x) ? |
|
63 |
* no isnan(x) ? |
|
64 |
*/ |
|
65 |
# ifndef isnan |
|
66 |
# define isnan(x) \ |
|
67 |
((((unsigned int *)(&x))[0] == 0x00000000) && \ |
|
68 |
(((unsigned int *)(&x))[1] == 0xFFF80000)) |
|
69 |
# endif |
|
4992 | 70 |
|
5276 | 71 |
# ifndef isPositiveInfinity |
72 |
# define isPositiveInfinity(x) \ |
|
73 |
((((unsigned int *)(&x))[0] == 0x00000000) && \ |
|
74 |
(((unsigned int *)(&x))[1] == 0x7FF00000)) |
|
75 |
# endif |
|
4992 | 76 |
|
5276 | 77 |
# ifndef isNegativeInfinity |
78 |
# define isNegativeInfinity(x) \ |
|
79 |
((((unsigned int *)(&x))[0] == 0x00000000) && \ |
|
80 |
(((unsigned int *)(&x))[1] == 0xFFF00000)) |
|
81 |
# endif |
|
5014 | 82 |
|
5276 | 83 |
# ifndef isinf |
84 |
# define isinf(x) \ |
|
85 |
((((unsigned int *)(&x))[0] == 0x00000000) && \ |
|
86 |
((((unsigned int *)(&x))[1] & 0x7FF00000) == 0x7FF00000)) |
|
87 |
# endif |
|
4992 | 88 |
|
5276 | 89 |
# ifndef isfinite |
90 |
# define isfinite(x) (!isinf(x) && !isnan(x)) |
|
91 |
# endif |
|
92 |
||
93 |
#else // not win32 |
|
5269
83163a7005f1
Fallback to version 1.36 - mingw couldn't compile
Stefan Vogel <sv@exept.de>
parents:
5268
diff
changeset
|
94 |
#endif |
4993 | 95 |
|
4992 | 96 |
%} |
97 |
! ! |
|
98 |
||
99 |
!QuadFloat primitiveVariables! |
|
5267 | 100 |
%{ |
101 |
%} |
|
4991 | 102 |
! ! |
103 |
||
4992 | 104 |
!QuadFloat primitiveFunctions! |
5267 | 105 |
%{ |
106 |
%} |
|
4992 | 107 |
! ! |
4991 | 108 |
|
4983 | 109 |
!QuadFloat class methodsFor:'documentation'! |
110 |
||
111 |
documentation |
|
112 |
" |
|
113 |
QuadFloats represent rational numbers with limited precision |
|
5007 | 114 |
and are mapped to IEEE quadruple precision format (128bit), |
115 |
also called binary128. |
|
116 |
||
4983 | 117 |
If the underlying cpu supports them natively, the machine format (long double) is |
118 |
used. Otherwise, a software emulation is done, which is much slower. |
|
119 |
Thus only use them, if you really need the additional precision; |
|
120 |
if not, use Float (which are doubles) or LongFloats which usually have IEEE extended precision (80bit). |
|
121 |
||
122 |
QuadFloats give you definite 128 bit quadruple floats, |
|
123 |
thus, code using quadFloats is guaranteed to be portable from one architecture to another. |
|
124 |
||
125 |
Representation: |
|
4991 | 126 |
128bit quadruple IEEE floats (16bytes); |
127 |
112 bit mantissa, |
|
128 |
16 bit exponent, |
|
129 |
34 decimal digits (approx.) |
|
4983 | 130 |
|
4992 | 131 |
On Sparc CPUs, this is a native supported type (long double) and fast; |
132 |
on x86 CPUs, this is emulated and slow. |
|
133 |
||
4983 | 134 |
Mixed mode arithmetic: |
5007 | 135 |
quadFloat op anyFloat -> quadFloat |
136 |
anyFloat op quadFloat -> quadFloat |
|
4983 | 137 |
|
138 |
Range and precision of storage formats: see LimitedPrecisionReal >> documentation |
|
139 |
||
140 |
[author:] |
|
4991 | 141 |
Claus Gittinger |
4983 | 142 |
|
143 |
[see also:] |
|
4991 | 144 |
Number |
145 |
Float ShortFloat LongFloat Fraction FixedPoint Integer Complex |
|
146 |
FloatArray DoubleArray |
|
147 |
https://en.wikipedia.org/wiki/Extended_precision |
|
4983 | 148 |
" |
149 |
! ! |
|
150 |
||
151 |
!QuadFloat class methodsFor:'instance creation'! |
|
152 |
||
153 |
basicNew |
|
154 |
"return a new quadFloat - here we return 0.0 |
|
155 |
- QuadFloats are usually NOT created this way ... |
|
156 |
Its implemented here to allow things like binary store & load |
|
157 |
of quadFloats. |
|
158 |
(but it is not a good idea to store the bits of a float - the reader might have a |
|
159 |
totally different representation - so floats should be |
|
160 |
binary stored in a device independent format)." |
|
161 |
||
162 |
%{ /* NOCONTEXT */ |
|
4993 | 163 |
#ifdef SUPPORT_QUADFLOAT |
4983 | 164 |
OBJ newFloat; |
4991 | 165 |
|
166 |
if (sizeof(long double) == sizeof(float128_t)) { |
|
167 |
__qMKLFLOAT(newFloat, 0.0); /* OBJECT ALLOCATION */ |
|
4983 | 168 |
} else { |
5276 | 169 |
float128_t qf = STX_qZero; |
4991 | 170 |
__qMKQFLOAT(newFloat, qf); /* OBJECT ALLOCATION */ |
4983 | 171 |
} |
172 |
RETURN (newFloat); |
|
4993 | 173 |
#endif /* SUPPORT_QUADFLOAT */ |
174 |
%}. |
|
175 |
self error:'QuadFloats not supported on this patform' |
|
4983 | 176 |
|
177 |
"Created: / 06-06-2019 / 17:18:58 / Claus Gittinger" |
|
178 |
! |
|
179 |
||
180 |
fromFloat:aFloat |
|
181 |
"return a new quadFloat, given a float value" |
|
182 |
||
183 |
%{ /* NOCONTEXT */ |
|
4993 | 184 |
#ifdef SUPPORT_QUADFLOAT |
4983 | 185 |
OBJ newFloat; |
186 |
||
187 |
if (__isFloatLike(aFloat)) { |
|
5290 | 188 |
double f = __floatVal(aFloat); |
189 |
float128_t qf; |
|
4991 | 190 |
|
5290 | 191 |
qf = STX_dbltoq (f); |
192 |
__qMKQFLOAT(newFloat, qf); /* OBJECT ALLOCATION */ |
|
193 |
RETURN (newFloat); |
|
4983 | 194 |
} |
5287 | 195 |
#endif /* |
196 |
*/ |
|
4983 | 197 |
%}. |
4993 | 198 |
aFloat isFloat ifTrue:[ |
5290 | 199 |
self errorUnsupported. |
200 |
^ aFloat asLongFloat |
|
4993 | 201 |
]. |
4997 | 202 |
ArgumentError raise |
4983 | 203 |
|
204 |
" |
|
205 |
QuadFloat fromFloat:123.0 |
|
206 |
123.0 asQuadFloat |
|
207 |
123 asQuadFloat |
|
208 |
" |
|
209 |
||
210 |
"Created: / 06-06-2019 / 18:01:03 / Claus Gittinger" |
|
5011 | 211 |
! |
212 |
||
5017 | 213 |
fromInteger:anInteger |
214 |
"return a new quadFloat, given an integer value" |
|
215 |
||
216 |
%{ /* NOCONTEXT */ |
|
217 |
#ifdef SUPPORT_QUADFLOAT |
|
218 |
OBJ newFloat; |
|
219 |
||
220 |
if (__isSmallInteger(anInteger)) { |
|
221 |
INT iVal = __intVal(anInteger); |
|
222 |
float128_t qf; |
|
223 |
||
5276 | 224 |
qf = STX_inttoq( (long)iVal ); |
5017 | 225 |
__qMKQFLOAT(newFloat, qf); /* OBJECT ALLOCATION */ |
226 |
RETURN (newFloat); |
|
227 |
} |
|
228 |
#endif /* SUPPORT_QUADFLOAT */ |
|
229 |
%}. |
|
230 |
^ super fromInteger:anInteger |
|
231 |
||
232 |
" |
|
233 |
QuadFloat fromInteger:123 |
|
234 |
123 asQuadFloat |
|
235 |
" |
|
236 |
! |
|
237 |
||
5014 | 238 |
fromLongFloat:aFloat |
239 |
"return a new quadFloat, given a long float value" |
|
240 |
||
241 |
%{ /* NOCONTEXT */ |
|
5276 | 242 |
#ifdef xSUPPORT_QUADFLOAT |
5014 | 243 |
OBJ newFloat; |
244 |
union { |
|
5290 | 245 |
LONGFLOAT_t lf; // is long double |
246 |
extFloat80_t ef; // is 80bit ext |
|
247 |
float128_t qf; // is 128bit quad |
|
5014 | 248 |
} u; |
249 |
||
250 |
if (__isLongFloat(aFloat)) { |
|
5290 | 251 |
u.lf = __longFloatVal(aFloat); |
5014 | 252 |
|
5290 | 253 |
if (sizeof(LONGFLOAT_t) == 16) { |
254 |
// longFloat is already 128 bits in size (sparc) |
|
255 |
__qMKQFLOAT(newFloat, u.qf); /* OBJECT ALLOCATION */ |
|
256 |
RETURN (newFloat); |
|
257 |
} |
|
258 |
if (sizeof(LONGFLOAT_t) < 16) { |
|
259 |
// assume 80bit extended float format (amd64, x86_64) |
|
260 |
u.qf = extF80_to_f128( u.ef); |
|
261 |
__qMKQFLOAT(newFloat, u.qf); /* OBJECT ALLOCATION */ |
|
262 |
RETURN (newFloat); |
|
263 |
} |
|
264 |
// fall into error case |
|
5014 | 265 |
} |
266 |
#endif /* SUPPORT_QUADFLOAT */ |
|
267 |
%}. |
|
268 |
aFloat isLongFloat ifTrue:[ |
|
5290 | 269 |
self errorUnsupported. |
270 |
^ aFloat asLongFloat |
|
5014 | 271 |
]. |
272 |
ArgumentError raise |
|
273 |
||
274 |
" |
|
275 |
QuadFloat fromLongFloat:123.0 asLongFloat |
|
276 |
" |
|
277 |
! |
|
278 |
||
5011 | 279 |
fromShortFloat:aShortFloat |
280 |
"return a new quadFloat, given a float value" |
|
281 |
||
5013 | 282 |
^ self fromFloat:(aShortFloat asFloat) |
5011 | 283 |
|
284 |
" |
|
285 |
QuadFloat fromShortFloat:123.0 asShortFloat |
|
286 |
" |
|
287 |
||
288 |
"Created: / 08-06-2019 / 03:28:37 / Claus Gittinger" |
|
4983 | 289 |
! ! |
290 |
||
291 |
!QuadFloat class methodsFor:'coercing & converting'! |
|
292 |
||
293 |
coerce:aNumber |
|
5328 | 294 |
"convert the argument aNumber into an instance of the receiver (class) and return it." |
4983 | 295 |
|
296 |
^ aNumber asQuadFloat. |
|
297 |
||
298 |
"Created: / 06-06-2019 / 16:51:01 / Claus Gittinger" |
|
299 |
! ! |
|
300 |
||
301 |
!QuadFloat class methodsFor:'constants'! |
|
302 |
||
303 |
NaN |
|
5011 | 304 |
"return a quadFloat which represents not-a-Number (i.e. an invalid number)" |
305 |
||
306 |
|nan| |
|
4983 | 307 |
|
308 |
NaN isNil ifTrue:[ |
|
5011 | 309 |
%{ /* NOCONTEXT */ |
5276 | 310 |
#ifdef xSUPPORT_QUADFLOAT |
5011 | 311 |
{ |
312 |
OBJ newFloat; |
|
313 |
float128_t qf; |
|
314 |
||
5014 | 315 |
softfloat_commonNaNToF128M( (uint32_t*)(&qf) ); |
5011 | 316 |
__qMKQFLOAT(newFloat, qf); /* OBJECT ALLOCATION */ |
317 |
nan = newFloat; |
|
318 |
} |
|
319 |
#endif /* SUPPORT_QUADFLOAT */ |
|
320 |
%}. |
|
5287 | 321 |
nan isNil ifTrue:[ |
322 |
self errorUnsupported |
|
323 |
]. |
|
5011 | 324 |
NaN := nan |
4983 | 325 |
]. |
326 |
^ NaN |
|
327 |
! |
|
328 |
||
329 |
e |
|
330 |
"return the constant e as quadFloat" |
|
331 |
||
332 |
E isNil ifTrue:[ |
|
4991 | 333 |
"/ eDigits has enough digits for 128bit IEEE quads |
334 |
"/ do not use as a literal constant here - we cannot depend on the underlying C-compiler here... |
|
335 |
E := self readFrom:(Number eDigits) |
|
4983 | 336 |
]. |
337 |
^ E |
|
338 |
||
339 |
"Created: / 06-06-2019 / 17:01:54 / Claus Gittinger" |
|
340 |
! |
|
341 |
||
5017 | 342 |
infinity |
343 |
"return a quadFloat which represents +INF" |
|
344 |
||
345 |
|inf| |
|
346 |
||
347 |
PositiveInfinity isNil ifTrue:[ |
|
348 |
%{ /* NOCONTEXT */ |
|
5276 | 349 |
#ifdef xSUPPORT_QUADFLOAT |
5017 | 350 |
{ |
351 |
OBJ newFloat; |
|
352 |
struct uint128 uiZ; |
|
353 |
union ui128_f128 uZ; |
|
354 |
float128_t qf; |
|
355 |
||
356 |
uiZ.v64 = packToF128UI64( 0, 0x7FFF, 0 ); |
|
357 |
uiZ.v0 = 0; |
|
358 |
uZ.ui = uiZ; |
|
359 |
qf = uZ.f; |
|
360 |
__qMKQFLOAT(newFloat, qf); /* OBJECT ALLOCATION */ |
|
361 |
inf = newFloat; |
|
362 |
} |
|
363 |
#endif /* SUPPORT_QUADFLOAT */ |
|
364 |
%}. |
|
5287 | 365 |
inf isNil ifTrue:[ |
366 |
self errorUnsupported |
|
367 |
]. |
|
5017 | 368 |
PositiveInfinity := inf |
369 |
]. |
|
370 |
^ PositiveInfinity |
|
371 |
||
372 |
"Created: / 08-06-2019 / 14:05:26 / Claus Gittinger" |
|
373 |
! |
|
374 |
||
5012 | 375 |
negativeInfinity |
376 |
"return a quadFloat which represents -INF" |
|
377 |
||
378 |
|inf| |
|
379 |
||
380 |
NegativeInfinity isNil ifTrue:[ |
|
381 |
%{ /* NOCONTEXT */ |
|
5276 | 382 |
#ifdef xSUPPORT_QUADFLOAT |
5013 | 383 |
{ |
384 |
OBJ newFloat; |
|
385 |
struct uint128 uiZ; |
|
386 |
union ui128_f128 uZ; |
|
387 |
float128_t qf; |
|
5012 | 388 |
|
5013 | 389 |
uiZ.v64 = packToF128UI64( 1, 0x7FFF, 0 ); |
390 |
uiZ.v0 = 0; |
|
391 |
uZ.ui = uiZ; |
|
392 |
qf = uZ.f; |
|
393 |
__qMKQFLOAT(newFloat, qf); /* OBJECT ALLOCATION */ |
|
394 |
inf = newFloat; |
|
395 |
} |
|
5012 | 396 |
#endif /* SUPPORT_QUADFLOAT */ |
397 |
%}. |
|
5287 | 398 |
inf isNil ifTrue:[ |
399 |
self errorUnsupported |
|
400 |
]. |
|
5013 | 401 |
NegativeInfinity := inf |
5012 | 402 |
]. |
403 |
^ NegativeInfinity |
|
404 |
||
405 |
"Created: / 08-06-2019 / 14:05:50 / Claus Gittinger" |
|
406 |
! |
|
407 |
||
5264 | 408 |
phi |
409 |
"return the constant phi as quadFloat" |
|
410 |
||
411 |
Phi isNil ifTrue:[ |
|
5276 | 412 |
"/ phiDigits has enough digits for 128bit IEEE quads |
413 |
"/ do not use as a literal constant here - we cannot depend on the underlying C-compiler here... |
|
414 |
Phi := self readFrom:(Number phiDigits) |
|
5264 | 415 |
]. |
416 |
^ Phi |
|
417 |
! |
|
418 |
||
4983 | 419 |
pi |
420 |
"return the constant pi as quadFloat" |
|
421 |
||
422 |
Pi isNil ifTrue:[ |
|
4991 | 423 |
"/ piDigits has enough digits for 128bit IEEE quads |
424 |
"/ do not use as a literal constant here - we cannot depend on the underlying C-compiler here... |
|
425 |
Pi := self readFrom:(Number piDigits) |
|
4983 | 426 |
]. |
427 |
^ Pi |
|
428 |
||
429 |
"Created: / 06-06-2019 / 17:09:51 / Claus Gittinger" |
|
5006 | 430 |
! |
431 |
||
432 |
unity |
|
433 |
"return the neutral element for multiplication (1.0) as QuadFloat" |
|
434 |
||
435 |
QuadFloatOne isNil ifTrue:[ |
|
5007 | 436 |
QuadFloatOne := 1.0 asQuadFloat. |
5006 | 437 |
]. |
438 |
^ QuadFloatOne |
|
439 |
||
440 |
"Created: / 07-06-2019 / 03:26:38 / Claus Gittinger" |
|
5008 | 441 |
! |
442 |
||
443 |
zero |
|
444 |
"return the neutral element for addition (0.0) as QuadFloat" |
|
445 |
||
446 |
QuadFloatZero isNil ifTrue:[ |
|
5011 | 447 |
QuadFloatZero := 0.0 asQuadFloat |
5008 | 448 |
]. |
449 |
^ QuadFloatZero |
|
450 |
||
451 |
"Created: / 07-06-2019 / 09:22:56 / Claus Gittinger" |
|
4983 | 452 |
! ! |
453 |
||
5003 | 454 |
!QuadFloat class methodsFor:'error reportng'! |
4994 | 455 |
|
4997 | 456 |
errorUnsupported |
5290 | 457 |
"you may proceed from this error, to get a long float number result |
458 |
(of course, with less than expected precision)" |
|
459 |
||
460 |
self errorUnsupported:'QuadFloats not supported on this patform' |
|
4997 | 461 |
|
5003 | 462 |
"Created: / 07-06-2019 / 02:44:39 / Claus Gittinger" |
5290 | 463 |
|
464 |
" |
|
465 |
UnimplementedFunctionalityError handle:[:ex | |
|
466 |
ex proceed |
|
467 |
] do:[ |
|
468 |
1.0 asQuadFloat |
|
469 |
]. |
|
470 |
" |
|
4994 | 471 |
! ! |
472 |
||
5005 | 473 |
!QuadFloat class methodsFor:'queries'! |
474 |
||
5017 | 475 |
epsilon |
476 |
"return the maximum relative spacing of instances of mySelf |
|
477 |
(i.e. the value-delta of the least significant bit)" |
|
478 |
||
479 |
Epsilon isNil ifTrue:[ |
|
5020 | 480 |
Epsilon := self computeEpsilon. |
5017 | 481 |
]. |
482 |
^ Epsilon |
|
483 |
||
484 |
" |
|
485 |
self epsilon |
|
486 |
" |
|
487 |
||
488 |
"Created: / 10-06-2019 / 21:21:18 / Claus Gittinger" |
|
489 |
! |
|
490 |
||
5018 | 491 |
exponentCharacter |
492 |
"return the character used to print between mantissa an exponent. |
|
493 |
Also used by the scanner when reading numbers." |
|
494 |
||
495 |
^ $Q |
|
496 |
||
497 |
"Created: / 10-06-2019 / 21:28:04 / Claus Gittinger" |
|
498 |
! |
|
499 |
||
5025 | 500 |
numBitsInExponent |
5276 | 501 |
"answer the number of bits in the exponent. |
502 |
This is an IEEE 128bit quadfloat, where 15 bits are available in the exponent: |
|
5287 | 503 |
seeeeeee eeeeeeee mmmmmmmm mmmmmmmm mmmmmmmm mmmmmmmm mmmmmmmm mmmmmmmm mmmmmmmm mmmmmmmm mmmmmmmm mmmmmmmm mmmmmmmm mmmmmmmm mmmmmmmm mmmmmmmm... |
5025 | 504 |
" |
505 |
||
506 |
^ 15 |
|
507 |
||
508 |
" |
|
509 |
1.0 class numBitsInExponent -> 11 |
|
510 |
1.0 asShortFloat class numBitsInExponent -> 8 |
|
511 |
1.0 asLongFloat class numBitsInExponent -> 15 |
|
512 |
1.0 asQuadFloat class numBitsInExponent -> 15 |
|
513 |
" |
|
514 |
||
515 |
"Created: / 11-06-2019 / 00:14:55 / Claus Gittinger" |
|
516 |
! |
|
517 |
||
5005 | 518 |
numBitsInMantissa |
519 |
"answer the number of bits in the mantissa |
|
520 |
the hidden bit is not counted here: |
|
521 |
||
5328 | 522 |
This is an 128bit quadfloat, where 112 bits are available in the mantissa: |
523 |
seeeeeee eeeeeeee mmmmmmmm mmmmmmmm mmmmmmmm mmmmmmmm mmmmmmmm mmmmmmmm mmmmmmmm mmmmmmmm mmmmmmmm mmmmmmmm mmmmmmmm mmmmmmmm mmmmmmmm mmmmmmmm... |
|
5005 | 524 |
" |
525 |
||
526 |
^ 112 |
|
527 |
||
528 |
" |
|
529 |
1.0 class numBitsInMantissa |
|
530 |
1.0 asShortFloat class numBitsInMantissa |
|
531 |
1.0 asLongFloat class numBitsInMantissa |
|
532 |
1.0 asQuadFloat class numBitsInMantissa |
|
533 |
" |
|
534 |
||
535 |
"Created: / 07-06-2019 / 03:24:20 / Claus Gittinger" |
|
5056 | 536 |
! |
537 |
||
538 |
radix |
|
539 |
"answer the radix of a QuadFloat's exponent |
|
540 |
This is an IEEE float, which is represented as binary" |
|
541 |
||
542 |
^ 2 "must be careful here, whenever ST/X is used on VAX or a 370" |
|
543 |
||
544 |
"Created: / 19-07-2019 / 17:28:00 / Claus Gittinger" |
|
5005 | 545 |
! ! |
546 |
||
4994 | 547 |
!QuadFloat methodsFor:'arithmetic'! |
548 |
||
549 |
* aNumber |
|
550 |
"return the product of the receiver and the argument." |
|
551 |
||
5074 | 552 |
aNumber class == QuadFloat ifTrue:[ |
5291 | 553 |
^ aNumber productFromQuadFloat:self |
5074 | 554 |
]. |
5291 | 555 |
"/ DEBUGGING |
5075 | 556 |
thisContext isReallyRecursive ifTrue:[self error]. |
4994 | 557 |
^ aNumber productFromQuadFloat:self |
558 |
! |
|
559 |
||
560 |
+ aNumber |
|
561 |
"return the sum of the receiver and the argument, aNumber" |
|
562 |
||
563 |
^ aNumber sumFromQuadFloat:self |
|
564 |
! |
|
565 |
||
566 |
- aNumber |
|
567 |
"return the difference of the receiver and the argument, aNumber" |
|
568 |
||
569 |
^ aNumber differenceFromQuadFloat:self |
|
570 |
! |
|
571 |
||
572 |
/ aNumber |
|
573 |
"return the quotient of the receiver and the argument, aNumber" |
|
574 |
||
575 |
aNumber isZero ifTrue:[ |
|
576 |
" |
|
577 |
No, you shalt not divide by zero |
|
578 |
" |
|
579 |
^ ZeroDivide raiseRequestWith:thisContext. |
|
580 |
]. |
|
581 |
^ aNumber quotientFromQuadFloat:self |
|
582 |
! |
|
583 |
||
5276 | 584 |
abs |
585 |
"return the absolute value of the receiver |
|
586 |
reimplemented here for speed" |
|
587 |
||
588 |
%{ /* NOCONTEXT */ |
|
589 |
#ifdef SUPPORT_QUADFLOAT |
|
590 |
OBJ newFloat; |
|
591 |
float128_t result, myVal; |
|
592 |
||
593 |
myVal = __quadFloatVal(self); |
|
594 |
result = STX_absq(myVal); |
|
595 |
__qMKQFLOAT(newFloat, result); |
|
596 |
RETURN ( newFloat ); |
|
597 |
#endif |
|
598 |
%}. |
|
599 |
! |
|
600 |
||
601 |
ceiling |
|
602 |
"return the smallest integer which is greater or equal to the receiver." |
|
603 |
||
604 |
|val| |
|
605 |
||
606 |
%{ |
|
5287 | 607 |
#ifdef SUPPORT_QUADFLOAT |
5276 | 608 |
float128_t qVal; |
609 |
float128_t qMinInt; |
|
610 |
float128_t qMaxInt; |
|
611 |
||
612 |
qVal = __quadFloatVal(self); |
|
613 |
qVal = STX_ceilq(qVal); |
|
614 |
||
615 |
qMinInt = STX_dbltoq((double)_MIN_INT); |
|
616 |
qMaxInt = STX_dbltoq((double)_MAX_INT); |
|
617 |
if (STX_geq(qVal, qMinInt) && STX_leq(qVal, qMaxInt)) { |
|
618 |
double dVal = STX_qtodbl(qVal); |
|
619 |
RETURN ( __mkSmallInteger( (INT) dVal ) ); |
|
620 |
} |
|
621 |
__qMKQFLOAT(val, qVal); |
|
5287 | 622 |
#endif |
5276 | 623 |
%}. |
624 |
^ val asInteger |
|
625 |
||
626 |
" |
|
627 |
0.5 asQuadFloat ceiling |
|
628 |
0.5 asQuadFloat ceilingAsFloat |
|
629 |
-0.5 asQuadFloat ceiling |
|
630 |
-0.5 asQuadFloat ceilingAsFloat |
|
631 |
" |
|
632 |
! |
|
633 |
||
634 |
ceilingAsFloat |
|
635 |
"return the smallest integer-valued float greater or equal to the receiver. |
|
636 |
This is much like #ceiling, but avoids a (possibly expensive) conversion |
|
637 |
of the result to an integer. |
|
638 |
It may be useful, if the result is to be further used in another float-operation." |
|
639 |
||
640 |
%{ /* NOCONTEXT */ |
|
641 |
#ifdef SUPPORT_QUADFLOAT |
|
642 |
OBJ newFloat; |
|
643 |
float128_t result, myVal; |
|
644 |
||
645 |
myVal = __quadFloatVal(self); |
|
646 |
result = STX_ceilq(myVal); |
|
647 |
__qMKQFLOAT(newFloat, result); |
|
648 |
RETURN ( newFloat ); |
|
649 |
#endif |
|
650 |
%}. |
|
651 |
! |
|
652 |
||
653 |
cos |
|
654 |
"return the cosine of the receiver (interpreted as radians)" |
|
655 |
||
656 |
%{ /* NOCONTEXT */ |
|
657 |
#ifdef SUPPORT_QUADFLOAT |
|
658 |
OBJ newFloat; |
|
659 |
float128_t result, myVal; |
|
660 |
||
661 |
myVal = __quadFloatVal(self); |
|
662 |
result = STX_cosq(myVal); |
|
663 |
__qMKQFLOAT(newFloat, result); |
|
664 |
RETURN ( newFloat ); |
|
665 |
#endif |
|
666 |
%}. |
|
667 |
! |
|
668 |
||
669 |
cosh |
|
670 |
"return the hyperbolic cosine of the receiver (interpreted as radians)" |
|
671 |
||
672 |
%{ /* NOCONTEXT */ |
|
673 |
#ifdef SUPPORT_QUADFLOAT |
|
674 |
OBJ newFloat; |
|
675 |
float128_t result, myVal; |
|
676 |
||
677 |
myVal = __quadFloatVal(self); |
|
678 |
result = STX_coshq(myVal); |
|
679 |
__qMKQFLOAT(newFloat, result); |
|
680 |
RETURN ( newFloat ); |
|
681 |
#endif |
|
682 |
%}. |
|
683 |
! |
|
684 |
||
685 |
exp |
|
686 |
"return e raised to the power of the receiver" |
|
687 |
||
688 |
%{ /* NOCONTEXT */ |
|
689 |
#ifdef SUPPORT_QUADFLOAT |
|
690 |
OBJ newFloat; |
|
691 |
float128_t result, myVal; |
|
692 |
||
693 |
myVal = __quadFloatVal(self); |
|
694 |
result = STX_expq(myVal); |
|
695 |
__qMKQFLOAT(newFloat, result); |
|
696 |
RETURN ( newFloat ); |
|
697 |
#endif |
|
698 |
%}. |
|
699 |
! |
|
700 |
||
701 |
exponent |
|
702 |
"extract a normalized float's (unbiased) exponent. |
|
703 |
The returned value depends on the float-representation of |
|
704 |
the underlying machine and is therefore highly unportable. |
|
705 |
This is not for general use. |
|
706 |
This assumes that the mantissa is normalized to |
|
707 |
0.5 .. 1.0 and the float's value is: mantissa * 2^exp" |
|
708 |
||
709 |
%{ /* NOCONTEXT */ |
|
5287 | 710 |
#ifdef SUPPORT_QUADFLOAT |
5276 | 711 |
float128_t myVal; |
712 |
float128_t frac; |
|
713 |
int exp; |
|
714 |
||
715 |
myVal = __quadFloatVal(self); |
|
716 |
#if 1 |
|
717 |
// should we? |
|
718 |
if (! (STX_isNanq(&myVal) || STX_isInfq(&myVal))) |
|
719 |
#endif |
|
720 |
{ |
|
5287 | 721 |
frac = STX_frexpq(myVal, &exp); |
722 |
RETURN (__mkSmallInteger(exp)); |
|
5276 | 723 |
} |
5287 | 724 |
#endif |
5276 | 725 |
%}. |
726 |
^ super exponent |
|
727 |
||
728 |
" |
|
729 |
1.0 exponent |
|
730 |
1.0 asQuadFloat exponent |
|
731 |
2.0 exponent |
|
732 |
2.0 asQuadFloat exponent |
|
733 |
3.0 exponent |
|
734 |
3.0 asQuadFloat exponent |
|
735 |
4.0 exponent |
|
736 |
4.0 asQuadFloat exponent |
|
737 |
0.5 exponent |
|
738 |
0.5 asQuadFloat exponent |
|
739 |
0.4 exponent |
|
740 |
0.4 asQuadFloat exponent |
|
741 |
0.25 exponent |
|
742 |
0.25 asQuadFloat exponent |
|
743 |
0.2 exponent |
|
744 |
0.2 asQuadFloat exponent |
|
745 |
0.00000011111 exponent |
|
746 |
0.00000011111 asQuadFloat exponent |
|
747 |
0.0 exponent |
|
748 |
0.0 asQuadFloat exponent |
|
749 |
||
750 |
1e1000 exponent -> error (INF) |
|
751 |
1e1000 asQuadFloat exponent -> error (INF) |
|
752 |
" |
|
753 |
! |
|
754 |
||
755 |
floor |
|
756 |
"return the integer nearest the receiver towards negative infinity." |
|
757 |
||
758 |
|val| |
|
759 |
||
760 |
%{ |
|
5287 | 761 |
#ifdef SUPPORT_QUADFLOAT |
5276 | 762 |
float128_t qVal; |
763 |
float128_t qMinInt; |
|
764 |
float128_t qMaxInt; |
|
765 |
||
766 |
qVal = __quadFloatVal(self); |
|
767 |
qVal = STX_floorq(qVal); |
|
768 |
||
769 |
qMinInt = STX_dbltoq((double)_MIN_INT); |
|
770 |
qMaxInt = STX_dbltoq((double)_MAX_INT); |
|
771 |
if (STX_geq(qVal, qMinInt) && STX_leq(qVal, qMaxInt)) { |
|
772 |
double dVal = STX_qtodbl(qVal); |
|
773 |
RETURN ( __mkSmallInteger( (INT) dVal ) ); |
|
774 |
} |
|
775 |
__qMKQFLOAT(val, qVal); |
|
5287 | 776 |
#endif |
5276 | 777 |
%}. |
778 |
^ val asInteger |
|
779 |
||
780 |
" |
|
781 |
0.5 asQuadFloat floor |
|
782 |
0.5 asQuadFloat floorAsFloat |
|
783 |
-0.5 asQuadFloat floor |
|
784 |
-0.5 asQuadFloat floorAsFloat |
|
785 |
" |
|
786 |
! |
|
787 |
||
788 |
floorAsFloat |
|
789 |
"return the integer nearest the receiver towards negative infinity as a float. |
|
790 |
This is much like #floor, but avoids a (possibly expensive) conversion |
|
791 |
of the result to an integer. |
|
792 |
It may be useful, if the result is to be further used in another float-operation." |
|
793 |
||
794 |
%{ /* NOCONTEXT */ |
|
795 |
#ifdef SUPPORT_QUADFLOAT |
|
796 |
OBJ newFloat; |
|
797 |
float128_t result, myVal; |
|
798 |
||
799 |
myVal = __quadFloatVal(self); |
|
800 |
result = STX_floorq(myVal); |
|
801 |
__qMKQFLOAT(newFloat, result); |
|
802 |
RETURN ( newFloat ); |
|
803 |
#endif |
|
804 |
%}. |
|
805 |
! |
|
806 |
||
807 |
ln |
|
808 |
"return natural logarithm of the receiver." |
|
809 |
||
810 |
%{ /* NOCONTEXT */ |
|
811 |
#ifdef SUPPORT_QUADFLOAT |
|
812 |
OBJ newFloat; |
|
813 |
float128_t result, myVal; |
|
814 |
||
815 |
myVal = __quadFloatVal(self); |
|
816 |
result = STX_logq(myVal); |
|
817 |
__qMKQFLOAT(newFloat, result); |
|
818 |
RETURN ( newFloat ); |
|
819 |
#endif |
|
820 |
%}. |
|
821 |
! |
|
822 |
||
823 |
log |
|
824 |
"return log base 10 of the receiver. |
|
825 |
Alias for log:10." |
|
826 |
||
827 |
%{ /* NOCONTEXT */ |
|
828 |
#ifdef SUPPORT_QUADFLOAT |
|
829 |
OBJ newFloat; |
|
830 |
float128_t result, myVal; |
|
831 |
||
832 |
myVal = __quadFloatVal(self); |
|
833 |
result = STX_log10q(myVal); |
|
834 |
__qMKQFLOAT(newFloat, result); |
|
835 |
RETURN ( newFloat ); |
|
836 |
#endif |
|
837 |
%}. |
|
838 |
! |
|
839 |
||
840 |
log2 |
|
841 |
"return logarithm dualis of the receiver." |
|
842 |
||
843 |
%{ /* NOCONTEXT */ |
|
844 |
#ifdef SUPPORT_QUADFLOAT |
|
845 |
OBJ newFloat; |
|
846 |
float128_t result, myVal; |
|
847 |
||
848 |
myVal = __quadFloatVal(self); |
|
849 |
result = STX_log2q(myVal); |
|
850 |
__qMKQFLOAT(newFloat, result); |
|
851 |
RETURN ( newFloat ); |
|
852 |
#endif |
|
853 |
%}. |
|
854 |
! |
|
855 |
||
856 |
mantissa |
|
857 |
"extract a normalized float's mantissa. |
|
858 |
The returned value depends on the float-representation of |
|
859 |
the underlying machine and is therefore highly unportable. |
|
860 |
This is not for general use. |
|
861 |
This assumes that the mantissa is normalized to |
|
862 |
0.5 .. 1.0 and the float's value is mantissa * 2^exp" |
|
863 |
||
864 |
%{ /* NOCONTEXT */ |
|
5287 | 865 |
#ifdef SUPPORT_QUADFLOAT |
5276 | 866 |
float128_t myVal; |
867 |
float128_t frac; |
|
868 |
int exp; |
|
869 |
OBJ newFloat; |
|
870 |
||
871 |
myVal = __quadFloatVal(self); |
|
872 |
// ouch: math libs seem to not care for NaN here; |
|
873 |
#if 1 |
|
874 |
// should we? |
|
875 |
if (! (STX_isNanq(&myVal) || STX_isInfq(&myVal))) |
|
876 |
#endif |
|
877 |
{ |
|
878 |
frac = STX_frexpq(myVal, &exp); |
|
879 |
__qMKQFLOAT(newFloat, frac); |
|
880 |
RETURN ( newFloat ); |
|
881 |
} |
|
5287 | 882 |
#endif |
5276 | 883 |
%}. |
884 |
^ super mantissa |
|
885 |
||
886 |
" |
|
887 |
1.0 exponent |
|
888 |
1.0 asQuadFloat exponent |
|
889 |
1.0 mantissa |
|
890 |
1.0 asQuadFloat mantissa |
|
891 |
||
892 |
0.25 exponent |
|
893 |
0.25 asQuadFloat exponent |
|
894 |
0.25 mantissa |
|
895 |
0.25 asQuadFloat mantissa |
|
896 |
||
897 |
0.00000011111 exponent |
|
898 |
0.00000011111 mantissa |
|
899 |
||
900 |
1e1000 mantissa |
|
901 |
" |
|
902 |
||
903 |
"Modified: / 20-06-2017 / 11:37:13 / cg" |
|
904 |
"Modified (comment): / 26-05-2019 / 03:12:55 / Claus Gittinger" |
|
905 |
! |
|
906 |
||
5021 | 907 |
negated |
908 |
"return the receiver negated" |
|
909 |
||
910 |
%{ /* NOCONTEXT */ |
|
911 |
#ifdef SUPPORT_QUADFLOAT |
|
912 |
OBJ newFloat; |
|
913 |
float128_t result, myVal; |
|
914 |
||
915 |
myVal = __quadFloatVal(self); |
|
5276 | 916 |
result = STX_negq(myVal); |
5021 | 917 |
__qMKQFLOAT(newFloat, result); |
918 |
RETURN ( newFloat ); |
|
919 |
#endif |
|
920 |
%}. |
|
5025 | 921 |
! |
922 |
||
923 |
rem: aNumber |
|
924 |
"return the floating point remainder of the receiver and the argument, aNumber" |
|
925 |
||
926 |
aNumber isZero ifTrue:[ |
|
927 |
" |
|
928 |
No, you shalt not divide by zero |
|
929 |
" |
|
930 |
^ ZeroDivide raiseRequestWith:thisContext. |
|
931 |
]. |
|
932 |
^ aNumber remainderFromLongFloat:self |
|
5276 | 933 |
! |
934 |
||
935 |
sin |
|
936 |
"return the sine of the receiver (interpreted as radians)" |
|
937 |
||
938 |
%{ /* NOCONTEXT */ |
|
939 |
#ifdef SUPPORT_QUADFLOAT |
|
940 |
OBJ newFloat; |
|
941 |
float128_t result, myVal; |
|
942 |
||
943 |
myVal = __quadFloatVal(self); |
|
944 |
result = STX_sinq(myVal); |
|
945 |
__qMKQFLOAT(newFloat, result); |
|
946 |
RETURN ( newFloat ); |
|
947 |
#endif |
|
948 |
%}. |
|
949 |
! |
|
950 |
||
951 |
sinh |
|
952 |
"return the hyperbolic sine of the receiver (interpreted as radians)" |
|
953 |
||
954 |
%{ /* NOCONTEXT */ |
|
955 |
#ifdef SUPPORT_QUADFLOAT |
|
956 |
OBJ newFloat; |
|
957 |
float128_t result, myVal; |
|
958 |
||
959 |
myVal = __quadFloatVal(self); |
|
960 |
result = STX_sinhq(myVal); |
|
961 |
__qMKQFLOAT(newFloat, result); |
|
962 |
RETURN ( newFloat ); |
|
963 |
#endif |
|
964 |
%}. |
|
965 |
! |
|
966 |
||
967 |
tan |
|
968 |
"return the tangent of the receiver (interpreted as radians)" |
|
969 |
||
970 |
%{ /* NOCONTEXT */ |
|
971 |
#ifdef SUPPORT_QUADFLOAT |
|
972 |
OBJ newFloat; |
|
973 |
float128_t result, myVal; |
|
974 |
||
975 |
myVal = __quadFloatVal(self); |
|
976 |
result = STX_tanq(myVal); |
|
977 |
__qMKQFLOAT(newFloat, result); |
|
978 |
RETURN ( newFloat ); |
|
979 |
#endif |
|
980 |
%}. |
|
981 |
! |
|
982 |
||
983 |
tanh |
|
984 |
"return the hyperbolic tangent of the receiver (interpreted as radians)" |
|
985 |
||
986 |
%{ /* NOCONTEXT */ |
|
987 |
#ifdef SUPPORT_QUADFLOAT |
|
988 |
OBJ newFloat; |
|
989 |
float128_t result, myVal; |
|
990 |
||
991 |
myVal = __quadFloatVal(self); |
|
992 |
result = STX_tanhq(myVal); |
|
993 |
__qMKQFLOAT(newFloat, result); |
|
994 |
RETURN ( newFloat ); |
|
995 |
#endif |
|
996 |
%}. |
|
4994 | 997 |
! ! |
998 |
||
5008 | 999 |
!QuadFloat methodsFor:'coercing & converting'! |
1000 |
||
5276 | 1001 |
asFloat |
1002 |
"return a Float with same value as the receiver. |
|
1003 |
Raises an error if the receiver exceeds the float range." |
|
1004 |
||
1005 |
%{ /* NOCONTEXT */ |
|
1006 |
#ifdef SUPPORT_QUADFLOAT |
|
1007 |
OBJ newFloat; |
|
1008 |
float128_t qVal = __quadFloatVal(self); |
|
1009 |
double dVal = STX_qtodbl(qVal); |
|
1010 |
||
1011 |
if (isfinite(dVal) || !STX_isfiniteq(qVal)) { |
|
5287 | 1012 |
__qMKFLOAT(newFloat, dVal); |
1013 |
RETURN ( newFloat ); |
|
5276 | 1014 |
} |
1015 |
#endif |
|
1016 |
%}. |
|
1017 |
" |
|
1018 |
value out of range |
|
1019 |
if you need -INF for a zero receiver, try Number trapInfinity:[...] |
|
1020 |
" |
|
1021 |
^ self class |
|
5287 | 1022 |
raise:#infiniteResultSignal |
1023 |
receiver:self |
|
1024 |
selector:#asFloat |
|
1025 |
arguments:#() |
|
1026 |
errorString:'receiver is out of the double-precision float range' |
|
5276 | 1027 |
|
1028 |
" |
|
1029 |
1.0 asQuadFloat asFloat |
|
1030 |
" |
|
1031 |
! |
|
1032 |
||
5328 | 1033 |
asIEEEFloat |
1034 |
"return an IEEE soft float with same value as receiver" |
|
1035 |
||
1036 |
^ IEEEFloat fromFloat:self |
|
1037 |
||
1038 |
" |
|
1039 |
123 asFloat asIEEEFloat |
|
1040 |
0 asShortFloat asIEEEFloat |
|
1041 |
0 asLongFloat asIEEEFloat |
|
1042 |
0 asQuadFloat asIEEEFloat |
|
1043 |
0.0 asIEEEFloat |
|
1044 |
" |
|
1045 |
! |
|
1046 |
||
5295 | 1047 |
asQuadFloat |
1048 |
^ self |
|
1049 |
||
1050 |
" |
|
1051 |
1.0 asQuadFloat asQuadFloat |
|
1052 |
" |
|
1053 |
! |
|
1054 |
||
5008 | 1055 |
generality |
1056 |
"return the generality value - see ArithmeticValue>>retry:coercing:" |
|
1057 |
||
1058 |
^ 93 |
|
1059 |
||
1060 |
"Created: / 07-06-2019 / 09:30:58 / Claus Gittinger" |
|
1061 |
! ! |
|
1062 |
||
1063 |
!QuadFloat methodsFor:'comparing'! |
|
1064 |
||
1065 |
< aNumber |
|
1066 |
"return true, if the argument is greater" |
|
1067 |
||
1068 |
^ aNumber lessFromQuadFloat:self |
|
1069 |
||
1070 |
"Created: / 07-06-2019 / 09:25:47 / Claus Gittinger" |
|
1071 |
! |
|
1072 |
||
1073 |
= aNumber |
|
1074 |
"return true, if the argument represents the same numeric value |
|
1075 |
as the receiver, false otherwise" |
|
1076 |
||
1077 |
^ aNumber equalFromQuadFloat:self |
|
1078 |
||
1079 |
"Created: / 07-06-2019 / 09:25:27 / Claus Gittinger" |
|
1080 |
! |
|
1081 |
||
1082 |
hash |
|
1083 |
"return a number for hashing; redefined, since floats compare |
|
1084 |
by numeric value (i.e. 3.0 = 3), therefore 3.0 hash must be the same |
|
1085 |
as 3 hash." |
|
1086 |
||
1087 |
|i| |
|
1088 |
||
1089 |
(self >= SmallInteger minVal and:[self <= SmallInteger maxVal]) ifTrue:[ |
|
5011 | 1090 |
i := self asInteger. |
1091 |
self = i ifTrue:[ |
|
1092 |
^ i hash |
|
1093 |
]. |
|
5008 | 1094 |
]. |
1095 |
||
1096 |
^ self asFloat hash |
|
1097 |
||
1098 |
" |
|
1099 |
1.2345 hash |
|
1100 |
1.2345 asShortFloat hash |
|
1101 |
1.2345 asLongFloat hash |
|
1102 |
1.2345 asQuadFloat hash |
|
5011 | 1103 |
|
5008 | 1104 |
1.0 hash |
1105 |
1.0 asShortFloat hash |
|
1106 |
1.0 asLongFloat hash |
|
1107 |
1.0 asQuadFloat hash |
|
5011 | 1108 |
|
5008 | 1109 |
0.5 asShortFloat hash |
1110 |
0.5 asShortFloat hash |
|
1111 |
0.5 asLongFloat hash |
|
1112 |
0.5 asQuadFloat hash |
|
1113 |
||
1114 |
0.25 asShortFloat hash |
|
1115 |
0.25 asShortFloat hash |
|
1116 |
0.25 asLongFloat hash |
|
1117 |
0.25 asQuadFloat hash |
|
1118 |
" |
|
1119 |
||
1120 |
"Created: / 07-06-2019 / 09:28:07 / Claus Gittinger" |
|
1121 |
! ! |
|
1122 |
||
5003 | 1123 |
!QuadFloat methodsFor:'double dispatching'! |
1124 |
||
1125 |
differenceFromQuadFloat:aQuadFloat |
|
1126 |
%{ |
|
1127 |
#ifdef SUPPORT_QUADFLOAT |
|
1128 |
OBJ newFloat; |
|
1129 |
float128_t result, myVal, argVal; |
|
1130 |
||
1131 |
myVal = __quadFloatVal(self); |
|
1132 |
argVal = __quadFloatVal(aQuadFloat); |
|
5276 | 1133 |
result = STX_addq( argVal, myVal, 1 ); |
5003 | 1134 |
__qMKQFLOAT(newFloat, result); |
1135 |
RETURN ( newFloat ); |
|
1136 |
#endif // SUPPORT_QUADFLOAT |
|
1137 |
%}. |
|
1138 |
self errorUnsupported |
|
1139 |
! |
|
1140 |
||
5005 | 1141 |
equalFromQuadFloat:aQuadFloat |
1142 |
"sent when aQuadFloat does not know how to compare agaist the receiver, self" |
|
5003 | 1143 |
|
1144 |
%{ |
|
1145 |
#ifdef SUPPORT_QUADFLOAT |
|
1146 |
OBJ newFloat; |
|
1147 |
float128_t result, myVal, argVal; |
|
1148 |
||
1149 |
myVal = __quadFloatVal(self); |
|
1150 |
argVal = __quadFloatVal(aQuadFloat); |
|
5276 | 1151 |
RETURN (STX_eqq(argVal, myVal) ? true : false); |
5003 | 1152 |
#endif // SUPPORT_QUADFLOAT |
1153 |
%}. |
|
1154 |
self errorUnsupported |
|
5011 | 1155 |
|
1156 |
"Modified: / 08-06-2019 / 13:31:48 / Claus Gittinger" |
|
5004 | 1157 |
! |
1158 |
||
5005 | 1159 |
lessFromQuadFloat:aQuadFloat |
1160 |
"sent when aQuadFloat does not know how to compare agaist the receiver, self" |
|
1161 |
||
1162 |
%{ |
|
1163 |
#ifdef SUPPORT_QUADFLOAT |
|
1164 |
OBJ newFloat; |
|
1165 |
float128_t result, myVal, argVal; |
|
1166 |
||
1167 |
myVal = __quadFloatVal(self); |
|
1168 |
argVal = __quadFloatVal(aQuadFloat); |
|
5276 | 1169 |
RETURN (STX_ltq(argVal, myVal) ? true : false); |
5005 | 1170 |
#endif // SUPPORT_QUADFLOAT |
1171 |
%}. |
|
1172 |
self errorUnsupported |
|
1173 |
! |
|
1174 |
||
5004 | 1175 |
productFromQuadFloat:aQuadFloat |
1176 |
"sent when aQuadFloat does not know how to multiply the receiver, self" |
|
1177 |
||
1178 |
%{ |
|
1179 |
#ifdef SUPPORT_QUADFLOAT |
|
1180 |
OBJ newFloat; |
|
1181 |
float128_t result, myVal, argVal; |
|
1182 |
||
1183 |
myVal = __quadFloatVal(self); |
|
1184 |
argVal = __quadFloatVal(aQuadFloat); |
|
5276 | 1185 |
result = STX_mulq(myVal, argVal); |
5004 | 1186 |
__qMKQFLOAT(newFloat, result); |
1187 |
RETURN ( newFloat ); |
|
1188 |
#endif // SUPPORT_QUADFLOAT |
|
1189 |
%}. |
|
1190 |
self errorUnsupported |
|
1191 |
! |
|
1192 |
||
1193 |
quotientFromQuadFloat:aQuadFloat |
|
1194 |
"sent when aQuadFloat does not know how to multiply the receiver, self" |
|
1195 |
||
1196 |
%{ |
|
1197 |
#ifdef SUPPORT_QUADFLOAT |
|
1198 |
OBJ newFloat; |
|
1199 |
float128_t result, myVal, argVal; |
|
1200 |
||
1201 |
myVal = __quadFloatVal(self); |
|
1202 |
argVal = __quadFloatVal(aQuadFloat); |
|
5276 | 1203 |
result = STX_divq(argVal, myVal); |
5004 | 1204 |
__qMKQFLOAT(newFloat, result); |
1205 |
RETURN ( newFloat ); |
|
1206 |
#endif // SUPPORT_QUADFLOAT |
|
1207 |
%}. |
|
1208 |
self errorUnsupported |
|
1209 |
! |
|
1210 |
||
5005 | 1211 |
sumFromQuadFloat:aQuadFloat |
1212 |
"sent when aQuadFloat does not know how to add the receiver, self" |
|
5004 | 1213 |
|
1214 |
%{ |
|
1215 |
#ifdef SUPPORT_QUADFLOAT |
|
1216 |
OBJ newFloat; |
|
1217 |
float128_t result, myVal, argVal; |
|
1218 |
||
1219 |
myVal = __quadFloatVal(self); |
|
1220 |
argVal = __quadFloatVal(aQuadFloat); |
|
5276 | 1221 |
result = STX_addq( myVal, argVal, 0 ); |
5005 | 1222 |
__qMKQFLOAT(newFloat, result); |
5004 | 1223 |
RETURN ( newFloat ); |
1224 |
#endif // SUPPORT_QUADFLOAT |
|
1225 |
%}. |
|
1226 |
self errorUnsupported |
|
5003 | 1227 |
! ! |
1228 |
||
1229 |
!QuadFloat methodsFor:'error reportng'! |
|
1230 |
||
1231 |
errorUnsupported |
|
1232 |
self class errorUnsupported |
|
1233 |
||
1234 |
"Modified: / 07-06-2019 / 02:44:51 / Claus Gittinger" |
|
1235 |
! ! |
|
1236 |
||
5025 | 1237 |
!QuadFloat methodsFor:'printing'! |
1238 |
||
1239 |
printOn:aStream |
|
5269
83163a7005f1
Fallback to version 1.36 - mingw couldn't compile
Stefan Vogel <sv@exept.de>
parents:
5268
diff
changeset
|
1240 |
|mantissa exponent| |
83163a7005f1
Fallback to version 1.36 - mingw couldn't compile
Stefan Vogel <sv@exept.de>
parents:
5268
diff
changeset
|
1241 |
|
83163a7005f1
Fallback to version 1.36 - mingw couldn't compile
Stefan Vogel <sv@exept.de>
parents:
5268
diff
changeset
|
1242 |
mantissa := self mantissa. |
83163a7005f1
Fallback to version 1.36 - mingw couldn't compile
Stefan Vogel <sv@exept.de>
parents:
5268
diff
changeset
|
1243 |
exponent := self exponent. |
5027 | 1244 |
|
5269
83163a7005f1
Fallback to version 1.36 - mingw couldn't compile
Stefan Vogel <sv@exept.de>
parents:
5268
diff
changeset
|
1245 |
self exponent == 0 ifTrue:[ |
83163a7005f1
Fallback to version 1.36 - mingw couldn't compile
Stefan Vogel <sv@exept.de>
parents:
5268
diff
changeset
|
1246 |
mantissa printOn:aStream. |
83163a7005f1
Fallback to version 1.36 - mingw couldn't compile
Stefan Vogel <sv@exept.de>
parents:
5268
diff
changeset
|
1247 |
aStream nextPutAll:'.0'. |
83163a7005f1
Fallback to version 1.36 - mingw couldn't compile
Stefan Vogel <sv@exept.de>
parents:
5268
diff
changeset
|
1248 |
^ self |
83163a7005f1
Fallback to version 1.36 - mingw couldn't compile
Stefan Vogel <sv@exept.de>
parents:
5268
diff
changeset
|
1249 |
]. |
83163a7005f1
Fallback to version 1.36 - mingw couldn't compile
Stefan Vogel <sv@exept.de>
parents:
5268
diff
changeset
|
1250 |
mantissa == 0 ifTrue:[ |
83163a7005f1
Fallback to version 1.36 - mingw couldn't compile
Stefan Vogel <sv@exept.de>
parents:
5268
diff
changeset
|
1251 |
"/ a zero mantissa is impossible - except for zero and a few others |
83163a7005f1
Fallback to version 1.36 - mingw couldn't compile
Stefan Vogel <sv@exept.de>
parents:
5268
diff
changeset
|
1252 |
exponent == 0 ifTrue:[ aStream nextPutAll:'0.0'. ^ self]. |
83163a7005f1
Fallback to version 1.36 - mingw couldn't compile
Stefan Vogel <sv@exept.de>
parents:
5268
diff
changeset
|
1253 |
self == NaN ifTrue:[ aStream nextPutAll:'NAN'. ^ self ]. |
83163a7005f1
Fallback to version 1.36 - mingw couldn't compile
Stefan Vogel <sv@exept.de>
parents:
5268
diff
changeset
|
1254 |
self == NegativeInfinity ifTrue:[ aStream nextPutAll:'-INF'. ^ self]. |
83163a7005f1
Fallback to version 1.36 - mingw couldn't compile
Stefan Vogel <sv@exept.de>
parents:
5268
diff
changeset
|
1255 |
self == PositiveInfinity ifTrue:[ aStream nextPutAll:'INF'. ^ self]. |
83163a7005f1
Fallback to version 1.36 - mingw couldn't compile
Stefan Vogel <sv@exept.de>
parents:
5268
diff
changeset
|
1256 |
|
83163a7005f1
Fallback to version 1.36 - mingw couldn't compile
Stefan Vogel <sv@exept.de>
parents:
5268
diff
changeset
|
1257 |
self error:'invalid largeFloat' mayProceed:true. |
83163a7005f1
Fallback to version 1.36 - mingw couldn't compile
Stefan Vogel <sv@exept.de>
parents:
5268
diff
changeset
|
1258 |
aStream nextPutAll:'Invalid'. ^ self. |
83163a7005f1
Fallback to version 1.36 - mingw couldn't compile
Stefan Vogel <sv@exept.de>
parents:
5268
diff
changeset
|
1259 |
]. |
83163a7005f1
Fallback to version 1.36 - mingw couldn't compile
Stefan Vogel <sv@exept.de>
parents:
5268
diff
changeset
|
1260 |
|
83163a7005f1
Fallback to version 1.36 - mingw couldn't compile
Stefan Vogel <sv@exept.de>
parents:
5268
diff
changeset
|
1261 |
exponent >= 0 ifTrue:[ |
83163a7005f1
Fallback to version 1.36 - mingw couldn't compile
Stefan Vogel <sv@exept.de>
parents:
5268
diff
changeset
|
1262 |
(mantissa bitShift:exponent) printOn:aStream. |
83163a7005f1
Fallback to version 1.36 - mingw couldn't compile
Stefan Vogel <sv@exept.de>
parents:
5268
diff
changeset
|
1263 |
aStream nextPutAll:'.0'. |
83163a7005f1
Fallback to version 1.36 - mingw couldn't compile
Stefan Vogel <sv@exept.de>
parents:
5268
diff
changeset
|
1264 |
^ self |
83163a7005f1
Fallback to version 1.36 - mingw couldn't compile
Stefan Vogel <sv@exept.de>
parents:
5268
diff
changeset
|
1265 |
]. |
83163a7005f1
Fallback to version 1.36 - mingw couldn't compile
Stefan Vogel <sv@exept.de>
parents:
5268
diff
changeset
|
1266 |
((mantissa / (1 bitShift:exponent negated)) asFixedPoint:6) printOn:aStream. |
5025 | 1267 |
|
1268 |
"Created: / 11-06-2019 / 00:13:00 / Claus Gittinger" |
|
1269 |
! ! |
|
1270 |
||
5011 | 1271 |
!QuadFloat methodsFor:'queries'! |
1272 |
||
1273 |
isFinite |
|
5194 | 1274 |
"return true, if the receiver is a finite float (not NaN and not +/-INF)" |
5011 | 1275 |
|
1276 |
%{ /* NOCONTEXT */ |
|
5016 | 1277 |
#ifdef SUPPORT_QUADFLOAT |
5011 | 1278 |
float128_t myVal; |
1279 |
||
1280 |
myVal = __quadFloatVal(self); |
|
5276 | 1281 |
RETURN (STX_isfiniteq(myVal) ? true : false); |
5016 | 1282 |
#endif // SUPPORT_QUADFLOAT |
5011 | 1283 |
%}. |
1284 |
||
1285 |
" |
|
5276 | 1286 |
1.0 isFinite |
1287 |
self NaN isFinite |
|
1288 |
self infinity isFinite |
|
1289 |
self negativeInfinity isFinite |
|
1290 |
(0.0 uncheckedDivide: 0.0) isFinite |
|
1291 |
(1.0 uncheckedDivide: 0.0) isFinite |
|
5020 | 1292 |
" |
1293 |
! |
|
1294 |
||
1295 |
isInfinite |
|
5194 | 1296 |
"return true, if the receiver is an infinite float (+Inf or -Inf)." |
5020 | 1297 |
|
1298 |
%{ /* NOCONTEXT */ |
|
1299 |
#ifdef SUPPORT_QUADFLOAT |
|
1300 |
float128_t myVal; |
|
1301 |
||
1302 |
myVal = __quadFloatVal(self); |
|
5276 | 1303 |
RETURN (STX_isInfq(&myVal) ? true : false); |
5020 | 1304 |
#endif // SUPPORT_QUADFLOAT |
1305 |
%}. |
|
1306 |
||
1307 |
" |
|
5276 | 1308 |
1.0 asQuadFloat isFinite |
1309 |
1.0 asQuadFloat isInfinite |
|
1310 |
self NaN isFinite |
|
1311 |
self NaN isInfinite |
|
1312 |
self infinity isFinite |
|
1313 |
self infinity isInfinite |
|
1314 |
self negativeInfinity isFinite |
|
1315 |
self negativeInfinity isInfinite |
|
1316 |
(0.0 uncheckedDivide: 0.0) isFinite |
|
1317 |
(1.0 uncheckedDivide: 0.0) isFinite |
|
5011 | 1318 |
" |
1319 |
! |
|
1320 |
||
1321 |
isNaN |
|
5194 | 1322 |
"return true, if the receiver is an invalid float (NaN - not a number). |
1323 |
These are not created by ST/X float operations (they raise an exception); |
|
1324 |
however, inline C-code could produce them." |
|
5011 | 1325 |
|
1326 |
%{ /* NOCONTEXT */ |
|
5016 | 1327 |
#ifdef SUPPORT_QUADFLOAT |
5011 | 1328 |
float128_t myVal; |
1329 |
||
1330 |
myVal = __quadFloatVal(self); |
|
5276 | 1331 |
RETURN (STX_isNanq(&myVal) ? true : false); |
5016 | 1332 |
#endif // SUPPORT_QUADFLOAT |
5011 | 1333 |
%}. |
1334 |
||
1335 |
" |
|
5276 | 1336 |
1.0 asQuadFloat isFinite |
1337 |
self NaN isFinite |
|
1338 |
self infinity isFinite |
|
1339 |
(0.0 uncheckedDivide: 0.0) isFinite |
|
1340 |
(1.0 uncheckedDivide: 0.0) isFinite |
|
5011 | 1341 |
" |
1342 |
! ! |
|
1343 |
||
5276 | 1344 |
!QuadFloat methodsFor:'testing'! |
1345 |
||
1346 |
isQuadFloat |
|
1347 |
"return true, if the receiver is some kind of quad floating point number (iee quad precision)" |
|
1348 |
||
1349 |
^ true |
|
1350 |
! ! |
|
1351 |
||
4983 | 1352 |
!QuadFloat class methodsFor:'documentation'! |
1353 |
||
1354 |
version_CVS |
|
1355 |
^ '$Header$' |
|
1356 |
! ! |
|
5290 | 1357 |