Re: is it safe to zero float array with memset?



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



Relevant Pages

  • Re: is it safe to zero float array with memset?
    ... If it's an array of float yes, of it's an array of float* ... > you're setting all ptrs to NULL, safe anyway. ... NULL need not be all-bits zero, ...
    (comp.lang.c)
  • Re: is it safe to zero float array with memset?
    ... > initialize NULL pointers this way. ... No, but it's "safe enough". ... that doesn't use all bits zero for float 0.0. ...
    (comp.lang.c)
  • Re: Interesting math
    ... precision when a number is zero. ... A C Float Number must have six or more significant digits ... and read about C Float Numbers and programming in C language. ...
    (alt.usage.english)
  • Re: is it safe to zero float array with memset?
    ... >> Not if you then try to use the value as a float or as a pointer. ... >> load it into an address register (no dereference needed). ... If it's a matter of the implementation itself, *IT'S NOT SAFE*. ...
    (comp.lang.c)
  • fpu code optimisation request
    ... Consider the following pseudo-code function: ... function myAND(float a, float b) ... Or, in "normal" language, zero stays zero, ...
    (comp.lang.asm.x86)