[Vm-dev] [commit] r2463 - CogVM source as per
VMMaker.oscog-eem.105. Fix signed32BitValueOf for most neg-
Igor Stasenko
siguctua at gmail.com
Thu Jul 21 22:40:45 UTC 2011
On 22 July 2011 01:24, Eliot Miranda <eliot.miranda at gmail.com> wrote:
>
>
>
> On Thu, Jul 21, 2011 at 3:17 PM, Stefan Marr <squeak at stefan-marr.de> wrote:
>>
>> Hi Eliot:
>>
>> On 21/07/11 19:35, Eliot Miranda wrote:
>>>
>>> On Thu, Jul 21, 2011 at 4:30 AM, David T. Lewis <lewis at mail.msen.com <mailto:lewis at mail.msen.com>> wrote:
>>>
>>>
>>>
>>> Hi Eliot,
>>>
>>> Can you say what the issue was with signed32BitValueOf? I can
>>> see the changes in InterpreterPrimitives>>signed32BitValueOf:
>>> but I'm not clear on whether this is something that affects
>>> Alien, or if it is something that has been causing problems
>>> more generally but went unnoticed. Also, I'd like to document
>>> this with a unit test, so if you can suggest a code snippet
>>> that would be great.
>>>
>>>
>>> The unit test is in the lien tests and is the attempt to assign max neg int (-2^31) through signedLongAt:put:. The problem is due to a pervasive C compiler bug with optimization.
>>
>> Maybe I misread this blog post: http://blog.llvm.org/2011/05/what-every-c-programmer-should-know.html (*Signed integer overflow:)*
>> but from that I would conclude that it is not a compiler bug, but undefined behavior in C instead. Thus, GCC and ICC are just doing there job in terms of what they are allowed to do by the spec.
>
> Thanks, Stefan. I hadn't known this (or had forgotten):
> Signed integer overflow: If arithmetic on an 'int' type (for example) overflows, the result is undefined. One example is that "INT_MAX+1" is not guaranteed to be INT_MIN. This behavior enables certain classes of optimizations that are important for some code. For example, knowing that INT_MAX+1 is undefined allows optimizing "X+1 > X" to "true". Knowing the multiplication "cannot" overflow (because doing so would be undefined) allows optimizing "X*2/2" to "X". While these may seem trivial, these sorts of things are commonly exposed by inlining and macro expansion. A more important optimization that this allows is for "<=" loops like this:
>
> for (i = 0; i <= N; ++i) { ... }
>
> In this loop, the compiler can assume that the loop will iterate exactly N+1 times if "i" is undefined on overflow, which allows a broad range of loop optimizations to kick in. On the other hand, if the variable is defined to wrap around on overflow, then the compiler must assume that the loop is possibly infinite (which happens if N is INT_MAX) - which then disables these important loop optimizations. This particularly affects 64-bit platforms since so much code uses "int" as induction variables.
> Makes evil sense :)
>
Ah yes.. integer types in C and implicit conversion between
signed/unsigned/different bit size..
Yeah.. a huge field for bugs and frustration.
>>
>> Best regards
>> Stefan
>>
>
>
>
> --
> best,
> Eliot
>
>
--
Best regards,
Igor Stasenko AKA sig.
More information about the Vm-dev
mailing list