Watch, Follow, &
Connect with Us

Developer Tools Community.

Welcome, Guest
Guest Settings

Replies: 8 - Last Post: Dec 29, 2016 10:11 AM Last Post By: Rudy Velthuis (...
 Posts: 32 Registered: 9/23/99
 Integer overflow Posted: Dec 4, 2016 9:45 PM var a,b: Int64; begin a := -634494603; b := -1566836587; a := a * a; //OK b := b * b; //OK a := a * b: //integer overflow Tested using ... Delphi 10.1 (Berlin) Can anyone explain this?
 Posts: 14 Registered: 9/22/03
 Re: Integer overflow Posted: Dec 26, 2016 11:34 AM   in response to: Angus Johnson var a,b: Int64; (Max Value = 2^63) begin a := -634494603; b := -1566836587; a := a * a; //OK //a now equals 402583401236127609 (< 2^63) b := b * b; //OK //b now equals 2454976890361808569 (< 2^63) a := a * b: //integer overflow // a * b = (402583401236127609 * 2454976890361808569) -> this is bigger then 2^63 // try (2^63) - (402583401236127609 * 2454976890361808569) using calc.exe in scientific mode and you get a negative number. Angus Johnson wrote: var a,b: Int64; begin a := -634494603; b := -1566836587; a := a * a; //OK b := b * b; //OK a := a * b: //integer overflow Tested using ... Delphi 10.1 (Berlin) Can anyone explain this?
 Posts: 32 Registered: 9/23/99
 Re: Integer overflow Posted: Dec 26, 2016 12:25 PM   in response to: Nigel Tavendale On 27/12/2016 6:34 AM, Nigel Tavendale wrote: var a,b: Int64; (Max Value = 2^63) begin a := -634494603; b := -1566836587; a := a * a; //OK //a now equals 402583401236127609 (< 2^63) b := b * b; //OK //b now equals 2454976890361808569 (< 2^63) a := a * b: //integer overflow // a * b = (402583401236127609 * 2454976890361808569) -> this is bigger then 2^63 Yes, but the point I was making (though evidently not very clearly) was this: {\$OVERFLOWCHECKS ON} var a,b: Int64; begin a := -634494603; b := -1566836587; a := a * b; //this is <2^63 but still raises an overflow exception
 Posts: 626 Registered: 10/8/06
 Re: Integer overflow Posted: Dec 27, 2016 5:53 AM   in response to: Angus Johnson Angus Johnson wrote: {\$OVERFLOWCHECKS ON} var a,b: Int64; begin a := -634494603; b := -1566836587; a := a * b; //this is <2^63 but still raises an overflow exception What is target platform (32, 64)? It would be interesting to see generated code (set breakpoint on "a:=...", run, press Ctrl+Alt+C to see CPU window, select and copy disassembled code with Ctrl+C). -- Alex
 Posts: 7,731 Registered: 9/22/99
 Re: Integer overflow Posted: Dec 28, 2016 3:02 AM   in response to: Alex Belo Alex Belo wrote: Angus Johnson wrote: {\$OVERFLOWCHECKS ON} var a,b: Int64; begin a := -634494603; b := -1566836587; a := a * b; //this is <2^63 but still raises an overflow exception What is target platform (32, 64)? It would be interesting to see generated code (set breakpoint on "a:=...", run, press Ctrl+Alt+C to see CPU window, select and copy disassembled code with Ctrl+C). In Win32, System.__llmulo is used. This, of course, uses 32 bit registers so several multiplications are performedto emulate 64 bit registers. This is buggy and sometimes returns the wrong overflow flag (the result of the multiplications is correct, BTW). In Win64, the multiplication is done directly using 64 bit registers (no call to __llmulo), and the processor of course returns the correct overflow flag. This is being worked on. -- Rudy Velthuis http://www.rvelthuis.de Canada Bill Jones' Motto: It's morally wrong to allow naive end users to keep their money.
 Posts: 7,731 Registered: 9/22/99
 Re: Integer overflow Posted: Dec 28, 2016 2:59 AM   in response to: Angus Johnson Angus Johnson wrote: On 27/12/2016 6:34 AM, Nigel Tavendale wrote: var a,b: Int64; (Max Value = 2^63) begin a := -634494603; b := -1566836587; a := a * a; //OK //a now equals 402583401236127609 (< 2^63) b := b * b; //OK //b now equals 2454976890361808569 (< 2^63) a := a * b: //integer overflow // a * b = (402583401236127609 * 2454976890361808569) -> this is bigger then 2^63 Yes, but the point I was making (though evidently not very clearly) was this: {\$OVERFLOWCHECKS ON} var a,b: Int64; begin a := -634494603; b := -1566836587; a := a * b; //this is <2^63 but still raises an overflow exception Yes, this is a known problem with System._llmulo, which returns the wrong overflow flag. Doesn't happen in Win64 (where a direct multiplication is done, and _llmulo is not used) or when overflow checks are off. This was reported in QC: http://qc.embarcadero.com/wc/qcmain.aspx?d=34049 This was obviously never fixed. I raised this issue with Embarcadero internally and they are looking at it. -- Rudy Velthuis http://www.rvelthuis.de "Never does nature say one thing and wisdom another." -- Juvenal
 Posts: 32 Registered: 9/23/99
 Re: Integer overflow Posted: Dec 28, 2016 7:11 AM   in response to: Rudy Velthuis (... On 28/12/2016 9:59 PM, Rudy Velthuis (TeamB) wrote: This was reported in QC: http://qc.embarcadero.com/wc/qcmain.aspx?d=34049 This was obviously never fixed. I raised this issue with Embarcadero internally and they are looking at it. Thanks Rudy, much appreciated.
 Posts: 7,731 Registered: 9/22/99
 Re: Integer overflow Posted: Dec 29, 2016 10:11 AM   in response to: Angus Johnson Angus Johnson wrote: On 28/12/2016 9:59 PM, Rudy Velthuis (TeamB) wrote: This was reported in QC: http://qc.embarcadero.com/wc/qcmain.aspx?d=34049 This was obviously never fixed. I raised this issue with Embarcadero internally and they are looking at it. Thanks Rudy, much appreciated. Also in QP now: https://quality.embarcadero.com/browse/RSP-16617 -- Rudy Velthuis http://www.rvelthuis.de "In order to learn, one must change one's mind." -- Orson Scott Card
 Posts: 7,731 Registered: 9/22/99
 Re: Integer overflow Posted: Dec 28, 2016 3:47 AM   in response to: Angus Johnson Angus Johnson wrote: var a,b: Int64; begin a := -634494603; b := -1566836587; a := a * a; //OK b := b * b; //OK a := a * b: //integer overflow Tested using ... Delphi 10.1 (Berlin) Can anyone explain this? I reported it (but to QP now, not QC), see: https://quality.embarcadero.com/browse/RSP-16617 -- Rudy Velthuis (TeamB) http://www.teamb.com
Legend