Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

LinaInverse2010

Passing a function as an argument

This topic is 5405 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I''m working on creating an SSE library to do some packet processing, anyways I want to create a generic operation funciton that I can pass a source, destination, number of loops, and an SSE arithmetic function. Here is my current code:

// Function Macro

inline void SimpleArithPktVec(Vec* Dest, const Vec* Src, int n_loops, __m128 myOperator(__m128 a , __m128 b ) )
{


	// By default we overestimate the size to allow 4 Vecs at a time for all loops


  __m128* pDest = (__m128*) Dest;
  __m128* pSrc = (__m128*) Src;

	// Do the 4 vector operations

	for(int i = 0; i < n_loops; i++)
	{

		// Prefetch next batch

		_mm_prefetch( (char *)(pSrc + 4), _MM_HINT_NTA);
		_mm_prefetch( (char *)(pDest + 4), _MM_HINT_NTA);

		// Do the operation	

		pDest[0] = myOperator( pDest[0], pSrc[0]);
		pDest[1] = myOperator( pDest[1], pSrc[1]);
		pDest[2] = myOperator( pDest[2], pSrc[2]);
		pDest[3] = myOperator( pDest[3], pSrc[3]);

		pDest += 4;
		pSrc += 4;
	}
}

and here is how I''m calling it:
// Adds rhs to current PktVec

void PktVec::operator+=(const PktVec &rhs)
{

	if(rhs.n_vecs != n_vecs)
		return;

	SimpleArithPktVec(myVecs, rhs.myVecs, n_loops, _mm_add_ps);

}
When I compile in VC++ .Net I get this error: PktLib error LNK2019: unresolved external symbol __mm_add_ps referenced in function "public: void __thiscall PktVec::operator+=(class PktVec const &)" (??YPktVec@@QAEXABV0@@Z) I had it working with the sse2 right in the function, but I want a generic template so if I find a problem or optimization I don''t have to change it in many places each time. LinaInverse

Share this post


Link to post
Share on other sites
Advertisement
_mm_add_ps is an intrinsic, not a real function, so you probably can''t treat it as a function or functor.

The work-around is to write a wrapper class which has an operator() which calls _mm_add_ps on its arguments. If the compiler is sufficiently smart, it will know to inline enough that it''ll be able to use the intrindic inline with no extra function calls.

However, you had better examine the code generated, because if it does generate a function call in optimized/relase mode, that''d be terrible, and you''d have to use some other work-around, such as a macro.

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!