Re: shame on MISRA



On Apr 28, 7:55 pm, CBFalconer <cbfalco...@xxxxxxxxx> wrote:

Are you saying that in 1's complement the integer constant 0 could
have the bit pattern for +0 (all 0's) *or* the bit pattern for -0
(all 1's) and that using the unary - operator on the integer
constant 0 can yield either all 1's or all 0's -- that my
memset(&object, 0, sizeof object) is equally likely to zero all
bits as set all bits of the memory that object occupies?

No, negative zero can not be produced by a literal constant, like
'0'. Negative zero can only be the result of some expressions where
at least one argument is already negative zero, or by directly
manipulating the bits.

This is wrong. It depends on how the adders are configured. The
obvious method of subtracting by complementing and adding can
generate negative 0. The presence of a negative 0 can force such
an output. In fact, the means of avoiding negative zero is to use
a subtractor, rather than an adder, and prevent the injection of
any negative zero.

The C99 standard says in section 6.2.6.2, paragraph 3:

"If the implementation supports negative zeros, they shall be
generated only by:

- the &, |, ^, ~, <<, and >> operators with arguments that produce
such a value;

- the +, -, *, /, and % operators where one argument is a negative
zero and
the result is zero;

- compound assignment operators based on the above cases.

It is unspecified whether these cases actually generate a negative
zero or a
normal zero, and whether a negative zero becomes a normal zero when
stored
in an object."

In other words, if a standards compliant compiler generates code for
hardware
where, for instance, the adder could generate negative zero results
from
ordinary inputs, then the compiler must not generate a standard add
instruction
to translate a '+' operator. Instead, it must generate an alternative
code sequence
that is equivalent to an add, but avoids the negative zero.

(Older standards may have different wording, though, and compilers may
not
be compliant)

.



Relevant Pages

  • Re: On writing negative zero - with or without sign
    ... But we're talking about the Fortran standard here, ... What's to prevent some new generation of computer and compiler to come ... Suppose the convention was changed such that an extra bit was ... answer is *exact* zero. ...
    (comp.lang.fortran)
  • Re: Ideas for finding blank data fields in user records?
    ... trick all tested for negative zero using assembler and the SIGN ... The code itself says it was written in Fortran II. ... But if you are testing for a negative zero, there has to be a way to ... tape of the programs and several boxes of cards which turned out to be ...
    (comp.lang.fortran)
  • Re: On writing negative zero - with or without sign
    ... the representation of a positive or zero internal ... The representation of a negative ... only the burden of saying that a negative zero is both a zero ... interpretation opens the possibility of prefixing the external ...
    (comp.lang.fortran)
  • Re: Read problem
    ... The word "undefined" is never a limitation on the compiler. ... limiting the programmer are almost exactly the opposite. ... When the standard says that a variable becomes undefined, ... the compiler could certainly set the values to zero if they were ...
    (comp.lang.fortran)
  • Re: (corr) !!, what is it?
    ... It's result is always either zero or one. ... This difference has seen according to compiler or target ... The C standard makes the language rules. ... Always has exact value of zero. ...
    (comp.lang.c)