author | Stefan Vogel <sv@exept.de> |
Tue, 28 Apr 2020 14:41:21 +0200 | |
changeset 25372 | 389daab3ee10 |
parent 24436 | e4f6ee192c0c |
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 |
||
23806 | 18 |
UnboxedFloatArray 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 |
||
24436 | 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" |
24436 | 70 |
! |
71 |
||
72 |
epsilon |
|
73 |
"return the maximum relative spacing of my elements |
|
74 |
(i.e. the value-delta of the least significant bit)" |
|
75 |
||
76 |
^ ShortFloat epsilon |
|
77 |
||
78 |
"Created: / 19-07-2019 / 17:16:08 / Claus Gittinger" |
|
79 |
! |
|
80 |
||
81 |
precision |
|
82 |
"answer the precision (the number of bits in the mantissa) of my elements (in bits) |
|
83 |
This is an IEEE float, where only the fraction from the normalized mantissa is stored |
|
84 |
and so there is a hidden bit and the mantissa is actually represented by 24 binary digits |
|
85 |
(although only 23 are needed in the binary representation)" |
|
86 |
||
87 |
^ ShortFloat precision |
|
88 |
||
89 |
"Created: / 19-07-2019 / 17:24:50 / Claus Gittinger" |
|
13707
a8f395adfac2
faster elementByteSize query
Claus Gittinger <cg@exept.de>
parents:
12577
diff
changeset
|
90 |
! ! |
a8f395adfac2
faster elementByteSize query
Claus Gittinger <cg@exept.de>
parents:
12577
diff
changeset
|
91 |
|
24436 | 92 |
|
5737 | 93 |
!FloatArray methodsFor:'copying'! |
94 |
||
95 |
clone |
|
96 |
"return a copy of the receiver" |
|
97 |
||
98 |
|newArr| |
|
99 |
||
100 |
newArr := self class new:(self size). |
|
101 |
%{ |
|
8913 | 102 |
if (__ClassInstPtr(__qClass(self))->c_ninstvars == __mkSmallInteger(0)) { |
20313 | 103 |
INT _sz = __floatArraySize(self); |
5737 | 104 |
|
20313 | 105 |
bcopy(__FloatArrayInstPtr(self)->f_element, |
106 |
__FloatArrayInstPtr(newArr)->f_element, |
|
107 |
sizeof(float) * _sz); |
|
5737 | 108 |
|
20313 | 109 |
RETURN (newArr); |
5737 | 110 |
} |
111 |
%}. |
|
112 |
newArr replaceFrom:1 to:self size with:self startingAt:1. |
|
113 |
^ newArr |
|
114 |
||
115 |
" |
|
116 |
|f1 f2| |
|
117 |
||
118 |
f1 := FloatArray withAll:#(1 2 3 4 5). |
|
14692 | 119 |
f1 clone |
5737 | 120 |
" |
121 |
! |
|
122 |
||
123 |
copyFrom:start to:stop |
|
124 |
"return a partial copy of the receiver" |
|
125 |
||
126 |
%{ |
|
8913 | 127 |
if (__ClassInstPtr(__qClass(self))->c_ninstvars == __mkSmallInteger(0)) { |
20313 | 128 |
if (__bothSmallInteger(start, stop)) { |
129 |
INT __start = __intVal(start) - 1; |
|
130 |
INT __stop = __intVal(stop) - 1 ; |
|
5737 | 131 |
|
20313 | 132 |
if (__stop >= __start) { |
133 |
INT __sz = __floatArraySize(self); |
|
134 |
// printf("o_size=%d floatArraySize=%d\n", __qSize(self), __sz); |
|
5737 | 135 |
|
20313 | 136 |
if (((unsigned INT)__start < __sz) |
137 |
&& ((unsigned INT)__stop < __sz)) { |
|
138 |
INT __n = __stop - __start + 1; |
|
139 |
OBJ __nObj; |
|
140 |
int nBytes; |
|
5737 | 141 |
|
22263 | 142 |
// here we need to differ 32bit and 64bit |
143 |
// because on 32bit machines we need to take care about the alignment |
|
144 |
// but sadly, the same code is wrong for 64bit machines |
|
145 |
if (__POINTER_SIZE__ == 8) { |
|
146 |
nBytes = OHDR_SIZE + (__n * sizeof(float)); |
|
147 |
} else { |
|
148 |
nBytes = sizeof(struct __FloatArray) + (__n - 1) * sizeof(float); |
|
149 |
} |
|
20303
060fea1cfebe
fixes for float/doubleArray alignemnt
Claus Gittinger <cg@exept.de>
parents:
20300
diff
changeset
|
150 |
|
20313 | 151 |
// printf("__n=%d nBytes=%d\n", __n, nBytes); |
152 |
__nObj = __STX___new(nBytes); |
|
22257 | 153 |
|
20313 | 154 |
if (__nObj != nil) { |
155 |
__objPtr(__nObj)->o_class = __qClass(self); |
|
156 |
__STORE(__nObj, __qClass(self)); |
|
22257 | 157 |
// bcopy is marked as deprecated |
158 |
// bcopy( |
|
159 |
// __FloatArrayInstPtr(self)->f_element, |
|
160 |
// __FloatArrayInstPtr(__nObj)->f_element + __start, |
|
161 |
// __n * sizeof(float)); |
|
162 |
||
163 |
memcpy( |
|
164 |
__FloatArrayInstPtr(__nObj)->f_element + __start, |
|
165 |
__FloatArrayInstPtr(self)->f_element, |
|
166 |
__n * sizeof(float)); |
|
167 |
||
20313 | 168 |
RETURN(__nObj); |
169 |
} |
|
170 |
} |
|
171 |
} |
|
172 |
} |
|
5737 | 173 |
} |
14692 | 174 |
%}. |
5737 | 175 |
^ super copyFrom:start to:stop |
176 |
||
177 |
" |
|
178 |
|f1 f2| |
|
179 |
||
180 |
f1 := FloatArray withAll:#(1 2 3 4 5 6). |
|
22257 | 181 |
f2 := f1 copyFrom:1 to:3. |
182 |
f2 |
|
5737 | 183 |
" |
184 |
! |
|
185 |
||
186 |
replaceFrom:start to:stop with:aCollection startingAt:replStart |
|
187 |
%{ |
|
8913 | 188 |
if ((__ClassInstPtr(__qClass(self))->c_ninstvars == __mkSmallInteger(0)) |
20313 | 189 |
&& __isFloats(aCollection) |
190 |
&& (__ClassInstPtr(__qClass(aCollection))->c_ninstvars == __mkSmallInteger(0)) |
|
191 |
&& __bothSmallInteger(start, stop) |
|
192 |
&& __isSmallInteger(replStart) |
|
24318 | 193 |
&& !__isImmutable(self) |
18577 | 194 |
) { |
20313 | 195 |
INT __start = __intVal(start) - 1; |
196 |
INT __stop = __intVal(stop) - 1 ; |
|
197 |
INT __replStart = __intVal(replStart) - 1 ; |
|
5737 | 198 |
|
20313 | 199 |
if (__stop >= __start) { |
200 |
INT __sz = __floatArraySize(self); |
|
201 |
INT __otherSz = __floatArraySize(aCollection); |
|
202 |
INT __replStop = __replStart + (__stop-__start); |
|
5737 | 203 |
|
20313 | 204 |
if (((unsigned INT)__start < __sz) |
205 |
&& ((unsigned INT)__stop < __sz) |
|
206 |
&& ((unsigned INT)__replStart < __otherSz) |
|
207 |
&& ((unsigned INT)__replStop < __otherSz)) { |
|
208 |
INT __n = __stop - __start + 1; |
|
5737 | 209 |
|
20313 | 210 |
if (aCollection == self) { |
211 |
memmove(&(__FloatArrayInstPtr(self)->f_element[__start]), |
|
212 |
&(__FloatArrayInstPtr(aCollection)->f_element[__replStart]), |
|
213 |
sizeof(float) * __n); |
|
214 |
} else { |
|
215 |
memcpy(&(__FloatArrayInstPtr(self)->f_element[__start]), |
|
216 |
&(__FloatArrayInstPtr(aCollection)->f_element[__replStart]), |
|
217 |
sizeof(float) * __n); |
|
218 |
} |
|
219 |
RETURN(self); |
|
220 |
} |
|
221 |
} |
|
5737 | 222 |
} |
223 |
%}. |
|
24318 | 224 |
"/ arrive here only in case of an error |
225 |
self isImmutable ifTrue:[ |
|
226 |
self noModificationError |
|
227 |
]. |
|
5737 | 228 |
^ super replaceFrom:start to:stop with:aCollection startingAt:replStart |
229 |
||
230 |
" |
|
231 |
|f1 f2| |
|
232 |
||
233 |
f1 := (1 to:5) asFloatArray. |
|
234 |
f2 := #(10 9 8 7 6) asFloatArray. |
|
14692 | 235 |
f1 replaceFrom:1 to:3 with:f2 startingAt:3 |
5737 | 236 |
" |
24318 | 237 |
|
238 |
"Modified: / 09-06-2019 / 15:33:14 / Claus Gittinger" |
|
5737 | 239 |
! ! |
240 |
||
17198 | 241 |
!FloatArray methodsFor:'destructive arithmetic support'! |
242 |
||
243 |
primAbs |
|
24318 | 244 |
"destructive absolute value of each element; |
245 |
does not check for immutability: only use internally after cloning" |
|
17198 | 246 |
|
247 |
%{ |
|
248 |
if (__ClassInstPtr(__qClass(self))->c_ninstvars == __mkSmallInteger(0)) { |
|
20313 | 249 |
INT _sz = __floatArraySize(self); |
250 |
INT i; |
|
251 |
float *_p = __FloatArrayInstPtr(self)->f_element; |
|
252 |
float prev_p; |
|
17198 | 253 |
|
20313 | 254 |
if (_sz > 0) { |
255 |
/* how about inline-mmx-asm for this ... */ |
|
256 |
prev_p = _p[0]; |
|
257 |
for (i=1; i<_sz; i++) { |
|
258 |
float next_p = _p[i]; |
|
17198 | 259 |
|
20313 | 260 |
if (prev_p < 0) { |
261 |
_p[i-1] = -prev_p; |
|
262 |
} |
|
263 |
prev_p = next_p; |
|
264 |
} |
|
265 |
if (prev_p < 0) { |
|
266 |
_p[i-1] = -prev_p; |
|
267 |
} |
|
268 |
} |
|
269 |
RETURN (self); |
|
17198 | 270 |
} |
271 |
%}. |
|
272 |
super primAbs |
|
273 |
||
274 |
" |
|
275 |
|f| |
|
276 |
||
277 |
f := FloatArray withAll:#(-1 2 -3 4 -5). |
|
278 |
f abs. |
|
279 |
f |
|
280 |
" |
|
281 |
||
282 |
"Created: / 30-05-2007 / 17:50:17 / cg" |
|
24318 | 283 |
"Modified (comment): / 09-06-2019 / 15:34:21 / Claus Gittinger" |
17198 | 284 |
! |
285 |
||
286 |
primAddArray: floatArray |
|
287 |
"add the vector argument into the receiver (destructive). |
|
24318 | 288 |
The argument must be another vector. |
289 |
does not check for immutability: only use internally after cloning" |
|
17198 | 290 |
|
291 |
%{ |
|
292 |
if ((__ClassInstPtr(__qClass(self))->c_ninstvars == __mkSmallInteger(0)) |
|
293 |
&& __isFloats(floatArray) |
|
294 |
&& (__ClassInstPtr(__qClass(floatArray))->c_ninstvars == __mkSmallInteger(0))) { |
|
20313 | 295 |
INT _sz1 = __floatArraySize(self); |
296 |
INT _sz2 = __floatArraySize(floatArray); |
|
297 |
INT i; |
|
298 |
float *_p1 = __FloatArrayInstPtr(self)->f_element; |
|
299 |
float *_p2 = __FloatArrayInstPtr(floatArray)->f_element; |
|
17198 | 300 |
|
20313 | 301 |
if (_sz1 > 0) { |
302 |
if (_sz2 >= _sz1) { |
|
303 |
/* how about inline-mmx-asm for this ... */ |
|
304 |
float prev_p1 = _p1[0]; |
|
305 |
float prev_p2 = _p2[0]; |
|
17198 | 306 |
|
20313 | 307 |
for (i=1; i<_sz1; i++) { |
308 |
float next_p1 = _p1[i]; |
|
309 |
float next_p2 = _p2[i]; |
|
310 |
_p1[i-1] = prev_p1 + prev_p2; |
|
311 |
prev_p1 = next_p1; |
|
312 |
prev_p2 = next_p2; |
|
313 |
} |
|
314 |
_p1[i-1] = prev_p1 + prev_p2; |
|
315 |
} |
|
316 |
} |
|
317 |
RETURN (self); |
|
17198 | 318 |
} |
319 |
%}. |
|
320 |
super primAddArray:floatArray |
|
321 |
||
322 |
" |
|
323 |
|f1 f2| |
|
324 |
||
325 |
f1 := FloatArray withAll:#(1 2 3 4 5). |
|
326 |
f2 := FloatArray withAll:#(2 2 2 3 3). |
|
327 |
f1 += f2. |
|
328 |
f1 |
|
329 |
" |
|
24318 | 330 |
|
331 |
"Modified (comment): / 09-06-2019 / 15:34:31 / Claus Gittinger" |
|
17198 | 332 |
! |
333 |
||
334 |
primAddScalar: aScalar |
|
24318 | 335 |
"add the scalar argument into the receiver (destructive). |
336 |
does not check for immutability: only use internally after cloning" |
|
17198 | 337 |
|
338 |
%{ |
|
339 |
if (__ClassInstPtr(__qClass(self))->c_ninstvars == __mkSmallInteger(0)) { |
|
20313 | 340 |
INT _sz1 = __floatArraySize(self); |
341 |
INT i; |
|
342 |
float *_p1 = __FloatArrayInstPtr(self)->f_element; |
|
343 |
float v; |
|
344 |
float prev_p1; |
|
17198 | 345 |
|
20313 | 346 |
if (_sz1 > 0) { |
347 |
if (__isFloat(aScalar)) { |
|
348 |
v = (float)(__floatVal(aScalar)); |
|
349 |
} else if (__isShortFloat(aScalar)) { |
|
350 |
v = __shortFloatVal(aScalar); |
|
351 |
} else if (__isSmallInteger(aScalar)) { |
|
352 |
v = (float)(__intVal(aScalar)); |
|
353 |
} else |
|
354 |
goto badArg; |
|
17198 | 355 |
|
20313 | 356 |
/* how about inline-mmx-asm for this ... */ |
357 |
prev_p1 = _p1[0]; |
|
358 |
for (i=1; i<_sz1; i++) { |
|
359 |
float next_p1 = _p1[i]; |
|
360 |
_p1[i-1] = prev_p1 + v; |
|
361 |
prev_p1 = next_p1; |
|
362 |
} |
|
363 |
_p1[i-1] = prev_p1 + v; |
|
364 |
} |
|
365 |
RETURN (self); |
|
17198 | 366 |
} |
367 |
badArg: ; |
|
368 |
%}. |
|
369 |
super primAddScalar:aScalar |
|
370 |
||
371 |
" |
|
372 |
|f1 f2| |
|
373 |
||
374 |
f1 := FloatArray withAll:#(1 2 3 4 5). |
|
375 |
f1 += 2.0. |
|
376 |
Transcript showCR:f1. |
|
377 |
f1 += 2.0 asShortFloat. |
|
378 |
Transcript showCR:f1. |
|
379 |
f1 += 2. |
|
380 |
Transcript showCR:f1. |
|
381 |
" |
|
24318 | 382 |
|
383 |
"Modified (comment): / 09-06-2019 / 15:34:37 / Claus Gittinger" |
|
17198 | 384 |
! |
385 |
||
386 |
primDivArray: floatArray |
|
387 |
"divide the vector argument into the receiver (destructive). |
|
24318 | 388 |
The argument must be another vector. |
389 |
does not check for immutability: only use internally after cloning" |
|
17198 | 390 |
|
391 |
%{ |
|
392 |
if ((__ClassInstPtr(__qClass(self))->c_ninstvars == __mkSmallInteger(0)) |
|
393 |
&& __isFloats(floatArray) |
|
394 |
&& (__ClassInstPtr(__qClass(floatArray))->c_ninstvars == __mkSmallInteger(0))) { |
|
20313 | 395 |
INT _sz1 = __floatArraySize(self); |
396 |
INT _sz2 = __floatArraySize(floatArray); |
|
397 |
INT i; |
|
398 |
float *_p1 = __FloatArrayInstPtr(self)->f_element; |
|
399 |
float *_p2 = __FloatArrayInstPtr(floatArray)->f_element; |
|
17198 | 400 |
|
20313 | 401 |
if (_sz1 > 0) { |
402 |
if (_sz2 >= _sz1) { |
|
403 |
/* how about inline-mmx-asm for this ... */ |
|
404 |
float prev_p1 = _p1[0]; |
|
405 |
float prev_p2 = _p2[0]; |
|
17198 | 406 |
|
20313 | 407 |
for (i=1; i<_sz1; i++) { |
408 |
float next_p1 = _p1[i]; |
|
409 |
float next_p2 = _p2[i]; |
|
410 |
_p1[i-1] = prev_p1 / prev_p2; |
|
411 |
prev_p1 = next_p1; |
|
412 |
prev_p2 = next_p2; |
|
413 |
} |
|
414 |
_p1[i-1] = prev_p1 / prev_p2; |
|
415 |
} |
|
416 |
} |
|
417 |
RETURN (self); |
|
17198 | 418 |
} |
419 |
%}. |
|
420 |
super primDivArray: floatArray |
|
421 |
||
422 |
" |
|
423 |
|f1 f2| |
|
424 |
||
425 |
f1 := FloatArray withAll:#(1 2 3 4 5). |
|
426 |
f2 := FloatArray withAll:#(2 2 2 3 3). |
|
427 |
f1 /= f2. |
|
428 |
f1 |
|
429 |
" |
|
430 |
||
431 |
"Modified: / 29-05-2007 / 16:01:34 / cg" |
|
24318 | 432 |
"Modified (comment): / 09-06-2019 / 15:34:43 / Claus Gittinger" |
17198 | 433 |
! |
434 |
||
435 |
primDivScalar: aScalar |
|
24318 | 436 |
"divide the scalar argument into the receiver (destructive). |
437 |
does not check for immutability: only use internally after cloning" |
|
17198 | 438 |
|
439 |
%{ |
|
440 |
if (__ClassInstPtr(__qClass(self))->c_ninstvars == __mkSmallInteger(0)) { |
|
20313 | 441 |
INT _sz1 = __floatArraySize(self); |
442 |
INT i; |
|
443 |
float *_p1 = __FloatArrayInstPtr(self)->f_element; |
|
444 |
float v; |
|
445 |
float prev_p1; |
|
17198 | 446 |
|
20313 | 447 |
if (_sz1 > 0) { |
448 |
if (__isFloat(aScalar)) { |
|
449 |
v = (float)(__floatVal(aScalar)); |
|
450 |
} else if (__isShortFloat(aScalar)) { |
|
451 |
v = __shortFloatVal(aScalar); |
|
452 |
} else if (__isSmallInteger(aScalar)) { |
|
453 |
v = (float)(__intVal(aScalar)); |
|
454 |
} else |
|
455 |
goto badArg; |
|
17198 | 456 |
|
20313 | 457 |
/* how about inline-mmx-asm for this ... */ |
458 |
prev_p1 = _p1[0]; |
|
459 |
for (i=1; i<_sz1; i++) { |
|
460 |
float next_p1 = _p1[i]; |
|
461 |
_p1[i-1] = prev_p1 / v; |
|
462 |
prev_p1 = next_p1; |
|
463 |
} |
|
464 |
_p1[i-1] = prev_p1 / v; |
|
465 |
} |
|
466 |
RETURN (self); |
|
17198 | 467 |
} |
468 |
badArg: ; |
|
469 |
%}. |
|
470 |
super primDivScalar:aScalar |
|
471 |
||
472 |
" |
|
473 |
|f1 f2| |
|
474 |
||
475 |
f1 := FloatArray withAll:#(1 2 3 4 5). |
|
476 |
f1 /= 2.0. |
|
477 |
Transcript showCR:f1. |
|
478 |
f1 /= 2.0 asShortFloat. |
|
479 |
Transcript showCR:f1. |
|
480 |
f1 /= 2. |
|
481 |
Transcript showCR:f1. |
|
482 |
" |
|
483 |
||
484 |
"Modified: / 29-05-2007 / 16:01:39 / cg" |
|
24318 | 485 |
"Modified (comment): / 09-06-2019 / 15:34:48 / Claus Gittinger" |
17198 | 486 |
! |
487 |
||
488 |
primMulArray: floatArray |
|
489 |
"multiply the vector argument into the receiver (destructive). |
|
24318 | 490 |
The argument must be another vector. |
491 |
does not check for immutability: only use internally after cloning" |
|
17198 | 492 |
|
493 |
%{ |
|
494 |
if ((__ClassInstPtr(__qClass(self))->c_ninstvars == __mkSmallInteger(0)) |
|
495 |
&& __isFloats(floatArray) |
|
496 |
&& (__ClassInstPtr(__qClass(floatArray))->c_ninstvars == __mkSmallInteger(0))) { |
|
20313 | 497 |
INT _sz1 = __floatArraySize(self); |
498 |
INT _sz2 = __floatArraySize(floatArray); |
|
499 |
INT i; |
|
500 |
float *_p1 = __FloatArrayInstPtr(self)->f_element; |
|
501 |
float *_p2 = __FloatArrayInstPtr(floatArray)->f_element; |
|
17198 | 502 |
|
20313 | 503 |
if (_sz1 > 0) { |
504 |
if (_sz2 >= _sz1) { |
|
505 |
float prev_p1 = _p1[0]; |
|
506 |
float prev_p2 = _p2[0]; |
|
17198 | 507 |
|
20313 | 508 |
for (i=1; i<_sz1; i++) { |
509 |
float next_p1 = _p1[i]; |
|
510 |
float next_p2 = _p2[i]; |
|
511 |
_p1[i-1] = prev_p1 * prev_p2; |
|
512 |
prev_p1 = next_p1; |
|
513 |
prev_p2 = next_p2; |
|
514 |
} |
|
515 |
_p1[i-1] = prev_p1 * prev_p2; |
|
516 |
} |
|
517 |
} |
|
518 |
RETURN (self); |
|
17198 | 519 |
} |
520 |
%}. |
|
521 |
super primMulArray: floatArray |
|
522 |
||
523 |
" |
|
524 |
|f1 f2| |
|
525 |
||
526 |
f1 := FloatArray withAll:#(1 2 3 4 5). |
|
527 |
f2 := FloatArray withAll:#(2 2 2 3 3). |
|
528 |
f1 *= f2. |
|
529 |
f1 |
|
530 |
" |
|
24318 | 531 |
|
532 |
"Modified (comment): / 09-06-2019 / 15:34:53 / Claus Gittinger" |
|
17198 | 533 |
! |
534 |
||
535 |
primMulScalar: aScalar |
|
24318 | 536 |
"multiply the scalar argument into the receiver (destructive). |
537 |
does not check for immutability: only use internally after cloning" |
|
17198 | 538 |
|
539 |
%{ |
|
540 |
if (__ClassInstPtr(__qClass(self))->c_ninstvars == __mkSmallInteger(0)) { |
|
20313 | 541 |
INT _sz1 = __floatArraySize(self); |
542 |
INT i; |
|
543 |
float *_p1 = __FloatArrayInstPtr(self)->f_element; |
|
544 |
float v; |
|
545 |
float prev_p1; |
|
17198 | 546 |
|
20313 | 547 |
if (_sz1 > 0) { |
548 |
if (__isFloat(aScalar)) { |
|
549 |
v = (float)(__floatVal(aScalar)); |
|
550 |
} else if (__isShortFloat(aScalar)) { |
|
551 |
v = __shortFloatVal(aScalar); |
|
552 |
} else if (__isSmallInteger(aScalar)) { |
|
553 |
v = (float)(__intVal(aScalar)); |
|
554 |
} else |
|
555 |
goto badArg; |
|
17198 | 556 |
|
20313 | 557 |
/* how about inline-mmx-asm for this ... */ |
558 |
prev_p1 = _p1[0]; |
|
559 |
for (i=1; i<_sz1; i++) { |
|
560 |
float next_p1 = _p1[i]; |
|
561 |
_p1[i-1] = prev_p1 * v; |
|
562 |
prev_p1 = next_p1; |
|
563 |
} |
|
564 |
_p1[i-1] = prev_p1 * v; |
|
565 |
} |
|
566 |
RETURN (self); |
|
17198 | 567 |
} |
568 |
badArg: ; |
|
569 |
%}. |
|
570 |
super primMulScalar:aScalar |
|
571 |
||
572 |
" |
|
573 |
|f1 f2| |
|
574 |
||
575 |
f1 := FloatArray withAll:#(1 2 3 4 5). |
|
576 |
f1 *= 2.0. |
|
577 |
Transcript showCR:f1. |
|
578 |
f1 *= 2.0 asShortFloat. |
|
579 |
Transcript showCR:f1. |
|
580 |
f1 *= 2. |
|
581 |
Transcript showCR:f1. |
|
582 |
" |
|
24318 | 583 |
|
584 |
"Modified (comment): / 09-06-2019 / 15:35:00 / Claus Gittinger" |
|
17198 | 585 |
! |
586 |
||
587 |
primNegated |
|
24318 | 588 |
"destructive negative value of each element. |
589 |
does not check for immutability: only use internally after cloning" |
|
17198 | 590 |
|
591 |
%{ |
|
592 |
if (__ClassInstPtr(__qClass(self))->c_ninstvars == __mkSmallInteger(0)) { |
|
20313 | 593 |
INT _sz = __floatArraySize(self); |
594 |
INT i; |
|
595 |
float *_p = __FloatArrayInstPtr(self)->f_element; |
|
596 |
float prev_p; |
|
17198 | 597 |
|
20313 | 598 |
if (_sz > 0) { |
599 |
/* how about inline-mmx-asm for this ... */ |
|
600 |
prev_p = _p[0]; |
|
601 |
for (i=1; i<_sz; i++) { |
|
602 |
float next_p = _p[i]; |
|
17198 | 603 |
|
20313 | 604 |
_p[i-1] = -prev_p; |
605 |
prev_p = next_p; |
|
606 |
} |
|
607 |
_p[i-1] = -prev_p; |
|
608 |
} |
|
609 |
RETURN (self); |
|
17198 | 610 |
} |
611 |
%}. |
|
612 |
super primNegated |
|
613 |
||
614 |
" |
|
615 |
|f| |
|
616 |
||
617 |
f := FloatArray withAll:#(-1 2 -3 4 -5). |
|
618 |
f negated. |
|
619 |
f |
|
620 |
" |
|
621 |
||
622 |
"Created: / 30-05-2007 / 17:51:29 / cg" |
|
24318 | 623 |
"Modified (comment): / 09-06-2019 / 15:35:04 / Claus Gittinger" |
17198 | 624 |
! |
625 |
||
626 |
primSubtractArray: floatArray |
|
627 |
"subtract the vector argument from the receiver (destructive). |
|
24318 | 628 |
The argument must be another vector. |
629 |
does not check for immutability: only use internally after cloning" |
|
17198 | 630 |
|
631 |
%{ |
|
632 |
if ((__ClassInstPtr(__qClass(self))->c_ninstvars == __mkSmallInteger(0)) |
|
633 |
&& __isFloats(floatArray) |
|
634 |
&& (__ClassInstPtr(__qClass(floatArray))->c_ninstvars == __mkSmallInteger(0))) { |
|
20313 | 635 |
INT _sz1 = __floatArraySize(self); |
636 |
INT _sz2 = __floatArraySize(floatArray); |
|
637 |
INT i; |
|
638 |
float *_p1 = __FloatArrayInstPtr(self)->f_element; |
|
639 |
float *_p2 = __FloatArrayInstPtr(floatArray)->f_element; |
|
17198 | 640 |
|
20313 | 641 |
if (_sz1 > 0) { |
642 |
if (_sz2 >= _sz1) { |
|
643 |
float prev_p1 = _p1[0]; |
|
644 |
float prev_p2 = _p2[0]; |
|
17198 | 645 |
|
20313 | 646 |
for (i=1; i<_sz1; i++) { |
647 |
float next_p1 = _p1[i]; |
|
648 |
float next_p2 = _p2[i]; |
|
649 |
_p1[i-1] = prev_p1 - prev_p2; |
|
650 |
prev_p1 = next_p1; |
|
651 |
prev_p2 = next_p2; |
|
652 |
} |
|
653 |
_p1[i-1] = prev_p1 - prev_p2; |
|
654 |
} |
|
655 |
} |
|
656 |
RETURN (self); |
|
17198 | 657 |
} |
658 |
%}. |
|
659 |
super primSubtractArray: floatArray |
|
660 |
||
661 |
" |
|
662 |
|f1 f2| |
|
663 |
||
664 |
f1 := FloatArray withAll:#(1 2 3 4 5). |
|
665 |
f2 := FloatArray withAll:#(2 2 2 3 3). |
|
666 |
f1 -= f2. |
|
667 |
f1 |
|
668 |
" |
|
669 |
||
670 |
"Created: / 30-05-2007 / 17:42:41 / cg" |
|
24318 | 671 |
"Modified (comment): / 09-06-2019 / 15:35:08 / Claus Gittinger" |
17198 | 672 |
! |
673 |
||
674 |
primSubtractScalar: aScalar |
|
24318 | 675 |
"subtract the scalar argument from the receiver (destructive). |
676 |
does not check for immutability: only use internally after cloning" |
|
17198 | 677 |
|
678 |
%{ |
|
679 |
if (__ClassInstPtr(__qClass(self))->c_ninstvars == __mkSmallInteger(0)) { |
|
20313 | 680 |
INT _sz1 = __floatArraySize(self); |
681 |
INT i; |
|
682 |
float *_p1 = __FloatArrayInstPtr(self)->f_element; |
|
683 |
float v; |
|
684 |
float prev_p1; |
|
17198 | 685 |
|
20313 | 686 |
if (_sz1 > 0) { |
687 |
if (__isFloat(aScalar)) { |
|
688 |
v = (float)(__floatVal(aScalar)); |
|
689 |
} else if (__isShortFloat(aScalar)) { |
|
690 |
v = __shortFloatVal(aScalar); |
|
691 |
} else if (__isSmallInteger(aScalar)) { |
|
692 |
v = (float)(__intVal(aScalar)); |
|
693 |
} else |
|
694 |
goto badArg; |
|
17198 | 695 |
|
20313 | 696 |
/* how about inline-mmx-asm for this... ? */ |
697 |
prev_p1 = _p1[0]; |
|
698 |
for (i=1; i<_sz1; i++) { |
|
699 |
float next_p1 = _p1[i]; |
|
700 |
_p1[i-1] = prev_p1 - v; |
|
701 |
prev_p1 = next_p1; |
|
702 |
} |
|
703 |
_p1[i-1] = prev_p1 - v; |
|
704 |
} |
|
705 |
RETURN (self); |
|
17198 | 706 |
} |
707 |
badArg: ; |
|
708 |
%}. |
|
709 |
super primSubtractScalar:aScalar |
|
710 |
||
711 |
" |
|
712 |
|f1 f2| |
|
713 |
||
714 |
f1 := FloatArray withAll:#(1 2 3 4 5). |
|
715 |
f1 -= 2.0. |
|
716 |
Transcript showCR:f1. |
|
717 |
f1 -= 2.0 asShortFloat. |
|
718 |
Transcript showCR:f1. |
|
719 |
f1 -= 2. |
|
720 |
Transcript showCR:f1. |
|
721 |
" |
|
722 |
||
723 |
"Created: / 30-05-2007 / 17:43:06 / cg" |
|
24318 | 724 |
"Modified (comment): / 09-06-2019 / 15:35:13 / Claus Gittinger" |
17198 | 725 |
! ! |
726 |
||
5737 | 727 |
!FloatArray methodsFor:'queries'! |
728 |
||
7220 | 729 |
defaultElement |
730 |
^ ShortFloat zero |
|
731 |
! |
|
732 |
||
20300 | 733 |
isValidElement:anObject |
734 |
"return true, if I can hold this kind of object" |
|
735 |
||
736 |
^ anObject isNumber |
|
737 |
! |
|
738 |
||
5737 | 739 |
minMax |
11212 | 740 |
"return a Tuple holding the smallest and largest element; |
5737 | 741 |
redefined for speed" |
742 |
||
20313 | 743 |
|min max empty| |
5737 | 744 |
|
745 |
%{ |
|
8913 | 746 |
if (__ClassInstPtr(__qClass(self))->c_ninstvars == __mkSmallInteger(0)) { |
20313 | 747 |
INT _sz = __floatArraySize(self); |
748 |
||
749 |
if (_sz > 0) { |
|
750 |
INT _i; |
|
751 |
float *_p = __FloatArrayInstPtr(self)->f_element; |
|
752 |
float _min, _max; |
|
753 |
OBJ retVal; |
|
754 |
||
755 |
_min = _max = _p[0]; |
|
756 |
for (_i=_sz-1; _i>0; _i-=2) { |
|
757 |
float _v1 = _p[_i]; |
|
758 |
float _v2 = _p[_i-1]; |
|
759 |
if (_v1 < _v2) { |
|
760 |
if (_v1 < _min) _min = _v1; |
|
761 |
if (_v2 > _max) _max = _v2; |
|
762 |
} else { |
|
763 |
if (_v2 < _min) _min = _v2; |
|
764 |
if (_v1 > _max) _max = _v1; |
|
765 |
} |
|
766 |
} |
|
21081 | 767 |
|
20313 | 768 |
min = __MKFLOAT(_min); |
769 |
__PROTECT__(min); |
|
770 |
max = __MKFLOAT(_max); |
|
771 |
__UNPROTECT__(min); |
|
772 |
RETURN (__ARRAY_WITH2(min, max)); |
|
773 |
} |
|
774 |
empty = true; |
|
7220 | 775 |
} |
5737 | 776 |
%}. |
20313 | 777 |
empty == true ifTrue:[ |
778 |
^ self emptyCollectionError. |
|
779 |
]. |
|
21081 | 780 |
^ Array with:(self min) with:(self max) |
5737 | 781 |
|
7220 | 782 |
" |
783 |
|f1| |
|
784 |
||
13774 | 785 |
f1 := (1 to:10000) asFloatArray. |
14692 | 786 |
Time millisecondsToRun:[ 1000 timesRepeat:[ f1 minMax ] ] |
7220 | 787 |
" |
788 |
" |
|
789 |
|f1| |
|
5737 | 790 |
|
7220 | 791 |
f1 := FloatArray withAll:#(1 2 3 4 5). |
14692 | 792 |
f1 minMax |
7220 | 793 |
" |
794 |
" |
|
795 |
|f1| |
|
5737 | 796 |
|
20313 | 797 |
f1 := FloatArray withAll:#(1 2 3 4). |
798 |
f1 minMax |
|
799 |
" |
|
800 |
" |
|
801 |
|f1| |
|
802 |
||
7220 | 803 |
f1 := FloatArray withAll:#(5 4 3 2 1). |
14692 | 804 |
f1 minMax |
7220 | 805 |
" |
13774 | 806 |
|
807 |
"Modified (comment): / 07-10-2011 / 13:03:30 / cg" |
|
10561 | 808 |
! |
809 |
||
12577 | 810 |
numFloats |
811 |
^ self size |
|
10561 | 812 |
! ! |
813 |
||
23798 | 814 |
!FloatArray methodsFor:'testing'! |
815 |
||
816 |
isFloatArray |
|
817 |
"return true if the receiver has float elements. |
|
818 |
These are Float, Double- and HalfFloat arrays" |
|
819 |
||
820 |
^ true |
|
821 |
||
822 |
"Created: / 02-03-2019 / 23:13:51 / Claus Gittinger" |
|
823 |
! |
|
824 |
||
825 |
isUnboxedFloatArray |
|
826 |
"return true if the receiver has float elements. |
|
827 |
These are Float, Double- and HalfFloat arrays" |
|
828 |
||
829 |
^ true |
|
830 |
||
831 |
"Created: / 02-03-2019 / 23:13:26 / Claus Gittinger" |
|
832 |
! ! |
|
833 |
||
10561 | 834 |
!FloatArray methodsFor:'vector arithmetic'! |
835 |
||
836 |
dot: aFloatVector |
|
837 |
"Return the dot product of the receiver and the argument. |
|
19032 | 838 |
Raises an error, if the argument is not of the same size as the receiver." |
10561 | 839 |
|
840 |
| mySize result | |
|
841 |
||
842 |
"/ <primitive:'primitiveDotProduct' module: 'FloatArrayPlugin'> |
|
843 |
%{ |
|
19032 | 844 |
if ((__ClassInstPtr(__qClass(self))->c_ninstvars == __mkSmallInteger(0)) |
845 |
&& (__ClassInstPtr(__qClass(aFloatVector))->c_ninstvars == __mkSmallInteger(0))) { |
|
20313 | 846 |
INT __mySize = __floatArraySize(self); |
847 |
double __result = 0.0; |
|
848 |
float *__p1 = __FloatArrayInstPtr(self)->f_element; |
|
19032 | 849 |
|
20313 | 850 |
if (__mySize > 0) { |
851 |
if (__isFloats(aFloatVector)) { |
|
852 |
INT __otherSize = __floatArraySize(aFloatVector); |
|
17198 | 853 |
|
20313 | 854 |
if (__mySize == __otherSize) { |
855 |
float *__p2 = __FloatArrayInstPtr(aFloatVector)->f_element; |
|
856 |
INT __i; |
|
857 |
/* how about inline-mmx-asm for this ... */ |
|
858 |
for (__i=0; __i<__mySize; __i++) { |
|
859 |
__result += (__p1[__i] * __p2[__i]); |
|
860 |
} |
|
861 |
RETURN (__MKFLOAT(__result)); |
|
862 |
} |
|
863 |
} else if (__isDoubles(aFloatVector)) { |
|
864 |
INT __otherSize = __doubleArraySize(aFloatVector); |
|
19032 | 865 |
|
20313 | 866 |
if (__mySize == __otherSize) { |
867 |
double *__p2 = __DoubleArrayInstPtr(aFloatVector)->d_element; |
|
868 |
INT __i; |
|
869 |
/* how about inline-mmx-asm for this ... */ |
|
870 |
for (__i=0; __i<__mySize; __i++) { |
|
871 |
__result += (__p1[__i] * __p2[__i]); |
|
872 |
} |
|
873 |
RETURN (__MKFLOAT(__result)); |
|
874 |
} |
|
875 |
} |
|
876 |
} |
|
10561 | 877 |
} |
878 |
%}. |
|
15935 | 879 |
^ super dot:aFloatVector |
10561 | 880 |
|
881 |
" |
|
882 |
|v| |
|
11212 | 883 |
v := #(2.0 2.0 1.0) asFloatArray. |
19003 | 884 |
v dot:v. |
19032 | 885 |
|
886 |
|v| |
|
887 |
v := #(2.0 2.0 1.0) asDoubleArray. |
|
888 |
v dot:v. |
|
889 |
||
890 |
|v| |
|
891 |
v := #(2.0 2.0 1.0). |
|
892 |
v dot:v. |
|
893 |
||
894 |
|v1 v2| |
|
895 |
v1 := #(2.0 2.0 1.0) asFloatArray. |
|
896 |
v2 := #(2.0 2.0 1.0) asDoubleArray. |
|
897 |
v1 dot:v2. |
|
898 |
||
899 |
|v1 v2| |
|
900 |
v1 := #(2.0 2.0 1.0) asDoubleArray. |
|
901 |
v2 := #(2.0 2.0 1.0) asFloatArray. |
|
902 |
v1 dot:v2. |
|
903 |
||
904 |
|v1 v2| |
|
905 |
v1 := #(2.0 2.0 1.0). |
|
906 |
v2 := #(2.0 2.0 1.0) asFloatArray. |
|
907 |
v1 dot:v2. |
|
908 |
||
909 |
|v1 v2| |
|
910 |
v1 := #(2.0 2.0 1.0) asFloatArray. |
|
911 |
v2 := #(2.0 2.0 1.0). |
|
912 |
v1 dot:v2. |
|
913 |
||
914 |
|v1 v2| |
|
915 |
v1 := #(2.0 2.0 1.0). |
|
916 |
v2 := #(2.0 2.0 1.0) asDoubleArray. |
|
917 |
v1 dot:v2. |
|
918 |
||
919 |
|v1 v2| |
|
920 |
v1 := #(2.0 2.0 1.0) asDoubleArray. |
|
921 |
v2 := #(2.0 2.0 1.0). |
|
922 |
v1 dot:v2. |
|
923 |
" |
|
20303
060fea1cfebe
fixes for float/doubleArray alignemnt
Claus Gittinger <cg@exept.de>
parents:
20300
diff
changeset
|
924 |
|
19032 | 925 |
" |
926 |
|v1 v2| |
|
927 |
||
928 |
v1 := Array new:10000 withAll:2. |
|
929 |
v2 := Array new:10000 withAll:3. |
|
930 |
Time millisecondsToRun:[ |
|
20313 | 931 |
10000 timesRepeat:[ |
932 |
v1 dot:v2. |
|
933 |
] |
|
19032 | 934 |
] |
17198 | 935 |
" |
936 |
" |
|
937 |
|v1 v2| |
|
938 |
||
939 |
v1 := FloatArray new:10000 withAll:2. |
|
19003 | 940 |
v2 := FloatArray new:10000 withAll:3. |
17198 | 941 |
Time millisecondsToRun:[ |
20313 | 942 |
10000 timesRepeat:[ |
943 |
v1 dot:v2. |
|
944 |
] |
|
19003 | 945 |
] |
10561 | 946 |
" |
947 |
||
19032 | 948 |
" |
949 |
|v1 v2| |
|
950 |
v1 := #(2.0 2.0 1.0) asFloatArray. |
|
951 |
v2 := #(2.0 2.0 1.0 0.0) asDoubleArray. |
|
952 |
v1 dot:v2. |
|
953 |
" |
|
954 |
||
20303
060fea1cfebe
fixes for float/doubleArray alignemnt
Claus Gittinger <cg@exept.de>
parents:
20300
diff
changeset
|
955 |
|
10561 | 956 |
"Created: / 29-05-2007 / 13:13:39 / cg" |
19032 | 957 |
! |
958 |
||
24306 | 959 |
esum |
960 |
"Return the (double) sum of the elements. |
|
961 |
Reduces accumulated rounding errors. |
|
962 |
This is an experimental algorithm" |
|
963 |
||
964 |
%{ |
|
965 |
if (__ClassInstPtr(__qClass(self))->c_ninstvars == __mkSmallInteger(0)) { |
|
966 |
INT __mySize = __floatArraySize(self); |
|
967 |
double __sum = 0.0; |
|
968 |
float *__pf = __FloatArrayInstPtr(self)->f_element; |
|
969 |
#define NBITS_EXP 11 |
|
970 |
#define NIDX_EXP ((1<<NBITS_EXP)) |
|
971 |
#define MASK_EXP (NIDX_EXP-1) |
|
972 |
double sumsPerE[ NIDX_EXP]; |
|
973 |
int __i, __minIdx = MASK_EXP, __maxIdx = 0; |
|
974 |
||
975 |
memset(sumsPerE, 0, sizeof(double)*NIDX_EXP); |
|
976 |
||
977 |
for (__i=0; __i<__mySize; __i++) { |
|
978 |
union { |
|
979 |
double f; |
|
24333
e6d6a165972d
Replace uint32_t (not known in mingw)
Stefan Vogel <sv@exept.de>
parents:
24318
diff
changeset
|
980 |
unsigned int32 ui32[2]; |
24306 | 981 |
} u; |
982 |
int exp; |
|
983 |
||
984 |
u.f = (double)__pf[__i]; |
|
985 |
#ifdef __LSBFIRST__ |
|
986 |
exp = (u.ui32[1] >> (32-1-NBITS_EXP)) & MASK_EXP; |
|
987 |
#else |
|
988 |
exp = (u.ui32[0] >> ((32-1-NBITS_EXP)) & MASK_EXP; |
|
989 |
#endif |
|
990 |
// printf("f:%04x %04x %04x %04x\n", u.ui32[0], u.ui32[1], u.ui32[2], u.ui32[3]); |
|
991 |
// printf("e:%d %lf + %lf -> %lf\n", exp, sumsPerE[exp], u.f, (sumsPerE[exp]+u.f)); |
|
992 |
sumsPerE[exp] += u.f; |
|
993 |
__minIdx = exp < __minIdx ? exp : __minIdx; |
|
994 |
__maxIdx = exp > __maxIdx ? exp : __maxIdx; |
|
995 |
} |
|
996 |
||
997 |
// printf("low:%d high:%d\n", __minIdx, __maxIdx); |
|
998 |
for (__i=__maxIdx; __i>=__minIdx; __i--) { |
|
999 |
__sum += sumsPerE[__i]; |
|
1000 |
} |
|
1001 |
RETURN (__MKFLOAT(__sum)); |
|
1002 |
} |
|
1003 |
%}. |
|
1004 |
^ super sum |
|
1005 |
||
1006 |
"here, sum and esum both deliver the same result |
|
1007 |
||
1008 |
|v| |
|
1009 |
v := #(2.0 2.0 1.0 1.0) asFloatArray. |
|
1010 |
v sum. |
|
1011 |
v esum. |
|
1012 |
||
1013 |
|v| |
|
1014 |
v := #(1e38 1.0 -1e38 1.0) asFloatArray. |
|
1015 |
v sum. |
|
1016 |
v esum. |
|
1017 |
" |
|
1018 |
||
1019 |
"Created: / 09-06-2019 / 14:10:58 / Claus Gittinger" |
|
1020 |
! |
|
1021 |
||
19032 | 1022 |
hornerMultiplyAndAdd:x |
1023 |
"primitive support for horner's-method computation of polynomials. |
|
1024 |
The vector is interpreted as providing the factors for a polynomial, |
|
20313 | 1025 |
an*x^n + (an-1)*x^(n-1) + ... + a2(x) + a1 |
19032 | 1026 |
where the ai are the elements of the Array. |
1027 |
The highest rank factor is at the first position, the 0-rank constant at last. |
|
1028 |
This is inlined c-code, which may get compiled to fast machine code, |
|
1029 |
using multiply-and-add or vector instructions, if the CPU/Compiler support them." |
|
1030 |
||
1031 |
| mySize result | |
|
1032 |
||
1033 |
%{ |
|
1034 |
double __x; |
|
20303
060fea1cfebe
fixes for float/doubleArray alignemnt
Claus Gittinger <cg@exept.de>
parents:
20300
diff
changeset
|
1035 |
|
19032 | 1036 |
if (__isFloat(x)) { |
20313 | 1037 |
__x = __floatVal(x); |
19032 | 1038 |
} else if (__isShortFloat(x)) { |
20313 | 1039 |
__x = (double)__shortFloatVal(x); |
19032 | 1040 |
} else if (__isSmallInteger(x)) { |
20313 | 1041 |
__x = (double)(__intVal(x)); |
19032 | 1042 |
} else { |
20313 | 1043 |
goto getOutOfHere; |
19032 | 1044 |
} |
20303
060fea1cfebe
fixes for float/doubleArray alignemnt
Claus Gittinger <cg@exept.de>
parents:
20300
diff
changeset
|
1045 |
|
19032 | 1046 |
if (__ClassInstPtr(__qClass(self))->c_ninstvars == __mkSmallInteger(0)) { |
20313 | 1047 |
INT __mySize = __floatArraySize(self); |
1048 |
float *__elements = __FloatArrayInstPtr(self)->f_element; |
|
1049 |
double __result = __elements[0]; |
|
19032 | 1050 |
|
20313 | 1051 |
if (__mySize > 1) { |
1052 |
INT __i; |
|
1053 |
/* how about inline-mmx-asm for this ... */ |
|
1054 |
for (__i=1; __i<__mySize; __i++) { |
|
1055 |
__result = (__result * __x) + __elements[__i]; |
|
1056 |
} |
|
1057 |
} |
|
1058 |
RETURN (__MKFLOAT(__result)); |
|
19032 | 1059 |
} |
20303
060fea1cfebe
fixes for float/doubleArray alignemnt
Claus Gittinger <cg@exept.de>
parents:
20300
diff
changeset
|
1060 |
getOutOfHere: ; |
19032 | 1061 |
%}. |
1062 |
^ super hornerMultiplyAndAdd:x |
|
1063 |
||
1064 |
" |
|
1065 |
|v| |
|
1066 |
v := #(2.0 3.0 4.0) asFloatArray. |
|
1067 |
v hornerMultiplyAndAdd:10. |
|
1068 |
||
1069 |
|v| |
|
1070 |
v := Array new:100 withAll:2.0. |
|
1071 |
v hornerMultiplyAndAdd:10 |
|
1072 |
||
1073 |
|v| |
|
1074 |
v := FloatArray new:100 withAll:2.0. |
|
1075 |
v hornerMultiplyAndAdd:10 |
|
1076 |
||
1077 |
|v| |
|
1078 |
v := Array new:100 withAll:2.0. |
|
1079 |
Time millisecondsToRun:[ |
|
20313 | 1080 |
10000 timesRepeat:[ v hornerMultiplyAndAdd:10] |
19032 | 1081 |
] |
1082 |
||
1083 |
|v| |
|
1084 |
v := FloatArray new:100 withAll:2. |
|
1085 |
Time millisecondsToRun:[ |
|
20313 | 1086 |
10000 timesRepeat:[ v hornerMultiplyAndAdd:10] |
19032 | 1087 |
] |
1088 |
||
1089 |
|v| |
|
1090 |
v := DoubleArray new:100 withAll:2. |
|
1091 |
Time millisecondsToRun:[ |
|
20313 | 1092 |
10000 timesRepeat:[ v hornerMultiplyAndAdd:10] |
19032 | 1093 |
] |
1094 |
" |
|
24306 | 1095 |
! |
1096 |
||
1097 |
sum |
|
1098 |
"Return the (double) sum of the elements. |
|
1099 |
May suffer from accumulated rounding error" |
|
1100 |
||
1101 |
%{ |
|
1102 |
if (__ClassInstPtr(__qClass(self))->c_ninstvars == __mkSmallInteger(0)) { |
|
1103 |
INT __mySize = __floatArraySize(self); |
|
1104 |
double __sum = 0.0; |
|
1105 |
float *__pf = __FloatArrayInstPtr(self)->f_element; |
|
1106 |
int __i; |
|
1107 |
||
1108 |
/* how about inline-mmx-asm for this ... */ |
|
1109 |
for (__i=0; __i<__mySize; __i++) { |
|
1110 |
__sum += __pf[__i]; |
|
1111 |
} |
|
1112 |
RETURN (__MKFLOAT(__sum)); |
|
1113 |
} |
|
1114 |
%}. |
|
1115 |
^ super sum |
|
1116 |
||
1117 |
" |
|
1118 |
|v| |
|
1119 |
v := #(2.0 2.0 1.0) asFloatArray. |
|
1120 |
v sum. |
|
1121 |
||
1122 |
|v| |
|
1123 |
v := #(1e30 1 -1e30 1) asFloatArray. |
|
1124 |
v sum. |
|
24308 | 1125 |
v esum. |
24306 | 1126 |
" |
1127 |
||
1128 |
"Created: / 09-06-2019 / 10:41:12 / Claus Gittinger" |
|
24308 | 1129 |
"Modified (comment): / 09-06-2019 / 14:14:47 / Claus Gittinger" |
5737 | 1130 |
! ! |
1131 |
||
24436 | 1132 |
|
628 | 1133 |
!FloatArray class methodsFor:'documentation'! |
603 | 1134 |
|
1135 |
version |
|
18577 | 1136 |
^ '$Header$' |
12577 | 1137 |
! |
1138 |
||
1139 |
version_CVS |
|
18577 | 1140 |
^ '$Header$' |
88 | 1141 |
! ! |
19032 | 1142 |