Re: Optimization techniques and undefined behavior

Andy Walker <anw@cuboid.co.uk>
Thu, 2 May 2019 11:29:30 +0100

          From comp.compilers

Related articles
[6 earlier articles]
Re: Optimization techniques and undefined behavior auriocus@gmx.de (Christian Gollwitzer) (2019-04-29)
Re: Optimization techniques and undefined behavior bc@freeuk.com (Bart) (2019-04-29)
Re: Optimization techniques and undefined behavior david.brown@hesbynett.no (David Brown) (2019-04-30)
Re: Optimization techniques and undefined behavior david.brown@hesbynett.no (David Brown) (2019-04-30)
Re: Optimization techniques and undefined behavior bc@freeuk.com (Bart) (2019-05-01)
Re: Optimization techniques and undefined behavior bc@freeuk.com (Bart) (2019-05-01)
Re: Optimization techniques and undefined behavior anw@cuboid.co.uk (Andy Walker) (2019-05-02)
Re: Optimization techniques and undefined behavior martin@gkc.org.uk (Martin Ward) (2019-05-02)
Re: Optimization techniques and undefined behavior david.brown@hesbynett.no (David Brown) (2019-05-02)
Re: Optimization techniques and undefined behavior david.brown@hesbynett.no (David Brown) (2019-05-02)
Re: Optimization techniques and undefined behavior 847-115-0292@kylheku.com (Kaz Kylheku) (2019-05-02)
Re: Optimization techniques and undefined behavior bc@freeuk.com (Bart) (2019-05-02)
Re: Optimization techniques and undefined behavior bc@freeuk.com (Bart) (2019-05-02)
[49 later articles]
| List of all articles for this month |
From: Andy Walker <anw@cuboid.co.uk>
Newsgroups: comp.compilers
Date: Thu, 2 May 2019 11:29:30 +0100
Organization: Not very much
References: <72d208c9-169f-155c-5e73-9ca74f78e390@gkc.org.uk> 19-04-021 19-04-023 19-04-037 19-04-039 19-04-042 19-04-044 19-04-047 19-05-004
Injection-Info: gal.iecc.com; posting-host="news.iecc.com:2001:470:1f07:1126:0:676f:7373:6970"; logging-data="65862"; mail-complaints-to="abuse@iecc.com"
Keywords: arithmetic, optimize, errors
Posted-Date: 02 May 2019 12:08:01 EDT
Content-Language: en-GB

On 01/05/2019 13:53, Bart wrote:
> If you have two unknown values A and B, and need to multiply, you won't
> know if the result will overflow.


        int A := ..., B := ...;
        int C := ( A = 0 | 0 |: abs B <= maxint % abs A | A*B | error (...); 0 );


E&OE. Convert to your own favourite language. Simplifies if you
already know that A and B are strictly positive.


Before anyone asks, yes, I know that can throw an unwonted error,
eg if A = -maxint-1 and B = 1; my view is that anyone doing arithmetic
should respect "maxint" and not exploit the vagaries of 2s-complement
arithmetic. If you really want to do that, use "unsigned" [or "bits"]
values properly, rather than "int".


Of course, in the old days, compilers used to build in range
checks on array indices and overflow checks on all arithmetic. A few
still do, esp interpreters, but the God of Speed dictates that most
languages in most circumstances don't. We see the results in the huge
amount of malware that exploits that failure.


[...]
> In the example posed, you have the additional problem that the input can
> be this:
>    P5
>    389000000000000000000000000000 9200000000000000000000000000
> with both dimensions exceeding int64.


My own favourite language will throw an "on value error" exception
if you try to read those values [or any other unsuitable strings] into an
integer variable. By default, that will terminate your program with
suitable error messages/diagnostics, but you can substitute your own
"on value error" procedure if you want to print a "Don't be daft, please
type sensible values" message and try again. But it's not hard to write
equivalent code in any half-sensible language by reading that line into
a string and parsing that. If you have a string of digits starting with
a non-zero, and a string corresponding to "maxint", then you have a good
value if either (a) the input string is shorter than that for "maxint" or
(b) it is the same length and alphabetically not larger.


--
Andy Walker,
Nottingham.


Post a followup to this message

Return to the comp.compilers page.
Search the comp.compilers archives again.