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