author | Claus Gittinger <cg@exept.de> |
Mon, 16 Dec 2019 16:00:23 +0100 | |
changeset 25135 | 98f799af1e48 |
parent 24435 | fb2ab71b12a4 |
permissions | -rw-r--r-- |
24435 | 1 |
"{ Encoding: utf8 }" |
2 |
||
1 | 3 |
" |
4 |
COPYRIGHT (c) 1993 by Claus Gittinger |
|
5 |
All Rights Reserved |
|
6 |
||
7 |
This software is furnished under a license and may be used |
|
8 |
only in accordance with the terms of that license and with the |
|
9 |
inclusion of the above copyright notice. This software may not |
|
10 |
be provided or otherwise made available to, or used by, any |
|
11 |
other person. No title to or ownership of the software is |
|
12 |
hereby transferred. |
|
13 |
" |
|
7220 | 14 |
"{ Package: 'stx:libbasic' }" |
15 |
||
19031 | 16 |
"{ NameSpace: Smalltalk }" |
17 |
||
23805 | 18 |
UnboxedFloatArray variableDoubleSubclass:#DoubleArray |
1263 | 19 |
instanceVariableNames:'' |
20 |
classVariableNames:'' |
|
21 |
poolDictionaries:'' |
|
22 |
category:'Collections-Arrayed' |
|
1 | 23 |
! |
24 |
||
88 | 25 |
!DoubleArray class methodsFor:'documentation'! |
26 |
||
27 |
copyright |
|
28 |
" |
|
29 |
COPYRIGHT (c) 1993 by Claus Gittinger |
|
1 | 30 |
All Rights Reserved |
31 |
||
88 | 32 |
This software is furnished under a license and may be used |
33 |
only in accordance with the terms of that license and with the |
|
34 |
inclusion of the above copyright notice. This software may not |
|
35 |
be provided or otherwise made available to, or used by, any |
|
36 |
other person. No title to or ownership of the software is |
|
37 |
hereby transferred. |
|
38 |
" |
|
39 |
! |
|
1 | 40 |
|
88 | 41 |
documentation |
42 |
" |
|
13825 | 43 |
DoubleArrays store doubleFloat values (and nothing else). |
16639 | 44 |
They have been added to support heavy duty number crunching and |
45 |
data exchange with openGL frameworks and other mass data libraries |
|
46 |
somewhat better than other smalltalks do. |
|
362 | 47 |
Storing Floats & Doubles in these objects (instead of Arrays) |
48 |
has some benefits: |
|
1 | 49 |
|
88 | 50 |
1) since the values are stored directly (instead of pointers to them) |
51 |
both access overhead and garbage collect overhead is minimized. |
|
52 |
||
53 |
2) they can be much faster passed to c functions (such as graphics |
|
54 |
libraries or heavy duty math packages), since the double values |
|
55 |
come packed and can be used in C by using a (double *) or double[]. |
|
362 | 56 |
There is no need to loop over the array extracting doubles. |
88 | 57 |
|
58 |
3) they could (in theory) be much more easily be processed by things like |
|
59 |
vector and array processors |
|
1 | 60 |
|
88 | 61 |
Be aware however, that Float- and DoubleArrays are not supported in other |
62 |
smalltalks - your program will thus become somewhat less portable. |
|
63 |
(since their protocol is the same as normal arrays filled with floats, |
|
64 |
they can of course be easily simulated - a bit slower though) |
|
1 | 65 |
|
2820 | 66 |
However, they could be simulated by a ByteArray, using doubleAt: and |
92 | 67 |
doubleAtPut: messages to access the elements, but that seems a bit |
68 |
clumsy and unelegant. Also, the stc-compiler may learn how to deal |
|
69 |
with Float- and DoubleArrays, making accesses very fast in the future. |
|
2820 | 70 |
Hint: if you use doubleArrays in your application and must port it |
71 |
to some other smalltalk, define a DoubleArray class there, which is derived |
|
72 |
from ByteArray, and add access methods. |
|
1 | 73 |
|
2145 | 74 |
Of course, DoubleArray can be subclassed, |
75 |
and named instance variables can be added there. |
|
1 | 76 |
|
362 | 77 |
See example uses in the GLX interface and GLDemos. |
1263 | 78 |
|
4062 | 79 |
[memory requirements:] |
80 |
OBJ-HEADER + (size * double-size) |
|
81 |
||
1263 | 82 |
[See also:] |
83 |
FloatArray Array |
|
1290 | 84 |
|
85 |
[author:] |
|
86 |
Claus Gittinger |
|
88 | 87 |
" |
627 | 88 |
! ! |
89 |
||
24434 | 90 |
|
91 |
||
13708
079c1e1c7594
faster elementByteSize query
Claus Gittinger <cg@exept.de>
parents:
7220
diff
changeset
|
92 |
!DoubleArray class methodsFor:'queries'! |
079c1e1c7594
faster elementByteSize query
Claus Gittinger <cg@exept.de>
parents:
7220
diff
changeset
|
93 |
|
079c1e1c7594
faster elementByteSize query
Claus Gittinger <cg@exept.de>
parents:
7220
diff
changeset
|
94 |
elementByteSize |
16850
4011fc651793
comment/format in: #elementByteSize
Claus Gittinger <cg@exept.de>
parents:
16639
diff
changeset
|
95 |
"for bit-like containers, return the number of bytes stored per element. |
4011fc651793
comment/format in: #elementByteSize
Claus Gittinger <cg@exept.de>
parents:
16639
diff
changeset
|
96 |
Here, 8 is returned" |
4011fc651793
comment/format in: #elementByteSize
Claus Gittinger <cg@exept.de>
parents:
16639
diff
changeset
|
97 |
|
13708
079c1e1c7594
faster elementByteSize query
Claus Gittinger <cg@exept.de>
parents:
7220
diff
changeset
|
98 |
^ 8 |
079c1e1c7594
faster elementByteSize query
Claus Gittinger <cg@exept.de>
parents:
7220
diff
changeset
|
99 |
|
079c1e1c7594
faster elementByteSize query
Claus Gittinger <cg@exept.de>
parents:
7220
diff
changeset
|
100 |
"Created: / 15-09-2011 / 14:12:46 / cg" |
24434 | 101 |
! |
102 |
||
103 |
epsilon |
|
24435 | 104 |
"return the maximum relative spacing of my elements |
24434 | 105 |
(i.e. the value-delta of the least significant bit)" |
106 |
||
107 |
^ Float epsilon |
|
108 |
||
109 |
"Created: / 19-07-2019 / 17:16:25 / Claus Gittinger" |
|
24435 | 110 |
! |
111 |
||
112 |
precision |
|
113 |
"answer the precision (the number of bits in the mantissa) of my elements (in bits) |
|
114 |
This is an IEEE halffloat, where only the fraction from the normalized mantissa is stored |
|
115 |
and so there is a hidden bit and the mantissa is actually represented by 53 binary digits |
|
116 |
(although only 52 are needed in the binary representation)" |
|
117 |
||
118 |
^ Float precision |
|
119 |
||
120 |
"Created: / 19-07-2019 / 17:23:12 / Claus Gittinger" |
|
13708
079c1e1c7594
faster elementByteSize query
Claus Gittinger <cg@exept.de>
parents:
7220
diff
changeset
|
121 |
! ! |
079c1e1c7594
faster elementByteSize query
Claus Gittinger <cg@exept.de>
parents:
7220
diff
changeset
|
122 |
|
24434 | 123 |
|
7220 | 124 |
!DoubleArray methodsFor:'queries'! |
125 |
||
126 |
defaultElement |
|
127 |
^ Float zero |
|
20301 | 128 |
! |
129 |
||
130 |
isValidElement:anObject |
|
131 |
"return true, if I can hold this kind of object" |
|
132 |
||
133 |
^ anObject isNumber |
|
20314 | 134 |
! |
135 |
||
136 |
max |
|
137 |
"return the largest element; |
|
138 |
redefined for speed" |
|
139 |
%{ /* NOCONTEXT */ |
|
140 |
if (__ClassInstPtr(__qClass(self))->c_ninstvars == __mkSmallInteger(0)) { |
|
141 |
INT _sz = __doubleArraySize(self); |
|
142 |
||
143 |
if (_sz > 0) { |
|
144 |
double *_p = __DoubleArrayInstPtr(self)->d_element; |
|
145 |
double _max; |
|
146 |
||
147 |
_max = _p[0]; |
|
148 |
if (_sz > 1) { |
|
149 |
INT _i; |
|
150 |
double _prev, _this; |
|
151 |
||
152 |
/* how about inline-mmx-asm for this ... */ |
|
153 |
_this = _p[1]; |
|
154 |
for (_i=2; _i<_sz; _i++) { |
|
155 |
_prev = _this; |
|
156 |
_this = _p[_i]; |
|
157 |
if (_prev > _max) _max = _prev; |
|
158 |
} |
|
159 |
if (_this > _max) _max = _this; |
|
160 |
} |
|
161 |
RETURN (__MKFLOAT(_max)); |
|
162 |
} |
|
163 |
} |
|
164 |
%}. |
|
165 |
^ super max |
|
166 |
||
167 |
" |
|
168 |
|f1| |
|
169 |
||
170 |
f1 := (1 to:10000) asDoubleArray. |
|
171 |
Time millisecondsToRun:[ 1000 timesRepeat:[ f1 max ] ] |
|
172 |
" |
|
173 |
" |
|
174 |
|a1| |
|
175 |
||
176 |
a1 := (1 to:10000) asArray collect:#asFloat. |
|
177 |
Time millisecondsToRun:[ 1000 timesRepeat:[ a1 max ] ] |
|
178 |
" |
|
179 |
" |
|
180 |
|f1| |
|
181 |
||
182 |
f1 := DoubleArray withAll:#(1 2 3 4 5). |
|
183 |
f1 max |
|
184 |
" |
|
185 |
" |
|
186 |
|f1| |
|
187 |
||
188 |
f1 := DoubleArray withAll:#(5 4 3 2 1). |
|
189 |
f1 max |
|
190 |
" |
|
191 |
! |
|
192 |
||
193 |
min |
|
194 |
"return the smallest element; |
|
195 |
redefined for speed" |
|
196 |
%{ /* NOCONTEXT */ |
|
197 |
if (__ClassInstPtr(__qClass(self))->c_ninstvars == __mkSmallInteger(0)) { |
|
198 |
INT _sz = __doubleArraySize(self); |
|
199 |
||
200 |
if (_sz > 0) { |
|
201 |
double *_p = __DoubleArrayInstPtr(self)->d_element; |
|
202 |
double _min; |
|
203 |
||
204 |
_min = _p[0]; |
|
205 |
if (_sz > 1) { |
|
206 |
INT _i; |
|
207 |
double _prev, _this; |
|
208 |
||
209 |
/* how about inline-mmx-asm for this ... */ |
|
210 |
_this = _p[1]; |
|
211 |
for (_i=2; _i<_sz; _i++) { |
|
212 |
_prev = _this; |
|
213 |
_this = _p[_i]; |
|
214 |
if (_prev < _min) _min = _prev; |
|
215 |
} |
|
216 |
if (_this < _min) _min = _this; |
|
217 |
} |
|
218 |
RETURN (__MKFLOAT(_min)); |
|
219 |
} |
|
220 |
} |
|
221 |
%}. |
|
222 |
^ super min |
|
223 |
||
224 |
" |
|
225 |
|f1| |
|
226 |
||
227 |
f1 := (1 to:10000) asDoubleArray. |
|
228 |
Time millisecondsToRun:[ 1000 timesRepeat:[ f1 min ] ] |
|
229 |
" |
|
230 |
" |
|
231 |
|a1| |
|
232 |
||
233 |
a1 := (1 to:10000) asArray collect:#asFloat. |
|
234 |
Time millisecondsToRun:[ 1000 timesRepeat:[ a1 min ] ] |
|
235 |
" |
|
236 |
" |
|
237 |
|f1| |
|
238 |
||
239 |
f1 := DoubleArray withAll:#(1 2 3 4 5). |
|
240 |
f1 min |
|
241 |
" |
|
242 |
" |
|
243 |
|f1| |
|
244 |
||
245 |
f1 := DoubleArray withAll:#(5 4 3 2 1). |
|
246 |
f1 min |
|
247 |
" |
|
248 |
! |
|
249 |
||
250 |
minMax |
|
251 |
"return a Tuple holding the smallest and largest element; |
|
252 |
redefined for speed" |
|
253 |
||
254 |
|min max empty| |
|
255 |
||
256 |
%{ |
|
257 |
if (__ClassInstPtr(__qClass(self))->c_ninstvars == __mkSmallInteger(0)) { |
|
21080 | 258 |
INT _sz = __doubleArraySize(self); |
20314 | 259 |
|
260 |
if (_sz > 0) { |
|
261 |
INT _i; |
|
262 |
double *_p = __DoubleArrayInstPtr(self)->d_element; |
|
263 |
double _min, _max; |
|
264 |
OBJ ret; |
|
265 |
||
266 |
_min = _max = _p[0]; |
|
267 |
for (_i=_sz-1; _i>0; _i-=2) { |
|
268 |
double _v1 = _p[_i]; |
|
269 |
double _v2 = _p[_i-1]; |
|
270 |
if (_v1 < _v2) { |
|
271 |
if (_v1 < _min) _min = _v1; |
|
272 |
if (_v2 > _max) _max = _v2; |
|
273 |
} else { |
|
274 |
if (_v2 < _min) _min = _v2; |
|
275 |
if (_v1 > _max) _max = _v1; |
|
276 |
} |
|
277 |
} |
|
21080 | 278 |
|
20314 | 279 |
min = __MKFLOAT(_min); |
280 |
__PROTECT__(min); |
|
281 |
max = __MKFLOAT(_max); |
|
282 |
__UNPROTECT__(min); |
|
283 |
RETURN (__ARRAY_WITH2(min, max)); |
|
284 |
} |
|
285 |
empty = true; |
|
286 |
} |
|
287 |
%}. |
|
288 |
empty == true ifTrue:[ |
|
289 |
^ self emptyCollectionError. |
|
290 |
]. |
|
291 |
"/ fallback if no primitive code |
|
292 |
^ super minMax |
|
293 |
||
294 |
" |
|
295 |
|f1| |
|
296 |
||
297 |
f1 := (1 to:10000) asDoubleArray. |
|
298 |
Time millisecondsToRun:[ 1000 timesRepeat:[ f1 minMax ] ] |
|
299 |
" |
|
300 |
" |
|
301 |
|f1| |
|
302 |
||
303 |
f1 := (1 to:10000) asDoubleArray. |
|
304 |
Time millisecondsToRun:[ 1000 timesRepeat:[ f1 min ] ] |
|
305 |
" |
|
306 |
" |
|
307 |
|f1| |
|
308 |
||
309 |
f1 := DoubleArray withAll:#(1 2 3 4 5). |
|
310 |
f1 minMax |
|
311 |
" |
|
312 |
" |
|
313 |
|f1| |
|
314 |
||
315 |
f1 := DoubleArray withAll:#(5 4 3 2 1). |
|
316 |
f1 minMax |
|
317 |
" |
|
7220 | 318 |
! ! |
319 |
||
23797 | 320 |
!DoubleArray methodsFor:'testing'! |
321 |
||
322 |
isFloatArray |
|
323 |
"return true if the receiver has float elements. |
|
324 |
These are Float, Double- and HalfFloat arrays" |
|
325 |
||
326 |
^ true |
|
327 |
||
328 |
"Created: / 02-03-2019 / 23:13:34 / Claus Gittinger" |
|
329 |
! ! |
|
330 |
||
19031 | 331 |
!DoubleArray methodsFor:'vector arithmetic'! |
332 |
||
333 |
dot: aFloatVector |
|
334 |
"Return the dot product of the receiver and the argument. |
|
335 |
Raises an error, if the argument is not of the same size as the receiver." |
|
336 |
||
337 |
| mySize result | |
|
338 |
||
339 |
%{ |
|
340 |
if ((__ClassInstPtr(__qClass(self))->c_ninstvars == __mkSmallInteger(0)) |
|
341 |
&& (__ClassInstPtr(__qClass(aFloatVector))->c_ninstvars == __mkSmallInteger(0))) { |
|
342 |
INT __mySize = __doubleArraySize(self); |
|
343 |
double __result = 0.0; |
|
344 |
double *__p1 = __DoubleArrayInstPtr(self)->d_element; |
|
345 |
||
346 |
if (__mySize > 0) { |
|
347 |
if (__isFloats(aFloatVector)) { |
|
348 |
INT __otherSize = __floatArraySize(aFloatVector); |
|
349 |
||
350 |
if (__mySize == __otherSize) { |
|
351 |
float *__p2 = __FloatArrayInstPtr(aFloatVector)->f_element; |
|
352 |
INT __i; |
|
353 |
/* how about inline-mmx-asm for this ... */ |
|
354 |
for (__i=0; __i<__mySize; __i++) { |
|
355 |
__result += (__p1[__i] * __p2[__i]); |
|
356 |
} |
|
357 |
RETURN (__MKFLOAT(__result)); |
|
358 |
} |
|
359 |
} else if (__isDoubles(aFloatVector)) { |
|
360 |
INT __otherSize = __doubleArraySize(aFloatVector); |
|
361 |
||
362 |
if (__mySize == __otherSize) { |
|
363 |
double *__p2 = __DoubleArrayInstPtr(aFloatVector)->d_element; |
|
364 |
INT __i; |
|
365 |
/* how about inline-mmx-asm for this ... */ |
|
366 |
for (__i=0; __i<__mySize; __i++) { |
|
367 |
__result += (__p1[__i] * __p2[__i]); |
|
368 |
} |
|
369 |
RETURN (__MKFLOAT(__result)); |
|
370 |
} |
|
371 |
} |
|
372 |
} |
|
373 |
} |
|
374 |
%}. |
|
375 |
^ super dot:aFloatVector |
|
376 |
||
377 |
" |
|
378 |
|v| |
|
379 |
||
380 |
v := #(2.0 2.0 1.0) asFloatArray. |
|
381 |
v dot:v. |
|
382 |
" |
|
383 |
" |
|
384 |
|v1 v2| |
|
385 |
||
386 |
v1 := FloatArray new:10000 withAll:2. |
|
387 |
v2 := FloatArray new:10000 withAll:3. |
|
388 |
Time millisecondsToRun:[ |
|
389 |
10000 timesRepeat:[ |
|
390 |
v1 dot:v2. |
|
391 |
] |
|
392 |
] |
|
393 |
" |
|
394 |
||
395 |
"Created: / 29-05-2007 / 13:13:39 / cg" |
|
396 |
! |
|
397 |
||
24307 | 398 |
esum |
399 |
"Return the sum of the elements. |
|
400 |
Reduces accumulated rounding errors. |
|
401 |
This is an experimental algorithm" |
|
402 |
||
403 |
%{ |
|
404 |
if (__ClassInstPtr(__qClass(self))->c_ninstvars == __mkSmallInteger(0)) { |
|
405 |
INT __mySize = __doubleArraySize(self); |
|
406 |
double __sum = 0.0; |
|
407 |
double *__pf = __DoubleArrayInstPtr(self)->d_element; |
|
408 |
#define NBITS_EXP 11 |
|
409 |
#define NIDX_EXP ((1<<NBITS_EXP)) |
|
410 |
#define MASK_EXP (NIDX_EXP-1) |
|
411 |
double sumsPerE[ NIDX_EXP]; |
|
412 |
int __i, __minIdx = MASK_EXP, __maxIdx = 0; |
|
413 |
||
414 |
memset(sumsPerE, 0, sizeof(double)*NIDX_EXP); |
|
415 |
||
416 |
for (__i=0; __i<__mySize; __i++) { |
|
417 |
union { |
|
418 |
double f; |
|
24333
e6d6a165972d
Replace uint32_t (not known in mingw)
Stefan Vogel <sv@exept.de>
parents:
24307
diff
changeset
|
419 |
unsigned int32 ui32[2]; |
24307 | 420 |
} u; |
421 |
int exp; |
|
422 |
||
423 |
u.f = __pf[__i]; |
|
424 |
#ifdef __LSBFIRST__ |
|
425 |
exp = (u.ui32[1] >> (32-1-NBITS_EXP)) & MASK_EXP; |
|
426 |
#else |
|
427 |
exp = (u.ui32[0] >> ((32-1-NBITS_EXP)) & MASK_EXP; |
|
428 |
#endif |
|
429 |
// printf("f:%04x %04x %04x %04x\n", u.ui32[0], u.ui32[1], u.ui32[2], u.ui32[3]); |
|
430 |
// printf("e:%d %lf + %lf -> %lf\n", exp, sumsPerE[exp], u.f, (sumsPerE[exp]+u.f)); |
|
431 |
sumsPerE[exp] += u.f; |
|
432 |
__minIdx = exp < __minIdx ? exp : __minIdx; |
|
433 |
__maxIdx = exp > __maxIdx ? exp : __maxIdx; |
|
434 |
} |
|
435 |
||
436 |
// printf("low:%d high:%d\n", __minIdx, __maxIdx); |
|
437 |
for (__i=__maxIdx; __i>=__minIdx; __i--) { |
|
438 |
__sum += sumsPerE[__i]; |
|
439 |
} |
|
440 |
RETURN (__MKFLOAT(__sum)); |
|
441 |
} |
|
442 |
%}. |
|
443 |
^ super sum |
|
444 |
||
445 |
" |
|
446 |
|v| |
|
447 |
v := #(2.0 2.0 1.0 1.0) asDoubleArray. |
|
448 |
v sum. |
|
449 |
v esum. |
|
450 |
||
451 |
|v| |
|
452 |
v := #(1e100 1.0 -1e100 1.0) asDoubleArray. |
|
453 |
v sum. |
|
454 |
v esum. |
|
455 |
" |
|
456 |
||
457 |
"Created: / 09-06-2019 / 13:48:38 / Claus Gittinger" |
|
458 |
! |
|
459 |
||
19031 | 460 |
hornerMultiplyAndAdd:x |
461 |
"primitive support for horner's-method computation of polynomials. |
|
462 |
The vector is interpreted as providing the factors for a polynomial, |
|
463 |
an*x^n + (an-1)*x^(n-1) + ... + a2(x) + a1 |
|
464 |
where the ai are the elements of the Array. |
|
465 |
The highest rank factor is at the first position, the 0-rank constant at last. |
|
466 |
This is inlined c-code, which may get compiled to fast machine code, |
|
467 |
using multiply-and-add or vector instructions, if the CPU/Compiler support them." |
|
468 |
||
469 |
| mySize result | |
|
470 |
||
471 |
%{ |
|
472 |
double __x; |
|
473 |
||
474 |
if (__isFloat(x)) { |
|
475 |
__x = __floatVal(x); |
|
476 |
} else if (__isShortFloat(x)) { |
|
477 |
__x = (double)__shortFloatVal(x); |
|
478 |
} else if (__isSmallInteger(x)) { |
|
479 |
__x = (double)(__intVal(x)); |
|
480 |
} else { |
|
481 |
goto getOutOfHere; |
|
482 |
} |
|
483 |
||
484 |
if (__ClassInstPtr(__qClass(self))->c_ninstvars == __mkSmallInteger(0)) { |
|
485 |
INT __mySize = __doubleArraySize(self); |
|
486 |
double *__elements = __DoubleArrayInstPtr(self)->d_element; |
|
487 |
double __result = __elements[0]; |
|
488 |
||
489 |
if (__mySize > 1) { |
|
490 |
INT __i; |
|
491 |
/* how about inline-mmx-asm for this ... */ |
|
492 |
for (__i=1; __i<__mySize; __i++) { |
|
493 |
__result = (__result * __x) + __elements[__i]; |
|
494 |
} |
|
495 |
} |
|
496 |
RETURN (__MKFLOAT(__result)); |
|
497 |
} |
|
498 |
getOutOfHere: ; |
|
499 |
%}. |
|
500 |
^ super hornerMultiplyAndAdd:x |
|
501 |
||
502 |
" |
|
503 |
|v| |
|
504 |
v := #(2.0 3.0 4.0) asDoubleArray. |
|
505 |
v hornerMultiplyAndAdd:10. |
|
506 |
||
507 |
|v| |
|
508 |
v := Array new:100 withAll:2.0. |
|
509 |
v hornerMultiplyAndAdd:10 |
|
510 |
||
511 |
|v| |
|
512 |
v := Array new:100 withAll:2.0. |
|
513 |
Time millisecondsToRun:[ |
|
514 |
10000 timesRepeat:[ v hornerMultiplyAndAdd:10] |
|
515 |
] |
|
516 |
||
517 |
|v| |
|
518 |
v := FloatArray new:100 withAll:2. |
|
519 |
Time millisecondsToRun:[ |
|
520 |
10000 timesRepeat:[ v hornerMultiplyAndAdd:10] |
|
521 |
] |
|
522 |
||
523 |
|v| |
|
524 |
v := DoubleArray new:100 withAll:2. |
|
525 |
Time millisecondsToRun:[ |
|
526 |
10000 timesRepeat:[ v hornerMultiplyAndAdd:10] |
|
527 |
] |
|
528 |
" |
|
24307 | 529 |
! |
530 |
||
531 |
sum |
|
532 |
"Return the sum of the elements. |
|
533 |
May suffer from accumulated rounding error" |
|
534 |
||
535 |
%{ |
|
536 |
if (__ClassInstPtr(__qClass(self))->c_ninstvars == __mkSmallInteger(0)) { |
|
537 |
INT __mySize = __doubleArraySize(self); |
|
538 |
double __sum = 0.0; |
|
539 |
double *__pf = __DoubleArrayInstPtr(self)->d_element; |
|
540 |
int __i; |
|
541 |
||
542 |
/* how about inline-mmx-asm for this ... */ |
|
543 |
for (__i=0; __i<__mySize; __i++) { |
|
544 |
__sum += __pf[__i]; |
|
545 |
} |
|
546 |
RETURN (__MKFLOAT(__sum)); |
|
547 |
} |
|
548 |
%}. |
|
549 |
^ super sum |
|
550 |
||
551 |
" |
|
552 |
|v| |
|
553 |
v := #(2.0 2.0 1.0) asDoubleArray. |
|
554 |
v sum. |
|
555 |
||
556 |
|v| |
|
557 |
v := #(1e100 1.0 -1e100 1.0) asDoubleArray. |
|
558 |
v sum. |
|
559 |
" |
|
560 |
||
561 |
"Created: / 09-06-2019 / 10:43:10 / Claus Gittinger" |
|
19031 | 562 |
! ! |
563 |
||
24434 | 564 |
|
627 | 565 |
!DoubleArray class methodsFor:'documentation'! |
565 | 566 |
|
567 |
version |
|
19031 | 568 |
^ '$Header$' |
16853 | 569 |
! |
570 |
||
571 |
version_CVS |
|
19031 | 572 |
^ '$Header$' |
88 | 573 |
! ! |
16639 | 574 |