DoubleArray.st
author Claus Gittinger <cg@exept.de>
Fri, 15 Jan 2016 13:17:45 +0100
changeset 19031 b8bbe1983d3a
parent 16853 e3e062d68503
child 19035 06aeb1259ba6
child 20301 af76baeacf78
permissions -rw-r--r--
#FEATURE class: DoubleArray added: #dot: #hornerMultiplyAndAdd: comments; dot: supports mixed float/double array operation
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
     1
"
a27a279701f8 Initial revision
claus
parents:
diff changeset
     2
 COPYRIGHT (c) 1993 by Claus Gittinger
a27a279701f8 Initial revision
claus
parents:
diff changeset
     3
	      All Rights Reserved
a27a279701f8 Initial revision
claus
parents:
diff changeset
     4
a27a279701f8 Initial revision
claus
parents:
diff changeset
     5
 This software is furnished under a license and may be used
a27a279701f8 Initial revision
claus
parents:
diff changeset
     6
 only in accordance with the terms of that license and with the
a27a279701f8 Initial revision
claus
parents:
diff changeset
     7
 inclusion of the above copyright notice.   This software may not
a27a279701f8 Initial revision
claus
parents:
diff changeset
     8
 be provided or otherwise made available to, or used by, any
a27a279701f8 Initial revision
claus
parents:
diff changeset
     9
 other person.  No title to or ownership of the software is
a27a279701f8 Initial revision
claus
parents:
diff changeset
    10
 hereby transferred.
a27a279701f8 Initial revision
claus
parents:
diff changeset
    11
"
7220
5225d13b2d57 inheritance & documentation
Claus Gittinger <cg@exept.de>
parents: 4062
diff changeset
    12
"{ Package: 'stx:libbasic' }"
5225d13b2d57 inheritance & documentation
Claus Gittinger <cg@exept.de>
parents: 4062
diff changeset
    13
19031
b8bbe1983d3a #FEATURE
Claus Gittinger <cg@exept.de>
parents: 16853
diff changeset
    14
"{ NameSpace: Smalltalk }"
b8bbe1983d3a #FEATURE
Claus Gittinger <cg@exept.de>
parents: 16853
diff changeset
    15
13825
a80eb51765c0 class definition
Claus Gittinger <cg@exept.de>
parents: 13708
diff changeset
    16
AbstractNumberVector variableDoubleSubclass:#DoubleArray
1263
92c1db6b0776 commentary
Claus Gittinger <cg@exept.de>
parents: 627
diff changeset
    17
	instanceVariableNames:''
92c1db6b0776 commentary
Claus Gittinger <cg@exept.de>
parents: 627
diff changeset
    18
	classVariableNames:''
92c1db6b0776 commentary
Claus Gittinger <cg@exept.de>
parents: 627
diff changeset
    19
	poolDictionaries:''
92c1db6b0776 commentary
Claus Gittinger <cg@exept.de>
parents: 627
diff changeset
    20
	category:'Collections-Arrayed'
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
    21
!
a27a279701f8 Initial revision
claus
parents:
diff changeset
    22
88
81dacba7a63a *** empty log message ***
claus
parents: 3
diff changeset
    23
!DoubleArray class methodsFor:'documentation'!
81dacba7a63a *** empty log message ***
claus
parents: 3
diff changeset
    24
81dacba7a63a *** empty log message ***
claus
parents: 3
diff changeset
    25
copyright
81dacba7a63a *** empty log message ***
claus
parents: 3
diff changeset
    26
"
81dacba7a63a *** empty log message ***
claus
parents: 3
diff changeset
    27
 COPYRIGHT (c) 1993 by Claus Gittinger
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
    28
	      All Rights Reserved
a27a279701f8 Initial revision
claus
parents:
diff changeset
    29
88
81dacba7a63a *** empty log message ***
claus
parents: 3
diff changeset
    30
 This software is furnished under a license and may be used
81dacba7a63a *** empty log message ***
claus
parents: 3
diff changeset
    31
 only in accordance with the terms of that license and with the
81dacba7a63a *** empty log message ***
claus
parents: 3
diff changeset
    32
 inclusion of the above copyright notice.   This software may not
81dacba7a63a *** empty log message ***
claus
parents: 3
diff changeset
    33
 be provided or otherwise made available to, or used by, any
81dacba7a63a *** empty log message ***
claus
parents: 3
diff changeset
    34
 other person.  No title to or ownership of the software is
81dacba7a63a *** empty log message ***
claus
parents: 3
diff changeset
    35
 hereby transferred.
81dacba7a63a *** empty log message ***
claus
parents: 3
diff changeset
    36
"
81dacba7a63a *** empty log message ***
claus
parents: 3
diff changeset
    37
!
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
    38
88
81dacba7a63a *** empty log message ***
claus
parents: 3
diff changeset
    39
documentation
81dacba7a63a *** empty log message ***
claus
parents: 3
diff changeset
    40
"
13825
a80eb51765c0 class definition
Claus Gittinger <cg@exept.de>
parents: 13708
diff changeset
    41
    DoubleArrays store doubleFloat values (and nothing else).
16639
d9417b0b93a1 class: DoubleArray
Claus Gittinger <cg@exept.de>
parents: 13825
diff changeset
    42
    They have been added to support heavy duty number crunching and
d9417b0b93a1 class: DoubleArray
Claus Gittinger <cg@exept.de>
parents: 13825
diff changeset
    43
    data exchange with openGL frameworks and other mass data libraries
d9417b0b93a1 class: DoubleArray
Claus Gittinger <cg@exept.de>
parents: 13825
diff changeset
    44
    somewhat better than other smalltalks do. 
362
claus
parents: 155
diff changeset
    45
    Storing Floats & Doubles in these objects (instead of Arrays)
claus
parents: 155
diff changeset
    46
    has some benefits:
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
    47
88
81dacba7a63a *** empty log message ***
claus
parents: 3
diff changeset
    48
    1) since the values are stored directly (instead of pointers to them)
81dacba7a63a *** empty log message ***
claus
parents: 3
diff changeset
    49
       both access overhead and garbage collect overhead is minimized.
81dacba7a63a *** empty log message ***
claus
parents: 3
diff changeset
    50
81dacba7a63a *** empty log message ***
claus
parents: 3
diff changeset
    51
    2) they can be much faster passed to c functions (such as graphics 
81dacba7a63a *** empty log message ***
claus
parents: 3
diff changeset
    52
       libraries or heavy duty math packages), since the double values
81dacba7a63a *** empty log message ***
claus
parents: 3
diff changeset
    53
       come packed and can be used in C by using a (double *) or double[].
362
claus
parents: 155
diff changeset
    54
       There is no need to loop over the array extracting doubles.      
88
81dacba7a63a *** empty log message ***
claus
parents: 3
diff changeset
    55
81dacba7a63a *** empty log message ***
claus
parents: 3
diff changeset
    56
    3) they could (in theory) be much more easily be processed by things like
81dacba7a63a *** empty log message ***
claus
parents: 3
diff changeset
    57
       vector and array processors
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
    58
88
81dacba7a63a *** empty log message ***
claus
parents: 3
diff changeset
    59
    Be aware however, that Float- and DoubleArrays are not supported in other
81dacba7a63a *** empty log message ***
claus
parents: 3
diff changeset
    60
    smalltalks - your program will thus become somewhat less portable.
81dacba7a63a *** empty log message ***
claus
parents: 3
diff changeset
    61
    (since their protocol is the same as normal arrays filled with floats,
81dacba7a63a *** empty log message ***
claus
parents: 3
diff changeset
    62
     they can of course be easily simulated - a bit slower though)
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
    63
2820
a128d890def3 comment
Claus Gittinger <cg@exept.de>
parents: 2145
diff changeset
    64
    However, they could be simulated by a ByteArray, using doubleAt: and 
92
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
    65
    doubleAtPut: messages to access the elements, but that seems a bit
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
    66
    clumsy and unelegant. Also, the stc-compiler may learn how to deal
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
    67
    with Float- and DoubleArrays, making accesses very fast in the future.
2820
a128d890def3 comment
Claus Gittinger <cg@exept.de>
parents: 2145
diff changeset
    68
    Hint: if you use doubleArrays in your application and must port it
a128d890def3 comment
Claus Gittinger <cg@exept.de>
parents: 2145
diff changeset
    69
    to some other smalltalk, define a DoubleArray class there, which is derived
a128d890def3 comment
Claus Gittinger <cg@exept.de>
parents: 2145
diff changeset
    70
    from ByteArray, and add access methods.
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
    71
2145
d243ffafeae3 more docu
Claus Gittinger <cg@exept.de>
parents: 1290
diff changeset
    72
    Of course, DoubleArray can be subclassed,
d243ffafeae3 more docu
Claus Gittinger <cg@exept.de>
parents: 1290
diff changeset
    73
    and named instance variables can be added there.
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
    74
362
claus
parents: 155
diff changeset
    75
    See example uses in the GLX interface and GLDemos.
