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