www.digitalmars.com         C & C++   DMDScript  

D - overflow

reply Tintor Marko <elud verat.net> writes:
How to check for overflow in integer operations?

void main()
{
	int a,b,c;
	scanf("%ld %ld %ld", &a, &b, &c);
	int d=a*b+c;
	if(overflow) printf("error\n"); else printf("%ld",d);
}
Dec 04 2003
parent reply Tintor Marko <elud verat.net> writes:
On Thu, 04 Dec 2003 10:48:58 +0100, Tintor Marko <elud verat.net> wrote:

 How to check for overflow in integer operations?

 void main()
 {
 	int a,b,c;
 	scanf("%ld %ld %ld", &a, &b, &c);
 	int d=a*b+c;
 	if(overflow) printf("error\n"); else printf("%ld",d);
 }

or something like: void main() { int a,b,c; scanf("%ld %ld %ld", &a, &b, &c); try printf("%ld",a*b+c); catch(OverflowException) printf("error"); } Can overflow chacking be done in D?
Dec 04 2003
next sibling parent reply "Walter" <walter digitalmars.com> writes:
"Tintor Marko" <elud verat.net> wrote in message
news:oprznpyvm1lucekh news.digitalmars.com...
 On Thu, 04 Dec 2003 10:48:58 +0100, Tintor Marko <elud verat.net> wrote:

 How to check for overflow in integer operations?

 void main()
 {
 int a,b,c;
 scanf("%ld %ld %ld", &a, &b, &c);
 int d=a*b+c;
 if(overflow) printf("error\n"); else printf("%ld",d);
 }

or something like: void main() { int a,b,c; scanf("%ld %ld %ld", &a, &b, &c); try printf("%ld",a*b+c); catch(OverflowException) printf("error"); } Can overflow chacking be done in D?

Not directly in the language any more than it can be done in C. You could do it with inline assembler, though.
Dec 07 2003
parent reply Roberto Mariottini <Roberto_member pathlink.com> writes:
In article <br15np$834$1 digitaldaemon.com>, Walter says...
"Tintor Marko" <elud verat.net> wrote in message
news:oprznpyvm1lucekh news.digitalmars.com...

 Can overflow chacking be done in D?

Not directly in the language any more than it can be done in C. You could do it with inline assembler, though.

What are the difficulty to do it in the language? Why no language I know give the opportunity to catch overflows, while all CPU (thus assembly) do? Ciao
Dec 09 2003
parent reply "Walter" <walter digitalmars.com> writes:
"Roberto Mariottini" <Roberto_member pathlink.com> wrote in message
news:br46c8$1q5j$1 digitaldaemon.com...
 In article <br15np$834$1 digitaldaemon.com>, Walter says...
"Tintor Marko" <elud verat.net> wrote in message
news:oprznpyvm1lucekh news.digitalmars.com...

 Can overflow chacking be done in D?

Not directly in the language any more than it can be done in C. You could


it with inline assembler, though.

What are the difficulty to do it in the language? Why no language I know give the opportunity to catch overflows, while all

 (thus assembly) do?

Good question. The answer is probably the difficulty in coming up with a sensible syntax for the carry flag.
Dec 09 2003
next sibling parent Georg Wrede <Georg_member pathlink.com> writes:
In article <br52km$7da$1 digitaldaemon.com>, Walter says...
"Roberto Mariottini" <Roberto_member pathlink.com> wrote in message
news:br46c8$1q5j$1 digitaldaemon.com...
 In article <br15np$834$1 digitaldaemon.com>, Walter says...
"Tintor Marko" <elud verat.net> wrote in message
news:oprznpyvm1lucekh news.digitalmars.com...

 Can overflow chacking be done in D?

You could do it with inline assembler, though.


up with a sensible syntax for the carry flag.

Hmm, isn't it more because of perceived performance issues? In non-release code this would be quite a welcome thing. We would not even need any syntax, integer overflow could throw an Error.
Dec 10 2003
prev sibling next sibling parent reply Roberto Mariottini <Roberto_member pathlink.com> writes:
In article <br52km$7da$1 digitaldaemon.com>, Walter says...