1263
92c1db6b0776 commentary
Claus Gittinger <cg@exept.de>
parents: 627
diff changeset
    76
4062
4959de96b06f documentation
Claus Gittinger <cg@exept.de>
parents: 2820
diff changeset
    77
    [memory requirements:]
4959de96b06f documentation
Claus Gittinger <cg@exept.de>
parents: 2820
diff changeset
    78
        OBJ-HEADER + (size * double-size)
4959de96b06f documentation
Claus Gittinger <cg@exept.de>
parents: 2820
diff changeset
    79
1263
92c1db6b0776 commentary
Claus Gittinger <cg@exept.de>
parents: 627
diff changeset
    80
    [See also:]
92c1db6b0776 commentary
Claus Gittinger <cg@exept.de>
parents: 627
diff changeset
    81
        FloatArray Array
1290
15ba3221b89b documentation
Claus Gittinger <cg@exept.de>
parents: 1263
diff changeset
    82
15ba3221b89b documentation
Claus Gittinger <cg@exept.de>
parents: 1263
diff changeset
    83
    [author:]
15ba3221b89b documentation
Claus Gittinger <cg@exept.de>
parents: 1263
diff changeset
    84
        Claus Gittinger
88
81dacba7a63a *** empty log message ***
claus
parents: 3
diff changeset
    85
"
627
0a9e18feab08 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 565
diff changeset
    86
! !
0a9e18feab08 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 565
diff changeset
    87
13708
079c1e1c7594 faster elementByteSize query
Claus Gittinger <cg@exept.de>
parents: 7220
diff changeset
    88
!DoubleArray class methodsFor:'queries'!
079c1e1c7594 faster elementByteSize query
Claus Gittinger <cg@exept.de>
parents: 7220
diff changeset
    89
079c1e1c7594 faster elementByteSize query
Claus Gittinger <cg@exept.de>
parents: 7220
diff changeset
    90
elementByteSize
16850
4011fc651793 comment/format in: #elementByteSize
Claus Gittinger <cg@exept.de>
parents: 16639
diff changeset
    91
    "for bit-like containers, return the number of bytes stored per element.
4011fc651793 comment/format in: #elementByteSize
Claus Gittinger <cg@exept.de>
parents: 16639
diff changeset
    92
     Here, 8 is returned"
4011fc651793 comment/format in: #elementByteSize
Claus Gittinger <cg@exept.de>
parents: 16639
diff changeset
    93
13708
079c1e1c7594 faster elementByteSize query
Claus Gittinger <cg@exept.de>
parents: 7220
diff changeset
    94
    ^ 8
079c1e1c7594 faster elementByteSize query
Claus Gittinger <cg@exept.de>
parents: 7220
diff changeset
    95
079c1e1c7594 faster elementByteSize query
Claus Gittinger <cg@exept.de>
parents: 7220
diff changeset
    96
    "Created: / 15-09-2011 / 14:12:46 / cg"
079c1e1c7594 faster elementByteSize query
Claus Gittinger <cg@exept.de>
parents: 7220
diff changeset
    97
! !
079c1e1c7594 faster elementByteSize query
Claus Gittinger <cg@exept.de>
parents: 7220
diff changeset
    98
7220
5225d13b2d57 inheritance & documentation
Claus Gittinger <cg@exept.de>
parents: 4062
diff changeset
    99
!DoubleArray methodsFor:'queries'!
5225d13b2d57 inheritance & documentation
Claus Gittinger <cg@exept.de>
parents: 4062
diff changeset
   100
5225d13b2d57 inheritance & documentation
Claus Gittinger <cg@exept.de>
parents: 4062
diff changeset
   101
defaultElement
5225d13b2d57 inheritance & documentation
Claus Gittinger <cg@exept.de>
parents: 4062
diff changeset
   102
    ^ Float zero
5225d13b2d57 inheritance & documentation
Claus Gittinger <cg@exept.de>
parents: 4062
diff changeset
   103
! !
5225d13b2d57 inheritance & documentation
Claus Gittinger <cg@exept.de>
parents: 4062
diff changeset
   104
19031
b8bbe1983d3a #FEATURE
Claus Gittinger <cg@exept.de>
parents: 16853
diff changeset
   105
!DoubleArray methodsFor:'vector arithmetic'!
b8bbe1983d3a #FEATURE
Claus Gittinger <cg@exept.de>
parents: 16853
diff changeset
   106
b8bbe1983d3a #FEATURE
Claus Gittinger <cg@exept.de>
parents: 16853
diff changeset
   107
