2
* Copyright (C) 1999-2005 Terence M. Welsh
4
* This file is part of rsMath.
6
* rsMath is free software; you can redistribute it and/or
7
* modify it under the terms of the GNU Lesser General Public
8
* License version 2.1 as published by the Free Software Foundation.
10
* rsMath is distributed in the hope that it will be useful,
11
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
* GNU General Public License for more details.
15
* You should have received a copy of the GNU Lesser General Public
16
* License along with this program; if not, write to the Free Software
17
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21
#ifndef RSTRIGONOMETRY_H
22
#define RSTRIGONOMETRY_H
26
static float rs_cosine_table[256] = {
27
1.0f, 0.999699f, 0.998795f, 0.99729f, 0.995185f, 0.99248f, 0.989177f, 0.985278f,
28
0.980785f, 0.975702f, 0.970031f, 0.963776f, 0.95694f, 0.949528f, 0.941544f, 0.932993f,
29
0.92388f, 0.91421f, 0.903989f, 0.893224f, 0.881921f, 0.870087f, 0.857729f, 0.844854f,
30
0.83147f, 0.817585f, 0.803208f, 0.788346f, 0.77301f, 0.757209f, 0.740951f, 0.724247f,
31
0.707107f, 0.689541f, 0.671559f, 0.653173f, 0.634393f, 0.615232f, 0.595699f, 0.575808f,
32
0.55557f, 0.534998f, 0.514103f, 0.492898f, 0.471397f, 0.449611f, 0.427555f, 0.405241f,
33
0.382683f, 0.359895f, 0.33689f, 0.313682f, 0.290285f, 0.266713f, 0.24298f, 0.219101f,
34
0.19509f, 0.170962f, 0.14673f, 0.122411f, 0.0980171f, 0.0735645f, 0.0490676f, 0.0245412f,
35
0.0f, -0.0245413f, -0.0490677f, -0.0735646f, -0.0980172f, -0.122411f, -0.146731f, -0.170962f,
36
-0.19509f, -0.219101f, -0.24298f, -0.266713f, -0.290285f, -0.313682f, -0.33689f, -0.359895f,
37
-0.382683f, -0.405241f, -0.427555f, -0.449611f, -0.471397f, -0.492898f, -0.514103f, -0.534998f,
38
-0.55557f, -0.575808f, -0.595699f, -0.615232f, -0.634393f, -0.653173f, -0.671559f, -0.689541f,
39
-0.707107f, -0.724247f, -0.740951f, -0.757209f, -0.77301f, -0.788346f, -0.803208f, -0.817585f,
40
-0.83147f, -0.844854f, -0.857729f, -0.870087f, -0.881921f, -0.893224f, -0.903989f, -0.91421f,
41
-0.92388f, -0.932993f, -0.941544f, -0.949528f, -0.95694f, -0.963776f, -0.970031f, -0.975702f,
42
-0.980785f, -0.985278f, -0.989177f, -0.99248f, -0.995185f, -0.99729f, -0.998795f, -0.999699f,
43
-1.0f, -0.999699f, -0.998795f, -0.99729f, -0.995185f, -0.99248f, -0.989177f, -0.985278f,
44
-0.980785f, -0.975702f, -0.970031f, -0.963776f, -0.95694f, -0.949528f, -0.941544f, -0.932993f,
45
-0.92388f, -0.91421f, -0.903989f, -0.893224f, -0.881921f, -0.870087f, -0.857729f, -0.844854f,
46
-0.83147f, -0.817585f, -0.803207f, -0.788346f, -0.77301f, -0.757209f, -0.740951f, -0.724247f,
47
-0.707107f, -0.68954f, -0.671559f, -0.653173f, -0.634393f, -0.615232f, -0.595699f, -0.575808f,
48
-0.55557f, -0.534998f, -0.514103f, -0.492898f, -0.471397f, -0.449611f, -0.427555f, -0.405241f,
49
-0.382683f, -0.359895f, -0.33689f, -0.313682f, -0.290285f, -0.266713f, -0.24298f, -0.219101f,
50
-0.19509f, -0.170962f, -0.14673f, -0.122411f, -0.098017f, -0.0735644f, -0.0490675f, -0.0245411f,
51
0.0f, 0.0245414f, 0.0490678f, 0.0735647f, 0.0980173f, 0.122411f, 0.146731f, 0.170962f,
52
0.19509f, 0.219101f, 0.24298f, 0.266713f, 0.290285f, 0.313682f, 0.33689f, 0.359895f,
53
0.382684f, 0.405241f, 0.427555f, 0.449611f, 0.471397f, 0.492898f, 0.514103f, 0.534998f,
54
0.55557f, 0.575808f, 0.595699f, 0.615232f, 0.634393f, 0.653173f, 0.671559f, 0.689541f,
55
0.707107f, 0.724247f, 0.740951f, 0.757209f, 0.773011f, 0.788347f, 0.803208f, 0.817585f,
56
0.83147f, 0.844854f, 0.857729f, 0.870087f, 0.881921f, 0.893224f, 0.903989f, 0.91421f,
57
0.92388f, 0.932993f, 0.941544f, 0.949528f, 0.95694f, 0.963776f, 0.970031f, 0.975702f,
58
0.980785f, 0.985278f, 0.989177f, 0.99248f, 0.995185f, 0.99729f, 0.998795f, 0.999699f
62
static float rs_cosine_fraction_table[256] = {
63
-0.000301181f, -0.000903363f, -0.001505f, -0.00210573f, -0.00270519f, -0.00330302f, -0.00389887f, -0.00449236f,
64
-0.00508315f, -0.00567088f, -0.00625519f, -0.00683573f, -0.00741216f, -0.00798412f, -0.00855127f, -0.00911327f,
65
-0.00966978f, -0.0102205f, -0.010765f, -0.011303f, -0.0118343f, -0.0123584f, -0.012875f, -0.013384f,
66
-0.0138848f, -0.0143773f, -0.0148611f, -0.015336f, -0.0158016f, -0.0162577f, -0.016704f, -0.0171403f,
67
-0.0175662f, -0.0179816f, -0.0183861f, -0.0187796f, -0.0191617f, -0.0195323f, -0.0198911f, -0.020238f,
68
-0.0205726f, -0.0208949f, -0.0212046f, -0.0215015f, -0.0217854f, -0.0220562f, -0.0223138f, -0.0225579f,
69
-0.0227884f, -0.0230052f, -0.0232081f, -0.0233971f, -0.0235719f, -0.0237326f, -0.0238789f, -0.0240109f,
70
-0.0241284f, -0.0242314f, -0.0243198f, -0.0243935f, -0.0244526f, -0.0244969f, -0.0245264f, -0.0245412f,
71
-0.0245412f, -0.0245264f, -0.0244969f, -0.0244526f, -0.0243935f, -0.0243198f, -0.0242314f, -0.0241284f,
72
-0.0240109f, -0.0238789f, -0.0237326f, -0.0235719f, -0.0233971f, -0.0232081f, -0.0230052f, -0.0227884f,
73
-0.0225579f, -0.0223138f, -0.0220562f, -0.0217854f, -0.0215015f, -0.0212046f, -0.0208949f, -0.0205726f,
74
-0.020238f, -0.0198911f, -0.0195323f, -0.0191617f, -0.0187796f, -0.0183861f, -0.0179816f, -0.0175662f,
75
-0.0171403f, -0.016704f, -0.0162577f, -0.0158016f, -0.015336f, -0.0148611f, -0.0143773f, -0.0138848f,
76
-0.013384f, -0.012875f, -0.0123584f, -0.0118343f, -0.011303f, -0.010765f, -0.0102205f, -0.00966978f,
77
-0.00911327f, -0.00855127f, -0.00798411f, -0.00741215f, -0.00683573f, -0.00625519f, -0.00567087f, -0.00508315f,
78
-0.00449236f, -0.00389887f, -0.00330302f, -0.00270519f, -0.00210573f, -0.001505f, -0.00090336f, -0.000301179f,
79
0.000301183f, 0.000903365f, 0.001505f, 0.00210573f, 0.00270519f, 0.00330303f, 0.00389887f, 0.00449236f,
80
0.00508315f, 0.00567088f, 0.00625519f, 0.00683573f, 0.00741216f, 0.00798412f, 0.00855127f, 0.00911327f,
81
0.00966978f, 0.0102205f, 0.010765f, 0.011303f, 0.0118343f, 0.0123584f, 0.012875f, 0.013384f,
82
0.0138848f, 0.0143773f, 0.0148611f, 0.015336f, 0.0158016f, 0.0162577f, 0.016704f, 0.0171403f,
83
0.0175662f, 0.0179816f, 0.0183861f, 0.0187796f, 0.0191617f, 0.0195323f, 0.0198911f, 0.020238f,
84
0.0205726f, 0.0208949f, 0.0212046f, 0.0215015f, 0.0217854f, 0.0220562f, 0.0223138f, 0.0225579f,
85
0.0227884f, 0.0230052f, 0.0232081f, 0.0233971f, 0.0235719f, 0.0237326f, 0.0238789f, 0.0240109f,
86
0.0241284f, 0.0242314f, 0.0243198f, 0.0243935f, 0.0244526f, 0.0244969f, 0.0245264f, 0.0245412f,
87
0.0245412f, 0.0245264f, 0.0244969f, 0.0244526f, 0.0243935f, 0.0243198f, 0.0242314f, 0.0241284f,
88
0.0240109f, 0.0238789f, 0.0237326f, 0.0235719f, 0.0233971f, 0.0232081f, 0.0230052f, 0.0227884f,
89
0.0225579f, 0.0223138f, 0.0220562f, 0.0217854f, 0.0215015f, 0.0212046f, 0.0208949f, 0.0205726f,
90
0.020238f, 0.0198911f, 0.0195323f, 0.0191617f, 0.0187796f, 0.0183861f, 0.0179816f, 0.0175662f,
91
0.0171403f, 0.016704f, 0.0162577f, 0.0158016f, 0.015336f, 0.0148611f, 0.0143773f, 0.0138848f,
92
0.013384f, 0.012875f, 0.0123584f, 0.0118343f, 0.011303f, 0.010765f, 0.0102205f, 0.00966977f,
93
0.00911326f, 0.00855126f, 0.00798411f, 0.00741215f, 0.00683573f, 0.00625518f, 0.00567087f, 0.00508315f,
94
0.00449236f, 0.00389886f, 0.00330302f, 0.00270519f, 0.00210573f, 0.001505f, 0.000903358f, 0.000301177f
98
static float rs_sine_table[256] = {
99
0.0f, 0.0245412f, 0.0490677f, 0.0735646f, 0.0980171f, 0.122411f, 0.14673f, 0.170962f,
100
0.19509f, 0.219101f, 0.24298f, 0.266713f, 0.290285f, 0.313682f, 0.33689f, 0.359895f,
101
0.382683f, 0.405241f, 0.427555f, 0.449611f, 0.471397f, 0.492898f, 0.514103f, 0.534998f,
102
0.55557f, 0.575808f, 0.595699f, 0.615232f, 0.634393f, 0.653173f, 0.671559f, 0.689541f,
103
0.707107f, 0.724247f, 0.740951f, 0.757209f, 0.77301f, 0.788346f, 0.803208f, 0.817585f,
104
0.83147f, 0.844854f, 0.857729f, 0.870087f, 0.881921f, 0.893224f, 0.903989f, 0.91421f,
105
0.92388f, 0.932993f, 0.941544f, 0.949528f, 0.95694f, 0.963776f, 0.970031f, 0.975702f,
106
0.980785f, 0.985278f, 0.989177f, 0.99248f, 0.995185f, 0.99729f, 0.998795f, 0.999699f,
107
1.0f, 0.999699f, 0.998795f, 0.99729f, 0.995185f, 0.99248f, 0.989177f, 0.985278f,
108
0.980785f, 0.975702f, 0.970031f, 0.963776f, 0.95694f, 0.949528f, 0.941544f, 0.932993f,
109
0.92388f, 0.91421f, 0.903989f, 0.893224f, 0.881921f, 0.870087f, 0.857729f, 0.844854f,
110
0.83147f, 0.817585f, 0.803208f, 0.788346f, 0.77301f, 0.757209f, 0.740951f, 0.724247f,
111
0.707107f, 0.689541f, 0.671559f, 0.653173f, 0.634393f, 0.615232f, 0.595699f, 0.575808f,
112
0.55557f, 0.534998f, 0.514103f, 0.492898f, 0.471397f, 0.449611f, 0.427555f, 0.405241f,
113
0.382683f, 0.359895f, 0.33689f, 0.313682f, 0.290285f, 0.266713f, 0.24298f, 0.219101f,
114
0.19509f, 0.170962f, 0.14673f, 0.122411f, 0.0980171f, 0.0735645f, 0.0490676f, 0.0245411f,
115
-0.0f, -0.0245413f, -0.0490678f, -0.0735647f, -0.0980172f, -0.122411f, -0.146731f, -0.170962f,
116
-0.19509f, -0.219101f, -0.24298f, -0.266713f, -0.290285f, -0.313682f, -0.33689f, -0.359895f,
117
-0.382684f, -0.405241f, -0.427555f, -0.449611f, -0.471397f, -0.492898f, -0.514103f, -0.534998f,
118
-0.55557f, -0.575808f, -0.595699f, -0.615232f, -0.634393f, -0.653173f, -0.671559f, -0.689541f,
119
-0.707107f, -0.724247f, -0.740951f, -0.757209f, -0.773011f, -0.788346f, -0.803208f, -0.817585f,
120
-0.83147f, -0.844854f, -0.857729f, -0.870087f, -0.881921f, -0.893224f, -0.903989f, -0.91421f,
121
-0.92388f, -0.932993f, -0.941544f, -0.949528f, -0.95694f, -0.963776f, -0.970031f, -0.975702f,
122
-0.980785f, -0.985278f, -0.989177f, -0.99248f, -0.995185f, -0.99729f, -0.998795f, -0.999699f,
123
-1.0f, -0.999699f, -0.998795f, -0.99729f, -0.995185f, -0.99248f, -0.989177f, -0.985278f,
124
-0.980785f, -0.975702f, -0.970031f, -0.963776f, -0.95694f, -0.949528f, -0.941544f, -0.932993f,
125
-0.92388f, -0.91421f, -0.903989f, -0.893224f, -0.881921f, -0.870087f, -0.857729f, -0.844853f,
126
-0.83147f, -0.817585f, -0.803207f, -0.788346f, -0.77301f, -0.757209f, -0.740951f, -0.724247f,
127
-0.707107f, -0.68954f, -0.671559f, -0.653173f, -0.634393f, -0.615231f, -0.595699f, -0.575808f,
128
-0.55557f, -0.534997f, -0.514103f, -0.492898f, -0.471397f, -0.449611f, -0.427555f, -0.405241f,
129
-0.382683f, -0.359895f, -0.33689f, -0.313682f, -0.290285f, -0.266713f, -0.24298f, -0.219101f,
130
-0.19509f, -0.170962f, -0.14673f, -0.122411f, -0.098017f, -0.0735644f, -0.0490675f, -0.0245411f
134
static float rs_sine_fraction_table[256] = {
135
0.0245412f, 0.0245264f, 0.0244969f, 0.0244526f, 0.0243935f, 0.0243198f, 0.0242314f, 0.0241284f,
136
0.0240109f, 0.0238789f, 0.0237326f, 0.0235719f, 0.0233971f, 0.0232081f, 0.0230052f, 0.0227884f,
137
0.0225579f, 0.0223138f, 0.0220562f, 0.0217854f, 0.0215015f, 0.0212046f, 0.0208949f, 0.0205726f,
138
0.020238f, 0.0198911f, 0.0195323f, 0.0191617f, 0.0187796f, 0.0183861f, 0.0179816f, 0.0175662f,
139
0.0171403f, 0.016704f, 0.0162577f, 0.0158016f, 0.015336f, 0.0148611f, 0.0143773f, 0.0138848f,
140
0.013384f, 0.012875f, 0.0123584f, 0.0118343f, 0.011303f, 0.010765f, 0.0102205f, 0.00966978f,
141
0.00911327f, 0.00855127f, 0.00798411f, 0.00741215f, 0.00683573f, 0.00625519f, 0.00567088f, 0.00508315f,
142
0.00449236f, 0.00389887f, 0.00330302f, 0.00270519f, 0.00210573f, 0.001505f, 0.000903361f, 0.00030118f,
143
-0.000301182f, -0.000903364f, -0.001505f, -0.00210573f, -0.00270519f, -0.00330303f, -0.00389887f, -0.00449236f,
144
-0.00508315f, -0.00567088f, -0.00625519f, -0.00683573f, -0.00741216f, -0.00798412f, -0.00855127f, -0.00911327f,
145
-0.00966978f, -0.0102205f, -0.010765f, -0.011303f, -0.0118343f, -0.0123584f, -0.012875f, -0.013384f,
146
-0.0138848f, -0.0143773f, -0.0148611f, -0.015336f, -0.0158016f, -0.0162577f, -0.016704f, -0.0171403f,
147
-0.0175662f, -0.0179816f, -0.0183861f, -0.0187796f, -0.0191617f, -0.0195323f, -0.0198911f, -0.020238f,
148
-0.0205726f, -0.0208949f, -0.0212046f, -0.0215015f, -0.0217854f, -0.0220562f, -0.0223138f, -0.0225579f,
149
-0.0227884f, -0.0230052f, -0.0232081f, -0.0233971f, -0.0235719f, -0.0237326f, -0.0238789f, -0.0240109f,
150
-0.0241284f, -0.0242314f, -0.0243198f, -0.0243935f, -0.0244526f, -0.0244969f, -0.0245264f, -0.0245412f,
151
-0.0245412f, -0.0245264f, -0.0244969f, -0.0244526f, -0.0243935f, -0.0243198f, -0.0242314f, -0.0241284f,
152
-0.0240109f, -0.0238789f, -0.0237326f, -0.0235719f, -0.0233971f, -0.0232081f, -0.0230052f, -0.0227884f,
153
-0.0225579f, -0.0223138f, -0.0220562f, -0.0217854f, -0.0215015f, -0.0212046f, -0.0208949f, -0.0205726f,
154
-0.020238f, -0.0198911f, -0.0195323f, -0.0191617f, -0.0187796f, -0.0183861f, -0.0179816f, -0.0175662f,
155
-0.0171403f, -0.016704f, -0.0162577f, -0.0158016f, -0.015336f, -0.0148611f, -0.0143773f, -0.0138848f,
156
-0.013384f, -0.012875f, -0.0123584f, -0.0118343f, -0.011303f, -0.010765f, -0.0102205f, -0.00966977f,
157
-0.00911326f, -0.00855126f, -0.00798411f, -0.00741215f, -0.00683573f, -0.00625518f, -0.00567087f, -0.00508315f,
158
-0.00449236f, -0.00389886f, -0.00330302f, -0.00270519f, -0.00210573f, -0.001505f, -0.000903359f, -0.000301178f,
159
0.000301185f, 0.000903366f, 0.001505f, 0.00210573f, 0.0027052f, 0.00330303f, 0.00389887f, 0.00449237f,
160
0.00508315f, 0.00567088f, 0.00625519f, 0.00683573f, 0.00741216f, 0.00798412f, 0.00855127f, 0.00911327f,
161
0.00966978f, 0.0102205f, 0.010765f, 0.011303f, 0.0118343f, 0.0123584f, 0.012875f, 0.013384f,
162
0.0138848f, 0.0143773f, 0.0148611f, 0.015336f, 0.0158016f, 0.0162577f, 0.016704f, 0.0171403f,
163
0.0175662f, 0.0179816f, 0.0183861f, 0.0187796f, 0.0191617f, 0.0195323f, 0.0198911f, 0.020238f,
164
0.0205726f, 0.0208949f, 0.0212046f, 0.0215015f, 0.0217854f, 0.0220562f, 0.0223138f, 0.0225579f,
165
0.0227884f, 0.0230052f, 0.0232081f, 0.0233971f, 0.0235719f, 0.0237326f, 0.0238789f, 0.0240109f,
166
0.0241284f, 0.0242314f, 0.0243198f, 0.0243935f, 0.0244526f, 0.0244969f, 0.0245264f, 0.0245412f
170
// union to help with fast typecasting from int to float
177
// Fast trig functions
178
// They're not as precise as cosf and sinf, but they're stupid fast.
179
inline float rsCosf(float value){
181
// Remap value from {0,2pi} to {0,65536} and add fast typecast bias
182
bof.f = value * (65536.0f / 6.28318530718f) + 12582912.0f;
183
// Use low-order byte for fractional multiplier
184
const float fraction(float(bof.c[0]) * 0.00390625f);
185
// Use high-order byte for table lookup
186
const unsigned char i(bof.c[1]);
188
return rs_cosine_table[i] + fraction * rs_cosine_fraction_table[i];
192
inline float rsSinf(float value){
194
// Remap value from {0,2pi} to {0,65536} and add fast typecast bias
195
bof.f = value * (65536.0f / 6.28318530718f) + 12582912.0f;
196
// Use low-order byte for fractional multiplier
197
const float fraction(float(bof.c[0]) * 0.00390625f);
198
// Use high-order byte for table lookup
199
const unsigned char i(bof.c[1]);
201
return rs_sine_table[i] + fraction * rs_sine_fraction_table[i];
205
/*outfile.open("outfile");
206
outfile << "float rs_cosine_table[256] = {" << std::endl;
207
for(int i=0; i<256; i++){
209
outfile << std::endl << "\t";
210
outfile << cosf(RS_PIx2 * float(i) / 256.0f) << "f, ";
212
outfile << std::endl << "};" << std::endl;
213
outfile << "float rs_cosine_fraction_table[256] = {" << std::endl;
214
for(int i=0; i<256; i++){
216
outfile << std::endl << "\t";
217
outfile << cosf(RS_PIx2 * float(i+1) / 256.0f) - cosf(RS_PIx2 * float(i) / 256.0f) << "f, ";
219
outfile << std::endl << "};" << std::endl;
220
outfile << "float rs_sine_table[256] = {" << std::endl;
221
for(int i=0; i<256; i++){
223
outfile << std::endl << "\t";
224
outfile << sinf(RS_PIx2 * float(i) / 256.0f) << "f, ";
226
outfile << std::endl << "};" << std::endl;
227
outfile << "float rs_sine_fraction_table[256] = {" << std::endl;
228
for(int i=0; i<256; i++){
230
outfile << std::endl << "\t";
231
outfile << sinf(RS_PIx2 * float(i+1) / 256.0f) - sinf(RS_PIx2 * float(i) / 256.0f) << "f, ";
233
outfile << std::endl << "};" << std::endl;*/