disabling unary "-" for unsigned types

bearophile bearophileHUGS at lycos.com
Tue Feb 16 10:36:13 PST 2010


Ellery Newcomer:

> OT: has anyone written a wrapper for int/long/etc that throws exceptions 
> on overflow/underflow? Maybe such a thing should exist in the standard 
> library?

No, that idea is trash. Think about arrays, do you want to litter your code with a different type of array that test the index bounds? Surely not. You want the built-in arrays to test the bounds (with eventually a compilation option to disable such tests), as in D. The same is true for integral numbers, as done in Delphi/C#.

-----------------

Adam D. Ruppe:

> 	T opAdd(T a) {
> 		T tmp = _payload + a;
> 		asm { jo overflow; }
> 		return tmp;
> 		overflow:
> 			throw new Exception("Overflow");
> 	}

There are some problems with that. Currently a struct can't used to fully replace a number, for example you can't do if(a) yet.

LDC has problems with gotos outside/inside the asm block:
Gotos into inline assembly: For labels inside inline asm blocks, the D spec says "They can be the target of goto statements.", this is not supported at the moment. Basically, LLVM does not allow jumping in to or out of an asm block. We work around this for jumping out of asm by converting these branches to assignments to a temporary that is then used in a switch statement right after the inline asm block to jump to the final destination. This same workaround could be applied for jumping into inline assembly. 


Another problem is that dmd will not inline those little methods (ldc can be forced to do it). I want some efficiency in such safer operations too, otherwise they become less useful.

Another problem with micro blocks of ASM in D is that the compiler is not always good at managing it, so it can use the stack and registers in a suboptimal way. In LDC they have invented asm Constraints to solve this problem:
http://www.dsource.org/projects/ldc/wiki/InlineAsmExpressions

Bye,
bearophile



More information about the Digitalmars-d mailing list