Sign in to follow this  
dawidjoubert

memset question

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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
Share on other sites
Guest Anonymous Poster
Quote:
Original post by Thetan
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)


Quote:
Final draft of C99
The memset function copies the value of c (converted to an unsigned char) into
each 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 this post


Link to post
Share on other sites
Quote:
Original post by Adam Hamilton
push es
push edi

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

pop edi
pop es


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

Share this post


Link to post
Share on other sites
Quote:
Original post by Adam Hamilton
You 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 this post


Link to post
Share on other sites
Quote:
Original post by Colin Jeanne
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));


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 AP
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.


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 this post


Link to post
Share on other sites
Quote:
Original post by bakery2k1
Quote:
Original post by Adam Hamilton
push es
push edi

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

pop edi
pop 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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
Share on other sites
Quote:
Original post by bakery2k1
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".


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 this post


Link to post
Share on other sites
Guest Anonymous Poster
Quote:
Original post by Adam Hamilton
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.


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

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
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 this post


Link to post
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 this post


Link to post
Share on other sites
[quote]Original post by Anonymous Poster
Quote:
Original post by Adam Hamilton
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.


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 this post


Link to post
Share on other sites
Quote:
Original post by Adam Hamilton
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.


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 this post


Link to post
Share on other sites
Guest Anonymous Poster
Quote:
Original post by Adam Hamilton
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
[/code]

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 this post


Link to post
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.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this