Re: is it safe to zero float array with memset?
- From: Keith Thompson <kst-u@xxxxxxx>
- Date: Fri, 22 Jul 2005 21:46:10 GMT
"Malcolm" <regniztar@xxxxxxxxxxxxxx> writes:
> <69dbb24b2db3daad932c457cccfd6@xxxxxxxxx> wrote
>>
>> I have to initialize all elements of a very big float point array to
>> zero. It seems memset(a, 0, len) is faster than a simple loop. I just
>> want to know whether it is safe to do so, since I know it's danger to
>> initialize NULL pointers this way. But how about floats?
>>
> No, but it's "safe enough". If you do need the speed increment, it is
> probably worth the small risk that someone will run your code on a system
> that doesn't use all bits zero for float 0.0.
I don't *quite* agree that it's "safe enough".
If I were going to use memset() to zero an array of floats, the first
thing I'd do is measure the performance to see if memset() really is
faster. If it isn't, there's no point in using anything other than an
explicit loop, which is known to be safe.
If I found that memset() really is faster, *and* that the increase in
performance is actually significant, I'd probably write a test
function, to be invoked once at program startup, that checks whether
0.0 really is all-bits-zero. If it isn't, it would abort the program.
If the program is eventually ported to a system were 0.0 *isn't*
all-bits-zero, it will then abort immediately rather than running
and giving mysteriously incorrect results.
If even invoking the test function once is a problem, I'd move the
test into the build procedure, so the program won't even build unless
it's first proven that 0.0 is all-bits-zero.
For that matter, as long as I'm messing with the build procedure, I
might as well have it set a macro that indicates whether memset() is
safe, and use that in the code to determine at compilation time which
method to use.
The only problem with this is that it's difficult to find a platform
on which 0.0 *isn't* all-bits-zero, so any code that assumes it isn't
may not be tested properly.
--
Keith Thompson (The_Other_Keith) kst-u@xxxxxxx <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
.
- Follow-Ups:
- Re: is it safe to zero float array with memset?
- From: Chris Croughton
- Re: is it safe to zero float array with memset?
- From: Malcolm
- Re: is it safe to zero float array with memset?
- References:
- Re: is it safe to zero float array with memset?
- From: Malcolm
- Re: is it safe to zero float array with memset?
- Prev by Date: Re: is it safe to zero float array with memset?
- Next by Date: Re: why is it so ?
- Previous by thread: Re: is it safe to zero float array with memset?
- Next by thread: Re: is it safe to zero float array with memset?
- Index(es):
Relevant Pages
|