memset with 32 bit values
-
Hi All! If I have, for example, the bits of a DIBSection and I want to set it to a given (32 bit) color, I could say: (1) for (int i=0; i<nPixels; i++) pbits[i] = mycolor; or: (2) __int32 *p=pbits; while (p<pbits+nPixels) *p++ = mycolor; but if I wanted to set it to black (or a similar color where the components are all the same) then I could just say: (3) memset(pbits, 0, nPixels*4); So I guess I'm wondering if there's a way to replace (1) and (2) by a built-in function that I don't know about but which looks something like: memset_32_at_a_time(pbits, mycolor, nPixels); (and which would presumably be faster than either of the first two methods) Thanks for any help with this!
-
Hi All! If I have, for example, the bits of a DIBSection and I want to set it to a given (32 bit) color, I could say: (1) for (int i=0; i<nPixels; i++) pbits[i] = mycolor; or: (2) __int32 *p=pbits; while (p<pbits+nPixels) *p++ = mycolor; but if I wanted to set it to black (or a similar color where the components are all the same) then I could just say: (3) memset(pbits, 0, nPixels*4); So I guess I'm wondering if there's a way to replace (1) and (2) by a built-in function that I don't know about but which looks something like: memset_32_at_a_time(pbits, mycolor, nPixels); (and which would presumably be faster than either of the first two methods) Thanks for any help with this!
You might use std::fill or std::fill_n. There are no C runtime library functions for this.
-
Hi All! If I have, for example, the bits of a DIBSection and I want to set it to a given (32 bit) color, I could say: (1) for (int i=0; i<nPixels; i++) pbits[i] = mycolor; or: (2) __int32 *p=pbits; while (p<pbits+nPixels) *p++ = mycolor; but if I wanted to set it to black (or a similar color where the components are all the same) then I could just say: (3) memset(pbits, 0, nPixels*4); So I guess I'm wondering if there's a way to replace (1) and (2) by a built-in function that I don't know about but which looks something like: memset_32_at_a_time(pbits, mycolor, nPixels); (and which would presumably be faster than either of the first two methods) Thanks for any help with this!
Alex, memset does almost exactly what you do in your second function (unless you using an Alpha or PowerPC chip set, which calls RtlFillMemory). You could use memcpy in a loop, but I doubt it would give you any enhancements in speed. -Ben --------- On the topic of code with no error handling -- It's not poor coding, it's "optimistic" ;)
-
Hi All! If I have, for example, the bits of a DIBSection and I want to set it to a given (32 bit) color, I could say: (1) for (int i=0; i<nPixels; i++) pbits[i] = mycolor; or: (2) __int32 *p=pbits; while (p<pbits+nPixels) *p++ = mycolor; but if I wanted to set it to black (or a similar color where the components are all the same) then I could just say: (3) memset(pbits, 0, nPixels*4); So I guess I'm wondering if there's a way to replace (1) and (2) by a built-in function that I don't know about but which looks something like: memset_32_at_a_time(pbits, mycolor, nPixels); (and which would presumably be faster than either of the first two methods) Thanks for any help with this!
Thanks for the responses! Continuing on Ben's idea, I could manually set the values of the first scanline and then memcpy it to the other scanlines. However my original goal wasn't mainly speed gain but rather to make the code nicer, so I guess I'll give it a miss and just set the values manually :cool: