Version française
Home     About     Download     Resources     Contact us    

This site is updated infrequently. For up-to-date information, please visit the new OCaml website at

Browse thread
if (n:int) < 0 then (-n) > 0 is FALSE
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: 2006-12-08 (11:09)
From: Mattias_Engdegård <mattias@v...>
Subject: Re: [Caml-list] if (n:int) < 0 then (-n) > 0 is FALSE
>> (Signed overflow is not legal in C (undefined behaviour), and a decent
>> compiler will warn this can be detected statically.)

s/warn/warn when/ (sorry)

>Hmmm. In the usual full word register operations most CPU
>have an overflow flag which a single conditional branch can
>detect. Given branch prediction the branch is unlikely to be
>taken the cost of detection is probably extremely small,
>provided you have control at the assembler level.

Even well-predicted branches are not free. They pollute the BTB, make
the code bigger (consuming I-cache), consume various resources
(decoding, instruction queue, ALU, rename registers etc) and as
integer operations are very common these all add up.

The ideal architecture in this regard would be one with optionally
trapping integer instructions, and very fast user-mode traps.

>Ocaml has slighly messier representation with one less
>bit, but i guess overflow detection wouldn't be so hard
>or all that expensive -- but I could be wrong, perhaps
>someone on Ocaml team has looked at this can can comment?

I suppose it wouldn't be impossible to add a compiler flag to change
the integer overflow semantics from wrapping to trapping.
Doing so for constant expressions (the original complaint in this thread)
would probably be less work.