dot: aFloatVector
b8bbe1983d3a #FEATURE
Claus Gittinger <cg@exept.de>
parents: 16853
diff changeset
   108
    "Return the dot product of the receiver and the argument.
b8bbe1983d3a #FEATURE
Claus Gittinger <cg@exept.de>
parents: 16853
diff changeset
   109
     Raises an error, if the argument is not of the same size as the receiver."
b8bbe1983d3a #FEATURE
Claus Gittinger <cg@exept.de>
parents: 16853
diff changeset
   110
b8bbe1983d3a #FEATURE
Claus Gittinger <cg@exept.de>
parents: 16853
diff changeset
   111
    | mySize result |
b8bbe1983d3a #FEATURE
Claus Gittinger <cg@exept.de>
parents: 16853
diff changeset
   112
b8bbe1983d3a #FEATURE
Claus Gittinger <cg@exept.de>
parents: 16853
diff changeset
   113
%{
b8bbe1983d3a #FEATURE
Claus Gittinger <cg@exept.de>
parents: 16853
diff changeset
   114
    if ((__ClassInstPtr(__qClass(self))->c_ninstvars == __mkSmallInteger(0))
b8bbe1983d3a #FEATURE
Claus Gittinger <cg@exept.de>
parents: 16853
diff changeset
   115
     && (__ClassInstPtr(__qClass(aFloatVector))->c_ninstvars == __mkSmallInteger(0))) {
b8bbe1983d3a #FEATURE
Claus Gittinger <cg@exept.de>
parents: 16853
diff changeset
   116
        INT __mySize = __doubleArraySize(self);
b8bbe1983d3a #FEATURE
Claus Gittinger <cg@exept.de>
parents: 16853
diff changeset
   117
        double __result = 0.0;
b8bbe1983d3a #FEATURE
Claus Gittinger <cg@exept.de>
parents: 16853
diff changeset
   118
        double *__p1 = __DoubleArrayInstPtr(self)->d_element;
b8bbe1983d3a #FEATURE
Claus Gittinger <cg@exept.de>
parents: 16853
diff changeset
   119
b8bbe1983d3a #FEATURE
Claus Gittinger <cg@exept.de>
parents: 16853
diff changeset
   120
        if (__mySize > 0) {
b8bbe1983d3a #FEATURE
Claus Gittinger <cg@exept.de>
parents: 16853
diff changeset
   121
            if (__isFloats(aFloatVector)) {
b8bbe1983d3a #FEATURE
Claus Gittinger <cg@exept.de>
parents: 16853
diff changeset
   122
                INT __otherSize = __floatArraySize(aFloatVector);
b8bbe1983d3a #FEATURE
Claus Gittinger <cg@exept.de>
parents: 16853
diff changeset
   123
b8bbe1983d3a #FEATURE
Claus Gittinger <cg@exept.de>
parents: 16853
diff changeset
   124
                if (__mySize == __otherSize) {
b8bbe1983d3a #FEATURE
Claus Gittinger <cg@exept.de>
parents: 16853
diff changeset
   125
                    float *__p2 = __FloatArrayInstPtr(aFloatVector)->f_element;
b8bbe1983d3a #FEATURE
Claus Gittinger <cg@exept.de>
parents: 16853
diff changeset
   126
                    INT __i;
b8bbe1983d3a #FEATURE
Claus Gittinger <cg@exept.de>
parents: 16853
diff changeset
   127
                    /* how about inline-mmx-asm for this ... */
b8bbe1983d3a #FEATURE
Claus Gittinger <cg@exept.de>
parents: 16853
diff changeset
   128
                    for (__i=0; __i<__mySize; __i++) {
b8bbe1983d3a #FEATURE
Claus Gittinger <cg@exept.de>
parents: 16853
diff changeset
   129
                        __result += (__p1[__i] * __p2[__i]);
b8bbe1983d3a #FEATURE
Claus Gittinger <cg@exept.de>
parents: 16853
diff changeset
   130
                    }
b8bbe1983d3a #FEATURE
Claus Gittinger <cg@exept.de>
parents: 16853
diff changeset
   131
                    RETURN (__MKFLOAT(__result));
b8bbe1983d3a #FEATURE
Claus Gittinger <cg@exept.de>
parents: 16853
diff changeset
   132
                }
b8bbe1983d3a #FEATURE
Claus Gittinger <cg@exept.de>
parents: 16853
diff changeset
   133
            } else if (__isDoubles(aFloatVector)) {
b8bbe1983d3a #FEATURE
Claus Gittinger <cg@exept.de>
parents: 16853
diff changeset
   134
                INT __otherSize = __doubleArraySize(aFloatVector);
b8bbe1983d3a #FEATURE
Claus Gittinger <cg@exept.de>
parents: 16853
diff changeset
   135
b8bbe1983d3a #FEATURE
Claus Gittinger <cg@exept.de>
parents: 16853
diff changeset
   136
                if (__mySize == __otherSize) {
b8bbe1983d3a #FEATURE
Claus Gittinger <cg@exept.de>
parents: 16853
diff changeset
   137
                    double *__p2 = __DoubleArrayInstPtr(aFloatVector)->d_element;
b8bbe1983d3a #FEATURE
Claus Gittinger <cg@exept.de>
parents: 16853
diff changeset
   138
                    INT __i;
b8bbe1983d3a #FEATURE
Claus Gittinger <cg@exept.de>
parents: 16853
diff changeset
   139
                    /* how about inline-mmx-asm for this ... */
b8bbe1983d3a #FEATURE
Claus Gittinger <cg@exept.de>
parents: 16853
diff changeset
   140
                    for (__i=0; __i<__mySize; __i++) {
b8bbe1983d3a #FEATURE
Claus Gittinger <cg@exept.de>
parents: 16853
diff changeset
   141
                        __result += (__p1[__i] * __p2[__i]);
b8bbe1983d3a #FEATURE
Claus Gittinger <cg@exept.de>
parents: 16853
diff changeset
   142
                    }
b8bbe1983d3a #FEATURE
Claus Gittinger <cg@exept.de>
parents: 16853
diff changeset
   143
                    RETURN (__MKFLOAT(__result));
b8bbe1983d3a #FEATURE
Claus Gittinger <cg@exept.de>
parents: 16853
diff changeset
   144
                }
b8bbe1983d3a #FEATURE
Claus Gittinger <cg@exept.de>
parents: 16853
diff changeset
   145
            }
b8bbe1983d3a #FEATURE
Claus Gittinger <cg@exept.de>
parents: 16853
diff changeset
   146
        }
b8bbe1983d3a #FEATURE
Claus Gittinger <cg@exept.de>
parents: 16853
diff changeset
   147
    }
b8bbe1983d3a #FEATURE
Claus Gittinger <cg@exept.de>
parents: 16853
diff changeset
   148
%}.
b8bbe1983d3a #FEATURE
Claus Gittinger <cg@exept.de>
parents: 16853
diff changeset
   149
    ^ super dot:aFloatVector
