arm9e: how do I invert sign of a halfword?

Multiplication and left shifting are not identical operations but they produce the same effect (in the absence of overflow). Having the same effect is allowed in a number of places in the C standard.

explicitly

Reply to
Everett M. Greene
Loading thread data ...

Nobody is saying that the equation is not mathematically correct, but as we all know, some arithmetic operations can have problems when being computed with limited range operations.

But the negate instruction does not produce the mathematically correct answer in all cases. In pure mathematics, there are no limits on the values which can take, so there's no such thing as the negative of any number not being realizable.

Reply to
Everett M. Greene

Just consider the meaning and action on machines using ones complement or sign-magnitude conventions. Also the fact that the occurence of an overflow can cause implementation defined action. That's why, for safe behaviour, you should restrict shifts to operating on unsigned quantities.

--
 [mail]: Chuck F (cbfalconer at maineline dot net) 
 [page]: 
            Try the download section.
Reply to
CBFalconer

No, but the C language does. And that language is deliberately machine independant. See the following links (the first two are versions of the standard):

Some useful references about C: (C99) (C99, txt) (C-library} (GNU docs) (C-faq)

--
 [mail]: Chuck F (cbfalconer at maineline dot net) 
 [page]: 
            Try the download section.
Reply to
CBFalconer

Apart from the ARM ARM there is the ARM EABI which very precisely defines data types, structure layout, calling standards and such.

Wilco

Reply to
Wilco Dijkstra

I meant interpreting the operations as modulo arithmetic. Compilers use the above identities to optimize code without worrying about overflow etc.

Yes negate is not mathematically correct but it is correct in modulo arithmetic. The interpretation matters. For example stdint.h defines abs as int abs(int), while it would be more correct to define it as unsigned abs(int). In the latter case the absolute value of the smallest negative number is correctly interpreted as a large positive number, eventhough the bitpattern is identical in both cases.

Wilco

Reply to
Wilco Dijkstra

u32 foo2 = 0x00000022; u32 fubar = ?????;

Reply to
MikeWhy

It's not a standard unless produced by a recognized national or international standards organization. Otherwise, it's just a formal more or less rigorous definition.

Reply to
Everett M. Greene

Whether or not a particular standard is rubber stamped by some US organization is not at all relevant. What matters is that there is a freely available standard which is correct, easy to understand and implement.

Wilco

Reply to
Wilco Dijkstra

I did consider that and this is exactly why the C standard is incorrect:

Arithmetic one-complement shifts shift the sign into either the MSB (right shift) or LSB (left shift). Sign magnitude only shifts the magnitude for both left and right shift. This gives the correct arithmetic results, ie.

-0 stays -0, -1 becomes -0 on a right shift, and -2 on a left shift etc.

So given arithmetic shifts are well defined in all 3 representations why would anyone make left shifts undefined and right shifts implementation defined? It doesn't make any sense.

No. There is no reason to restrict shifts at all as you get the same results irrespectively of which representation is used.

Wilco

Reply to
Wilco Dijkstra

... snip ...

Please give a reference section (s) in the C99 standard that imposes this.

Some useful references about C: (C-faq) (C99) (C99, txt) (C-library}

--
 [mail]: Chuck F (cbfalconer at maineline dot net) 
 [page]: 
            Try the download section.
Reply to
CBFalconer

... snip ...

ISO is the universally accepted international organization. As far as C goes, it is the definition of correctness.

--
 [mail]: Chuck F (cbfalconer at maineline dot net) 
 [page]: 
            Try the download section.
Reply to
CBFalconer

If an ISO standard is so correct why is there a system for defect reports? I agree that the term 'standard' is overly used for something that is no more than a specification. Often conformance to a standard is not sufficient to make something usable and more is needed - more that standards committees haven't been able to agree on yet.

Peter

Reply to
Peter Dickerson

?
e

I think the reason for defect reports is so that contradictions within the standard can be corrected. I agree that just because it is correct (by definition, it is the standard) does not mean it is usable. Some posters have suggested that the standard is incorrect because it does not define certain things the way they would like them defined or in what they think is a logical way.

Reply to
Rocky

The standard has nothing to do with this. Remember you asked me to "consider the meaning and action on machines using ones complement or sign-magnitude conversions". That's what I did, and I described how shifts work on such machines, and that there is clearly no reason for left shifts to be undefined when right shifts are implementation defined.

Once you understand that, you'll understand the C standard is incorrect.

Wilco

Reply to
Wilco Dijkstra

Standards are by definition works in progress, and as such are never complete nor perfect. Some standards are better than others of course. If the majority of implementers and users of a standard consistently ignore specific parts of it then it should be obvious something is wrong and that this should be corrected in a future version of the standard. Contrary to what some believe, it is quite feasible to ignore/violate parts of a standard and still get most of the benefits.

Wilco

Reply to
Wilco Dijkstra

If you go back to the original post in this thread, you will find C code proposed. In addition, something like 90% of embedded programming is done in C. If you want to consider other languages, it is necessary to state that in an obvious place.

The C standard allows for the use of non-standard systems and extensions. However, any sane programmer makes sure such items are clearly marked and identified. Your statement about the standard is wrong, to put it gently.

--
 [mail]: Chuck F (cbfalconer at maineline dot net) 
 [page]: 
            Try the download section.
Reply to
CBFalconer

... snip ...

Conceded. That is why extensions are allowed, and why there is a distinction between 'undefined' and 'implementation defined'. The C standard was originally intended to define existing practice, and to define characteristics that allowed any C program to compile and function on any conforming C system. Since systems differ, that obviously cannot cover every possibility.

The point is that you should never need to worry about the machines reaction to code that meets the specifications of the C standard. Other sections - worry. It is no sweat to ensure that 90% (or more) of your source meets that standard.

--
 [mail]: Chuck F (cbfalconer at maineline dot net) 
 [page]: 
            Try the download section.
Reply to
CBFalconer

Nobody is talking about other languages - we're talking about C and how C translates onto machine instructions. If the underlying machine instructions work correctly in different number representations then the C operators that map onto them work correctly too.

You still haven't answered my question as to why left shifts are undefined while right shifts are implementation defined. If you believe the standard is correct, then explain why. "Because the standard says so" is not good enough, especially since most software disagrees.

Wilco

Reply to
Wilco Dijkstra

It's like how God exists because the bible tells me so. And since the bible is the word of God, so it must be true.

--

John Devereux
Reply to
John Devereux

ElectronDepot website is not affiliated with any of the manufacturers or service providers discussed here. All logos and trade names are the property of their respective owners.