1 // Copyright 2008 the V8 project authors. All rights reserved.
2 // Copyright (C) 2013 Apple Inc. All rights reserved.
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are
8 // * Redistributions of source code must retain the above copyright
9 // notice, this list of conditions and the following disclaimer.
10 // * Redistributions in binary form must reproduce the above
11 // copyright notice, this list of conditions and the following
12 // disclaimer in the documentation and/or other materials provided
13 // with the distribution.
14 // * Neither the name of Google Inc. nor the names of its
15 // contributors may be used to endorse or promote products derived
16 // from this software without specific prior written permission.
18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 // Test the performance of Int16Array by implementing a bubble sort.
32 function bubbleSort(array)
34 for (var i = 1; i < array.length; ++i) {
35 for (var j = 0; j < array.length - i; ++j) {
36 if (array[j + 1] < array[j]) {
38 array[j] = array[j + 1];
45 // V8's random function.
46 myRandom = (function() {
49 // Robert Jenkins' 32 bit integer hash function.
50 seed = ((seed + 0x7ed55d16) + (seed << 12)) & 0xffffffff;
51 seed = ((seed ^ 0xc761c23c) ^ (seed >>> 19)) & 0xffffffff;
52 seed = ((seed + 0x165667b1) + (seed << 5)) & 0xffffffff;
53 seed = ((seed + 0xd3a2646c) ^ (seed << 9)) & 0xffffffff;
54 seed = ((seed + 0xfd7046c5) + (seed << 3)) & 0xffffffff;
55 seed = ((seed ^ 0xb55a4f09) ^ (seed >>> 16)) & 0xffffffff;
56 return (seed & 0xfffffff) / 0x10000000;
60 function arrayToString(array) {
62 for (var i = 0; i < array.length; ++i) {
70 function validateSort(array) {
71 for (var i = 0; i < array.length - 1; ++i) {
72 if (array[i + 1] < array[i])
78 var array = new Int16Array(1000);
79 for (var count = 0; count < 10; ++count) {
80 for (var i = 0; i < array.length; ++i) {
81 array[i] = myRandom() * 65535;
86 if (!validateSort(array))
87 throw "Bad result: " + arrayToString(array);