--[[1* Copyright (C) 2007 Apple Inc. All rights reserved.2*3* Redistribution and use in source and binary forms, with or without4* modification, are permitted provided that the following conditions5* are met:6* 1. Redistributions of source code must retain the above copyright7* notice, this list of conditions and the following disclaimer.8* 2. Redistributions in binary form must reproduce the above copyright9* notice, this list of conditions and the following disclaimer in the10* documentation and/or other materials provided with the distribution.11*12* THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY13* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE14* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR15* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR16* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,17* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,18* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR19* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY20* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT21* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE22* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.23]]2425local function prequire(name) local success, result = pcall(require, name); return success and result end26local bench = script and require(script.Parent.bench_support) or prequire("bench_support") or require("../../bench_support")2728function test()2930local loops = 1531local nx = 12032local nz = 1203334local function morph(a, f)35local PI2nx = math.pi * 8/nx36local sin = math.sin37local f30 = -(50 * sin(f*math.pi*2))3839for i = 0,nz-1 do40for j = 0,nx-1 do41a[3*(i*nx+j)+1] = sin((j-1) * PI2nx ) * -f3042end43end44end454647local a = {}48for i = 0,nx*nz*3-1 do49a[i] = 050end5152for i = 0,loops-1 do53morph(a, i/loops)54end5556testOutput = 0;57for i = 0,nx-1 do58testOutput = testOutput + a[3*(i*nx+i)+1];59end6061a = nil;6263-- This has to be an approximate test since ECMAscript doesn't formally specify64-- what sin() returns. Even if it did specify something like for example what Java 765-- says - that sin() has to return a value within 1 ulp of exact - then we still66-- would not be able to do an exact test here since that would allow for just enough67-- low-bit slop to create possibly big errors due to testOutput being a sum.68local epsilon = 1e-13;69if (math.abs(testOutput) >= epsilon) then70assert(false, "Error: bad test output: expected magnitude below " .. epsilon .. " but got " .. testOutput);71end7273end7475bench.runCode(test, "3d-morph")767778