Mercury Bugs - mercury
View Issue Details
0000141mercuryBugpublic2010-04-01 15:372010-04-01 18:32
Reportermgiuca 
Assigned To 
PrioritynormalSeverityfeatureReproducibilityN/A
StatusnewResolutionopen 
PlatformOSOS Version
Product Version 
Target VersionFixed in Version 
Summary0000141: integer has no method for converting to int with wraparound
DescriptionThe integer.int function converts an integer to an int but aborts if the integer is out of range.

It is occasionally useful to convert an integer to an int with out-of-range numbers being wrapped (truncated) just like conversion from int to short in C, for example.

I have attached a patch which adds a function integer.int_with_overflow which does this.
Additional InformationThe patch sort-of relies upon two's complement (it would probably work for one's complement or sign-bit form as well, but does make some bit-level assumptions). I have included an XXX comment to this effect. I assume this is OK since there are other such comments in the same file.
TagsNo tags attached.
Attached Filespatch integer.patch (1,243) 2010-04-01 15:37
https://bugs.mercurylang.org/file_download.php?file_id=97&type=bug
patch integer-2.patch (1,249) 2010-04-01 18:09
https://bugs.mercurylang.org/file_download.php?file_id=98&type=bug

Notes
(0000258)
mgiuca   
2010-04-01 16:05   
Irk... that patch is wrong. The "Wrapped" value is not necessarily in int range (I thought it worked because conversion to int truncated anyway, but you can't assume that since it's specified as undefined.)

Positive integers in range 2^31 to 2^32-1 need to become negative, and vice versa for negative integers in range -2^31-1 to -2^32. This is tricky to do with integer bit operations since they specifically *don't* wrap values. I'm thinking it'll be easier to use mod/rem but I'll have to think hard about it. Does anyone have a better mathematical formula for wraparound conversion?

Also, is there a test suite I need to write test cases for?
(0000259)
mgiuca   
2010-04-01 16:14   
A valid formula appears to be (at least for 2's complement):

((X + int_max + 1) `mod` (int_max+1) * 2) - int_max - 1

Is this overly complicated?
(0000260)
mgiuca   
2010-04-01 18:10   
Added a new patch (integer-2.patch) which uses the above formula.

I'm aware that there is a formal patch review process, and this isn't it. I'm just seeing if anyone is interested first.
(0000261)
juliensf   
2010-04-01 18:32   
Hi Matt,

Yes, we're interested - could you please submit your patch to the mercury-reviews list.
(Or ask one of the developers to do so on your behalf)

The review process used in the Mercury project is documented here:
http://www.mercury.csse.unimelb.edu.au/information/doc-latest/reviews.html

Cheers,
Julien.

Issue History
2010-04-01 15:37mgiucaNew Issue
2010-04-01 15:37mgiucaFile Added: integer.patch
2010-04-01 16:05mgiucaNote Added: 0000258
2010-04-01 16:14mgiucaNote Added: 0000259
2010-04-01 18:09mgiucaFile Added: integer-2.patch
2010-04-01 18:10mgiucaNote Added: 0000260
2010-04-01 18:32juliensfNote Added: 0000261