[Scummvm-git-logs] scummvm master -> 51c11efbbc5a85853fe8bdedd2012f7080b893d6

sev- sev at scummvm.org
Sat Aug 25 23:51:43 CEST 2018


This automated email contains information about 1 new commit which have been
pushed to the 'scummvm' repo located at https://github.com/scummvm/scummvm .

Summary:
51c11efbbc COMMON: Cos/Sin Table switch internal structure so at() is faster


Commit: 51c11efbbc5a85853fe8bdedd2012f7080b893d6
    https://github.com/scummvm/scummvm/commit/51c11efbbc5a85853fe8bdedd2012f7080b893d6
Author: David Fioramonti (dafioram at gmail.com)
Date: 2018-08-25T23:51:40+02:00

Commit Message:
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.

Changed paths:
    common/cosinetables.cpp
    common/cosinetables.h
    common/sinetables.cpp
    common/sinetables.h


diff --git a/common/cosinetables.cpp b/common/cosinetables.cpp
index 589c7c7..92b1723 100644
--- a/common/cosinetables.cpp
+++ b/common/cosinetables.cpp
@@ -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;
 }
 
diff --git a/common/cosinetables.h b/common/cosinetables.h
index ac0aef8..65cfc2e 100644
--- a/common/cosinetables.h
+++ b/common/cosinetables.h
@@ -46,15 +46,23 @@ public:
 	 * - Entries (excluding) nPoints/4 up to nPoints/2:
 	 *           (excluding) cos(3/2*pi) till (excluding) cos(2*pi)
 	 */
-	const float *getTable() { return _table; }
+	const float *getTable() { return _tableEOS; }
 
 	/**
 	 * Returns cos(2*pi * index / nPoints )
 	 * Index must be in range [0, nPoints - 1]
+	 * Faster than atLegacy
 	 */
 	float at(int index) const;
 
+	/**
+	 * Returns cos(2*pi * index / nPoints )
+	 * Index must be in range [0, nPoints - 1]
+	 */
+	float atLegacy(int index) const;
+
 private:
+	float *_tableEOS;
 	float *_table;
 	double _radResolution; // Smallest radian increment
 	int _refSize; // _nPoints / 4
diff --git a/common/sinetables.cpp b/common/sinetables.cpp
index 1dce529..4c0ee62 100644
--- a/common/sinetables.cpp
+++ b/common/sinetables.cpp
@@ -34,39 +34,48 @@ SineTable::SineTable(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] = sin(i * _radResolution);
 
 	// Table contains sin(2*pi*i/_nPoints) for 0<=i<_nPoints/4,
 	// followed by _nPoints/2<=i<3_nPoints/4
 	for (int i = 0; i < _nPoints / 4; i++)
-		_table[i] = sin(i * _radResolution);
+		_tableEOS[i] = sin(i * _radResolution);
 
 	for (int i = 0; i < _nPoints / 4; i++)
-		_table[_nPoints / 4 + i] = -_table[i];
+		_tableEOS[_nPoints / 4 + i] = -_tableEOS[i];
+}
 
+float SineTable::at(int index) const {
+	assert((index >= 0) && (index < _nPoints));
+	return _table[index];
 }
 
-float SineTable::at(int index) const { 
+float SineTable::atLegacy(int index) const { 
 	assert((index >= 0) && (index < _nPoints));
 	if (index < _refSize)
 		// [0,pi/2)
-		return _table[index];
+		return _tableEOS[index];
 	if (index == _refSize)
 		// pi/2
 		return 1.0f; // sin(pi/2) = 1.0	
 	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 -1.0f; // sin(3pi/2) = -1.0
 }
 
 SineTable::~SineTable() {
+	delete[] _tableEOS;
 	delete[] _table;
 }
 
diff --git a/common/sinetables.h b/common/sinetables.h
index 5a5f246..bd030ca 100644
--- a/common/sinetables.h
+++ b/common/sinetables.h
@@ -46,15 +46,23 @@ public:
 	 * - Entries 2_nPoints/4 up to nPoints/2:
 	 *           sin(pi) till (excluding) sin(3/2*pi)
 	 */
-	const float *getTable() { return _table; }
+	const float *getTable() { return _tableEOS; }
 
 	/**
 	 * Returns sin(2*pi * index / nPoints )
 	 * Index must be in range [0, nPoints - 1]
+	 * Faster than atLegacy
 	 */
 	float at(int index) const;
 
+	/**
+	 * Returns sin(2*pi * index / nPoints )
+	 * Index must be in range [0, nPoints - 1]
+	 */
+	float atLegacy(int index) const;	
+
 private:
+	float *_tableEOS;
 	float *_table;
 	double _radResolution; // Smallest radian increment
 	int _refSize; // _nPoints / 4





More information about the Scummvm-git-logs mailing list