Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Roblox
GitHub Repository: Roblox/luau
Path: blob/master/bench/tests/sunspider/math-cordic.lua
2727 views
1
--[[
2
* Copyright (C) Rich Moore. All rights reserved.
3
*
4
* Redistribution and use in source and binary forms, with or without
5
* modification, are permitted provided that the following conditions
6
* are met:
7
* 1. Redistributions of source code must retain the above copyright
8
* notice, this list of conditions and the following disclaimer.
9
* 2. Redistributions in binary form must reproduce the above copyright
10
* notice, this list of conditions and the following disclaimer in the
11
* documentation and/or other materials provided with the distribution.
12
*
13
* THIS SOFTWARE IS PROVIDED BY CONTRIBUTORS ``AS IS'' AND ANY
14
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
17
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21
* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24
]]
25
26
local function prequire(name) local success, result = pcall(require, name); return success and result end
27
local bench = script and require(script.Parent.bench_support) or prequire("bench_support") or require("../../bench_support")
28
29
function test()
30
31
--. Start CORDIC
32
33
local AG_CONST = 0.6072529350;
34
35
local function FIXED(X)
36
return X * 65536.0;
37
end
38
39
local function FLOAT(X)
40
return X / 65536.0;
41
end
42
43
local function DEG2RAD(X)
44
return 0.017453 * (X);
45
end
46
47
local Angles = {
48
FIXED(45.0), FIXED(26.565), FIXED(14.0362), FIXED(7.12502),
49
FIXED(3.57633), FIXED(1.78991), FIXED(0.895174), FIXED(0.447614),
50
FIXED(0.223811), FIXED(0.111906), FIXED(0.055953),
51
FIXED(0.027977)
52
};
53
54
local Target = 28.027;
55
56
local function cordicsincos(Target)
57
local X;
58
local Y;
59
local TargetAngle;
60
local CurrAngle;
61
62
X = FIXED(AG_CONST); -- AG_CONST * cos(0)
63
Y = 0; -- AG_CONST * sin(0)
64
65
TargetAngle = FIXED(Target);
66
CurrAngle = 0;
67
for Step = 0,11 do
68
local NewX;
69
if (TargetAngle > CurrAngle) then
70
NewX = X - bit32.rshift(math.floor(Y), Step) -- (Y >> Step);
71
Y = bit32.rshift(math.floor(X), Step) + Y;
72
X = NewX;
73
CurrAngle = CurrAngle + Angles[Step + 1];
74
else
75
NewX = X + bit32.rshift(math.floor(Y), Step)
76
Y = -bit32.rshift(math.floor(X), Step) + Y;
77
X = NewX;
78
CurrAngle = CurrAngle - Angles[Step + 1];
79
end
80
end
81
82
return FLOAT(X) * FLOAT(Y);
83
end
84
85
-- End CORDIC
86
87
local total = 0;
88
89
local function cordic( runs )
90
for i = 1,runs do
91
total = total + cordicsincos(Target);
92
end
93
end
94
95
cordic(25000);
96
97
local expected = 10362.570468755888;
98
99
if (total ~= expected) then
100
assert(false, "ERROR: bad result: expected " .. expected .. " but got " .. total);
101
end
102
103
end
104
105
bench.runCode(test, "math-cordic")
106
107