0

I'm developing an application that should use very few resources and be very fast. And in my app I use an unsigned char* rawData which contains bytes got from image. So in this rawData array I have to keep some bytes and others set to zero. But I'm not permitted to use any loop(otherwise I can just run through each byte and set them zero).

So here are questions.

Q1) Is there any method in Objective C like ZeroMemory in C

Q2) Is there any other ways to set nessecary bytes to zero without using any loop.

Thanks In Advance...

P.S. Can provide some code if nessecary...

Garnik
  • 423
  • 1
  • 6
  • 20

1 Answers1

2

If you don't know the size of the buffer, you can't do it without a loop. Even if you don't write the loop yourself, calling something like strlen will result in a loop. I'm counting recursion as a loop here too.

How do you know which bytes to keep and which to set to zero? If these bytes are in known positions, you can use vector operations to zero out some of the bytes and not others. The following example zeros out only the even bytes over the first 64 bytes of rawData:

__m128i zeros = _mm_setzero_si128();
uint8_t mask[] = {8, 0, 8, 0, 8, 0, 8, 0, 8, 0, 8, 0, 8, 0, 8, 0};
__m128i sse_mask = _mm_load_si128(mask);
_mm_maskmoveu_si128(zeros, sse_mask, &rawData[0]);
_mm_maskmoveu_si128(zeros, sse_mask, &rawData[16]);
_mm_maskmoveu_si128(zeros, sse_mask, &rawData[32]);
_mm_maskmoveu_si128(zeros, sse_mask, &rawData[48]);

If the high bit of each byte in mask is 1, the corresponding value in zeros will be copied to rawData. You can use a sequence of these masked copies to quickly replace some bytes and not others. The resulting machine code uses SSE operations, so this is actually quite fast. It's not required, but SSE operations will run much faster if rawData is 16-byte aligned.

Sorry if you're targeting ARM. I believe the NEON intrinsics are similar, but not identical.

ccurtsinger
  • 208
  • 1
  • 4
  • Hmmm... First thing I don't know why, maybe it should be that way, but size of my `rawData`(I initialize it like this `unsigned char *rawData = (unsigned char*) calloc(gridSize*gridSize, sizeof(unsigned char));`)is always 4, I think it returns 4byte. Second thing I can have size of my buffer(rawData) without any strlen. And third problem,, sorry but I didn't understand your code. :( – Garnik Apr 27 '12 at 14:30