fast check for binary zeroes in memory

V

vrigo

hi folks,

is there a fast way to check for large regions of binary zeroes in
memory? the solution could be either in C or in assemble language? i
am interested in any RISC platforms.

TIA,

vrigo
 
A

Alex Fraser

vrigo said:
is there a fast way to check for large regions of binary zeroes in
memory?

The only method available in standard C is to do it a byte at a time, with a
pointer to unsigned char.

Platform-specific methods may perform better. After ensuring
platform-specific alignment, you can use a platform-specific larger data
type that has no padding bits.
the solution could be either in C or in assemble language? i am
interested in any RISC platforms.

If the platform is relevant to your question, then this is the wrong place
to ask: find a group for that platform.

If you are asking how to get the best performance for some operation, then
this is the wrong place to ask: it's either an algorithm question (find a
group relating to the problem domain, or try comp.programming), or the
platform is relevant and the previous case applies.

Alex
 
E

Emmanuel Delahaye

In said:
is there a fast way to check for large regions of binary zeroes in
memory? the solution could be either in C or in assemble language? i
am interested in any RISC platforms.

memcmp();

a loop.
 
D

Dan Pop

In said:
is there a fast way to check for large regions of binary zeroes in
memory? the solution could be either in C or in assemble language? i
am interested in any RISC platforms.

The fastest solution can be obtained in assembly, but this is not
something we can discuss here, and it's higly processor specific, anyway.

At C level, it's easier if the region is aligned for long access, but you
can do the alignment yourself, by converting the pointer to an unsigned
long, aligning the integer value and then converting it back to a
pointer. Do the testing accessing as much as possible of the memory
block as an array of unsigned long and test the first and the last
few bytes as unsigned char, if necessary. As unsigned long has the same
size as the processor word on any decent implementation for RISC
platforms, this is the best you can do, in C code. If the chances that
the memory block is zeroed are high, you may try OR-ing all the words
together in a loop and only testing the final result.

Ignore any objections other people might raise about padding bits:
they have yet to be seen on any RISC platform. And the unsigned integer
types are immune from any dual representation of the zero value (in the
absence of padding bits).

Dan
 
E

Eric Sosman

Dan said:
To be of any use, you already need to have an equally large memory block
that is already zeroed.

You snipped the words that followed "memcmp();",
which were "a loop." Begin by establishing that the
first N bytes are all zero -- use a smallish known-zero
block, or start with N=1 and a simple `=='. Then use
memcmp() to compare the first N bytes to the second N,
establishing (or disproving) that the first 2N bytes
are all zero. Then compare the first 2N to the second
2N to extend to 4N, and so on. The edge conditions
take a wee bit of care, but it's not complicated.

I used a similar technique in a function that fills
a memory area with copies of a multi-byte object, rather
like memset() but without the limitation of a single-byte
value. The loop uses memcpy() instead of memcmp(), of
course, but the outline is similar. <OT>And it doesn't
thrash the cache, either, although I was half expecting
it to do so.</OT>
 
D

Dan Pop

In said:
You snipped the words that followed "memcmp();",
which were "a loop."

I took it as being another alternative solution, not as part of a single
solution.

Dan
 
V

vrigo

Thanks for all the answers :) For processor specific answers I will
also post the query to appropriate news group. Thanks again.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,540
Members
45,025
Latest member
KetoRushACVFitness

Latest Threads

Top