--[[1* Copyright (C) Rich Moore. 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 CONTRIBUTORS ``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()2930--. Start CORDIC3132local AG_CONST = 0.6072529350;3334local function FIXED(X)35return X * 65536.0;36end3738local function FLOAT(X)39return X / 65536.0;40end4142local function DEG2RAD(X)43return 0.017453 * (X);44end4546local Angles = {47FIXED(45.0), FIXED(26.565), FIXED(14.0362), FIXED(7.12502),48FIXED(3.57633), FIXED(1.78991), FIXED(0.895174), FIXED(0.447614),49FIXED(0.223811), FIXED(0.111906), FIXED(0.055953),50FIXED(0.027977)51};5253local Target = 28.027;5455local function cordicsincos(Target)56local X;57local Y;58local TargetAngle;59local CurrAngle;6061X = FIXED(AG_CONST); -- AG_CONST * cos(0)62Y = 0; -- AG_CONST * sin(0)6364TargetAngle = FIXED(Target);65CurrAngle = 0;66for Step = 0,11 do67local NewX;68if (TargetAngle > CurrAngle) then69NewX = X - bit32.rshift(math.floor(Y), Step) -- (Y >> Step);70Y = bit32.rshift(math.floor(X), Step) + Y;71X = NewX;72CurrAngle = CurrAngle + Angles[Step + 1];73else74NewX = X + bit32.rshift(math.floor(Y), Step)75Y = -bit32.rshift(math.floor(X), Step) + Y;76X = NewX;77CurrAngle = CurrAngle - Angles[Step + 1];78end79end8081return FLOAT(X) * FLOAT(Y);82end8384-- End CORDIC8586local total = 0;8788local function cordic( runs )89for i = 1,runs do90total = total + cordicsincos(Target);91end92end9394cordic(25000);9596local expected = 10362.570468755888;9798if (total ~= expected) then99assert(false, "ERROR: bad result: expected " .. expected .. " but got " .. total);100end101102end103104bench.runCode(test, "math-cordic")105106107