Re: shame on MISRA
- From: Arlet <usenet+5@xxxxxxxxxx>
- Date: 28 Apr 2007 12:41:43 -0700
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)
.
- References:
- Re: shame on MISRA
- From: Arlet
- Re: shame on MISRA
- From: Marcin Wolcendorf
- Re: shame on MISRA
- From: Marcin Wolcendorf
- Re: shame on MISRA
- From: Robert Adsett
- Re: shame on MISRA
- From: Marcin Wolcendorf
- Re: shame on MISRA
- From: CBFalconer
- Re: shame on MISRA
- From: Marcin Wolcendorf
- Re: shame on MISRA
- From: CBFalconer
- Re: shame on MISRA
- From: Dan Henry
- Re: shame on MISRA
- From: CBFalconer
- Re: shame on MISRA
- From: Dan Henry
- Re: shame on MISRA
- From: CBFalconer
- Re: shame on MISRA
- From: Dan Henry
- Re: shame on MISRA
- From: Arlet
- Re: shame on MISRA
- From: CBFalconer
- Re: shame on MISRA
- Prev by Date: Re: Selection of a USB microcontroller
- Next by Date: Re: shame on MISRA
- Previous by thread: Re: shame on MISRA
- Next by thread: Re: shame on MISRA
- Index(es):
Relevant Pages
|