3 ms·
> Arithmetic overflow? Two's complement I don't really want this either; I'd rather the program abort. The vast majority of situations where I'm using signed
by kelnos 3y ago
> Arithmetic overflow? Two's complement
I don't really want this either; I'd rather the program abort. The vast majority of situations where I'm using signed arithmetic, I never want a positive number to overflow to negative (and vice versa).
Unsigned arithmetic is already designed to wrap back to zero; that's useful for things like sequence numbers where wrapping is ok after you "run out".
- toast0 3y agoThis is possible, but unless processor accelerated comes at a great cost. You'd need a branch after every math, unless the compiler could prove the math wouldn't overflow. The ARM mode described elsewhere in the thread where there's an overflow flag that persists across operations would help; then you could do a check less frequently. A mode were you get a processor exception would be great, if adding that doesn't add significant cost to operations that don't overflow. Assuming the response to such an exception is expected to be a core dump, the cost of generating such an exception can be high; of course if someone builds their bignumber library around the exception, that won't be great.
- pdw 3y agoTrapping on integer overflow was completely standard before C came along. Fortran and Pascal compilers did it. (Lisp compilers transparently switched to bignums :)
- kccqzy 3y ago> You'd need a branch after every math A predictable branch is basically free. In your case such a branch is almost never taken.
- celeritascelery 3y ago"basically free" is very different then actually "free". Adding a ton of extra branches comes at a cost. Even though it is cheap it still takes an instructions that could be used for something else. You fill your branch prediction target cache must faster, ejecting branches you actually care about. It also makes it harder for the compiler to move code around and harder for the prefetcher to break data dependencies. This all adds up to non-trival overhead. You can tell your C compiler to always check arithmetic, but most don't because of the cost.
- deleted 3y ago[deleted]