author | Jan Vrany <jan.vrany@fit.cvut.cz> |
Wed, 19 Oct 2016 09:22:53 +0100 | |
branch | jv |
changeset 23071 | 77ad9497363c |
parent 20344 | 152b525b5c63 |
child 21089 | d50f93ca9622 |
permissions | -rw-r--r-- |
20300 | 1 |
"{ Encoding: utf8 }" |
2 |
||
1 | 3 |
" |
4 |
COPYRIGHT (c) 1993 by Claus Gittinger |
|
20313 | 5 |
All Rights Reserved |
1 | 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 |
" |
|
5737 | 14 |
"{ Package: 'stx:libbasic' }" |
15 |
||
18577 | 16 |
"{ NameSpace: Smalltalk }" |
17 |
||
13826 | 18 |
AbstractNumberVector variableFloatSubclass:#FloatArray |
20313 | 19 |
instanceVariableNames:'' |
20 |
classVariableNames:'' |
|
21 |
poolDictionaries:'' |
|
22 |
category:'Collections-Arrayed' |
|
1 | 23 |
! |
24 |
||
88 | 25 |
!FloatArray class methodsFor:'documentation'! |
26 |
||
27 |
copyright |
|
28 |
" |
|
29 |
COPYRIGHT (c) 1993 by Claus Gittinger |
|
20313 | 30 |
All Rights Reserved |
1 | 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 |
" |
|
43 |
FloatArrays store floats (and nothing else). |
|
16638 | 44 |
They have been added to support heavy duty number crunching and |
19003 | 45 |
data exchange with openGL frameworks and other mass data libraries. |
88 | 46 |
See documentation in DoubleArray for more information. |
1263 | 47 |
|
4062 | 48 |
[memory requirements:] |
20313 | 49 |
OBJ-HEADER + (size * float-size) |
4062 | 50 |
|
1263 | 51 |
[See also:] |
20313 | 52 |
DoubleArray Array |
1290 | 53 |
|
54 |
[author:] |
|
20313 | 55 |
Claus Gittinger |
88 | 56 |
" |
628 | 57 |
! ! |
58 |
||
20313 | 59 |
|
60 |
||
13707
a8f395adfac2
faster elementByteSize query
Claus Gittinger <cg@exept.de>
parents:
12577
diff
changeset
|
61 |
!FloatArray class methodsFor:'queries'! |
a8f395adfac2
faster elementByteSize query
Claus Gittinger <cg@exept.de>
parents:
12577
diff
changeset
|
62 |
|
a8f395adfac2
faster elementByteSize query
Claus Gittinger <cg@exept.de>
parents:
12577
diff
changeset
|
63 |
elementByteSize |
16849 | 64 |
"for bit-like containers, return the number of bytes stored per element. |
65 |
Here, 4 is returned" |
|
66 |
||
13707
a8f395adfac2
faster elementByteSize query
Claus Gittinger <cg@exept.de>
parents:
12577
diff
changeset
|
67 |
^ 4 |
a8f395adfac2
faster elementByteSize query
Claus Gittinger <cg@exept.de>
parents:
12577
diff
changeset
|
68 |
|
a8f395adfac2
faster elementByteSize query
Claus Gittinger <cg@exept.de>
parents:
12577
diff
changeset
|
69 |
"Created: / 15-09-2011 / 14:12:39 / cg" |
a8f395adfac2
faster elementByteSize query
Claus Gittinger <cg@exept.de>
parents:
12577
diff
changeset
|
70 |
! ! |
a8f395adfac2
faster elementByteSize query
Claus Gittinger <cg@exept.de>
parents:
12577
diff
changeset
|
71 |
|
20313 | 72 |
|
5737 | 73 |
!FloatArray methodsFor:'copying'! |
74 |
||
75 |
clone |
|
76 |
"return a copy of the receiver" |
|
77 |
||
78 |
|newArr| |
|
79 |
||
80 |
newArr := self class new:(self size). |
|
81 |
%{ |
|
8913 | 82 |
if (__ClassInstPtr(__qClass(self))->c_ninstvars == __mkSmallInteger(0)) { |
20313 | 83 |
INT _sz = __floatArraySize(self); |
5737 | 84 |
|
20313 | 85 |
bcopy(__FloatArrayInstPtr(self)->f_element, |
86 |
__FloatArrayInstPtr(newArr)->f_element, |
|
87 |
sizeof(float) * _sz); |
|
5737 | 88 |
|
20313 | 89 |
RETURN (newArr); |
5737 | 90 |
} |
91 |
%}. |
|
92 |
newArr replaceFrom:1 to:self size with:self startingAt:1. |
|
93 |
^ newArr |
|
94 |
||
95 |
" |
|
96 |
|f1 f2| |
|
97 |
||
98 |
f1 := FloatArray withAll:#(1 2 3 4 5). |
|
14692 | 99 |
f1 clone |
5737 | 100 |
" |
101 |
! |
|
102 |
||
103 |
copyFrom:start to:stop |
|
104 |
"return a partial copy of the receiver" |
|
105 |
||
106 |
%{ |
|
8913 | 107 |
if (__ClassInstPtr(__qClass(self))->c_ninstvars == __mkSmallInteger(0)) { |
20313 | 108 |
if (__bothSmallInteger(start, stop)) { |
109 |
INT __start = __intVal(start) - 1; |
|
110 |
INT __stop = __intVal(stop) - 1 ; |
|
5737 | 111 |
|
20313 | 112 |
if (__stop >= __start) { |
113 |
INT __sz = __floatArraySize(self); |
|
114 |
// printf("o_size=%d floatArraySize=%d\n", __qSize(self), __sz); |
|
5737 | 115 |
|
20313 | 116 |
if (((unsigned INT)__start < __sz) |
117 |
&& ((unsigned INT)__stop < __sz)) { |
|
118 |
INT __n = __stop - __start + 1; |
|
119 |
OBJ __nObj; |
|
120 |
int nBytes; |
|
5737 | 121 |
|
20313 | 122 |
// don't do this; sizeof returns the padded size! |
123 |
// nBytes = sizeof(struct __FloatArray) + (__n - 1) * sizeof(float); |
|
124 |
nBytes = OHDR_SIZE + (__n * sizeof(float)); |
|
20303
060fea1cfebe
fixes for float/doubleArray alignemnt
Claus Gittinger <cg@exept.de>
parents:
20300
diff
changeset
|
125 |
|
20313 | 126 |
// printf("__n=%d nBytes=%d\n", __n, nBytes); |
127 |
__nObj = __STX___new(nBytes); |
|
128 |
if (__nObj != nil) { |
|
129 |
__objPtr(__nObj)->o_class = __qClass(self); |
|
130 |
__STORE(__nObj, __qClass(self)); |
|
131 |
bcopy(__FloatArrayInstPtr(self)->f_element + __start, |
|
132 |
__FloatArrayInstPtr(__nObj)->f_element, |
|
133 |
sizeof(float) * __n); |
|
134 |
RETURN(__nObj); |
|
135 |
} |
|
136 |
} |
|
137 |
} |
|
138 |
} |
|
5737 | 139 |
} |
14692 | 140 |
%}. |
5737 | 141 |
^ super copyFrom:start to:stop |
142 |
||
143 |
" |
|
144 |
|f1 f2| |
|
145 |
||
146 |
f1 := FloatArray withAll:#(1 2 3 4 5 6). |
|
14692 | 147 |
f2 := f1 copyFrom:2 to:4. |
148 |
f2 |
|
5737 | 149 |
" |
150 |
! |
|
151 |
||
152 |
replaceFrom:start to:stop with:aCollection startingAt:replStart |
|
153 |
%{ |
|
8913 | 154 |
if ((__ClassInstPtr(__qClass(self))->c_ninstvars == __mkSmallInteger(0)) |
20313 | 155 |
&& __isFloats(aCollection) |
156 |
&& (__ClassInstPtr(__qClass(aCollection))->c_ninstvars == __mkSmallInteger(0)) |
|
157 |
&& __bothSmallInteger(start, stop) |
|
158 |
&& __isSmallInteger(replStart) |
|
18577 | 159 |
) { |
20313 | 160 |
INT __start = __intVal(start) - 1; |
161 |
INT __stop = __intVal(stop) - 1 ; |
|
162 |
INT __replStart = __intVal(replStart) - 1 ; |
|
5737 | 163 |
|
20313 | 164 |
if (__stop >= __start) { |
165 |
INT __sz = __floatArraySize(self); |
|
166 |
INT __otherSz = __floatArraySize(aCollection); |
|
167 |
INT __replStop = __replStart + (__stop-__start); |
|
5737 | 168 |
|
20313 | 169 |
if (((unsigned INT)__start < __sz) |
170 |
&& ((unsigned INT)__stop < __sz) |
|
171 |
&& ((unsigned INT)__replStart < __otherSz) |
|
172 |
&& ((unsigned INT)__replStop < __otherSz)) { |
|
173 |
INT __n = __stop - __start + 1; |
|
5737 | 174 |
|
20313 | 175 |
if (aCollection == self) { |
176 |
memmove(&(__FloatArrayInstPtr(self)->f_element[__start]), |
|
177 |
&(__FloatArrayInstPtr(aCollection)->f_element[__replStart]), |
|
178 |
sizeof(float) * __n); |
|
179 |
} else { |
|
180 |
memcpy(&(__FloatArrayInstPtr(self)->f_element[__start]), |
|
181 |
&(__FloatArrayInstPtr(aCollection)->f_element[__replStart]), |
|
182 |
sizeof(float) * __n); |
|
183 |
} |
|
184 |
RETURN(self); |
|
185 |
} |
|
186 |
} |
|
5737 | 187 |
} |
188 |
%}. |
|
189 |
^ super replaceFrom:start to:stop with:aCollection startingAt:replStart |
|
190 |
||
191 |
" |
|
192 |
|f1 f2| |
|
193 |
||
194 |
f1 := (1 to:5) asFloatArray. |
|
195 |
f2 := #(10 9 8 7 6) asFloatArray. |
|
14692 | 196 |
f1 replaceFrom:1 to:3 with:f2 startingAt:3 |
5737 | 197 |
" |
198 |
! ! |
|
199 |
||
17198 | 200 |
!FloatArray methodsFor:'destructive arithmetic support'! |
201 |
||
202 |
primAbs |
|
203 |
"destructive absolute value of each element" |
|
204 |
||
205 |
%{ |
|
206 |
if (__ClassInstPtr(__qClass(self))->c_ninstvars == __mkSmallInteger(0)) { |
|
20313 | 207 |
INT _sz = __floatArraySize(self); |
208 |
INT i; |
|
209 |
float *_p = __FloatArrayInstPtr(self)->f_element; |
|
210 |
float prev_p; |
|
17198 | 211 |
|
20313 | 212 |
if (_sz > 0) { |
213 |
/* how about inline-mmx-asm for this ... */ |
|
214 |
prev_p = _p[0]; |
|
215 |
for (i=1; i<_sz; i++) { |
|
216 |
float next_p = _p[i]; |
|
17198 | 217 |
|
20313 | 218 |
if (prev_p < 0) { |
219 |
_p[i-1] = -prev_p; |
|
220 |
} |
|
221 |
prev_p = next_p; |
|
222 |
} |
|
223 |
if (prev_p < 0) { |
|
224 |
_p[i-1] = -prev_p; |
|
225 |
} |
|
226 |
} |
|
227 |
RETURN (self); |
|
17198 | 228 |
} |
229 |
%}. |
|
230 |
super primAbs |
|
231 |
||
232 |
" |
|
233 |
|f| |
|
234 |
||
235 |
f := FloatArray withAll:#(-1 2 -3 4 -5). |
|
236 |
f abs. |
|
237 |
f |
|
238 |
" |
|
239 |
||
240 |
"Created: / 30-05-2007 / 17:50:17 / cg" |
|
241 |
! |
|
242 |
||
243 |
primAddArray: floatArray |
|
244 |
"add the vector argument into the receiver (destructive). |
|
245 |
The argument must be another vector" |
|
246 |
||
247 |
%{ |
|
248 |
if ((__ClassInstPtr(__qClass(self))->c_ninstvars == __mkSmallInteger(0)) |
|
249 |
&& __isFloats(floatArray) |
|
250 |
&& (__ClassInstPtr(__qClass(floatArray))->c_ninstvars == __mkSmallInteger(0))) { |
|
20313 | 251 |
INT _sz1 = __floatArraySize(self); |
252 |
INT _sz2 = __floatArraySize(floatArray); |
|
253 |
INT i; |
|
254 |
float *_p1 = __FloatArrayInstPtr(self)->f_element; |
|
255 |
float *_p2 = __FloatArrayInstPtr(floatArray)->f_element; |
|
17198 | 256 |
|
20313 | 257 |
if (_sz1 > 0) { |
258 |
if (_sz2 >= _sz1) { |
|
259 |
/* how about inline-mmx-asm for this ... */ |
|
260 |
float prev_p1 = _p1[0]; |
|
261 |
float prev_p2 = _p2[0]; |
|
17198 | 262 |
|
20313 | 263 |
for (i=1; i<_sz1; i++) { |
264 |
float next_p1 = _p1[i]; |
|
265 |
float next_p2 = _p2[i]; |
|
266 |
_p1[i-1] = prev_p1 + prev_p2; |
|
267 |
prev_p1 = next_p1; |
|
268 |
prev_p2 = next_p2; |
|
269 |
} |
|
270 |
_p1[i-1] = prev_p1 + prev_p2; |
|
271 |
} |
|
272 |
} |
|
273 |
RETURN (self); |
|
17198 | 274 |
} |
275 |
%}. |
|
276 |
super primAddArray:floatArray |
|
277 |
||
278 |
" |
|
279 |
|f1 f2| |
|
280 |
||
281 |
f1 := FloatArray withAll:#(1 2 3 4 5). |
|
282 |
f2 := FloatArray withAll:#(2 2 2 3 3). |
|
283 |
f1 += f2. |
|
284 |
f1 |
|
285 |
" |
|
286 |
! |
|
287 |
||
288 |
primAddScalar: aScalar |
|
289 |
"add the scalar argument into the receiver (destructive)." |
|
290 |
||
291 |
%{ |
|
292 |
if (__ClassInstPtr(__qClass(self))->c_ninstvars == __mkSmallInteger(0)) { |
|
20313 | 293 |
INT _sz1 = __floatArraySize(self); |
294 |
INT i; |
|
295 |
float *_p1 = __FloatArrayInstPtr(self)->f_element; |
|
296 |
float v; |
|
297 |
float prev_p1; |
|
17198 | 298 |
|
20313 | 299 |
if (_sz1 > 0) { |
300 |
if (__isFloat(aScalar)) { |
|
301 |
v = (float)(__floatVal(aScalar)); |
|
302 |
} else if (__isShortFloat(aScalar)) { |
|
303 |
v = __shortFloatVal(aScalar); |
|
304 |
} else if (__isSmallInteger(aScalar)) { |
|
305 |
v = (float)(__intVal(aScalar)); |
|
306 |
} else |
|
307 |
goto badArg; |
|
17198 | 308 |
|
20313 | 309 |
/* how about inline-mmx-asm for this ... */ |
310 |
prev_p1 = _p1[0]; |
|
311 |
for (i=1; i<_sz1; i++) { |
|
312 |
float next_p1 = _p1[i]; |
|
313 |
_p1[i-1] = prev_p1 + v; |
|
314 |
prev_p1 = next_p1; |
|
315 |
} |
|
316 |
_p1[i-1] = prev_p1 + v; |
|
317 |
} |
|
318 |
RETURN (self); |
|
17198 | 319 |
} |
320 |
badArg: ; |
|
321 |
%}. |
|
322 |
super primAddScalar:aScalar |
|
323 |
||
324 |
" |
|
325 |
|f1 f2| |
|
326 |
||
327 |
f1 := FloatArray withAll:#(1 2 3 4 5). |
|
328 |
f1 += 2.0. |
|
329 |
Transcript showCR:f1. |
|
330 |
f1 += 2.0 asShortFloat. |
|
331 |
Transcript showCR:f1. |
|
332 |
f1 += 2. |
|
333 |
Transcript showCR:f1. |
|
334 |
" |
|
335 |
! |
|
336 |
||
337 |
primDivArray: floatArray |
|
338 |
"divide the vector argument into the receiver (destructive). |
|
339 |
The argument must be another vector" |
|
340 |
||
341 |
%{ |
|
342 |
if ((__ClassInstPtr(__qClass(self))->c_ninstvars == __mkSmallInteger(0)) |
|
343 |
&& __isFloats(floatArray) |
|
344 |
&& (__ClassInstPtr(__qClass(floatArray))->c_ninstvars == __mkSmallInteger(0))) { |
|
20313 | 345 |
INT _sz1 = __floatArraySize(self); |
346 |
INT _sz2 = __floatArraySize(floatArray); |
|
347 |
INT i; |
|
348 |
float *_p1 = __FloatArrayInstPtr(self)->f_element; |
|
349 |
float *_p2 = __FloatArrayInstPtr(floatArray)->f_element; |
|
17198 | 350 |
|
20313 | 351 |
if (_sz1 > 0) { |
352 |
if (_sz2 >= _sz1) { |
|
353 |
/* how about inline-mmx-asm for this ... */ |
|
354 |
float prev_p1 = _p1[0]; |
|
355 |
float prev_p2 = _p2[0]; |
|
17198 | 356 |
|
20313 | 357 |
for (i=1; i<_sz1; i++) { |
358 |
float next_p1 = _p1[i]; |
|
359 |
float next_p2 = _p2[i]; |
|
360 |
_p1[i-1] = prev_p1 / prev_p2; |
|
361 |
prev_p1 = next_p1; |
|
362 |
prev_p2 = next_p2; |
|
363 |
} |
|
364 |
_p1[i-1] = prev_p1 / prev_p2; |
|
365 |
} |
|
366 |
} |
|
367 |
RETURN (self); |
|
17198 | 368 |
} |
369 |
%}. |
|
370 |
super primDivArray: floatArray |
|
371 |
||
372 |
" |
|
373 |
|f1 f2| |
|
374 |
||
375 |
f1 := FloatArray withAll:#(1 2 3 4 5). |
|
376 |
f2 := FloatArray withAll:#(2 2 2 3 3). |
|
377 |
f1 /= f2. |
|
378 |
f1 |
|
379 |
" |
|
380 |
||
381 |
"Modified: / 29-05-2007 / 16:01:34 / cg" |
|
382 |
! |
|
383 |
||
384 |
primDivScalar: aScalar |
|
385 |
"divide the scalar argument into the receiver (destructive)." |
|
386 |
||
387 |
%{ |
|
388 |
if (__ClassInstPtr(__qClass(self))->c_ninstvars == __mkSmallInteger(0)) { |
|
20313 | 389 |
INT _sz1 = __floatArraySize(self); |
390 |
INT i; |
|
391 |
float *_p1 = __FloatArrayInstPtr(self)->f_element; |
|
392 |
float v; |
|
393 |
float prev_p1; |
|
17198 | 394 |
|
20313 | 395 |
if (_sz1 > 0) { |
396 |
if (__isFloat(aScalar)) { |
|
397 |
v = (float)(__floatVal(aScalar)); |
|
398 |
} else if (__isShortFloat(aScalar)) { |
|
399 |
v = __shortFloatVal(aScalar); |
|
400 |
} else if (__isSmallInteger(aScalar)) { |
|
401 |
v = (float)(__intVal(aScalar)); |
|
402 |
} else |
|
403 |
goto badArg; |
|
17198 | 404 |
|
20313 | 405 |
/* how about inline-mmx-asm for this ... */ |
406 |
prev_p1 = _p1[0]; |
|
407 |
for (i=1; i<_sz1; i++) { |
|
408 |
float next_p1 = _p1[i]; |
|
409 |
_p1[i-1] = prev_p1 / v; |
|
410 |
prev_p1 = next_p1; |
|
411 |
} |
|
412 |
_p1[i-1] = prev_p1 / v; |
|
413 |
} |
|
414 |
RETURN (self); |
|
17198 | 415 |
} |
416 |
badArg: ; |
|
417 |
%}. |
|
418 |
super primDivScalar:aScalar |
|
419 |
||
420 |
" |
|
421 |
|f1 f2| |
|
422 |
||
423 |
f1 := FloatArray withAll:#(1 2 3 4 5). |
|
424 |
f1 /= 2.0. |
|
425 |
Transcript showCR:f1. |
|
426 |
f1 /= 2.0 asShortFloat. |
|
427 |
Transcript showCR:f1. |
|
428 |
f1 /= 2. |
|
429 |
Transcript showCR:f1. |
|
430 |
" |
|
431 |
||
432 |
"Modified: / 29-05-2007 / 16:01:39 / cg" |
|
433 |
! |
|
434 |
||
435 |
primMulArray: floatArray |
|
436 |
"multiply the vector argument into the receiver (destructive). |
|
437 |
The argument must be another vector" |
|
438 |
||
439 |
%{ |
|
440 |
if ((__ClassInstPtr(__qClass(self))->c_ninstvars == __mkSmallInteger(0)) |
|
441 |
&& __isFloats(floatArray) |
|
442 |
&& (__ClassInstPtr(__qClass(floatArray))->c_ninstvars == __mkSmallInteger(0))) { |
|
20313 | 443 |
INT _sz1 = __floatArraySize(self); |
444 |
INT _sz2 = __floatArraySize(floatArray); |
|
445 |
INT i; |
|
446 |
float *_p1 = __FloatArrayInstPtr(self)->f_element; |
|
447 |
float *_p2 = __FloatArrayInstPtr(floatArray)->f_element; |
|
17198 | 448 |
|
20313 | 449 |
if (_sz1 > 0) { |
450 |
if (_sz2 >= _sz1) { |
|
451 |
float prev_p1 = _p1[0]; |
|
452 |
float prev_p2 = _p2[0]; |
|
17198 | 453 |
|
20313 | 454 |
for (i=1; i<_sz1; i++) { |
455 |
float next_p1 = _p1[i]; |
|
456 |
float next_p2 = _p2[i]; |
|
457 |
_p1[i-1] = prev_p1 * prev_p2; |
|
458 |
prev_p1 = next_p1; |
|
459 |
prev_p2 = next_p2; |
|
460 |
} |
|
461 |
_p1[i-1] = prev_p1 * prev_p2; |
|
462 |
} |
|
463 |
} |
|
464 |
RETURN (self); |
|
17198 | 465 |
} |
466 |
%}. |
|
467 |
super primMulArray: floatArray |
|
468 |
||
469 |
" |
|
470 |
|f1 f2| |
|
471 |
||
472 |
f1 := FloatArray withAll:#(1 2 3 4 5). |
|
473 |
f2 := FloatArray withAll:#(2 2 2 3 3). |
|
474 |
f1 *= f2. |
|
475 |
f1 |
|
476 |
" |
|
477 |
! |
|
478 |
||
479 |
primMulScalar: aScalar |
|
480 |
"multiply the scalar argument into the receiver (destructive)." |
|
481 |
||
482 |
%{ |
|
483 |
if (__ClassInstPtr(__qClass(self))->c_ninstvars == __mkSmallInteger(0)) { |
|
20313 | 484 |
INT _sz1 = __floatArraySize(self); |
485 |
INT i; |
|
486 |
float *_p1 = __FloatArrayInstPtr(self)->f_element; |
|
487 |
float v; |
|
488 |
float prev_p1; |
|
17198 | 489 |
|
20313 | 490 |
if (_sz1 > 0) { |
491 |
if (__isFloat(aScalar)) { |
|
492 |
v = (float)(__floatVal(aScalar)); |
|
493 |
} else if (__isShortFloat(aScalar)) { |
|
494 |
v = __shortFloatVal(aScalar); |
|
495 |
} else if (__isSmallInteger(aScalar)) { |
|
496 |
v = (float)(__intVal(aScalar)); |
|
497 |
} else |
|
498 |
goto badArg; |
|
17198 | 499 |
|
20313 | 500 |
/* how about inline-mmx-asm for this ... */ |
501 |
prev_p1 = _p1[0]; |
|
502 |
for (i=1; i<_sz1; i++) { |
|
503 |
float next_p1 = _p1[i]; |
|
504 |
_p1[i-1] = prev_p1 * v; |
|
505 |
prev_p1 = next_p1; |
|
506 |
} |
|
507 |
_p1[i-1] = prev_p1 * v; |
|
508 |
} |
|
509 |
RETURN (self); |
|
17198 | 510 |
} |
511 |
badArg: ; |
|
512 |
%}. |
|
513 |
super primMulScalar:aScalar |
|
514 |
||
515 |
" |
|
516 |
|f1 f2| |
|
517 |
||
518 |
f1 := FloatArray withAll:#(1 2 3 4 5). |
|
519 |
f1 *= 2.0. |
|
520 |
Transcript showCR:f1. |
|
521 |
f1 *= 2.0 asShortFloat. |
|
522 |
Transcript showCR:f1. |
|
523 |
f1 *= 2. |
|
524 |
Transcript showCR:f1. |
|
525 |
" |
|
526 |
! |
|
527 |
||
528 |
primNegated |
|
529 |
"destructive negative value of each element" |
|
530 |
||
531 |
%{ |
|
532 |
if (__ClassInstPtr(__qClass(self))->c_ninstvars == __mkSmallInteger(0)) { |
|
20313 | 533 |
INT _sz = __floatArraySize(self); |
534 |
INT i; |
|
535 |
float *_p = __FloatArrayInstPtr(self)->f_element; |
|
536 |
float prev_p; |
|
17198 | 537 |
|
20313 | 538 |
if (_sz > 0) { |
539 |
/* how about inline-mmx-asm for this ... */ |
|
540 |
prev_p = _p[0]; |
|
541 |
for (i=1; i<_sz; i++) { |
|
542 |
float next_p = _p[i]; |
|
17198 | 543 |
|
20313 | 544 |
_p[i-1] = -prev_p; |
545 |
prev_p = next_p; |
|
546 |
} |
|
547 |
_p[i-1] = -prev_p; |
|
548 |
} |
|
549 |
RETURN (self); |
|
17198 | 550 |
} |
551 |
%}. |
|
552 |
super primNegated |
|
553 |
||
554 |
" |
|
555 |
|f| |
|
556 |
||
557 |
f := FloatArray withAll:#(-1 2 -3 4 -5). |
|
558 |
f negated. |
|
559 |
f |
|
560 |
" |
|
561 |
||
562 |
"Created: / 30-05-2007 / 17:51:29 / cg" |
|
563 |
! |
|
564 |
||
565 |
primSubtractArray: floatArray |
|
566 |
"subtract the vector argument from the receiver (destructive). |
|
567 |
The argument must be another vector" |
|
568 |
||
569 |
%{ |
|
570 |
if ((__ClassInstPtr(__qClass(self))->c_ninstvars == __mkSmallInteger(0)) |
|
571 |
&& __isFloats(floatArray) |
|
572 |
&& (__ClassInstPtr(__qClass(floatArray))->c_ninstvars == __mkSmallInteger(0))) { |
|
20313 | 573 |
INT _sz1 = __floatArraySize(self); |
574 |
INT _sz2 = __floatArraySize(floatArray); |
|
575 |
INT i; |
|
576 |
float *_p1 = __FloatArrayInstPtr(self)->f_element; |
|
577 |
float *_p2 = __FloatArrayInstPtr(floatArray)->f_element; |
|
17198 | 578 |
|
20313 | 579 |
if (_sz1 > 0) { |
580 |
if (_sz2 >= _sz1) { |
|
581 |
float prev_p1 = _p1[0]; |
|
582 |
float prev_p2 = _p2[0]; |
|
17198 | 583 |
|
20313 | 584 |
for (i=1; i<_sz1; i++) { |
585 |
float next_p1 = _p1[i]; |
|
586 |
float next_p2 = _p2[i]; |
|
587 |
_p1[i-1] = prev_p1 - prev_p2; |
|
588 |
prev_p1 = next_p1; |
|
589 |
prev_p2 = next_p2; |
|
590 |
} |
|
591 |
_p1[i-1] = prev_p1 - prev_p2; |
|
592 |
} |
|
593 |
} |
|
594 |
RETURN (self); |
|
17198 | 595 |
} |
596 |
%}. |
|
597 |
super primSubtractArray: floatArray |
|
598 |
||
599 |
" |
|
600 |
|f1 f2| |
|
601 |
||
602 |
f1 := FloatArray withAll:#(1 2 3 4 5). |
|
603 |
f2 := FloatArray withAll:#(2 2 2 3 3). |
|
604 |
f1 -= f2. |
|
605 |
f1 |
|
606 |
" |
|
607 |
||
608 |
"Created: / 30-05-2007 / 17:42:41 / cg" |
|
609 |
! |
|
610 |
||
611 |
primSubtractScalar: aScalar |
|
612 |
"subtract the scalar argument from the receiver (destructive)." |
|
613 |
||
614 |
%{ |
|
615 |
if (__ClassInstPtr(__qClass(self))->c_ninstvars == __mkSmallInteger(0)) { |
|
20313 | 616 |
INT _sz1 = __floatArraySize(self); |
617 |
INT i; |
|
618 |
float *_p1 = __FloatArrayInstPtr(self)->f_element; |
|
619 |
float v; |
|
620 |
float prev_p1; |
|
17198 | 621 |
|
20313 | 622 |
if (_sz1 > 0) { |
623 |
if (__isFloat(aScalar)) { |
|
624 |
v = (float)(__floatVal(aScalar)); |
|
625 |
} else if (__isShortFloat(aScalar)) { |
|
626 |
v = __shortFloatVal(aScalar); |
|
627 |
} else if (__isSmallInteger(aScalar)) { |
|
628 |
v = (float)(__intVal(aScalar)); |
|
629 |
} else |
|
630 |
goto badArg; |
|
17198 | 631 |
|
20313 | 632 |
/* how about inline-mmx-asm for this... ? */ |
633 |
prev_p1 = _p1[0]; |
|
634 |
for (i=1; i<_sz1; i++) { |
|
635 |
float next_p1 = _p1[i]; |
|
636 |
_p1[i-1] = prev_p1 - v; |
|
637 |
prev_p1 = next_p1; |
|
638 |
} |
|
639 |
_p1[i-1] = prev_p1 - v; |
|
640 |
} |
|
641 |
RETURN (self); |
|
17198 | 642 |
} |
643 |
badArg: ; |
|
644 |
%}. |
|
645 |
super primSubtractScalar:aScalar |
|
646 |
||
647 |
" |
|
648 |
|f1 f2| |
|
649 |
||
650 |
f1 := FloatArray withAll:#(1 2 3 4 5). |
|
651 |
f1 -= 2.0. |
|
652 |
Transcript showCR:f1. |
|
653 |
f1 -= 2.0 asShortFloat. |
|
654 |
Transcript showCR:f1. |
|
655 |
f1 -= 2. |
|
656 |
Transcript showCR:f1. |
|
657 |
" |
|
658 |
||
659 |
"Created: / 30-05-2007 / 17:43:06 / cg" |
|
660 |
! ! |
|
661 |
||
5737 | 662 |
!FloatArray methodsFor:'queries'! |
663 |
||
7220 | 664 |
defaultElement |
665 |
^ ShortFloat zero |
|
666 |
! |
|
667 |
||
20300 | 668 |
isValidElement:anObject |
669 |
"return true, if I can hold this kind of object" |
|
670 |
||
671 |
^ anObject isNumber |
|
672 |
! |
|
673 |
||
5737 | 674 |
minMax |
11212 | 675 |
"return a Tuple holding the smallest and largest element; |
5737 | 676 |
redefined for speed" |
677 |
||
20313 | 678 |
|min max empty| |
5737 | 679 |
|
680 |
%{ |
|
8913 | 681 |
if (__ClassInstPtr(__qClass(self))->c_ninstvars == __mkSmallInteger(0)) { |
20313 | 682 |
INT _sz = __floatArraySize(self); |
683 |
||
684 |
if (_sz > 0) { |
|
685 |
INT _i; |
|
686 |
float *_p = __FloatArrayInstPtr(self)->f_element; |
|
687 |
float _min, _max; |
|
688 |
OBJ retVal; |
|
689 |
||
690 |
_min = _max = _p[0]; |
|
13774 | 691 |
#ifdef PRE_4_OCT_2011 |
20313 | 692 |
if (_sz > 1) { |
693 |
float _this = _p[1]; |
|
694 |
float _prev; |
|
7220 | 695 |
|
20313 | 696 |
/* how about inline-mmx-asm for this ... */ |
697 |
for (_i=2; _i<_sz; _i++) { |
|
698 |
_prev = _this; |
|
699 |
_this = _p[_i]; |
|
700 |
if (_prev < _min) { |
|
701 |
_min = _prev; |
|
702 |
} else if (_prev > _max) { |
|
703 |
_max = _prev; |
|
704 |
} |
|
705 |
} |
|
706 |
if (_this < _min) { |
|
707 |
_min = _this; |
|
708 |
} else if (_this > _max) { |
|
709 |
_max = _this; |
|
710 |
} |
|
711 |
} |
|
13774 | 712 |
#else |
20313 | 713 |
for (_i=_sz-1; _i>0; _i-=2) { |
714 |
float _v1 = _p[_i]; |
|
715 |
float _v2 = _p[_i-1]; |
|
716 |
if (_v1 < _v2) { |
|
717 |
if (_v1 < _min) _min = _v1; |
|
718 |
if (_v2 > _max) _max = _v2; |
|
719 |
} else { |
|
720 |
if (_v2 < _min) _min = _v2; |
|
721 |
if (_v1 > _max) _max = _v1; |
|
722 |
} |
|
723 |
} |
|
13774 | 724 |
#endif |
20313 | 725 |
min = __MKFLOAT(_min); |
726 |
__PROTECT__(min); |
|
727 |
max = __MKFLOAT(_max); |
|
728 |
__UNPROTECT__(min); |
|
729 |
RETURN (__ARRAY_WITH2(min, max)); |
|
730 |
} |
|
731 |
empty = true; |
|
7220 | 732 |
} |
5737 | 733 |
%}. |
20313 | 734 |
empty == true ifTrue:[ |
735 |
^ self emptyCollectionError. |
|
736 |
]. |
|
7220 | 737 |
^ Array with:(super min) with:(super max) |
5737 | 738 |
|
7220 | 739 |
" |
740 |
|f1| |
|
741 |
||
13774 | 742 |
f1 := (1 to:10000) asFloatArray. |
14692 | 743 |
Time millisecondsToRun:[ 1000 timesRepeat:[ f1 minMax ] ] |
7220 | 744 |
" |
745 |
" |
|
746 |
|f1| |
|
5737 | 747 |
|
7220 | 748 |
f1 := FloatArray withAll:#(1 2 3 4 5). |
14692 | 749 |
f1 minMax |
7220 | 750 |
" |
751 |
" |
|
752 |
|f1| |
|
5737 | 753 |
|
20313 | 754 |
f1 := FloatArray withAll:#(1 2 3 4). |
755 |
f1 minMax |
|
756 |
" |
|
757 |
" |
|
758 |
|f1| |
|
759 |
||
7220 | 760 |
f1 := FloatArray withAll:#(5 4 3 2 1). |
14692 | 761 |
f1 minMax |
7220 | 762 |
" |
13774 | 763 |
|
764 |
"Modified (comment): / 07-10-2011 / 13:03:30 / cg" |
|
10561 | 765 |
! |
766 |
||
12577 | 767 |
numFloats |
768 |
^ self size |
|
10561 | 769 |
! ! |
770 |
||
771 |
!FloatArray methodsFor:'vector arithmetic'! |
|
772 |
||
773 |
dot: aFloatVector |
|
774 |
"Return the dot product of the receiver and the argument. |
|
19032 | 775 |
Raises an error, if the argument is not of the same size as the receiver." |
10561 | 776 |
|
777 |
| mySize result | |
|
778 |
||
779 |
"/ <primitive:'primitiveDotProduct' module: 'FloatArrayPlugin'> |
|
780 |
%{ |
|
19032 | 781 |
if ((__ClassInstPtr(__qClass(self))->c_ninstvars == __mkSmallInteger(0)) |
782 |
&& (__ClassInstPtr(__qClass(aFloatVector))->c_ninstvars == __mkSmallInteger(0))) { |
|
20313 | 783 |
INT __mySize = __floatArraySize(self); |
784 |
double __result = 0.0; |
|
785 |
float *__p1 = __FloatArrayInstPtr(self)->f_element; |
|
19032 | 786 |
|
20313 | 787 |
if (__mySize > 0) { |
788 |
if (__isFloats(aFloatVector)) { |
|
789 |
INT __otherSize = __floatArraySize(aFloatVector); |
|
17198 | 790 |
|
20313 | 791 |
if (__mySize == __otherSize) { |
792 |
float *__p2 = __FloatArrayInstPtr(aFloatVector)->f_element; |
|
793 |
INT __i; |
|
794 |
/* how about inline-mmx-asm for this ... */ |
|
795 |
for (__i=0; __i<__mySize; __i++) { |
|
796 |
__result += (__p1[__i] * __p2[__i]); |
|
797 |
} |
|
798 |
RETURN (__MKFLOAT(__result)); |
|
799 |
} |
|
800 |
} else if (__isDoubles(aFloatVector)) { |
|
801 |
INT __otherSize = __doubleArraySize(aFloatVector); |
|
19032 | 802 |
|
20313 | 803 |
if (__mySize == __otherSize) { |
804 |
double *__p2 = __DoubleArrayInstPtr(aFloatVector)->d_element; |
|
805 |
INT __i; |
|
806 |
/* how about inline-mmx-asm for this ... */ |
|
807 |
for (__i=0; __i<__mySize; __i++) { |
|
808 |
__result += (__p1[__i] * __p2[__i]); |
|
809 |
} |
|
810 |
RETURN (__MKFLOAT(__result)); |
|
811 |
} |
|
812 |
} |
|
813 |
} |
|
10561 | 814 |
} |
815 |
%}. |
|
15935 | 816 |
^ super dot:aFloatVector |
10561 | 817 |
|
818 |
" |
|
819 |
|v| |
|
11212 | 820 |
v := #(2.0 2.0 1.0) asFloatArray. |
19003 | 821 |
v dot:v. |
19032 | 822 |
|
823 |
|v| |
|
824 |
v := #(2.0 2.0 1.0) asDoubleArray. |
|
825 |
v dot:v. |
|
826 |
||
827 |
|v| |
|
828 |
v := #(2.0 2.0 1.0). |
|
829 |
v dot:v. |
|
830 |
||
831 |
|v1 v2| |
|
832 |
v1 := #(2.0 2.0 1.0) asFloatArray. |
|
833 |
v2 := #(2.0 2.0 1.0) asDoubleArray. |
|
834 |
v1 dot:v2. |
|
835 |
||
836 |
|v1 v2| |
|
837 |
v1 := #(2.0 2.0 1.0) asDoubleArray. |
|
838 |
v2 := #(2.0 2.0 1.0) asFloatArray. |
|
839 |
v1 dot:v2. |
|
840 |
||
841 |
|v1 v2| |
|
842 |
v1 := #(2.0 2.0 1.0). |
|
843 |
v2 := #(2.0 2.0 1.0) asFloatArray. |
|
844 |
v1 dot:v2. |
|
845 |
||
846 |
|v1 v2| |
|
847 |
v1 := #(2.0 2.0 1.0) asFloatArray. |
|
848 |
v2 := #(2.0 2.0 1.0). |
|
849 |
v1 dot:v2. |
|
850 |
||
851 |
|v1 v2| |
|
852 |
v1 := #(2.0 2.0 1.0). |
|
853 |
v2 := #(2.0 2.0 1.0) asDoubleArray. |
|
854 |
v1 dot:v2. |
|
855 |
||
856 |
|v1 v2| |
|
857 |
v1 := #(2.0 2.0 1.0) asDoubleArray. |
|
858 |
v2 := #(2.0 2.0 1.0). |
|
859 |
v1 dot:v2. |
|
860 |
" |
|
20303
060fea1cfebe
fixes for float/doubleArray alignemnt
Claus Gittinger <cg@exept.de>
parents:
20300
diff
changeset
|
861 |
|
19032 | 862 |
" |
863 |
|v1 v2| |
|
864 |
||
865 |
v1 := Array new:10000 withAll:2. |
|
866 |
v2 := Array new:10000 withAll:3. |
|
867 |
Time millisecondsToRun:[ |
|
20313 | 868 |
10000 timesRepeat:[ |
869 |
v1 dot:v2. |
|
870 |
] |
|
19032 | 871 |
] |
17198 | 872 |
" |
873 |
" |
|
874 |
|v1 v2| |
|
875 |
||
876 |
v1 := FloatArray new:10000 withAll:2. |
|
19003 | 877 |
v2 := FloatArray new:10000 withAll:3. |
17198 | 878 |
Time millisecondsToRun:[ |
20313 | 879 |
10000 timesRepeat:[ |
880 |
v1 dot:v2. |
|
881 |
] |
|
19003 | 882 |
] |
10561 | 883 |
" |
884 |
||
19032 | 885 |
" |
886 |
|v1 v2| |
|
887 |
v1 := #(2.0 2.0 1.0) asFloatArray. |
|
888 |
v2 := #(2.0 2.0 1.0 0.0) asDoubleArray. |
|
889 |
v1 dot:v2. |
|
890 |
" |
|
891 |
||
20303
060fea1cfebe
fixes for float/doubleArray alignemnt
Claus Gittinger <cg@exept.de>
parents:
20300
diff
changeset
|
892 |
|
10561 | 893 |
"Created: / 29-05-2007 / 13:13:39 / cg" |
19032 | 894 |
! |
895 |
||
896 |
hornerMultiplyAndAdd:x |
|
897 |
"primitive support for horner's-method computation of polynomials. |
|
898 |
The vector is interpreted as providing the factors for a polynomial, |
|
20313 | 899 |
an*x^n + (an-1)*x^(n-1) + ... + a2(x) + a1 |
19032 | 900 |
where the ai are the elements of the Array. |
901 |
The highest rank factor is at the first position, the 0-rank constant at last. |
|
902 |
This is inlined c-code, which may get compiled to fast machine code, |
|
903 |
using multiply-and-add or vector instructions, if the CPU/Compiler support them." |
|
904 |
||
905 |
| mySize result | |
|
906 |
||
907 |
%{ |
|
908 |
double __x; |
|
20303
060fea1cfebe
fixes for float/doubleArray alignemnt
Claus Gittinger <cg@exept.de>
parents:
20300
diff
changeset
|
909 |
|
19032 | 910 |
if (__isFloat(x)) { |
20313 | 911 |
__x = __floatVal(x); |
19032 | 912 |
} else if (__isShortFloat(x)) { |
20313 | 913 |
__x = (double)__shortFloatVal(x); |
19032 | 914 |
} else if (__isSmallInteger(x)) { |
20313 | 915 |
__x = (double)(__intVal(x)); |
19032 | 916 |
} else { |
20313 | 917 |
goto getOutOfHere; |
19032 | 918 |
} |
20303
060fea1cfebe
fixes for float/doubleArray alignemnt
Claus Gittinger <cg@exept.de>
parents:
20300
diff
changeset
|
919 |
|
19032 | 920 |
if (__ClassInstPtr(__qClass(self))->c_ninstvars == __mkSmallInteger(0)) { |
20313 | 921 |
INT __mySize = __floatArraySize(self); |
922 |
float *__elements = __FloatArrayInstPtr(self)->f_element; |
|
923 |
double __result = __elements[0]; |
|
19032 | 924 |
|
20313 | 925 |
if (__mySize > 1) { |
926 |
INT __i; |
|
927 |
/* how about inline-mmx-asm for this ... */ |
|
928 |
for (__i=1; __i<__mySize; __i++) { |
|
929 |
__result = (__result * __x) + __elements[__i]; |
|
930 |
} |
|
931 |
} |
|
932 |
RETURN (__MKFLOAT(__result)); |
|
19032 | 933 |
} |
20303
060fea1cfebe
fixes for float/doubleArray alignemnt
Claus Gittinger <cg@exept.de>
parents:
20300
diff
changeset
|
934 |
getOutOfHere: ; |
19032 | 935 |
%}. |
936 |
^ super hornerMultiplyAndAdd:x |
|
937 |
||
938 |
" |
|
939 |
|v| |
|
940 |
v := #(2.0 3.0 4.0) asFloatArray. |
|
941 |
v hornerMultiplyAndAdd:10. |
|
942 |
||
943 |
|v| |
|
944 |
v := Array new:100 withAll:2.0. |
|
945 |
v hornerMultiplyAndAdd:10 |
|
946 |
||
947 |
|v| |
|
948 |
v := FloatArray new:100 withAll:2.0. |
|
949 |
v hornerMultiplyAndAdd:10 |
|
950 |
||
951 |
|v| |
|
952 |
v := Array new:100 withAll:2.0. |
|
953 |
Time millisecondsToRun:[ |
|
20313 | 954 |
10000 timesRepeat:[ v hornerMultiplyAndAdd:10] |
19032 | 955 |
] |
956 |
||
957 |
|v| |
|
958 |
v := FloatArray new:100 withAll:2. |
|
959 |
Time millisecondsToRun:[ |
|
20313 | 960 |
10000 timesRepeat:[ v hornerMultiplyAndAdd:10] |
19032 | 961 |
] |
962 |
||
963 |
|v| |
|
964 |
v := DoubleArray new:100 withAll:2. |
|
965 |
Time millisecondsToRun:[ |
|
20313 | 966 |
10000 timesRepeat:[ v hornerMultiplyAndAdd:10] |
19032 | 967 |
] |
968 |
" |
|
5737 | 969 |
! ! |
970 |
||
20313 | 971 |
|
628 | 972 |
!FloatArray class methodsFor:'documentation'! |
603 | 973 |
|
974 |
version |
|
18577 | 975 |
^ '$Header$' |
12577 | 976 |
! |
977 |
||
978 |
version_CVS |
|
18577 | 979 |
^ '$Header$' |
88 | 980 |
! ! |
19032 | 981 |