b8bbe1983d3a #FEATURE
Claus Gittinger <cg@exept.de>
parents: 16853
diff changeset
   150
b8bbe1983d3a #FEATURE
Claus Gittinger <cg@exept.de>
parents: 16853
diff changeset
   151
    "
b8bbe1983d3a #FEATURE
Claus Gittinger <cg@exept.de>
parents: 16853
diff changeset
   152
     |v|
b8bbe1983d3a #FEATURE
Claus Gittinger <cg@exept.de>
parents: 16853
diff changeset
   153
b8bbe1983d3a #FEATURE
Claus Gittinger <cg@exept.de>
parents: 16853
diff changeset
   154
     v := #(2.0 2.0 1.0) asFloatArray.
b8bbe1983d3a #FEATURE
Claus Gittinger <cg@exept.de>
parents: 16853
diff changeset
   155
     v dot:v.
b8bbe1983d3a #FEATURE
Claus Gittinger <cg@exept.de>
parents: 16853
diff changeset
   156
    "
b8bbe1983d3a #FEATURE
Claus Gittinger <cg@exept.de>
parents: 16853
diff changeset
   157
    "
b8bbe1983d3a #FEATURE
Claus Gittinger <cg@exept.de>
parents: 16853
diff changeset
   158
     |v1 v2|
b8bbe1983d3a #FEATURE
Claus Gittinger <cg@exept.de>
parents: 16853
diff changeset
   159
b8bbe1983d3a #FEATURE
Claus Gittinger <cg@exept.de>
parents: 16853
diff changeset
   160
     v1 := FloatArray new:10000 withAll:2.
b8bbe1983d3a #FEATURE
Claus Gittinger <cg@exept.de>
parents: 16853
diff changeset
   161
     v2 := FloatArray new:10000 withAll:3.
