author | Claus Gittinger <cg@exept.de> |
Wed, 13 Feb 2019 22:10:53 +0100 | |
changeset 23735 | 77363fc65861 |
parent 21080 | ffebd509a3ae |
child 21089 | d50f93ca9622 |
child 23797 | 119ce5d12f09 |
permissions | -rw-r--r-- |
1 | 1 |
" |
2 |
COPYRIGHT (c) 1993 by Claus Gittinger |
|
3 |
All Rights Reserved |
|
4 |
||
5 |
This software is furnished under a license and may be used |
|
6 |
only in accordance with the terms of that license and with the |
|
7 |
inclusion of the above copyright notice. This software may not |
|
8 |
be provided or otherwise made available to, or used by, any |
|
9 |
other person. No title to or ownership of the software is |
|
10 |
hereby transferred. |
|
11 |
" |
|
7220 | 12 |
"{ Package: 'stx:libbasic' }" |
13 |
||
19031 | 14 |
"{ NameSpace: Smalltalk }" |
15 |
||
13825 | 16 |
AbstractNumberVector variableDoubleSubclass:#DoubleArray |
1263 | 17 |
instanceVariableNames:'' |
18 |
classVariableNames:'' |
|
19 |
poolDictionaries:'' |
|
20 |
category:'Collections-Arrayed' |
|
1 | 21 |
! |
22 |
||
88 | 23 |
!DoubleArray class methodsFor:'documentation'! |
24 |
||
25 |
copyright |
|
26 |
" |
|
27 |
COPYRIGHT (c) 1993 by Claus Gittinger |
|
1 | 28 |
All Rights Reserved |
29 |
||
88 | 30 |
This software is furnished under a license and may be used |
31 |
only in accordance with the terms of that license and with the |
|
32 |
inclusion of the above copyright notice. This software may not |
|
33 |
be provided or otherwise made available to, or used by, any |
|
34 |
other person. No title to or ownership of the software is |
|
35 |
hereby transferred. |
|
36 |
" |
|
37 |
! |
|
1 | 38 |
|
88 | 39 |
documentation |
40 |
" |
|
13825 | 41 |
DoubleArrays store doubleFloat values (and nothing else). |
16639 | 42 |
They have been added to support heavy duty number crunching and |
43 |
data exchange with openGL frameworks and other mass data libraries |
|
44 |
somewhat better than other smalltalks do. |
|
362 | 45 |
Storing Floats & Doubles in these objects (instead of Arrays) |
46 |
has some benefits: |
|
1 | 47 |
|
88 | 48 |
1) since the values are stored directly (instead of pointers to them) |
49 |
both access overhead and garbage collect overhead is minimized. |
|
50 |
||
51 |
2) they can be much faster passed to c functions (such as graphics |
|
52 |
libraries or heavy duty math packages), since the double values |
|
53 |
come packed and can be used in C by using a (double *) or double[]. |
|
362 | 54 |
There is no need to loop over the array extracting doubles. |
88 | 55 |
|
56 |
3) they could (in theory) be much more easily be processed by things like |
|
57 |
vector and array processors |
|
1 | 58 |
|
88 | 59 |
Be aware however, that Float- and DoubleArrays are not supported in other |
60 |
smalltalks - your program will thus become somewhat less portable. |
|
61 |
(since their protocol is the same as normal arrays filled with floats, |
|
62 |
they can of course be easily simulated - a bit slower though) |
|
1 | 63 |
|
2820 | 64 |
However, they could be simulated by a ByteArray, using doubleAt: and |
92 | 65 |
doubleAtPut: messages to access the elements, but that seems a bit |
66 |
clumsy and unelegant. Also, the stc-compiler may learn how to deal |
|
67 |
with Float- and DoubleArrays, making accesses very fast in the future. |
|
2820 | 68 |
Hint: if you use doubleArrays in your application and must port it |
69 |
to some other smalltalk, define a DoubleArray class there, which is derived |
|
70 |
from ByteArray, and add access methods. |
|
1 | 71 |
|
2145 | 72 |
Of course, DoubleArray can be subclassed, |
73 |
and named instance variables can be added there. |
|
1 | 74 |
|
362 | 75 |
See example uses in the GLX interface and GLDemos. |
1263 | 76 |
|
4062 | 77 |
[memory requirements:] |
78 |
OBJ-HEADER + (size * double-size) |
|
79 |
||
1263 | 80 |
[See also:] |
81 |
FloatArray Array |
|
1290 | 82 |
|
83 |
[author:] |
|
84 |
Claus Gittinger |
|
88 | 85 |
" |
627 | 86 |
! ! |
87 |
||
20314 | 88 |
|
89 |
||
13708
079c1e1c7594
faster elementByteSize query
Claus Gittinger <cg@exept.de>
parents:
7220
diff
changeset
|
90 |
!DoubleArray class methodsFor:'queries'! |
079c1e1c7594
faster elementByteSize query
Claus Gittinger <cg@exept.de>
parents:
7220
diff
changeset
|
91 |
|
079c1e1c7594
faster elementByteSize query
Claus Gittinger <cg@exept.de>
parents:
7220
diff
changeset
|
92 |
elementByteSize |
16850
4011fc651793
comment/format in: #elementByteSize
Claus Gittinger <cg@exept.de>
parents:
16639
diff
changeset
|
93 |
"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
|
94 |
Here, 8 is returned" |
4011fc651793
comment/format in: #elementByteSize
Claus Gittinger <cg@exept.de>
parents:
16639
diff
changeset
|
95 |
|
13708
079c1e1c7594
faster elementByteSize query
Claus Gittinger <cg@exept.de>
parents:
7220
diff
changeset
|
96 |
^ 8 |
079c1e1c7594
faster elementByteSize query
Claus Gittinger <cg@exept.de>
parents:
7220
diff
changeset
|
97 |
|
079c1e1c7594
faster elementByteSize query
Claus Gittinger <cg@exept.de>
parents:
7220
diff
changeset
|
98 |
"Created: / 15-09-2011 / 14:12:46 / cg" |
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 |
|
20314 | 101 |
|
7220 | 102 |
!DoubleArray methodsFor:'queries'! |
103 |
||
104 |
defaultElement |
|
105 |
^ Float zero |
|
20301 | 106 |
! |
107 |
||
108 |
isValidElement:anObject |
|
109 |
"return true, if I can hold this kind of object" |
|
110 |
||
111 |
^ anObject isNumber |
|
20314 | 112 |
! |
113 |
||
114 |
max |
|
115 |
"return the largest element; |
|
116 |
redefined for speed" |
|
117 |
%{ /* NOCONTEXT */ |
|
118 |
if (__ClassInstPtr(__qClass(self))->c_ninstvars == __mkSmallInteger(0)) { |
|
119 |
INT _sz = __doubleArraySize(self); |
|
120 |
||
121 |
if (_sz > 0) { |
|
122 |
double *_p = __DoubleArrayInstPtr(self)->d_element; |
|
123 |
double _max; |
|
124 |
||
125 |
_max = _p[0]; |
|
126 |
if (_sz > 1) { |
|
127 |
INT _i; |
|
128 |
double _prev, _this; |
|
129 |
||
130 |
/* how about inline-mmx-asm for this ... */ |
|
131 |
_this = _p[1]; |
|
132 |
for (_i=2; _i<_sz; _i++) { |
|
133 |
_prev = _this; |
|
134 |
_this = _p[_i]; |
|
135 |
if (_prev > _max) _max = _prev; |
|
136 |
} |
|
137 |
if (_this > _max) _max = _this; |
|
138 |
} |
|
139 |
RETURN (__MKFLOAT(_max)); |
|
140 |
} |
|
141 |
} |
|
142 |
%}. |
|
143 |
^ super max |
|
144 |
||
145 |
" |
|
146 |
|f1| |
|
147 |
||
148 |
f1 := (1 to:10000) asDoubleArray. |
|
149 |
Time millisecondsToRun:[ 1000 timesRepeat:[ f1 max ] ] |
|
150 |
" |
|
151 |
" |
|
152 |
|a1| |
|
153 |
||
154 |
a1 := (1 to:10000) asArray collect:#asFloat. |
|
155 |
Time millisecondsToRun:[ 1000 timesRepeat:[ a1 max ] ] |
|
156 |
" |
|
157 |
" |
|
158 |
|f1| |
|
159 |
||
160 |
f1 := DoubleArray withAll:#(1 2 3 4 5). |
|
161 |
f1 max |
|
162 |
" |
|
163 |
" |
|
164 |
|f1| |
|
165 |
||
166 |
f1 := DoubleArray withAll:#(5 4 3 2 1). |
|
167 |
f1 max |
|
168 |
" |
|
169 |
! |
|
170 |
||
171 |
min |
|
172 |
"return the smallest element; |
|
173 |
redefined for speed" |
|
174 |
%{ /* NOCONTEXT */ |
|
175 |
if (__ClassInstPtr(__qClass(self))->c_ninstvars == __mkSmallInteger(0)) { |
|
176 |
INT _sz = __doubleArraySize(self); |
|
177 |
||
178 |
if (_sz > 0) { |
|
179 |
double *_p = __DoubleArrayInstPtr(self)->d_element; |
|
180 |
double _min; |
|
181 |
||
182 |
_min = _p[0]; |
|
183 |
if (_sz > 1) { |
|
184 |
INT _i; |
|
185 |
double _prev, _this; |
|
186 |
||
187 |
/* how about inline-mmx-asm for this ... */ |
|
188 |
_this = _p[1]; |
|
189 |
for (_i=2; _i<_sz; _i++) { |
|
190 |
_prev = _this; |
|
191 |
_this = _p[_i]; |
|
192 |
if (_prev < _min) _min = _prev; |
|
193 |
} |
|
194 |
if (_this < _min) _min = _this; |
|
195 |
} |
|
196 |
RETURN (__MKFLOAT(_min)); |
|
197 |
} |
|
198 |
} |
|
199 |
%}. |
|
200 |
^ super min |
|
201 |
||
202 |
" |
|
203 |
|f1| |
|
204 |
||
205 |
f1 := (1 to:10000) asDoubleArray. |
|
206 |
Time millisecondsToRun:[ 1000 timesRepeat:[ f1 min ] ] |
|
207 |
" |
|
208 |
" |
|
209 |
|a1| |
|
210 |
||
211 |
a1 := (1 to:10000) asArray collect:#asFloat. |
|
212 |
Time millisecondsToRun:[ 1000 timesRepeat:[ a1 min ] ] |
|
213 |
" |
|
214 |
" |
|
215 |
|f1| |
|
216 |
||
217 |
f1 := DoubleArray withAll:#(1 2 3 4 5). |
|
218 |
f1 min |
|
219 |
" |
|
220 |
" |
|
221 |
|f1| |
|
222 |
||
223 |
f1 := DoubleArray withAll:#(5 4 3 2 1). |
|
224 |
f1 min |
|
225 |
" |
|
226 |
! |
|
227 |
||
228 |
minMax |
|
229 |
"return a Tuple holding the smallest and largest element; |
|
230 |
redefined for speed" |
|
231 |
||
232 |
|min max empty| |
|
233 |
||
234 |
%{ |
|
235 |
if (__ClassInstPtr(__qClass(self))->c_ninstvars == __mkSmallInteger(0)) { |
|
21080 | 236 |
INT _sz = __doubleArraySize(self); |
20314 | 237 |
|
238 |
if (_sz > 0) { |
|
239 |
INT _i; |
|
240 |
double *_p = __DoubleArrayInstPtr(self)->d_element; |
|
241 |
double _min, _max; |
|
242 |
OBJ ret; |
|
243 |
||
244 |
_min = _max = _p[0]; |
|
245 |
for (_i=_sz-1; _i>0; _i-=2) { |
|
246 |
double _v1 = _p[_i]; |
|
247 |
double _v2 = _p[_i-1]; |
|
248 |
if (_v1 < _v2) { |
|
249 |
if (_v1 < _min) _min = _v1; |
|
250 |
if (_v2 > _max) _max = _v2; |
|
251 |
} else { |
|
252 |
if (_v2 < _min) _min = _v2; |
|
253 |
if (_v1 > _max) _max = _v1; |
|
254 |
} |
|
255 |
} |
|
21080 | 256 |
|
20314 | 257 |
min = __MKFLOAT(_min); |
258 |
__PROTECT__(min); |
|
259 |
max = __MKFLOAT(_max); |
|
260 |
__UNPROTECT__(min); |
|
261 |
RETURN (__ARRAY_WITH2(min, max)); |
|
262 |
} |
|
263 |
empty = true; |
|
264 |
} |
|
265 |
%}. |
|
266 |
empty == true ifTrue:[ |
|
267 |
^ self emptyCollectionError. |
|
268 |
]. |
|
269 |
"/ fallback if no primitive code |
|
270 |
^ super minMax |
|
271 |
||
272 |
" |
|
273 |
|f1| |
|
274 |
||
275 |
f1 := (1 to:10000) asDoubleArray. |
|
276 |
Time millisecondsToRun:[ 1000 timesRepeat:[ f1 minMax ] ] |
|
277 |
" |
|
278 |
" |
|
279 |
|f1| |
|
280 |
||
281 |
f1 := (1 to:10000) asDoubleArray. |
|
282 |
Time millisecondsToRun:[ 1000 timesRepeat:[ f1 min ] ] |
|
283 |
" |
|
284 |
" |
|
285 |
|f1| |
|
286 |
||
287 |
f1 := DoubleArray withAll:#(1 2 3 4 5). |
|
288 |
f1 minMax |
|
289 |
" |
|
290 |
" |
|
291 |
|f1| |
|
292 |
||
293 |
f1 := DoubleArray withAll:#(5 4 3 2 1). |
|
294 |
f1 minMax |
|
295 |
" |
|
7220 | 296 |
! ! |
297 |
||
19031 | 298 |
!DoubleArray methodsFor:'vector arithmetic'! |
299 |
||
300 |
dot: aFloatVector |
|
301 |
"Return the dot product of the receiver and the argument. |
|
302 |
Raises an error, if the argument is not of the same size as the receiver." |
|
303 |
||
304 |
| mySize result | |
|
305 |
||
306 |
%{ |
|
307 |
if ((__ClassInstPtr(__qClass(self))->c_ninstvars == __mkSmallInteger(0)) |
|
308 |
&& (__ClassInstPtr(__qClass(aFloatVector))->c_ninstvars == __mkSmallInteger(0))) { |
|
309 |
INT __mySize = __doubleArraySize(self); |
|
310 |
double __result = 0.0; |
|
311 |
double *__p1 = __DoubleArrayInstPtr(self)->d_element; |
|
312 |
||
313 |
if (__mySize > 0) { |
|
314 |
if (__isFloats(aFloatVector)) { |
|
315 |
INT __otherSize = __floatArraySize(aFloatVector); |
|
316 |
||
317 |
if (__mySize == __otherSize) { |
|
318 |
float *__p2 = __FloatArrayInstPtr(aFloatVector)->f_element; |
|
319 |
INT __i; |
|
320 |
/* how about inline-mmx-asm for this ... */ |
|
321 |
for (__i=0; __i<__mySize; __i++) { |
|
322 |
__result += (__p1[__i] * __p2[__i]); |
|
323 |
} |
|
324 |
RETURN (__MKFLOAT(__result)); |
|
325 |
} |
|
326 |
} else if (__isDoubles(aFloatVector)) { |
|
327 |
INT __otherSize = __doubleArraySize(aFloatVector); |
|
328 |
||
329 |
if (__mySize == __otherSize) { |
|
330 |
double *__p2 = __DoubleArrayInstPtr(aFloatVector)->d_element; |
|
331 |
INT __i; |
|
332 |
/* how about inline-mmx-asm for this ... */ |
|
333 |
for (__i=0; __i<__mySize; __i++) { |
|
334 |
__result += (__p1[__i] * __p2[__i]); |
|
335 |
} |
|
336 |
RETURN (__MKFLOAT(__result)); |
|
337 |
} |
|
338 |
} |
|
339 |
} |
|
340 |
} |
|
341 |
%}. |
|
342 |
^ super dot:aFloatVector |
|
343 |
||
344 |
" |
|
345 |
|v| |
|
346 |
||
347 |
v := #(2.0 2.0 1.0) asFloatArray. |
|
348 |
v dot:v. |
|
349 |
" |
|
350 |
" |
|
351 |
|v1 v2| |
|
352 |
||
353 |
v1 := FloatArray new:10000 withAll:2. |
|
354 |
v2 := FloatArray new:10000 withAll:3. |
|
355 |
Time millisecondsToRun:[ |
|
356 |
10000 timesRepeat:[ |
|
357 |
v1 dot:v2. |
|
358 |
] |
|
359 |
] |
|
360 |
" |
|
361 |
||
362 |
"Created: / 29-05-2007 / 13:13:39 / cg" |
|
363 |
! |
|
364 |
||
365 |
hornerMultiplyAndAdd:x |
|
366 |
"primitive support for horner's-method computation of polynomials. |
|
367 |
The vector is interpreted as providing the factors for a polynomial, |
|
368 |
an*x^n + (an-1)*x^(n-1) + ... + a2(x) + a1 |
|
369 |
where the ai are the elements of the Array. |
|
370 |
The highest rank factor is at the first position, the 0-rank constant at last. |
|
371 |
This is inlined c-code, which may get compiled to fast machine code, |
|
372 |
using multiply-and-add or vector instructions, if the CPU/Compiler support them." |
|
373 |
||
374 |
| mySize result | |
|
375 |
||
376 |
%{ |
|
377 |
double __x; |
|
378 |
||
379 |
if (__isFloat(x)) { |
|
380 |
__x = __floatVal(x); |
|
381 |
} else if (__isShortFloat(x)) { |
|
382 |
__x = (double)__shortFloatVal(x); |
|
383 |
} else if (__isSmallInteger(x)) { |
|
384 |
__x = (double)(__intVal(x)); |
|
385 |
} else { |
|
386 |
goto getOutOfHere; |
|
387 |
} |
|
388 |
||
389 |
if (__ClassInstPtr(__qClass(self))->c_ninstvars == __mkSmallInteger(0)) { |
|
390 |
INT __mySize = __doubleArraySize(self); |
|
391 |
double *__elements = __DoubleArrayInstPtr(self)->d_element; |
|
392 |
double __result = __elements[0]; |
|
393 |
||
394 |
if (__mySize > 1) { |
|
395 |
INT __i; |
|
396 |
/* how about inline-mmx-asm for this ... */ |
|
397 |
for (__i=1; __i<__mySize; __i++) { |
|
398 |
__result = (__result * __x) + __elements[__i]; |
|
399 |
} |
|
400 |
} |
|
401 |
RETURN (__MKFLOAT(__result)); |
|
402 |
} |
|
403 |
getOutOfHere: ; |
|
404 |
%}. |
|
405 |
^ super hornerMultiplyAndAdd:x |
|
406 |
||
407 |
" |
|
408 |
|v| |
|
409 |
v := #(2.0 3.0 4.0) asDoubleArray. |
|
410 |
v hornerMultiplyAndAdd:10. |
|
411 |
||
412 |
|v| |
|
413 |
v := Array new:100 withAll:2.0. |
|
414 |
v hornerMultiplyAndAdd:10 |
|
415 |
||
416 |
|v| |
|
417 |
v := Array new:100 withAll:2.0. |
|
418 |
Time millisecondsToRun:[ |
|
419 |
10000 timesRepeat:[ v hornerMultiplyAndAdd:10] |
|
420 |
] |
|
421 |
||
422 |
|v| |
|
423 |
v := FloatArray new:100 withAll:2. |
|
424 |
Time millisecondsToRun:[ |
|
425 |
10000 timesRepeat:[ v hornerMultiplyAndAdd:10] |
|
426 |
] |
|
427 |
||
428 |
|v| |
|
429 |
v := DoubleArray new:100 withAll:2. |
|
430 |
Time millisecondsToRun:[ |
|
431 |
10000 timesRepeat:[ v hornerMultiplyAndAdd:10] |
|
432 |
] |
|
433 |
" |
|
434 |
! ! |
|
435 |
||
20314 | 436 |
|
627 | 437 |
!DoubleArray class methodsFor:'documentation'! |
565 | 438 |
|
439 |
version |
|
19031 | 440 |
^ '$Header$' |
16853 | 441 |
! |
442 |
||
443 |
version_CVS |
|
19031 | 444 |
^ '$Header$' |
88 | 445 |
! ! |
16639 | 446 |