Good question. The answer is probably the difficulty in coming up with a
sensible syntax for the carry flag.

What about: try { .. x = checkoverflow ( y * w + z ); .. } catch (OverflowError oe) { .. } The "checkoverflow" keyword will throw an OverflowError if the expression generates an integer overflow (the carry/overflow flag is on). Note: - Any better idea about the "checkoverflow" name?. - Can it be an ordinary function (implemented in assembly)? - It should be limited to integers or can be extended to floating points? - What about a "try" extension? i.e.: overflow { .. } catch(...) .. - And a version-like statement? i.e.: // begin of file overflow { // here all integer calculations throw an OverflowError .. // the whole file skipped } // end of file Ciao
Dec 12 2003
parent reply Roberto Mariottini <Roberto_member pathlink.com> writes:
I realized that maybe an ordinary function like checkoverflow can be already
done with inline assembly. Is it true?

Ciao

In article <brc43p$1r8d$1 digitaldaemon.com>, Roberto Mariottini says...
In article <br52km$7da$1 digitaldaemon.com>, Walter says...

Good question. The answer is probably the difficulty in coming up with a
sensible syntax for the carry flag.

What about: try { .. x = checkoverflow ( y * w + z ); .. } catch (OverflowError oe) { .. } The "checkoverflow" keyword will throw an OverflowError if the expression generates an integer overflow (the carry/overflow flag is on). Note: - Any better idea about the "checkoverflow" name?. - Can it be an ordinary function (implemented in assembly)? - It should be limited to integers or can be extended to floating points? - What about a "try" extension? i.e.: overflow { .. } catch(...) .. - And a version-like statement? i.e.: // begin of file overflow { // here all integer calculations throw an OverflowError .. // the whole file skipped } // end of file Ciao

Dec 16 2003
parent reply Ilya Minkov <minkov cs.tum.edu> writes:
Roberto Mariottini wrote:
 I realized that maybe an ordinary function like checkoverflow can be already
 done with inline assembly. Is it true?

No, since you cannot rely upon execution order, and the compiler is free to insert any stuff between the statements it likes, which is very likely to zero the flags. The syntax must thus imply which operation *exactly* has to be checked. I would guess something like int bla = overloadguard(expr); bla gets assigned the result of expression, and the outermost operation is checked in expr against overflow. Or maybe only allow expr to be an atomic expression, do not allow nesting. -eye
Dec 16 2003
next sibling parent J Anderson <REMOVEanderson badmama.com.au> writes:
Ilya Minkov wrote:

 Roberto Mariottini wrote:

 I realized that maybe an ordinary function like checkoverflow can be 
 already
 done with inline assembly. Is it true?

No, since you cannot rely upon execution order, and the compiler is free to insert any stuff between the statements it likes, which is very likely to zero the flags. The syntax must thus imply which operation *exactly* has to be checked. I would guess something like int bla = overloadguard(expr); bla gets assigned the result of expression, and the outermost operation is checked in expr against overflow. Or maybe only allow expr to be an atomic expression, do not allow nesting. -eye

Parhaps someone could just define add(), mul(), sub() functions which could *either* return an exception or be written like: int add(int l, int r, out bool overflow);
Dec 16 2003
prev sibling parent Roberto Mariottini <Roberto_member pathlink.com> writes:
In article <brndrl$1dnt$1 digitaldaemon.com>, Ilya Minkov says...
Roberto Mariottini wrote:
 I realized that maybe an ordinary function like checkoverflow can be already
 done with inline assembly. Is it true?

No, since you cannot rely upon execution order, and the compiler is free to insert any stuff between the statements it likes, which is very likely to zero the flags. The syntax must thus imply which operation *exactly* has to be checked. I would guess something like int bla = overloadguard(expr); bla gets assigned the result of expression, and the outermost operation is checked in expr against overflow. Or maybe only allow expr to be an atomic expression, do not allow nesting.

So you can have: int x = oguard(x + oguard(y * z)); If oguard is inline expanded, I think it will do the job. Do you have an idea of a possible implementation? Ciao
Dec 17 2003
prev sibling parent reply "Julio CÚsar Carrascal Urquijo" <adnoctum phreaker.net> writes:
"Walter" <walter digitalmars.com> wrote in message
news:br52km$7da$1 digitaldaemon.com...
 Good question. The answer is probably the difficulty in coming up with a
 sensible syntax for the carry flag.

And why not: try { x = a + b; } catch (OverflowException oe) { if (oe.carry) (...) }
Dec 12 2003
parent "Sean L. Palmer" <palmer.sean verizon.net> writes:
Because integer overflow is not normally an exceptional condition.  Not to
mention, why would you test an OverflowException for a carry flag?  It seems
if the exception is thrown, you wouldn't have to test the flag.  However I
wouldn't want the compiler looking for overflows all the time.  Doesn't
overflow bit stick around until it is cleared?

It might be better to put this kind of requirement into the type.  If we did
that, we'd end up with this variety of ints in the type system:

int_modular    // (the current int, no exception, wraps around to other
side)
int_checked   // (throws exception on overflow or wrap)
int_saturate    // (prevents overflow by saturating, but doesn't generate
exception)

Sean

"Julio CÚsar Carrascal Urquijo" <adnoctum phreaker.net> wrote in message
news:brcu9j$84$1 digitaldaemon.com...
 "Walter" <walter digitalmars.com> wrote in message
 news:br52km$7da$1 digitaldaemon.com...
 Good question. The answer is probably the difficulty in coming up with a
 sensible syntax for the carry flag.

And why not: try { x = a + b; } catch (OverflowException oe) { if (oe.carry) (...) }

Dec 12 2003
prev sibling parent reply Tintor Marko <elud verat.net> writes:
 How to check for overflow in integer operations?

 void main()
 {
 	int a,b,c;
 	scanf("%ld %ld %ld", &a, &b, &c);
 	int d=a*b+c;
 	if(overflow) printf("error\n"); else printf("%ld",d);
 }

or something like: void main() { int a,b,c; scanf("%ld %ld %ld", &a, &b, &c); try printf("%ld",a*b+c); catch(OverflowException) printf("error"); } Can overflow chacking be done in D?

How about this syntax for compiler directives (similar to atributes)? directive = (enable | disable) "(" identifier ")" (command | block | ":") example: enable(IntOverflowCheck) { int func(int a, int b, int c) { b++; // checking disable(IntOverflowCheck) a*=2; // not checking return a*b+c; // checking } } or like this: check(ArrayIndex) {} uncheck(ArrayIndex) {} check(IntOverflow) {}
Dec 17 2003
parent reply "Sean L. Palmer" <palmer.sean verizon.net> writes:
I would like having this much control.  And a standard way to set options
within the language would be nice...

Sean

"Tintor Marko" <elud verat.net> wrote in message
news:opr0cfaxdhlucekh news.digitalmars.com...
 How about this syntax for compiler directives (similar to atributes)?

 directive = (enable | disable) "(" identifier ")" (command | block | ":")

 example:

 enable(IntOverflowCheck)
 {
 int func(int a, int b, int c)
 {
 b++; // checking
 disable(IntOverflowCheck) a*=2; // not checking
 return a*b+c; // checking
 }
 }

 or like this:
 check(ArrayIndex) {}
 uncheck(ArrayIndex) {}
 check(IntOverflow) {}

Dec 17 2003
parent Patrick Down <Patrick_member pathlink.com> writes:
I would second this.  I would like a way to turn array bounds checking
on and off.


In article <brql4n$mck$1 digitaldaemon.com>, Sean L. Palmer says...
I would like having this much control.  And a standard way to set options
within the language would be nice...

Sean

"Tintor Marko" <elud verat.net> wrote in message
news:opr0cfaxdhlucekh news.digitalmars.com...
 How about this syntax for compiler directives (similar to atributes)?

 directive = (enable | disable) "(" identifier ")" (command | block | ":")

 example:

 enable(IntOverflowCheck)
 {
 int func(int a, int b, int c)
 {
 b++; // checking
 disable(IntOverflowCheck) a*=2; // not checking
 return a*b+c; // checking
 }
 }

 or like this:
 check(ArrayIndex) {}
 uncheck(ArrayIndex) {}
 check(IntOverflow) {}


Dec 17 2003