b8bbe1983d3a #FEATURE
Claus Gittinger <cg@exept.de>
parents: 16853
diff changeset
   162
     Time millisecondsToRun:[
b8bbe1983d3a #FEATURE
Claus Gittinger <cg@exept.de>
parents: 16853
diff changeset
   163
        10000 timesRepeat:[
b8bbe1983d3a #FEATURE
Claus Gittinger <cg@exept.de>
parents: 16853
diff changeset
   164
          v1 dot:v2.
b8bbe1983d3a #FEATURE
Claus Gittinger <cg@exept.de>
parents: 16853
diff changeset
   165
        ]
b8bbe1983d3a #FEATURE
Claus Gittinger <cg@exept.de>
parents: 16853
diff changeset
   166
     ]
b8bbe1983d3a #FEATURE
Claus Gittinger <cg@exept.de>
parents: 16853
diff changeset
   167
    "
b8bbe1983d3a #FEATURE
Claus Gittinger <cg@exept.de>
parents: 16853
diff changeset
   168
b8bbe1983d3a #FEATURE
Claus Gittinger <cg@exept.de>
parents: 16853
diff changeset
   169
    "Created: / 29-05-2007 / 13:13:39 / cg"
b8bbe1983d3a #FEATURE
Claus Gittinger <cg@exept.de>
parents: 16853
diff changeset
   170
!
b8bbe1983d3a #FEATURE
Claus Gittinger <cg@exept.de>
parents: 16853
diff changeset
   171
b8bbe1983d3a #FEATURE
Claus Gittinger <cg@exept.de>
parents: 16853
diff changeset
   172
hornerMultiplyAndAdd:x
b8bbe1983d3a #FEATURE
Claus Gittinger <cg@exept.de>
parents: 16853
diff changeset
   173
    "primitive support for horner's-method computation of polynomials.
b8bbe1983d3a #FEATURE
Claus Gittinger <cg@exept.de>
parents: 16853
diff changeset
   174
     The vector is interpreted as providing the factors for a polynomial,
b8bbe1983d3a #FEATURE
Claus Gittinger <cg@exept.de>
parents: 16853
diff changeset
   175
        an*x^n + (an-1)*x^(n-1) + ... + a2(x) + a1
b8bbe1983d3a #FEATURE
Claus Gittinger <cg@exept.de>
parents: 16853
diff changeset
   176
     where the ai are the elements of the Array.
b8bbe1983d3a #FEATURE
Claus Gittinger <cg@exept.de>
parents: 16853
diff changeset
   177
     The highest rank factor is at the first position, the 0-rank constant at last.
b8bbe1983d3a #FEATURE
Claus Gittinger <cg@exept.de>
parents: 16853
diff changeset
   178
     This is inlined c-code, which may get compiled to fast machine code,
b8bbe1983d3a #FEATURE
Claus Gittinger <cg@exept.de>
parents: 16853
diff changeset
   179
     using multiply-and-add or vector instructions, if the CPU/Compiler support them."
b8bbe1983d3a #FEATURE
Claus Gittinger <cg@exept.de>
parents: 16853
diff changeset
   180
b8bbe1983d3a #FEATURE
Claus Gittinger <cg@exept.de>
parents: 16853
diff changeset
   181
    | mySize result |
b8bbe1983d3a #FEATURE
Claus Gittinger <cg@exept.de>
parents: 16853
diff changeset
   182
b8bbe1983d3a #FEATURE
Claus Gittinger <cg@exept.de>
parents: 16853
diff changeset
   183
