User:Evilpie/Simd: Difference between revisions

 
(9 intermediate revisions by the same user not shown)
Line 1: Line 1:
=== SIMD API ==
= SIMD API =
"Single instruction, multiple data"
"Single instruction, multiple data"


Line 6: Line 6:
This API is currently in the drafting phase, but the main idea is to have different instruction two work on arbitrary sized slices of two arrays. Previous work include for example [http://tirania.org/blog/archive/2008/Nov-03.html Mono's SIMD support] or [http://msdn.microsoft.com/en-us/library/ms590098.aspx Microsoft's .net Vector methods].
This API is currently in the drafting phase, but the main idea is to have different instruction two work on arbitrary sized slices of two arrays. Previous work include for example [http://tirania.org/blog/archive/2008/Nov-03.html Mono's SIMD support] or [http://msdn.microsoft.com/en-us/library/ms590098.aspx Microsoft's .net Vector methods].


The SIMD API is going to expose one new global to the JavaScript environment, called "Simd". This plain object with the [[Class]] "Simd", has the following "static method". (These are methods directly defined on the object)
The SIMD API is going to expose one new global to the JavaScript environment, called "Simd". This plain object with the [[Class]] "Simd", has the following "static methods". (These are methods directly defined on the object)


== 1. Simd.add(arrayA, indexA, arrayB, indexB, count) ==  
== 1. Simd.add(arrayA, indexA, arrayB, indexB, count) ==  
    1. If not arrayA is an object, throw TypeError
1. If not arrayA is an object, throw TypeError.
    2. Let indexA be ToUint32(indexA)
2. Let indexA be ToUint32(indexA).
    3. If not arrayB is an object, throw TypeError
3. If not arrayB is an object, throw TypeError.
    4. Let indexB be ToUint32(indexB)
4. Let indexB be ToUint32(indexB).
    4. Let count be ToUint32(count)
5. Let count be ToUint32(count).
    5. Throw TypeError, if arrayA is not a TypeArray (the class is either Float32Array, Float64 etc.)
6. Throw TypeError, if arrayA is not a TypeArray (the class is either Float32Array, Float64 etc.)
    6. Throw TypeError, if the internal property [[ Class ]] of arrayA is different from internal property [[ Class ]] of arrayB
7. Throw TypeError, if the internal property [[ Class ]] of arrayA is different from internal property [[ Class ]] of arrayB
    7. Throw RangeError if indexA + count is larger than the length of the arrayA
8. Throw RangeError, if indexA + count is larger than the length of the array arrayA.
    8. Throw RangeError if indexB + count is larger than the length of the arrayB
9. Throw RangeError, if indexB + count is larger than the length of the array arrayB.
    9. Let k be 0
10. Let k be 0.
    10. Repeat, while k < count
11. Repeat, while k < count
        a. Let a be arrayA[indexA + k]
    a. Let a be arrayA[indexA + k].
        b. Let b be arrayB[indexB + k]
    b. Let b be arrayB[indexB + k].
        c. Assign a + b to arrayA[indexA + k]
    c. Assign a + b to arrayA[indexA + k].
        c. Increment k by one
    d. Increase k by 1.


= Example =
=== Example ===
    var a = new Float64Array(2);
var a = new Float64Array(2);
    var b = new Float64Array(2);
var b = new Float64Array(2);
      
      
    a[0] = 0;
  a[0] = 0, a[1] = 1;
    b[0] = 2;
  b[0] = 2, b[1] = 1;
    a[1] = 1
    b[1] = 2;
      
      
    Simd.add(a, b, 0, 0, 2);
  Simd.add(a, 0, b, 0, 2);
Now a[0] and [1] should be 2, b should not have changed.
Now a[0] and [1] should be 2, b should not have changed.

Latest revision as of 20:32, 17 November 2011

SIMD API

"Single instruction, multiple data"

The goal of this is API to enable easier optimizations of JavaScript code that needs to work on TypedArrays repeatedly with the same operation. For example adding or subtracting the contents of two arrays.

This API is currently in the drafting phase, but the main idea is to have different instruction two work on arbitrary sized slices of two arrays. Previous work include for example Mono's SIMD support or Microsoft's .net Vector methods.

The SIMD API is going to expose one new global to the JavaScript environment, called "Simd". This plain object with the Class "Simd", has the following "static methods". (These are methods directly defined on the object)

1. Simd.add(arrayA, indexA, arrayB, indexB, count)

1. If not arrayA is an object, throw TypeError.
2. Let indexA be ToUint32(indexA).
3. If not arrayB is an object, throw TypeError.
4. Let indexB be ToUint32(indexB).
5. Let count be ToUint32(count).
6. Throw TypeError, if arrayA is not a TypeArray (the class is either Float32Array, Float64 etc.)
7. Throw TypeError, if the internal property Class  of arrayA is different from internal property Class  of arrayB
8. Throw RangeError, if indexA + count is larger than the length of the array arrayA.
9. Throw RangeError, if indexB + count is larger than the length of the array arrayB.
10. Let k be 0.
11. Repeat, while k < count
   a. Let a be arrayA[indexA + k].
   b. Let b be arrayB[indexB + k].
   c. Assign a + b to arrayA[indexA + k].
   d. Increase k by 1.

Example

var a = new Float64Array(2);
var b = new Float64Array(2);
   
 a[0] = 0, a[1] = 1;
 b[0] = 2, b[1] = 1;
   
 Simd.add(a, 0, b, 0, 2);

Now a[0] and [1] should be 2, b should not have changed.