COMMON: Cos/Sin Table switch internal structure so at() is faster
A new internal table has been added so that no if checks need to be performed for the at() lookup. The old table can still be accessed using getTable or atLegacy(). at() and atLegacy() return the same values, but at() is faster.
This commit is contained in:
parent
cf99bb0a5e
commit
51c11efbbc
4 changed files with 55 additions and 20 deletions
|
@ -34,35 +34,45 @@ CosineTable::CosineTable(int nPoints) {
|
|||
_nPoints = nPoints;
|
||||
_radResolution = 2.0 * M_PI / _nPoints;
|
||||
_refSize = _nPoints / 4;
|
||||
_table = new float[_nPoints / 2];
|
||||
_tableEOS = new float[_nPoints / 2];
|
||||
_table = new float[_nPoints];
|
||||
|
||||
for (int i = 0; i < _nPoints; i++)
|
||||
_table[i] = cos(i * _radResolution);
|
||||
|
||||
// Table contains cos(2*pi*i/_nPoints) for 0<=i<=_nPoints/4,
|
||||
// followed by 3_nPoints/4<=i<_nPoints
|
||||
for (int i = 0; i <= _nPoints / 4; i++)
|
||||
_table[i] = cos(i * _radResolution);
|
||||
_tableEOS[i] = cos(i * _radResolution);
|
||||
|
||||
for (int i = 1; i < _nPoints / 4; i++)
|
||||
_table[_nPoints / 2 - i] = _table[i];
|
||||
_tableEOS[_nPoints / 2 - i] = _tableEOS[i];
|
||||
}
|
||||
|
||||
float CosineTable::at(int index) const {
|
||||
assert((index >= 0) && (index < _nPoints));
|
||||
return _table[index];
|
||||
}
|
||||
|
||||
float CosineTable::atLegacy(int index) const {
|
||||
assert((index >= 0) && (index < _nPoints));
|
||||
if (index < _refSize)
|
||||
// [0,pi/2)
|
||||
return _table[index];
|
||||
return _tableEOS[index];
|
||||
if ((index > _refSize) && (index < 2 * _refSize))
|
||||
// (pi/2,pi)
|
||||
return -_table[2 * _refSize - index];
|
||||
return -_tableEOS[2 * _refSize - index];
|
||||
if ((index >= 2 * _refSize) && (index < 3 * _refSize))
|
||||
// [pi,3/2pi)
|
||||
return -_table[index - 2 * _refSize];
|
||||
return -_tableEOS[index - 2 * _refSize];
|
||||
if ((index > 3 * _refSize) && (index < _nPoints))
|
||||
// (3/2pi,2pi)
|
||||
return _table[_nPoints - index];
|
||||
return _tableEOS[_nPoints - index];
|
||||
return 0.0f; // cos(pi/2) and cos(3pi/2) = 0
|
||||
}
|
||||
|
||||
CosineTable::~CosineTable() {
|
||||
delete[] _tableEOS;
|
||||
delete[] _table;
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue