Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Vectorisation in o-w collisions #131

Open
1 task
Jerboa-app opened this issue Dec 15, 2023 · 2 comments
Open
1 task

Vectorisation in o-w collisions #131

Jerboa-app opened this issue Dec 15, 2023 · 2 comments

Comments

@Jerboa-app
Copy link
Collaborator

We use marching squares/ tile based collision resolution, currently via branches.

  • Can we do the same with a lookup table (function pointer valued?) for each tile code?
@Jerboa-app
Copy link
Collaborator Author

#include <vector>
#include <iostream>
#include <memory>
#include <cmath>
#include <iterator>

inline void doSomeStuff(double & y, double x, int i)
{	
	y = std::cos(x);
}

inline void doSomeOtherStuff(double & y, double x, int i)
{
	y = std::cos(x);
}

typedef void (*fptr) (double &, double, int);

void doWork(std::vector<double> & x, std::vector<double> & y)
{
        // vectorises
	for (int i = 0; i < 1024; i++)
	{
		//y[i] = std::sqrt(x[i]); nb sqrt requires -ffast-math
		doSomeStuff(y[i], x[i], i);
	}
}

void doWorkFptrs(std::vector<double> & x, std::vector<double> & y, std::vector<fptr> & fs)
{

        // missed: couldn't vectorize loop
        // missed: statement clobbers memory: _2 (_8, _3, i_22);
        //  missed: statement clobbers memory: _2 (_8, _3, i_22);
	for (int i = 0; i < 1024; i++)
	{
		fs[i](y[i], x[i], i);
	}
}


int main()
{
	std::vector<fptr> work;
	std::vector<double> x;
	std::vector<double> y(0.0, 1024);
	for (unsigned i = 0; i < 1024; i++)
	{
		if (std::cos(i) > 0.0)
		{
			x.push_back(3.14159*std::sqrt(i));
		}
		else
		{
			x.push_back(std::sqrt(i));
		}
		i % 2 == 0 ? work.push_back(&doSomeStuff) : work.push_back(&doSomeOtherStuff);
	}

	doWork(x, y);

	for (unsigned i = 0; i < 1024; i++)
	{
		std::cout << y[i] << "\n";
	}

	doWorkFptrs(x, y, work);

	for (unsigned i = 0; i < 1024; i++)
	{
		std::cout << y[i] << "\n";
	}

}

@Jerboa-app
Copy link
Collaborator Author

  • function called must be inlineable for vectorisation
  • so far function pointers appear to be an issue

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant