# memset question

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

## Recommended Posts

Does memset cpy 8bits at a time.. Is there a memset equivalent for an unsigned int (32bit) This is what i am doing memset(start,color,(x2-x1) << 2); obviously it is not working because it is copying over bytes not longwords

##### Share on other sites
void *memset(void *s, int c, size_t n);

int == 32-bits on just about all most modern systems (not sure about 64-bit systems)

##### Share on other sites
memset() will set one byte at a time. If yor datatype is more than a byte long then you can use sizeof().

int sum_ints[25];/* Set every byte in this array of ints to 0 */memset(sum_ints, 0, 25 * sizeof(int));

##### Share on other sites
Thanks but memset won't work because i am blitting a colour, that is 4 different bytes and then 4 different bytes again.

But my solution, behold:
int x = x1;
while (x < x2)
{
(*start) = color;
start++;
x++;
}

##### Share on other sites
not to nitpick but
int x;for (x = x1; x < x2; ++x){    start[x] = color;}

looks alot cleaner

in C/C++ pointer notation and array notation are pretty much interchangable most of the time, but at times its just cleaner to use array notation. Then again we also have times were using pointer notation makes things easier and cleaner, but now isnt one of thoughs times ;)

<3 Thetan

##### Share on other sites
Assuming C++:
#include <algorithm>...std::fill_n(array, size, color);

##### Share on other sites
you could try using inline assembly

You must use this with multiple of 4 bytes. There are tricks so that you can do this with any number of bytes.

This will be nice and fast.

push es
push edi

les edi, buffer
mov eax, color
mov ecx, count
rep stosd

pop edi
pop es

Or you could do this :)

You can use pointers as the counting variable in for loops.

for (int* s=&start[x1]; s<&start[x2]; s++)  *s = color;

##### Share on other sites
Quote:
 Original post by Thetanvoid *memset(void *s, int c, size_t n);int == 32-bits on just about all most modern systems (not sure about 64-bit systems)

Quote:
 Final draft of C99The memset function copies the value of c (converted to an unsigned char) intoeach of the first n characters of the object pointed to by s.

memset() copies ((unsigned char)c) into each byte of s. int may be 32 bits on almost all modern systems, but memset() uses only 8 bits of that on almost all modern systems.

Really, memset() and calloc() aren't very useful in a strictly conforming program where you can't assume "all bits 0" is a valid representation of floating point 0 and/or a null pointer. For similar reasons, the most common value passed as the second parameter to memset() is 0.

##### Share on other sites
Quote:
 Original post by Adam Hamiltonpush espush ediles edi, buffermov eax, colormov ecx, countrep stosdpop edipop es

There's no need to fiddle with es in a flat memory model (as used for Windows).

##### Share on other sites
Quote:
 Original post by Adam HamiltonYou can use pointers as the counting variable in for loops.for (int* s=&start[x1]; s<&start[x2]; s++) *s = color;
Yes, this is what I would do too. It usually saves a register or two, allowing for better optimisation as well.

##### Share on other sites
Quote:
 Original post by Colin Jeannememset() will set one byte at a time. If yor datatype is more than a byte long then you can use sizeof().int sum_ints[25];/* Set every byte in this array of ints to 0 */memset(sum_ints, 0, 25 * sizeof(int));

In fact, memset (and memcpy BTW) will set n bytes at a time (where n is 4 on 32 bit machine). memset is a rather clever routine that will write memory as fast as it can (you won't be able to beat it; Adam Hamilton's version is the core of memset, but memset first align variable and try to pair asm instructions in order to achieve the best speed). Only the lowest byte of the memset argument is used (the other bytes are discarded).

You can use std::fill or std::fill_n to copy more than one byte to a destination variable.

(edit:)

Quote:
 Original post by APReally, memset() and calloc() aren't very useful in a strictly conforming program where you can't assume "all bits 0" is a valid representation of floating point 0 and/or a null pointer. For similar reasons, the most common value passed as the second parameter to memset() is 0.

AFAIK, floating point 0 is 0 (IEEE standard), and NULL pointer is 0 too (of course, if you don't like your co-workers, you can define NULL to be 42; the NULL value is implementation defined, but is supposed to be the null pointer constant (which is defined as an integral constant expression rvalue of integer type that evaluates to zero (4.10 of the holy C++ standard))). Did I miss something?

HTH,

##### Share on other sites
Quote:
Original post by bakery2k1
Quote:
 Original post by Adam Hamiltonpush espush ediles edi, buffermov eax, colormov ecx, countrep stosdpop edipop es

There's no need to fiddle with es in a flat memory model (as used for Windows).

Sorry... but I am going to have to slam you down on this one. If you knew anything about assembler you would know that stosb, stosw, etc.. uses ES:DI or ES:EDI

##### Share on other sites
Yes, but in Windows the base of es is the same as that of ds (they're both zero). That's the meaning of a "flat memory model", as stated in my post.

If _you_ knew "anything about assembler", you'd know this. You'd also know that your "les" instruction is loading the es register with a random selector (whichever Word follows "buffer" in memory), which is likely to cause a GPF and hence crash your program.

Consider _yourself_ "slammed down".

##### Share on other sites
I am sorry... I guess I am slammed down. I thought a flat memory model still doesn't guarantee that ES and DS are the same.

##### Share on other sites
BTW. Thanks a bunch for the neg comment... Maybe I should take back that sorry... I would only neg comment someone if they were consistantly unhelpful.

##### Share on other sites
Under a flat memory model you shouldn't have to fuss with segment registers at all - except for fs with regards to exception handling - and even then it's best to let the compiler work that for you.

##### Share on other sites
Quote:
 Original post by bakery2k1If _you_ knew "anything about assembler", you'd know this. You'd also know that your "les" instruction is loading the es register with a random selector (whichever Word follows "buffer" in memory), which is likely to cause a GPF and hence crash your program.Consider _yourself_ "slammed down".

I'm sure this was an edit cause some of it wasn't there when you first posted... Seems kinda childish to go back an edit a post and add insult to injury after I had apoligised.

And no... I didn't know that les would load with a random selector - I have used this instruction before with windows with no GPF and my program worked exactly the way I wanted it to.

Once again, I am sorry for my previous statement.

Did you not expect me to apologise???

##### Share on other sites
Quote:
 Original post by Adam HamiltonI'm sure this was an edit cause some of it wasn't there when you first posted... Seems kinda childish to go back an edit a post and add insult to injury after I had apoligised.

*cough* http://www.gamedev.net/community/forums/topic.asp?topic_id=383613&whichpage=1� *cough*

##### Share on other sites
Quote:
 Original post by Anonymous Poster*cough* http://www.gamedev.net/community/forums/topic.asp?topic_id=383613&whichpage=1?? *cough*

err... um...

*cough* http://www.gamedev.net/community/forums/topic.asp?topic_id=383613&whichpage=1&#2534085 *cough*

##### Share on other sites
I get what your saying AP (I had changed a post - forgot to put EDIT:) but...

If you had continued reading through the thread, I mentioned in my next post I had edited the original post because I thought I sounded a bit mean... (I Didn't proof read before I posted)

Bakery2k1 on the other hand basically read my apology and decided to edit his post and make it sound much more nasty

Who is the childish one?

Someone who realises they have maybe sounded rash, edited their post sounding not mean and owned up to editing their post

or

Someone who posts something slamming them (which was warranted) reading an apology and then edits their post filling it with nastyness.

Come on... I think the latter is more childish.

##### Share on other sites
Original post by Anonymous Poster
Quote:
 Original post by Adam HamiltonI'm sure this was an edit cause some of it wasn't there when you first posted... Seems kinda childish to go back an edit a post and add insult to injury after I had apoligised.

Curious though AP... My post was refering to the childishness of adding insult to injury *after* an apology from me to him.

Do you think I added insult to injury???

I know what hypocrysy is but I don't think what you are refering to qualifies.... I am disappointed that you did not understand what my above quoted post meant.

##### Share on other sites
Quote:
 Original post by Adam HamiltonWho is the childish one?Someone who realises they have maybe sounded rash, edited their post sounding not mean and owned up to editing their postorSomeone who posts something slamming them (which was warranted) reading an apology and then edits their post filling it with nastyness.

or

Someone who slams someone without provocation.

or

Someone who keeps on about being rated negatively.

Quote:
 Come on... I think the latter is more childish.

This is the internet. We can't see whether your tongue is in your cheek or your foot is in your mouth. I only ever saw the "nasty" version of bakery2k1's post, which is obviously copying the form and tone of one of your own posts directed at him. Why would you expect someone to kindly point out your mistake after you less than kindly pointed out theirs? (His first post was certainly more tactful than your response.)

As for apologizing, check out this thread. My first attempt at an apology practically fell on deaf ears. Sometimes you just have to let things go and move on. (Although, it can be hard sometimes. [sad])

##### Share on other sites
This is going nowhere...

Moral of forum posting....Apologies are not welcome because no-one cares

##### Share on other sites
Quote:
 Original post by Adam Hamiltonyou could try using inline assemblyYou must use this with multiple of 4 bytes. There are tricks so that you can do this with any number of bytes.This will be nice and fast.push espush ediles edi, buffermov eax, colormov ecx, countrep stosdpop edipop es

If you don't need to support PII systems, you can use an even faster method than using the well-known REPMOVSB. The method I'm talking about uses the MOVNTQ instruction and can copy more than 500 MB/s. It'd be even easier to implement this approach since you're working with groups of four bytes. P4s even have the MOVNTI instruction.

##### Share on other sites
Quote:
 I'm sure this was an edit cause some of it wasn't there when you first posted... Seems kinda childish to go back an edit a post and add insult to injury after I had apoligised.

Quote:
 Bakery2k1 on the other hand basically read my apology and decided to edit his post and make it sound much more nasty

Quote:
 My post was refering to the childishness of adding insult to injury *after* an apology from me to him.

I didn't edit to "add insult to injury", I edited my post to add another comment - that not only do you not need to touch es, actually doing so will crash your application. Also, I made the edit _before_ you posted your apology (or at least simultaneously - I definitely made the edit before I read the apology).

Quote:
 I only ever saw the "nasty" version of bakery2k1's post, which is obviously copying the form and tone of one of your own posts directed at him.

Exactly. If your post had been "I beleive you do have to change the value in es, because..." then I would have kindly explained why that is not necessary. Instead, I read a post which baselessly accuses me of not "knowing anything about assembler", and I must say that that hit a nerve.

Your post insulted me without provocation and if you go around doing that you should expect to be rated down and to receive responses in a similar tone.