PDA

View Full Version : Filling a buffer really fast


Loco
11-18-2004, 10:22 PM
Hello everyone,

Does anyone know how to fill a buffer really fast?
But beware, the size of each buffer element is not a byte, so memset() (at least as I know it ;) ) won't work...

Here is an example:

I have an int array of, say, 250K elements. I need to put into each element the value "2147483647" (which BTW is the max signed 64-bit integer).

My code is something like:


int the_buffer[25000];
...
void some_function(void) {
memset(the_buffer, 0x7FFFFFFF, sizeof(int) * 25000 );
}


which, of course, is completely wrong... however, I didn't notice until the program failed... Then, after debugging a while, I realized that, although memset() accepts an INTEGER as the second parameter, in fact it only uses one byte of such to fill the buffer and it does this byte by byte, which was the source of my problems...

Now I need a fast way to fill in such buffer with the value... (assembler is accepted)

Best regards,

Loco

RobSeace
11-19-2004, 01:31 PM
which BTW is the max signed 64-bit integer


I think you meant max signed 32-bit integer... Unless someone is
severely short-changing you on your 64-bit values... ;-)

If you use glibc (at least on Linux, but presumably other systems,
as well), take a look in "/usr/include/asm/string-486.h", and look
for the definition of inline function __memset_cc_by4()... I'm no
x86 assembly guru, but I THINK all you need to do is remove the
multiplication of c by 0x01010101 (which is a slick way of distributing
the lone byte to all 4 bytes of a 32-bit value), and just declare c as
an unsigned int, and it should give you what you're looking for...

Loco
11-22-2004, 02:48 PM
Rob,

Yes... my mistake ;)

It's 32-bits :oops:

I'll lcheck on the code and see what I can get of it