<html>
<head>
<style><!--
.hmmessage P
{
margin:0px;
padding:0px
}
body.hmmessage
{
font-size: 10pt;
font-family:Verdana
}
--></style>
</head>
<body class='hmmessage'>
I *suspect* the "real" "big" problem is that left and right shift and "plain" shift should take a size.<BR>
<BR>
- Jay<BR><BR><BR> <BR>
<HR id=stopSpelling>
From: jay.krell@cornell.edu<BR>To: hosking@cs.purdue.edu; m3devel@elegosoft.com<BR>Subject: opacity of Target.Int, overflow of Target.Int<BR>Date: Sun, 21 Feb 2010 10:31:06 +0000<BR><BR>
<STYLE>
.ExternalClass .ecxhmmessage P
{padding:0px;}
.ExternalClass body.ecxhmmessage
{font-size:10pt;font-family:Verdana;}
</STYLE>
1) Tony, I claim that if Target.Int is really opaque, then values of it should only be formed in Target.i3/Target.m3.<BR>Or TInt/TWord/.i3/.m3, since they know deeply about it.<BR>Having m3back know about them seems a bit off.<BR>Granted, pushing constants that only m3back uses into Target/TInt/TWord isn't great either, pollutes them some.<BR>This is a minor dilemna though.<BR> <BR> <BR>2) Are you sure having a fixed precision of 8 bytes is correct here?<BR>More generally are you sure TInt has the right interface?<BR>How does one check for overflow within the precision one cares about?<BR>I think I know.<BR>I think Chop should return a boolean, as to if the value fits.<BR>For that matter, have SignedChop and UnsignedChop.<BR> Or TInt.Chop and TWord.Chop.<BR>TInt.Chop is already SignedChop.<BR> <BR>Do the math in 64bits, if that overflows, then overflow.<BR>And then truncate to whatever, and that can overflow too.<BR>This is related to "you all have convinced me that overflow isn't<BR>all that important really, but range checking is".<BR>So you in sense defer the overflow. You don't do the complete<BR>overflow check at Add or whatever, but then you do a range<BR>check converting to a small size.<BR> <BR>But hm. This will skip "intermediate overflow". Does that matter?<BR>1 billion * 8 / 8<BR> <BR> <BR>Should that overflow in 32bits?<BR> <BR> <BR>Well, easy enough, do the chop after each operation.<BR>I said "defer", but it is brief.<BR>1B * 8 / 8<BR>isn't written as<BR>1* 8 / 8<BR>convert to 32bits<BR> <BR>but rather<BR>1B * 8<BR>convert to 32bits<BR> => failure here <BR>/ 8<BR>convert to 32bits<BR> <BR>?<BR> <BR> <BR>3) Related to #2.<BR>Where should RightShift insert zeros?<BR>I guess you chop first, and then it works.<BR> <BR> <BR>Proposed something like:<BR> <BR><BR>TInt:<BR>PROCEDURE Chop (VAR r: Int; n: CARDINAL): BOOLEAN =<BR> VAR extension := Mask * ORD(And (r [n-1], SignMask) = 0);<BR> result := TRUE;<BR> BEGIN<BR> FOR i := n TO LAST(Int) DO<BR> result := (result AND (r[i] # extension));<BR> r[i] := extension;<BR> END;<BR> return result;<BR> END Chop;<BR><BR>TWord:<BR>PROCEDURE Chop (VAR r: Int; n: CARDINAL): BOOLEAN =<BR> CONST extension = 0;<BR> VAR result := TRUE;<BR> BEGIN<BR> FOR i := n TO LAST(Int) DO<BR> result := (result AND (r[i] # extension));<BR> r[i] := extension;<BR> END;<BR> return result;<BR> END Chop;<BR><BR> <BR> <BR>But I have to back to your version of m3back and try these out.<BR>Very speculative at the moment.<BR> <BR>Could also leave the data alone upon failure, trivially related to previous:<BR> <BR>TInt:<BR>PROCEDURE Chop (VAR r: Int; n: CARDINAL): BOOLEAN =<BR> VAR extension := Mask * ORD(And (r [n-1], SignMask) = 0);<BR> BEGIN<BR> FOR i := n TO LAST(Int) DO<BR> IF r[i] # extension THEN<BR> RETURN FALSE;<BR> END;<BR> END;<BR> FOR i := n TO LAST(Int) DO<BR> r[i] := extension;<BR> END;<BR> RETURN TRUE;<BR> END Chop;<BR><BR> <BR>TWord:<BR>PROCEDURE Chop (VAR r: Int; n: CARDINAL): BOOLEAN =<BR> VAR result := TRUE;<BR> BEGIN<BR> FOR i := n TO LAST(Int) DO<BR> IF r[i] # 0 THEN<BR> RETURN FALSE;<BR> END;<BR> END;<BR> FOR i := n TO LAST(Int) DO<BR> r[i] := 0;<BR> END;<BR> return TRUE;<BR> END Chop;<BR><BR> <BR>I'm not sure I like the subtlety of TInt.Chop vs. TWord.Chop.<BR>I'd prefer TInt.UnsignedChop and TInt.SignedChop.<BR>Granted, "Un" doesn't exactly stand out, but at least "signed" does and makes you wonder about and discover the existance of signed vs. unsigned.<BR> <BR> <BR>Alternatively Chop and UChop. More subtle, but I think plenty programmars are used to the names "uchar", "ushort", "uint", "ulong". "u" is pretty widely recognized as meaning "unsigned". More so I believe than "Word".<BR>(I still don't like the name "interface Long"!, it doesn't imply unsigned at all!)<BR> <BR> <BR> - Jay<BR> </body>
</html>