This project is read-only.

How to call _popc

Dec 12, 2012 at 11:48 PM

How does one call _popc from CUDAfy?

Dec 13, 2012 at 3:09 PM
Edited Dec 13, 2012 at 3:18 PM

Wil, if you're willing to download and compile the latest version of cudafy, you'll have a solution for your problem.

GThread has a new static method, called GThread.InsertCode. You can use it to emit a __popc instruction. Example:

int a = 10;

int b = 0; // need to initialize it with something.

GThread.InsertCode("{0} = __popc({1})", b, a); // will generate the CUDA instruction "b = __popc(a);"




P.S. Nick, this wasn't me, I swear!


Dec 13, 2012 at 5:37 PM

Ha, ye olde trolls are lurking around ;-)

Actually funny how these things work out. I'll be reviewing the code you sent and most like will add shortly. There are indeed a bunch of useful functions that should be in there.

Dec 14, 2012 at 2:14 AM

Thanks a lot Pedritolo1!  I'll give it a shot.  I had worked around it by using [CudafyDummy] but this is much better.



Dec 14, 2012 at 3:02 AM

It worked!  Thanks again!

I had a bit of a problem because of the name mangling so I had to do the entire method calling InsertCode.  Don't know if there's an easier way.

        public static int PopulationCount(ulong value)
            GThread.InsertCode("int* LValue = (int*)&value;");
            GThread.InsertCode("return __popc(LValue[0]) + __popc(LValue[1]);");
            return 0;
__device__ int PopulationCount(unsigned long long value)
 int* LValue = (int*)&value;
 return __popc(LValue[0]) + __popc(LValue[1]);
 return 0;
Dec 14, 2012 at 12:36 PM

Wil, why not use __popcll instead?

GThread.InsertCode("return __popcll({0});", value);


Dec 14, 2012 at 12:40 PM

Thanks pedritolo1!  I didn't know about __popcll!

The params version of InsertCode won't work because it can't take a pointer :(



Dec 14, 2012 at 12:43 PM

Never mind, with __popcll I don't need the extra variable so the params version works then.

Thanks again