%{
b8bbe1983d3a #FEATURE
Claus Gittinger <cg@exept.de>
parents: 16853
diff changeset
   184
    double __x;
b8bbe1983d3a #FEATURE
Claus Gittinger <cg@exept.de>
parents: 16853
diff changeset
   185
b8bbe1983d3a #FEATURE
Claus Gittinger <cg@exept.de>
parents: 16853
diff changeset
   186
    if (__isFloat(x)) {
b8bbe1983d3a #FEATURE
Claus Gittinger <cg@exept.de>
parents: 16853
diff changeset
   187
        __x = __floatVal(x);
b8bbe1983d3a #FEATURE
Claus Gittinger <cg@exept.de>
parents: 16853
diff changeset
   188
    } else if (__isShortFloat(x)) {
b8bbe1983d3a #FEATURE
Claus Gittinger <cg@exept.de>
parents: 16853
diff changeset
   189
        __x = (double)__shortFloatVal(x);
b8bbe1983d3a #FEATURE
Claus Gittinger <cg@exept.de>
parents: 16853
diff changeset
   190
    } else if (__isSmallInteger(x)) {
b8bbe1983d3a #FEATURE
Claus Gittinger <cg@exept.de>
parents: 16853
diff changeset
   191
        __x = (double)(__intVal(x));
b8bbe1983d3a #FEATURE
Claus Gittinger <cg@exept.de>
parents: 16853
diff changeset
   192
    } else {
b8bbe1983d3a #FEATURE
Claus Gittinger <cg@exept.de>
parents: 16853
diff changeset
   193
        goto getOutOfHere;
b8bbe1983d3a #FEATURE
Claus Gittinger <cg@exept.de>
parents: 16853
diff changeset
   194
    }
b8bbe1983d3a #FEATURE
Claus Gittinger <cg@exept.de>
parents: 16853
diff changeset
   195
b8bbe1983d3a #FEATURE
Claus Gittinger <cg@exept.de>
parents: 16853
diff changeset
   196
    if (__ClassInstPtr(__qClass(self))->c_ninstvars == __mkSmallInteger(0)) {
b8bbe1983d3a #FEATURE
Claus Gittinger <cg@exept.de>
parents: 16853
diff changeset
   197
        INT __mySize = __doubleArraySize(self);
b8bbe1983d3a #FEATURE
Claus Gittinger <cg@exept.de>
parents: 16853
diff changeset
   198
        double *__elements = __DoubleArrayInstPtr(self)->d_element;
b8bbe1983d3a #FEATURE
Claus Gittinger <cg@exept.de>
parents: 16853
diff changeset
   199
        double __result = __elements[0];
b8bbe1983d3a #FEATURE
Claus Gittinger <cg@exept.de>
parents: 16853
diff changeset
   200
b8bbe1983d3a #FEATURE
Claus Gittinger <cg@exept.de>
parents: 16853
diff changeset
   201
        if (__mySize > 1) {
b8bbe1983d3a #FEATURE
Claus Gittinger <cg@exept.de>
parents: 16853
diff changeset
   202
            INT __i;
b8bbe1983d3a #FEATURE
Claus Gittinger <cg@exept.de>
parents: 16853
diff changeset
   203
            /* how about inline-mmx-asm for this ... */
b8bbe1983d3a #FEATURE
Claus Gittinger <cg@exept.de>
parents: 16853
diff changeset
   204
            for (__i=1; __i<__mySize; __i++) {
b8bbe1983d3a #FEATURE
Claus Gittinger <cg@exept.de>
parents: 16853
diff changeset
   205
                __result = (__result * __x) + __elements[__i];
b8bbe1983d3a #FEATURE
Claus Gittinger <cg@exept.de>
parents: 16853
diff changeset
   206
            }
b8bbe1983d3a #FEATURE
Claus Gittinger <cg@exept.de>
parents: 16853
diff changeset
   207
        }
b8bbe1983d3a #FEATURE
Claus Gittinger <cg@exept.de>
parents: 16853
diff changeset
   208
        RETURN (__MKFLOAT(__result));
b8bbe1983d3a #FEATURE
Claus Gittinger <cg@exept.de>
parents: 16853
diff changeset
   209
    }
b8bbe1983d3a #FEATURE
Claus Gittinger <cg@exept.de>
parents: 16853
diff changeset
   210
getOutOfHere: ;    
b8bbe1983d3a #FEATURE
Claus Gittinger <cg@exept.de>
parents: 16853
diff changeset
   211
%}.
b8bbe1983d3a #FEATURE
Claus Gittinger <cg@exept.de>
parents: 16853
diff changeset
   212
    ^ super hornerMultiplyAndAdd:x
b8bbe1983d3a #FEATURE
Claus Gittinger <cg@exept.de>
parents: 16853
diff changeset
   213
b8bbe1983d3a #FEATURE
Claus Gittinger <cg@exept.de>
parents: 16853
diff changeset
   214
    "
b8bbe1983d3a #FEATURE
Claus Gittinger <cg@exept.de>
parents: 16853
diff changeset
   215
     |v|
b8bbe1983d3a #FEATURE
Claus Gittinger <cg@exept.de>
parents: 16853
diff changeset
   216
     v := #(2.0 3.0 4.0) asDoubleArray.
b8bbe1983d3a #FEATURE
Claus Gittinger <cg@exept.de>
parents: 16853
diff changeset
   217
     v  hornerMultiplyAndAdd:10.
b8bbe1983d3a #FEATURE
Claus Gittinger <cg@exept.de>
parents: 16853
diff changeset
   218
    
b8bbe1983d3a #FEATURE
Claus Gittinger <cg@exept.de>
parents: 16853
diff changeset
   219
     |v|
b8bbe1983d3a #FEATURE
Claus Gittinger <cg@exept.de>
parents: 16853
diff changeset
   220
     v := Array new:100 withAll:2.0.
b8bbe1983d3a #FEATURE
Claus Gittinger <cg@exept.de>
parents: 16853
diff changeset
   221
     v hornerMultiplyAndAdd:10
b8bbe1983d3a #FEATURE
Claus Gittinger <cg@exept.de>
parents: 16853
diff changeset
   222
b8bbe1983d3a #FEATURE
Claus Gittinger <cg@exept.de>
parents: 16853
diff changeset
   223
     |v|
b8bbe1983d3a #FEATURE
Claus Gittinger <cg@exept.de>
parents: 16853
diff changeset
   224
     v := Array new:100 withAll:2.0.
b8bbe1983d3a #FEATURE
Claus Gittinger <cg@exept.de>
parents: 16853
diff changeset
   225
     Time millisecondsToRun:[
b8bbe1983d3a #FEATURE
Claus Gittinger <cg@exept.de>
parents: 16853
diff changeset
   226
        10000 timesRepeat:[ v hornerMultiplyAndAdd:10]
b8bbe1983d3a #FEATURE
Claus Gittinger <cg@exept.de>
parents: 16853
diff changeset
   227
     ]
b8bbe1983d3a #FEATURE
Claus Gittinger <cg@exept.de>
parents: 16853
diff changeset
   228
b8bbe1983d3a #FEATURE
Claus Gittinger <cg@exept.de>
parents: 16853
diff changeset
   229
     |v|
b8bbe1983d3a #FEATURE
Claus Gittinger <cg@exept.de>
parents: 16853
diff changeset
   230
     v := FloatArray new:100 withAll:2.
b8bbe1983d3a #FEATURE
Claus Gittinger <cg@exept.de>
parents: 16853
diff changeset
   231
     Time millisecondsToRun:[
b8bbe1983d3a #FEATURE
Claus Gittinger <cg@exept.de>
parents: 16853
diff changeset
   232
        10000 timesRepeat:[ v hornerMultiplyAndAdd:10]
b8bbe1983d3a #FEATURE
Claus Gittinger <cg@exept.de>
parents: 16853
diff changeset
   233
     ]
b8bbe1983d3a #FEATURE
Claus Gittinger <cg@exept.de>
parents: 16853
diff changeset
   234
b8bbe1983d3a #FEATURE
Claus Gittinger <cg@exept.de>
parents: 16853
diff changeset
   235
     |v|
b8bbe1983d3a #FEATURE
Claus Gittinger <cg@exept.de>
parents: 16853
diff changeset
   236
     v := DoubleArray new:100 withAll:2.
b8bbe1983d3a #FEATURE
Claus Gittinger <cg@exept.de>
parents: 16853
diff changeset
   237
     Time millisecondsToRun:[
b8bbe1983d3a #FEATURE
Claus Gittinger <cg@exept.de>
parents: 16853
diff changeset
   238
        10000 timesRepeat:[ v hornerMultiplyAndAdd:10]
b8bbe1983d3a #FEATURE
Claus Gittinger <cg@exept.de>
parents: 16853
diff changeset
   239
     ]
b8bbe1983d3a #FEATURE
Claus Gittinger <cg@exept.de>
parents: 16853
diff changeset
   240
    "
b8bbe1983d3a #FEATURE
Claus Gittinger <cg@exept.de>
parents: 16853
diff changeset
   241
! !
b8bbe1983d3a #FEATURE
Claus Gittinger <cg@exept.de>
parents: 16853
diff changeset
   242
627
0a9e18feab08 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 565
diff changeset
   243
!DoubleArray class methodsFor:'documentation'!
565
4f64b1faa6e2 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 529
diff changeset
   244
4f64b1faa6e2 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 529
diff changeset
   245
version
19031
b8bbe1983d3a #FEATURE
Claus Gittinger <cg@exept.de>
parents: 16853
diff changeset
   246
    ^ '$Header$'
16853
e3e062d68503 class: DoubleArray
Claus Gittinger <cg@exept.de>
parents: 16850
diff changeset
   247
!
e3e062d68503 class: DoubleArray
Claus Gittinger <cg@exept.de>
parents: 16850
diff changeset
   248
e3e062d68503 class: DoubleArray
Claus Gittinger <cg@exept.de>
parents: 16850
diff changeset
   249
version_CVS
19031
b8bbe1983d3a #FEATURE
Claus Gittinger <cg@exept.de>
parents: 16853
diff changeset
   250
    ^ '$Header$'
88
81dacba7a63a *** empty log message ***
claus
parents: 3
diff changeset
   251
! !
16639
d9417b0b93a1 class: DoubleArray
Claus Gittinger <cg@exept.de>
parents: 13825
diff changeset
   252